From 2be2dc6ac7b041a4f53f72b348651a469dc57e77 Mon Sep 17 00:00:00 2001 From: Janis Date: Fri, 16 Dec 2022 13:10:22 +0100 Subject: [PATCH] behold, 'q' and 'esc' --- main.zig | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/main.zig b/main.zig index b5cb6c4..09721b8 100644 --- a/main.zig +++ b/main.zig @@ -158,6 +158,10 @@ fn ListItem(comptime T: type) type { return @fieldParentPtr(Self, "link", link); } + fn fromValue(value: *T) *Self { + return @fieldParentPtr(Self, "value", value); + } + /// automatically calls `deinit()` on `data` if a function with that name exists fn remove(self: *Self) void { if (@hasDecl(T, "deinit")) { @@ -316,7 +320,9 @@ const Output = struct { layer.ackConfigure(cfg.serial); // TODO: send frame }, - else => {}, + .closed => { + self.state.removeOutput(self); + }, } } @@ -431,7 +437,28 @@ const Seat = struct { } }, .key => |key| { - std.debug.print("key: {} {s}", .{key.key, if (key.state == .pressed) "pressed" else "released"}); + if (self.keyboard.?.xkb) |xkb_| { + const keysym = xkb.xkb_state_key_get_one_sym(xkb_.state, key.key + 8); + + const buf_len = 32; + const buf = self.state.ally.alloc(u8, buf_len) catch null; + if (buf) |buffer| { + _ = xkb.xkb_keysym_get_name(keysym, @ptrCast([*c]u8, buffer), buf_len); + std.debug.print("key: {s} {s}\n", .{buffer, if (key.state == .pressed) "pressed" else "released"}); + } + switch (key.state) { + .pressed => { + switch (keysym) { + xkb.XKB_KEY_Escape, xkb.XKB_KEY_q => { + self.state.running = false; + }, + else => {}, + } + }, + .released => {}, + else => {}, + } + } }, else => {}, } @@ -634,6 +661,8 @@ const State = struct { ally: std.mem.Allocator = std.heap.c_allocator, + running: bool = true, + dpy: *wl.Display = undefined, registry: *wl.Registry = undefined, @@ -747,13 +776,18 @@ const State = struct { output.surface.attach(buffer, 0, 0); output.surface.commit(); - while (true) { + while (self.running) { if (self.dpy.dispatch() != .SUCCESS) return error.DispatchFailed; - std.debug.print("asdf\n", .{}); } } } + fn removeOutput(self: *Self, output: *Output) void { + const link = ListItem(Output).fromValue(output); + link.remove(); + self.ally.destroy(link); + } + fn deinit(self: *Self) void { defer self.ally.destroy(self);