Keyboard: style/naming nits and comment tweaks

This commit includes no functional changes
This commit is contained in:
Isaac Freund 2024-02-18 17:27:51 +01:00
parent 93c2a2fd08
commit d1bb27038b
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
2 changed files with 22 additions and 31 deletions

View File

@ -38,35 +38,30 @@ const KeyConsumer = enum {
focus, focus,
}; };
const KeySet = struct { pub const Pressed = struct {
const Key = struct { const Key = struct {
code: u32, code: u32,
consumer: KeyConsumer, consumer: KeyConsumer,
}; };
items: std.BoundedArray(Key, 32) = .{}, pub const capacity = 32;
comptime { comptime {
// The same magic number is also used in Seat.keyboardNotifyEnter() // wlroots uses a buffer of length 32 for pressed keys and asserts that it does not overflow.
assert(@typeInfo(std.meta.fieldInfo( assert(capacity == @typeInfo(std.meta.fieldInfo(wlr.Keyboard, .keycodes).type).Array.len);
std.meta.fieldInfo(KeySet, .items).type,
.buffer,
).type).Array.len ==
@typeInfo(std.meta.fieldInfo(wlr.Keyboard, .keycodes).type).Array.len);
} }
pub fn add(keyset: *KeySet, new: Key) error{Overflow}!void { keys: std.BoundedArray(Key, capacity) = .{},
for (keyset.items.constSlice()) |item| assert(new.code != item.code);
keyset.items.append(new) catch |err| { pub fn add(pressed: *Pressed, new: Key) void {
log.err("KeySet limit reached, code {d} omitted", .{new.code}); for (pressed.keys.constSlice()) |item| assert(new.code != item.code);
return err;
}; pressed.keys.append(new) catch unreachable; // see comptime assert above
} }
pub fn remove(keyset: *KeySet, code: u32) ?KeyConsumer { pub fn remove(pressed: *Pressed, code: u32) ?KeyConsumer {
for (keyset.items.constSlice(), 0..) |item, idx| { for (pressed.keys.constSlice(), 0..) |item, idx| {
if (item.code == code) return keyset.items.swapRemove(idx).consumer; if (item.code == code) return pressed.keys.swapRemove(idx).consumer;
} }
return null; return null;
@ -75,8 +70,8 @@ const KeySet = struct {
device: InputDevice, device: InputDevice,
/// Pressed keys along with where they've been sent /// Pressed keys along with where their press event has been sent
keycodes: KeySet = .{}, pressed: Pressed = .{},
key: wl.Listener(*wlr.Keyboard.event.Key) = wl.Listener(*wlr.Keyboard.event.Key).init(handleKey), key: wl.Listener(*wlr.Keyboard.event.Key) = wl.Listener(*wlr.Keyboard.event.Key).init(handleKey),
modifiers: wl.Listener(*wlr.Keyboard) = wl.Listener(*wlr.Keyboard).init(handleModifiers), modifiers: wl.Listener(*wlr.Keyboard) = wl.Listener(*wlr.Keyboard).init(handleModifiers),
@ -176,18 +171,18 @@ fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboa
if (!released and handleBuiltinMapping(sym)) return; if (!released and handleBuiltinMapping(sym)) return;
} }
// Every sent press event, to a regular client or the IM, should have // Every sent press event, to a regular client or the input method, should have
// the corresponding release event sent to the same client. // the corresponding release event sent to the same client.
// Similarly, no press event means no release event. // Similarly, no press event means no release event.
const consumer: KeyConsumer = blk: { const consumer: KeyConsumer = blk: {
// Decision is made on press; release only follows it // Decision is made on press; release only follows it
if (released) break :blk self.keycodes.remove(event.keycode) orelse unreachable; if (released) break :blk self.pressed.remove(event.keycode).?;
if (self.device.seat.hasMapping(keycode, modifiers, released, xkb_state)) { if (self.device.seat.hasMapping(keycode, modifiers, released, xkb_state)) {
// We cannot handle the mapping before we know if the key gets saved, // The key must be added to the set of pressed keys with the correct consumer before
// in order to pair press/release mappings as expected (and for // the mapping is run. Otherwise, the mapping may, for example, trigger a focus change
// consistency and code simplicity) // which sends an incorrect wl_keyboard.enter event.
break :blk .mapping; break :blk .mapping;
} else if (self.getInputMethodGrab() != null) { } else if (self.getInputMethodGrab() != null) {
break :blk .im_grab; break :blk .im_grab;
@ -196,11 +191,7 @@ fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboa
break :blk .focus; break :blk .focus;
}; };
if (!released) self.keycodes.add(.{ .code = event.keycode, .consumer = consumer }) catch { if (!released) self.pressed.add(.{ .code = event.keycode, .consumer = consumer });
// We've run out of capacity and cannot process the key correctly.
// If wlroots hasn't failed on a similar assertion, this logic needs to be updated.
unreachable;
};
switch (consumer) { switch (consumer) {
.mapping => if (!released) { .mapping => if (!released) {

View File

@ -306,8 +306,8 @@ fn keyboardNotifyEnter(self: *Self, wlr_surface: *wlr.Surface) void {
if (self.wlr_seat.getKeyboard()) |wlr_keyboard| { if (self.wlr_seat.getKeyboard()) |wlr_keyboard| {
const keyboard: *Keyboard = @ptrFromInt(wlr_keyboard.data); const keyboard: *Keyboard = @ptrFromInt(wlr_keyboard.data);
var keycodes: std.BoundedArray(u32, 32) = .{}; var keycodes: std.BoundedArray(u32, Keyboard.Pressed.capacity) = .{};
for (keyboard.keycodes.items.constSlice()) |item| { for (keyboard.pressed.keys.constSlice()) |item| {
if (item.consumer == .focus) keycodes.appendAssumeCapacity(item.code); if (item.consumer == .focus) keycodes.appendAssumeCapacity(item.code);
} }