diff options
Diffstat (limited to 'swaybar/input.c')
-rw-r--r-- | swaybar/input.c | 84 |
1 files changed, 37 insertions, 47 deletions
diff --git a/swaybar/input.c b/swaybar/input.c index 263d0253..bdd55e58 100644 --- a/swaybar/input.c +++ b/swaybar/input.c | |||
@@ -1,9 +1,5 @@ | |||
1 | #include <assert.h> | 1 | #include <assert.h> |
2 | #ifdef __FreeBSD__ | ||
3 | #include <dev/evdev/input-event-codes.h> | ||
4 | #else | ||
5 | #include <linux/input-event-codes.h> | 2 | #include <linux/input-event-codes.h> |
6 | #endif | ||
7 | #include <stdlib.h> | 3 | #include <stdlib.h> |
8 | #include <wayland-client.h> | 4 | #include <wayland-client.h> |
9 | #include <wayland-cursor.h> | 5 | #include <wayland-cursor.h> |
@@ -26,33 +22,39 @@ void free_hotspots(struct wl_list *list) { | |||
26 | } | 22 | } |
27 | } | 23 | } |
28 | 24 | ||
29 | static enum x11_button wl_button_to_x11_button(uint32_t button) { | 25 | static uint32_t wl_axis_to_button(uint32_t axis, wl_fixed_t value) { |
30 | switch (button) { | 26 | bool negative = wl_fixed_to_double(value) < 0; |
31 | case BTN_LEFT: | ||
32 | return LEFT; | ||
33 | case BTN_MIDDLE: | ||
34 | return MIDDLE; | ||
35 | case BTN_RIGHT: | ||
36 | return RIGHT; | ||
37 | case BTN_SIDE: | ||
38 | return BACK; | ||
39 | case BTN_EXTRA: | ||
40 | return FORWARD; | ||
41 | default: | ||
42 | return NONE; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | static enum x11_button wl_axis_to_x11_button(uint32_t axis, wl_fixed_t value) { | ||
47 | switch (axis) { | 27 | switch (axis) { |
48 | case WL_POINTER_AXIS_VERTICAL_SCROLL: | 28 | case WL_POINTER_AXIS_VERTICAL_SCROLL: |
49 | return wl_fixed_to_double(value) < 0 ? SCROLL_UP : SCROLL_DOWN; | 29 | return negative ? SWAY_SCROLL_UP : SWAY_SCROLL_DOWN; |
50 | case WL_POINTER_AXIS_HORIZONTAL_SCROLL: | 30 | case WL_POINTER_AXIS_HORIZONTAL_SCROLL: |
51 | return wl_fixed_to_double(value) < 0 ? SCROLL_LEFT : SCROLL_RIGHT; | 31 | return negative ? SWAY_SCROLL_LEFT : SWAY_SCROLL_RIGHT; |
52 | default: | 32 | default: |
53 | wlr_log(WLR_DEBUG, "Unexpected axis value on mouse scroll"); | 33 | wlr_log(WLR_DEBUG, "Unexpected axis value on mouse scroll"); |
54 | return NONE; | 34 | return 0; |
35 | } | ||
36 | } | ||
37 | |||
38 | void update_cursor(struct swaybar *bar) { | ||
39 | struct swaybar_pointer *pointer = &bar->pointer; | ||
40 | if (pointer->cursor_theme) { | ||
41 | wl_cursor_theme_destroy(pointer->cursor_theme); | ||
55 | } | 42 | } |
43 | int scale = pointer->current ? pointer->current->scale : 1; | ||
44 | pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * scale, bar->shm); | ||
45 | struct wl_cursor *cursor; | ||
46 | cursor = wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); | ||
47 | pointer->cursor_image = cursor->images[0]; | ||
48 | wl_surface_set_buffer_scale(pointer->cursor_surface, scale); | ||
49 | wl_surface_attach(pointer->cursor_surface, | ||
50 | wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); | ||
51 | wl_pointer_set_cursor(pointer->pointer, pointer->serial, | ||
52 | pointer->cursor_surface, | ||
53 | pointer->cursor_image->hotspot_x / scale, | ||
54 | pointer->cursor_image->hotspot_y / scale); | ||
55 | wl_surface_damage_buffer(pointer->cursor_surface, 0, 0, | ||
56 | INT32_MAX, INT32_MAX); | ||
57 | wl_surface_commit(pointer->cursor_surface); | ||
56 | } | 58 | } |
57 | 59 | ||
58 | static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, | 60 | static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, |
@@ -60,6 +62,7 @@ static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, | |||
60 | wl_fixed_t surface_x, wl_fixed_t surface_y) { | 62 | wl_fixed_t surface_x, wl_fixed_t surface_y) { |
61 | struct swaybar *bar = data; | 63 | struct swaybar *bar = data; |
62 | struct swaybar_pointer *pointer = &bar->pointer; | 64 | struct swaybar_pointer *pointer = &bar->pointer; |
65 | pointer->serial = serial; | ||
63 | struct swaybar_output *output; | 66 | struct swaybar_output *output; |
64 | wl_list_for_each(output, &bar->outputs, link) { | 67 | wl_list_for_each(output, &bar->outputs, link) { |
65 | if (output->surface == surface) { | 68 | if (output->surface == surface) { |
@@ -67,20 +70,7 @@ static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, | |||
67 | break; | 70 | break; |
68 | } | 71 | } |
69 | } | 72 | } |
70 | int max_scale = 1; | 73 | update_cursor(bar); |
71 | struct swaybar_output *_output; | ||
72 | wl_list_for_each(_output, &bar->outputs, link) { | ||
73 | if (_output->scale > max_scale) { | ||
74 | max_scale = _output->scale; | ||
75 | } | ||
76 | } | ||
77 | wl_surface_set_buffer_scale(pointer->cursor_surface, max_scale); | ||
78 | wl_surface_attach(pointer->cursor_surface, | ||
79 | wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); | ||
80 | wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, | ||
81 | pointer->cursor_image->hotspot_x / max_scale, | ||
82 | pointer->cursor_image->hotspot_y / max_scale); | ||
83 | wl_surface_commit(pointer->cursor_surface); | ||
84 | } | 74 | } |
85 | 75 | ||
86 | static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, | 76 | static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, |
@@ -96,12 +86,12 @@ static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, | |||
96 | bar->pointer.y = wl_fixed_to_int(surface_y); | 86 | bar->pointer.y = wl_fixed_to_int(surface_y); |
97 | } | 87 | } |
98 | 88 | ||
99 | static bool check_bindings(struct swaybar *bar, uint32_t x11_button, | 89 | static bool check_bindings(struct swaybar *bar, uint32_t button, |
100 | uint32_t state) { | 90 | uint32_t state) { |
101 | bool released = state == WL_POINTER_BUTTON_STATE_RELEASED; | 91 | bool released = state == WL_POINTER_BUTTON_STATE_RELEASED; |
102 | for (int i = 0; i < bar->config->bindings->length; i++) { | 92 | for (int i = 0; i < bar->config->bindings->length; i++) { |
103 | struct swaybar_binding *binding = bar->config->bindings->items[i]; | 93 | struct swaybar_binding *binding = bar->config->bindings->items[i]; |
104 | if (binding->button == x11_button && binding->release == released) { | 94 | if (binding->button == button && binding->release == released) { |
105 | ipc_execute_binding(bar, binding); | 95 | ipc_execute_binding(bar, binding); |
106 | return true; | 96 | return true; |
107 | } | 97 | } |
@@ -118,7 +108,7 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, | |||
118 | return; | 108 | return; |
119 | } | 109 | } |
120 | 110 | ||
121 | if (check_bindings(bar, wl_button_to_x11_button(button), state)) { | 111 | if (check_bindings(bar, button, state)) { |
122 | return; | 112 | return; |
123 | } | 113 | } |
124 | 114 | ||
@@ -133,8 +123,8 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, | |||
133 | && y >= hotspot->y | 123 | && y >= hotspot->y |
134 | && x < hotspot->x + hotspot->width | 124 | && x < hotspot->x + hotspot->width |
135 | && y < hotspot->y + hotspot->height) { | 125 | && y < hotspot->y + hotspot->height) { |
136 | if (HOTSPOT_IGNORE == hotspot->callback(output, pointer->x, pointer->y, | 126 | if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, |
137 | wl_button_to_x11_button(button), hotspot->data)) { | 127 | pointer->x, pointer->y, button, hotspot->data)) { |
138 | return; | 128 | return; |
139 | } | 129 | } |
140 | } | 130 | } |
@@ -152,7 +142,7 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | |||
152 | 142 | ||
153 | // If there is a button press binding, execute it, skip default behavior, | 143 | // If there is a button press binding, execute it, skip default behavior, |
154 | // and check button release bindings | 144 | // and check button release bindings |
155 | enum x11_button button = wl_axis_to_x11_button(axis, value); | 145 | uint32_t button = wl_axis_to_button(axis, value); |
156 | if (check_bindings(bar, button, WL_POINTER_BUTTON_STATE_PRESSED)) { | 146 | if (check_bindings(bar, button, WL_POINTER_BUTTON_STATE_PRESSED)) { |
157 | check_bindings(bar, button, WL_POINTER_BUTTON_STATE_RELEASED); | 147 | check_bindings(bar, button, WL_POINTER_BUTTON_STATE_RELEASED); |
158 | return; | 148 | return; |
@@ -166,8 +156,8 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | |||
166 | && y >= hotspot->y | 156 | && y >= hotspot->y |
167 | && x < hotspot->x + hotspot->width | 157 | && x < hotspot->x + hotspot->width |
168 | && y < hotspot->y + hotspot->height) { | 158 | && y < hotspot->y + hotspot->height) { |
169 | if (HOTSPOT_IGNORE == hotspot->callback( | 159 | if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, |
170 | output, pointer->x, pointer->y, button, hotspot->data)) { | 160 | pointer->x, pointer->y, button, hotspot->data)) { |
171 | return; | 161 | return; |
172 | } | 162 | } |
173 | } | 163 | } |