aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/bind.c
diff options
context:
space:
mode:
authorLibravatar Tony Crisci <tony@dubstepdish.com>2017-12-28 18:50:22 -0500
committerLibravatar Tony Crisci <tony@dubstepdish.com>2017-12-28 18:50:22 -0500
commit62b7ab3959468124086a1ba95361b3eed069b4a7 (patch)
tree5569e29c86c9a38d6ca33be14615879566c845c7 /sway/commands/bind.c
parentfix keyboard_execute_bindcode (diff)
downloadsway-62b7ab3959468124086a1ba95361b3eed069b4a7.tar.gz
sway-62b7ab3959468124086a1ba95361b3eed069b4a7.tar.zst
sway-62b7ab3959468124086a1ba95361b3eed069b4a7.zip
overwrite old bindings
Diffstat (limited to 'sway/commands/bind.c')
-rw-r--r--sway/commands/bind.c83
1 files changed, 76 insertions, 7 deletions
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
index 62aa535a..99f54f46 100644
--- a/sway/commands/bind.c
+++ b/sway/commands/bind.c
@@ -28,6 +28,44 @@ void free_sway_binding(struct sway_binding *binding) {
28 free(binding); 28 free(binding);
29} 29}
30 30
31/**
32 * Returns true if the bindings have the same key and modifier combinations.
33 * Note that keyboard layout is not considered, so the bindings might actually
34 * not be equivalent on some layouts.
35 */
36bool binding_key_compare(struct sway_binding *binding_a,
37 struct sway_binding *binding_b) {
38 if (binding_a->bindcode != binding_b->bindcode) {
39 return false;
40 }
41
42 if (binding_a->modifiers ^ binding_b->modifiers) {
43 return false;
44 }
45
46 if (binding_a->keys->length != binding_b->keys->length) {
47 return false;
48 }
49
50 int keys_len = binding_a->keys->length;
51 for (int i = 0; i < keys_len; ++i) {
52 uint32_t key_a = *(uint32_t*)binding_a->keys->items[i];
53 bool found = false;
54 for (int j = 0; j < keys_len; ++j) {
55 uint32_t key_b = *(uint32_t*)binding_b->keys->items[j];
56 if (key_b == key_a) {
57 found = true;
58 break;
59 }
60 }
61 if (!found) {
62 return false;
63 }
64 }
65
66 return true;
67}
68
31struct cmd_results *cmd_bindsym(int argc, char **argv) { 69struct cmd_results *cmd_bindsym(int argc, char **argv) {
32 struct cmd_results *error = NULL; 70 struct cmd_results *error = NULL;
33 if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { 71 if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) {
@@ -95,11 +133,26 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) {
95 list_add(binding->keys, key); 133 list_add(binding->keys, key);
96 } 134 }
97 free_flat_list(split); 135 free_flat_list(split);
98
99 struct sway_mode *mode = config->current_mode;
100 // TODO overwrite the binding if it already exists
101 binding->order = binding_order++; 136 binding->order = binding_order++;
102 list_add(mode->keysym_bindings, binding); 137
138 list_t *mode_bindings = config->current_mode->keysym_bindings;
139
140 // overwrite the binding if it already exists
141 bool overwritten = false;
142 for (int i = 0; i < mode_bindings->length; ++i) {
143 struct sway_binding *config_binding = mode_bindings->items[i];
144 if (binding_key_compare(binding, config_binding)) {
145 sway_log(L_DEBUG, "overwriting old binding with command '%s'",
146 config_binding->command);
147 free_sway_binding(config_binding);
148 mode_bindings->items[i] = binding;
149 overwritten = true;
150 }
151 }
152
153 if (!overwritten) {
154 list_add(mode_bindings, binding);
155 }
103 156
104 sway_log(L_DEBUG, "bindsym - Bound %s to command %s", 157 sway_log(L_DEBUG, "bindsym - Bound %s to command %s",
105 argv[0], binding->command); 158 argv[0], binding->command);
@@ -162,10 +215,26 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
162 } 215 }
163 free_flat_list(split); 216 free_flat_list(split);
164 217
165 struct sway_mode *mode = config->current_mode;
166 // TODO overwrite binding if it already exists
167 binding->order = binding_order++; 218 binding->order = binding_order++;
168 list_add(mode->keycode_bindings, binding); 219
220 list_t *mode_bindings = config->current_mode->keycode_bindings;
221
222 // overwrite the binding if it already exists
223 bool overwritten = false;
224 for (int i = 0; i < mode_bindings->length; ++i) {
225 struct sway_binding *config_binding = mode_bindings->items[i];
226 if (binding_key_compare(binding, config_binding)) {
227 sway_log(L_DEBUG, "overwriting old binding with command '%s'",
228 config_binding->command);
229 free_sway_binding(config_binding);
230 mode_bindings->items[i] = binding;
231 overwritten = true;
232 }
233 }
234
235 if (!overwritten) {
236 list_add(mode_bindings, binding);
237 }
169 238
170 sway_log(L_DEBUG, "bindcode - Bound %s to command %s", 239 sway_log(L_DEBUG, "bindcode - Bound %s to command %s",
171 argv[0], binding->command); 240 argv[0], binding->command);