diff options
author | 2015-08-23 15:28:49 -0400 | |
---|---|---|
committer | 2015-08-23 15:28:49 -0400 | |
commit | 1ac0c8cd47f734809c20bf6a6a0a7278680ed597 (patch) | |
tree | c15286165247ae28a192f66aa5886f1d956c5b07 /sway/input_state.c | |
parent | Merge pull request #123 from Luminarys/master (diff) | |
download | sway-1ac0c8cd47f734809c20bf6a6a0a7278680ed597.tar.gz sway-1ac0c8cd47f734809c20bf6a6a0a7278680ed597.tar.zst sway-1ac0c8cd47f734809c20bf6a6a0a7278680ed597.zip |
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
Diffstat (limited to 'sway/input_state.c')
-rw-r--r-- | sway/input_state.c | 54 |
1 files changed, 40 insertions, 14 deletions
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 @@ | |||
1 | #include <string.h> | 1 | #include <string.h> |
2 | #include <stdbool.h> | 2 | #include <stdbool.h> |
3 | #include <ctype.h> | 3 | #include <ctype.h> |
4 | #include "log.h" | ||
4 | 5 | ||
5 | #include "input_state.h" | 6 | #include "input_state.h" |
6 | 7 | ||
7 | #define KEY_STATE_MAX_LENGTH 64 | 8 | #define KEY_STATE_MAX_LENGTH 64 |
8 | 9 | ||
9 | static keycode key_state_array[KEY_STATE_MAX_LENGTH]; | 10 | struct key_state { |
11 | /* | ||
12 | * Aims to store state regardless of modifiers. | ||
13 | * If you press a key, then hold shift, then release the key, we'll | ||
14 | * get two different key syms, but the same key code. This handles | ||
15 | * that scenario and makes sure we can use the right bindings. | ||
16 | */ | ||
17 | uint32_t key_sym; | ||
18 | uint32_t alt_sym; | ||
19 | uint32_t key_code; | ||
20 | }; | ||
21 | |||
22 | static struct key_state key_state_array[KEY_STATE_MAX_LENGTH]; | ||
10 | 23 | ||
11 | void input_init(void) { | 24 | void input_init(void) { |
12 | int i; | 25 | int i; |
13 | for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { | 26 | for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { |
14 | key_state_array[i] = 0; | 27 | struct key_state none = { 0, 0, 0 }; |
28 | key_state_array[i] = none; | ||
15 | } | 29 | } |
16 | } | 30 | } |
17 | 31 | ||
18 | static uint8_t find_key(keycode key) { | 32 | static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) { |
19 | int i; | 33 | int i; |
20 | for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { | 34 | for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) { |
21 | if (key_state_array[i] == key) { | 35 | if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) { |
36 | break; | ||
37 | } | ||
38 | if (key_state_array[i].key_sym == key_sym | ||
39 | || key_state_array[i].alt_sym == key_sym) { | ||
40 | break; | ||
41 | } | ||
42 | if (update && key_state_array[i].key_code == key_code) { | ||
43 | key_state_array[i].alt_sym = key_sym; | ||
22 | break; | 44 | break; |
23 | } | 45 | } |
24 | } | 46 | } |
25 | return i; | 47 | return i; |
26 | } | 48 | } |
27 | 49 | ||
28 | bool check_key(keycode key) { | 50 | bool check_key(uint32_t key_sym, uint32_t key_code) { |
29 | return find_key(key) < KEY_STATE_MAX_LENGTH; | 51 | return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH; |
30 | } | 52 | } |
31 | 53 | ||
32 | void press_key(keycode key) { | 54 | void press_key(uint32_t key_sym, uint32_t key_code) { |
55 | if (key_code == 0) { | ||
56 | return; | ||
57 | } | ||
33 | // Check if key exists | 58 | // Check if key exists |
34 | if (!check_key(key)) { | 59 | if (!check_key(key_sym, key_code)) { |
35 | // Check that we dont exceed buffer length | 60 | // Check that we dont exceed buffer length |
36 | int insert = find_key(0); | 61 | int insert = find_key(0, 0, true); |
37 | if (insert < KEY_STATE_MAX_LENGTH) { | 62 | if (insert < KEY_STATE_MAX_LENGTH) { |
38 | key_state_array[insert] = key; | 63 | key_state_array[insert].key_sym = key_sym; |
64 | key_state_array[insert].key_code = key_code; | ||
39 | } | 65 | } |
40 | } | 66 | } |
41 | } | 67 | } |
42 | 68 | ||
43 | void release_key(keycode key) { | 69 | void release_key(uint32_t key_sym, uint32_t key_code) { |
44 | uint8_t index = find_key(key); | 70 | uint8_t index = find_key(key_sym, key_code, true); |
45 | if (index < KEY_STATE_MAX_LENGTH) { | 71 | if (index < KEY_STATE_MAX_LENGTH) { |
46 | // shift it over and remove key | 72 | struct key_state none = { 0, 0, 0 }; |
47 | key_state_array[index] = 0; | 73 | key_state_array[index] = none; |
48 | } | 74 | } |
49 | } | 75 | } |
50 | 76 | ||