aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2022-02-18 17:40:19 +0000
committerDaniel Eklöf <daniel@ekloef.se>2022-02-18 17:40:19 +0000
commitf76c9e77f1bd2c2c844a703542e939bfb0e77512 (patch)
tree96d6ab4a1c0aa744d78728605e037038f6646acb
parentmain: --server: don’t exit with code 0 on failure (diff)
parentdoc: foot.ini: document the new [text-bindings] section (diff)
Merge branch 'custom-input-escapes'
Closes #325
-rw-r--r--CHANGELOG.md4
-rw-r--r--config.c150
-rw-r--r--config.h11
-rw-r--r--doc/foot.ini.5.scd31
-rw-r--r--foot.ini3
-rw-r--r--input.c5
-rw-r--r--tests/test-config.c37
-rw-r--r--wayland.h2
8 files changed, 221 insertions, 22 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ab8d4b81..e85fb439 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -48,6 +48,8 @@
* Mouse selections are now finalized when the window is resized
(https://codeberg.org/dnkl/foot/issues/922).
+* Support for re-mapping input, i.e. mapping input to custom escape
+ sequences (https://codeberg.org/dnkl/foot/issues/325).
### Deprecated
@@ -72,6 +74,8 @@
* Exit code being 0 when a foot server with no open windows terminate
due to e.g. a Wayland connection failure
(https://codeberg.org/dnkl/foot/issues/943).
+* Key binding collisions not detected for bindings specified as option
+ overrides on the command line.
### Security
diff --git a/config.c b/config.c
index 37768a8e..f50ee685 100644
--- a/config.c
+++ b/config.c
@@ -111,6 +111,7 @@ static const char *const binding_action_map[] = {
[BIND_ACTION_PIPE_SELECTED] = "pipe-selected",
[BIND_ACTION_SHOW_URLS_COPY] = "show-urls-copy",
[BIND_ACTION_SHOW_URLS_LAUNCH] = "show-urls-launch",
+ [BIND_ACTION_TEXT_BINDING] = "text-binding",
/* Mouse-specific actions */
[BIND_ACTION_SELECT_BEGIN] = "select-begin",
@@ -1480,7 +1481,8 @@ free_binding_aux(struct binding_aux *aux)
{
switch (aux->type) {
case BINDING_AUX_NONE: break;
- case BINDING_AUX_PIPE: free_argv(&aux->pipe);
+ case BINDING_AUX_PIPE: free_argv(&aux->pipe); break;
+ case BINDING_AUX_TEXT: free(aux->text.data); break;
}
}
@@ -1578,8 +1580,15 @@ binding_aux_equal(const struct binding_aux *a,
return false;
switch (a->type) {
- case BINDING_AUX_NONE: return true;
- case BINDING_AUX_PIPE: return argv_compare(&a->pipe, &b->pipe) == 0;
+ case BINDING_AUX_NONE:
+ return true;
+
+ case BINDING_AUX_PIPE:
+ return argv_compare(&a->pipe, &b->pipe) == 0;
+
+ case BINDING_AUX_TEXT:
+ return a->text.len == b->text.len &&
+ memcmp(a->text.data, b->text.data, a->text.len) == 0;
}
BUG("invalid AUX type: %d", a->type);
@@ -2231,6 +2240,81 @@ parse_section_mouse_bindings(struct context *ctx)
}
static bool
+parse_section_text_bindings(struct context *ctx)
+{
+ struct config *conf = ctx->conf;
+ const char *key = ctx->key;
+
+ const size_t key_len = strlen(key);
+
+ uint8_t *data = xmalloc(key_len + 1);
+ size_t data_len = 0;
+ bool esc = false;
+
+ for (size_t i = 0; i < key_len; i++) {
+ if (key[i] == '\\') {
+ if (i + 1 >= key_len) {
+ ctx->value = "";
+ LOG_CONTEXTUAL_ERR("trailing backslash");
+ goto err;
+ }
+
+ esc = true;
+ }
+
+ else if (esc) {
+ if (key[i] != 'x') {
+ ctx->value = "";
+ LOG_CONTEXTUAL_ERR("invalid escaped character: %c", key[i]);
+ goto err;
+ }
+ if (i + 2 >= key_len) {
+ ctx->value = "";
+ LOG_CONTEXTUAL_ERR("\\x sequence too short");
+ goto err;
+ }
+
+ const uint8_t nib1 = hex2nibble(key[i + 1]);
+ const uint8_t nib2 = hex2nibble(key[i + 2]);
+
+ if (nib1 >= HEX_DIGIT_INVALID || nib2 >= HEX_DIGIT_INVALID) {
+ ctx->value = "";
+ LOG_CONTEXTUAL_ERR("invalid \\x sequence: \\x%c%c",
+ key[i + 1], key[i + 2]);
+ goto err;
+ }
+
+ data[data_len++] = nib1 << 4 | nib2;
+ esc = false;
+ i += 2;
+ }
+
+ else
+ data[data_len++] = key[i];
+ }
+
+ struct binding_aux aux = {
+ .type = BINDING_AUX_TEXT,
+ .text = {
+ .data = data,
+ .len = data_len,
+ },
+ };
+
+ if (!value_to_key_combos(ctx, BIND_ACTION_TEXT_BINDING, &aux,
+ &conf->bindings.key, KEY_BINDING))
+ {
+ goto err;
+ }
+
+ return true;
+
+err:
+ free(data);
+ return false;
+}
+
+static bool
parse_section_tweak(struct context *ctx)
{
struct config *conf = ctx->conf;
@@ -2429,6 +2513,7 @@ enum section {
SECTION_SEARCH_BINDINGS,
SECTION_URL_BINDINGS,
SECTION_MOUSE_BINDINGS,
+ SECTION_TEXT_BINDINGS,
SECTION_TWEAK,
SECTION_COUNT,
};
@@ -2452,6 +2537,7 @@ static const struct {
[SECTION_SEARCH_BINDINGS] = {&parse_section_search_bindings, "search-bindings"},
[SECTION_URL_BINDINGS] = {&parse_section_url_bindings, "url-bindings"},
[SECTION_MOUSE_BINDINGS] = {&parse_section_mouse_bindings, "mouse-bindings"},
+ [SECTION_TEXT_BINDINGS] = {&parse_section_text_bindings, "text-bindings"},
[SECTION_TWEAK] = {&parse_section_tweak, "tweak"},
};
@@ -2967,25 +3053,12 @@ config_load(struct config *conf, const char *conf_path,
goto out;
}
- ret = parse_config_file(f, conf, conf_file.path, errors_are_fatal) &&
- config_override_apply(conf, overrides, errors_are_fatal);
-
- if (ret &&
- (!resolve_key_binding_collisions(
- conf, section_info[SECTION_KEY_BINDINGS].name,
- binding_action_map, &conf->bindings.key, KEY_BINDING) ||
- !resolve_key_binding_collisions(
- conf, section_info[SECTION_SEARCH_BINDINGS].name,
- search_binding_action_map, &conf->bindings.search, KEY_BINDING) ||
- !resolve_key_binding_collisions(
- conf, section_info[SECTION_URL_BINDINGS].name,
- url_binding_action_map, &conf->bindings.url, KEY_BINDING) ||
- !resolve_key_binding_collisions(
- conf, section_info[SECTION_MOUSE_BINDINGS].name,
- binding_action_map, &conf->bindings.mouse, MOUSE_BINDING)))
+ if (!parse_config_file(f, conf, conf_file.path, errors_are_fatal) ||
+ !config_override_apply(conf, overrides, errors_are_fatal))
{
ret = !errors_are_fatal;
- }
+ } else
+ ret = true;
fclose(f);
@@ -3077,7 +3150,19 @@ config_override_apply(struct config *conf, config_override_t *overrides,
conf->csd.border_width = max(
min_csd_border_width, conf->csd.border_width_visible);
- return true;
+ return
+ resolve_key_binding_collisions(
+ conf, section_info[SECTION_KEY_BINDINGS].name,
+ binding_action_map, &conf->bindings.key, KEY_BINDING) &&
+ resolve_key_binding_collisions(
+ conf, section_info[SECTION_SEARCH_BINDINGS].name,
+ search_binding_action_map, &conf->bindings.search, KEY_BINDING) &&
+ resolve_key_binding_collisions(
+ conf, section_info[SECTION_URL_BINDINGS].name,
+ url_binding_action_map, &conf->bindings.url, KEY_BINDING) &&
+ resolve_key_binding_collisions(
+ conf, section_info[SECTION_MOUSE_BINDINGS].name,
+ binding_action_map, &conf->bindings.mouse, MOUSE_BINDING);
}
static void NOINLINE
@@ -3085,6 +3170,8 @@ key_binding_list_clone(struct config_key_binding_list *dst,
const struct config_key_binding_list *src)
{
struct argv *last_master_argv = NULL;
+ uint8_t *last_master_text_data = NULL;
+ size_t last_master_text_len = 0;
dst->count = src->count;
dst->arr = xmalloc(src->count * sizeof(dst->arr[0]));
@@ -3098,6 +3185,8 @@ key_binding_list_clone(struct config_key_binding_list *dst,
switch (old->aux.type) {
case BINDING_AUX_NONE:
last_master_argv = NULL;
+ last_master_text_data = NULL;
+ last_master_text_len = 0;
break;
case BINDING_AUX_PIPE:
@@ -3108,6 +3197,25 @@ key_binding_list_clone(struct config_key_binding_list *dst,
xassert(last_master_argv != NULL);
new->aux.pipe = *last_master_argv;
}
+ last_master_text_data = NULL;
+ last_master_text_len = 0;
+ break;
+
+ case BINDING_AUX_TEXT:
+ if (old->aux.master_copy) {
+ const size_t len = old->aux.text.len;
+ new->aux.text.len = len;
+ new->aux.text.data = xmalloc(len);
+ memcpy(new->aux.text.data, old->aux.text.data, len);
+
+ last_master_text_len = len;
+ last_master_text_data = new->aux.text.data;
+ } else {
+ xassert(last_master_text_data != NULL);
+ new->aux.text.len = last_master_text_len;
+ new->aux.text.data = last_master_text_data;
+ }
+ last_master_argv = NULL;
break;
}
}
diff --git a/config.h b/config.h
index cd5bbd14..cf9ca3db 100644
--- a/config.h
+++ b/config.h
@@ -47,6 +47,7 @@ struct argv {
enum binding_aux_type {
BINDING_AUX_NONE,
BINDING_AUX_PIPE,
+ BINDING_AUX_TEXT,
};
struct binding_aux {
@@ -55,6 +56,11 @@ struct binding_aux {
union {
struct argv pipe;
+
+ struct {
+ uint8_t *data;
+ size_t len;
+ } text;
};
};
@@ -63,6 +69,11 @@ enum key_binding_type {
MOUSE_BINDING,
};
+struct config_key_binding_text {
+ char *text;
+ bool master_copy;
+};
+
struct config_key_binding {
int action; /* One of the varios bind_action_* enums from wayland.h */
struct config_key_modifiers modifiers;
diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd
index 7701cc69..7763a8b3 100644
--- a/doc/foot.ini.5.scd
+++ b/doc/foot.ini.5.scd
@@ -855,6 +855,37 @@ Be careful; do not use single-letter keys that are also used in
Default: _t_.
+# SECTION: text-bindings
+
+This section lets you remap key combinations to custom escape
+sequences.
+
+The format is _text=combo1...comboN_. That is, the string to emit may
+have one or more key combinations, space separated. Each combination
+is on the form _mod1+mod2+key_. The names of the modifiers and the key
+*must* be valid XKB key names.
+
+The text string specifies the characters, or bytes, to emit when the
+associated key combination(s) are pressed. There are two ways to
+specify a character:
+
+- Normal, printable characters are written as-is: *abcdef*.
+- Bytes (e.g. ESC) are written as two-digit hexadecimal numbers, with
+ a *\\x* prefix: *\\x1b*.
+
+Example: you would like to remap _Super+k_ to the _Up_ key.
+
+The escape sequence for the Up key is _ESC [ A_ (without the
+spaces). Thus, we need to specify this in foot.ini (*Mod4* is the XKB
+name for the Super/logo key):
+
+*\\x1b[A = Mod4+k*
+
+Another example: to remap _Super+c_ to _Control+c_:
+
+*\\x03 = Mod4+c*
+
+
# SECTION: mouse-bindings
This section lets you override the default mouse bindings.
diff --git a/foot.ini b/foot.ini
index 072613d0..2ae09313 100644
--- a/foot.ini
+++ b/foot.ini
@@ -167,6 +167,9 @@
# cancel=Control+g Control+c Control+d Escape
# toggle-url-visible=t
+[text-bindings]
+# \x03=Mod4+c # Map Super+c -> Ctrl+c
+
[mouse-bindings]
# selection-override-modifiers=Shift
# primary-paste=BTN_MIDDLE
diff --git a/input.c b/input.c
index e7b02fb8..26af0807 100644
--- a/input.c
+++ b/input.c
@@ -313,6 +313,11 @@ execute_binding(struct seat *seat, struct terminal *term,
return true;
}
+ case BIND_ACTION_TEXT_BINDING:
+ xassert(binding->aux->type == BINDING_AUX_TEXT);
+ term_to_slave(term, binding->aux->text.data, binding->aux->text.len);
+ return true;
+
case BIND_ACTION_SELECT_BEGIN:
selection_start(
term, seat->mouse.col, seat->mouse.row, SELECTION_CHAR_WISE, false);
diff --git a/tests/test-config.c b/tests/test-config.c
index 04149a59..eb99780d 100644
--- a/tests/test-config.c
+++ b/tests/test-config.c
@@ -1095,6 +1095,42 @@ test_section_mouse_bindings_collisions(void)
}
static void
+test_section_text_bindings(void)
+{
+ struct config conf = {0};
+ struct context ctx = {
+ .conf = &conf, .section = "text-bindings", .path = "unittest"};
+
+ ctx.key = "abcd";
+ ctx.value = XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT "+x";
+ xassert(parse_section_text_bindings(&ctx));
+
+ ctx.key = "\\x07";
+ xassert(parse_section_text_bindings(&ctx));
+
+ ctx.key = "\\x1g";
+ xassert(!parse_section_text_bindings(&ctx));
+
+ ctx.key = "\\x1";
+ xassert(!parse_section_text_bindings(&ctx));
+
+ ctx.key = "\\x";
+ xassert(!parse_section_text_bindings(&ctx));
+
+ ctx.key = "\\";
+ xassert(!parse_section_text_bindings(&ctx));
+
+ ctx.key = "\\y";
+ xassert(!parse_section_text_bindings(&ctx));
+
+ ctx.key = "abcd";
+ ctx.value = "InvalidMod+y";
+ xassert(!parse_section_text_bindings(&ctx));
+
+ config_free(conf);
+}
+
+static void
test_section_tweak(void)
{
struct config conf = {0};
@@ -1188,6 +1224,7 @@ main(int argc, const char *const *argv)
test_section_url_bindings_collisions();
test_section_mouse_bindings();
test_section_mouse_bindings_collisions();
+ test_section_text_bindings();
test_section_tweak();
log_deinit();
return 0;
diff --git a/wayland.h b/wayland.h
index 81109673..2d753e70 100644
--- a/wayland.h
+++ b/wayland.h
@@ -54,6 +54,7 @@ enum bind_action_normal {
BIND_ACTION_PIPE_SELECTED,
BIND_ACTION_SHOW_URLS_COPY,
BIND_ACTION_SHOW_URLS_LAUNCH,
+ BIND_ACTION_TEXT_BINDING,
/* Mouse specific actions - i.e. they require a mouse coordinate */
BIND_ACTION_SELECT_BEGIN,
@@ -119,7 +120,6 @@ struct key_binding {
};
const struct binding_aux *aux;
-
};
typedef tll(struct key_binding) key_binding_list_t;