From 5dca0458a09ebc586fda49c50533976f055d8de2 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 26 Jun 2021 22:15:09 +0100 Subject: log: add LOG_CLASS_NONE and use as initializer for log_level This means that logging will be completely disabled until log_init() has been called, which is useful to prevent log spam when running UNITTEST{} blocks in debug builds. Note that this doesn't change the default log level at runtime, which was already being set to LOG_CLASS_INFO in main.c and client.c. The new log level is also exposed to the command-line interface as `--log-level=none`, which allows disabling logging entirely. --- config.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index a95f8485..c417fb19 100644 --- a/config.c +++ b/config.c @@ -103,8 +103,10 @@ log_and_notify(struct config *conf, enum log_class log_class, case LOG_CLASS_INFO: case LOG_CLASS_DEBUG: - BUG("unsupported log class: %d", log_class); - break; + case LOG_CLASS_NONE: + default: + BUG("unsupported log class: %d", (int)log_class); + return; } va_list va1, va2; -- cgit v1.3 From 149c52bd4496eb4dd13eaa53bec690c60499502a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 29 Jun 2021 18:08:15 +0200 Subject: config: remove replaced/removed key bindings, instead of marking as ‘unused’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of keeping removed/replaced key bindings in the key binding array (marked as ‘unused’), remove them, by compacting the array. The invariant is thus that there should be *no* entries in the key binding list with the `BIND_ACTION_NONE` for action. Add code to debug builds that verifies this, plus a unit test. Closes #614 --- CHANGELOG.md | 2 + config.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++---------- input.c | 6 --- 3 files changed, 134 insertions(+), 32 deletions(-) (limited to 'config.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index a44d8105..78016bae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ ### Fixed * Grapheme cluster state being reset between codepoints. +* Regression: custom URL key bindings not working + (https://codeberg.org/dnkl/foot/issues/614). ### Security 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); diff --git a/input.c b/input.c index 7db3ca85..9cf18b53 100644 --- a/input.c +++ b/input.c @@ -529,8 +529,6 @@ convert_key_bindings(const struct config *conf, struct seat *seat) { for (size_t i = 0; i < conf->bindings.key.count; i++) { const struct config_key_binding *binding = &conf->bindings.key.arr[i]; - if (binding->action == BIND_ACTION_NONE) - continue; convert_key_binding(seat, binding, &seat->kbd.bindings.key); } } @@ -540,8 +538,6 @@ convert_search_bindings(const struct config *conf, struct seat *seat) { for (size_t i = 0; i < conf->bindings.search.count; i++) { const struct config_key_binding *binding = &conf->bindings.search.arr[i]; - if (binding->action == BIND_ACTION_SEARCH_NONE) - continue; convert_key_binding(seat, binding, &seat->kbd.bindings.search); } } @@ -551,8 +547,6 @@ convert_url_bindings(const struct config *conf, struct seat *seat) { for (size_t i = 0; i < conf->bindings.url.count; i++) { const struct config_key_binding *binding = &conf->bindings.url.arr[i]; - if (binding->action == BIND_ACTION_URL_NONE) - continue; convert_key_binding(seat, binding, &seat->kbd.bindings.url); } } -- cgit v1.3 From 88fb8429b0d0abeed7087373241dd57918550974 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Jun 2021 19:10:37 +0200 Subject: config: NOINLINE a couple of functions doing tll operations --- config.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 2fa9100e..5fd68a2d 100644 --- a/config.c +++ b/config.c @@ -193,7 +193,7 @@ struct path_component { }; typedef tll(struct path_component) path_components_t; -static void +static void NOINLINE path_component_add(path_components_t *components, const char *comp, int fd) { xassert(comp != NULL); @@ -203,14 +203,14 @@ path_component_add(path_components_t *components, const char *comp, int fd) tll_push_back(*components, pc); } -static void +static void NOINLINE path_component_destroy(struct path_component *component) { xassert(component->fd >= 0); close(component->fd); } -static void +static void NOINLINE path_components_destroy(path_components_t *components) { tll_foreach(*components, it) { @@ -1662,7 +1662,7 @@ pipe_argv_from_string(const char *value, char ***argv, return remove_len; } -static void +static void NOINLINE remove_action_from_key_bindings_list(struct config_key_binding_list *bindings, int action, char **pipe_argv) { -- cgit v1.3 From 55bd3cfd623a86044fd028787c216810ee274af7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Jun 2021 21:06:21 +0200 Subject: config: add unit test for config_clone() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We’re mainly interested in seeing that we don’t leak memory, or double free heap allocated data. Valgrind and/or gcc/clang sanitizers are best at this, hence the very few asserts in the test itself. --- config.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'config.c') diff --git a/config.c b/config.c index 5fd68a2d..372e9f9c 100644 --- a/config.c +++ b/config.c @@ -3140,6 +3140,27 @@ config_clone(const struct config *old) return conf; } +UNITTEST +{ + struct config original; + user_notifications_t nots = tll_init(); + config_override_t overrides = tll_init(); + + bool ret = config_load(&original, "/dev/null", ¬s, &overrides, false); + xassert(ret); + + struct config *clone = config_clone(&original); + xassert(clone != NULL); + xassert(clone != &original); + + config_free(original); + config_free(*clone); + free(clone); + + tll_free(overrides); + tll_free(nots); +} + void config_free(struct config conf) { -- cgit v1.3 From 9817e44c325f5d44f64bfa768107998134ea7f59 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Jun 2021 18:00:33 +0200 Subject: config: add tweak.grapheme-width-method=wcswidth|at-most-2 --- CHANGELOG.md | 3 +++ config.c | 10 ++++++++++ config.h | 1 + doc/foot.ini.5.scd | 18 ++++++++++++++++++ vt.c | 19 ++++++++++++++----- 5 files changed, 46 insertions(+), 5 deletions(-) (limited to 'config.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index 78016bae..9337652b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,9 @@ terminfo entries. This should make 24-bit RGB colors work in tmux and neovim, without the need for config hacks or detection heuristics (https://codeberg.org/dnkl/foot/issues/615). +* `[tweak].grapheme-width-method=wcswidth|at-most-2` option to + `foot.ini`. + ### Changed ### Deprecated diff --git a/config.c b/config.c index 372e9f9c..cb407c24 100644 --- a/config.c +++ b/config.c @@ -2255,6 +2255,15 @@ parse_section_tweak( LOG_WARN("tweak: grapheme shaping"); } + else if (strcmp(key, "grapheme-width-method") == 0) { + if (strcmp(value, "at-most-2") == 0) + conf->tweak.grapheme_width_method = GRAPHEME_WIDTH_MAX_2; + else if (strcmp(value, "wcswidth") == 0) + conf->tweak.grapheme_width_method = GRAPHEME_WIDTH_WCSWIDTH; + + LOG_WARN("%s:%d [tweak]: grapheme-width-method=%s", path, lineno, value); + } + else if (strcmp(key, "render-timer") == 0) { if (strcmp(value, "none") == 0) { conf->tweak.render_timer_osd = false; @@ -2823,6 +2832,7 @@ config_load(struct config *conf, const char *conf_path, .fcft_filter = FCFT_SCALING_FILTER_LANCZOS3, .allow_overflowing_double_width_glyphs = true, .grapheme_shaping = false, + .grapheme_width_method = GRAPHEME_WIDTH_MAX_2, .delayed_render_lower_ns = 500000, /* 0.5ms */ .delayed_render_upper_ns = 16666666 / 2, /* half a frame period (60Hz) */ .max_shm_pool_size = 512 * 1024 * 1024, diff --git a/config.h b/config.h index ee427267..57305b22 100644 --- a/config.h +++ b/config.h @@ -246,6 +246,7 @@ struct config { enum fcft_scaling_filter fcft_filter; bool allow_overflowing_double_width_glyphs; bool grapheme_shaping; + enum {GRAPHEME_WIDTH_WCSWIDTH, GRAPHEME_WIDTH_MAX_2} grapheme_width_method; bool render_timer_osd; bool render_timer_log; bool damage_whole_window; diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 2e408b3a..a0829ad7 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -997,8 +997,26 @@ any of these options. but is necessary to not break cursor synchronization with the application running in foot. + See also: *grapheme-width-method*. + Default: _no_ +*grapheme-width-method* + Selects which method to use when calculating the width + (i.e. number of columns) of a grapheme cluster. One of *at-most-2* + and *wcswidth*. + + *wcswidth* simply adds together the individual width of all + codepoints making up the cluster. + + *at-most-2* does the same, but limits the maximum number of + columns to 2. This is more correct, but is likely to break + applications since applications typically use *wcswidth*(3) + internally to calculate the width. This results in cursor + de-synchronization issues. + + Default: _at-most-2_ + *max-shm-pool-size-mb* This option controls the amount of virtual address space used by the pixmap memory to which the terminal screen content is diff --git a/vt.c b/vt.c index 5f2b3c0d..6ef2b782 100644 --- a/vt.c +++ b/vt.c @@ -764,17 +764,26 @@ action_utf8_print(struct terminal *term, wchar_t wc) (wanted_count - 2) * sizeof(new_cc->chars[0])); } - int grapheme_width = composed != NULL ? composed->width : base_width; + const int grapheme_width = + composed != NULL ? composed->width : base_width; + + switch (term->conf->tweak.grapheme_width_method) { + case GRAPHEME_WIDTH_MAX_2: + if (unlikely(wc == 0xfe0f)) + width = 2; + new_cc->width = min(grapheme_width + width, 2); + break; - if (wc == 0xfe0f) - width = 2; - new_cc->width = min(max(grapheme_width, width), 2); + case GRAPHEME_WIDTH_WCSWIDTH: + new_cc->width = grapheme_width + width; + break; + } term->composed_count++; composed_insert(&term->composed, new_cc); wc = CELL_COMB_CHARS_LO + key; - width = grapheme_width; + width = new_cc->width; xassert(wc >= CELL_COMB_CHARS_LO); xassert(wc <= CELL_COMB_CHARS_HI); -- cgit v1.3 From 5138f022146e447d8bb0be56a06e9065b35b6489 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 1 Jul 2021 08:00:23 +0200 Subject: config: rename at-most-2 (value for grapheme-width-method) to double-width --- CHANGELOG.md | 2 +- config.c | 6 +++--- config.h | 2 +- doc/foot.ini.5.scd | 8 ++++---- vt.c | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) (limited to 'config.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index 9337652b..0b79b0ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,7 @@ terminfo entries. This should make 24-bit RGB colors work in tmux and neovim, without the need for config hacks or detection heuristics (https://codeberg.org/dnkl/foot/issues/615). -* `[tweak].grapheme-width-method=wcswidth|at-most-2` option to +* `[tweak].grapheme-width-method=wcswidth|double-width` option to `foot.ini`. diff --git a/config.c b/config.c index cb407c24..e02ca3de 100644 --- a/config.c +++ b/config.c @@ -2256,8 +2256,8 @@ parse_section_tweak( } else if (strcmp(key, "grapheme-width-method") == 0) { - if (strcmp(value, "at-most-2") == 0) - conf->tweak.grapheme_width_method = GRAPHEME_WIDTH_MAX_2; + if (strcmp(value, "double-width") == 0) + conf->tweak.grapheme_width_method = GRAPHEME_WIDTH_DOUBLE; else if (strcmp(value, "wcswidth") == 0) conf->tweak.grapheme_width_method = GRAPHEME_WIDTH_WCSWIDTH; @@ -2832,7 +2832,7 @@ config_load(struct config *conf, const char *conf_path, .fcft_filter = FCFT_SCALING_FILTER_LANCZOS3, .allow_overflowing_double_width_glyphs = true, .grapheme_shaping = false, - .grapheme_width_method = GRAPHEME_WIDTH_MAX_2, + .grapheme_width_method = GRAPHEME_WIDTH_DOUBLE, .delayed_render_lower_ns = 500000, /* 0.5ms */ .delayed_render_upper_ns = 16666666 / 2, /* half a frame period (60Hz) */ .max_shm_pool_size = 512 * 1024 * 1024, diff --git a/config.h b/config.h index 57305b22..2300a0ad 100644 --- a/config.h +++ b/config.h @@ -246,7 +246,7 @@ struct config { enum fcft_scaling_filter fcft_filter; bool allow_overflowing_double_width_glyphs; bool grapheme_shaping; - enum {GRAPHEME_WIDTH_WCSWIDTH, GRAPHEME_WIDTH_MAX_2} grapheme_width_method; + enum {GRAPHEME_WIDTH_WCSWIDTH, GRAPHEME_WIDTH_DOUBLE} grapheme_width_method; bool render_timer_osd; bool render_timer_log; bool damage_whole_window; diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index a0829ad7..684497ca 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -1003,19 +1003,19 @@ any of these options. *grapheme-width-method* Selects which method to use when calculating the width - (i.e. number of columns) of a grapheme cluster. One of *at-most-2* - and *wcswidth*. + (i.e. number of columns) of a grapheme cluster. One of + *double-width* and *wcswidth*. *wcswidth* simply adds together the individual width of all codepoints making up the cluster. - *at-most-2* does the same, but limits the maximum number of + *double-width* does the same, but limits the maximum number of columns to 2. This is more correct, but is likely to break applications since applications typically use *wcswidth*(3) internally to calculate the width. This results in cursor de-synchronization issues. - Default: _at-most-2_ + Default: _double-width_ *max-shm-pool-size-mb* This option controls the amount of virtual address space used by diff --git a/vt.c b/vt.c index 6ef2b782..b9a4f4b0 100644 --- a/vt.c +++ b/vt.c @@ -768,7 +768,7 @@ action_utf8_print(struct terminal *term, wchar_t wc) composed != NULL ? composed->width : base_width; switch (term->conf->tweak.grapheme_width_method) { - case GRAPHEME_WIDTH_MAX_2: + case GRAPHEME_WIDTH_DOUBLE: if (unlikely(wc == 0xfe0f)) width = 2; new_cc->width = min(grapheme_width + width, 2); -- cgit v1.3