From 1ac0c8cd47f734809c20bf6a6a0a7278680ed597 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 23 Aug 2015 15:28:49 -0400 Subject: [PATCH] Refactor keyboard to consider modified keysyms Press Shift Press 0 # Reads as ')' Release Shift Release 0 # Reads as '0' but we now recognize it as the same --- include/input_state.h | 8 +++---- sway/handlers.c | 16 +++---------- sway/input_state.c | 54 ++++++++++++++++++++++++++++++++----------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/include/input_state.h b/include/input_state.h index 04fde42d..29064fd0 100644 --- a/include/input_state.h +++ b/include/input_state.h @@ -6,16 +6,14 @@ /* Keyboard state */ -typedef uint32_t keycode; - // returns true if key has been pressed, otherwise false -bool check_key(keycode key); +bool check_key(uint32_t key_sym, uint32_t key_code); // sets a key as pressed -void press_key(keycode key); +void press_key(uint32_t key_sym, uint32_t key_code); // unsets a key as pressed -void release_key(keycode key); +void release_key(uint32_t key_sym, uint32_t key_code); /* Pointer state */ diff --git a/sway/handlers.c b/sway/handlers.c index 23db5c15..acf3e6a4 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -295,22 +295,12 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier struct sway_mode *mode = config->current_mode; - if (sym < 70000 /* bullshit made up number */) { - if (!isalnum(sym) && sym != ' ' && sym != XKB_KEY_Escape && sym != XKB_KEY_Tab) { - // God fucking dammit - return false; - } - } - - // Lowercase if necessary - sym = tolower(sym); - int i; if (state == WLC_KEY_STATE_PRESSED) { - press_key(sym); + press_key(sym, key); } else { // WLC_KEY_STATE_RELEASED - release_key(sym); + release_key(sym, key); } // TODO: reminder to check conflicts with mod+q+a versus mod+q @@ -322,7 +312,7 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier int j; for (j = 0; j < binding->keys->length; ++j) { xkb_keysym_t *key = binding->keys->items[j]; - if ((match = check_key(*key)) == false) { + if ((match = check_key(*key, 0)) == false) { break; } } diff --git a/sway/input_state.c b/sway/input_state.c index e2f3c754..9e065e60 100644 --- a/sway/input_state.c +++ b/sway/input_state.c @@ -1,50 +1,76 @@ #include #include #include +#include "log.h" #include "input_state.h" #define KEY_STATE_MAX_LENGTH 64 -static keycode key_state_array[KEY_STATE_MAX_LENGTH]; +struct key_state { + /* + * Aims to store state regardless of modifiers. + * If you press a key, then hold shift, then release the key, we'll + * get two different key syms, but the same key code. This handles + * that scenario and makes sure we can use the right bindings. + */ + uint32_t key_sym; + uint32_t alt_sym; + uint32_t key_code; +}; + +static struct key_state key_state_array[KEY_STATE_MAX_LENGTH]; void input_init(void) { int i; for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { - key_state_array[i] = 0; + struct key_state none = { 0, 0, 0 }; + key_state_array[i] = none; } } -static uint8_t find_key(keycode key) { +static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) { int i; for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { - if (key_state_array[i] == key) { + if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) { + break; + } + if (key_state_array[i].key_sym == key_sym + || key_state_array[i].alt_sym == key_sym) { + break; + } + if (update && key_state_array[i].key_code == key_code) { + key_state_array[i].alt_sym = key_sym; break; } } return i; } -bool check_key(keycode key) { - return find_key(key) < KEY_STATE_MAX_LENGTH; +bool check_key(uint32_t key_sym, uint32_t key_code) { + return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH; } -void press_key(keycode key) { +void press_key(uint32_t key_sym, uint32_t key_code) { + if (key_code == 0) { + return; + } // Check if key exists - if (!check_key(key)) { + if (!check_key(key_sym, key_code)) { // Check that we dont exceed buffer length - int insert = find_key(0); + int insert = find_key(0, 0, true); if (insert < KEY_STATE_MAX_LENGTH) { - key_state_array[insert] = key; + key_state_array[insert].key_sym = key_sym; + key_state_array[insert].key_code = key_code; } } } -void release_key(keycode key) { - uint8_t index = find_key(key); +void release_key(uint32_t key_sym, uint32_t key_code) { + uint8_t index = find_key(key_sym, key_code, true); if (index < KEY_STATE_MAX_LENGTH) { - // shift it over and remove key - key_state_array[index] = 0; + struct key_state none = { 0, 0, 0 }; + key_state_array[index] = none; } }