From b22bb3097634206956392929795fd2902f08faa7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 8 Mar 2020 12:08:46 +0100 Subject: wip: initial framework for dealing with key/mouse bindings in different modes This adds initial support for defining key and mouse bindings that are applied in different terminal modes/states. For example, there are two arrays dealing with key and mouse bindings in the "normal" mode. Most bindings will go here. There's also an array for "search" mode. These bindings will be used when the user has started a scrollback search. In the future, there may be a model selection mode as well. Or maybe "search" and "modal selection" will be combined into a single "keyboard" mode. We'll see. Since the keyboard bindings depend on the current XKB keymap, translation from the user specified key combination string cannot be done when loading the configuration, but must be done when we've received a keymap from the wayland server. We should explore if it's possible to load some kind of default keymap just to be able to verify the validity of the key combination strings at configuration load time, to be able to reject the configuration at startup. A couple of key bindings have been added as proof of concept. Mouse bindings aren't handled at all yet, and is likely to be re-written. For example, we can probably translate the configuration strings at configuration load time. --- config.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'config.c') diff --git a/config.c b/config.c index 46056567..0baa5eb5 100644 --- a/config.c +++ b/config.c @@ -619,6 +619,19 @@ config_load(struct config *conf, const char *conf_path) }, }, + .bindings = { + .key = { + [BIND_ACTION_CLIPBOARD_COPY] = strdup("Control+Shift+C"), + [BIND_ACTION_CLIPBOARD_PASTE] = strdup("Control+Shift+V"), + [BIND_ACTION_SEARCH_START] = strdup("Control+Shift+R"), + }, + .mouse = { + [BIND_ACTION_PRIMARY_PASTE] = strdup("BTN_MIDDLE"), + }, + .search = { + }, + }, + .csd = { .preferred = CONF_CSD_PREFER_SERVER, .title_height = 26, @@ -671,4 +684,10 @@ config_free(struct config conf) free(conf.shell); tll_free_and_free(conf.fonts, free); free(conf.server_socket_path); + + for (size_t i = 0; i < BIND_ACTION_COUNT; i++) { + free(conf.bindings.key[i]); + free(conf.bindings.mouse[i]); + free(conf.bindings.search[i]); + } } -- cgit v1.3 From fcf483277568a7839708428cd30f53b01465cc04 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 8 Mar 2020 15:17:29 +0100 Subject: input: convert the rest of the normal key bindings to configurable bindings New actions defined and implemented: * scrollback up/down * font size up/down/reset * spawn terminal Break out key combo parsing to a new function, parse_key_binding_for_action(). This function parses a string containing one or more space separated key combo definitions on the form (mod1+mod2+...+key), where key is a XKB key name (e.g. KP_Add). Convert all hardcoded key bindings to configuration based bindings. These still cannot actually be configured by the user, but at least lives in the conf struct. --- config.c | 6 +++ input.c | 171 +++++++++++++++++++++++++++++++++----------------------------- wayland.c | 2 +- wayland.h | 11 +++- 4 files changed, 107 insertions(+), 83 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 0baa5eb5..124c76b6 100644 --- a/config.c +++ b/config.c @@ -621,9 +621,15 @@ config_load(struct config *conf, const char *conf_path) .bindings = { .key = { + [BIND_ACTION_SCROLLBACK_UP] = strdup("Shift+Page_Up"), + [BIND_ACTION_SCROLLBACK_DOWN] = strdup("Shift+Page_Down"), [BIND_ACTION_CLIPBOARD_COPY] = strdup("Control+Shift+C"), [BIND_ACTION_CLIPBOARD_PASTE] = strdup("Control+Shift+V"), [BIND_ACTION_SEARCH_START] = strdup("Control+Shift+R"), + [BIND_ACTION_FONT_SIZE_UP] = strdup("Control+plus Control+equal Control+KP_Add"), + [BIND_ACTION_FONT_SIZE_DOWN] = strdup("Control+minus Control+KP_Subtract"), + [BIND_ACTION_FONT_SIZE_RESET] = strdup("Control+0 Control+KP_0"), + [BIND_ACTION_SPAWN_TERMINAL] = strdup("Control+Shift+Return"), }, .mouse = { [BIND_ACTION_PRIMARY_PASTE] = strdup("BTN_MIDDLE"), diff --git a/input.c b/input.c index 0e6b3c92..830a0293 100644 --- a/input.c +++ b/input.c @@ -36,6 +36,14 @@ execute_binding(struct terminal *term, enum binding_action action, uint32_t serial) { switch (action) { + case BIND_ACTION_SCROLLBACK_UP: + cmd_scrollback_up(term, term->rows); + break; + + case BIND_ACTION_SCROLLBACK_DOWN: + cmd_scrollback_down(term, term->rows); + break; + case BIND_ACTION_CLIPBOARD_COPY: selection_to_clipboard(term, serial); break; @@ -54,12 +62,87 @@ execute_binding(struct terminal *term, enum binding_action action, search_begin(term); break; + case BIND_ACTION_FONT_SIZE_UP: + term_font_size_increase(term); + break; + + case BIND_ACTION_FONT_SIZE_DOWN: + term_font_size_decrease(term); + break; + + case BIND_ACTION_FONT_SIZE_RESET: + term_font_size_reset(term); + break; + + case BIND_ACTION_SPAWN_TERMINAL: + term_spawn_new(term); + break; + case BIND_ACTION_COUNT: assert(false); break; } } +static bool +parse_key_binding_for_action( + struct xkb_keymap *keymap, enum binding_action action, + const char *combos, key_binding_list_t *bindings) +{ + if (combos == NULL) + return true; + + xkb_mod_mask_t mod_mask = 0; + xkb_keysym_t sym = 0; + + char *copy = strdup(combos); + + for (char *save1 = NULL, *combo = strtok_r(copy, " ", &save1); + combo != NULL; + combo = strtok_r(NULL, " ", &save1)) + { + LOG_DBG("%s", combo); + for (char *save2 = NULL, *key = strtok_r(combo, "+", &save2), + *next_key = strtok_r(NULL, "+", &save2); + key != NULL; + key = next_key, next_key = strtok_r(NULL, "+", &save2)) + { + if (next_key != NULL) { + /* Modifier */ + xkb_mod_index_t mod = xkb_keymap_mod_get_index(keymap, key); + + if (mod == -1) { + LOG_ERR("%s: not a valid modifier name", key); + free(copy); + return false; + } + + mod_mask |= 1 << mod; + LOG_DBG("MOD: %d - %s", mod, key); + } else { + /* Symbol */ + sym = xkb_keysym_from_name(key, 0); + } + } + + LOG_DBG("action=%zu: mods=0x%08x, sym=%d", i, mod_mask, sym); + + assert(sym != 0); + if (bindings != NULL) { + const struct key_binding binding = { + .mods = mod_mask, + .sym = sym, + .action = action, + }; + + tll_push_back(*bindings, binding); + } + } + + free(copy); + return true; +} + static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size) @@ -88,7 +171,7 @@ keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, xkb_context_unref(wayl->kbd.xkb); wayl->kbd.xkb = NULL; } - tll_free(wayl->kbd.key_bindings); + tll_free(wayl->kbd.bindings.key); wayl->kbd.xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS); wayl->kbd.xkb_keymap = xkb_keymap_new_from_string( @@ -113,45 +196,8 @@ keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, close(fd); for (size_t i = 0; i < BIND_ACTION_COUNT; i++) { - const char *combo = wayl->conf->bindings.key[i]; - - if (combo == NULL) - continue; - - xkb_mod_mask_t mod_mask = 0; - xkb_keysym_t sym = 0; - - char *copy = strdup(combo); - - for (char *p = strtok(copy, "+"), *n = strtok(NULL, "+"); - p != NULL; - p = n, n = strtok(NULL, "+")) - { - if (n != NULL) { - /* Modifier */ - xkb_mod_index_t mod = xkb_keymap_mod_get_index( - wayl->kbd.xkb_keymap, p); - - if (mod == -1) { - LOG_ERR("%s: not a valid modifier name", p); - continue; - } - - mod_mask |= 1 << mod; - LOG_DBG("MOD: %d - %s", mod, p); - } else { - /* Symbol */ - sym = xkb_keysym_from_name(p, 0); - } - } - - free(copy); - - assert(sym != 0); - - /* TODO: convert to DBG */ - LOG_INFO("binding: %s -> mods=0x%08x, sym=%d", combo, mod_mask, sym); - tll_push_back(wayl->kbd.key_bindings, ((struct key_binding){mod_mask, sym, i})); + const char *combos = wayl->conf->bindings.key[i]; + parse_key_binding_for_action(wayl->kbd.xkb_keymap, i, combos, &wayl->kbd.bindings.key); } } @@ -446,49 +492,12 @@ keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, "effective=0x%08x, repeats=%d", sym, mods, consumed, significant, effective_mods, should_repeat); - tll_foreach(wayl->kbd.key_bindings, it) { - if (it->item.mods == effective_mods && it->item.sym == sym) { - execute_binding(term, it->item.action, serial); - goto maybe_repeat; - } - } - /* - * Builtin shortcuts + * User configurable bindings */ - - if (effective_mods == shift) { - if (sym == XKB_KEY_Page_Up) { - cmd_scrollback_up(term, term->rows); - goto maybe_repeat; - } - - else if (sym == XKB_KEY_Page_Down) { - cmd_scrollback_down(term, term->rows); - goto maybe_repeat; - } - } - - else if (effective_mods == ctrl) { - if (sym == XKB_KEY_equal || sym == XKB_KEY_plus || sym == XKB_KEY_KP_Add) { - term_font_size_increase(term); - goto maybe_repeat; - } - - else if (sym == XKB_KEY_minus || sym == XKB_KEY_KP_Subtract) { - term_font_size_decrease(term); - goto maybe_repeat; - } - - else if (sym == XKB_KEY_0 || sym == XKB_KEY_KP_Equal || sym == XKB_KEY_KP_0) { - term_font_size_reset(term); - goto maybe_repeat; - } - } - - else if (effective_mods == (shift | ctrl)) { - if (sym == XKB_KEY_Return) { - term_spawn_new(term); + tll_foreach(wayl->kbd.bindings.key, it) { + if (it->item.mods == effective_mods && it->item.sym == sym) { + execute_binding(term, it->item.action, serial); goto maybe_repeat; } } diff --git a/wayland.c b/wayland.c index c508c710..8916baed 100644 --- a/wayland.c +++ b/wayland.c @@ -911,7 +911,7 @@ wayl_destroy(struct wayland *wayl) if (wayl->presentation != NULL) wp_presentation_destroy(wayl->presentation); - tll_free(wayl->kbd.key_bindings); + tll_free(wayl->kbd.bindings.key); if (wayl->kbd.xkb_compose_state != NULL) xkb_compose_state_unref(wayl->kbd.xkb_compose_state); if (wayl->kbd.xkb_compose_table != NULL) diff --git a/wayland.h b/wayland.h index 775aee0f..219e4b8d 100644 --- a/wayland.h +++ b/wayland.h @@ -42,10 +42,16 @@ struct monitor { }; enum binding_action { + BIND_ACTION_SCROLLBACK_UP, + BIND_ACTION_SCROLLBACK_DOWN, BIND_ACTION_CLIPBOARD_COPY, BIND_ACTION_CLIPBOARD_PASTE, BIND_ACTION_PRIMARY_PASTE, BIND_ACTION_SEARCH_START, + BIND_ACTION_FONT_SIZE_UP, + BIND_ACTION_FONT_SIZE_DOWN, + BIND_ACTION_FONT_SIZE_RESET, + BIND_ACTION_SPAWN_TERMINAL, BIND_ACTION_COUNT, }; @@ -54,6 +60,7 @@ struct key_binding { xkb_keysym_t sym; enum binding_action action; }; +typedef tll(struct key_binding) key_binding_list_t; struct kbd { struct xkb_context *xkb; @@ -81,7 +88,9 @@ struct kbd { bool ctrl; bool meta; - tll(struct key_binding) key_bindings; + struct { + key_binding_list_t key; + } bindings; }; struct wl_clipboard { -- cgit v1.3 From 048edc58f1709707f98a87336abe4fa7a45ae5c7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 9 Mar 2020 20:03:04 +0100 Subject: config: load custom key bindings This adds parsing of a new section - "key-bindings" - where the user can configure the key bindings on the form "action=combo1 .. comboN" The validity of the key combinations are checked with the default XKB keymap. --- config.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 22 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 7d7fe18d..62a060c0 100644 --- a/config.c +++ b/config.c @@ -12,9 +12,15 @@ #include #include +#include + #define LOG_MODULE "config" #define LOG_ENABLE_DBG 0 #include "log.h" +#include "input.h" +#include "wayland.h" + +#define ALEN(v) (sizeof(v) / sizeof(v[0])) static const uint32_t default_foreground = 0xdcdccc; static const uint32_t default_background = 0x111111; @@ -41,6 +47,19 @@ static const uint32_t default_bright[] = { 0xffffff, }; +static const char *binding_action_map[] = { + [BIND_ACTION_SCROLLBACK_UP] = "scrollback-up", + [BIND_ACTION_SCROLLBACK_DOWN] = "scrollback-down", + [BIND_ACTION_CLIPBOARD_COPY] = "clipboard-copy", + [BIND_ACTION_CLIPBOARD_PASTE] = "clipboard-paste", + [BIND_ACTION_PRIMARY_PASTE] = "primary-paste", + [BIND_ACTION_SEARCH_START] = "search-start", + [BIND_ACTION_FONT_SIZE_UP] = "font-increase", + [BIND_ACTION_FONT_SIZE_DOWN] = "font-decrease", + [BIND_ACTION_FONT_SIZE_RESET] = "font-reset", + [BIND_ACTION_SPAWN_TERMINAL] = "spawn-terminal", +}; + static char * get_shell(void) { @@ -422,6 +441,39 @@ parse_section_csd(const char *key, const char *value, struct config *conf, return true; } +static bool +parse_section_key_bindings(const char *key, const char *value, struct config *conf, + const char *path, unsigned lineno) +{ + for (enum binding_action action = 0; action < BIND_ACTION_COUNT; action++) { + if (strcmp(key, binding_action_map[action]) != 0) + continue; + + struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_keymap *keymap = xkb_keymap_new_from_names( + ctx, &(struct xkb_rule_names){0}, XKB_KEYMAP_COMPILE_NO_FLAGS); + + bool valid_combo = input_parse_key_binding_for_action(keymap, action, value, NULL); + + xkb_keymap_unref(keymap); + xkb_context_unref(ctx); + + if (!valid_combo) { + LOG_ERR("%s:%d: invalid key combination: %s", path, lineno, value); + return false; + } + + free(conf->bindings.key[action]); + conf->bindings.key[action] = strdup(value); + return true; + } + + + LOG_WARN("%s:%u: invalid key: %s", path, lineno, key); + return false; + +} + static bool parse_config_file(FILE *f, struct config *conf, const char *path) { @@ -430,6 +482,8 @@ parse_config_file(FILE *f, struct config *conf, const char *path) SECTION_COLORS, SECTION_CURSOR, SECTION_CSD, + SECTION_KEY_BINDINGS, + SECTION_COUNT, } section = SECTION_MAIN; /* Function pointer, called for each key/value line */ @@ -437,22 +491,18 @@ parse_config_file(FILE *f, struct config *conf, const char *path) const char *key, const char *value, struct config *conf, const char *path, unsigned lineno); - /* Maps sections to line parser functions */ - static const parser_fun_t section_parser_map[] = { - [SECTION_MAIN] = &parse_section_main, - [SECTION_COLORS] = &parse_section_colors, - [SECTION_CURSOR] = &parse_section_cursor, - [SECTION_CSD] = &parse_section_csd, + static const struct { + parser_fun_t fun; + const char *name; + } section_info[] = { + [SECTION_MAIN] = {&parse_section_main, "main"}, + [SECTION_COLORS] = {&parse_section_colors, "colors"}, + [SECTION_CURSOR] = {&parse_section_cursor, "cursor"}, + [SECTION_CSD] = {&parse_section_csd, "csd"}, + [SECTION_KEY_BINDINGS] = {&parse_section_key_bindings, "key-bindings"}, }; -#if defined(_DEBUG) && defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG - static const char *const section_names[] = { - [SECTION_MAIN] = "main", - [SECTION_COLORS] = "colors", - [SECTION_CURSOR] = "cursor", - [SECTION_CSD] = "csd", - }; -#endif + static_assert(ALEN(section_info) == SECTION_COUNT, "section info array size mismatch"); unsigned lineno = 0; @@ -500,17 +550,19 @@ parse_config_file(FILE *f, struct config *conf, const char *path) *end = '\0'; - if (strcmp(&line[1], "colors") == 0) - section = SECTION_COLORS; - else if (strcmp(&line[1], "cursor") == 0) - section = SECTION_CURSOR; - else if (strcmp(&line[1], "csd") == 0) - section = SECTION_CSD; - else { + section = SECTION_COUNT; + for (enum section i = 0; i < SECTION_COUNT; i++) { + if (strcmp(&line[1], section_info[i].name) == 0) { + section = i; + } + } + + if (section == SECTION_COUNT) { LOG_ERR("%s:%d: invalid section name: %s", path, lineno, &line[1]); goto err; } + /* Process next line */ continue; } @@ -549,7 +601,7 @@ parse_config_file(FILE *f, struct config *conf, const char *path) LOG_DBG("section=%s, key='%s', value='%s'", section_names[section], key, value); - parser_fun_t section_parser = section_parser_map[section]; + parser_fun_t section_parser = section_info[section].fun; assert(section_parser != NULL); if (!section_parser(key, value, conf, path, lineno)) -- cgit v1.3 From 65ff582358c07c8bb69f56dc8192c86d21b620d3 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 12 Mar 2020 09:34:09 +0100 Subject: bindings: add minimize/maximize/fullscreen actions These actions are by default not bound to anything. --- config.c | 6 ++++++ doc/foot.5.scd | 9 +++++++++ footrc | 3 +++ input.c | 18 ++++++++++++++++++ wayland.h | 3 +++ 5 files changed, 39 insertions(+) (limited to 'config.c') diff --git a/config.c b/config.c index 62a060c0..19377b8a 100644 --- a/config.c +++ b/config.c @@ -58,8 +58,14 @@ static const char *binding_action_map[] = { [BIND_ACTION_FONT_SIZE_DOWN] = "font-decrease", [BIND_ACTION_FONT_SIZE_RESET] = "font-reset", [BIND_ACTION_SPAWN_TERMINAL] = "spawn-terminal", + [BIND_ACTION_MINIMIZE] = "minimize", + [BIND_ACTION_MAXIMIZE] = "maximize", + [BIND_ACTION_FULLSCREEN] = "fullscreen", }; +static_assert(ALEN(binding_action_map) == BIND_ACTION_COUNT, + "binding action map size mismatch"); + static char * get_shell(void) { diff --git a/doc/foot.5.scd b/doc/foot.5.scd index 2946ddc8..5c8becdf 100644 --- a/doc/foot.5.scd +++ b/doc/foot.5.scd @@ -194,6 +194,15 @@ Note that _Alt_ is usually called *Mod1*. *spawn-terminal* Spawns a new terminal. Default: _Control+Shift+Return_. +*minimize* + Minimizes the window. Default: _not bound_. + +*maximize* + Toggle the maximized state. Default: _not bound_. + +*fullscreen* + Toggles the fullscreen state. Default: _not bound_. + # FONT FORMAT The font is specified in FontConfig syntax. That is, a colon-separated diff --git a/footrc b/footrc index 60b2d516..468bc67d 100644 --- a/footrc +++ b/footrc @@ -53,3 +53,6 @@ # font-decrease=Control+minus Control+KP_Subtract # font-reset=Control+0 Control+KP_0 # spawn-terminal=Control+Shift+Return +# # minimize= +# # maximize= +# # fullscreen= diff --git a/input.c b/input.c index 96de8160..399d9d26 100644 --- a/input.c +++ b/input.c @@ -78,6 +78,24 @@ input_execute_binding(struct terminal *term, enum binding_action action, term_spawn_new(term); break; + case BIND_ACTION_MINIMIZE: + xdg_toplevel_set_minimized(term->window->xdg_toplevel); + break; + + case BIND_ACTION_MAXIMIZE: + if (term->window->is_maximized) + xdg_toplevel_unset_maximized(term->window->xdg_toplevel); + else + xdg_toplevel_set_maximized(term->window->xdg_toplevel); + break; + + case BIND_ACTION_FULLSCREEN: + if (term->window->is_fullscreen) + xdg_toplevel_unset_fullscreen(term->window->xdg_toplevel); + else + xdg_toplevel_set_fullscreen(term->window->xdg_toplevel, NULL); + break; + case BIND_ACTION_COUNT: assert(false); break; diff --git a/wayland.h b/wayland.h index bf765f04..a824c21f 100644 --- a/wayland.h +++ b/wayland.h @@ -52,6 +52,9 @@ enum binding_action { BIND_ACTION_FONT_SIZE_DOWN, BIND_ACTION_FONT_SIZE_RESET, BIND_ACTION_SPAWN_TERMINAL, + BIND_ACTION_MINIMIZE, + BIND_ACTION_MAXIMIZE, + BIND_ACTION_FULLSCREEN, BIND_ACTION_COUNT, }; -- cgit v1.3 From 9fae38a4b22afe0baf54d3a62e7f4df8a606b5e5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 12 Mar 2020 10:19:21 +0100 Subject: config: 'invalid key' is always an error, not a warning --- config.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 19377b8a..77b74227 100644 --- a/config.c +++ b/config.c @@ -258,7 +258,7 @@ parse_section_main(const char *key, const char *value, struct config *conf, } else { - LOG_WARN("%s:%u: invalid key: %s", path, lineno, key); + LOG_ERR("%s:%u: invalid key: %s", path, lineno, key); return false; } @@ -440,7 +440,7 @@ parse_section_csd(const char *key, const char *value, struct config *conf, } else { - LOG_WARN("%s:%u: invalid key: %s", path, lineno, key); + LOG_ERR("%s:%u: invalid key: %s", path, lineno, key); return false; } @@ -474,8 +474,7 @@ parse_section_key_bindings(const char *key, const char *value, struct config *co return true; } - - LOG_WARN("%s:%u: invalid key: %s", path, lineno, key); + LOG_ERR("%s:%u: invalid key: %s", path, lineno, key); return false; } -- cgit v1.3 From 45384839f0e9627cd5b376b045e11c14277d40d0 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 12 Mar 2020 10:20:05 +0100 Subject: config/input: implement mouse bindings * New config section, "mouse-bindings", where bindings are defined on the form "action=BTN_ * pointer_button() handler now scans the bindings list instead of hardcoding primary-paste to BTN_MIDDLE. * The implementation handles single- double- and triple clicks in the bindings, but there is currently no way to define anything but a single-click binding in the configuration. --- config.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++---------- config.h | 2 +- doc/foot.5.scd | 12 ++++++++++++ footrc | 3 +++ input.c | 30 ++++++++++++++++++++--------- wayland.h | 6 ++++++ 6 files changed, 94 insertions(+), 20 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 77b74227..7e1f8939 100644 --- a/config.c +++ b/config.c @@ -12,6 +12,7 @@ #include #include +#include #include #define LOG_MODULE "config" @@ -448,8 +449,9 @@ parse_section_csd(const char *key, const char *value, struct config *conf, } static bool -parse_section_key_bindings(const char *key, const char *value, struct config *conf, - const char *path, unsigned lineno) +parse_section_key_bindings( + const char *key, const char *value, struct config *conf, + const char *path, unsigned lineno) { for (enum binding_action action = 0; action < BIND_ACTION_COUNT; action++) { if (strcmp(key, binding_action_map[action]) != 0) @@ -459,7 +461,8 @@ parse_section_key_bindings(const char *key, const char *value, struct config *co struct xkb_keymap *keymap = xkb_keymap_new_from_names( ctx, &(struct xkb_rule_names){0}, XKB_KEYMAP_COMPILE_NO_FLAGS); - bool valid_combo = input_parse_key_binding_for_action(keymap, action, value, NULL); + bool valid_combo = input_parse_key_binding_for_action( + keymap, action, value, NULL); xkb_keymap_unref(keymap); xkb_context_unref(ctx); @@ -479,6 +482,43 @@ parse_section_key_bindings(const char *key, const char *value, struct config *co } +static bool +parse_section_mouse_bindings( + const char *key, const char *value, struct config *conf, + const char *path, unsigned lineno) +{ + for (enum binding_action action = 0; action < BIND_ACTION_COUNT; action++) { + if (strcmp(key, binding_action_map[action]) != 0) + continue; + + const char *map[] = { + [BTN_LEFT] = "BTN_LEFT", + [BTN_RIGHT] = "BTN_RIGHT", + [BTN_MIDDLE] = "BTN_MIDDLE", + [BTN_SIDE] = "BTN_SIDE", + [BTN_EXTRA] = "BTN_EXTRA", + [BTN_FORWARD] = "BTN_FORWARD", + [BTN_BACK] = "BTN_BACK", + [BTN_TASK] = "BTN_TASK", + }; + + for (size_t i = 0; i < ALEN(map); i++) { + if (map[i] == NULL || strcmp(map[i], value) != 0) + continue; + + conf->bindings.mouse[action] = (struct mouse_binding){i, 1, action}; + return true; + } + + LOG_ERR("%s:%d: invalid mouse button: %s", path, lineno, value); + return false; + + } + + LOG_ERR("%s:%u: invalid key: %s", path, lineno, key); + return false; +} + static bool parse_config_file(FILE *f, struct config *conf, const char *path) { @@ -488,6 +528,7 @@ parse_config_file(FILE *f, struct config *conf, const char *path) SECTION_CURSOR, SECTION_CSD, SECTION_KEY_BINDINGS, + SECTION_MOUSE_BINDINGS, SECTION_COUNT, } section = SECTION_MAIN; @@ -500,11 +541,12 @@ parse_config_file(FILE *f, struct config *conf, const char *path) parser_fun_t fun; const char *name; } section_info[] = { - [SECTION_MAIN] = {&parse_section_main, "main"}, - [SECTION_COLORS] = {&parse_section_colors, "colors"}, - [SECTION_CURSOR] = {&parse_section_cursor, "cursor"}, - [SECTION_CSD] = {&parse_section_csd, "csd"}, - [SECTION_KEY_BINDINGS] = {&parse_section_key_bindings, "key-bindings"}, + [SECTION_MAIN] = {&parse_section_main, "main"}, + [SECTION_COLORS] = {&parse_section_colors, "colors"}, + [SECTION_CURSOR] = {&parse_section_cursor, "cursor"}, + [SECTION_CSD] = {&parse_section_csd, "csd"}, + [SECTION_KEY_BINDINGS] = {&parse_section_key_bindings, "key-bindings"}, + [SECTION_MOUSE_BINDINGS] = {&parse_section_mouse_bindings, "mouse-bindings"}, }; static_assert(ALEN(section_info) == SECTION_COUNT, "section info array size mismatch"); @@ -699,7 +741,7 @@ config_load(struct config *conf, const char *conf_path) [BIND_ACTION_SPAWN_TERMINAL] = strdup("Control+Shift+Return"), }, .mouse = { - [BIND_ACTION_PRIMARY_PASTE] = strdup("BTN_MIDDLE"), + [BIND_ACTION_PRIMARY_PASTE] = {BTN_MIDDLE, 1, BIND_ACTION_PRIMARY_PASTE}, }, .search = { }, @@ -760,7 +802,6 @@ config_free(struct config conf) for (size_t i = 0; i < BIND_ACTION_COUNT; i++) { free(conf.bindings.key[i]); - free(conf.bindings.mouse[i]); free(conf.bindings.search[i]); } } diff --git a/config.h b/config.h index af078b00..bf205305 100644 --- a/config.h +++ b/config.h @@ -39,7 +39,7 @@ struct config { struct { /* Bindings for "normal" mode */ char *key[BIND_ACTION_COUNT]; - char *mouse[BIND_ACTION_COUNT]; + struct mouse_binding mouse[BIND_ACTION_COUNT]; /* * Special modes diff --git a/doc/foot.5.scd b/doc/foot.5.scd index 5c8becdf..dbbcc52d 100644 --- a/doc/foot.5.scd +++ b/doc/foot.5.scd @@ -203,6 +203,18 @@ Note that _Alt_ is usually called *Mod1*. *fullscreen* Toggles the fullscreen state. Default: _not bound_. +# SECTION: mouse-bindings + +This section lets you override the default mouse bindings. + +The general format is *action*=*BTN\_*, where *BTN\_* is +the name of the event code (e.g. *BTN\_LEFT*, *BTN\_RIGHT*). You can +find the event names using *libinput debug-events*. + +*primary-paste* + Pastes from the _primary selection_. Default: _BTN_MIDDLE_. + + # FONT FORMAT The font is specified in FontConfig syntax. That is, a colon-separated diff --git a/footrc b/footrc index 468bc67d..73512474 100644 --- a/footrc +++ b/footrc @@ -56,3 +56,6 @@ # # minimize= # # maximize= # # fullscreen= + +[mouse-bindings] +# primary-paste=BTN_MIDDLE diff --git a/input.c b/input.c index 399d9d26..8fbb02cd 100644 --- a/input.c +++ b/input.c @@ -31,6 +31,8 @@ #include "terminal.h" #include "vt.h" +#define ALEN(v) (sizeof(v) / sizeof(v[0])) + void input_execute_binding(struct terminal *term, enum binding_action action, uint32_t serial) @@ -54,8 +56,7 @@ input_execute_binding(struct terminal *term, enum binding_action action, break; case BIND_ACTION_PRIMARY_PASTE: - LOG_ERR("unimplemented"); - assert(false); + selection_from_primary(term); break; case BIND_ACTION_SEARCH_START: @@ -321,7 +322,6 @@ keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, static const struct key_data * keymap_data_for_sym(xkb_keysym_t sym, size_t *count) { -#define ALEN(a) (sizeof(a) / sizeof(a[0])) switch (sym) { case XKB_KEY_Escape: *count = ALEN(key_escape); return key_escape; case XKB_KEY_Return: *count = ALEN(key_return); return key_return; @@ -402,7 +402,6 @@ keymap_data_for_sym(xkb_keysym_t sym, size_t *count) case XKB_KEY_KP_9: *count = ALEN(key_kp_9); return key_kp_9; } - #undef ALEN return NULL; } @@ -1151,11 +1150,24 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, selection_mark_row(term, wayl->mouse.row, serial); break; } - } else { - if (wayl->mouse.count == 1 && button == BTN_MIDDLE && - selection_enabled(term)) - { - selection_from_primary(term); + } + + else { + for (size_t i = 0; i < ALEN(wayl->conf->bindings.mouse); i++) { + const struct mouse_binding *binding = + &wayl->conf->bindings.mouse[i]; + + if (binding->button != button) { + /* Wrong button */ + continue; + } + + if (binding->count != wayl->mouse.count) { + /* Not correct click count */ + continue; + } + + input_execute_binding(term, binding->action, serial); } selection_cancel(term); } diff --git a/wayland.h b/wayland.h index a824c21f..2ddfdf87 100644 --- a/wayland.h +++ b/wayland.h @@ -65,6 +65,12 @@ struct key_binding { }; typedef tll(struct key_binding) key_binding_list_t; +struct mouse_binding { + uint32_t button; + int count; + enum binding_action action; +}; + struct kbd { struct xkb_context *xkb; struct xkb_keymap *xkb_keymap; -- cgit v1.3 From c58f9a9ef93fd3fd2be711906b8214a53665e45f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 12 Mar 2020 10:46:27 +0100 Subject: config: mouse-bindings: verify button isn't already mapped A button may only be mapped to a single action. Detect when the user tried to map the same button to multiple actions and error out. To clear a binding (for example, to free up a button from the default bindings), one can set the action it is bound to to `NONE` (e.g. `primary-paste=NONE`). --- config.c | 26 +++++++++++++++++++++++++- doc/foot.5.scd | 14 ++++++++++---- input.c | 4 ++++ wayland.h | 1 + 4 files changed, 40 insertions(+), 5 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 7e1f8939..ba216348 100644 --- a/config.c +++ b/config.c @@ -49,6 +49,7 @@ static const uint32_t default_bright[] = { }; static const char *binding_action_map[] = { + [BIND_ACTION_NONE] = NULL, [BIND_ACTION_SCROLLBACK_UP] = "scrollback-up", [BIND_ACTION_SCROLLBACK_DOWN] = "scrollback-down", [BIND_ACTION_CLIPBOARD_COPY] = "clipboard-copy", @@ -454,6 +455,9 @@ parse_section_key_bindings( const char *path, unsigned lineno) { for (enum binding_action action = 0; action < BIND_ACTION_COUNT; action++) { + if (binding_action_map[action] == NULL) + continue; + if (strcmp(key, binding_action_map[action]) != 0) continue; @@ -488,9 +492,17 @@ parse_section_mouse_bindings( const char *path, unsigned lineno) { for (enum binding_action action = 0; action < BIND_ACTION_COUNT; action++) { + if (binding_action_map[action] == NULL) + continue; + if (strcmp(key, binding_action_map[action]) != 0) continue; + if (strcmp(value, "NONE") == 0) { + conf->bindings.mouse[action] = (struct mouse_binding){0, 0, BIND_ACTION_NONE}; + return true; + } + const char *map[] = { [BTN_LEFT] = "BTN_LEFT", [BTN_RIGHT] = "BTN_RIGHT", @@ -506,7 +518,19 @@ parse_section_mouse_bindings( if (map[i] == NULL || strcmp(map[i], value) != 0) continue; - conf->bindings.mouse[action] = (struct mouse_binding){i, 1, action}; + const int count = 1; + + /* Make sure button isn't already mapped to another action */ + for (enum binding_action j = 0; j < BIND_ACTION_COUNT; j++) { + const struct mouse_binding *collision = &conf->bindings.mouse[j]; + if (collision->button == i && collision->count == count) { + LOG_ERR("%s:%d: %s already mapped to %s", path, lineno, + value, binding_action_map[collision->action]); + return false; + } + } + + conf->bindings.mouse[action] = (struct mouse_binding){i, count, action}; return true; } diff --git a/doc/foot.5.scd b/doc/foot.5.scd index dbbcc52d..457d053b 100644 --- a/doc/foot.5.scd +++ b/doc/foot.5.scd @@ -151,15 +151,15 @@ component. This section lets you override the default key bindings. -The general format is *action*=*combo1*...*comboN*. That is, each +The general format is _action_=_combo1_..._comboN_. That is, each action may have one or more key combinations, space separated. Each -combination is on the form *mod1*+*mod2*+*key*. The names of the +combination is on the form _mod1_+_mod2_+_key_. The names of the modifiers and the key *must* be a valid XKB key name. Note that if *Shift* is one of the modifiers, the _key_ *must* be in upper case. -Note that _Alt_ is usually called *Mod1*. +Note that *Alt* is usually called *Mod1*. *scrollback-up* Scrolls up/back in history. Default: _Shift+Page_Up_. @@ -207,10 +207,16 @@ Note that _Alt_ is usually called *Mod1*. This section lets you override the default mouse bindings. -The general format is *action*=*BTN\_*, where *BTN\_* is +The general format is _action_=_BTN\__, where _BTN\__ is the name of the event code (e.g. *BTN\_LEFT*, *BTN\_RIGHT*). You can find the event names using *libinput debug-events*. +A button can only be mapped to *one* action. Lets say you want to bind +*BTN\_MIDDLE* to *fullscreen*. Since *BTN\_MIDDLE* is the default +binding for *primary-paste*, you first need to unmap the default +binding. This can be done by setting _action_=*NONE*; +e.g. *primary-paste*=*NONE*. + *primary-paste* Pastes from the _primary selection_. Default: _BTN_MIDDLE_. diff --git a/input.c b/input.c index 8fbb02cd..1c80dd8b 100644 --- a/input.c +++ b/input.c @@ -38,6 +38,9 @@ input_execute_binding(struct terminal *term, enum binding_action action, uint32_t serial) { switch (action) { + case BIND_ACTION_NONE: + break; + case BIND_ACTION_SCROLLBACK_UP: cmd_scrollback_up(term, term->rows); break; @@ -1168,6 +1171,7 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, } input_execute_binding(term, binding->action, serial); + break; } selection_cancel(term); } diff --git a/wayland.h b/wayland.h index 2ddfdf87..b8b91c00 100644 --- a/wayland.h +++ b/wayland.h @@ -42,6 +42,7 @@ struct monitor { }; enum binding_action { + BIND_ACTION_NONE, BIND_ACTION_SCROLLBACK_UP, BIND_ACTION_SCROLLBACK_DOWN, BIND_ACTION_CLIPBOARD_COPY, -- cgit v1.3 From 5cbd0527d703b2013b175d371de6c81aa7792863 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 12 Mar 2020 17:16:35 +0100 Subject: config: key-bindings: verify key combo isn't already mapped to another action --- config.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++----------- doc/foot.5.scd | 6 ++++++ 2 files changed, 53 insertions(+), 11 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index ba216348..bd237604 100644 --- a/config.c +++ b/config.c @@ -449,6 +449,47 @@ parse_section_csd(const char *key, const char *value, struct config *conf, return true; } +static bool +verify_key_combo(const struct config *conf, const char *combo, const char *path, unsigned lineno) +{ + for (enum binding_action action = 0; action < BIND_ACTION_COUNT; action++) { + if (conf->bindings.key[action] == NULL) + continue; + + char *copy = strdup(conf->bindings.key[action]); + + for (char *save = NULL, *collision = strtok_r(copy, " ", &save); + collision != NULL; + collision = strtok_r(NULL, " ", &save)) + { + if (strcmp(combo, collision) == 0) { + LOG_ERR("%s:%d: %s already mapped to %s", path, lineno, combo, binding_action_map[action]); + free(copy); + return false; + } + } + + free(copy); + } + + struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_keymap *keymap = xkb_keymap_new_from_names( + ctx, &(struct xkb_rule_names){0}, XKB_KEYMAP_COMPILE_NO_FLAGS); + + bool valid_combo = input_parse_key_binding_for_action( + keymap, BIND_ACTION_NONE, combo, NULL); + + xkb_keymap_unref(keymap); + xkb_context_unref(ctx); + + if (!valid_combo) { + LOG_ERR("%s:%d: invalid key combination: %s", path, lineno, combo); + return false; + } + + return true; +} + static bool parse_section_key_bindings( const char *key, const char *value, struct config *conf, @@ -461,18 +502,13 @@ parse_section_key_bindings( if (strcmp(key, binding_action_map[action]) != 0) continue; - struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - struct xkb_keymap *keymap = xkb_keymap_new_from_names( - ctx, &(struct xkb_rule_names){0}, XKB_KEYMAP_COMPILE_NO_FLAGS); - - bool valid_combo = input_parse_key_binding_for_action( - keymap, action, value, NULL); - - xkb_keymap_unref(keymap); - xkb_context_unref(ctx); + if (strcmp(value, "NONE") == 0) { + free(conf->bindings.key[action]); + conf->bindings.key[action] = NULL; + return true; + } - if (!valid_combo) { - LOG_ERR("%s:%d: invalid key combination: %s", path, lineno, value); + if (!verify_key_combo(conf, value, path, lineno)) { return false; } diff --git a/doc/foot.5.scd b/doc/foot.5.scd index 457d053b..d01a3d6e 100644 --- a/doc/foot.5.scd +++ b/doc/foot.5.scd @@ -161,6 +161,12 @@ upper case. Note that *Alt* is usually called *Mod1*. +A key combination can only be mapped to *one* action. Lets say you +want to bind *Control*+*Shift*+*R* to *fullscreen*. Since this is the +default shortcut for *search-start*, you first need to unmap the +default binding. This can be done by setting _action_=*NONE*; +e.g. *search-start*=*NONE*. + *scrollback-up* Scrolls up/back in history. Default: _Shift+Page_Up_. -- cgit v1.3