aboutsummaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-06-30 16:05:14 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-06-30 16:05:14 +0000
commit56f592e308302f36ea5eebcd4fa832869a6b33eb (patch)
tree33985109eef583b91d2ace55ee6945d8fb2df757 /config.c
parentrender: don’t look at glyphs[0]->cols when determining if overflow should be allowed (diff)
parentconfig: remove replaced/removed key bindings, instead of marking as ‘unused’ (diff)
Merge branch 'remove-unused-key-bindings'
Closes #614
Diffstat (limited to 'config.c')
-rw-r--r--config.c158
1 files changed, 132 insertions, 26 deletions
diff --git a/config.c b/config.c
index c417fb19..2fa9100e 100644
--- a/config.c
+++ b/config.c
@@ -1662,6 +1662,43 @@ pipe_argv_from_string(const char *value, char ***argv,
return remove_len;
}
+static void
+remove_action_from_key_bindings_list(struct config_key_binding_list *bindings,
+ int action, char **pipe_argv)
+{
+ size_t remove_first_idx = 0;
+ size_t remove_count = 0;
+
+ for (size_t i = 0; i < bindings->count; i++) {
+ struct config_key_binding *binding = &bindings->arr[i];
+
+ if (binding->action == action &&
+ ((binding->pipe.argv.args == NULL && pipe_argv == NULL) ||
+ (binding->pipe.argv.args != NULL && pipe_argv != NULL &&
+ argv_compare(binding->pipe.argv.args, pipe_argv) == 0)))
+ {
+ if (remove_count++ == 0)
+ remove_first_idx = i;
+
+ xassert(remove_first_idx + remove_count - 1 == i);
+
+ if (binding->pipe.master_copy)
+ free_argv(&binding->pipe.argv);
+ }
+ }
+
+ if (remove_count == 0)
+ return;
+
+ size_t move_count = bindings->count - (remove_first_idx + remove_count);
+
+ memmove(
+ &bindings->arr[remove_first_idx],
+ &bindings->arr[remove_first_idx + remove_count],
+ move_count * sizeof(bindings->arr[0]));
+ bindings->count -= remove_count;
+}
+
static bool NOINLINE
parse_key_binding_section(
const char *section, const char *key, const char *value,
@@ -1688,17 +1725,7 @@ parse_key_binding_section(
/* Unset binding */
if (strcasecmp(value, "none") == 0) {
- for (size_t i = 0; i < bindings->count; i++) {
- struct config_key_binding *binding = &bindings->arr[i];
-
- if (binding->action != action)
- continue;
-
- if (binding->pipe.master_copy)
- free_argv(&binding->pipe.argv);
- binding->action = BIND_ACTION_NONE;
- }
-
+ remove_action_from_key_bindings_list(bindings, action, pipe_argv);
free(pipe_argv);
return true;
}
@@ -1715,21 +1742,7 @@ parse_key_binding_section(
return false;
}
- /* Remove existing bindings for this action+pipe */
- for (size_t i = 0; i < bindings->count; i++) {
- struct config_key_binding *binding = &bindings->arr[i];
-
- if (binding->action == action &&
- ((binding->pipe.argv.args == NULL && pipe_argv == NULL) ||
- (binding->pipe.argv.args != NULL && pipe_argv != NULL &&
- argv_compare(binding->pipe.argv.args, pipe_argv) == 0)))
- {
-
- if (binding->pipe.master_copy)
- free_argv(&binding->pipe.argv);
- binding->action = BIND_ACTION_NONE;
- }
- }
+ remove_action_from_key_bindings_list(bindings, action, pipe_argv);
/* Emit key bindings */
size_t ofs = bindings->count;
@@ -1767,6 +1780,90 @@ parse_key_binding_section(
return false;
}
+UNITTEST
+{
+ enum test_actions {
+ TEST_ACTION_NONE,
+ TEST_ACTION_FOO,
+ TEST_ACTION_BAR,
+ TEST_ACTION_COUNT,
+ };
+
+ const char *const map[] = {
+ [TEST_ACTION_NONE] = NULL,
+ [TEST_ACTION_FOO] = "foo",
+ [TEST_ACTION_BAR] = "bar",
+ };
+
+ struct config conf = {0};
+ struct config_key_binding_list bindings = {0};
+
+ /*
+ * ADD foo=Escape
+ *
+ * This verifies we can bind a single key combo to an action.
+ */
+ xassert(parse_key_binding_section(
+ "", "foo", "Escape", ALEN(map), map, &bindings, &conf, "", 0));
+ xassert(bindings.count == 1);
+ xassert(bindings.arr[0].action == TEST_ACTION_FOO);
+ xassert(bindings.arr[0].sym == XKB_KEY_Escape);
+
+ /*
+ * ADD bar=Control+g Control+Shift+x
+ *
+ * This verifies we can bind multiple key combos to an action.
+ */
+ xassert(parse_key_binding_section(
+ "", "bar", "Control+g Control+Shift+x", ALEN(map), map,
+ &bindings, &conf, "", 0));
+ xassert(bindings.count == 3);
+ xassert(bindings.arr[0].action == TEST_ACTION_FOO);
+ xassert(bindings.arr[1].action == TEST_ACTION_BAR);
+ xassert(bindings.arr[1].sym == XKB_KEY_g);
+ xassert(bindings.arr[1].modifiers.ctrl);
+ xassert(bindings.arr[2].action == TEST_ACTION_BAR);
+ xassert(bindings.arr[2].sym == XKB_KEY_x);
+ xassert(bindings.arr[2].modifiers.ctrl && bindings.arr[2].modifiers.shift);
+
+ /*
+ * REPLACE foo with foo=Mod+v Shift+q
+ *
+ * This verifies we can update a single-combo action with multiple
+ * key combos.
+ */
+ xassert(parse_key_binding_section(
+ "", "foo", "Mod1+v Shift+q", ALEN(map), map,
+ &bindings, &conf, "", 0));
+ xassert(bindings.count == 4);
+ xassert(bindings.arr[0].action == TEST_ACTION_BAR);
+ xassert(bindings.arr[1].action == TEST_ACTION_BAR);
+ xassert(bindings.arr[2].action == TEST_ACTION_FOO);
+ xassert(bindings.arr[2].sym == XKB_KEY_v);
+ xassert(bindings.arr[2].modifiers.alt);
+ xassert(bindings.arr[3].action == TEST_ACTION_FOO);
+ xassert(bindings.arr[3].sym == XKB_KEY_q);
+ xassert(bindings.arr[3].modifiers.shift);
+
+ /*
+ * REMOVE bar
+ */
+ xassert(parse_key_binding_section(
+ "", "bar", "none", ALEN(map), map, &bindings, &conf, "", 0));
+ xassert(bindings.count == 2);
+ xassert(bindings.arr[0].action == TEST_ACTION_FOO);
+ xassert(bindings.arr[1].action == TEST_ACTION_FOO);
+
+ /*
+ * REMOVE foo
+ */
+ xassert(parse_key_binding_section(
+ "", "foo", "none", ALEN(map), map, &bindings, &conf, "", 0));
+ xassert(bindings.count == 0);
+
+ free(bindings.arr);
+}
+
static bool
parse_section_key_bindings(
const char *key, const char *value, struct config *conf,
@@ -2852,6 +2949,15 @@ out:
}
}
+#if defined(_DEBUG)
+ for (size_t i = 0; i < conf->bindings.key.count; i++)
+ xassert(conf->bindings.key.arr[i].action != BIND_ACTION_NONE);
+ for (size_t i = 0; i < conf->bindings.search.count; i++)
+ xassert(conf->bindings.search.arr[i].action != BIND_ACTION_SEARCH_NONE);
+ for (size_t i = 0; i < conf->bindings.url.count; i++)
+ xassert(conf->bindings.url.arr[i].action != BIND_ACTION_URL_NONE);
+#endif
+
free(conf_file.path);
if (conf_file.fd >= 0)
close(conf_file.fd);