From 0a9d94c1c06fbae476dadb7171f565bb213eed64 Mon Sep 17 00:00:00 2001 From: Jeeves Date: Tue, 9 Apr 2024 17:13:14 -0600 Subject: [PATCH] keyboard (sorta) --- src/main.zig | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main.zig b/src/main.zig index 2755e97..2901f79 100644 --- a/src/main.zig +++ b/src/main.zig @@ -52,7 +52,6 @@ pub fn main() !void { while (true) { const events = try term.getEvents(); _ = events; - // for (events) |ev| if (ev.system == .int) break; try term.draw(); std.time.sleep(1000); } @@ -82,10 +81,12 @@ pub const Terminal = struct { try term.uncook(); try attachSignalHandlers(); try term.updateWinSize(); + try term.print("\x1b[>1u", .{}); return term; } pub fn deinit(self: *Terminal) void { + self.print("\x1b[ try self.events.append(ev), } }, - // else => try self.events.append(ev); + else => try self.events.append(ev), } S.ev = null; } + var n: usize = 0; + if (os.linux.ioctl(self.tty.handle, os.linux.T.FIONREAD, @intFromPtr(&n)) == 0 and n > 0) { + var key: [1]u8 = undefined; + _ = try self.tty.reader().read(&key); + try self.events.append(Event{ .keyboard = .{ .char = key[0] } }); + } return try self.events.toOwnedSlice(); } @@ -155,7 +162,7 @@ pub const Terminal = struct { fn handlerFn(sig: i32) callconv(.C) void { switch (sig) { - posix.SIG.INT => ev = Event{ .system = .int }, + // posix.SIG.INT => ev = Event{ .system = .int }, posix.SIG.USR1 => ev = Event{ .system = .usr1 }, posix.SIG.USR2 => ev = Event{ .system = .usr2 }, posix.SIG.WINCH => ev = Event{ .system = .winch }, @@ -166,11 +173,14 @@ pub const Terminal = struct { pub const Event = union(enum) { system: enum { - int, + // int, usr1, usr2, winch, }, + keyboard: struct { + char: u8, + }, }; fn updateWinSize(self: *Terminal) !void {