From 6ad90e63236408d4b661050a96d2486854c69cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 15 Sep 2020 18:33:58 +0200 Subject: [PATCH 1/7] =?UTF-8?q?input:=20don=E2=80=99t=20do=20grid=20scroll?= =?UTF-8?q?back=20if=20mouse=20events=20are=20being=20captured?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- input.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/input.c b/input.c index 65dbba0b..0f477acf 100644 --- a/input.c +++ b/input.c @@ -1660,9 +1660,8 @@ mouse_scroll(struct seat *seat, int amount) term_mouse_up( term, button, seat->mouse.row, seat->mouse.col, seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl); - } - - scrollback(term, amount); + } else + scrollback(term, amount); } } From 028bbd03a3284d60db802631a71ebb1626befa68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 15 Sep 2020 18:40:51 +0200 Subject: [PATCH 2/7] =?UTF-8?q?input:=20don=E2=80=99t=20do=20alternate=20m?= =?UTF-8?q?ode=20scrolling=20if=20application=20is=20grabbing=20mouse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- input.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/input.c b/input.c index 0f477acf..ab15fc46 100644 --- a/input.c +++ b/input.c @@ -1624,7 +1624,8 @@ mouse_scroll(struct seat *seat, int amount) amount = abs(amount); if ((button == BTN_BACK || button == BTN_FORWARD) && - term->grid == &term->alt && term->alt_scrolling) + term->grid == &term->alt && term->alt_scrolling && + term->mouse_tracking == MOUSE_NONE) { /* * alternateScroll/faux scrolling - translate mouse From cb41db85965698dcf34217f8d26d9163a72a2e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 15 Sep 2020 18:44:54 +0200 Subject: [PATCH 3/7] =?UTF-8?q?input:=20don=E2=80=99t=20use=20stale=20keyc?= =?UTF-8?q?odes=20for=20arrow-up/down?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The keycodes will change if the seat’s keymap changes. Make sure we only do alternate scrolling if the seat has a keyboard, and use the *current* layout’s keycodes for arrow up/down (instead of the *first* layout’s). --- input.c | 21 ++++++++++----------- wayland.h | 3 +++ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/input.c b/input.c index ab15fc46..e8c49d5d 100644 --- a/input.c +++ b/input.c @@ -498,6 +498,9 @@ keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, seat->kbd.mod_ctrl = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, "Control"); seat->kbd.mod_meta = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, "Mod4"); + seat->kbd.key_arrow_up = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "UP"); + seat->kbd.key_arrow_down = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "DOWN"); + /* Compose (dead keys) */ seat->kbd.xkb_compose_table = xkb_compose_table_new_from_locale( seat->kbd.xkb, setlocale(LC_CTYPE, NULL), XKB_COMPOSE_COMPILE_NO_FLAGS); @@ -1623,6 +1626,7 @@ mouse_scroll(struct seat *seat, int amount) amount = abs(amount); + if ((button == BTN_BACK || button == BTN_FORWARD) && term->grid == &term->alt && term->alt_scrolling && term->mouse_tracking == MOUSE_NONE) @@ -1632,19 +1636,14 @@ mouse_scroll(struct seat *seat, int amount) * "back"/"forward" to up/down keys */ - static xkb_keycode_t key_arrow_up = 0; - static xkb_keycode_t key_arrow_down = 0; + if (seat->wl_keyboard != NULL) { + xkb_keycode_t key = button == BTN_BACK + ? seat->kbd.key_arrow_up : seat->kbd.key_arrow_down; - if (key_arrow_up == 0) { - key_arrow_up = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "UP"); - key_arrow_down = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "DOWN"); + for (int i = 0; i < amount; i++) + keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_DOWN); + keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_UP); } - - xkb_keycode_t key = button == BTN_BACK ? key_arrow_up : key_arrow_down; - - for (int i = 0; i < amount; i++) - keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_DOWN); - keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_UP); } else { if (!term_mouse_grabbed(term, seat) && seat->mouse.col >= 0 && seat->mouse.row >= 0) diff --git a/wayland.h b/wayland.h index 430d6b66..7d512ba0 100644 --- a/wayland.h +++ b/wayland.h @@ -149,6 +149,9 @@ struct seat { xkb_mod_index_t mod_ctrl; xkb_mod_index_t mod_meta; + xkb_keycode_t key_arrow_up; + xkb_keycode_t key_arrow_down; + /* Enabled modifiers */ bool shift; bool alt; From 04c1bab7b0d24028403667d9081ca0081c947ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 15 Sep 2020 18:56:22 +0200 Subject: [PATCH 4/7] input: mouse_scroll(): refactor --- input.c | 71 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/input.c b/input.c index e8c49d5d..cfa99574 100644 --- a/input.c +++ b/input.c @@ -1613,6 +1613,20 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, } } +static void +alternate_scroll(struct seat *seat, int amount, int button) +{ + if (seat->wl_keyboard == NULL) + return; + + xkb_keycode_t key = button == BTN_BACK + ? seat->kbd.key_arrow_up : seat->kbd.key_arrow_down; + + for (int i = 0; i < amount; i++) + keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_DOWN); + keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_UP); +} + static void mouse_scroll(struct seat *seat, int amount) { @@ -1620,48 +1634,33 @@ mouse_scroll(struct seat *seat, int amount) assert(term != NULL); int button = amount < 0 ? BTN_BACK : BTN_FORWARD; - - void (*scrollback)(struct terminal *term, int rows) - = amount < 0 ? &cmd_scrollback_up : &cmd_scrollback_down; - amount = abs(amount); - - if ((button == BTN_BACK || button == BTN_FORWARD) && - term->grid == &term->alt && term->alt_scrolling && - term->mouse_tracking == MOUSE_NONE) - { - /* - * alternateScroll/faux scrolling - translate mouse - * "back"/"forward" to up/down keys - */ - - if (seat->wl_keyboard != NULL) { - xkb_keycode_t key = button == BTN_BACK - ? seat->kbd.key_arrow_up : seat->kbd.key_arrow_down; - - for (int i = 0; i < amount; i++) - keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_DOWN); - keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_UP); + if (term->mouse_tracking == MOUSE_NONE) { + if (term->grid == &term->alt) { + if (term->alt_scrolling) + alternate_scroll(seat, amount, button); + } else { + if (button == BTN_BACK) + cmd_scrollback_up(term, amount); + else + cmd_scrollback_down(term, amount); } - } else { - if (!term_mouse_grabbed(term, seat) && - seat->mouse.col >= 0 && seat->mouse.row >= 0) - { - assert(seat->mouse.col < term->cols); - assert(seat->mouse.row < term->rows); + } else if (!term_mouse_grabbed(term, seat) && + seat->mouse.col >= 0 && seat->mouse.row >= 0) + { + assert(seat->mouse.col < term->cols); + assert(seat->mouse.row < term->rows); - for (int i = 0; i < amount; i++) { - term_mouse_down( - term, button, seat->mouse.row, seat->mouse.col, - seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl); - } - - term_mouse_up( + for (int i = 0; i < amount; i++) { + term_mouse_down( term, button, seat->mouse.row, seat->mouse.col, seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl); - } else - scrollback(term, amount); + } + + term_mouse_up( + term, button, seat->mouse.row, seat->mouse.col, + seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl); } } From f2497320c68978fb4c25226966c11f115cb88e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 15 Sep 2020 19:09:00 +0200 Subject: [PATCH 5/7] config: add mouse.alternate-scroll-mode option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This option controls the initial state of the Alternate Scroll Mode, and defaults to ‘enabled’. --- CHANGELOG.md | 6 ++++++ config.c | 4 ++++ config.h | 1 + doc/foot.ini.5.scd | 18 ++++++++++++++++++ foot.ini | 1 + terminal.c | 1 + 6 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20a0a687..85e4facb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,12 @@ (https://codeberg.org/dnkl/foot/issues/128). * **scrollback-up-line** and **scrollback-down-line** key bindings. They scroll up/down a single line in the scrollback. +* **mouse.alternate-scroll-mode** option to `foot.ini`. This option + controls the initial state of the _Alternate Scroll Mode_, and + defaults to `yes`. When enabled, mouse events are translated to + up/down key events in the alternate screen, letting you scroll in + e.g. `less` and other applications without enabling native mouse + support in them (https://codeberg.org/dnkl/foot/issues/135). ### Removed diff --git a/config.c b/config.c index de4e24c9..418c9c37 100644 --- a/config.c +++ b/config.c @@ -764,6 +764,9 @@ parse_section_mouse(const char *key, const char *value, struct config *conf, if (strcmp(key, "hide-when-typing") == 0) conf->mouse.hide_when_typing = str_to_bool(value); + else if (strcmp(key, "alternate-scroll-mode") == 0) + conf->mouse.alternate_scroll_mode = str_to_bool(value); + else { LOG_AND_NOTIFY_ERR("%s:%d: [mouse]: %s: invalid key", path, lineno, key); return false; @@ -1907,6 +1910,7 @@ config_load(struct config *conf, const char *conf_path, }, .mouse = { .hide_when_typing = false, + .alternate_scroll_mode = true, }, .csd = { .preferred = CONF_CSD_PREFER_SERVER, diff --git a/config.h b/config.h index 9a897e62..1c0edc58 100644 --- a/config.h +++ b/config.h @@ -119,6 +119,7 @@ struct config { struct { bool hide_when_typing; + bool alternate_scroll_mode; } mouse; struct { diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 30c36b51..4513de05 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -143,6 +143,24 @@ applications can change these at runtime. *hide-when-typing* Boolean. When enabled, the mouse cursor is hidden while typing. +*alternate-scroll-mode* + Boolean. This option controls the initial value for the _alternate + scroll mode_. When this mode is enabled, mouse scroll events are + translated to _up_/_down_ key events when displaying the alternate + screen. + + This lets you scroll with the mouse in e.g. pagers (like _less_) + without enabling native mouse support in them. + + Alternate scrolling is *not* used if the application enables + native mouse support. + + This option can be modified by applications at run-time using the + escape sequences *CSI ? 1007 h* (enable) and *CSI ? 1007 l` + (disable). + + Default: _yes_. + # SECTION: colors diff --git a/foot.ini b/foot.ini index 7c49be5a..5e8dc47e 100644 --- a/foot.ini +++ b/foot.ini @@ -23,6 +23,7 @@ [mouse] # hide-when-typing=no +# alternate-scroll-mode=yes [colors] # alpha=1.0 diff --git a/terminal.c b/terminal.c index 9d68167d..5e0dc65a 100644 --- a/terminal.c +++ b/terminal.c @@ -964,6 +964,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .grid = &term->normal, .composed_count = 0, .composed = NULL, + .alt_scrolling = conf->mouse.alternate_scroll_mode, .meta = { .esc_prefix = true, .eight_bit = true, From 3e9186a04218e1093d0ed72160f4167a142c5b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 15 Sep 2020 19:29:38 +0200 Subject: [PATCH 6/7] changelog: mouse events -> mouse scroll events --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85e4facb..974d217a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,8 +75,8 @@ bindings. They scroll up/down a single line in the scrollback. * **mouse.alternate-scroll-mode** option to `foot.ini`. This option controls the initial state of the _Alternate Scroll Mode_, and - defaults to `yes`. When enabled, mouse events are translated to - up/down key events in the alternate screen, letting you scroll in + defaults to `yes`. When enabled, mouse scroll events are translated + to up/down key events in the alternate screen, letting you scroll in e.g. `less` and other applications without enabling native mouse support in them (https://codeberg.org/dnkl/foot/issues/135). From f30d7cce2700bf6da156bc392cd6a4c46b981251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 16 Sep 2020 08:05:38 +0200 Subject: [PATCH 7/7] doc: foot.ini: alternate-scroll-mode: fix highlighting of CSI escape --- doc/foot.ini.5.scd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 4513de05..7467d0b8 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -156,7 +156,7 @@ applications can change these at runtime. native mouse support. This option can be modified by applications at run-time using the - escape sequences *CSI ? 1007 h* (enable) and *CSI ? 1007 l` + escape sequences *CSI ? 1007 h* (enable) and *CSI ? 1007 l* (disable). Default: _yes_.