set character callback and update the terminal

This commit is contained in:
Mitchell Hashimoto
2022-04-19 13:10:50 -07:00
parent 4da0c789de
commit 19692f297e
2 changed files with 19 additions and 4 deletions

View File

@ -28,7 +28,7 @@ pub fn init(alloc: Allocator) !App {
} }
pub fn deinit(self: *App) void { pub fn deinit(self: *App) void {
self.window.destroy(self.alloc); self.window.destroy();
self.* = undefined; self.* = undefined;
} }

View File

@ -16,6 +16,9 @@ const Terminal = @import("terminal/Terminal.zig");
const log = std.log.scoped(.window); const log = std.log.scoped(.window);
/// Allocator
alloc: Allocator,
/// The glfw window handle. /// The glfw window handle.
window: glfw.Window, window: glfw.Window,
@ -92,6 +95,7 @@ pub fn create(alloc: Allocator) !*Window {
try term.append(alloc, "hello!\r\nworld!"); try term.append(alloc, "hello!\r\nworld!");
self.* = .{ self.* = .{
.alloc = alloc,
.window = window, .window = window,
.grid = grid, .grid = grid,
.pty = pty, .pty = pty,
@ -101,16 +105,17 @@ pub fn create(alloc: Allocator) !*Window {
// Setup our callbacks and user data // Setup our callbacks and user data
window.setUserPointer(self); window.setUserPointer(self);
window.setSizeCallback(sizeCallback); window.setSizeCallback(sizeCallback);
window.setCharCallback(charCallback);
return self; return self;
} }
pub fn destroy(self: *Window, alloc: Allocator) void { pub fn destroy(self: *Window) void {
self.terminal.deinit(alloc); self.terminal.deinit(self.alloc);
self.pty.deinit(); self.pty.deinit();
self.grid.deinit(); self.grid.deinit();
self.window.destroy(); self.window.destroy();
alloc.destroy(self); self.alloc.destroy(self);
} }
pub fn run(self: Window) !void { pub fn run(self: Window) !void {
@ -159,3 +164,13 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void {
gl.viewport(0, 0, width, height) catch |err| gl.viewport(0, 0, width, height) catch |err|
log.err("error updating OpenGL viewport err={}", .{err}); log.err("error updating OpenGL viewport err={}", .{err});
} }
fn charCallback(window: glfw.Window, codepoint: u21) void {
const win = window.getUserPointer(Window) orelse return;
// Append this character to the terminal
win.terminal.appendChar(win.alloc, @intCast(u8, codepoint)) catch unreachable;
// Update the cells for drawing
win.grid.updateCells(win.terminal) catch unreachable;
}