Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git
Keyboard: eat key before running mapping
To see why this is needed, compare the following flows:
- user: press key 'j' with Super already pressed
- Keyboard: handle mapping, focusing next view
- Seat: send wl_keyboard.enter with keys Super and 'j'
- Keyboard: eat key 'j'
versus:
- user: press key 'j' with Super already pressed
- Keyboard: eat key 'j'
- Keyboard: handle mapping, focusing next view
- Seat: send wl_keyboard.enter with key Super.
The necessity of this was already mentioned in 1e3b8ed1; however,
without a comment in code, it was removed in 393bfb42 as superfluous.
Hopefully, the newly added comment will prevent such mistakes in the
future.
Fixes https://github.com/riverwm/river/issues/978
river/Keyboard.zig | 11 +++++++++--
river/Seat.zig | 19 +++++++++++++++++++
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/river/Keyboard.zig b/river/Keyboard.zig
index cee1f3e..67dbbfd 100644
--- a/river/Keyboard.zig
+++ b/river/Keyboard.zig
@@ -143,7 +143,10 @@ fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboa
// We can only eat a key on press; never on release
if (released) break :blk self.eaten_keycodes.remove(event.keycode);
- if (self.device.seat.handleMapping(keycode, modifiers, released, xkb_state)) {
+ if (self.device.seat.hasMapping(keycode, modifiers, released, xkb_state)) {
+ // The key needs to be eaten before the mapping is run
+ // Otherwise the mapping may e.g. trigger a focus change which sends an incorrect
+ // wl_keyboard.enter event since eaten_keycodes has not yet been updated.
break :blk self.eaten_keycodes.add(event.keycode, .mapping);
} else if (self.getInputMethodGrab() != null) {
break :blk self.eaten_keycodes.add(event.keycode, .im_grab);
@@ -158,7 +161,11 @@ fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboa
wlr_seat.setKeyboard(self.device.wlr_device.toKeyboard());
wlr_seat.keyboardNotifyKey(event.time_msec, event.keycode, event.state);
},
- .mapping => {},
+ .mapping => if (!released) {
+ // We handle release mappings separately, regardless of whether press mapping exists
+ const handled = self.device.seat.handleMapping(keycode, modifiers, released, xkb_state);
+ assert(handled);
+ },
.im_grab => if (self.getInputMethodGrab()) |keyboard_grab| {
keyboard_grab.setKeyboard(keyboard_grab.keyboard);
keyboard_grab.sendKey(event.time_msec, event.keycode, event.state);
diff --git a/river/Seat.zig b/river/Seat.zig
index d5af958..68679fe 100644
--- a/river/Seat.zig
+++ b/river/Seat.zig
@@ -359,6 +359,25 @@ pub fn enterMode(self: *Self, mode_id: u32) void {
}
}
+/// Is there a user-defined mapping for passed keycode, modifiers and keyboard state?
+pub fn hasMapping(
+ self: *Self,
+ keycode: xkb.Keycode,
+ modifiers: wlr.Keyboard.ModifierMask,
+ released: bool,
+ xkb_state: *xkb.State,
+) bool {
+ const modes = &server.config.modes;
+ for (modes.items[self.mode_id].mappings.items) |*mapping| {
+ if (mapping.match(keycode, modifiers, released, xkb_state, .no_translate) or
+ mapping.match(keycode, modifiers, released, xkb_state, .translate))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
/// Handle any user-defined mapping for passed keycode, modifiers and keyboard state
/// Returns true if a mapping was run
pub fn handleMapping(