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 */
722 ecore_wl_input_cursor_from_name_set(input, "left_ptr");
724 if ((win = wl_surface_get_user_data(surface)))
726 win->pointer_device = input;
727 input->pointer_focus = win;
729 _ecore_wl_input_mouse_in_send(input, win, input->timestamp);
734 _ecore_wl_input_cb_pointer_leave(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, struct wl_surface *surface)
736 Ecore_Wl_Input *input;
737 Ecore_Wl_Window *win;
739 LOGFN(__FILE__, __LINE__, __FUNCTION__);
741 if (!surface) return;
742 if (!(input = data)) return;
744 input->display->serial = serial;
746 if (!surface) return;
747 if (!(win = wl_surface_get_user_data(surface))) return;
749 win->pointer_device = NULL;
750 input->pointer_focus = NULL;
752 /* _ecore_wl_input_mouse_move_send(input, win, input->timestamp); */
753 _ecore_wl_input_mouse_out_send(input, win, input->timestamp);
757 /* move or resize started */
759 /* printf("Pointer Leave WITH a Grab\n"); */
764 _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__)
766 Ecore_Wl_Input *input;
767 Ecore_Wl_Window *win = NULL;
769 LOGFN(__FILE__, __LINE__, __FUNCTION__);
771 if (!surface) return;
772 if (!(input = data)) return;
774 if (!input->timestamp)
778 gettimeofday(&tv, NULL);
779 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
782 input->display->serial = serial;
784 if (!(win = wl_surface_get_user_data(surface))) return;
786 win->keyboard_device = input;
787 input->keyboard_focus = win;
789 _ecore_wl_input_focus_in_send(input, win, input->timestamp);
793 _ecore_wl_input_cb_keyboard_leave(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, struct wl_surface *surface)
795 Ecore_Wl_Input *input;
796 Ecore_Wl_Window *win;
798 LOGFN(__FILE__, __LINE__, __FUNCTION__);
800 if (!surface) return;
801 if (!(input = data)) return;
803 if (input->repeat.timerfd)
805 struct itimerspec ts;
807 ts.it_interval.tv_sec = 0;
808 ts.it_interval.tv_nsec = 0;
809 ts.it_value.tv_sec = 0;
810 ts.it_value.tv_nsec = 0;
812 timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
815 if (!input->timestamp)
819 gettimeofday(&tv, NULL);
820 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
823 input->display->serial = serial;
825 if (!surface) return;
826 if (!(win = wl_surface_get_user_data(surface))) return;
828 win->keyboard_device = NULL;
829 _ecore_wl_input_focus_out_send(input, win, input->timestamp);
831 input->keyboard_focus = NULL;
835 _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)
837 Ecore_Wl_Input *input;
839 LOGFN(__FILE__, __LINE__, __FUNCTION__);
841 if (!surface) return;
842 if (!(input = data)) return;
844 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
845 * This needs to be tested with an actual touch device */
846 /* input->timestamp = timestamp; */
847 input->display->serial = serial;
848 input->sx = wl_fixed_to_int(x);
849 input->sy = wl_fixed_to_int(y);
850 _ecore_wl_input_cb_pointer_enter(data, NULL, serial, surface, x, y);
851 if ((input->pointer_focus) && (!input->grab))
853 ecore_wl_input_grab(input, input->pointer_focus, BTN_LEFT);
854 input->grab_timestamp = timestamp;
857 _ecore_wl_input_mouse_down_send(input, input->pointer_focus, BTN_LEFT, timestamp);
861 _ecore_wl_input_cb_touch_up(void *data, struct wl_touch *touch __UNUSED__, unsigned int serial, unsigned int timestamp, int id __UNUSED__)
863 Ecore_Wl_Input *input;
865 LOGFN(__FILE__, __LINE__, __FUNCTION__);
867 if (!(input = data)) return;
869 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
870 * This needs to be tested with an actual touch device */
871 /* input->timestamp = timestamp; */
872 input->display->serial = serial;
873 _ecore_wl_input_mouse_up_send(input, input->pointer_focus, BTN_LEFT, timestamp);
874 if ((input->grab) && (input->grab_button == BTN_LEFT))
875 ecore_wl_input_ungrab(input);
879 _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)
881 Ecore_Wl_Input *input;
883 LOGFN(__FILE__, __LINE__, __FUNCTION__);
885 if (!(input = data)) return;
887 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
888 * This needs to be tested with an actual touch device */
889 /* input->timestamp = timestamp; */
890 input->sx = wl_fixed_to_int(x);
891 input->sy = wl_fixed_to_int(y);
893 _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
897 _ecore_wl_input_cb_touch_frame(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
899 LOGFN(__FILE__, __LINE__, __FUNCTION__);
903 _ecore_wl_input_cb_touch_cancel(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
905 LOGFN(__FILE__, __LINE__, __FUNCTION__);
909 _ecore_wl_input_cb_data_offer(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
911 LOGFN(__FILE__, __LINE__, __FUNCTION__);
913 _ecore_wl_dnd_add(data, data_device, offer);
917 _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)
919 LOGFN(__FILE__, __LINE__, __FUNCTION__);
921 if (!surface) return;
923 _ecore_wl_dnd_enter(data, data_device, timestamp, surface, x, y, offer);
927 _ecore_wl_input_cb_data_leave(void *data, struct wl_data_device *data_device)
929 LOGFN(__FILE__, __LINE__, __FUNCTION__);
931 _ecore_wl_dnd_leave(data, data_device);
935 _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)
937 LOGFN(__FILE__, __LINE__, __FUNCTION__);
939 _ecore_wl_dnd_motion(data, data_device, timestamp, x, y);
943 _ecore_wl_input_cb_data_drop(void *data, struct wl_data_device *data_device)
945 LOGFN(__FILE__, __LINE__, __FUNCTION__);
947 _ecore_wl_dnd_drop(data, data_device);
951 _ecore_wl_input_cb_data_selection(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
953 LOGFN(__FILE__, __LINE__, __FUNCTION__);
955 _ecore_wl_dnd_selection(data, data_device, offer);
959 _ecore_wl_input_mouse_move_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
961 Ecore_Event_Mouse_Move *ev;
963 LOGFN(__FILE__, __LINE__, __FUNCTION__);
965 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Move)))) return;
967 ev->timestamp = timestamp;
970 /* ev->root.x = input->sx; */
971 /* ev->root.y = input->sy; */
972 ev->modifiers = input->modifiers;
973 ev->multi.device = 0;
974 ev->multi.radius = 1;
975 ev->multi.radius_x = 1;
976 ev->multi.radius_y = 1;
977 ev->multi.pressure = 1.0;
978 ev->multi.angle = 0.0;
979 ev->multi.x = input->sx;
980 ev->multi.y = input->sy;
984 ev->window = win->id;
985 ev->event_window = win->id;
988 ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
992 _ecore_wl_input_mouse_in_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
994 Ecore_Wl_Event_Mouse_In *ev;
996 LOGFN(__FILE__, __LINE__, __FUNCTION__);
998 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_In)))) return;
1002 /* ev->root.x = input->sx; */
1003 /* ev->root.y = input->sy; */
1004 ev->modifiers = input->modifiers;
1005 ev->timestamp = timestamp;
1009 ev->window = win->id;
1010 ev->event_window = win->id;
1013 ecore_event_add(ECORE_WL_EVENT_MOUSE_IN, ev, NULL, NULL);
1017 _ecore_wl_input_mouse_out_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1019 Ecore_Wl_Event_Mouse_Out *ev;
1021 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1023 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_Out)))) return;
1027 /* ev->root.x = input->sx; */
1028 /* ev->root.y = input->sy; */
1029 ev->modifiers = input->modifiers;
1030 ev->timestamp = timestamp;
1034 ev->window = win->id;
1035 ev->event_window = win->id;
1038 ecore_event_add(ECORE_WL_EVENT_MOUSE_OUT, ev, NULL, NULL);
1042 _ecore_wl_input_focus_in_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1044 Ecore_Wl_Event_Focus_In *ev;
1046 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1048 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Focus_In)))) return;
1049 ev->timestamp = timestamp;
1050 if (win) ev->win = win->id;
1051 ecore_event_add(ECORE_WL_EVENT_FOCUS_IN, ev, NULL, NULL);
1055 _ecore_wl_input_focus_out_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1057 Ecore_Wl_Event_Focus_Out *ev;
1059 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1061 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Focus_Out)))) return;
1062 ev->timestamp = timestamp;
1063 if (win) ev->win = win->id;
1064 ecore_event_add(ECORE_WL_EVENT_FOCUS_OUT, ev, NULL, NULL);
1068 _ecore_wl_input_mouse_down_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button, unsigned int timestamp)
1070 Ecore_Event_Mouse_Button *ev;
1072 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1074 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1076 if (button == BTN_LEFT)
1078 else if (button == BTN_MIDDLE)
1080 else if (button == BTN_RIGHT)
1083 ev->buttons = button;
1085 ev->timestamp = timestamp;
1088 /* ev->root.x = input->sx; */
1089 /* ev->root.y = input->sy; */
1090 ev->modifiers = input->modifiers;
1092 /* FIXME: Need to get these from wayland somehow */
1093 ev->double_click = 0;
1094 ev->triple_click = 0;
1096 ev->multi.device = 0;
1097 ev->multi.radius = 1;
1098 ev->multi.radius_x = 1;
1099 ev->multi.radius_y = 1;
1100 ev->multi.pressure = 1.0;
1101 ev->multi.angle = 0.0;
1102 ev->multi.x = input->sx;
1103 ev->multi.y = input->sy;
1107 ev->window = win->id;
1108 ev->event_window = win->id;
1111 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
1115 _ecore_wl_input_mouse_up_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button, unsigned int timestamp)
1117 Ecore_Event_Mouse_Button *ev;
1119 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1121 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1123 if (button == BTN_LEFT)
1125 else if (button == BTN_MIDDLE)
1127 else if (button == BTN_RIGHT)
1130 ev->buttons = button;
1132 ev->timestamp = timestamp;
1135 /* ev->root.x = input->sx; */
1136 /* ev->root.y = input->sy; */
1137 ev->modifiers = input->modifiers;
1139 /* FIXME: Need to get these from wayland somehow */
1140 ev->double_click = 0;
1141 ev->triple_click = 0;
1143 ev->multi.device = 0;
1144 ev->multi.radius = 1;
1145 ev->multi.radius_x = 1;
1146 ev->multi.radius_y = 1;
1147 ev->multi.pressure = 1.0;
1148 ev->multi.angle = 0.0;
1149 ev->multi.x = input->sx;
1150 ev->multi.y = input->sy;
1154 ev->window = win->id;
1155 ev->event_window = win->id;
1158 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
1162 _ecore_wl_input_mouse_wheel_send(Ecore_Wl_Input *input, unsigned int axis, int value, unsigned int timestamp)
1164 Ecore_Event_Mouse_Wheel *ev;
1166 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1168 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Wheel)))) return;
1170 ev->timestamp = timestamp;
1171 ev->modifiers = input->modifiers;
1174 /* ev->root.x = input->sx; */
1175 /* ev->root.y = input->sy; */
1177 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
1182 else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
1190 ev->window = input->grab->id;
1191 ev->event_window = input->grab->id;
1193 else if (input->pointer_focus)
1195 ev->window = input->pointer_focus->id;
1196 ev->event_window = input->pointer_focus->id;
1199 ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
1203 _ecore_wl_input_set_selection(Ecore_Wl_Input *input, struct wl_data_source *source)
1205 wl_data_device_set_selection(input->data_device, source, input->display->serial);