Skip to content

Commit

Permalink
swaybar: Fix scrolling with precise trackpads
Browse files Browse the repository at this point in the history
  • Loading branch information
mortie committed Mar 3, 2020
1 parent 476773d commit bdfa665
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions include/swaybar/input.h
Expand Up @@ -2,6 +2,7 @@
#define _SWAYBAR_INPUT_H

#include <wayland-client.h>
#include <stdbool.h>
#include "list.h"

#define SWAY_SCROLL_UP KEY_MAX + 1
Expand Down Expand Up @@ -56,6 +57,9 @@ struct swaybar_seat {
struct wl_seat *wl_seat;
struct swaybar_pointer pointer;
struct swaybar_touch touch;
uint32_t scroll_time;
bool scroll_is_discrete_horizontal;
bool scroll_is_discrete_vertical;
struct wl_list link; // swaybar_seat:link
};

Expand Down
2 changes: 1 addition & 1 deletion swaybar/bar.c
Expand Up @@ -340,7 +340,7 @@ static void handle_global(void *data, struct wl_registry *registry,
}
seat->bar = bar;
seat->wl_name = name;
seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 3);
seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 5);
wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
wl_list_insert(&bar->seats, &seat->link);
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
Expand Down
26 changes: 24 additions & 2 deletions swaybar/input.c
Expand Up @@ -246,7 +246,24 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
return;
}

workspace_next(seat->bar, output, amt < 0.0);
// If the scroll device is discrete, we don't need to do anything fancy.
// If it's continuous, do our best to emulate discrete scrolling.
bool do_scroll =
(axis == WL_POINTER_AXIS_VERTICAL_SCROLL && seat->scroll_is_discrete_vertical) ||
(axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL && seat->scroll_is_discrete_horizontal) ||
fabs(amt) >= 10 ||
(fabs(amt) >= 1 && time >= seat->scroll_time + 200);

// Clear flags in case the next event isn't discrete
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
seat->scroll_is_discrete_vertical = false;
else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
seat->scroll_is_discrete_horizontal = false;

if (do_scroll) {
seat->scroll_time = time;
workspace_next(seat->bar, output, amt < 0.0);
}

// Check button release bindings
check_bindings(seat->bar, button, WL_POINTER_BUTTON_STATE_RELEASED);
Expand All @@ -268,7 +285,12 @@ static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,

static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
uint32_t axis, int32_t discrete) {
// Who cares
struct swaybar_seat *seat = data;
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
seat->scroll_is_discrete_vertical = true;
} else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
seat->scroll_is_discrete_horizontal = true;
}
}

static struct wl_pointer_listener pointer_listener = {
Expand Down

0 comments on commit bdfa665

Please sign in to comment.