aboutsummaryrefslogtreecommitdiffstats
path: root/sway/input/keyboard.c
diff options
context:
space:
mode:
authorLibravatar Tony Crisci <tony@dubstepdish.com>2018-01-04 07:54:14 -0500
committerLibravatar Tony Crisci <tony@dubstepdish.com>2018-01-04 07:54:14 -0500
commit8b4eb5d7d1846447b54ebf786c7dd6869a27670a (patch)
tree5972221216c0699d419e7f83e61ecea014fa9698 /sway/input/keyboard.c
parentbinding release (diff)
downloadsway-8b4eb5d7d1846447b54ebf786c7dd6869a27670a.tar.gz
sway-8b4eb5d7d1846447b54ebf786c7dd6869a27670a.tar.zst
sway-8b4eb5d7d1846447b54ebf786c7dd6869a27670a.zip
cleanup bindings
Diffstat (limited to 'sway/input/keyboard.c')
-rw-r--r--sway/input/keyboard.c162
1 files changed, 81 insertions, 81 deletions
diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c
index 7b90dacf..c2bb2578 100644
--- a/sway/input/keyboard.c
+++ b/sway/input/keyboard.c
@@ -7,6 +7,22 @@
7#include "sway/commands.h" 7#include "sway/commands.h"
8#include "log.h" 8#include "log.h"
9 9
10static bool keysym_is_modifier(xkb_keysym_t keysym) {
11 switch (keysym) {
12 case XKB_KEY_Shift_L: case XKB_KEY_Shift_R:
13 case XKB_KEY_Control_L: case XKB_KEY_Control_R:
14 case XKB_KEY_Caps_Lock:
15 case XKB_KEY_Shift_Lock:
16 case XKB_KEY_Meta_L: case XKB_KEY_Meta_R:
17 case XKB_KEY_Alt_L: case XKB_KEY_Alt_R:
18 case XKB_KEY_Super_L: case XKB_KEY_Super_R:
19 case XKB_KEY_Hyper_L: case XKB_KEY_Hyper_R:
20 return true;
21 default:
22 return false;
23 }
24}
25
10static size_t pressed_keysyms_length(xkb_keysym_t *pressed_keysyms) { 26static size_t pressed_keysyms_length(xkb_keysym_t *pressed_keysyms) {
11 size_t n = 0; 27 size_t n = 0;
12 for (size_t i = 0; i < SWAY_KEYBOARD_PRESSED_KEYSYMS_CAP; ++i) { 28 for (size_t i = 0; i < SWAY_KEYBOARD_PRESSED_KEYSYMS_CAP; ++i) {
@@ -27,6 +43,63 @@ static ssize_t pressed_keysyms_index(xkb_keysym_t *pressed_keysyms,
27 return -1; 43 return -1;
28} 44}
29 45
46static void pressed_keysyms_add(xkb_keysym_t *pressed_keysyms,
47 xkb_keysym_t keysym) {
48 ssize_t i = pressed_keysyms_index(pressed_keysyms, keysym);
49 if (i < 0) {
50 i = pressed_keysyms_index(pressed_keysyms, XKB_KEY_NoSymbol);
51 if (i >= 0) {
52 pressed_keysyms[i] = keysym;
53 }
54 }
55}
56
57static void pressed_keysyms_remove(xkb_keysym_t *pressed_keysyms,
58 xkb_keysym_t keysym) {
59 ssize_t i = pressed_keysyms_index(pressed_keysyms, keysym);
60 if (i >= 0) {
61 pressed_keysyms[i] = XKB_KEY_NoSymbol;
62 }
63}
64
65static void pressed_keysyms_update(xkb_keysym_t *pressed_keysyms,
66 const xkb_keysym_t *keysyms, size_t keysyms_len,
67 enum wlr_key_state state) {
68 for (size_t i = 0; i < keysyms_len; ++i) {
69 if (keysym_is_modifier(keysyms[i])) {
70 continue;
71 }
72 if (state == WLR_KEY_PRESSED) {
73 pressed_keysyms_add(pressed_keysyms, keysyms[i]);
74 } else { // WLR_KEY_RELEASED
75 pressed_keysyms_remove(pressed_keysyms, keysyms[i]);
76 }
77 }
78}
79
80static bool binding_matches_key_state(struct sway_binding *binding,
81 enum wlr_key_state key_state) {
82 if (key_state == WLR_KEY_PRESSED && !binding->release) {
83 return true;
84 }
85 if (key_state == WLR_KEY_RELEASED && binding->release) {
86 return true;
87 }
88
89 return false;
90}
91
92static void binding_execute_command(struct sway_binding *binding) {
93 sway_log(L_DEBUG, "running command for binding: %s",
94 binding->command);
95 struct cmd_results *results = handle_command(binding->command);
96 if (results->status != CMD_SUCCESS) {
97 sway_log(L_DEBUG, "could not run command for binding: %s",
98 binding->command);
99 }
100 free_cmd_results(results);
101}
102
30/** 103/**
31 * Execute a built-in, hardcoded compositor binding. These are triggered from a 104 * Execute a built-in, hardcoded compositor binding. These are triggered from a
32 * single keysym. 105 * single keysym.
@@ -55,32 +128,21 @@ static bool keyboard_execute_compositor_binding(struct sway_keyboard *keyboard,
55 return false; 128 return false;
56} 129}
57 130
58static bool binding_matches_keystate(struct sway_binding *binding,
59 enum wlr_key_state key_state) {
60 if (key_state == WLR_KEY_PRESSED && !binding->release) {
61 return true;
62 }
63 if (key_state == WLR_KEY_RELEASED && binding->release) {
64 return true;
65 }
66
67 return false;
68}
69
70/** 131/**
71 * Execute keyboard bindings bound with `bindysm`. 132 * Execute keyboard bindings bound with `bindysm` for the given keyboard state.
72 * 133 *
73 * Returns true if the keysym was handled by a binding and false if the event 134 * Returns true if the keysym was handled by a binding and false if the event
74 * should be propagated to clients. 135 * should be propagated to clients.
75 */ 136 */
76static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard, 137static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard,
77 xkb_keysym_t *pressed_keysyms, uint32_t modifiers, enum wlr_key_state key_state) { 138 xkb_keysym_t *pressed_keysyms, uint32_t modifiers,
139 enum wlr_key_state key_state) {
78 // configured bindings 140 // configured bindings
79 int n = pressed_keysyms_length(pressed_keysyms); 141 int n = pressed_keysyms_length(pressed_keysyms);
80 list_t *keysym_bindings = config->current_mode->keysym_bindings; 142 list_t *keysym_bindings = config->current_mode->keysym_bindings;
81 for (int i = 0; i < keysym_bindings->length; ++i) { 143 for (int i = 0; i < keysym_bindings->length; ++i) {
82 struct sway_binding *binding = keysym_bindings->items[i]; 144 struct sway_binding *binding = keysym_bindings->items[i];
83 if (!binding_matches_keystate(binding, key_state) || 145 if (!binding_matches_key_state(binding, key_state) ||
84 modifiers ^ binding->modifiers || 146 modifiers ^ binding->modifiers ||
85 n != binding->keys->length) { 147 n != binding->keys->length) {
86 continue; 148 continue;
@@ -98,14 +160,7 @@ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard,
98 } 160 }
99 161
100 if (match) { 162 if (match) {
101 sway_log(L_DEBUG, "running command for binding: %s", 163 binding_execute_command(binding);
102 binding->command);
103 struct cmd_results *results = handle_command(binding->command);
104 if (results->status != CMD_SUCCESS) {
105 sway_log(L_DEBUG, "could not run command for binding: %s",
106 binding->command);
107 }
108 free_cmd_results(results);
109 return true; 164 return true;
110 } 165 }
111 } 166 }
@@ -113,29 +168,13 @@ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard,
113 return false; 168 return false;
114} 169}
115 170
116static bool keysym_is_modifier(xkb_keysym_t keysym) {
117 switch (keysym) {
118 case XKB_KEY_Shift_L: case XKB_KEY_Shift_R:
119 case XKB_KEY_Control_L: case XKB_KEY_Control_R:
120 case XKB_KEY_Caps_Lock:
121 case XKB_KEY_Shift_Lock:
122 case XKB_KEY_Meta_L: case XKB_KEY_Meta_R:
123 case XKB_KEY_Alt_L: case XKB_KEY_Alt_R:
124 case XKB_KEY_Super_L: case XKB_KEY_Super_R:
125 case XKB_KEY_Hyper_L: case XKB_KEY_Hyper_R:
126 return true;
127 default:
128 return false;
129 }
130}
131
132static bool binding_matches_keycodes(struct wlr_keyboard *keyboard, 171static bool binding_matches_keycodes(struct wlr_keyboard *keyboard,
133 struct sway_binding *binding, struct wlr_event_keyboard_key *event) { 172 struct sway_binding *binding, struct wlr_event_keyboard_key *event) {
134 assert(binding->bindcode); 173 assert(binding->bindcode);
135 174
136 uint32_t keycode = event->keycode + 8; 175 uint32_t keycode = event->keycode + 8;
137 176
138 if (!binding_matches_keystate(binding, event->state)) { 177 if (!binding_matches_key_state(binding, event->state)) {
139 return false; 178 return false;
140 } 179 }
141 180
@@ -215,7 +254,7 @@ static bool binding_matches_keycodes(struct wlr_keyboard *keyboard,
215} 254}
216 255
217/** 256/**
218 * Execute keyboard bindings bound with `bindcode`. 257 * Execute keyboard bindings bound with `bindcode` for the given keyboard state.
219 * 258 *
220 * Returns true if the keysym was handled by a binding and false if the event 259 * Returns true if the keysym was handled by a binding and false if the event
221 * should be propagated to clients. 260 * should be propagated to clients.
@@ -228,12 +267,7 @@ static bool keyboard_execute_bindcode(struct sway_keyboard *keyboard,
228 for (int i = 0; i < keycode_bindings->length; ++i) { 267 for (int i = 0; i < keycode_bindings->length; ++i) {
229 struct sway_binding *binding = keycode_bindings->items[i]; 268 struct sway_binding *binding = keycode_bindings->items[i];
230 if (binding_matches_keycodes(wlr_keyboard, binding, event)) { 269 if (binding_matches_keycodes(wlr_keyboard, binding, event)) {
231 struct cmd_results *results = handle_command(binding->command); 270 binding_execute_command(binding);
232 if (results->status != CMD_SUCCESS) {
233 sway_log(L_DEBUG, "could not run command for binding: %s",
234 binding->command);
235 }
236 free_cmd_results(results);
237 return true; 271 return true;
238 } 272 }
239 } 273 }
@@ -286,40 +320,6 @@ static size_t keyboard_keysyms_raw(struct sway_keyboard *keyboard,
286 keycode, layout_index, 0, keysyms); 320 keycode, layout_index, 0, keysyms);
287} 321}
288 322
289static void pressed_keysyms_add(xkb_keysym_t *pressed_keysyms,
290 xkb_keysym_t keysym) {
291 ssize_t i = pressed_keysyms_index(pressed_keysyms, keysym);
292 if (i < 0) {
293 i = pressed_keysyms_index(pressed_keysyms, XKB_KEY_NoSymbol);
294 if (i >= 0) {
295 pressed_keysyms[i] = keysym;
296 }
297 }
298}
299
300static void pressed_keysyms_remove(xkb_keysym_t *pressed_keysyms,
301 xkb_keysym_t keysym) {
302 ssize_t i = pressed_keysyms_index(pressed_keysyms, keysym);
303 if (i >= 0) {
304 pressed_keysyms[i] = XKB_KEY_NoSymbol;
305 }
306}
307
308static void pressed_keysyms_update(xkb_keysym_t *pressed_keysyms,
309 const xkb_keysym_t *keysyms, size_t keysyms_len,
310 enum wlr_key_state state) {
311 for (size_t i = 0; i < keysyms_len; ++i) {
312 if (keysym_is_modifier(keysyms[i])) {
313 continue;
314 }
315 if (state == WLR_KEY_PRESSED) {
316 pressed_keysyms_add(pressed_keysyms, keysyms[i]);
317 } else { // WLR_KEY_RELEASED
318 pressed_keysyms_remove(pressed_keysyms, keysyms[i]);
319 }
320 }
321}
322
323static void handle_keyboard_key(struct wl_listener *listener, void *data) { 323static void handle_keyboard_key(struct wl_listener *listener, void *data) {
324 struct sway_keyboard *keyboard = 324 struct sway_keyboard *keyboard =
325 wl_container_of(listener, keyboard, keyboard_key); 325 wl_container_of(listener, keyboard, keyboard_key);