From e55a845920cc06af664a51562be9927428adf5b5 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 19 Jul 2020 20:39:18 +0000 Subject: [PATCH] Add discrete pointer scroll --- include/pointer.h | 7 +++++++ src/main.c | 34 +++++++++++++++++++++++++++++++++- src/pointer.c | 18 ++++++++++++++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/include/pointer.h b/include/pointer.h index a426602..a51640b 100644 --- a/include/pointer.h +++ b/include/pointer.h @@ -30,6 +30,10 @@ enum pointer_button_mask { POINTER_BUTTON_MIDDLE = 1 << 1, POINTER_BUTTON_RIGHT = 1 << 2, // TODO: More buttons + POINTER_SCROLL_UP = 1 << 3, + POINTER_SCROLL_DOWN = 1 << 4, + POINTER_SCROLL_LEFT = 1 << 5, + POINTER_SCROLL_RIGHT = 1 << 6, }; struct pointer { @@ -40,6 +44,9 @@ struct pointer { enum pointer_button_mask pressed; wl_fixed_t x, y; + int vertical_scroll_steps; + int horizontal_scroll_steps; + struct wl_cursor_theme* cursor_theme; struct wl_surface* cursor_surface; diff --git a/src/main.c b/src/main.c index 1e63e96..5322cc6 100644 --- a/src/main.c +++ b/src/main.c @@ -365,7 +365,39 @@ void on_pointer_event(struct pointer_collection* collection, int x = wl_fixed_to_int(pointer->x); int y = wl_fixed_to_int(pointer->y); - vnc_client_send_pointer_event(client, x, y, pointer->pressed); + enum pointer_button_mask pressed = pointer->pressed; + int vertical_steps = pointer->vertical_scroll_steps; + int horizontal_steps = pointer->horizontal_scroll_steps; + + if (!vertical_steps && !horizontal_steps) { + vnc_client_send_pointer_event(client, x, y, pressed); + return; + } + + enum pointer_button_mask scroll_mask = 0; + if (vertical_steps < 0) { + vertical_steps *= -1; + scroll_mask |= POINTER_SCROLL_UP; + } else { + scroll_mask |= POINTER_SCROLL_DOWN; + } + + if (horizontal_steps < 0) { + horizontal_steps *= -1; + scroll_mask |= POINTER_SCROLL_LEFT; + } else { + scroll_mask |= POINTER_SCROLL_RIGHT; + } + + while (horizontal_steps > 0 || vertical_steps > 0) { + vnc_client_send_pointer_event(client, x, y, pressed | scroll_mask); + vnc_client_send_pointer_event(client, x, y, pressed); + + if (--vertical_steps <= 0) + scroll_mask &= ~(POINTER_SCROLL_UP | POINTER_SCROLL_DOWN); + if (--horizontal_steps <= 0) + scroll_mask &= ~(POINTER_SCROLL_LEFT | POINTER_SCROLL_RIGHT); + } } void on_keyboard_event(struct keyboard_collection* collection, diff --git a/src/pointer.c b/src/pointer.c index 69fe30d..82daea9 100644 --- a/src/pointer.c +++ b/src/pointer.c @@ -221,7 +221,6 @@ static void pointer_button(void* data, struct wl_pointer* wl_pointer, case BTN_MIDDLE: pointer_set_button_state(pointer, POINTER_BUTTON_MIDDLE, state); break; - // TODO: More buttons } } @@ -247,7 +246,19 @@ static void pointer_axis_stop(void* data, struct wl_pointer* wl_pointer, static void pointer_axis_discrete(void* data, struct wl_pointer* wl_pointer, enum wl_pointer_axis axis, int steps) { - // TODO + struct pointer_collection* self = data; + struct pointer* pointer = + pointer_collection_find_wl_pointer(self, wl_pointer); + + switch (axis) { + case WL_POINTER_AXIS_VERTICAL_SCROLL: + pointer->vertical_scroll_steps += steps; + break; + case WL_POINTER_AXIS_HORIZONTAL_SCROLL: + pointer->horizontal_scroll_steps += steps; + break; + default:; + } } static void pointer_frame(void* data, struct wl_pointer* wl_pointer) @@ -257,6 +268,9 @@ static void pointer_frame(void* data, struct wl_pointer* wl_pointer) pointer_collection_find_wl_pointer(self, wl_pointer); self->on_frame(self, pointer); + + pointer->vertical_scroll_steps = 0; + pointer->horizontal_scroll_steps = 0; } static struct wl_pointer_listener pointer_listener = {