summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/input_state.h8
-rw-r--r--sway/handlers.c16
-rw-r--r--sway/input_state.c54
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 @@
6 6
7/* Keyboard state */ 7/* Keyboard state */
8 8
9typedef uint32_t keycode;
10
11// returns true if key has been pressed, otherwise false 9// returns true if key has been pressed, otherwise false
12bool check_key(keycode key); 10bool check_key(uint32_t key_sym, uint32_t key_code);
13 11
14// sets a key as pressed 12// sets a key as pressed
15void press_key(keycode key); 13void press_key(uint32_t key_sym, uint32_t key_code);
16 14
17// unsets a key as pressed 15// unsets a key as pressed
18void release_key(keycode key); 16void release_key(uint32_t key_sym, uint32_t key_code);
19 17
20/* Pointer state */ 18/* Pointer state */
21 19
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
295 295
296 struct sway_mode *mode = config->current_mode; 296 struct sway_mode *mode = config->current_mode;
297 297
298 if (sym < 70000 /* bullshit made up number */) {
299 if (!isalnum(sym) && sym != ' ' && sym != XKB_KEY_Escape && sym != XKB_KEY_Tab) {
300 // God fucking dammit
301 return false;
302 }
303 }
304
305 // Lowercase if necessary
306 sym = tolower(sym);
307
308 int i; 298 int i;
309 299
310 if (state == WLC_KEY_STATE_PRESSED) { 300 if (state == WLC_KEY_STATE_PRESSED) {
311 press_key(sym); 301 press_key(sym, key);
312 } else { // WLC_KEY_STATE_RELEASED 302 } else { // WLC_KEY_STATE_RELEASED
313 release_key(sym); 303 release_key(sym, key);
314 } 304 }
315 305
316 // TODO: reminder to check conflicts with mod+q+a versus mod+q 306 // 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
322 int j; 312 int j;
323 for (j = 0; j < binding->keys->length; ++j) { 313 for (j = 0; j < binding->keys->length; ++j) {
324 xkb_keysym_t *key = binding->keys->items[j]; 314 xkb_keysym_t *key = binding->keys->items[j];
325 if ((match = check_key(*key)) == false) { 315 if ((match = check_key(*key, 0)) == false) {
326 break; 316 break;
327 } 317 }
328 } 318 }
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
9static keycode key_state_array[KEY_STATE_MAX_LENGTH]; 10struct 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
22static struct key_state key_state_array[KEY_STATE_MAX_LENGTH];
10 23
11void input_init(void) { 24void 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
18static uint8_t find_key(keycode key) { 32static 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
28bool check_key(keycode key) { 50bool 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
32void press_key(keycode key) { 54void 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
43void release_key(keycode key) { 69void 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