From 50e791cadbfc51d531fff44cfbe3323a3b456adf Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Thu, 4 Jan 2018 07:25:52 -0500 Subject: [PATCH] binding release --- include/sway/input/keyboard.h | 3 ++ sway/commands/bind.c | 4 ++ sway/input/keyboard.c | 96 +++++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 21 deletions(-) diff --git a/include/sway/input/keyboard.h b/include/sway/input/keyboard.h index 0a5857f0a..8ec3eb356 100644 --- a/include/sway/input/keyboard.h +++ b/include/sway/input/keyboard.h @@ -14,7 +14,10 @@ struct sway_keyboard { struct wl_listener keyboard_modifiers; xkb_keysym_t pressed_keysyms_translated[SWAY_KEYBOARD_PRESSED_KEYSYMS_CAP]; + uint32_t modifiers_translated; + xkb_keysym_t pressed_keysyms_raw[SWAY_KEYBOARD_PRESSED_KEYSYMS_CAP]; + uint32_t modifiers_raw; }; struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, diff --git a/sway/commands/bind.c b/sway/commands/bind.c index ef9a4b7aa..791214041 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -34,6 +34,10 @@ void free_sway_binding(struct sway_binding *binding) { */ bool binding_key_compare(struct sway_binding *binding_a, struct sway_binding *binding_b) { + if (binding_a->release != binding_b->release) { + return false; + } + if (binding_a->bindcode != binding_b->bindcode) { return false; } diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 3090d32f2..7b90dacf1 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -1,3 +1,4 @@ +#include #include #include #include "sway/input/seat.h" @@ -54,6 +55,18 @@ static bool keyboard_execute_compositor_binding(struct sway_keyboard *keyboard, return false; } +static bool binding_matches_keystate(struct sway_binding *binding, + enum wlr_key_state key_state) { + if (key_state == WLR_KEY_PRESSED && !binding->release) { + return true; + } + if (key_state == WLR_KEY_RELEASED && binding->release) { + return true; + } + + return false; +} + /** * Execute keyboard bindings bound with `bindysm`. * @@ -61,13 +74,15 @@ static bool keyboard_execute_compositor_binding(struct sway_keyboard *keyboard, * should be propagated to clients. */ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard, - xkb_keysym_t *pressed_keysyms, uint32_t modifiers, size_t keysyms_len) { + xkb_keysym_t *pressed_keysyms, uint32_t modifiers, enum wlr_key_state key_state) { // configured bindings int n = pressed_keysyms_length(pressed_keysyms); list_t *keysym_bindings = config->current_mode->keysym_bindings; for (int i = 0; i < keysym_bindings->length; ++i) { struct sway_binding *binding = keysym_bindings->items[i]; - if (modifiers ^ binding->modifiers || n != binding->keys->length) { + if (!binding_matches_keystate(binding, key_state) || + modifiers ^ binding->modifiers || + n != binding->keys->length) { continue; } @@ -115,16 +130,43 @@ static bool keysym_is_modifier(xkb_keysym_t keysym) { } static bool binding_matches_keycodes(struct wlr_keyboard *keyboard, - struct sway_binding *binding) { + struct sway_binding *binding, struct wlr_event_keyboard_key *event) { + assert(binding->bindcode); + + uint32_t keycode = event->keycode + 8; + + if (!binding_matches_keystate(binding, event->state)) { + return false; + } + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard); if (modifiers ^ binding->modifiers) { return false; } + // on release, the released key must be in the binding + if (event->state == WLR_KEY_RELEASED) { + bool found = false; + for (int i = 0; i < binding->keys->length; ++i) { + uint32_t binding_keycode = *(uint32_t*)binding->keys->items[i] + 8; + if (binding_keycode == keycode) { + found = true; + break; + } + } + if (!found) { + return false; + } + } + // every keycode in the binding must be present in the pressed keys on the // keyboard for (int i = 0; i < binding->keys->length; ++i) { uint32_t binding_keycode = *(uint32_t*)binding->keys->items[i] + 8; + if (event->state == WLR_KEY_RELEASED && keycode == binding_keycode) { + continue; + } + bool found = false; for (size_t j = 0; j < keyboard->num_keycodes; ++j) { xkb_keycode_t keycode = keyboard->keycodes[j] + 8; @@ -178,13 +220,14 @@ static bool binding_matches_keycodes(struct wlr_keyboard *keyboard, * Returns true if the keysym was handled by a binding and false if the event * should be propagated to clients. */ -static bool keyboard_execute_bindcode(struct sway_keyboard *keyboard) { +static bool keyboard_execute_bindcode(struct sway_keyboard *keyboard, + struct wlr_event_keyboard_key *event) { struct wlr_keyboard *wlr_keyboard = keyboard->seat_device->input_device->wlr_device->keyboard; list_t *keycode_bindings = config->current_mode->keycode_bindings; for (int i = 0; i < keycode_bindings->length; ++i) { struct sway_binding *binding = keycode_bindings->items[i]; - if (binding_matches_keycodes(wlr_keyboard, binding)) { + if (binding_matches_keycodes(wlr_keyboard, binding, event)) { struct cmd_results *results = handle_command(binding->command); if (results->status != CMD_SUCCESS) { sway_log(L_DEBUG, "could not run command for binding: %s", @@ -289,50 +332,61 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { bool handled = false; // handle keycodes - handled = keyboard_execute_bindcode(keyboard); + handled = keyboard_execute_bindcode(keyboard, event); // handle translated keysyms + if (!handled && event->state == WLR_KEY_RELEASED) { + handled = keyboard_execute_bindsym(keyboard, + keyboard->pressed_keysyms_translated, + keyboard->modifiers_translated, + event->state); + } const xkb_keysym_t *translated_keysyms; - uint32_t translated_modifiers; size_t translated_keysyms_len = keyboard_keysyms_translated(keyboard, keycode, &translated_keysyms, - &translated_modifiers); + &keyboard->modifiers_translated); pressed_keysyms_update(keyboard->pressed_keysyms_translated, translated_keysyms, translated_keysyms_len, event->state); - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_bindsym(keyboard, - keyboard->pressed_keysyms_translated, translated_modifiers, - translated_keysyms_len); + keyboard->pressed_keysyms_translated, + keyboard->modifiers_translated, + event->state); } // Handle raw keysyms + if (!handled && event->state == WLR_KEY_RELEASED) { + handled = keyboard_execute_bindsym(keyboard, + keyboard->pressed_keysyms_raw, keyboard->modifiers_raw, + event->state); + } const xkb_keysym_t *raw_keysyms; - uint32_t raw_modifiers; size_t raw_keysyms_len = - keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers); + keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &keyboard->modifiers_raw); pressed_keysyms_update(keyboard->pressed_keysyms_raw, raw_keysyms, raw_keysyms_len, event->state); - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_bindsym(keyboard, - keyboard->pressed_keysyms_raw, raw_modifiers, - raw_keysyms_len); + keyboard->pressed_keysyms_raw, keyboard->modifiers_raw, + event->state); } // Compositor bindings - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_compositor_binding(keyboard, - keyboard->pressed_keysyms_translated, translated_modifiers, + keyboard->pressed_keysyms_translated, + keyboard->modifiers_translated, translated_keysyms_len); } - if (event->state == WLR_KEY_PRESSED && !handled) { + if (!handled && event->state == WLR_KEY_PRESSED) { handled = keyboard_execute_compositor_binding(keyboard, - keyboard->pressed_keysyms_raw, raw_modifiers, + keyboard->pressed_keysyms_raw, keyboard->modifiers_raw, raw_keysyms_len); } - if (!handled) { + if (!handled || event->state == WLR_KEY_RELEASED) { wlr_seat_set_keyboard(wlr_seat, wlr_device); wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec, event->keycode, event->state);