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 timestamp);
77 static void _ecore_wl_input_mouse_up_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, 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;
154 ecore_wl_input_pointer_set(Ecore_Wl_Input *input, struct wl_surface *surface, int hot_x, int hot_y)
156 LOGFN(__FILE__, __LINE__, __FUNCTION__);
159 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
160 surface, hot_x, hot_y);
164 ecore_wl_input_cursor_from_name_set(Ecore_Wl_Input *input, const char *cursor_name)
166 struct wl_cursor_image *cursor_image;
167 struct wl_buffer *buffer;
168 struct wl_cursor *cursor;
170 LOGFN(__FILE__, __LINE__, __FUNCTION__);
174 eina_stringshare_replace(&input->cursor_name, cursor_name);
176 /* No cursor. Set to default Left Pointer */
178 eina_stringshare_replace(&input->cursor_name, "left_ptr");
180 /* try to get this cursor from the theme */
181 if (!(cursor = ecore_wl_cursor_get(input->cursor_name)))
183 /* if the theme does not have this cursor, default to left pointer */
184 if (!(cursor = ecore_wl_cursor_get("left_ptr")))
188 if ((!cursor->images) || (!cursor->images[0]))
190 ecore_wl_input_pointer_set(input, NULL, 0, 0);
194 cursor_image = cursor->images[0];
195 if ((buffer = wl_cursor_image_get_buffer(cursor_image)))
197 ecore_wl_input_pointer_set(input, input->cursor_surface,
198 cursor_image->hotspot_x,
199 cursor_image->hotspot_y);
200 wl_surface_attach(input->cursor_surface, buffer, 0, 0);
201 wl_surface_damage(input->cursor_surface, 0, 0,
202 cursor_image->width, cursor_image->height);
203 wl_surface_commit(input->cursor_surface);
205 if (!input->cursor_frame_cb)
206 _ecore_wl_input_cb_pointer_frame(input, NULL, 0);
211 ecore_wl_input_cursor_default_restore(Ecore_Wl_Input *input)
213 LOGFN(__FILE__, __LINE__, __FUNCTION__);
217 /* Restore to default wayland cursor */
218 ecore_wl_input_cursor_from_name_set(input, "left_ptr");
221 /* local functions */
223 _ecore_wl_input_add(Ecore_Wl_Display *ewd, unsigned int id)
225 Ecore_Wl_Input *input;
227 LOGFN(__FILE__, __LINE__, __FUNCTION__);
229 if (!(input = malloc(sizeof(Ecore_Wl_Input)))) return;
231 memset(input, 0, sizeof(Ecore_Wl_Input));
233 input->display = ewd;
234 input->pointer_focus = NULL;
235 input->keyboard_focus = NULL;
238 wl_registry_bind(ewd->wl.registry, id, &wl_seat_interface, 1);
239 wl_list_insert(ewd->inputs.prev, &input->link);
241 wl_seat_add_listener(input->seat,
242 &_ecore_wl_seat_listener, input);
243 wl_seat_set_user_data(input->seat, input);
246 wl_data_device_manager_get_data_device(ewd->wl.data_device_manager,
248 wl_data_device_add_listener(input->data_device,
249 &_ecore_wl_data_listener, input);
250 input->cursor_surface =
251 wl_compositor_create_surface(_ecore_wl_disp->wl.compositor);
253 input->repeat.timerfd =
254 timerfd_create(CLOCK_MONOTONIC, (TFD_CLOEXEC | TFD_NONBLOCK));
257 ecore_main_fd_handler_add(input->repeat.timerfd, ECORE_FD_READ,
258 _ecore_wl_input_cb_keyboard_repeat, input,
263 /* create Ecore_Wl_Dnd */
265 if (!(glb_dnd = calloc(1, sizeof(Ecore_Wl_Dnd)))) return;
267 glb_dnd->input = input;
268 input->dnd = glb_dnd;
269 wl_array_init(&glb_dnd->types_offered);
273 _ecore_wl_input_del(Ecore_Wl_Input *input)
277 if (input->cursor_name) eina_stringshare_del(input->cursor_name);
278 input->cursor_name = NULL;
280 if (input->keyboard_focus)
282 Ecore_Wl_Window *win = NULL;
284 if ((win = input->keyboard_focus))
285 win->keyboard_device = NULL;
287 input->keyboard_focus = NULL;
290 if (input->drag_source) _ecore_wl_dnd_del(input->drag_source);
291 input->drag_source = NULL;
293 if (input->selection_source) _ecore_wl_dnd_del(input->selection_source);
294 input->selection_source = NULL;
296 if (input->data_device) wl_data_device_destroy(input->data_device);
298 if (input->xkb.state)
299 xkb_state_unref(input->xkb.state);
300 if (input->xkb.keymap)
301 xkb_map_unref(input->xkb.keymap);
303 if (input->cursor_surface)
304 wl_surface_destroy(input->cursor_surface);
306 wl_list_remove(&input->link);
307 if (input->seat) wl_seat_destroy(input->seat);
309 if (input->repeat.hdlr) ecore_main_fd_handler_del(input->repeat.hdlr);
310 input->repeat.hdlr = NULL;
312 if (input->repeat.timerfd) close(input->repeat.timerfd);
313 input->repeat.timerfd = 0;
319 _ecore_wl_input_pointer_xy_get(int *x, int *y)
321 if (x) *x = _pointer_x;
322 if (y) *y = _pointer_y;
326 _ecore_wl_input_seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps)
328 Ecore_Wl_Input *input;
330 if (!(input = data)) return;
332 if ((caps & WL_SEAT_CAPABILITY_POINTER) && (!input->pointer))
334 input->pointer = wl_seat_get_pointer(seat);
335 wl_pointer_set_user_data(input->pointer, input);
336 wl_pointer_add_listener(input->pointer, &pointer_listener, input);
338 else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && (input->pointer))
340 wl_pointer_destroy(input->pointer);
341 input->pointer = NULL;
344 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && (!input->keyboard))
346 input->keyboard = wl_seat_get_keyboard(seat);
347 wl_keyboard_set_user_data(input->keyboard, input);
348 wl_keyboard_add_listener(input->keyboard, &keyboard_listener, input);
350 else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && (input->keyboard))
352 wl_keyboard_destroy(input->keyboard);
353 input->keyboard = NULL;
356 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && (!input->touch))
358 input->touch = wl_seat_get_touch(seat);
359 wl_touch_set_user_data(input->touch, input);
360 wl_touch_add_listener(input->touch, &touch_listener, input);
362 else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && (input->touch))
364 wl_touch_destroy(input->touch);
371 _ecore_wl_input_cb_pointer_motion(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int timestamp, wl_fixed_t sx, wl_fixed_t sy)
373 Ecore_Wl_Input *input;
375 /* LOGFN(__FILE__, __LINE__, __FUNCTION__); */
377 if (!(input = data)) return;
379 _pointer_x = input->sx = wl_fixed_to_int(sx);
380 _pointer_y = input->sy = wl_fixed_to_int(sy);
382 input->timestamp = timestamp;
384 if (input->pointer_focus)
385 _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
387 ecore_wl_input_cursor_from_name_set(input, input->cursor_name);
391 _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)
393 Ecore_Wl_Input *input;
395 LOGFN(__FILE__, __LINE__, __FUNCTION__);
397 if (!(input = data)) return;
399 input->timestamp = timestamp;
400 input->display->serial = serial;
402 // _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
406 if ((input->pointer_focus) && (!input->grab) && (state))
407 ecore_wl_input_grab(input, input->pointer_focus, button);
409 input->button = button;
410 _ecore_wl_input_mouse_down_send(input, input->pointer_focus,
415 _ecore_wl_input_mouse_up_send(input, input->pointer_focus,
419 if ((input->grab) && (input->grab_button == button) && (!state))
420 ecore_wl_input_ungrab(input);
423 // _ecore_wl_input_mouse_move_send(input, timestamp);
427 _ecore_wl_input_cb_pointer_axis(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int timestamp, unsigned int axis, wl_fixed_t value)
429 Ecore_Wl_Input *input;
431 LOGFN(__FILE__, __LINE__, __FUNCTION__);
433 if (!(input = data)) return;
434 _ecore_wl_input_mouse_wheel_send(input, axis, wl_fixed_to_int(value),
439 _ecore_wl_input_cb_pointer_frame(void *data, struct wl_callback *callback, unsigned int timestamp __UNUSED__)
441 Ecore_Wl_Input *input;
443 LOGFN(__FILE__, __LINE__, __FUNCTION__);
445 if (!(input = data)) return;
449 if (callback != input->cursor_frame_cb) return;
450 wl_callback_destroy(callback);
451 input->cursor_frame_cb = NULL;
454 if (!input->cursor_name)
456 ecore_wl_input_pointer_set(input, NULL, 0, 0);
460 if (!input->cursor_frame_cb)
462 input->cursor_frame_cb = wl_surface_frame(input->cursor_surface);
463 wl_callback_add_listener(input->cursor_frame_cb,
464 &_ecore_wl_pointer_surface_listener, input);
469 _ecore_wl_input_cb_keyboard_keymap(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int format, int fd, unsigned int size)
471 Ecore_Wl_Input *input;
474 LOGFN(__FILE__, __LINE__, __FUNCTION__);
482 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
488 map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
489 if (map == MAP_FAILED)
496 xkb_map_new_from_string(input->display->xkb.context, map,
497 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
502 if (!(input->xkb.keymap)) return;
503 if (!(input->xkb.state = xkb_state_new(input->xkb.keymap)))
505 xkb_map_unref(input->xkb.keymap);
506 input->xkb.keymap = NULL;
510 input->xkb.control_mask =
511 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
512 input->xkb.alt_mask =
513 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
514 input->xkb.shift_mask =
515 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
519 _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)
521 Ecore_Wl_Input *input;
522 Ecore_Wl_Window *win;
523 unsigned int code, num;
524 const xkb_keysym_t *syms;
525 xkb_keysym_t sym = XKB_KEY_NoSymbol;
527 char string[32], key[32], keyname[32];// compose[32];
529 struct itimerspec ts;
532 LOGFN(__FILE__, __LINE__, __FUNCTION__);
534 if (!(input = data)) return;
535 input->display->serial = serial;
537 /* xkb rules reflect X broken keycodes, so offset by 8 */
540 win = input->keyboard_focus;
541 if ((!win) || (win->keyboard_device != input) || (!input->xkb.state))
544 mask = xkb_state_serialize_mods(input->xkb.state,
545 XKB_STATE_DEPRESSED | XKB_STATE_LATCHED);
547 input->modifiers = 0;
549 /* The Ecore_Event_Modifiers don't quite match the X mask bits */
550 if (mask & input->xkb.control_mask)
551 input->modifiers |= MOD_CONTROL_MASK;
552 if (mask & input->xkb.alt_mask)
553 input->modifiers |= MOD_ALT_MASK;
554 if (mask & input->xkb.shift_mask)
555 input->modifiers |= MOD_SHIFT_MASK;
557 num = xkb_key_get_syms(input->xkb.state, code, &syms);
558 if (num == 1) sym = syms[0];
560 memset(key, 0, sizeof(key));
561 xkb_keysym_get_name(sym, key, sizeof(key));
563 memset(keyname, 0, sizeof(keyname));
564 xkb_keysym_get_name(sym, keyname, sizeof(keyname));
565 if (keyname[0] == '\0')
566 snprintf(keyname, sizeof(keyname), "Keycode-%i", code);
568 memset(string, 0, sizeof(string));
569 if (xkb_keysym_to_utf8(sym, string, 32) <= 0)
571 /* FIXME: NB: We may need to add more checks here for other
572 * non-printable characters */
573 if ((sym == XKB_KEY_Tab) || (sym == XKB_KEY_ISO_Left_Tab))
574 string[len++] = '\t';
577 /* FIXME: NB: Start hacking on compose key support */
578 /* memset(compose, 0, sizeof(compose)); */
579 /* if (sym == XKB_KEY_Multi_key) */
581 /* if (xkb_keysym_to_utf8(sym, compose, 32) <= 0) */
582 /* compose[0] = '\0'; */
585 e = malloc(sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
586 ((string[0] != '\0') ? strlen(string) : 0) + 3);
589 e->keyname = (char *)(e + 1);
590 e->key = e->keyname + strlen(keyname) + 1;
591 e->string = strlen(string) ? e->key + strlen(key) + 1 : NULL;
592 e->compose = e->string;
594 strcpy((char *)e->keyname, keyname);
595 strcpy((char *)e->key, key);
596 if (strlen(string)) strcpy((char *)e->string, string);
599 e->event_window = win->id;
600 e->timestamp = timestamp;
601 e->modifiers = input->modifiers;
604 ecore_event_add(ECORE_EVENT_KEY_DOWN, e, NULL, NULL);
606 ecore_event_add(ECORE_EVENT_KEY_UP, e, NULL, NULL);
608 if ((!state) && (keycode == input->repeat.key))
610 input->repeat.sym = 0;
611 input->repeat.key = 0;
612 input->repeat.time = 0;
614 ts.it_interval.tv_sec = 0;
615 ts.it_interval.tv_nsec = 0;
616 ts.it_value.tv_sec = 0;
617 ts.it_value.tv_nsec = 0;
619 timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
622 ((!input->repeat.key) ||
623 ((keycode) && (keycode != input->repeat.key))))
625 input->repeat.sym = sym;
626 input->repeat.key = keycode;
627 input->repeat.time = timestamp;
629 /* interval after expires */
630 ts.it_interval.tv_sec = 0;
631 ts.it_interval.tv_nsec = 35 * 1000 * 1000;
633 /* initial expiration */
634 ts.it_value.tv_sec = 0;
635 ts.it_value.tv_nsec = 500 * 1000 * 1000;
637 timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
642 _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)
644 Ecore_Wl_Input *input;
646 LOGFN(__FILE__, __LINE__, __FUNCTION__);
648 if (!(input = data)) return;
649 xkb_state_update_mask(input->xkb.state, depressed, latched,
650 locked, 0, 0, group);
654 _ecore_wl_input_cb_keyboard_repeat(void *data, Ecore_Fd_Handler *handler __UNUSED__)
656 Ecore_Wl_Input *input;
657 Ecore_Wl_Window *win = NULL;
658 unsigned long long int xp;
660 LOGFN(__FILE__, __LINE__, __FUNCTION__);
662 if (!(input = data)) return ECORE_CALLBACK_RENEW;
664 /* Trap for EAGAIN */
665 if (read(input->repeat.timerfd, &xp, sizeof(xp)) != sizeof(xp))
666 return ECORE_CALLBACK_RENEW;
668 if ((win = input->keyboard_focus))
669 _ecore_wl_input_cb_keyboard_key(input, NULL, input->display->serial,
671 input->repeat.key, EINA_TRUE);
673 return ECORE_CALLBACK_RENEW;
677 _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)
679 Ecore_Wl_Input *input;
680 Ecore_Wl_Window *win = NULL;
682 LOGFN(__FILE__, __LINE__, __FUNCTION__);
684 if (!surface) return;
685 if (!(input = data)) return;
687 if (!input->timestamp)
691 gettimeofday(&tv, NULL);
692 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
695 input->sx = wl_fixed_to_double(sx);
696 input->sy = wl_fixed_to_double(sy);
697 input->display->serial = serial;
698 input->pointer_enter_serial = serial;
700 /* The cursor on the surface is undefined until we set it */
701 ecore_wl_input_cursor_from_name_set(input, "left_ptr");
703 if ((win = wl_surface_get_user_data(surface)))
705 win->pointer_device = input;
706 input->pointer_focus = win;
708 _ecore_wl_input_mouse_in_send(input, win, input->timestamp);
711 /* NB: This whole 'if' below is a major HACK due to wayland's stupidness
712 * of not sending a mouse_up (or any notification at all for that matter)
713 * when a move or resize grab is finished */
716 /* NB: This COULD mean a move has finished, or it could mean that
717 * a 'drag' is being done to a different surface */
719 if ((input->grab == win) && (win->moving))
721 /* NB: 'Fake' a mouse_up for move finished */
722 win->moving = EINA_FALSE;
723 _ecore_wl_input_mouse_up_send(input, win, input->timestamp);
727 if ((input->grab) && (input->grab_button == BTN_LEFT))
728 ecore_wl_input_ungrab(input);
730 else if ((input->grab == win) && (win->resizing))
732 /* NB: 'Fake' a mouse_up for resize finished */
733 win->resizing = EINA_FALSE;
734 _ecore_wl_input_mouse_up_send(input, win, input->timestamp);
738 if ((input->grab) && (input->grab_button == BTN_LEFT))
739 ecore_wl_input_ungrab(input);
741 /* FIXME: Test d-n-d and potentially add needed case here */
746 _ecore_wl_input_cb_pointer_leave(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, struct wl_surface *surface)
748 Ecore_Wl_Input *input;
749 Ecore_Wl_Window *win;
751 LOGFN(__FILE__, __LINE__, __FUNCTION__);
753 if (!surface) return;
754 if (!(input = data)) return;
756 input->display->serial = serial;
758 if (!surface) return;
759 if (!(win = wl_surface_get_user_data(surface))) return;
761 win->pointer_device = NULL;
762 input->pointer_focus = NULL;
764 /* _ecore_wl_input_mouse_move_send(input, win, input->timestamp); */
765 _ecore_wl_input_mouse_out_send(input, win, input->timestamp);
769 /* move or resize started */
771 /* printf("Pointer Leave WITH a Grab\n"); */
776 _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__)
778 Ecore_Wl_Input *input;
779 Ecore_Wl_Window *win = NULL;
781 LOGFN(__FILE__, __LINE__, __FUNCTION__);
783 if (!surface) return;
784 if (!(input = data)) return;
786 if (!input->timestamp)
790 gettimeofday(&tv, NULL);
791 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
794 input->display->serial = serial;
796 if (!(win = wl_surface_get_user_data(surface))) return;
798 win->keyboard_device = input;
799 input->keyboard_focus = win;
801 _ecore_wl_input_focus_in_send(input, win, input->timestamp);
805 _ecore_wl_input_cb_keyboard_leave(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, struct wl_surface *surface)
807 Ecore_Wl_Input *input;
808 Ecore_Wl_Window *win;
810 LOGFN(__FILE__, __LINE__, __FUNCTION__);
812 if (!surface) return;
813 if (!(input = data)) return;
815 if (input->repeat.timerfd)
817 struct itimerspec ts;
819 ts.it_interval.tv_sec = 0;
820 ts.it_interval.tv_nsec = 0;
821 ts.it_value.tv_sec = 0;
822 ts.it_value.tv_nsec = 0;
824 timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
827 if (!input->timestamp)
831 gettimeofday(&tv, NULL);
832 input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
835 input->display->serial = serial;
837 if (!surface) return;
838 if (!(win = wl_surface_get_user_data(surface))) return;
840 win->keyboard_device = NULL;
841 _ecore_wl_input_focus_out_send(input, win, input->timestamp);
843 input->keyboard_focus = NULL;
847 _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)
849 Ecore_Wl_Input *input;
851 LOGFN(__FILE__, __LINE__, __FUNCTION__);
853 if (!surface) return;
854 if (!(input = data)) return;
856 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
857 * This needs to be tested with an actual touch device */
858 /* input->timestamp = timestamp; */
859 input->display->serial = serial;
860 input->button = BTN_LEFT;
861 input->sx = wl_fixed_to_int(x);
862 input->sy = wl_fixed_to_int(y);
863 _ecore_wl_input_cb_pointer_enter(data, NULL, serial, surface, x, y);
864 _ecore_wl_input_mouse_down_send(input, input->pointer_focus, timestamp);
868 _ecore_wl_input_cb_touch_up(void *data, struct wl_touch *touch __UNUSED__, unsigned int serial, unsigned int timestamp, int id __UNUSED__)
870 Ecore_Wl_Input *input;
872 LOGFN(__FILE__, __LINE__, __FUNCTION__);
874 if (!(input = data)) return;
876 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
877 * This needs to be tested with an actual touch device */
878 /* input->timestamp = timestamp; */
879 input->button = BTN_LEFT;
880 input->display->serial = serial;
881 _ecore_wl_input_mouse_up_send(input, input->pointer_focus, timestamp);
886 _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)
888 Ecore_Wl_Input *input;
890 LOGFN(__FILE__, __LINE__, __FUNCTION__);
892 if (!(input = data)) return;
894 /* FIXME: NB: Not sure yet if input->timestamp should be set here.
895 * This needs to be tested with an actual touch device */
896 /* input->timestamp = timestamp; */
897 input->sx = wl_fixed_to_int(x);
898 input->sy = wl_fixed_to_int(y);
900 _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
904 _ecore_wl_input_cb_touch_frame(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
906 LOGFN(__FILE__, __LINE__, __FUNCTION__);
910 _ecore_wl_input_cb_touch_cancel(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
912 LOGFN(__FILE__, __LINE__, __FUNCTION__);
916 _ecore_wl_input_cb_data_offer(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
918 LOGFN(__FILE__, __LINE__, __FUNCTION__);
920 _ecore_wl_dnd_add(data, data_device, offer);
924 _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)
926 LOGFN(__FILE__, __LINE__, __FUNCTION__);
928 if (!surface) return;
930 _ecore_wl_dnd_enter(data, data_device, timestamp, surface, x, y, offer);
934 _ecore_wl_input_cb_data_leave(void *data, struct wl_data_device *data_device)
936 LOGFN(__FILE__, __LINE__, __FUNCTION__);
938 _ecore_wl_dnd_leave(data, data_device);
942 _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)
944 LOGFN(__FILE__, __LINE__, __FUNCTION__);
946 _ecore_wl_dnd_motion(data, data_device, timestamp, x, y);
950 _ecore_wl_input_cb_data_drop(void *data, struct wl_data_device *data_device)
952 LOGFN(__FILE__, __LINE__, __FUNCTION__);
954 _ecore_wl_dnd_drop(data, data_device);
958 _ecore_wl_input_cb_data_selection(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
960 LOGFN(__FILE__, __LINE__, __FUNCTION__);
962 _ecore_wl_dnd_selection(data, data_device, offer);
966 _ecore_wl_input_mouse_move_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
968 Ecore_Event_Mouse_Move *ev;
970 LOGFN(__FILE__, __LINE__, __FUNCTION__);
972 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Move)))) return;
974 ev->timestamp = timestamp;
977 /* ev->root.x = input->sx; */
978 /* ev->root.y = input->sy; */
979 ev->modifiers = input->modifiers;
980 ev->multi.device = 0;
981 ev->multi.radius = 1;
982 ev->multi.radius_x = 1;
983 ev->multi.radius_y = 1;
984 ev->multi.pressure = 1.0;
985 ev->multi.angle = 0.0;
986 ev->multi.x = input->sx;
987 ev->multi.y = input->sy;
991 ev->window = win->id;
992 ev->event_window = win->id;
995 ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
999 _ecore_wl_input_mouse_in_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1001 Ecore_Wl_Event_Mouse_In *ev;
1003 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1005 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_In)))) return;
1009 /* ev->root.x = input->sx; */
1010 /* ev->root.y = input->sy; */
1011 ev->modifiers = input->modifiers;
1012 ev->timestamp = timestamp;
1016 ev->window = win->id;
1017 ev->event_window = win->id;
1020 ecore_event_add(ECORE_WL_EVENT_MOUSE_IN, ev, NULL, NULL);
1024 _ecore_wl_input_mouse_out_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1026 Ecore_Wl_Event_Mouse_Out *ev;
1028 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1030 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_Out)))) return;
1034 /* ev->root.x = input->sx; */
1035 /* ev->root.y = input->sy; */
1036 ev->modifiers = input->modifiers;
1037 ev->timestamp = timestamp;
1041 ev->window = win->id;
1042 ev->event_window = win->id;
1045 ecore_event_add(ECORE_WL_EVENT_MOUSE_OUT, ev, NULL, NULL);
1049 _ecore_wl_input_focus_in_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1051 Ecore_Wl_Event_Focus_In *ev;
1053 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1055 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Focus_In)))) return;
1056 ev->timestamp = timestamp;
1057 if (win) ev->win = win->id;
1058 ecore_event_add(ECORE_WL_EVENT_FOCUS_IN, ev, NULL, NULL);
1062 _ecore_wl_input_focus_out_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1064 Ecore_Wl_Event_Focus_Out *ev;
1066 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1068 if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Focus_Out)))) return;
1069 ev->timestamp = timestamp;
1070 if (win) ev->win = win->id;
1071 ecore_event_add(ECORE_WL_EVENT_FOCUS_OUT, ev, NULL, NULL);
1075 _ecore_wl_input_mouse_down_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1077 Ecore_Event_Mouse_Button *ev;
1079 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1081 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1083 if (input->button == BTN_LEFT)
1085 else if (input->button == BTN_MIDDLE)
1087 else if (input->button == BTN_RIGHT)
1090 ev->buttons = input->button;
1092 ev->timestamp = timestamp;
1095 /* ev->root.x = input->sx; */
1096 /* ev->root.y = input->sy; */
1097 ev->modifiers = input->modifiers;
1099 /* FIXME: Need to get these from wayland somehow */
1100 ev->double_click = 0;
1101 ev->triple_click = 0;
1103 ev->multi.device = 0;
1104 ev->multi.radius = 1;
1105 ev->multi.radius_x = 1;
1106 ev->multi.radius_y = 1;
1107 ev->multi.pressure = 1.0;
1108 ev->multi.angle = 0.0;
1109 ev->multi.x = input->sx;
1110 ev->multi.y = input->sy;
1114 ev->window = win->id;
1115 ev->event_window = win->id;
1118 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
1122 _ecore_wl_input_mouse_up_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1124 Ecore_Event_Mouse_Button *ev;
1126 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1128 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1130 if (input->button == BTN_LEFT)
1132 else if (input->button == BTN_MIDDLE)
1134 else if (input->button == BTN_RIGHT)
1137 ev->buttons = input->button;
1139 ev->timestamp = timestamp;
1142 /* ev->root.x = input->sx; */
1143 /* ev->root.y = input->sy; */
1144 ev->modifiers = input->modifiers;
1146 /* FIXME: Need to get these from wayland somehow */
1147 ev->double_click = 0;
1148 ev->triple_click = 0;
1150 ev->multi.device = 0;
1151 ev->multi.radius = 1;
1152 ev->multi.radius_x = 1;
1153 ev->multi.radius_y = 1;
1154 ev->multi.pressure = 1.0;
1155 ev->multi.angle = 0.0;
1156 ev->multi.x = input->sx;
1157 ev->multi.y = input->sy;
1161 ev->window = win->id;
1162 ev->event_window = win->id;
1165 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
1169 _ecore_wl_input_mouse_wheel_send(Ecore_Wl_Input *input, unsigned int axis, int value, unsigned int timestamp)
1171 Ecore_Event_Mouse_Wheel *ev;
1173 LOGFN(__FILE__, __LINE__, __FUNCTION__);
1175 if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Wheel)))) return;
1177 ev->timestamp = timestamp;
1178 ev->modifiers = input->modifiers;
1181 /* ev->root.x = input->sx; */
1182 /* ev->root.y = input->sy; */
1184 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
1189 else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
1197 ev->window = input->grab->id;
1198 ev->event_window = input->grab->id;
1200 else if (input->pointer_focus)
1202 ev->window = input->pointer_focus->id;
1203 ev->event_window = input->pointer_focus->id;
1206 ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
1210 _ecore_wl_input_set_selection(Ecore_Wl_Input *input, struct wl_data_source *source)
1212 wl_data_device_set_selection(input->data_device, source, input->display->serial);