Imported Upstream version 1.7.3
[platform/upstream/ecore.git] / src / lib / ecore_wayland / ecore_wl_input.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 /**
6  * NB: Events that receive a 'serial' instead of timestamp
7  * 
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
17  * 
18  **/
19
20 #include "ecore_wl_private.h"
21 #include <sys/mman.h>
22 #include <sys/timerfd.h>
23
24 /* FIXME: This gives BTN_LEFT/RIGHT/MIDDLE for linux systems ... 
25  *        What about other OSs ?? */
26 #ifdef __linux__
27 # include <linux/input.h>
28 #else
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
36 #endif
37
38 #define MOD_SHIFT_MASK 0x01
39 #define MOD_ALT_MASK 0x02
40 #define MOD_CONTROL_MASK 0x04
41
42 Ecore_Wl_Dnd *glb_dnd = NULL;
43
44 /* local function prototypes */
45 static void _ecore_wl_input_seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps);
46
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);
70
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);
79
80 /* static int _ecore_wl_input_keysym_to_string(unsigned int symbol, char *buffer, int len); */
81
82 /* wayland interfaces */
83 static const struct wl_pointer_listener pointer_listener = 
84 {
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,
90 };
91
92 static const struct wl_keyboard_listener keyboard_listener = 
93 {
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,
99 };
100
101 static const struct wl_touch_listener touch_listener = 
102 {
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
108 };
109
110 static const struct wl_seat_listener _ecore_wl_seat_listener = 
111 {
112    _ecore_wl_input_seat_handle_capabilities,
113 };
114
115 static const struct wl_data_device_listener _ecore_wl_data_listener = 
116 {
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
123 };
124
125 static const struct wl_callback_listener _ecore_wl_pointer_surface_listener = 
126 {
127    _ecore_wl_input_cb_pointer_frame
128 };
129
130 /* local variables */
131 static int _pointer_x, _pointer_y;
132
133 EAPI void 
134 ecore_wl_input_grab(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int button)
135 {
136    LOGFN(__FILE__, __LINE__, __FUNCTION__);
137
138    if (!input) return;
139    input->grab = win;
140    input->grab_button = button;
141 }
142
143 EAPI void 
144 ecore_wl_input_ungrab(Ecore_Wl_Input *input)
145 {
146    LOGFN(__FILE__, __LINE__, __FUNCTION__);
147
148    if (!input) return;
149    input->grab = NULL;
150    input->grab_button = 0;
151 }
152
153 EAPI void
154 ecore_wl_input_pointer_set(Ecore_Wl_Input *input, struct wl_surface *surface, int hot_x, int hot_y)
155 {
156    LOGFN(__FILE__, __LINE__, __FUNCTION__);
157
158    if (input)
159      wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial, 
160                            surface, hot_x, hot_y);
161 }
162
163 EAPI void
164 ecore_wl_input_cursor_from_name_set(Ecore_Wl_Input *input, const char *cursor_name)
165 {
166    struct wl_cursor_image *cursor_image;
167    struct wl_buffer *buffer;
168    struct wl_cursor *cursor;
169
170    LOGFN(__FILE__, __LINE__, __FUNCTION__);
171
172    if (!input) return;
173
174    eina_stringshare_replace(&input->cursor_name, cursor_name);
175
176    /* No cursor. Set to default Left Pointer */
177    if (!cursor_name) 
178      eina_stringshare_replace(&input->cursor_name, "left_ptr");
179
180    /* try to get this cursor from the theme */
181    if (!(cursor = ecore_wl_cursor_get(input->cursor_name)))
182      {
183         /* if the theme does not have this cursor, default to left pointer */
184         if (!(cursor = ecore_wl_cursor_get("left_ptr")))
185           return;
186      }
187
188    if ((!cursor->images) || (!cursor->images[0]))
189      {
190         ecore_wl_input_pointer_set(input, NULL, 0, 0);
191         return;
192      }
193
194    cursor_image = cursor->images[0];
195    if ((buffer = wl_cursor_image_get_buffer(cursor_image)))
196      {
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);
204
205         if (!input->cursor_frame_cb)
206           _ecore_wl_input_cb_pointer_frame(input, NULL, 0);
207      }
208 }
209
210 EAPI void
211 ecore_wl_input_cursor_default_restore(Ecore_Wl_Input *input)
212 {
213    LOGFN(__FILE__, __LINE__, __FUNCTION__);
214
215    if (!input) return;
216
217    /* Restore to default wayland cursor */
218    ecore_wl_input_cursor_from_name_set(input, "left_ptr");
219 }
220
221 /* local functions */
222 void 
223 _ecore_wl_input_add(Ecore_Wl_Display *ewd, unsigned int id)
224 {
225    Ecore_Wl_Input *input;
226
227    LOGFN(__FILE__, __LINE__, __FUNCTION__);
228
229    if (!(input = malloc(sizeof(Ecore_Wl_Input)))) return;
230
231    memset(input, 0, sizeof(Ecore_Wl_Input));
232
233    input->display = ewd;
234    input->pointer_focus = NULL;
235    input->keyboard_focus = NULL;
236
237    input->seat = 
238      wl_registry_bind(ewd->wl.registry, id, &wl_seat_interface, 1);
239    wl_list_insert(ewd->inputs.prev, &input->link);
240
241    wl_seat_add_listener(input->seat, 
242                         &_ecore_wl_seat_listener, input);
243    wl_seat_set_user_data(input->seat, input);
244
245    input->data_device = 
246      wl_data_device_manager_get_data_device(ewd->wl.data_device_manager, 
247                                             input->seat);
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);
252
253    input->repeat.timerfd = 
254      timerfd_create(CLOCK_MONOTONIC, (TFD_CLOEXEC | TFD_NONBLOCK));
255
256    input->repeat.hdlr = 
257      ecore_main_fd_handler_add(input->repeat.timerfd, ECORE_FD_READ, 
258                                _ecore_wl_input_cb_keyboard_repeat, input, 
259                                NULL, NULL);
260
261    ewd->input = input;
262
263    /* create Ecore_Wl_Dnd */
264    if (!glb_dnd)
265      if (!(glb_dnd = calloc(1, sizeof(Ecore_Wl_Dnd)))) return;
266    glb_dnd->ewd = ewd;
267    glb_dnd->input = input;
268    input->dnd = glb_dnd;
269    wl_array_init(&glb_dnd->types_offered);
270 }
271
272 void 
273 _ecore_wl_input_del(Ecore_Wl_Input *input)
274 {
275    if (!input) return;
276
277    if (input->cursor_name) eina_stringshare_del(input->cursor_name);
278    input->cursor_name = NULL;
279
280    if (input->keyboard_focus)
281      {
282         Ecore_Wl_Window *win = NULL;
283
284         if ((win = input->keyboard_focus))
285           win->keyboard_device = NULL;
286
287         input->keyboard_focus = NULL;
288      }
289
290    if (input->drag_source) _ecore_wl_dnd_del(input->drag_source);
291    input->drag_source = NULL;
292
293    if (input->selection_source) _ecore_wl_dnd_del(input->selection_source);
294    input->selection_source = NULL;
295
296    if (input->data_device) wl_data_device_destroy(input->data_device);
297
298    if (input->xkb.state)
299      xkb_state_unref(input->xkb.state);
300    if (input->xkb.keymap)
301      xkb_map_unref(input->xkb.keymap);
302
303    if (input->cursor_surface)
304      wl_surface_destroy(input->cursor_surface);
305
306    wl_list_remove(&input->link);
307    if (input->seat) wl_seat_destroy(input->seat);
308
309    if (input->repeat.hdlr) ecore_main_fd_handler_del(input->repeat.hdlr);
310    input->repeat.hdlr = NULL;
311
312    if (input->repeat.timerfd) close(input->repeat.timerfd);
313    input->repeat.timerfd = 0;
314
315    free(input);
316 }
317
318 void 
319 _ecore_wl_input_pointer_xy_get(int *x, int *y)
320 {
321    if (x) *x = _pointer_x;
322    if (y) *y = _pointer_y;
323 }
324
325 static void 
326 _ecore_wl_input_seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps)
327 {
328    Ecore_Wl_Input *input;
329
330    if (!(input = data)) return;
331
332    if ((caps & WL_SEAT_CAPABILITY_POINTER) && (!input->pointer))
333      {
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);
337      }
338    else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && (input->pointer))
339      {
340         wl_pointer_destroy(input->pointer);
341         input->pointer = NULL;
342      }
343
344    if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && (!input->keyboard))
345      {
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);
349      }
350    else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && (input->keyboard))
351      {
352         wl_keyboard_destroy(input->keyboard);
353         input->keyboard = NULL;
354      }
355
356    if ((caps & WL_SEAT_CAPABILITY_TOUCH) && (!input->touch))
357      {
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);
361      }
362    else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && (input->touch))
363      {
364         wl_touch_destroy(input->touch);
365         input->touch = NULL;
366      }
367 }
368
369
370 static void 
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)
372 {
373    Ecore_Wl_Input *input;
374
375    /* LOGFN(__FILE__, __LINE__, __FUNCTION__); */
376
377    if (!(input = data)) return;
378
379    _pointer_x = input->sx = wl_fixed_to_int(sx);
380    _pointer_y = input->sy = wl_fixed_to_int(sy);
381
382    input->timestamp = timestamp;
383
384    if (input->pointer_focus)
385      _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
386
387    ecore_wl_input_cursor_from_name_set(input, input->cursor_name);
388 }
389
390 static void 
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)
392 {
393    Ecore_Wl_Input *input;
394
395    LOGFN(__FILE__, __LINE__, __FUNCTION__);
396
397    if (!(input = data)) return;
398
399    input->timestamp = timestamp;
400    input->display->serial = serial;
401
402 //   _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
403
404    if (state)
405      {
406         if ((input->pointer_focus) && (!input->grab) && (state))
407           ecore_wl_input_grab(input, input->pointer_focus, button);
408
409         input->button = button;
410         _ecore_wl_input_mouse_down_send(input, input->pointer_focus, 
411                                         timestamp);
412      }
413    else
414      {
415         _ecore_wl_input_mouse_up_send(input, input->pointer_focus, 
416                                       timestamp);
417         input->button = 0;
418
419         if ((input->grab) && (input->grab_button == button) && (!state))
420           ecore_wl_input_ungrab(input);
421      }
422
423 //   _ecore_wl_input_mouse_move_send(input, timestamp);
424 }
425
426 static void 
427 _ecore_wl_input_cb_pointer_axis(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int timestamp, unsigned int axis, wl_fixed_t value)
428 {
429    Ecore_Wl_Input *input;
430
431    LOGFN(__FILE__, __LINE__, __FUNCTION__);
432
433    if (!(input = data)) return;
434    _ecore_wl_input_mouse_wheel_send(input, axis, wl_fixed_to_int(value), 
435                                     timestamp);
436 }
437
438 static void 
439 _ecore_wl_input_cb_pointer_frame(void *data, struct wl_callback *callback, unsigned int timestamp __UNUSED__)
440 {
441    Ecore_Wl_Input *input;
442
443    LOGFN(__FILE__, __LINE__, __FUNCTION__);
444
445    if (!(input = data)) return;
446
447    if (callback)
448      {
449         if (callback != input->cursor_frame_cb) return;
450         wl_callback_destroy(callback);
451         input->cursor_frame_cb = NULL;
452      }
453
454    if (!input->cursor_name)
455      {
456         ecore_wl_input_pointer_set(input, NULL, 0, 0);
457         return;
458      }
459
460    if (!input->cursor_frame_cb)
461      {
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);
465      }
466 }
467
468 static void 
469 _ecore_wl_input_cb_keyboard_keymap(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int format, int fd, unsigned int size)
470 {
471    Ecore_Wl_Input *input;
472    char *map = NULL;
473
474    LOGFN(__FILE__, __LINE__, __FUNCTION__);
475
476    if (!(input = data))
477      {
478         close(fd);
479         return;
480      }
481
482    if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
483      {
484         close(fd);
485         return;
486      }
487
488    map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
489    if (map == MAP_FAILED)
490      {
491         close(fd);
492         return;
493      }
494
495    input->xkb.keymap = 
496      xkb_map_new_from_string(input->display->xkb.context, map, 
497                              XKB_KEYMAP_FORMAT_TEXT_V1, 0);
498
499    munmap(map, size);
500    close(fd);
501
502    if (!(input->xkb.keymap)) return;
503    if (!(input->xkb.state = xkb_state_new(input->xkb.keymap)))
504      {
505         xkb_map_unref(input->xkb.keymap);
506         input->xkb.keymap = NULL;
507         return;
508      }
509
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");
516 }
517
518 static void 
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)
520 {
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;
526    xkb_mod_mask_t mask;
527    char string[32], key[32], keyname[32];// compose[32];
528    Ecore_Event_Key *e;
529    struct itimerspec ts;
530    int len = 0;
531
532    LOGFN(__FILE__, __LINE__, __FUNCTION__);
533
534    if (!(input = data)) return;
535    input->display->serial = serial;
536
537    /* xkb rules reflect X broken keycodes, so offset by 8 */
538    code = keycode + 8;
539
540    win = input->keyboard_focus;
541    if ((!win) || (win->keyboard_device != input) || (!input->xkb.state)) 
542      return;
543
544    mask = xkb_state_serialize_mods(input->xkb.state, 
545                                    XKB_STATE_DEPRESSED | XKB_STATE_LATCHED);
546
547    input->modifiers = 0;
548
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;
556
557    num = xkb_key_get_syms(input->xkb.state, code, &syms);
558    if (num == 1) sym = syms[0];
559
560    memset(key, 0, sizeof(key));
561    xkb_keysym_get_name(sym, key, sizeof(key));
562
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);
567
568    memset(string, 0, sizeof(string));
569    if (xkb_keysym_to_utf8(sym, string, 32) <= 0)
570      {
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';
575      }
576
577    /* FIXME: NB: Start hacking on compose key support */
578    /* memset(compose, 0, sizeof(compose)); */
579    /* if (sym == XKB_KEY_Multi_key) */
580    /*   { */
581    /*      if (xkb_keysym_to_utf8(sym, compose, 32) <= 0) */
582    /*        compose[0] = '\0'; */
583    /*   } */
584
585    e = malloc(sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
586               ((string[0] != '\0') ? strlen(string) : 0) + 3);
587    if (!e) return;
588
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;
593
594    strcpy((char *)e->keyname, keyname);
595    strcpy((char *)e->key, key);
596    if (strlen(string)) strcpy((char *)e->string, string);
597
598    e->window = win->id;
599    e->event_window = win->id;
600    e->timestamp = timestamp;
601    e->modifiers = input->modifiers;
602
603    if (state)
604      ecore_event_add(ECORE_EVENT_KEY_DOWN, e, NULL, NULL);
605    else
606      ecore_event_add(ECORE_EVENT_KEY_UP, e, NULL, NULL);
607
608    if ((!state) && (keycode == input->repeat.key))
609      {
610         input->repeat.sym = 0;
611         input->repeat.key = 0;
612         input->repeat.time = 0;
613
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;
618
619         timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
620      }
621    else if ((state) && 
622             ((!input->repeat.key) || 
623                 ((keycode) && (keycode != input->repeat.key))))
624      {
625         input->repeat.sym = sym;
626         input->repeat.key = keycode;
627         input->repeat.time = timestamp;
628
629         /* interval after expires */
630         ts.it_interval.tv_sec = 0;
631         ts.it_interval.tv_nsec = 35 * 1000 * 1000;
632
633         /* initial expiration */
634         ts.it_value.tv_sec = 0;
635         ts.it_value.tv_nsec = 500 * 1000 * 1000;
636
637         timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
638      }
639 }
640
641 static void 
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)
643 {
644    Ecore_Wl_Input *input;
645
646    LOGFN(__FILE__, __LINE__, __FUNCTION__);
647
648    if (!(input = data)) return;
649    xkb_state_update_mask(input->xkb.state, depressed, latched, 
650                          locked, 0, 0, group);
651 }
652
653 static Eina_Bool 
654 _ecore_wl_input_cb_keyboard_repeat(void *data, Ecore_Fd_Handler *handler __UNUSED__)
655 {
656    Ecore_Wl_Input *input;
657    Ecore_Wl_Window *win = NULL;
658    unsigned long long int xp;
659
660    LOGFN(__FILE__, __LINE__, __FUNCTION__);
661
662    if (!(input = data)) return ECORE_CALLBACK_RENEW;
663
664    /* Trap for EAGAIN */
665    if (read(input->repeat.timerfd, &xp, sizeof(xp)) != sizeof(xp))
666      return ECORE_CALLBACK_RENEW;
667
668    if ((win = input->keyboard_focus))
669      _ecore_wl_input_cb_keyboard_key(input, NULL, input->display->serial, 
670                                      input->repeat.time, 
671                                      input->repeat.key, EINA_TRUE);
672
673    return ECORE_CALLBACK_RENEW;
674 }
675
676 static void 
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)
678 {
679    Ecore_Wl_Input *input;
680    Ecore_Wl_Window *win = NULL;
681
682    LOGFN(__FILE__, __LINE__, __FUNCTION__);
683
684    if (!surface) return;
685    if (!(input = data)) return;
686
687    if (!input->timestamp)
688      {
689         struct timeval tv;
690
691         gettimeofday(&tv, NULL);
692         input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
693      }
694
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;
699
700    /* The cursor on the surface is undefined until we set it */
701    ecore_wl_input_cursor_from_name_set(input, "left_ptr");
702
703    if ((win = wl_surface_get_user_data(surface)))
704      {
705         win->pointer_device = input;
706         input->pointer_focus = win;
707
708         _ecore_wl_input_mouse_in_send(input, win, input->timestamp);
709      }
710
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 */
714    if (input->grab)
715      {
716         /* NB: This COULD mean a move has finished, or it could mean that 
717          * a 'drag' is being done to a different surface */
718
719         if ((input->grab == win) && (win->moving))
720           {
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);
724
725              input->button = 0;
726
727              if ((input->grab) && (input->grab_button == BTN_LEFT))
728                ecore_wl_input_ungrab(input);
729           }
730         else if ((input->grab == win) && (win->resizing))
731           {
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);
735
736              input->button = 0;
737
738              if ((input->grab) && (input->grab_button == BTN_LEFT))
739                ecore_wl_input_ungrab(input);
740           }
741         /* FIXME: Test d-n-d and potentially add needed case here */
742      }
743 }
744
745 static void 
746 _ecore_wl_input_cb_pointer_leave(void *data, struct wl_pointer *pointer __UNUSED__, unsigned int serial, struct wl_surface *surface)
747 {
748    Ecore_Wl_Input *input;
749    Ecore_Wl_Window *win;
750
751    LOGFN(__FILE__, __LINE__, __FUNCTION__);
752
753    if (!surface) return;
754    if (!(input = data)) return;
755
756    input->display->serial = serial;
757
758    if (!surface) return;
759    if (!(win = wl_surface_get_user_data(surface))) return;
760
761    win->pointer_device = NULL;
762    input->pointer_focus = NULL;
763
764    /* _ecore_wl_input_mouse_move_send(input, win, input->timestamp); */
765    _ecore_wl_input_mouse_out_send(input, win, input->timestamp);
766
767    if (input->grab)
768      {
769         /* move or resize started */
770
771         /* printf("Pointer Leave WITH a Grab\n"); */
772      }
773 }
774
775 static void 
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__)
777 {
778    Ecore_Wl_Input *input;
779    Ecore_Wl_Window *win = NULL;
780
781    LOGFN(__FILE__, __LINE__, __FUNCTION__);
782
783    if (!surface) return;
784    if (!(input = data)) return;
785
786    if (!input->timestamp)
787      {
788         struct timeval tv;
789
790         gettimeofday(&tv, NULL);
791         input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
792      }
793
794    input->display->serial = serial;
795
796    if (!(win = wl_surface_get_user_data(surface))) return;
797
798    win->keyboard_device = input;
799    input->keyboard_focus = win;
800
801    _ecore_wl_input_focus_in_send(input, win, input->timestamp);
802 }
803
804 static void 
805 _ecore_wl_input_cb_keyboard_leave(void *data, struct wl_keyboard *keyboard __UNUSED__, unsigned int serial, struct wl_surface *surface)
806 {
807    Ecore_Wl_Input *input;
808    Ecore_Wl_Window *win;
809
810    LOGFN(__FILE__, __LINE__, __FUNCTION__);
811
812    if (!surface) return;
813    if (!(input = data)) return;
814
815    if (input->repeat.timerfd)
816      {
817         struct itimerspec ts;
818         
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;
823
824         timerfd_settime(input->repeat.timerfd, 0, &ts, NULL);
825      }
826
827    if (!input->timestamp)
828      {
829         struct timeval tv;
830
831         gettimeofday(&tv, NULL);
832         input->timestamp = (tv.tv_sec * 1000 + tv.tv_usec / 1000);
833      }
834
835    input->display->serial = serial;
836
837    if (!surface) return;
838    if (!(win = wl_surface_get_user_data(surface))) return;
839
840    win->keyboard_device = NULL;
841    _ecore_wl_input_focus_out_send(input, win, input->timestamp);
842
843    input->keyboard_focus = NULL;
844 }
845
846 static void 
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)
848 {
849    Ecore_Wl_Input *input;
850
851    LOGFN(__FILE__, __LINE__, __FUNCTION__);
852
853    if (!surface) return;
854    if (!(input = data)) return;
855
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);
865 }
866
867 static void 
868 _ecore_wl_input_cb_touch_up(void *data, struct wl_touch *touch __UNUSED__, unsigned int serial, unsigned int timestamp, int id __UNUSED__)
869 {
870    Ecore_Wl_Input *input;
871
872    LOGFN(__FILE__, __LINE__, __FUNCTION__);
873
874    if (!(input = data)) return;
875
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);
882    input->button = 0;
883 }
884
885 static void 
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)
887 {
888    Ecore_Wl_Input *input;
889
890    LOGFN(__FILE__, __LINE__, __FUNCTION__);
891
892    if (!(input = data)) return;
893
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);
899
900    _ecore_wl_input_mouse_move_send(input, input->pointer_focus, timestamp);
901 }
902
903 static void 
904 _ecore_wl_input_cb_touch_frame(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
905 {
906    LOGFN(__FILE__, __LINE__, __FUNCTION__);
907 }
908
909 static void 
910 _ecore_wl_input_cb_touch_cancel(void *data __UNUSED__, struct wl_touch *touch __UNUSED__)
911 {
912    LOGFN(__FILE__, __LINE__, __FUNCTION__);
913 }
914
915 static void 
916 _ecore_wl_input_cb_data_offer(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
917 {
918    LOGFN(__FILE__, __LINE__, __FUNCTION__);
919
920    _ecore_wl_dnd_add(data, data_device, offer);
921 }
922
923 static void 
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)
925 {
926    LOGFN(__FILE__, __LINE__, __FUNCTION__);
927
928    if (!surface) return;
929
930    _ecore_wl_dnd_enter(data, data_device, timestamp, surface, x, y, offer);
931 }
932
933 static void 
934 _ecore_wl_input_cb_data_leave(void *data, struct wl_data_device *data_device)
935 {
936    LOGFN(__FILE__, __LINE__, __FUNCTION__);
937
938    _ecore_wl_dnd_leave(data, data_device);
939 }
940
941 static void 
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)
943 {
944    LOGFN(__FILE__, __LINE__, __FUNCTION__);
945
946    _ecore_wl_dnd_motion(data, data_device, timestamp, x, y);
947 }
948
949 static void 
950 _ecore_wl_input_cb_data_drop(void *data, struct wl_data_device *data_device)
951 {
952    LOGFN(__FILE__, __LINE__, __FUNCTION__);
953
954    _ecore_wl_dnd_drop(data, data_device);
955 }
956
957 static void 
958 _ecore_wl_input_cb_data_selection(void *data, struct wl_data_device *data_device, struct wl_data_offer *offer)
959 {
960    LOGFN(__FILE__, __LINE__, __FUNCTION__);
961
962    _ecore_wl_dnd_selection(data, data_device, offer);
963 }
964
965 static void 
966 _ecore_wl_input_mouse_move_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
967 {
968    Ecore_Event_Mouse_Move *ev;
969
970    LOGFN(__FILE__, __LINE__, __FUNCTION__);
971
972    if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Move)))) return;
973
974    ev->timestamp = timestamp;
975    ev->x = input->sx;
976    ev->y = input->sy;
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;
988
989    if (win)
990      {
991         ev->window = win->id;
992         ev->event_window = win->id;
993      }
994
995    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
996 }
997
998 static void 
999 _ecore_wl_input_mouse_in_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1000 {
1001    Ecore_Wl_Event_Mouse_In *ev;
1002
1003    LOGFN(__FILE__, __LINE__, __FUNCTION__);
1004
1005    if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_In)))) return;
1006
1007    ev->x = input->sx;
1008    ev->y = input->sy;
1009    /* ev->root.x = input->sx; */
1010    /* ev->root.y = input->sy; */
1011    ev->modifiers = input->modifiers;
1012    ev->timestamp = timestamp;
1013
1014    if (win)
1015      {
1016         ev->window = win->id;
1017         ev->event_window = win->id;
1018      }
1019
1020    ecore_event_add(ECORE_WL_EVENT_MOUSE_IN, ev, NULL, NULL);
1021 }
1022
1023 static void 
1024 _ecore_wl_input_mouse_out_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1025 {
1026    Ecore_Wl_Event_Mouse_Out *ev;
1027
1028    LOGFN(__FILE__, __LINE__, __FUNCTION__);
1029
1030    if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Mouse_Out)))) return;
1031
1032    ev->x = input->sx;
1033    ev->y = input->sy;
1034    /* ev->root.x = input->sx; */
1035    /* ev->root.y = input->sy; */
1036    ev->modifiers = input->modifiers;
1037    ev->timestamp = timestamp;
1038
1039    if (win)
1040      {
1041         ev->window = win->id;
1042         ev->event_window = win->id;
1043      }
1044
1045    ecore_event_add(ECORE_WL_EVENT_MOUSE_OUT, ev, NULL, NULL);
1046 }
1047
1048 static void 
1049 _ecore_wl_input_focus_in_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1050 {
1051    Ecore_Wl_Event_Focus_In *ev;
1052
1053    LOGFN(__FILE__, __LINE__, __FUNCTION__);
1054
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);
1059 }
1060
1061 static void 
1062 _ecore_wl_input_focus_out_send(Ecore_Wl_Input *input __UNUSED__, Ecore_Wl_Window *win, unsigned int timestamp)
1063 {
1064    Ecore_Wl_Event_Focus_Out *ev;
1065
1066    LOGFN(__FILE__, __LINE__, __FUNCTION__);
1067
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);
1072 }
1073
1074 static void 
1075 _ecore_wl_input_mouse_down_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1076 {
1077    Ecore_Event_Mouse_Button *ev;
1078
1079    LOGFN(__FILE__, __LINE__, __FUNCTION__);
1080
1081    if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1082
1083    if (input->button == BTN_LEFT)
1084      ev->buttons = 1;
1085    else if (input->button == BTN_MIDDLE)
1086      ev->buttons = 2;
1087    else if (input->button == BTN_RIGHT)
1088      ev->buttons = 3;
1089    else
1090      ev->buttons = input->button;
1091
1092    ev->timestamp = timestamp;
1093    ev->x = input->sx;
1094    ev->y = input->sy;
1095    /* ev->root.x = input->sx; */
1096    /* ev->root.y = input->sy; */
1097    ev->modifiers = input->modifiers;
1098
1099    /* FIXME: Need to get these from wayland somehow */
1100    ev->double_click = 0;
1101    ev->triple_click = 0;
1102
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;
1111
1112    if (win)
1113      {
1114         ev->window = win->id;
1115         ev->event_window = win->id;
1116      }
1117
1118    ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
1119 }
1120
1121 static void 
1122 _ecore_wl_input_mouse_up_send(Ecore_Wl_Input *input, Ecore_Wl_Window *win, unsigned int timestamp)
1123 {
1124    Ecore_Event_Mouse_Button *ev;
1125
1126    LOGFN(__FILE__, __LINE__, __FUNCTION__);
1127
1128    if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Button)))) return;
1129
1130    if (input->button == BTN_LEFT)
1131      ev->buttons = 1;
1132    else if (input->button == BTN_MIDDLE)
1133      ev->buttons = 2;
1134    else if (input->button == BTN_RIGHT)
1135      ev->buttons = 3;
1136    else
1137      ev->buttons = input->button;
1138
1139    ev->timestamp = timestamp;
1140    ev->x = input->sx;
1141    ev->y = input->sy;
1142    /* ev->root.x = input->sx; */
1143    /* ev->root.y = input->sy; */
1144    ev->modifiers = input->modifiers;
1145
1146    /* FIXME: Need to get these from wayland somehow */
1147    ev->double_click = 0;
1148    ev->triple_click = 0;
1149
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;
1158
1159    if (win)
1160      {
1161         ev->window = win->id;
1162         ev->event_window = win->id;
1163      }
1164
1165    ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
1166 }
1167
1168 static void 
1169 _ecore_wl_input_mouse_wheel_send(Ecore_Wl_Input *input, unsigned int axis, int value, unsigned int timestamp)
1170 {
1171    Ecore_Event_Mouse_Wheel *ev;
1172
1173    LOGFN(__FILE__, __LINE__, __FUNCTION__);
1174
1175    if (!(ev = malloc(sizeof(Ecore_Event_Mouse_Wheel)))) return;
1176
1177    ev->timestamp = timestamp;
1178    ev->modifiers = input->modifiers;
1179    ev->x = input->sx;
1180    ev->y = input->sy;
1181    /* ev->root.x = input->sx; */
1182    /* ev->root.y = input->sy; */
1183
1184    if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
1185      {
1186         ev->direction = 0;
1187         ev->z = value;
1188      }
1189    else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
1190      {
1191         ev->direction = 1;
1192         ev->z = value;
1193      }
1194
1195    if (input->grab)
1196      {
1197         ev->window = input->grab->id;
1198         ev->event_window = input->grab->id;
1199      }
1200    else if (input->pointer_focus)
1201      {
1202         ev->window = input->pointer_focus->id;
1203         ev->event_window = input->pointer_focus->id;
1204      }
1205
1206    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
1207 }
1208
1209 void
1210 _ecore_wl_input_set_selection(Ecore_Wl_Input *input, struct wl_data_source *source)
1211 {
1212    wl_data_device_set_selection(input->data_device, source, input->display->serial);
1213 }