From 19692f297e650580c7e749fd82d5d5c71da40460 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 19 Apr 2022 13:10:50 -0700 Subject: [PATCH] set character callback and update the terminal --- src/App.zig | 2 +- src/Window.zig | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/App.zig b/src/App.zig index 4f3b4cc6a..3394c5c71 100644 --- a/src/App.zig +++ b/src/App.zig @@ -28,7 +28,7 @@ pub fn init(alloc: Allocator) !App { } pub fn deinit(self: *App) void { - self.window.destroy(self.alloc); + self.window.destroy(); self.* = undefined; } diff --git a/src/Window.zig b/src/Window.zig index 3c8c856b3..f6e7a5ca0 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -16,6 +16,9 @@ const Terminal = @import("terminal/Terminal.zig"); const log = std.log.scoped(.window); +/// Allocator +alloc: Allocator, + /// The glfw window handle. window: glfw.Window, @@ -92,6 +95,7 @@ pub fn create(alloc: Allocator) !*Window { try term.append(alloc, "hello!\r\nworld!"); self.* = .{ + .alloc = alloc, .window = window, .grid = grid, .pty = pty, @@ -101,16 +105,17 @@ pub fn create(alloc: Allocator) !*Window { // Setup our callbacks and user data window.setUserPointer(self); window.setSizeCallback(sizeCallback); + window.setCharCallback(charCallback); return self; } -pub fn destroy(self: *Window, alloc: Allocator) void { - self.terminal.deinit(alloc); +pub fn destroy(self: *Window) void { + self.terminal.deinit(self.alloc); self.pty.deinit(); self.grid.deinit(); self.window.destroy(); - alloc.destroy(self); + self.alloc.destroy(self); } 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| 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; +}