aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sway/commands/bind.c83
-rw-r--r--sway/input/keyboard.c14
2 files changed, 85 insertions, 12 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) {
free(binding);
}
+/**
+ * Returns true if the bindings have the same key and modifier combinations.
+ * Note that keyboard layout is not considered, so the bindings might actually
+ * not be equivalent on some layouts.
+ */
+bool binding_key_compare(struct sway_binding *binding_a,
+ struct sway_binding *binding_b) {
+ if (binding_a->bindcode != binding_b->bindcode) {
+ return false;
+ }
+
+ if (binding_a->modifiers ^ binding_b->modifiers) {
+ return false;
+ }
+
+ if (binding_a->keys->length != binding_b->keys->length) {
+ return false;
+ }
+
+ int keys_len = binding_a->keys->length;
+ for (int i = 0; i < keys_len; ++i) {
+ uint32_t key_a = *(uint32_t*)binding_a->keys->items[i];
+ bool found = false;
+ for (int j = 0; j < keys_len; ++j) {
+ uint32_t key_b = *(uint32_t*)binding_b->keys->items[j];
+ if (key_b == key_a) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
struct cmd_results *cmd_bindsym(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) {
@@ -95,11 +133,26 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) {
list_add(binding->keys, key);
}
free_flat_list(split);
-
- struct sway_mode *mode = config->current_mode;
- // TODO overwrite the binding if it already exists
binding->order = binding_order++;
- list_add(mode->keysym_bindings, binding);
+
+ list_t *mode_bindings = config->current_mode->keysym_bindings;
+
+ // overwrite the binding if it already exists
+ bool overwritten = false;
+ for (int i = 0; i < mode_bindings->length; ++i) {
+ struct sway_binding *config_binding = mode_bindings->items[i];
+ if (binding_key_compare(binding, config_binding)) {
+ sway_log(L_DEBUG, "overwriting old binding with command '%s'",
+ config_binding->command);
+ free_sway_binding(config_binding);
+ mode_bindings->items[i] = binding;
+ overwritten = true;
+ }
+ }
+
+ if (!overwritten) {
+ list_add(mode_bindings, binding);
+ }
sway_log(L_DEBUG, "bindsym - Bound %s to command %s",
argv[0], binding->command);
@@ -162,10 +215,26 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
}
free_flat_list(split);
- struct sway_mode *mode = config->current_mode;
- // TODO overwrite binding if it already exists
binding->order = binding_order++;
- list_add(mode->keycode_bindings, binding);
+
+ list_t *mode_bindings = config->current_mode->keycode_bindings;
+
+ // overwrite the binding if it already exists
+ bool overwritten = false;
+ for (int i = 0; i < mode_bindings->length; ++i) {
+ struct sway_binding *config_binding = mode_bindings->items[i];
+ if (binding_key_compare(binding, config_binding)) {
+ sway_log(L_DEBUG, "overwriting old binding with command '%s'",
+ config_binding->command);
+ free_sway_binding(config_binding);
+ mode_bindings->items[i] = binding;
+ overwritten = true;
+ }
+ }
+
+ if (!overwritten) {
+ list_add(mode_bindings, binding);
+ }
sway_log(L_DEBUG, "bindcode - Bound %s to command %s",
argv[0], binding->command);
diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c
index eacb6ceb..6272dcce 100644
--- a/sway/input/keyboard.c
+++ b/sway/input/keyboard.c
@@ -67,7 +67,9 @@ static bool keyboard_execute_bindsym(struct sway_keyboard *keyboard,
list_t *keysym_bindings = config->current_mode->keysym_bindings;
for (int i = 0; i < keysym_bindings->length; ++i) {
struct sway_binding *binding = keysym_bindings->items[i];
+ sway_log(L_DEBUG, "@@ checking binding: %s", binding->command);
if (modifiers ^ binding->modifiers || n != binding->keys->length) {
+ sway_log(L_DEBUG, "@@ modifiers or key num dont match");
continue;
}
@@ -286,17 +288,18 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) {
xkb_keycode_t keycode = event->keycode + 8;
bool handled = false;
- const xkb_keysym_t *keysyms;
// handle keycodes
handled = keyboard_execute_bindcode(keyboard);
+ sway_log(L_DEBUG, "@@ handled by bindcode? %d", handled);
// handle translated keysyms
+ const xkb_keysym_t *translated_keysyms;
uint32_t translated_modifiers;
size_t translated_keysyms_len =
- keyboard_keysyms_translated(keyboard, keycode, &keysyms,
+ keyboard_keysyms_translated(keyboard, keycode, &translated_keysyms,
&translated_modifiers);
- pressed_keysyms_update(keyboard->pressed_keysyms_translated, keysyms,
+ pressed_keysyms_update(keyboard->pressed_keysyms_translated, translated_keysyms,
translated_keysyms_len, event->state);
if (event->state == WLR_KEY_PRESSED && !handled) {
handled = keyboard_execute_bindsym(keyboard,
@@ -305,9 +308,10 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) {
}
// Handle raw keysyms
+ const xkb_keysym_t *raw_keysyms;
uint32_t raw_modifiers;
- size_t raw_keysyms_len = keyboard_keysyms_raw(keyboard, keycode, &keysyms, &raw_modifiers);
- pressed_keysyms_update(keyboard->pressed_keysyms_raw, keysyms, raw_keysyms_len,
+ size_t raw_keysyms_len = keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers);
+ pressed_keysyms_update(keyboard->pressed_keysyms_raw, raw_keysyms, raw_keysyms_len,
event->state);
if (event->state == WLR_KEY_PRESSED && !handled) {
handled = keyboard_execute_bindsym(keyboard,