22 #include "uwac-priv.h"
23 #include "uwac-utils.h"
35 #include <sys/timerfd.h>
36 #include <sys/epoll.h>
39 #include "wayland-cursor.h"
40 #include "wayland-client-protocol.h"
42 static struct wl_buffer* create_pointer_buffer(UwacSeat* seat,
const void* src,
size_t size)
44 struct wl_buffer* buffer = NULL;
47 struct wl_shm_pool* pool = NULL;
51 fd = uwac_create_anonymous_file(size);
56 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
58 if (data == MAP_FAILED)
62 memcpy(data, src, size);
64 pool = wl_shm_create_pool(seat->display->shm, fd, size);
73 wl_shm_pool_create_buffer(pool, 0, seat->pointer_image->width, seat->pointer_image->height,
74 seat->pointer_image->width * 4, WL_SHM_FORMAT_ARGB8888);
75 wl_shm_pool_destroy(pool);
77 if (munmap(data, size) < 0)
79 char buffer[256] = { 0 };
80 (void)fprintf(stderr,
"%s: munmap(%p, %zu) failed with [%d] %s\n", __func__, data, size,
81 errno, uwac_strerror(errno, buffer,
sizeof(buffer)));
89 static void on_buffer_release(
void* data,
struct wl_buffer* wl_buffer)
92 wl_buffer_destroy(wl_buffer);
95 static const struct wl_buffer_listener buffer_release_listener = { on_buffer_release };
97 static UwacReturnCode set_cursor_image(UwacSeat* seat, uint32_t serial)
99 struct wl_buffer* buffer = NULL;
100 struct wl_cursor* cursor = NULL;
101 struct wl_cursor_image* image = NULL;
102 struct wl_surface* surface = NULL;
106 if (!seat || !seat->display || !seat->default_cursor || !seat->default_cursor->images)
107 return UWAC_ERROR_INTERNAL;
110 if (seat->pointer_focus)
111 scale = seat->pointer_focus->display->actual_scale;
113 switch (seat->pointer_type)
116 image = seat->pointer_image;
117 buffer = create_pointer_buffer(seat, seat->pointer_data, seat->pointer_size);
119 return UWAC_ERROR_INTERNAL;
120 if (wl_buffer_add_listener(buffer, &buffer_release_listener, seat) < 0)
121 return UWAC_ERROR_INTERNAL;
123 surface = seat->pointer_surface;
124 x = image->hotspot_x / scale;
125 y = image->hotspot_y / scale;
130 cursor = seat->default_cursor;
132 return UWAC_ERROR_INTERNAL;
133 image = cursor->images[0];
135 return UWAC_ERROR_INTERNAL;
136 x = image->hotspot_x;
137 y = image->hotspot_y;
138 buffer = wl_cursor_image_get_buffer(image);
140 return UWAC_ERROR_INTERNAL;
141 surface = seat->pointer_surface;
145 if (surface && buffer)
147 wl_surface_set_buffer_scale(surface, scale);
148 wl_surface_attach(surface, buffer, 0, 0);
149 wl_surface_damage(surface, 0, 0, image->width, image->height);
150 wl_surface_commit(surface);
153 wl_pointer_set_cursor(seat->pointer, serial, surface, x, y);
158 static void keyboard_repeat_func(UwacTask* task, uint32_t events)
160 UwacSeat* input = container_of(task, UwacSeat, repeat_task);
162 UwacWindow* window = input->keyboard_focus;
165 if (read(input->repeat_timer_fd, &exp,
sizeof exp) !=
sizeof exp)
173 UwacKeyEvent* key = NULL;
175 key = (UwacKeyEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_KEY);
179 key->window = window;
180 key->sym = input->repeat_sym;
181 key->raw_key = input->repeat_key;
183 key->repeated =
true;
187 static void keyboard_handle_keymap(
void* data,
struct wl_keyboard* keyboard, uint32_t format,
188 int fd, uint32_t size)
190 UwacSeat* input = data;
191 struct xkb_keymap* keymap = NULL;
192 struct xkb_state* state = NULL;
193 char* map_str = NULL;
194 int mapFlags = MAP_SHARED;
202 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
208 if (input->seat_version >= 7)
209 mapFlags = MAP_PRIVATE;
211 map_str = mmap(NULL, size, PROT_READ, mapFlags, fd, 0);
212 if (map_str == MAP_FAILED)
218 keymap = xkb_keymap_new_from_string(input->xkb_context, map_str, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
219 munmap(map_str, size);
224 assert(uwacErrorHandler(input->display, UWAC_ERROR_INTERNAL,
"failed to compile keymap\n"));
228 state = xkb_state_new(keymap);
232 uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY,
"failed to create XKB state\n"));
233 xkb_keymap_unref(keymap);
237 xkb_keymap_unref(input->xkb.keymap);
238 xkb_state_unref(input->xkb.state);
239 input->xkb.keymap = keymap;
240 input->xkb.state = state;
242 input->xkb.control_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap,
"Control");
243 input->xkb.alt_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap,
"Mod1");
244 input->xkb.shift_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap,
"Shift");
245 input->xkb.caps_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap,
"Lock");
246 input->xkb.num_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap,
"Mod2");
249 static void keyboard_handle_key(
void* data,
struct wl_keyboard* keyboard, uint32_t serial,
250 uint32_t time, uint32_t key, uint32_t state_w);
252 static void keyboard_handle_enter(
void* data,
struct wl_keyboard* keyboard, uint32_t serial,
253 struct wl_surface* surface,
struct wl_array* keys)
255 UwacSeat* input = (UwacSeat*)data;
258 UwacKeyboardEnterLeaveEvent*
event = (UwacKeyboardEnterLeaveEvent*)UwacDisplayNewEvent(
259 input->display, UWAC_EVENT_KEYBOARD_ENTER);
263 event->window = input->keyboard_focus = (UwacWindow*)wl_surface_get_user_data(surface);
276 static void keyboard_handle_leave(
void* data,
struct wl_keyboard* keyboard, uint32_t serial,
277 struct wl_surface* surface)
279 struct itimerspec its = { 0 };
280 uint32_t* pressedKey = NULL;
283 UwacSeat* input = (UwacSeat*)data;
286 its.it_interval.tv_sec = 0;
287 its.it_interval.tv_nsec = 0;
288 its.it_value.tv_sec = 0;
289 its.it_value.tv_nsec = 0;
290 (void)timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
292 UwacPointerEnterLeaveEvent*
event =
293 (UwacPointerEnterLeaveEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_LEAVE);
297 event->window = input->keyboard_focus;
303 for (pressedKey = input->pressed_keys.data, i = 0; i < input->pressed_keys.size;
304 i +=
sizeof(uint32_t))
306 keyboard_handle_key(data, keyboard, serial, 0, *pressedKey, WL_KEYBOARD_KEY_STATE_RELEASED);
311 static int update_key_pressed(UwacSeat* seat, uint32_t key)
313 uint32_t* keyPtr = NULL;
317 wl_array_for_each(keyPtr, &seat->pressed_keys)
323 keyPtr = wl_array_add(&seat->pressed_keys,
sizeof(uint32_t));
331 static int update_key_released(UwacSeat* seat, uint32_t key)
339 uint32_t* keyPtr = seat->pressed_keys.data;
340 for (; i < seat->pressed_keys.size; i++, keyPtr++)
351 toMove = seat->pressed_keys.size - ((i + 1) *
sizeof(uint32_t));
353 memmove(keyPtr, keyPtr + 1, toMove);
355 seat->pressed_keys.size -=
sizeof(uint32_t);
360 static void keyboard_handle_key(
void* data,
struct wl_keyboard* keyboard, uint32_t serial,
361 uint32_t time, uint32_t key, uint32_t state_w)
363 UwacSeat* input = (UwacSeat*)data;
366 UwacWindow* window = input->keyboard_focus;
367 UwacKeyEvent* keyEvent = NULL;
370 uint32_t num_syms = 0;
371 enum wl_keyboard_key_state state = state_w;
372 const xkb_keysym_t* syms = NULL;
373 xkb_keysym_t sym = 0;
374 struct itimerspec its;
376 if (state_w == WL_KEYBOARD_KEY_STATE_PRESSED)
377 update_key_pressed(input, key);
379 update_key_released(input, key);
381 input->display->serial = serial;
383 if (!window || !input->xkb.state)
394 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
396 sym = XKB_KEY_NoSymbol;
400 if (state == WL_KEYBOARD_KEY_STATE_RELEASED && key == input->repeat_key)
402 its.it_interval.tv_sec = 0;
403 its.it_interval.tv_nsec = 0;
404 its.it_value.tv_sec = 0;
405 its.it_value.tv_nsec = 0;
406 (void)timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
408 else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
409 xkb_keymap_key_repeats(input->xkb.keymap, code))
411 input->repeat_sym = sym;
412 input->repeat_key = key;
413 input->repeat_time = time;
414 its.it_interval.tv_sec = input->repeat_rate_sec;
415 its.it_interval.tv_nsec = input->repeat_rate_nsec;
416 its.it_value.tv_sec = input->repeat_delay_sec;
417 its.it_value.tv_nsec = input->repeat_delay_nsec;
418 (void)timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
421 keyEvent = (UwacKeyEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_KEY);
425 keyEvent->window = window;
427 keyEvent->raw_key = key;
428 keyEvent->pressed = (state == WL_KEYBOARD_KEY_STATE_PRESSED);
429 keyEvent->repeated =
false;
432 static void keyboard_handle_modifiers(
void* data,
struct wl_keyboard* keyboard, uint32_t serial,
433 uint32_t mods_depressed, uint32_t mods_latched,
434 uint32_t mods_locked, uint32_t group)
436 UwacSeat* input = data;
439 UwacKeyboardModifiersEvent*
event = NULL;
440 xkb_mod_mask_t mask = 0;
443 if (!input->xkb.keymap)
446 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
447 mask = xkb_state_serialize_mods(input->xkb.state, XKB_STATE_MODS_DEPRESSED |
448 XKB_STATE_MODS_LATCHED |
449 XKB_STATE_MODS_LOCKED);
450 input->modifiers = 0;
451 if (mask & input->xkb.control_mask)
452 input->modifiers |= UWAC_MOD_CONTROL_MASK;
453 if (mask & input->xkb.alt_mask)
454 input->modifiers |= UWAC_MOD_ALT_MASK;
455 if (mask & input->xkb.shift_mask)
456 input->modifiers |= UWAC_MOD_SHIFT_MASK;
457 if (mask & input->xkb.caps_mask)
458 input->modifiers |= UWAC_MOD_CAPS_MASK;
459 if (mask & input->xkb.num_mask)
460 input->modifiers |= UWAC_MOD_NUM_MASK;
462 event = (UwacKeyboardModifiersEvent*)UwacDisplayNewEvent(input->display,
463 UWAC_EVENT_KEYBOARD_MODIFIERS);
467 event->modifiers = input->modifiers;
470 static void set_repeat_info(UwacSeat* input, int32_t rate, int32_t delay)
474 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
475 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
483 input->repeat_rate_sec = 1;
485 input->repeat_rate_nsec = 1000000000 / rate;
487 input->repeat_delay_sec = delay / 1000;
488 delay -= (input->repeat_delay_sec * 1000);
489 input->repeat_delay_nsec = delay * 1000 * 1000;
492 static void keyboard_handle_repeat_info(
void* data,
struct wl_keyboard* keyboard, int32_t rate,
495 UwacSeat* input = data;
498 set_repeat_info(input, rate, delay);
501 static const struct wl_keyboard_listener keyboard_listener = {
502 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave,
503 keyboard_handle_key, keyboard_handle_modifiers, keyboard_handle_repeat_info
506 static bool touch_send_start_frame(UwacSeat* seat)
510 UwacTouchFrameBegin* ev =
511 (UwacTouchFrameBegin*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_FRAME_BEGIN);
515 seat->touch_frame_started =
true;
519 static void touch_handle_down(
void* data,
struct wl_touch* wl_touch, uint32_t serial, uint32_t time,
520 struct wl_surface* surface, int32_t
id, wl_fixed_t x_w,
523 UwacSeat* seat = data;
524 UwacTouchDown* tdata = NULL;
527 assert(seat->display);
529 seat->display->serial = serial;
530 if (!seat->touch_frame_started && !touch_send_start_frame(seat))
533 tdata = (UwacTouchDown*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_DOWN);
540 double sx = wl_fixed_to_double(x_w);
541 double sy = wl_fixed_to_double(y_w);
543 tdata->x = (wl_fixed_t)lround(sx);
544 tdata->y = (wl_fixed_t)lround(sy);
547 struct widget *widget;
548 float sx = wl_fixed_to_double(x);
549 float sy = wl_fixed_to_double(y);
552 input->touch_focus = wl_surface_get_user_data(surface);
553 if (!input->touch_focus) {
554 DBG(
"Failed to find to touch focus for surface %p\n", (
void*) surface);
558 if (surface != input->touch_focus->main_surface->surface) {
559 DBG(
"Ignoring input event from subsurface %p\n", (
void*) surface);
560 input->touch_focus = NULL;
565 widget = input->grab;
567 widget = window_find_widget(input->touch_focus,
568 wl_fixed_to_double(x),
569 wl_fixed_to_double(y));
571 struct touch_point *tp = xmalloc(
sizeof *tp);
577 wl_list_insert(&input->touch_point_list, &tp->link);
579 if (widget->touch_down_handler)
580 (*widget->touch_down_handler)(widget, input,
589 static void touch_handle_up(
void* data,
struct wl_touch* wl_touch, uint32_t serial, uint32_t time,
592 UwacSeat* seat = data;
593 UwacTouchUp* tdata = NULL;
597 if (!seat->touch_frame_started && !touch_send_start_frame(seat))
600 tdata = (UwacTouchUp*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_UP);
608 struct touch_point *tp, *tmp;
610 if (!input->touch_focus) {
611 DBG(
"No touch focus found for touch up event!\n");
615 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
619 if (tp->widget->touch_up_handler)
620 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
622 tp->widget->user_data);
624 wl_list_remove(&tp->link);
632 static void touch_handle_motion(
void* data,
struct wl_touch* wl_touch, uint32_t time, int32_t
id,
633 wl_fixed_t x_w, wl_fixed_t y_w)
635 UwacSeat* seat = data;
638 UwacTouchMotion* tdata = NULL;
640 if (!seat->touch_frame_started && !touch_send_start_frame(seat))
643 tdata = (UwacTouchMotion*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_MOTION);
650 double sx = wl_fixed_to_double(x_w);
651 double sy = wl_fixed_to_double(y_w);
653 tdata->x = (wl_fixed_t)lround(sx);
654 tdata->y = (wl_fixed_t)lround(sy);
657 struct touch_point *tp;
658 float sx = wl_fixed_to_double(x);
659 float sy = wl_fixed_to_double(y);
661 DBG(
"touch_handle_motion: %i %i\n",
id, wl_list_length(&seat->touch_point_list));
663 if (!seat->touch_focus) {
664 DBG(
"No touch focus found for touch motion event!\n");
668 wl_list_for_each(tp, &seat->touch_point_list, link) {
674 if (tp->widget->touch_motion_handler)
675 (*tp->widget->touch_motion_handler)(tp->widget, seat, time,
677 tp->widget->user_data);
683 static void touch_handle_frame(
void* data,
struct wl_touch* wl_touch)
685 UwacSeat* seat = data;
688 UwacTouchFrameEnd* ev =
689 (UwacTouchFrameEnd*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_FRAME_END);
694 seat->touch_frame_started =
false;
697 static void touch_handle_cancel(
void* data,
struct wl_touch* wl_touch)
699 UwacSeat* seat = data;
702 UwacTouchCancel* ev =
703 (UwacTouchCancel*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_CANCEL);
708 seat->touch_frame_started =
false;
711 struct touch_point *tp, *tmp;
713 DBG(
"touch_handle_cancel\n");
715 if (!input->touch_focus) {
716 DBG(
"No touch focus found for touch cancel event!\n");
720 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
721 if (tp->widget->touch_cancel_handler)
722 (*tp->widget->touch_cancel_handler)(tp->widget, input,
723 tp->widget->user_data);
725 wl_list_remove(&tp->link);
731 static void touch_handle_shape(
void* data,
struct wl_touch* wl_touch, int32_t
id, wl_fixed_t major,
734 UwacSeat* seat = data;
740 static void touch_handle_orientation(
void* data,
struct wl_touch* wl_touch, int32_t
id,
741 wl_fixed_t orientation)
743 UwacSeat* seat = data;
749 static const struct wl_touch_listener touch_listener = {
750 touch_handle_down, touch_handle_up, touch_handle_motion, touch_handle_frame,
751 touch_handle_cancel, touch_handle_shape, touch_handle_orientation
754 static void pointer_handle_enter(
void* data,
struct wl_pointer* pointer, uint32_t serial,
755 struct wl_surface* surface, wl_fixed_t sx_w, wl_fixed_t sy_w)
757 UwacSeat* input = data;
758 UwacWindow* window = NULL;
759 UwacPointerEnterLeaveEvent*
event = NULL;
763 double sx = wl_fixed_to_double(sx_w);
764 double sy = wl_fixed_to_double(sy_w);
772 input->display->serial = serial;
773 input->display->pointer_focus_serial = serial;
774 window = wl_surface_get_user_data(surface);
776 window->pointer_enter_serial = serial;
777 input->pointer_focus = window;
782 (UwacPointerEnterLeaveEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_ENTER);
787 event->window = window;
788 event->x = (uint32_t)lround(sx);
789 event->y = (uint32_t)lround(sy);
792 set_cursor_image(input, serial);
795 static void pointer_handle_leave(
void* data,
struct wl_pointer* pointer, uint32_t serial,
796 struct wl_surface* surface)
798 UwacPointerEnterLeaveEvent*
event = NULL;
799 UwacWindow* window = NULL;
800 UwacSeat* input = data;
803 input->display->serial = serial;
806 (UwacPointerEnterLeaveEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_LEAVE);
810 window = wl_surface_get_user_data(surface);
813 event->window = window;
816 static void pointer_handle_motion(
void* data,
struct wl_pointer* pointer, uint32_t time,
817 wl_fixed_t sx_w, wl_fixed_t sy_w)
819 UwacPointerMotionEvent* motion_event = NULL;
820 UwacSeat* input = data;
823 UwacWindow* window = input->pointer_focus;
824 if (!window || !window->display)
827 int scale = window->display->actual_scale;
828 int sx_i = wl_fixed_to_int(sx_w) * scale;
829 int sy_i = wl_fixed_to_int(sy_w) * scale;
830 double sx_d = wl_fixed_to_double(sx_w) * scale;
831 double sy_d = wl_fixed_to_double(sy_w) * scale;
833 if ((sx_i < 0) || (sy_i < 0))
840 (UwacPointerMotionEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_MOTION);
844 motion_event->seat = input;
845 motion_event->window = window;
846 motion_event->x = sx_i;
847 motion_event->y = sy_i;
850 static void pointer_handle_button(
void* data,
struct wl_pointer* pointer, uint32_t serial,
851 uint32_t time, uint32_t button, uint32_t state_w)
853 UwacPointerButtonEvent*
event = NULL;
854 UwacSeat* seat = data;
857 UwacWindow* window = seat->pointer_focus;
859 seat->display->serial = serial;
861 event = (UwacPointerButtonEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_BUTTONS);
866 event->window = window;
867 event->x = (uint32_t)lround(seat->sx);
868 event->y = (uint32_t)lround(seat->sy);
869 event->button = button;
870 event->state = (
enum wl_pointer_button_state)state_w;
873 static void pointer_handle_axis(
void* data,
struct wl_pointer* pointer, uint32_t time,
874 uint32_t axis, wl_fixed_t value)
876 UwacPointerAxisEvent*
event = NULL;
877 UwacSeat* seat = data;
880 UwacWindow* window = seat->pointer_focus;
885 event = (UwacPointerAxisEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_AXIS);
890 event->window = window;
891 event->x = (uint32_t)lround(seat->sx);
892 event->y = (uint32_t)lround(seat->sy);
894 event->value = value;
897 static void pointer_frame(
void* data,
struct wl_pointer* wl_pointer)
899 UwacPointerFrameEvent*
event = NULL;
900 UwacSeat* seat = data;
903 UwacWindow* window = seat->pointer_focus;
908 event = (UwacPointerFrameEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_FRAME);
913 event->window = window;
916 static void pointer_axis_source(
void* data,
struct wl_pointer* wl_pointer, uint32_t axis_source)
918 UwacPointerSourceEvent*
event = NULL;
919 UwacSeat* seat = data;
922 UwacWindow* window = seat->pointer_focus;
927 event = (UwacPointerSourceEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_SOURCE);
932 event->window = window;
933 event->axis_source = axis_source;
936 static void pointer_axis_stop(
void* data,
struct wl_pointer* wl_pointer, uint32_t time,
939 UwacSeat* seat = data;
943 static void pointer_axis_discrete(
void* data,
struct wl_pointer* wl_pointer, uint32_t axis,
947 UwacPointerAxisEvent*
event = NULL;
948 UwacSeat* seat = data;
951 UwacWindow* window = seat->pointer_focus;
957 (UwacPointerAxisEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_AXIS_DISCRETE);
962 event->window = window;
963 event->x = (uint32_t)lround(seat->sx);
964 event->y = (uint32_t)lround(seat->sy);
966 event->value = discrete;
969 static void pointer_axis_value120(
void* data,
struct wl_pointer* wl_pointer, uint32_t axis,
973 UwacPointerAxisEvent*
event = NULL;
974 UwacSeat* seat = data;
977 UwacWindow* window = seat->pointer_focus;
983 (UwacPointerAxisEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_AXIS_DISCRETE);
988 event->window = window;
989 event->x = (uint32_t)lround(seat->sx);
990 event->y = (uint32_t)lround(seat->sy);
992 event->value = value120 / 120;
995 static const struct wl_pointer_listener pointer_listener = {
996 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button,
997 pointer_handle_axis, pointer_frame, pointer_axis_source, pointer_axis_stop,
998 pointer_axis_discrete, pointer_axis_value120
1001 static void seat_handle_capabilities(
void* data,
struct wl_seat* seat, uint32_t caps)
1003 UwacSeat* input = data;
1006 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer)
1008 input->pointer = wl_seat_get_pointer(seat);
1009 wl_pointer_set_user_data(input->pointer, input);
1010 wl_pointer_add_listener(input->pointer, &pointer_listener, input);
1012 input->cursor_theme = wl_cursor_theme_load(NULL, 32, input->display->shm);
1013 if (!input->cursor_theme)
1015 assert(uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY,
1016 "unable to get wayland cursor theme\n"));
1020 input->default_cursor = wl_cursor_theme_get_cursor(input->cursor_theme,
"left_ptr");
1021 if (!input->default_cursor)
1023 assert(uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY,
1024 "unable to get wayland cursor left_ptr\n"));
1028 else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer)
1030 #ifdef WL_POINTER_RELEASE_SINCE_VERSION
1031 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1032 wl_pointer_release(input->pointer);
1035 wl_pointer_destroy(input->pointer);
1036 if (input->cursor_theme)
1037 wl_cursor_theme_destroy(input->cursor_theme);
1039 input->default_cursor = NULL;
1040 input->cursor_theme = NULL;
1041 input->pointer = NULL;
1044 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard)
1046 input->keyboard = wl_seat_get_keyboard(seat);
1047 wl_keyboard_set_user_data(input->keyboard, input);
1048 wl_keyboard_add_listener(input->keyboard, &keyboard_listener, input);
1050 else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard)
1052 #ifdef WL_KEYBOARD_RELEASE_SINCE_VERSION
1053 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1054 wl_keyboard_release(input->keyboard);
1057 wl_keyboard_destroy(input->keyboard);
1058 input->keyboard = NULL;
1061 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch)
1063 input->touch = wl_seat_get_touch(seat);
1064 wl_touch_set_user_data(input->touch, input);
1065 wl_touch_add_listener(input->touch, &touch_listener, input);
1067 else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch)
1069 #ifdef WL_TOUCH_RELEASE_SINCE_VERSION
1070 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1071 wl_touch_release(input->touch);
1074 wl_touch_destroy(input->touch);
1075 input->touch = NULL;
1079 static void seat_handle_name(
void* data,
struct wl_seat* seat,
const char* name)
1081 UwacSeat* input = data;
1087 input->name = strdup(name);
1089 assert(uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY,
1090 "unable to strdup seat's name\n"));
1093 static const struct wl_seat_listener seat_listener = { seat_handle_capabilities, seat_handle_name };
1095 UwacSeat* UwacSeatNew(UwacDisplay* d, uint32_t
id, uint32_t version)
1097 UwacSeat* ret = xzalloc(
sizeof(UwacSeat));
1103 ret->seat_version = version;
1105 wl_array_init(&ret->pressed_keys);
1106 ret->xkb_context = xkb_context_new(0);
1107 if (!ret->xkb_context)
1109 (void)fprintf(stderr,
"%s: unable to allocate a xkb_context\n", __func__);
1113 ret->seat = wl_registry_bind(d->registry,
id, &wl_seat_interface, version);
1114 wl_seat_add_listener(ret->seat, &seat_listener, ret);
1115 wl_seat_set_user_data(ret->seat, ret);
1117 ret->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
1118 if (ret->repeat_timer_fd < 0)
1120 (void)fprintf(stderr,
"%s: error creating repeat timer\n", __func__);
1123 ret->repeat_task.run = keyboard_repeat_func;
1124 if (UwacDisplayWatchFd(d, ret->repeat_timer_fd, EPOLLIN, &ret->repeat_task) < 0)
1126 (void)fprintf(stderr,
"%s: error polling repeat timer\n", __func__);
1130 wl_list_insert(d->seats.prev, &ret->link);
1134 UwacSeatDestroy(ret);
1138 void UwacSeatDestroy(UwacSeat* s)
1143 UwacSeatInhibitShortcuts(s,
false);
1146 #ifdef WL_SEAT_RELEASE_SINCE_VERSION
1147 if (s->seat_version >= WL_SEAT_RELEASE_SINCE_VERSION)
1148 wl_seat_release(s->seat);
1151 wl_seat_destroy(s->seat);
1156 wl_array_release(&s->pressed_keys);
1158 xkb_state_unref(s->xkb.state);
1159 xkb_context_unref(s->xkb_context);
1163 #ifdef WL_POINTER_RELEASE_SINCE_VERSION
1164 if (s->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1165 wl_pointer_release(s->pointer);
1168 wl_pointer_destroy(s->pointer);
1173 #ifdef WL_TOUCH_RELEASE_SINCE_VERSION
1174 if (s->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1175 wl_touch_release(s->touch);
1178 wl_touch_destroy(s->touch);
1183 #ifdef WL_KEYBOARD_RELEASE_SINCE_VERSION
1184 if (s->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1185 wl_keyboard_release(s->keyboard);
1188 wl_keyboard_destroy(s->keyboard);
1192 wl_data_device_destroy(s->data_device);
1195 wl_data_source_destroy(s->data_source);
1197 if (s->pointer_surface)
1198 wl_surface_destroy(s->pointer_surface);
1200 free(s->pointer_image);
1201 free(s->pointer_data);
1203 wl_list_remove(&s->link);
1207 const char* UwacSeatGetName(
const UwacSeat* seat)
1213 UwacSeatId UwacSeatGetId(
const UwacSeat* seat)
1216 return seat->seat_id;
1219 UwacReturnCode UwacSeatInhibitShortcuts(UwacSeat* s,
bool inhibit)
1222 return UWAC_ERROR_CLOSED;
1224 if (s->keyboard_inhibitor)
1226 zwp_keyboard_shortcuts_inhibitor_v1_destroy(s->keyboard_inhibitor);
1227 s->keyboard_inhibitor = NULL;
1229 if (inhibit && s->display && s->display->keyboard_inhibit_manager)
1230 s->keyboard_inhibitor = zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(
1231 s->display->keyboard_inhibit_manager, s->keyboard_focus->surface, s->seat);
1233 if (inhibit && !s->keyboard_inhibitor)
1234 return UWAC_ERROR_INTERNAL;
1235 return UWAC_SUCCESS;
1238 UwacReturnCode UwacSeatSetMouseCursor(UwacSeat* seat,
const void* data,
size_t length,
size_t width,
1239 size_t height,
size_t hot_x,
size_t hot_y)
1242 return UWAC_ERROR_CLOSED;
1244 free(seat->pointer_image);
1245 seat->pointer_image = NULL;
1247 free(seat->pointer_data);
1248 seat->pointer_data = NULL;
1249 seat->pointer_size = 0;
1252 if ((data != NULL) && (length != 0))
1254 seat->pointer_image = xzalloc(
sizeof(
struct wl_cursor_image));
1255 if (!seat->pointer_image)
1256 return UWAC_ERROR_NOMEMORY;
1257 seat->pointer_image->width = width;
1258 seat->pointer_image->height = height;
1259 seat->pointer_image->hotspot_x = hot_x;
1260 seat->pointer_image->hotspot_y = hot_y;
1262 free(seat->pointer_data);
1263 seat->pointer_data = xmalloc(length);
1264 memcpy(seat->pointer_data, data, length);
1265 seat->pointer_size = length;
1267 seat->pointer_type = 2;
1270 else if (length != 0)
1272 seat->pointer_type = 0;
1277 seat->pointer_type = 1;
1279 if (seat && !seat->default_cursor)
1280 return UWAC_SUCCESS;
1281 return set_cursor_image(seat, seat->display->pointer_focus_serial);