From c88990d2c2c66835f9098dc642b6d679529a84a8 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Tue, 7 Nov 2017 12:32:55 +0100 Subject: [PATCH 1/3] fix the keybind handling in rootston. As mentioned in https://github.com/swaywm/wlroots/issues/393 keybinds did't trigger / were checked with "odd" keys and modifiers. This commit sends the keycode through two paths, one to get the keycode and modifiers *after* xkb handles them, a secondary path to get a "raw" keysym without modifiers and then add the modifiers rootston knows about. This will result in the `[Alt]+[Shift]+2` combination I mention earlier going through the keybind detection twice. 1) `[Alt]+[at]` 2) `[Alt]+[Shift]+2` When either combination is found, the appropriate keybind is executed. The xkb handled version will be prefered over the "raw" version. --- rootston/keyboard.c | 60 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index e174b731..6b1fde86 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -60,7 +60,7 @@ static void keyboard_binding_execute(struct roots_keyboard *keyboard, * should be propagated to clients. */ static bool keyboard_keysym_press(struct roots_keyboard *keyboard, - xkb_keysym_t keysym) { + xkb_keysym_t keysym, uint32_t modifiers) { ssize_t i = keyboard_pressed_keysym_index(keyboard, keysym); if (i < 0) { i = keyboard_pressed_keysym_index(keyboard, XKB_KEY_NoSymbol); @@ -88,7 +88,6 @@ static bool keyboard_keysym_press(struct roots_keyboard *keyboard, wlr_seat_keyboard_end_grab(keyboard->input->wl_seat); } - uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); struct wl_list *bindings = &keyboard->input->server->config->bindings; struct binding_config *bc; wl_list_for_each(bc, bindings, link) { @@ -122,25 +121,70 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, } } -static void keyboard_key_notify(struct wl_listener *listener, void *data) { - struct wlr_event_keyboard_key *event = data; - struct roots_keyboard *keyboard = wl_container_of(listener, keyboard, key); +static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, + uint32_t keycode, enum wlr_key_state state) +{ + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); + const xkb_keysym_t *syms; + xkb_layout_index_t layout_index = xkb_state_key_get_layout( + keyboard->device->keyboard->xkb_state, + keycode); + int syms_len = xkb_keymap_key_get_syms_by_level(keyboard->device->keyboard->keymap, + keycode, layout_index, 0, &syms); - uint32_t keycode = event->keycode + 8; + bool handled = false; + for (int i = 0; i < syms_len; i++) { + if (state) { + bool keysym_handled = keyboard_keysym_press(keyboard, + syms[i], modifiers); + handled = handled || keysym_handled; + } else { // WLR_KEY_RELEASED + keyboard_keysym_release(keyboard, syms[i]); + } + } + + return handled; +} + +static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, + uint32_t keycode, enum wlr_key_state state) +{ + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, keycode, &syms); + uint32_t consumed = xkb_state_key_get_consumed_mods2( + keyboard->device->keyboard->xkb_state, + keycode, + XKB_CONSUMED_MODE_XKB); + + modifiers = modifiers & ~consumed; bool handled = false; for (int i = 0; i < syms_len; i++) { - if (event->state == WLR_KEY_PRESSED) { - bool keysym_handled = keyboard_keysym_press(keyboard, syms[i]); + if (state) { + bool keysym_handled = keyboard_keysym_press(keyboard, + syms[i], modifiers); handled = handled || keysym_handled; } else { // WLR_KEY_RELEASED keyboard_keysym_release(keyboard, syms[i]); } } + return handled; +} + +static void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct wlr_event_keyboard_key *event = data; + struct roots_keyboard *keyboard = wl_container_of(listener, keyboard, key); + + uint32_t keycode = event->keycode + 8; + + bool handled = keyboard_keysyms_xkb(keyboard, keycode, event->state); + + if (!handled) + handled |= keyboard_keysyms_simple(keyboard, keycode, event->state); + if (!handled) { wlr_seat_set_keyboard(keyboard->input->wl_seat, keyboard->device); wlr_seat_keyboard_notify_key(keyboard->input->wl_seat, event->time_msec, From 8ca76ff7f27175e275cbebcc788a1e09ce831293 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Tue, 7 Nov 2017 12:55:19 +0100 Subject: [PATCH 2/3] explains 2 way keybind trying. --- rootston/keyboard.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 6b1fde86..86f10374 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -121,14 +121,21 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, } } +/** + * Process keypresses from the keyboard as if modifiers didn't change keysyms. + * + * This avoids the xkb keysym translation based on modifiers considered pressed + * in the state and uses the list of modifiers saved on the rootston side. + * + * This will trigger the keybind: [Alt]+[Shift]+2 + */ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, uint32_t keycode, enum wlr_key_state state) { uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; xkb_layout_index_t layout_index = xkb_state_key_get_layout( - keyboard->device->keyboard->xkb_state, - keycode); + keyboard->device->keyboard->xkb_state, keycode); int syms_len = xkb_keymap_key_get_syms_by_level(keyboard->device->keyboard->keymap, keycode, layout_index, 0, &syms); @@ -146,6 +153,15 @@ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, return handled; } +/** + * Process keypresses from the keyboard as xkb sees them. + * + * This uses the xkb keysyms translation based on pressed modifiers and clears + * the consumed modifiers from the list of modifiers passed to keybind + * detection. + * + * (On US layout) this will trigger: [Alt]+[at] + */ static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, uint32_t keycode, enum wlr_key_state state) { @@ -154,9 +170,7 @@ static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, keycode, &syms); uint32_t consumed = xkb_state_key_get_consumed_mods2( - keyboard->device->keyboard->xkb_state, - keycode, - XKB_CONSUMED_MODE_XKB); + keyboard->device->keyboard->xkb_state, keycode, XKB_CONSUMED_MODE_XKB); modifiers = modifiers & ~consumed; From 23991861a461acabafde3c8d63c4355320e67db3 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Tue, 7 Nov 2017 16:50:34 +0100 Subject: [PATCH 3/3] works in review comments --- rootston/keyboard.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 86f10374..6c419df8 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -121,7 +121,7 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, } } -/** +/* * Process keypresses from the keyboard as if modifiers didn't change keysyms. * * This avoids the xkb keysym translation based on modifiers considered pressed @@ -130,8 +130,7 @@ static void keyboard_keysym_release(struct roots_keyboard *keyboard, * This will trigger the keybind: [Alt]+[Shift]+2 */ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, - uint32_t keycode, enum wlr_key_state state) -{ + uint32_t keycode, enum wlr_key_state state) { uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; xkb_layout_index_t layout_index = xkb_state_key_get_layout( @@ -153,7 +152,7 @@ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, return handled; } -/** +/* * Process keypresses from the keyboard as xkb sees them. * * This uses the xkb keysyms translation based on pressed modifiers and clears @@ -163,8 +162,7 @@ static bool keyboard_keysyms_simple(struct roots_keyboard *keyboard, * (On US layout) this will trigger: [Alt]+[at] */ static bool keyboard_keysyms_xkb(struct roots_keyboard *keyboard, - uint32_t keycode, enum wlr_key_state state) -{ + uint32_t keycode, enum wlr_key_state state) { uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); const xkb_keysym_t *syms; int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, @@ -196,8 +194,10 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { bool handled = keyboard_keysyms_xkb(keyboard, keycode, event->state); - if (!handled) - handled |= keyboard_keysyms_simple(keyboard, keycode, event->state); + if (!handled) { + bool key_handled = keyboard_keysyms_simple(keyboard, keycode, event->state); + handled = handled || key_handled; + } if (!handled) { wlr_seat_set_keyboard(keyboard->input->wl_seat, keyboard->device);