6 * NB: Events that receive a 'serial' instead of timestamp
8 * input_device_attach (for pointer image)
9 * input_device_button_event (button press/release)
10 * input_device_key_press
11 * input_device_pointer_enter
12 * input_device_pointer_leave
13 * input_device_keyboard_enter
14 * input_device_keyboard_leave
15 * input_device_touch_down
16 * input_device_touch_up
20 #include "ecore_wl_private.h"
22 #include <sys/timerfd.h>
24 /* FIXME: This gives BTN_LEFT/RIGHT/MIDDLE for linux systems ...
25 * What about other OSs ?? */
27 # include <linux/input.h>
29 # define BTN_LEFT 0x110
30 # define BTN_RIGHT 0x111
31 # define BTN_MIDDLE 0x112
32 # define BTN_SIDE 0x113
33 # define BTN_EXTRA 0x114
34 # define BTN_FORWARD 0x115
35 # define BTN_BACK 0x116
38 #define MOD_SHIFT_MASK 0x01
39 #define MOD_ALT_MASK 0x02
40 #define MOD_CONTROL_MASK 0x04
42 Ecore_Wl_Dnd *glb_dnd = NULL;
44 /* local function prototypes */
45 static void _ecore_wl_input_seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps);
47 static void _ecore_wl_input_cb_pointer_enter(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, struct wl_surface *surface, wl_fixed_t sx, wl_fixed_t sy);
48 static void _ecore_wl_input_cb_pointer_leave(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, struct wl_surface *surface);
49 static void _ecore_wl_input_cb_pointer_motion(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int timestamp, wl_fixed_t sx, wl_fixed_t sy);
50 static void _ecore_wl_input_cb_pointer_button(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, unsigned int timestamp, unsigned int button, unsigned int state);
51 static void _ecore_wl_input_cb_pointer_axis(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int timestamp, unsigned int axis, wl_fixed_t value);
52 static void _ecore_wl_input_cb_pointer_frame(void *data, struct wl_callback *callback, unsigned int timestamp __UNUSED__);
53 static void _ecore_wl_input_cb_keyboard_keymap(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int format, int fd, unsigned int size);
54 static void _ecore_wl_input_cb_keyboard_enter(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, struct wl_surface *surface, struct wl_array *keys __UNUSED__);
55 static void _ecore_wl_input_cb_keyboard_leave(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, struct wl_surface *surface);
56 static void _ecore_wl_input_cb_keyboard_key(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, unsigned int timestamp, unsigned int key, unsigned int state);
57 static void _ecore_wl_input_cb_keyboard_modifiers(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial __UNUSED__, unsigned int depressed, unsigned int latched, unsigned int locked, unsigned int group);
58 static Eina_Bool _ecore_wl_input_cb_keyboard_repeat(void *data, Ecore_Fd_Handler *handler __UNUSED__);
59 static void _ecore_wl_input_cb_touch_down(void *data, struct wl_touch *touch __UNUSED__, unsigned int serial, unsigned int timestamp, struct wl_surface *surface __UNUSED__, int id __UNUSED__, wl_fixed_t x, wl_fixed_t y);
60 static void _ecore_wl_input_cb_touch_up(void *data, struct wl_touch *touch __UNUSED__, unsigned int serial, unsigned int timestamp, int id __UNUSED__);
61 static void _ecore_wl_input_cb_touch_motion(void *data, struct wl_touch *touch __UNUSED__, unsigned int timestamp, int id __UNUSED__, wl_fixed_t x, wl_fixed_t y);
62 static void _ecore_wl_input_cb_touch_frame(void *data __UNUSED__, struct wl_touch *touch __UNUSED__);
63 static void _ecore_wl_input_cb_touch_cancel(void *data __UNUSED__, struct wl_touch *touch __UNUSED__);
64 static void _ecore_wl_input_cb_data_offer(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer);
65 static void _ecore_wl_input_cb_data_enter(void *data, struct wl_data_device *data_device, unsigned int timestamp, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *offer);
66 static void _ecore_wl_input_cb_data_leave(void *data, struct wl_data_device *data_device);
67 static void _ecore_wl_input_cb_data_motion(void *data, struct wl_data_device *data_device, unsigned int timestamp, wl_fixed_t x, wl_fixed_t y);
68 static void _ecore_wl_input_cb_data_drop(void *data, struct wl_data_device *data_device);
69 static void _ecore_wl_input_cb_data_selection(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer);
71 static void _ecore_wl_input_mouse_move_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp);
72 static void _ecore_wl_input_mouse_in_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp);
73 static void _ecore_wl_input_mouse_out_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp);
74 static void _ecore_wl_input_focus_in_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp);
75 static void _ecore_wl_input_focus_out_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp);
76 static void _ecore_wl_input_mouse_down_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button, unsigned int timestamp);
77 static void _ecore_wl_input_mouse_up_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button, unsigned int timestamp);
78 static void _ecore_wl_input_mouse_wheel_send(Ecore_Wl_Input *input, unsigned int axis, int value, unsigned int timestamp);
80 /* static int _ecore_wl_input_keysym_to_string(unsigned int symbol, char *buffer, int len); */
82 /* wayland interfaces */
83 static const struct wl_pointer_listener pointer_listener =
85 _ecore_wl_input_cb_pointer_enter,
86 _ecore_wl_input_cb_pointer_leave,
87 _ecore_wl_input_cb_pointer_motion,
88 _ecore_wl_input_cb_pointer_button,
89 _ecore_wl_input_cb_pointer_axis,
92 static const struct wl_keyboard_listener keyboard_listener =
94 _ecore_wl_input_cb_keyboard_keymap,
95 _ecore_wl_input_cb_keyboard_enter,
96 _ecore_wl_input_cb_keyboard_leave,
97 _ecore_wl_input_cb_keyboard_key,
98 _ecore_wl_input_cb_keyboard_modifiers,
101 static const struct wl_touch_listener touch_listener =
103 _ecore_wl_input_cb_touch_down,
104 _ecore_wl_input_cb_touch_up,
105 _ecore_wl_input_cb_touch_motion,
106 _ecore_wl_input_cb_touch_frame,
107 _ecore_wl_input_cb_touch_cancel
110 static const struct wl_seat_listener _ecore_wl_seat_listener =
112 _ecore_wl_input_seat_handle_capabilities,
115 static const struct wl_data_device_listener _ecore_wl_data_listener =
117 _ecore_wl_input_cb_data_offer,
118 _ecore_wl_input_cb_data_enter,
119 _ecore_wl_input_cb_data_leave,
120 _ecore_wl_input_cb_data_motion,
121 _ecore_wl_input_cb_data_drop,
122 _ecore_wl_input_cb_data_selection
125 static const struct wl_callback_listener _ecore_wl_pointer_surface_listener =
127 _ecore_wl_input_cb_pointer_frame
130 /* local variables */
131 static int _pointer_x, _pointer_y;
134 ecore_wl_input_grab(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button)
136 LOGFN(__FILE__, __LINE__, __FUNCTION__);
140 input->grab_button = button;
144 ecore_wl_input_ungrab(Ecore_Wl_Input *input)
146 LOGFN(__FILE__, __LINE__, __FUNCTION__);
150 input->grab_button = 0;
153 /* NB: This function should be called just before shell move and shell resize
154 * functions. Those requests will trigger a mouse/touch implicit grab on the
155 * compositor that will prevent the respective mouse/touch up events being
156 * released after the end of the operation. This function checks if such grab
157 * is in place for those windows and, if so, emit the respective mouse up
158 * event. It's a workaround to the fact that wayland doesn't inform the
159 * application about this move or resize grab being finished.
162 _ecore_wl_input_grab_release(Ecore_Wl_Input *input, Ecore_Wl_Window *win)
164 LOGFN(__FILE__, __LINE__, __FUNCTION__);
167 if (input->grab != win) return;
169 _ecore_wl_input_mouse_up_send(input, input->grab,
170 input->grab_button, input->grab_timestamp);
171 ecore_wl_input_ungrab(input);
175 ecore_wl_input_pointer_set(Ecore_Wl_Input *input, struct wl_surface *surface, int hot_x, int hot_y)
177 LOGFN(__FILE__, __LINE__, __FUNCTION__);
180 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
181 surface, hot_x, hot_y);
185 ecore_wl_input_cursor_from_name_set(Ecore_Wl_Input *input, const char *cursor_name)
187 struct wl_cursor_image *cursor_image;
188 struct wl_buffer *buffer;
189 struct wl_cursor *cursor;
191 LOGFN(__FILE__, __LINE__, __FUNCTION__);
195 eina_stringshare_replace(&input->cursor_name, cursor_name);
197 /* No cursor. Set to default Left Pointer */
199 eina_stringshare_replace(&input->cursor_name, "left_ptr");
201 /* try to get this cursor from the theme */
202 if (!(cursor = ecore_wl_cursor_get(input->cursor_name)))
204 /* if the theme does not have this cursor, default to left pointer */
205 if (!(cursor = ecore_wl_cursor_get("left_ptr")))
209 if ((!cursor->images) || (!cursor->images[0]))
211 ecore_wl_input_pointer_set(input, NULL, 0, 0);
215 cursor_image = cursor->images[0];
216 if ((buffer = wl_cursor_image_get_buffer(cursor_image)))
218 ecore_wl_input_pointer_set(input, input->cursor_surface,
219 cursor_image->hotspot_x,
220 cursor_image->hotspot_y);
221 wl_surface_attach(input->cursor_surface, buffer, 0, 0);
222 wl_surface_damage(input->cursor_surface, 0, 0,
223 cursor_image->width, cursor_image->height);
224 wl_surface_commit(input->cursor_surface);
226 if (!input->cursor_frame_cb)
227 _ecore_wl_input_cb_pointer_frame(input, NULL, 0);
232 ecore_wl_input_cursor_default_restore(Ecore_Wl_Input *input)
234 LOGFN(__FILE__, __LINE__, __FUNCTION__);
238 /* Restore to default wayland cursor */
239 ecore_wl_input_cursor_from_name_set(input, "left_ptr");
242 /* local functions */
244 _ecore_wl_input_add(Ecore_Wl_Display *ewd, unsigned int id)
246 Ecore_Wl_Input *input;
248 LOGFN(__FILE__, __LINE__, __FUNCTION__);
250 if (!(input = malloc(sizeof(Ecore_Wl_Input)))) return;
252 memset(input, 0, sizeof(Ecore_Wl_Input));
254 input->display = ewd;
255 input->pointer_focus = NULL;
256 input->keyboard_focus = NULL;
259 wl_registry_bind(ewd->wl.registry, id, &wl_seat_interface, 1);
260 wl_list_insert(ewd->inputs.prev, &input->link);
262 wl_seat_add_listener(input->seat,
263 &_ecore_wl_seat_listener, input);
264 wl_seat_set_user_data(input->seat, input);
267 wl_data_device_manager_get_data_device(ewd->wl.data_device_manager,
269 wl_data_device_add_listener(input->data_device,
270 &_ecore_wl_data_listener, input);
271 input->cursor_surface =
272 wl_compositor_create_surface(_ecore_wl_disp->wl.compositor);
274 input->repeat.timerfd =
275 timerfd_create(CLOCK_MONOTONIC, (TFD_CLOEXEC | TFD_NONBLOCK));
278 ecore_main_fd_handler_add(input->repeat.timerfd, ECORE_FD_READ,
279 _ecore_wl_input_cb_keyboard_repeat, input,
284 /* create Ecore_Wl_Dnd */
286 if (!(glb_dnd = calloc(1, sizeof(Ecore_Wl_Dnd)))) return;
288 glb_dnd->input = input;
289 input->dnd = glb_dnd;
290 wl_array_init(&glb_dnd->types_offered);
294 _ecore_wl_input_del(Ecore_Wl_Input *input)
298 if (input->cursor_name) eina_stringshare_del(input->cursor_name);
299 input->cursor_name = NULL;
301 if (input->keyboard_focus)
303 Ecore_Wl_Window *win = NULL;
305 if ((win = input->keyboard_focus))
306 win->keyboard_device = NULL;
308 input->keyboard_focus = NULL;
311 if (input->drag_source) _ecore_wl_dnd_del(input->drag_source);
312 input->drag_source = NULL;
314 if (input->selection_source) _ecore_wl_dnd_del(input->selection_source);
315 input->selection_source = NULL;
317 if (input->data_device) wl_data_device_destroy(input->data_device);
319 if (input->xkb.state)
320 xkb_state_unref(input->xkb.state);
321 if (input->xkb.keymap)
322 xkb_map_unref(input->xkb.keymap);
324 if (input->cursor_surface)
325 wl_surface_destroy(input->cursor_surface);
327 wl_list_remove(&input->link);
328 if (input->seat) wl_seat_destroy(input->seat);
330 if (input->repeat.hdlr) ecore_main_fd_handler_del(input->repeat.hdlr);
331 input->repeat.hdlr = NULL;
333 if (input->repeat.timerfd) close(input->repeat.timerfd);
334 input->repeat.timerfd = 0;
340 _ecore_wl_input_pointer_xy_get(int *x, int *y)
342 if (x) *x = _pointer_x;
343 if (y) *y = _pointer_y;
347 _ecore_wl_input_seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps)
349 Ecore_Wl_Input *input;
351 if (!(input = data)) return;
353 if ((caps & WL_SEAT_CAPABILITY_POINTER) && (!input->pointer))
355 input->pointer = wl_seat_get_pointer(seat);
356 wl_pointer_set_user_data(input->pointer, input);
357 wl_pointer_add_listener(input->pointer, &pointer_listener, input);
359 else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && (input->pointer))
361 wl_pointer_destroy(input->pointer);
362 input->pointer = NULL;
365 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && (!input->keyboard))
367 input->keyboard = wl_seat_get_keyboard(seat);
368 wl_keyboard_set_user_data(input->keyboard, input);
369 wl_keyboard_add_listener(input->keyboard, &keyboard_listener, input);
371 else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && (input->keyboard))
373 wl_keyboard_destroy(input->keyboard);
374 input->keyboard = NULL;
377 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && (!input->touch))
379 input->touch = wl_seat_get_touch(seat);
380 wl_touch_set_user_data(input->touch, input);
381 wl_touch_add_listener(input->touch, &touch_listener, input);
383 else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && (input->touch))
385 wl_touch_destroy(input->touch);
392 _ecore_wl_input_cb_pointer_motion(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int timestamp, wl_fixed_t sx, wl_fixed_t sy)
394 Ecore_Wl_Input *input;
396 /* LOGFN(__FILE__, __LINE__, __FUNCTION__); */
398 if (!(input = data)) return;
400 _pointer_x = input->sx = wl_fixed_to_int(sx);
401 _pointer_y = input->sy = wl_fixed_to_int(sy);
403 input->timestamp = timestamp;
405 if (input->pointer_focus)
406 _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
408 ecore_wl_input_cursor_from_name_set(input, input->cursor_name);
412 _ecore_wl_input_cb_pointer_button(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, unsigned int timestamp, unsigned int button, unsigned int state)
414 Ecore_Wl_Input *input;
416 LOGFN(__FILE__, __LINE__, __FUNCTION__);
418 if (!(input = data)) return;
420 input->timestamp = timestamp;
421 input->display->serial = serial;
423 // _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
427 if ((input->pointer_focus) && (!input->grab) && (state))
429 ecore_wl_input_grab(input, input->pointer_focus, button);
430 input->grab_timestamp = timestamp;
433 _ecore_wl_input_mouse_down_send(input, input->pointer_focus,
438 _ecore_wl_input_mouse_up_send(input, input->pointer_focus,
440 if ((input->grab) && (input->grab_button == button) && (!state))
441 ecore_wl_input_ungrab(input);
444 // _ecore_wl_input_mouse_move_send(input, timestamp);
448 _ecore_wl_input_cb_pointer_axis(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int timestamp, unsigned int axis, wl_fixed_t value)
450 Ecore_Wl_Input *input;
452 LOGFN(__FILE__, __LINE__, __FUNCTION__);
454 if (!(input = data)) return;
455 _ecore_wl_input_mouse_wheel_send(input, axis, wl_fixed_to_int(value),
460 _ecore_wl_input_cb_pointer_frame(void *data, struct wl_callback *callback, unsigned int timestamp __UNUSED__)
462 Ecore_Wl_Input *input;
464 LOGFN(__FILE__, __LINE__, __FUNCTION__);
466 if (!(input = data)) return;
470 if (callback != input->cursor_frame_cb) return;
471 wl_callback_destroy(callback);
472 input->cursor_frame_cb = NULL;
475 if (!input->cursor_name)
477 ecore_wl_input_pointer_set(input, NULL, 0, 0);
481 if (!input->cursor_frame_cb)
483 input->cursor_frame_cb = wl_surface_frame(input->cursor_surface);
484 wl_callback_add_listener(input->cursor_frame_cb,
485 &_ecore_wl_pointer_surface_listener, input);
490 _ecore_wl_input_cb_keyboard_keymap(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int format, int fd, unsigned int size)
492 Ecore_Wl_Input *input;
495 LOGFN(__FILE__, __LINE__, __FUNCTION__);
503 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
509 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
510 if (map == MAP_FAILED)
517 xkb_map_new_from_string(input->display->xkb.context, map,
518 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
523 if (!(input->xkb.keymap)) return;
524 if (!(input->xkb.state = xkb_state_new(input->xkb.keymap)))
526 xkb_map_unref(input->xkb.keymap);
527 input->xkb.keymap = NULL;
531 input->xkb.control_mask =
532 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
533 input->xkb.alt_mask =
534 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
535 input->xkb.shift_mask =
536 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
540 _ecore_wl_input_cb_keyboard_key(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, unsigned int timestamp, unsigned int keycode, unsigned int state)
542 Ecore_Wl_Input *input;
543 Ecore_Wl_Window *win;
544 unsigned int code, num;
545 const xkb_keysym_t *syms;
546 xkb_keysym_t sym = XKB_KEY_NoSymbol;
548 char string[32], key[32], keyname[32];// compose[32];
550 struct itimerspec ts;
553 LOGFN(__FILE__, __LINE__, __FUNCTION__);
555 if (!(input = data)) return;
556 input->display->serial = serial;
558 /* xkb rules reflect X broken keycodes, so offset by 8 */
561 win = input->keyboard_focus;
562 if ((!win) || (win->keyboard_device != input) || (!input->xkb.state))
565 mask = xkb_state_serialize_mods(input->xkb.state,
566 XKB_STATE_DEPRESSED | XKB_STATE_LATCHED);
568 input->modifiers = 0;
570 /* The Ecore_Event_Modifiers don't quite match the X mask bits */
571 if (mask & input->xkb.control_mask)
572 input->modifiers |= MOD_CONTROL_MASK;
573 if (mask & input->xkb.alt_mask)
574 input->modifiers |= MOD_ALT_MASK;
575 if (mask & input->xkb.shift_mask)
576 input->modifiers |= MOD_SHIFT_MASK;
578 num = xkb_key_get_syms(input->xkb.state, code, &syms);
579 if (num == 1) sym = syms[0];
581 memset(key, 0, sizeof(key));
582 xkb_keysym_get_name(sym, key, sizeof(key));
584 memset(keyname, 0, sizeof(keyname));
585 xkb_keysym_get_name(sym, keyname, sizeof(keyname));
586 if (keyname[0] == '\0')
587 snprintf(keyname, sizeof(keyname), "Keycode-%i", code);
589 memset(string, 0, sizeof(string));
590 if (xkb_keysym_to_utf8(sym, string, 32) <= 0)
592 /* FIXME: NB: We may need to add more checks here for other
593 * non-printable characters */
594 if ((sym == XKB_KEY_Tab) || (sym == XKB_KEY_ISO_Left_Tab))
595 string[len++] = '\t';
598 /* FIXME: NB: Start hacking on compose key support */
599 /* memset(compose, 0, sizeof(compose)); */
600 /* if (sym == XKB_KEY_Multi_key) */
602 /* if (xkb_keysym_to_utf8(sym, compose, 32) <= 0) */
603 /* compose[0] = '\0'; */
606 e = malloc(sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
607 ((string[0] != '\0') ? strlen(string) : 0) + 3);
610 e->keyname = (char *)(e + 1);
611 e->key = e->keyname + strlen(keyname) + 1;
612 e->string = strlen(string) ? e->key + strlen(key) + 1 : NULL;
613 e->compose = e->string;
615 strcpy((char *)e->keyname, keyname);
616 strcpy((char *)e->key, key);
617 if (strlen(string)) strcpy((char *)e->string, string);
620 e->event_window = win->id;
621 e->timestamp = timestamp;
622 e->modifiers = input->modifiers;
625 ecore_event_add(ECORE_EVENT_KEY_DOWN, e, NULL, NULL);
627 ecore_event_add(ECORE_EVENT_KEY_UP, e, NULL, NULL);
629 if ((!state) && (keycode == input->repeat.key))
631 input->repeat.sym = 0;
632 input->repeat.key = 0;
633 input->repeat.time = 0;
635 ts.it_interval.tv_sec = 0;
636 ts.it_interval.tv_nsec = 0;
637 ts.it_value.tv_sec = 0;
638 ts.it_value.tv_nsec = 0;
640 timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
643 ((!input->repeat.key) ||
644 ((keycode) && (keycode != input->repeat.key))))
646 input->repeat.sym = sym;
647 input->repeat.key = keycode;
648 input->repeat.time = timestamp;
650 /* interval after expires */
651 ts.it_interval.tv_sec = 0;
652 ts.it_interval.tv_nsec = 35 * 1000 * 1000;
654 /* initial expiration */
655 ts.it_value.tv_sec = 0;
656 ts.it_value.tv_nsec = 500 * 1000 * 1000;
658 timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
663 _ecore_wl_input_cb_keyboard_modifiers(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial __UNUSED__, unsigned int depressed, unsigned int latched, unsigned int locked, unsigned int group)
665 Ecore_Wl_Input *input;
667 LOGFN(__FILE__, __LINE__, __FUNCTION__);
669 if (!(input = data)) return;
670 xkb_state_update_mask(input->xkb.state, depressed, latched,
671 locked, 0, 0, group);
675 _ecore_wl_input_cb_keyboard_repeat(void *data, Ecore_Fd_Handler *handler __UNUSED__)
677 Ecore_Wl_Input *input;
678 Ecore_Wl_Window *win = NULL;
679 unsigned long long int xp;
681 LOGFN(__FILE__, __LINE__, __FUNCTION__);
683 if (!(input = data)) return ECORE_CALLBACK_RENEW;
685 /* Trap for EAGAIN */
686 if (read(input->repeat.timerfd, &xp, sizeof(xp)) != sizeof(xp))
687 return ECORE_CALLBACK_RENEW;
689 if ((win = input->keyboard_focus))
690 _ecore_wl_input_cb_keyboard_key(input, NULL, input->display->serial,
692 input->repeat.key, EINA_TRUE);
694 return ECORE_CALLBACK_RENEW;
698 _ecore_wl_input_cb_pointer_enter(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, struct wl_surface *surface, wl_fixed_t sx, wl_fixed_t sy)
700 Ecore_Wl_Input *input;
701 Ecore_Wl_Window *win = NULL;
703 LOGFN(__FILE__, __LINE__, __FUNCTION__);
705 if (!surface) return;
706 if (!(input = data)) return;
708 if (!input->timestamp)
712 gettimeofday(&tv, NULL);
713 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
716 input->sx = wl_fixed_to_double(sx);
717 input->sy = wl_fixed_to_double(sy);
718 input->display->serial = serial;
719 input->pointer_enter_serial = serial;
721 /* The cursor on the surface is undefined until we set it */
723 ecore_wl_input_cursor_from_name_set(input, "left_ptr");
725 if ((win = wl_surface_get_user_data(surface)))
727 win->pointer_device = input;
728 input->pointer_focus = win;
730 _ecore_wl_input_mouse_in_send(input, win, input->timestamp);
735 _ecore_wl_input_cb_pointer_leave(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, struct wl_surface *surface)
737 Ecore_Wl_Input *input;
738 Ecore_Wl_Window *win;
740 LOGFN(__FILE__, __LINE__, __FUNCTION__);
742 if (!surface) return;
743 if (!(input = data)) return;
745 input->display->serial = serial;
747 if (!surface) return;
748 if (!(win = wl_surface_get_user_data(surface))) return;
750 win->pointer_device = NULL;
751 input->pointer_focus = NULL;
753 /* _ecore_wl_input_mouse_move_send(input, win, input->timestamp); */
754 _ecore_wl_input_mouse_out_send(input, win, input->timestamp);
758 /* move or resize started */
760 /* printf("Pointer Leave WITH a Grab\n"); */
765 _ecore_wl_input_cb_keyboard_enter(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, struct wl_surface *surface, struct wl_array *keys __UNUSED__)
767 Ecore_Wl_Input *input;
768 Ecore_Wl_Window *win = NULL;
770 LOGFN(__FILE__, __LINE__, __FUNCTION__);
772 if (!surface) return;
773 if (!(input = data)) return;
775 if (!input->timestamp)
779 gettimeofday(&tv, NULL);
780 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
783 input->display->serial = serial;
785 if (!(win = wl_surface_get_user_data(surface))) return;
787 win->keyboard_device = input;
788 input->keyboard_focus = win;
790 _ecore_wl_input_focus_in_send(input, win, input->timestamp);
794 _ecore_wl_input_cb_keyboard_leave(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, struct wl_surface *surface)
796 Ecore_Wl_Input *input;
797 Ecore_Wl_Window *win;
799 LOGFN(__FILE__, __LINE__, __FUNCTION__);
801 if (!surface) return;
802 if (!(input = data)) return;
804 if (input->repeat.timerfd)
806 struct itimerspec ts;
808 ts.it_interval.tv_sec = 0;
809 ts.it_interval.tv_nsec = 0;
810 ts.it_value.tv_sec = 0;
811 ts.it_value.tv_nsec = 0;
813 timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
816 if (!input->timestamp)
820 gettimeofday(&tv, NULL);
821 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
824 input->display->serial = serial;
826 if (!surface) return;
827 if (!(win = wl_surface_get_user_data(surface))) return;
829 win->keyboard_device = NULL;
830 _ecore_wl_input_focus_out_send(input, win, input->timestamp);
832 input->keyboard_focus = NULL;
836 _ecore_wl_input_cb_touch_down(void *data, struct wl_touch *touch __UNUSED__, unsigned int serial, unsigned int timestamp, struct wl_surface *surface __UNUSED__, int id __UNUSED__, wl_fixed_t x, wl_fixed_t y)
838 Ecore_Wl_Input *input;
840 LOGFN(__FILE__, __LINE__, __FUNCTION__);
842 if (!surface) return;
843 if (!(input = data)) return;
845 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
846 * This needs to be tested with an actual touch device */
847 /* input->timestamp = timestamp; */
848 input->display->serial = serial;
849 input->sx = wl_fixed_to_int(x);
850 input->sy = wl_fixed_to_int(y);
851 _ecore_wl_input_cb_pointer_enter(data, NULL, serial, surface, x, y);
852 if ((input->pointer_focus) && (!input->grab))
854 ecore_wl_input_grab(input, input->pointer_focus, BTN_LEFT);
855 input->grab_timestamp = timestamp;
858 _ecore_wl_input_mouse_down_send(input, input->pointer_focus, BTN_LEFT, timestamp);
862 _ecore_wl_input_cb_touch_up(void *data, struct wl_touch *touch __UNUSED__, unsigned int serial, unsigned int timestamp, int id __UNUSED__)
864 Ecore_Wl_Input *input;
866 LOGFN(__FILE__, __LINE__, __FUNCTION__);
868 if (!(input = data)) return;
870 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
871 * This needs to be tested with an actual touch device */
872 /* input->timestamp = timestamp; */
873 input->display->serial = serial;
874 _ecore_wl_input_mouse_up_send(input, input->pointer_focus, BTN_LEFT, timestamp);
875 if ((input->grab) && (input->grab_button == BTN_LEFT))
876 ecore_wl_input_ungrab(input);
880 _ecore_wl_input_cb_touch_motion(void *data, struct wl_touch *touch __UNUSED__, unsigned int timestamp, int id __UNUSED__, wl_fixed_t x, wl_fixed_t y)
882 Ecore_Wl_Input *input;
884 LOGFN(__FILE__, __LINE__, __FUNCTION__);
886 if (!(input = data)) return;
888 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
889 * This needs to be tested with an actual touch device */
890 /* input->timestamp = timestamp; */
891 input->sx = wl_fixed_to_int(x);
892 input->sy = wl_fixed_to_int(y);
894 _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
898 _ecore_wl_input_cb_touch_frame(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
900 LOGFN(__FILE__, __LINE__, __FUNCTION__);
904 _ecore_wl_input_cb_touch_cancel(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
906 LOGFN(__FILE__, __LINE__, __FUNCTION__);
910 _ecore_wl_input_cb_data_offer(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
912 LOGFN(__FILE__, __LINE__, __FUNCTION__);
914 _ecore_wl_dnd_add(data, data_device, offer);
918 _ecore_wl_input_cb_data_enter(void *data, struct wl_data_device *data_device, unsigned int timestamp, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *offer)
920 LOGFN(__FILE__, __LINE__, __FUNCTION__);
922 if (!surface) return;
924 _ecore_wl_dnd_enter(data, data_device, timestamp, surface, x, y, offer);
928 _ecore_wl_input_cb_data_leave(void *data, struct wl_data_device *data_device)
930 LOGFN(__FILE__, __LINE__, __FUNCTION__);
932 _ecore_wl_dnd_leave(data, data_device);
936 _ecore_wl_input_cb_data_motion(void *data, struct wl_data_device *data_device, unsigned int timestamp, wl_fixed_t x, wl_fixed_t y)
938 LOGFN(__FILE__, __LINE__, __FUNCTION__);
940 _ecore_wl_dnd_motion(data, data_device, timestamp, x, y);
944 _ecore_wl_input_cb_data_drop(void *data, struct wl_data_device *data_device)
946 LOGFN(__FILE__, __LINE__, __FUNCTION__);
948 _ecore_wl_dnd_drop(data, data_device);
952 _ecore_wl_input_cb_data_selection(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
954 LOGFN(__FILE__, __LINE__, __FUNCTION__);
956 _ecore_wl_dnd_selection(data, data_device, offer);
960 _ecore_wl_input_mouse_move_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
962 Ecore_Event_Mouse_Move *ev;
964 LOGFN(__FILE__, __LINE__, __FUNCTION__);
966 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Move)))) return;
968 ev->timestamp = timestamp;
971 /* ev->root.x = input->sx; */
972 /* ev->root.y = input->sy; */
973 ev->modifiers = input->modifiers;
974 ev->multi.device = 0;
975 ev->multi.radius = 1;
976 ev->multi.radius_x = 1;
977 ev->multi.radius_y = 1;
978 ev->multi.pressure = 1.0;
979 ev->multi.angle = 0.0;
980 ev->multi.x = input->sx;
981 ev->multi.y = input->sy;
985 ev->window = win->id;
986 ev->event_window = win->id;
989 ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
993 _ecore_wl_input_mouse_in_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
995 Ecore_Wl_Event_Mouse_In *ev;
997 LOGFN(__FILE__, __LINE__, __FUNCTION__);
999 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_In)))) return;
1003 /* ev->root.x = input->sx; */
1004 /* ev->root.y = input->sy; */
1005 ev->modifiers = input->modifiers;
1006 ev->timestamp = timestamp;
1010 ev->window = win->id;
1011 ev->event_window = win->id;
1014 ecore_event_add(ECORE_WL_EVENT_MOUSE_IN, ev, NULL, NULL);
1018 _ecore_wl_input_mouse_out_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1020 Ecore_Wl_Event_Mouse_Out *ev;
1022 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1024 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_Out)))) return;
1028 /* ev->root.x = input->sx; */
1029 /* ev->root.y = input->sy; */
1030 ev->modifiers = input->modifiers;
1031 ev->timestamp = timestamp;
1035 ev->window = win->id;
1036 ev->event_window = win->id;
1039 ecore_event_add(ECORE_WL_EVENT_MOUSE_OUT, ev, NULL, NULL);
1043 _ecore_wl_input_focus_in_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1045 Ecore_Wl_Event_Focus_In *ev;
1047 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1049 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Focus_In)))) return;
1050 ev->timestamp = timestamp;
1051 if (win) ev->win = win->id;
1052 ecore_event_add(ECORE_WL_EVENT_FOCUS_IN, ev, NULL, NULL);
1056 _ecore_wl_input_focus_out_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1058 Ecore_Wl_Event_Focus_Out *ev;
1060 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1062 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Focus_Out)))) return;
1063 ev->timestamp = timestamp;
1064 if (win) ev->win = win->id;
1065 ecore_event_add(ECORE_WL_EVENT_FOCUS_OUT, ev, NULL, NULL);
1069 _ecore_wl_input_mouse_down_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button, unsigned int timestamp)
1071 Ecore_Event_Mouse_Button *ev;
1073 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1075 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1077 if (button == BTN_LEFT)
1079 else if (button == BTN_MIDDLE)
1081 else if (button == BTN_RIGHT)
1084 ev->buttons = button;
1086 ev->timestamp = timestamp;
1089 /* ev->root.x = input->sx; */
1090 /* ev->root.y = input->sy; */
1091 ev->modifiers = input->modifiers;
1093 /* FIXME: Need to get these from wayland somehow */
1094 ev->double_click = 0;
1095 ev->triple_click = 0;
1097 ev->multi.device = 0;
1098 ev->multi.radius = 1;
1099 ev->multi.radius_x = 1;
1100 ev->multi.radius_y = 1;
1101 ev->multi.pressure = 1.0;
1102 ev->multi.angle = 0.0;
1103 ev->multi.x = input->sx;
1104 ev->multi.y = input->sy;
1108 ev->window = win->id;
1109 ev->event_window = win->id;
1112 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
1116 _ecore_wl_input_mouse_up_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button, unsigned int timestamp)
1118 Ecore_Event_Mouse_Button *ev;
1120 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1122 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1124 if (button == BTN_LEFT)
1126 else if (button == BTN_MIDDLE)
1128 else if (button == BTN_RIGHT)
1131 ev->buttons = button;
1133 ev->timestamp = timestamp;
1136 /* ev->root.x = input->sx; */
1137 /* ev->root.y = input->sy; */
1138 ev->modifiers = input->modifiers;
1140 /* FIXME: Need to get these from wayland somehow */
1141 ev->double_click = 0;
1142 ev->triple_click = 0;
1144 ev->multi.device = 0;
1145 ev->multi.radius = 1;
1146 ev->multi.radius_x = 1;
1147 ev->multi.radius_y = 1;
1148 ev->multi.pressure = 1.0;
1149 ev->multi.angle = 0.0;
1150 ev->multi.x = input->sx;
1151 ev->multi.y = input->sy;
1155 ev->window = win->id;
1156 ev->event_window = win->id;
1159 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
1163 _ecore_wl_input_mouse_wheel_send(Ecore_Wl_Input *input, unsigned int axis, int value, unsigned int timestamp)
1165 Ecore_Event_Mouse_Wheel *ev;
1167 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1169 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Wheel)))) return;
1171 ev->timestamp = timestamp;
1172 ev->modifiers = input->modifiers;
1175 /* ev->root.x = input->sx; */
1176 /* ev->root.y = input->sy; */
1178 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
1183 else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
1191 ev->window = input->grab->id;
1192 ev->event_window = input->grab->id;
1194 else if (input->pointer_focus)
1196 ev->window = input->pointer_focus->id;
1197 ev->event_window = input->pointer_focus->id;
1200 ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
1204 _ecore_wl_input_set_selection(Ecore_Wl_Input *input, struct wl_data_source *source)
1206 wl_data_device_set_selection(input->data_device, source, input->display->serial);