diff options
Diffstat (limited to 'swaybar/input.c')
-rw-r--r-- | swaybar/input.c | 115 |
1 files changed, 71 insertions, 44 deletions
diff --git a/swaybar/input.c b/swaybar/input.c index c83d8c33..92972146 100644 --- a/swaybar/input.c +++ b/swaybar/input.c | |||
@@ -59,13 +59,17 @@ static uint32_t wl_axis_to_button(uint32_t axis, wl_fixed_t value) { | |||
59 | } | 59 | } |
60 | } | 60 | } |
61 | 61 | ||
62 | void update_cursor(struct swaybar *bar) { | 62 | void update_cursor(struct swaybar_seat *seat) { |
63 | struct swaybar_pointer *pointer = &bar->pointer; | 63 | struct swaybar_pointer *pointer = &seat->pointer; |
64 | if (!pointer || !pointer->cursor_surface) { | ||
65 | return; | ||
66 | } | ||
64 | if (pointer->cursor_theme) { | 67 | if (pointer->cursor_theme) { |
65 | wl_cursor_theme_destroy(pointer->cursor_theme); | 68 | wl_cursor_theme_destroy(pointer->cursor_theme); |
66 | } | 69 | } |
67 | int scale = pointer->current ? pointer->current->scale : 1; | 70 | int scale = pointer->current ? pointer->current->scale : 1; |
68 | pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * scale, bar->shm); | 71 | pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * scale, |
72 | seat->bar->shm); | ||
69 | struct wl_cursor *cursor; | 73 | struct wl_cursor *cursor; |
70 | cursor = wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); | 74 | cursor = wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); |
71 | pointer->cursor_image = cursor->images[0]; | 75 | pointer->cursor_image = cursor->images[0]; |
@@ -84,30 +88,30 @@ void update_cursor(struct swaybar *bar) { | |||
84 | static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, | 88 | static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, |
85 | uint32_t serial, struct wl_surface *surface, | 89 | uint32_t serial, struct wl_surface *surface, |
86 | wl_fixed_t surface_x, wl_fixed_t surface_y) { | 90 | wl_fixed_t surface_x, wl_fixed_t surface_y) { |
87 | struct swaybar *bar = data; | 91 | struct swaybar_seat *seat = data; |
88 | struct swaybar_pointer *pointer = &bar->pointer; | 92 | struct swaybar_pointer *pointer = &seat->pointer; |
89 | pointer->serial = serial; | 93 | pointer->serial = serial; |
90 | struct swaybar_output *output; | 94 | struct swaybar_output *output; |
91 | wl_list_for_each(output, &bar->outputs, link) { | 95 | wl_list_for_each(output, &seat->bar->outputs, link) { |
92 | if (output->surface == surface) { | 96 | if (output->surface == surface) { |
93 | pointer->current = output; | 97 | pointer->current = output; |
94 | break; | 98 | break; |
95 | } | 99 | } |
96 | } | 100 | } |
97 | update_cursor(bar); | 101 | update_cursor(seat); |
98 | } | 102 | } |
99 | 103 | ||
100 | static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, | 104 | static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, |
101 | uint32_t serial, struct wl_surface *surface) { | 105 | uint32_t serial, struct wl_surface *surface) { |
102 | struct swaybar *bar = data; | 106 | struct swaybar_seat *seat = data; |
103 | bar->pointer.current = NULL; | 107 | seat->pointer.current = NULL; |
104 | } | 108 | } |
105 | 109 | ||
106 | static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, | 110 | static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, |
107 | uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { | 111 | uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { |
108 | struct swaybar *bar = data; | 112 | struct swaybar_seat *seat = data; |
109 | bar->pointer.x = wl_fixed_to_int(surface_x); | 113 | seat->pointer.x = wl_fixed_to_int(surface_x); |
110 | bar->pointer.y = wl_fixed_to_int(surface_y); | 114 | seat->pointer.y = wl_fixed_to_int(surface_y); |
111 | } | 115 | } |
112 | 116 | ||
113 | static bool check_bindings(struct swaybar *bar, uint32_t button, | 117 | static bool check_bindings(struct swaybar *bar, uint32_t button, |
@@ -142,14 +146,14 @@ static void process_hotspots(struct swaybar_output *output, | |||
142 | 146 | ||
143 | static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, | 147 | static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, |
144 | uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { | 148 | uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { |
145 | struct swaybar *bar = data; | 149 | struct swaybar_seat *seat = data; |
146 | struct swaybar_pointer *pointer = &bar->pointer; | 150 | struct swaybar_pointer *pointer = &seat->pointer; |
147 | struct swaybar_output *output = pointer->current; | 151 | struct swaybar_output *output = pointer->current; |
148 | if (!sway_assert(output, "button with no active output")) { | 152 | if (!sway_assert(output, "button with no active output")) { |
149 | return; | 153 | return; |
150 | } | 154 | } |
151 | 155 | ||
152 | if (check_bindings(bar, button, state)) { | 156 | if (check_bindings(seat->bar, button, state)) { |
153 | return; | 157 | return; |
154 | } | 158 | } |
155 | 159 | ||
@@ -199,8 +203,8 @@ static void workspace_next(struct swaybar *bar, struct swaybar_output *output, | |||
199 | 203 | ||
200 | static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | 204 | static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, |
201 | uint32_t time, uint32_t axis, wl_fixed_t value) { | 205 | uint32_t time, uint32_t axis, wl_fixed_t value) { |
202 | struct swaybar *bar = data; | 206 | struct swaybar_seat *seat = data; |
203 | struct swaybar_pointer *pointer = &bar->pointer; | 207 | struct swaybar_pointer *pointer = &seat->pointer; |
204 | struct swaybar_output *output = pointer->current; | 208 | struct swaybar_output *output = pointer->current; |
205 | if (!sway_assert(output, "axis with no active output")) { | 209 | if (!sway_assert(output, "axis with no active output")) { |
206 | return; | 210 | return; |
@@ -209,8 +213,8 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | |||
209 | // If there is a button press binding, execute it, skip default behavior, | 213 | // If there is a button press binding, execute it, skip default behavior, |
210 | // and check button release bindings | 214 | // and check button release bindings |
211 | uint32_t button = wl_axis_to_button(axis, value); | 215 | uint32_t button = wl_axis_to_button(axis, value); |
212 | if (check_bindings(bar, button, WL_POINTER_BUTTON_STATE_PRESSED)) { | 216 | if (check_bindings(seat->bar, button, WL_POINTER_BUTTON_STATE_PRESSED)) { |
213 | check_bindings(bar, button, WL_POINTER_BUTTON_STATE_RELEASED); | 217 | check_bindings(seat->bar, button, WL_POINTER_BUTTON_STATE_RELEASED); |
214 | return; | 218 | return; |
215 | } | 219 | } |
216 | 220 | ||
@@ -229,10 +233,10 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | |||
229 | } | 233 | } |
230 | } | 234 | } |
231 | 235 | ||
232 | struct swaybar_config *config = bar->config; | 236 | struct swaybar_config *config = seat->bar->config; |
233 | double amt = wl_fixed_to_double(value); | 237 | double amt = wl_fixed_to_double(value); |
234 | if (amt == 0.0 || !config->workspace_buttons) { | 238 | if (amt == 0.0 || !config->workspace_buttons) { |
235 | check_bindings(bar, button, WL_POINTER_BUTTON_STATE_RELEASED); | 239 | check_bindings(seat->bar, button, WL_POINTER_BUTTON_STATE_RELEASED); |
236 | return; | 240 | return; |
237 | } | 241 | } |
238 | 242 | ||
@@ -240,10 +244,10 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | |||
240 | return; | 244 | return; |
241 | } | 245 | } |
242 | 246 | ||
243 | workspace_next(bar, output, amt < 0.0); | 247 | workspace_next(seat->bar, output, amt < 0.0); |
244 | 248 | ||
245 | // Check button release bindings | 249 | // Check button release bindings |
246 | check_bindings(bar, button, WL_POINTER_BUTTON_STATE_RELEASED); | 250 | check_bindings(seat->bar, button, WL_POINTER_BUTTON_STATE_RELEASED); |
247 | } | 251 | } |
248 | 252 | ||
249 | static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) { | 253 | static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) { |
@@ -297,9 +301,9 @@ static struct touch_slot *get_touch_slot(struct swaybar_touch *touch, int32_t id | |||
297 | static void wl_touch_down(void *data, struct wl_touch *wl_touch, | 301 | static void wl_touch_down(void *data, struct wl_touch *wl_touch, |
298 | uint32_t serial, uint32_t time, struct wl_surface *surface, | 302 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
299 | int32_t id, wl_fixed_t _x, wl_fixed_t _y) { | 303 | int32_t id, wl_fixed_t _x, wl_fixed_t _y) { |
300 | struct swaybar *bar = data; | 304 | struct swaybar_seat *seat = data; |
301 | struct swaybar_output *_output = NULL, *output = NULL; | 305 | struct swaybar_output *_output = NULL, *output = NULL; |
302 | wl_list_for_each(_output, &bar->outputs, link) { | 306 | wl_list_for_each(_output, &seat->bar->outputs, link) { |
303 | if (_output->surface == surface) { | 307 | if (_output->surface == surface) { |
304 | output = _output; | 308 | output = _output; |
305 | break; | 309 | break; |
@@ -309,7 +313,7 @@ static void wl_touch_down(void *data, struct wl_touch *wl_touch, | |||
309 | sway_log(SWAY_DEBUG, "Got touch event for unknown surface"); | 313 | sway_log(SWAY_DEBUG, "Got touch event for unknown surface"); |
310 | return; | 314 | return; |
311 | } | 315 | } |
312 | struct touch_slot *slot = get_touch_slot(&bar->touch, id); | 316 | struct touch_slot *slot = get_touch_slot(&seat->touch, id); |
313 | if (!slot) { | 317 | if (!slot) { |
314 | return; | 318 | return; |
315 | } | 319 | } |
@@ -322,8 +326,8 @@ static void wl_touch_down(void *data, struct wl_touch *wl_touch, | |||
322 | 326 | ||
323 | static void wl_touch_up(void *data, struct wl_touch *wl_touch, | 327 | static void wl_touch_up(void *data, struct wl_touch *wl_touch, |
324 | uint32_t serial, uint32_t time, int32_t id) { | 328 | uint32_t serial, uint32_t time, int32_t id) { |
325 | struct swaybar *bar = data; | 329 | struct swaybar_seat *seat = data; |
326 | struct touch_slot *slot = get_touch_slot(&bar->touch, id); | 330 | struct touch_slot *slot = get_touch_slot(&seat->touch, id); |
327 | if (!slot) { | 331 | if (!slot) { |
328 | return; | 332 | return; |
329 | } | 333 | } |
@@ -336,8 +340,8 @@ static void wl_touch_up(void *data, struct wl_touch *wl_touch, | |||
336 | 340 | ||
337 | static void wl_touch_motion(void *data, struct wl_touch *wl_touch, | 341 | static void wl_touch_motion(void *data, struct wl_touch *wl_touch, |
338 | uint32_t time, int32_t id, wl_fixed_t x, wl_fixed_t y) { | 342 | uint32_t time, int32_t id, wl_fixed_t x, wl_fixed_t y) { |
339 | struct swaybar *bar = data; | 343 | struct swaybar_seat *seat = data; |
340 | struct touch_slot *slot = get_touch_slot(&bar->touch, id); | 344 | struct touch_slot *slot = get_touch_slot(&seat->touch, id); |
341 | if (!slot) { | 345 | if (!slot) { |
342 | return; | 346 | return; |
343 | } | 347 | } |
@@ -351,7 +355,7 @@ static void wl_touch_motion(void *data, struct wl_touch *wl_touch, | |||
351 | int progress = (int)((slot->x - slot->start_x) | 355 | int progress = (int)((slot->x - slot->start_x) |
352 | / slot->output->width * 100); | 356 | / slot->output->width * 100); |
353 | if (abs(progress) / 20 != abs(prev_progress) / 20) { | 357 | if (abs(progress) / 20 != abs(prev_progress) / 20) { |
354 | workspace_next(bar, slot->output, progress - prev_progress < 0); | 358 | workspace_next(seat->bar, slot->output, progress - prev_progress < 0); |
355 | } | 359 | } |
356 | } | 360 | } |
357 | 361 | ||
@@ -360,8 +364,8 @@ static void wl_touch_frame(void *data, struct wl_touch *wl_touch) { | |||
360 | } | 364 | } |
361 | 365 | ||
362 | static void wl_touch_cancel(void *data, struct wl_touch *wl_touch) { | 366 | static void wl_touch_cancel(void *data, struct wl_touch *wl_touch) { |
363 | struct swaybar *bar = data; | 367 | struct swaybar_seat *seat = data; |
364 | struct swaybar_touch *touch = &bar->touch; | 368 | struct swaybar_touch *touch = &seat->touch; |
365 | for (size_t i = 0; i < sizeof(touch->slots) / sizeof(*touch->slots); ++i) { | 369 | for (size_t i = 0; i < sizeof(touch->slots) / sizeof(*touch->slots); ++i) { |
366 | touch->slots[i].output = NULL; | 370 | touch->slots[i].output = NULL; |
367 | } | 371 | } |
@@ -389,22 +393,27 @@ static const struct wl_touch_listener touch_listener = { | |||
389 | 393 | ||
390 | static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, | 394 | static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, |
391 | enum wl_seat_capability caps) { | 395 | enum wl_seat_capability caps) { |
392 | struct swaybar *bar = data; | 396 | struct swaybar_seat *seat = data; |
393 | if (bar->pointer.pointer != NULL) { | 397 | if (seat->pointer.pointer != NULL) { |
394 | wl_pointer_release(bar->pointer.pointer); | 398 | wl_pointer_release(seat->pointer.pointer); |
395 | bar->pointer.pointer = NULL; | 399 | seat->pointer.pointer = NULL; |
396 | } | 400 | } |
397 | if (bar->touch.touch != NULL) { | 401 | if (seat->touch.touch != NULL) { |
398 | wl_touch_release(bar->touch.touch); | 402 | wl_touch_release(seat->touch.touch); |
399 | bar->touch.touch = NULL; | 403 | seat->touch.touch = NULL; |
400 | } | 404 | } |
401 | if ((caps & WL_SEAT_CAPABILITY_POINTER)) { | 405 | if ((caps & WL_SEAT_CAPABILITY_POINTER)) { |
402 | bar->pointer.pointer = wl_seat_get_pointer(wl_seat); | 406 | seat->pointer.pointer = wl_seat_get_pointer(wl_seat); |
403 | wl_pointer_add_listener(bar->pointer.pointer, &pointer_listener, bar); | 407 | if (seat->bar->running && !seat->pointer.cursor_surface) { |
408 | seat->pointer.cursor_surface = | ||
409 | wl_compositor_create_surface(seat->bar->compositor); | ||
410 | assert(seat->pointer.cursor_surface); | ||
411 | } | ||
412 | wl_pointer_add_listener(seat->pointer.pointer, &pointer_listener, seat); | ||
404 | } | 413 | } |
405 | if ((caps & WL_SEAT_CAPABILITY_TOUCH)) { | 414 | if ((caps & WL_SEAT_CAPABILITY_TOUCH)) { |
406 | bar->touch.touch = wl_seat_get_touch(wl_seat); | 415 | seat->touch.touch = wl_seat_get_touch(wl_seat); |
407 | wl_touch_add_listener(bar->touch.touch, &touch_listener, bar); | 416 | wl_touch_add_listener(seat->touch.touch, &touch_listener, seat); |
408 | } | 417 | } |
409 | } | 418 | } |
410 | 419 | ||
@@ -417,3 +426,21 @@ const struct wl_seat_listener seat_listener = { | |||
417 | .capabilities = seat_handle_capabilities, | 426 | .capabilities = seat_handle_capabilities, |
418 | .name = seat_handle_name, | 427 | .name = seat_handle_name, |
419 | }; | 428 | }; |
429 | |||
430 | void swaybar_seat_free(struct swaybar_seat *seat) { | ||
431 | if (!seat) { | ||
432 | return; | ||
433 | } | ||
434 | if (seat->pointer.pointer != NULL) { | ||
435 | wl_pointer_release(seat->pointer.pointer); | ||
436 | } | ||
437 | if (seat->pointer.cursor_surface != NULL) { | ||
438 | wl_surface_destroy(seat->pointer.cursor_surface); | ||
439 | } | ||
440 | if (seat->touch.touch != NULL) { | ||
441 | wl_touch_release(seat->touch.touch); | ||
442 | } | ||
443 | wl_seat_destroy(seat->wl_seat); | ||
444 | wl_list_remove(&seat->link); | ||
445 | free(seat); | ||
446 | } | ||