input: mouse_scroll(): refactor

This commit is contained in:
Daniel Eklöf 2020-09-15 18:56:22 +02:00
parent cb41db8596
commit 04c1bab7b0
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

49
input.c
View file

@ -1614,38 +1614,39 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
} }
static void static void
mouse_scroll(struct seat *seat, int amount) alternate_scroll(struct seat *seat, int amount, int button)
{ {
struct terminal *term = seat->mouse_focus; if (seat->wl_keyboard == NULL)
assert(term != NULL); return;
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 xkb_keycode_t key = button == BTN_BACK
? seat->kbd.key_arrow_up : seat->kbd.key_arrow_down; ? seat->kbd.key_arrow_up : seat->kbd.key_arrow_down;
for (int i = 0; i < amount; i++) 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_DOWN);
keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_UP); keyboard_key(seat, NULL, seat->kbd.serial, 0, key - 8, XKB_KEY_UP);
} }
static void
mouse_scroll(struct seat *seat, int amount)
{
struct terminal *term = seat->mouse_focus;
assert(term != NULL);
int button = amount < 0 ? BTN_BACK : BTN_FORWARD;
amount = abs(amount);
if (term->mouse_tracking == MOUSE_NONE) {
if (term->grid == &term->alt) {
if (term->alt_scrolling)
alternate_scroll(seat, amount, button);
} else { } else {
if (!term_mouse_grabbed(term, seat) && 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) seat->mouse.col >= 0 && seat->mouse.row >= 0)
{ {
assert(seat->mouse.col < term->cols); assert(seat->mouse.col < term->cols);
@ -1660,8 +1661,6 @@ mouse_scroll(struct seat *seat, int amount)
term_mouse_up( term_mouse_up(
term, button, seat->mouse.row, seat->mouse.col, term, button, seat->mouse.row, seat->mouse.col,
seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl); seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl);
} else
scrollback(term, amount);
} }
} }