behold, 'q' and 'esc'

This commit is contained in:
Janis 2022-12-16 13:10:22 +01:00
parent 24796ba50d
commit 2be2dc6ac7

View file

@ -158,6 +158,10 @@ fn ListItem(comptime T: type) type {
return @fieldParentPtr(Self, "link", link); 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 /// automatically calls `deinit()` on `data` if a function with that name exists
fn remove(self: *Self) void { fn remove(self: *Self) void {
if (@hasDecl(T, "deinit")) { if (@hasDecl(T, "deinit")) {
@ -316,7 +320,9 @@ const Output = struct {
layer.ackConfigure(cfg.serial); layer.ackConfigure(cfg.serial);
// TODO: send frame // TODO: send frame
}, },
else => {}, .closed => {
self.state.removeOutput(self);
},
} }
} }
@ -431,7 +437,28 @@ const Seat = struct {
} }
}, },
.key => |key| { .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 => {}, else => {},
} }
@ -634,6 +661,8 @@ const State = struct {
ally: std.mem.Allocator = std.heap.c_allocator, ally: std.mem.Allocator = std.heap.c_allocator,
running: bool = true,
dpy: *wl.Display = undefined, dpy: *wl.Display = undefined,
registry: *wl.Registry = undefined, registry: *wl.Registry = undefined,
@ -747,13 +776,18 @@ const State = struct {
output.surface.attach(buffer, 0, 0); output.surface.attach(buffer, 0, 0);
output.surface.commit(); output.surface.commit();
while (true) { while (self.running) {
if (self.dpy.dispatch() != .SUCCESS) return error.DispatchFailed; 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 { fn deinit(self: *Self) void {
defer self.ally.destroy(self); defer self.ally.destroy(self);