e_devicemgr: enhance keyboard_grab by sending device info when sending key event
[platform/upstream/enlightenment.git] / src / bin / e_input_evdev.c
1 #include "e.h"
2 #include "e_input_private.h"
3 #include "e_device.h"
4 #include "e_keyrouter_private.h"
5 #include "e_input_event.h"
6 #include "e_devicemgr.h"
7
8 #include <glib.h>
9
10 #define G_LIST_GET_DATA(list) ((list) ? (((GList *)(list))->data) : NULL)
11
12 static void  _device_modifiers_update(E_Input_Evdev *edev);
13 static void  _device_configured_size_get(E_Input_Evdev *edev, int *x, int *y, int *w, int *h);
14 static void  _device_output_assign(E_Input_Evdev *edev, E_Input_Seat_Capabilities cap);
15
16 void
17 _device_calibration_set(E_Input_Evdev *edev)
18 {
19    E_Output *output;
20    int w = 0, h = 0;
21    int temp;
22    E_Comp_Config *comp_conf;
23
24    if (e_comp->e_comp_screen->num_outputs > 1) //multiple outputs
25      {
26         comp_conf = e_comp_config_get();
27         if (comp_conf && (comp_conf->input_output_assign_policy == 1)) //use output-assignment policy
28           {
29              _device_output_assign(edev, E_INPUT_SEAT_POINTER);
30              _device_output_assign(edev, E_INPUT_SEAT_TOUCH);
31           }
32      }
33
34    output = e_comp_screen_primary_output_get(e_comp->e_comp_screen);
35    e_output_size_get(output, &w, &h);
36
37    if (output)
38      {
39         edev->mouse.minx = edev->mouse.miny = 0;
40         edev->mouse.maxw = w;
41         edev->mouse.maxh = h;
42
43         if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
44           {
45              edev->seat->ptr.dx = (double)(w / 2);
46              edev->seat->ptr.dy = (double)(h / 2);
47              edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
48              edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
49              edev->mouse.dx = edev->seat->ptr.dx;
50              edev->mouse.dy = edev->seat->ptr.dy;
51
52              if (output->config.rotation == 90 || output->config.rotation == 270)
53                {
54                   temp = edev->mouse.minx;
55                   edev->mouse.minx = edev->mouse.miny;
56                   edev->mouse.miny = temp;
57
58                   temp = edev->mouse.maxw;
59                   edev->mouse.maxw = edev->mouse.maxh;
60                   edev->mouse.maxh = temp;
61                }
62           }
63      }
64
65 //LCOV_EXCL_START
66 #ifdef _F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
67    const char *sysname;
68    float cal[6];
69    const char *device;
70    Eina_List *devices;
71
72    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
73        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
74      return;
75
76    sysname = libinput_device_get_sysname(edev->device);
77
78    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
79    if (eina_list_count(devices) < 1) return;
80
81 #ifdef _F_E_INPUT_USE_WL_CALIBRATION_
82    const char *vals;
83    enum libinput_config_status status;
84
85    EINA_LIST_FREE(devices, device)
86      {
87         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
88         if ((!vals) ||
89             (sscanf(vals, "%f %f %f %f %f %f",
90                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
91           goto cont;
92
93         cal[2] /= w;
94         cal[5] /= h;
95
96         status =
97           libinput_device_config_calibration_set_matrix(edev->device, cal);
98
99         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
100           ERR("Failed to apply calibration");
101
102 cont:
103         eina_stringshare_del(device);
104         continue;
105      }
106 #endif//_F_E_INPUT_USE_WL_CALIBRATION_
107 #endif//_F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
108 //LCOV_EXCL_STOP
109 }
110
111 static void
112 _device_output_assign(E_Input_Evdev *edev, E_Input_Seat_Capabilities cap)
113 {
114    int last_output_idx;
115    E_Input_Evdev *ed;
116    Eina_List *l;
117    Eina_Bool need_assign_output = EINA_FALSE;
118    const char *output_name;
119    E_Output *output;
120
121    if (!(edev->caps & cap)) return;
122    if (edev->output_name) return; //already assigned
123    if (e_comp->e_comp_screen->num_outputs <= 1) return;
124
125    last_output_idx = e_comp->e_comp_screen->num_outputs - 1;
126    EINA_LIST_FOREACH(edev->seat->devices, l, ed)
127      {
128         if (!(ed->caps & cap)) continue;
129         if (ed == edev) continue;
130         if (ed->output_name)
131           {
132              need_assign_output = EINA_FALSE;
133              break;
134           }
135           need_assign_output = EINA_TRUE;
136      }
137    if (need_assign_output)
138      {
139         output = e_output_find_by_index(last_output_idx);
140         if (!output || !output->info.connected) return;
141         output_name = e_output_output_id_get(output);
142         if (output_name) edev->output_name = eina_stringshare_add(output_name);
143         ELOGF("E_INPUT_EVDEV", "Device is assigned to output:%s", NULL, output_name);
144      }
145 }
146
147 static void
148 _device_touch_count_update(E_Input_Evdev *edev)
149 {
150    unsigned int device_touch_count = 0;
151    static unsigned int _max_device_touch_count = 0;
152
153    E_Input *ei = e_input_get();
154    EINA_SAFETY_ON_NULL_RETURN(ei);
155
156    device_touch_count = libinput_device_touch_get_touch_count(edev->device);
157
158    if (_max_device_touch_count < device_touch_count)
159      _max_device_touch_count = device_touch_count;
160
161    if (e_config->configured_max_touch.use)
162      {
163         if (_max_device_touch_count > 0 && e_config->configured_max_touch.count > _max_device_touch_count)
164           {
165              ELOGF("E_INPUT_EVDEV_TOUCH", "Touch max count(%d) must be less or equal to device's touch count(%d) !\nTouch max count has been shrinken to %d.\n",
166                          NULL, e_config->configured_max_touch.count, _max_device_touch_count, _max_device_touch_count);
167              ei->touch_max_count = _max_device_touch_count;
168           }
169         else
170           {
171              if (ei->touch_max_count != e_config->configured_max_touch.count)
172              {
173                 ELOGF("E_INPUT_EVDEV_TOUCH", "Touch_max_count has been updated to %d -> %d.\n",
174                            NULL, ei->touch_max_count, e_config->configured_max_touch.count);
175                 ei->touch_max_count = e_config->configured_max_touch.count;
176              }
177           }
178      }
179    else
180      {
181         if (ei->touch_max_count < _max_device_touch_count)
182           {
183              ELOGF("E_INPUT_EVDEV_TOUCH", "Touch_max_count has been updated to %d -> %d.\n", NULL,
184                    ei->touch_max_count, _max_device_touch_count);
185              ei->touch_max_count = _max_device_touch_count;
186           }
187      }
188
189    ei->touch_device_count++;
190    ELOGF("E_INPUT_EVDEV_TOUCH", "Touch device count is %d.\n", NULL, ei->touch_device_count);
191 }
192
193 static void
194 _device_configure(E_Input_Evdev *edev)
195 {
196    if (libinput_device_config_tap_get_finger_count(edev->device) > 0)
197      {
198         Eina_Bool tap = EINA_FALSE;
199
200         tap = libinput_device_config_tap_get_default_enabled(edev->device);
201         libinput_device_config_tap_set_enabled(edev->device, tap);
202      }
203
204    _device_calibration_set(edev);
205 }
206
207 static void
208 _device_keyboard_setup(E_Input_Evdev *edev)
209 {
210    E_Input_Backend *input;
211    xkb_mod_index_t xkb_idx;
212
213    if ((!edev) || (!edev->seat)) return;
214    if (!(input = edev->seat->input)) return;
215    if (!input->dev->xkb_ctx) return;
216
217    g_mutex_init(&edev->xkb.keymap_mutex);
218
219    /* create keymap from xkb context */
220    g_mutex_lock(&edev->xkb.keymap_mutex);
221    edev->xkb.keymap = _e_input_device_cached_keymap_get(input->dev->xkb_ctx, NULL, 0);
222    if (!edev->xkb.keymap)
223      {
224         g_mutex_unlock(&edev->xkb.keymap_mutex);
225         ERR("Failed to create keymap: %m");
226         return;
227      }
228
229    g_mutex_lock(&edev->xkb.state_mutex);
230
231    /* create xkb state */
232    if (!(edev->xkb.state = xkb_state_new(edev->xkb.keymap)))
233      {
234         g_mutex_unlock(&edev->xkb.state_mutex);
235         g_mutex_unlock(&edev->xkb.keymap_mutex);
236         ERR("Failed to create xkb state: %m");
237         return;
238      }
239
240    g_mutex_unlock(&edev->xkb.state_mutex);
241
242    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CTRL);
243    if (xkb_idx != XKB_MOD_INVALID)
244      edev->xkb.ctrl_mask = 1 << xkb_idx;
245    else
246      edev->xkb.ctrl_mask = 0;
247
248    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_ALT);
249    if (xkb_idx != XKB_MOD_INVALID)
250      edev->xkb.alt_mask = 1 << xkb_idx;
251    else
252      edev->xkb.alt_mask = 0;
253
254    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_SHIFT);
255    if (xkb_idx != XKB_MOD_INVALID)
256      edev->xkb.shift_mask = 1 << xkb_idx;
257    else
258      edev->xkb.shift_mask = 0;
259
260    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_LOGO);
261    if (xkb_idx != XKB_MOD_INVALID)
262      edev->xkb.win_mask = 1 << xkb_idx;
263    else
264      edev->xkb.win_mask = 0;
265
266    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_SCROLL);
267    if (xkb_idx != XKB_MOD_INVALID)
268      edev->xkb.scroll_mask = 1 << xkb_idx;
269    else
270      edev->xkb.scroll_mask = 0;
271
272    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_NUM);
273    if (xkb_idx != XKB_MOD_INVALID)
274      edev->xkb.num_mask = 1 << xkb_idx;
275    else
276      edev->xkb.num_mask = 0;
277
278    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CAPS);
279    if (xkb_idx != XKB_MOD_INVALID)
280      edev->xkb.caps_mask = 1 << xkb_idx;
281    else
282      edev->xkb.caps_mask = 0;
283
284    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, "ISO_Level3_Shift");
285    if (xkb_idx != XKB_MOD_INVALID)
286      edev->xkb.altgr_mask = 1 << xkb_idx;
287    else
288      edev->xkb.altgr_mask = 0;
289
290    g_mutex_unlock(&edev->xkb.keymap_mutex);
291 }
292
293 static int
294 _device_keysym_translate(xkb_keysym_t keysym, unsigned int modifiers, char *buffer, int bytes)
295 {
296    unsigned long hbytes = 0;
297    unsigned char c;
298
299    if (!keysym) return 0;
300    hbytes = (keysym >> 8);
301
302    if (!(bytes &&
303          ((hbytes == 0) ||
304           ((hbytes == 0xFF) &&
305            (((keysym >= XKB_KEY_BackSpace) && (keysym <= XKB_KEY_Clear)) ||
306             (keysym == XKB_KEY_Return) || (keysym == XKB_KEY_Escape) ||
307             (keysym == XKB_KEY_KP_Space) || (keysym == XKB_KEY_KP_Tab) ||
308             (keysym == XKB_KEY_KP_Enter) ||
309             ((keysym >= XKB_KEY_KP_Multiply) && (keysym <= XKB_KEY_KP_9)) ||
310             (keysym == XKB_KEY_KP_Equal) || (keysym == XKB_KEY_Delete))))))
311      return 0;
312
313    if (keysym == XKB_KEY_KP_Space)
314      c = (XKB_KEY_space & 0x7F);
315    else if (hbytes == 0xFF)
316      c = (keysym & 0x7F);
317    else
318      c = (keysym & 0xFF);
319
320    if (modifiers & ECORE_EVENT_MODIFIER_CTRL)
321      {
322         if (((c >= '@') && (c < '\177')) || c == ' ')
323           c &= 0x1F;
324         else if (c == '2')
325           c = '\000';
326         else if ((c >= '3') && (c <= '7'))
327           c -= ('3' - '\033');
328         else if (c == '8')
329           c = '\177';
330         else if (c == '/')
331           c = '_' & 0x1F;
332      }
333    buffer[0] = c;
334    return 1;
335 }
336
337 static void
338 _device_modifiers_update_device(E_Input_Evdev *edev, E_Input_Evdev *from)
339 {
340    xkb_mod_mask_t mask;
341
342    g_mutex_lock(&from->xkb.state_mutex);
343
344    edev->xkb.depressed =
345      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_DEPRESSED);
346    edev->xkb.latched =
347      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LATCHED);
348    edev->xkb.locked =
349      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LOCKED);
350    edev->xkb.group =
351      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_EFFECTIVE);
352
353    g_mutex_unlock(&from->xkb.state_mutex);
354
355    mask = (edev->xkb.depressed | edev->xkb.latched);
356
357    if (mask & from->xkb.ctrl_mask)
358      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_CTRL;
359    if (mask & from->xkb.alt_mask)
360      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALT;
361    if (mask & from->xkb.shift_mask)
362      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
363    if (mask & from->xkb.win_mask)
364      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_WIN;
365    if (mask & from->xkb.scroll_mask)
366      edev->xkb.modifiers |= ECORE_EVENT_LOCK_SCROLL;
367    if (mask & from->xkb.num_mask)
368      edev->xkb.modifiers |= ECORE_EVENT_LOCK_NUM;
369    if (mask & from->xkb.caps_mask)
370      edev->xkb.modifiers |= ECORE_EVENT_LOCK_CAPS;
371    if (mask & from->xkb.altgr_mask)
372      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALTGR;
373 }
374
375 static void
376 _device_modifiers_update(E_Input_Evdev *edev)
377 {
378    Eina_List *l;
379    E_Input_Evdev *ed;
380
381    edev->xkb.modifiers = 0;
382
383    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
384      _device_modifiers_update_device(edev, edev);
385    else
386      {
387         EINA_LIST_FOREACH(edev->seat->devices, l, ed)
388           {
389              if (!(ed->caps & E_INPUT_SEAT_KEYBOARD)) continue;
390              _device_modifiers_update_device(edev, ed);
391           }
392      }
393
394 }
395
396 static int
397 _device_remapped_key_get(E_Input_Evdev *edev, int code)
398 {
399    void *ret = NULL;
400
401    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, code);
402    if (!edev->key_remap_enabled) return code;
403    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->key_remap_hash, code);
404
405    ret = eina_hash_find(edev->key_remap_hash, &code);
406
407    if (ret) code = (int)(intptr_t)ret;
408
409    return code;
410 }
411
412 EINTERN E_Device *
413 e_input_evdev_get_e_device(const char *path, Ecore_Device_Class clas)
414 {
415    const GList *dev_list = NULL;
416    const GList *l;
417    E_Device *dev = NULL;
418    const char *identifier;
419
420    if (!path) return NULL;
421
422    dev_list = e_device_list_get();
423    if (!dev_list) return NULL;
424
425    l = dev_list;
426    while (l)
427      {
428         dev = l->data;
429         if (!dev) continue;
430         identifier = e_device_identifier_get(dev);
431         if (!identifier) continue;
432         if ((e_device_class_get(dev) == clas) && !(strcmp(identifier, path)))
433           return dev;
434
435         l = g_list_next(l);
436      }
437
438    return NULL;
439 }
440
441 EINTERN Ecore_Device *
442 e_input_evdev_get_ecore_device(const char *path, Ecore_Device_Class clas)
443 {
444    const Eina_List *dev_list = NULL;
445    const Eina_List *l;
446    Ecore_Device *dev = NULL;
447    const char *identifier;
448
449    if (!path) return NULL;
450
451    dev_list = ecore_device_list();
452    if (!dev_list) return NULL;
453    EINA_LIST_FOREACH(dev_list, l, dev)
454      {
455         if (!dev) continue;
456         identifier = ecore_device_identifier_get(dev);
457         if (!identifier) continue;
458         if ((ecore_device_class_get(dev) == clas) && !(strcmp(identifier, path)))
459           return dev;
460      }
461    return NULL;
462 }
463
464 static void
465 _e_input_event_mouse_move_cb_free(void *data EINA_UNUSED, void *event)
466 {
467    Ecore_Event_Mouse_Move *ev = event;
468
469    if (ev->dev) ecore_device_unref(ev->dev);
470
471    free(ev);
472 }
473
474 static void
475 _e_input_event_mouse_relative_move_cb_free(void *data EINA_UNUSED, void *event)
476 {
477    Ecore_Event_Mouse_Relative_Move *ev = event;
478
479    if (ev->dev) ecore_device_unref(ev->dev);
480
481    free(ev);
482 }
483
484 static void
485 _e_input_event_mouse_wheel_cb_free(void *data EINA_UNUSED, void *event)
486 {
487    Ecore_Event_Mouse_Wheel *ev = event;
488
489    if (ev->dev) ecore_device_unref(ev->dev);
490
491    free(ev);
492 }
493
494 static void
495 _e_input_event_mouse_button_cb_free(void *data EINA_UNUSED, void *event)
496 {
497    Ecore_Event_Mouse_Button *ev = event;
498
499    if (ev->dev) ecore_device_unref(ev->dev);
500
501    free(ev);
502 }
503
504 static void
505 _e_input_event_key_cb_free(void *data EINA_UNUSED, void *event)
506 {
507    Ecore_Event_Key *ev = event;
508
509    if (e_input_thread_mode_get())
510      {
511         if (ev->dev) g_object_unref(ev->dev);
512      }
513    else
514      {
515         if (ev->dev) ecore_device_unref(ev->dev);
516      }
517
518    if (ev->data) E_FREE(ev->data);
519
520    free(ev);
521 }
522
523 static void
524 _device_handle_key(struct libinput_device *device, struct libinput_event_keyboard *event)
525 {
526    E_Input_Evdev *edev;
527    E_Input_Backend *input;
528    uint32_t timestamp;
529    uint32_t code, nsyms;
530    const xkb_keysym_t *syms;
531    enum libinput_key_state state;
532    int key_count;
533    xkb_keysym_t sym = XKB_KEY_NoSymbol;
534    char key[256] = {0, }, keyname[256] = {0, }, compose_buffer[256] = {0, };
535    Ecore_Event_Key *e;
536    char *tmp = NULL, *compose = NULL;
537    E_Keyrouter_Event_Data *key_data;
538    Ecore_Device *ecore_dev = NULL, *data;
539    E_Device *e_dev = NULL, *e_dev_data;
540    Eina_List *l = NULL, *l_next = NULL;
541    GList *glist = NULL;
542    E_Comp_Config *comp_conf = NULL;
543    int *pressed_keycode = NULL, *idata = NULL;
544    Eina_Bool dup_found = EINA_FALSE;
545    const char *device_name = NULL;
546    Ecore_Device_Subclass device_subclas = ECORE_DEVICE_SUBCLASS_NONE;
547
548    if (!(edev = libinput_device_get_user_data(device)))
549      {
550         return;
551      }
552
553    if (!(input = edev->seat->input))
554      {
555         return;
556      }
557
558    if (!e_input_thread_mode_get())
559      {
560         if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
561         else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
562           {
563              EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
564                {
565                   if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_KEYBOARD)
566                     {
567                        ecore_dev = data;
568                        break;
569                     }
570                }
571           }
572         else
573           {
574              edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_KEYBOARD);
575              ecore_dev = edev->ecore_dev;
576           }
577
578         if (!ecore_dev)
579           {
580              ERR("Failed to get source ecore device from event !\n");
581              return;
582           }
583
584         device_name = ecore_device_name_get(ecore_dev);
585         device_subclas = ecore_device_subclass_get(ecore_dev);
586      }
587    else
588      {
589         if (edev->e_dev) e_dev = edev->e_dev;
590         else if (edev->e_dev_list && g_list_length(edev->e_dev_list) > 0)
591           {
592              glist = edev->e_dev_list;
593              while (glist)
594                {
595                   e_dev_data = glist->data;
596                   if (e_device_class_get(e_dev_data) == ECORE_DEVICE_CLASS_KEYBOARD)
597                     {
598                        e_dev = e_dev_data;
599                        break;
600                     }
601
602                   glist = g_list_next(glist);
603                }
604           }
605         else
606           {
607              edev->e_dev = e_input_evdev_get_e_device(edev->path, ECORE_DEVICE_CLASS_KEYBOARD);
608              e_dev = edev->e_dev;
609           }
610
611         if (!e_dev)
612           {
613              ERR("Failed to get source e device from event !\n");
614              return;
615           }
616
617         device_name = e_device_name_get(e_dev);
618         device_subclas = e_device_subclass_get(e_dev);
619      }
620
621    timestamp = libinput_event_keyboard_get_time(event);
622    code = libinput_event_keyboard_get_key(event);
623    code = _device_remapped_key_get(edev, code + 8);
624    state = libinput_event_keyboard_get_key_state(event);
625    key_count = libinput_event_keyboard_get_seat_key_count(event);
626
627    if (state == LIBINPUT_KEY_STATE_PRESSED)
628      {
629         if ((edev->seat->dev->blocked & E_INPUT_SEAT_KEYBOARD) ||
630             (edev->seat->dev->server_blocked & E_INPUT_SEAT_KEYBOARD))
631           {
632              g_rec_mutex_lock(&edev->seat->dev->blocked_client_mutex);
633              ELOGF("Key", "Press (keycode: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
634                    code, device_name, edev->seat->dev->blocked_client,
635                    edev->seat->dev->server_blocked);
636              g_rec_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
637              return;
638           }
639
640         /* FIXME: Currently to maintain press/release event pair during input block,
641          *        used Eina_List and this is method used in devicemgr module.
642          *        But we need to consider which way is better to maintain key press/release pair.
643          */
644
645         EINA_LIST_FOREACH(edev->xkb.pressed_keys, l, idata)
646           {
647              if (*idata == code)
648                {
649                   dup_found = EINA_TRUE;
650                   break;
651                }
652           }
653         if (!dup_found)
654           {
655              pressed_keycode = E_NEW(int, 1);
656              EINA_SAFETY_ON_NULL_RETURN(pressed_keycode);
657              *pressed_keycode = code;
658
659              edev->xkb.pressed_keys = eina_list_append(edev->xkb.pressed_keys, pressed_keycode);
660           }
661      }
662    else
663      {
664         dup_found = EINA_FALSE;
665         EINA_LIST_FOREACH_SAFE(edev->xkb.pressed_keys, l, l_next, idata)
666           {
667              if (code == *idata)
668                {
669                   edev->xkb.pressed_keys = eina_list_remove_list(edev->xkb.pressed_keys, l);
670                   E_FREE(idata);
671                   dup_found = EINA_TRUE;
672                   break;
673                }
674           }
675         if (!dup_found)
676           {
677              g_rec_mutex_lock(&edev->seat->dev->blocked_client_mutex);
678              ELOGF("Key", "Release (keycode: %d, device: %s) is blocked by %p", NULL,
679                    code, device_name, edev->seat->dev->blocked_client);
680              g_rec_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
681              return;
682           }
683      }
684
685    /* ignore key events that are not seat wide state changes */
686    if (((state == LIBINPUT_KEY_STATE_PRESSED) && (key_count != 1)) ||
687        ((state == LIBINPUT_KEY_STATE_RELEASED) && (key_count != 0)))
688      {
689         return;
690      }
691
692    e_input_keyboard_grab_key_cb func = e_input_keyboard_grab_key_handler_get();
693    if (func)
694      {
695         if (e_devicemgr_keyboard_grab_subtype_is_grabbed(device_subclas))
696           {
697              if (!e_input_thread_mode_get())
698                func(code, state, timestamp, ecore_dev);
699              else
700                func(code, state, timestamp, e_dev);
701              return;
702           }
703      }
704
705    g_mutex_lock(&edev->xkb.state_mutex);
706    xkb_state_update_key(edev->xkb.state, code,
707                         (state ? XKB_KEY_DOWN : XKB_KEY_UP));
708
709    /* get the keysym for this code */
710    nsyms = xkb_key_get_syms(edev->xkb.state, code, &syms);
711    g_mutex_unlock(&edev->xkb.state_mutex);
712
713    if (nsyms == 1) sym = syms[0];
714
715    /* If no keysym was found, name it "Keycode-NNN" */
716    if (sym == XKB_KEY_NoSymbol)
717      snprintf(key, sizeof(key), "Keycode-%u", code);
718    else
719      {
720         /* get the keyname for this sym */
721         xkb_keysym_get_name(sym, key, sizeof(key));
722      }
723
724    if (key[0] == '\0')
725      {
726         /* If no keyname was found, name it "Keycode-NNN" */
727         snprintf(keyname, sizeof(keyname), "Keycode-%u", code);
728      }
729    else
730      {
731         memcpy(keyname, key, sizeof(keyname));
732      }
733
734    /* if shift is active, we need to transform the key to lower */
735    g_mutex_lock(&edev->xkb.keymap_mutex);
736    g_mutex_lock(&edev->xkb.state_mutex);
737    if (xkb_state_mod_index_is_active(edev->xkb.state,
738                                      xkb_map_mod_get_index(edev->xkb.keymap,
739                                      XKB_MOD_NAME_SHIFT),
740                                      XKB_STATE_MODS_EFFECTIVE))
741      {
742         if (keyname[0] != '\0')
743           keyname[0] = tolower(keyname[0]);
744      }
745    g_mutex_unlock(&edev->xkb.keymap_mutex);
746    g_mutex_unlock(&edev->xkb.state_mutex);
747
748    if (_device_keysym_translate(sym, edev->xkb.modifiers,
749                                 compose_buffer, sizeof(compose_buffer)))
750      {
751         compose = eina_str_convert("ISO8859-1", "UTF-8", compose_buffer);
752         if (!compose)
753           {
754              ERR("E Input cannot convert input key string '%s' to UTF-8. "
755                  "Is Eina built with iconv support?", compose_buffer);
756           }
757         else
758           tmp = compose;
759      }
760
761    if (!compose) compose = compose_buffer;
762
763    e = calloc(1, sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
764               ((compose[0] != '\0') ? strlen(compose) : 0) + 3);
765    if (!e)
766      {
767         E_FREE(tmp);
768         return;
769      }
770    key_data = E_NEW(E_Keyrouter_Event_Data, 1);
771    if (!key_data)
772      {
773         E_FREE(tmp);
774         E_FREE(e);
775         return;
776      }
777
778    e->keyname = (char *)(e + 1);
779    e->key = e->keyname + strlen(keyname) + 1;
780    e->compose = strlen(compose) ? e->key + strlen(key) + 1 : NULL;
781    e->string = e->compose;
782
783    strncpy((char *)e->keyname, keyname, strlen(keyname));
784    strncpy((char *)e->key, key, strlen(key));
785    if (strlen(compose)) strncpy((char *)e->compose, compose, strlen(compose));
786
787    e->window = (Ecore_Window)input->dev->window;
788    e->event_window = (Ecore_Window)input->dev->window;
789    e->root_window = (Ecore_Window)input->dev->window;
790    e->timestamp = timestamp;
791    e->same_screen = 1;
792    e->keycode = code;
793    e->data = key_data;
794
795    _device_modifiers_update(edev);
796
797    e->modifiers = edev->xkb.modifiers;
798
799    comp_conf = e_comp_config_get();
800
801    if (comp_conf && comp_conf->input_log_enable)
802      ELOGF("Key", "%s (keyname: %s, keycode: %d, timestamp: %u, device: %s)", NULL, state?"Press":"Release", e->keyname, e->keycode, e->timestamp, device_name);
803
804    if (e_input_thread_mode_get())
805      {
806         e->dev = (Eo *)g_object_ref(e_dev);
807         e_input_event_add(input->event_source, state ? ECORE_EVENT_KEY_DOWN : ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
808      }
809    else
810      {
811         e->dev = ecore_device_ref(ecore_dev);
812         ecore_event_add(state ? ECORE_EVENT_KEY_DOWN : ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
813      }
814
815    if (tmp) free(tmp);
816 }
817
818 static void
819 _device_pointer_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event)
820 {
821    E_Input_Backend *input;
822    Ecore_Event_Mouse_Move *ev;
823    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
824    Eina_List *l;
825    int x = 0, y = 0, w = 0, h = 0;
826
827    if (!(input = edev->seat->input)) return;
828
829    ecore_thread_main_loop_begin();
830
831    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
832    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
833      {
834         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
835           {
836              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
837                {
838                   ecore_dev = data;
839                   break;
840                }
841              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
842                {
843                   detent_data = data;
844                }
845           }
846         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
847           {
848              ecore_dev = detent_data;
849           }
850      }
851    else
852      {
853         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
854         ecore_dev = edev->ecore_dev;
855      }
856
857    if (!ecore_dev)
858      {
859         ERR("Failed to get source ecore device from event !\n");
860         goto end;
861      }
862    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
863      {
864         /* Do not process detent device's move events. */
865         goto end;
866      }
867
868    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
869
870    _device_configured_size_get(edev, &x, &y, &w, &h);
871
872    if (edev->seat->ptr.ix < x)
873      edev->seat->ptr.dx = edev->seat->ptr.ix = x;
874    else if (edev->seat->ptr.ix >= (x + w))
875      edev->seat->ptr.dx = edev->seat->ptr.ix = (x + w - 1);
876
877    if (edev->seat->ptr.iy < y)
878      edev->seat->ptr.dy = edev->seat->ptr.iy = y;
879    else if (edev->seat->ptr.iy >= (y + h))
880      edev->seat->ptr.dy = edev->seat->ptr.iy = (y + h - 1);
881
882    edev->mouse.dx = edev->seat->ptr.dx;
883    edev->mouse.dy = edev->seat->ptr.dy;
884
885    ev->window = (Ecore_Window)input->dev->window;
886    ev->event_window = (Ecore_Window)input->dev->window;
887    ev->root_window = (Ecore_Window)input->dev->window;
888    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
889    ev->same_screen = 1;
890
891    _device_modifiers_update(edev);
892    ev->modifiers = edev->xkb.modifiers;
893
894    ev->x = edev->seat->ptr.ix;
895    ev->y = edev->seat->ptr.iy;
896    ev->root.x = ev->x;
897    ev->root.y = ev->y;
898
899    ev->multi.device = edev->mt_slot;
900    ev->multi.radius = 1;
901    ev->multi.radius_x = 1;
902    ev->multi.radius_y = 1;
903    ev->multi.pressure = 1.0;
904    ev->multi.angle = 0.0;
905    ev->multi.x = ev->x;
906    ev->multi.y = ev->y;
907    ev->multi.root.x = ev->x;
908    ev->multi.root.y = ev->y;
909
910    ev->dev = ecore_device_ref(ecore_dev);
911
912    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
913
914 end:
915    ecore_thread_main_loop_end();
916 }
917
918 void
919 _e_input_pointer_motion_post(E_Input_Evdev *edev)
920 {
921    _device_pointer_motion(edev, NULL);
922 }
923
924 static void
925 _device_pointer_relative_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event, double dx[2], double dy[2])
926 {
927    E_Input_Backend *input;
928    Ecore_Event_Mouse_Relative_Move *ev;
929    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
930    Eina_List *l;
931    E_Comp_Config *comp_conf;
932
933    if (!(input = edev->seat->input)) return;
934
935    ecore_thread_main_loop_begin();
936
937    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
938    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
939      {
940         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
941           {
942              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
943                {
944                   ecore_dev = data;
945                   break;
946                }
947              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
948                {
949                   detent_data = data;
950                }
951           }
952         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
953           {
954              ecore_dev = detent_data;
955           }
956      }
957    else
958      {
959         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
960         ecore_dev = edev->ecore_dev;
961      }
962
963    if (!ecore_dev)
964      {
965         ERR("Failed to get source ecore device from event !\n");
966         goto end;
967      }
968    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
969      {
970         /* Do not process detent device's move events. */
971         goto end;
972      }
973
974    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Relative_Move)))) return;
975
976    ev->window = (Ecore_Window)input->dev->window;
977    ev->event_window = (Ecore_Window)input->dev->window;
978    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
979
980    ev->modifiers = edev->xkb.modifiers;
981
982    ev->dx = (int)dx[0];
983    ev->dy = (int)dy[0];
984    ev->dx_unaccel = (int)dx[1];
985    ev->dy_unaccel = (int)dy[1];
986
987    ev->dev = ecore_device_ref(ecore_dev);
988
989    comp_conf = e_comp_config_get();
990    if (comp_conf && comp_conf->input_log_enable)
991      ELOGF("Mouse", "Relative Move (dx: %d, dy: %d, unaccel(%d, %d) device: %s)", NULL, ev->dx, ev->dx, ev->dx_unaccel, ev->dy_unaccel, ecore_device_name_get(ev->dev));
992
993    ecore_event_add(ECORE_EVENT_MOUSE_RELATIVE_MOVE, ev, _e_input_event_mouse_relative_move_cb_free, NULL);
994
995 end:
996    ecore_thread_main_loop_end();
997 }
998
999 static void
1000 _device_pointer_motion_send(E_Input_Evdev *edev, struct libinput_event_pointer *event, double dx[2], double dy[2])
1001 {
1002    e_input_relative_motion_cb func = e_input_relative_motion_handler_get();
1003    if (func)
1004      {
1005         //func(dx, dy, time_us);
1006         _device_pointer_relative_motion(edev, event, dx, dy);
1007      }
1008    else
1009      {
1010         double seat_dx, seat_dy, temp;
1011         if (edev->disable_acceleration)
1012           {
1013               seat_dx = dx[1];
1014               seat_dy = dy[1];
1015           }
1016         else
1017           {
1018               seat_dx = dx[0];
1019               seat_dy = dy[0];
1020           }
1021
1022         if (edev->seat->ptr.swap)
1023           {
1024               temp = seat_dx;
1025               seat_dx = seat_dy;
1026               seat_dy = temp;
1027           }
1028         if (edev->seat->ptr.invert_x)
1029           seat_dx *= -1;
1030         if (edev->seat->ptr.invert_y)
1031           seat_dy *= -1;
1032
1033         edev->seat->ptr.dx += seat_dx;
1034         edev->seat->ptr.dy += seat_dy;
1035
1036         edev->mouse.dx = edev->seat->ptr.dx;
1037         edev->mouse.dy = edev->seat->ptr.dy;
1038
1039         if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1040             floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1041           {
1042               return;
1043           }
1044
1045         edev->seat->ptr.ix = edev->seat->ptr.dx;
1046         edev->seat->ptr.iy = edev->seat->ptr.dy;
1047
1048         if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1049             (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1050           {
1051               return;
1052           }
1053
1054         _device_pointer_motion(edev, event);
1055      }
1056 }
1057
1058 static void
1059 _device_handle_pointer_motion(struct libinput_device *device, struct libinput_event_pointer *event)
1060 {
1061    E_Input_Evdev *edev;
1062    double delta_x[2]; /* delta_x[0] for accelerated, delta_x[1] for unaccelerated */
1063    double delta_y[2]; /* delta_y[0] for accelerated, delta_y[1] for unaccelerated */
1064
1065    if (!(edev = libinput_device_get_user_data(device)))
1066      {
1067         return;
1068      }
1069
1070    delta_x[0] = libinput_event_pointer_get_dx(event);
1071    delta_x[1] = libinput_event_pointer_get_dx_unaccelerated(event);
1072    delta_y[0] = libinput_event_pointer_get_dy(event);
1073    delta_y[1] = libinput_event_pointer_get_dy_unaccelerated(event);
1074
1075    _device_pointer_motion_send(edev, event, &delta_x[0], &delta_y[0]);
1076 }
1077
1078 static void
1079 _device_handle_pointer_motion_absolute(struct libinput_device *device, struct libinput_event_pointer *event)
1080 {
1081    E_Input_Evdev *edev;
1082    int w = 0, h = 0;
1083
1084    if (!(edev = libinput_device_get_user_data(device)))
1085      {
1086         return;
1087      }
1088
1089    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1090
1091    edev->mouse.dx = edev->seat->ptr.dx =
1092      libinput_event_pointer_get_absolute_x_transformed(event, w);
1093    edev->mouse.dy = edev->seat->ptr.dy =
1094      libinput_event_pointer_get_absolute_y_transformed(event, h);
1095
1096    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1097        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1098      {
1099         return;
1100      }
1101
1102    edev->seat->ptr.ix = edev->seat->ptr.dx;
1103    edev->seat->ptr.iy = edev->seat->ptr.dy;
1104
1105    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1106        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1107      {
1108         return;
1109      }
1110
1111    _device_pointer_motion(edev, event);
1112 }
1113
1114 static void
1115 _device_handle_button(struct libinput_device *device, struct libinput_event_pointer *event)
1116 {
1117    E_Input_Evdev *edev;
1118    E_Input_Backend *input;
1119    Ecore_Event_Mouse_Button *ev;
1120    enum libinput_button_state state;
1121    uint32_t button, timestamp;
1122    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
1123    Eina_List *l;
1124    E_Comp_Config *comp_conf = NULL;
1125
1126    if (!(edev = libinput_device_get_user_data(device)))
1127      {
1128         return;
1129      }
1130    if (!(input = edev->seat->input))
1131      {
1132         return;
1133      }
1134
1135    ecore_thread_main_loop_begin();
1136
1137    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1138    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1139      {
1140         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1141           {
1142              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
1143                {
1144                   ecore_dev = data;
1145                   break;
1146                }
1147              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
1148                {
1149                   detent_data = data;
1150                }
1151           }
1152         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
1153           {
1154              ecore_dev = detent_data;
1155           }
1156      }
1157    else
1158      {
1159         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
1160         ecore_dev = edev->ecore_dev;
1161      }
1162
1163    if (!ecore_dev)
1164      {
1165         ERR("Failed to get source ecore device from event !\n");
1166         goto end;
1167      }
1168
1169    state = libinput_event_pointer_get_button_state(event);
1170    button = libinput_event_pointer_get_button(event);
1171    timestamp = libinput_event_pointer_get_time(event);
1172
1173    button = ((button & 0x00F) + 1);
1174    if (button == 3) button = 2;
1175    else if (button == 2) button = 3;
1176
1177    if (state)
1178      {
1179         if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1180             (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1181           {
1182              g_rec_mutex_lock(&edev->seat->dev->blocked_client_mutex);
1183              ELOGF("Mouse", "Button Press (btn: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
1184                    button, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
1185                    edev->seat->dev->server_blocked);
1186              g_rec_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
1187              goto end;
1188           }
1189         else
1190           {
1191              edev->mouse.pressed_button |= (1 << button);
1192           }
1193      }
1194    else
1195      {
1196         if (!(edev->mouse.pressed_button & (1 << button)))
1197           {
1198              g_rec_mutex_lock(&edev->seat->dev->blocked_client_mutex);
1199              ELOGF("Mouse", "Button Release (btn: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
1200                    button, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
1201                    edev->seat->dev->server_blocked);
1202              g_rec_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
1203              goto end;
1204           }
1205         edev->mouse.pressed_button &= ~(1 << button);
1206      }
1207
1208    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button))))
1209      {
1210         goto end;
1211      }
1212
1213    ev->window = (Ecore_Window)input->dev->window;
1214    ev->event_window = (Ecore_Window)input->dev->window;
1215    ev->root_window = (Ecore_Window)input->dev->window;
1216    ev->timestamp = timestamp;
1217    ev->same_screen = 1;
1218
1219    _device_modifiers_update(edev);
1220    ev->modifiers = edev->xkb.modifiers;
1221
1222    ev->x = edev->seat->ptr.ix;
1223    ev->y = edev->seat->ptr.iy;
1224    ev->root.x = ev->x;
1225    ev->root.y = ev->y;
1226
1227    ev->multi.device = edev->mt_slot;
1228    ev->multi.radius = 1;
1229    ev->multi.radius_x = 1;
1230    ev->multi.radius_y = 1;
1231    ev->multi.pressure = 1.0;
1232    ev->multi.angle = 0.0;
1233    ev->multi.x = ev->x;
1234    ev->multi.y = ev->y;
1235    ev->multi.root.x = ev->x;
1236    ev->multi.root.y = ev->y;
1237    ev->dev = ecore_device_ref(ecore_dev);
1238
1239    if (state)
1240      {
1241         unsigned int current;
1242
1243         current = timestamp;
1244         edev->mouse.did_double = EINA_FALSE;
1245         edev->mouse.did_triple = EINA_FALSE;
1246
1247         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
1248             (button == edev->mouse.prev_button))
1249           {
1250              edev->mouse.did_double = EINA_TRUE;
1251              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
1252                  (button == edev->mouse.last_button))
1253                {
1254                   edev->mouse.did_triple = EINA_TRUE;
1255                   edev->mouse.prev = 0;
1256                   edev->mouse.last = 0;
1257                   current = 0;
1258                }
1259           }
1260
1261         edev->mouse.last = edev->mouse.prev;
1262         edev->mouse.prev = current;
1263         edev->mouse.last_button = edev->mouse.prev_button;
1264         edev->mouse.prev_button = button;
1265      }
1266
1267    ev->buttons = button;
1268
1269    if (edev->mouse.did_double)
1270      ev->double_click = 1;
1271    if (edev->mouse.did_triple)
1272      ev->triple_click = 1;
1273
1274    comp_conf = e_comp_config_get();
1275    if (comp_conf && comp_conf->input_log_enable)
1276      ELOGF("Mouse", "Button %s (btn: %d, device: %s)", NULL, state?"Press":"Release", button, ecore_device_name_get(ev->dev));
1277
1278    if (state)
1279      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, _e_input_event_mouse_button_cb_free, NULL);
1280    else
1281      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, _e_input_event_mouse_button_cb_free, NULL);
1282
1283 end:
1284    ecore_thread_main_loop_end();
1285 }
1286
1287 #if !LIBINPUT_HAVE_SCROLL_VALUE_V120
1288 static int
1289 _axis_value_get(struct libinput_event_pointer *pointer_event, enum libinput_pointer_axis axis)
1290 {
1291    enum libinput_pointer_axis_source source;
1292    double value = 0.0, value_discrete = 0.0;
1293    int ret = 0;
1294    E_Comp_Config *comp_conf = NULL;
1295
1296    comp_conf = e_comp_config_get();
1297    source = libinput_event_pointer_get_axis_source(pointer_event);
1298    switch (source)
1299      {
1300       case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
1301         value_discrete = libinput_event_pointer_get_axis_value_discrete(pointer_event, axis);
1302         value = libinput_event_pointer_get_axis_value(pointer_event, axis);
1303         if (comp_conf && comp_conf->input_log_enable)
1304           {
1305              ELOGF("Axis", "SOURCE_WHEEL discrete: %lf (cf. value: %lf)", NULL, value_discrete, value);
1306           }
1307         ret = (int)value_discrete;
1308         break;
1309       case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
1310       case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
1311         value = libinput_event_pointer_get_axis_value(pointer_event, axis);
1312         if (comp_conf && comp_conf->input_log_enable)
1313           {
1314              ELOGF("Axis", "SOURCE_FINGER/CONTINUOUS value: %lf", NULL, value);
1315           }
1316         if (value >= E_INPUT_FINGER_SCROLL_THRESHOLD)
1317           ret = 1;
1318         else if (value <= E_INPUT_FINGER_SCROLL_THRESHOLD * (-1))
1319           ret = -1;
1320         else
1321           ret = 0;
1322         break;
1323       default:
1324         break;
1325      }
1326
1327    return ret;
1328 }
1329
1330 static void
1331 _device_handle_axis(struct libinput_device *device, struct libinput_event_pointer *event)
1332 {
1333    E_Input_Evdev *edev;
1334    E_Input_Backend *input;
1335    Ecore_Event_Mouse_Wheel *ev;
1336    uint32_t timestamp;
1337    enum libinput_pointer_axis axis;
1338    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
1339    Eina_List *l;
1340    E_Comp_Config *comp_conf = NULL;
1341    int direction = 0, z = 0;
1342
1343    if (!(edev = libinput_device_get_user_data(device)))
1344      {
1345         return;
1346      }
1347    if (!(input = edev->seat->input))
1348      {
1349         return;
1350      }
1351
1352    ecore_thread_main_loop_begin();
1353
1354    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1355    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1356      {
1357         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1358           {
1359              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
1360                {
1361                   ecore_dev = data;
1362                   break;
1363                }
1364              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
1365                {
1366                   detent_data = data;
1367                }
1368           }
1369         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
1370           {
1371              ecore_dev = detent_data;
1372           }
1373      }
1374    else
1375      {
1376         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
1377         ecore_dev = edev->ecore_dev;
1378      }
1379
1380    if (!ecore_dev)
1381      {
1382         ERR("Failed to get source ecore device from event !\n");
1383         goto end;
1384      }
1385
1386    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
1387    if (libinput_event_pointer_has_axis(event, axis))
1388      z = _axis_value_get(event, axis);
1389
1390    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
1391    if (libinput_event_pointer_has_axis(event, axis))
1392      {
1393         direction = 1;
1394         z = _axis_value_get(event, axis);
1395      }
1396
1397   if (z == 0)
1398     {
1399        ELOGF("Mouse", "Axis event is ignored since it has zero value", NULL);
1400        goto end;
1401     }
1402
1403    comp_conf = e_comp_config_get();
1404    if (comp_conf && comp_conf->e_wheel_click_angle)
1405      {
1406         z = (int)(z * comp_conf->e_wheel_click_angle);
1407      }
1408
1409    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1410        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1411      {
1412         g_mutex_lock(&edev->seat->dev->blocked_client_mutex);
1413
1414         if (detent_data)
1415           ELOGF("Mouse", "Detent (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1416                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
1417         else
1418           ELOGF("Mouse", "Wheel (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1419                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
1420
1421         g_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
1422         goto end;
1423      }
1424
1425    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
1426      {
1427         goto end;
1428      }
1429
1430    timestamp = libinput_event_pointer_get_time(event);
1431
1432    ev->window = (Ecore_Window)input->dev->window;
1433    ev->event_window = (Ecore_Window)input->dev->window;
1434    ev->root_window = (Ecore_Window)input->dev->window;
1435    ev->timestamp = timestamp;
1436    ev->same_screen = 1;
1437
1438    _device_modifiers_update(edev);
1439    ev->modifiers = edev->xkb.modifiers;
1440
1441    ev->x = edev->seat->ptr.ix;
1442    ev->y = edev->seat->ptr.iy;
1443    ev->root.x = ev->x;
1444    ev->root.y = ev->y;
1445    ev->z = z;
1446    ev->direction = direction;
1447
1448    if (comp_conf && comp_conf->input_log_enable)
1449      {
1450         if (detent_data)
1451           ELOGF("Mouse", "Detent (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1452         else
1453           ELOGF("Mouse", "Wheel (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1454      }
1455
1456
1457    ev->dev = ecore_device_ref(ecore_dev);
1458    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
1459
1460 end:
1461    ecore_thread_main_loop_end();
1462 }
1463 #endif
1464
1465 #if LIBINPUT_HAVE_SCROLL_VALUE_V120
1466 static int
1467 _scroll_value_get(struct libinput_event_pointer *pointer_event, enum libinput_pointer_axis axis, E_Input_Axis_Source source)
1468 {
1469    double value = 0.0;
1470    double value_v120 = 0.0;
1471    int ret;
1472    E_Comp_Config *comp_conf = NULL;
1473
1474    comp_conf = e_comp_config_get();
1475    switch (source)
1476      {
1477       case E_INPUT_AXIS_SOURCE_WHEEL:
1478         value_v120 = libinput_event_pointer_get_scroll_value_v120(pointer_event, axis);
1479         value = libinput_event_pointer_get_scroll_value(pointer_event, axis);
1480         if (comp_conf && comp_conf->input_log_enable)
1481           {
1482              ELOGF("Scroll", "SOURCE_WHEEL value_v120: %lf (cf. value: %lf)", NULL, value_v120, value);
1483           }
1484         if (((int)value_v120 % E_INPUT_POINTER_AXIS_DISCRETE_STEP) != 0)
1485           {
1486              ERR("Wheel movements should be multiples (or fractions) of 120!\n");
1487              ret = 0;
1488           }
1489         else
1490           {
1491              ret = value_v120 / E_INPUT_POINTER_AXIS_DISCRETE_STEP;
1492           }
1493         break;
1494       case E_INPUT_AXIS_SOURCE_FINGER:
1495       case E_INPUT_AXIS_SOURCE_CONTINUOUS:
1496         value = libinput_event_pointer_get_scroll_value(pointer_event, axis);
1497         if (comp_conf && comp_conf->input_log_enable)
1498           {
1499              ELOGF("Scroll", "SOURCE_FINGER/CONTINUOUS value: %lf", NULL, value);
1500           }
1501         if (value >= E_INPUT_FINGER_SCROLL_THRESHOLD)
1502           ret = 1;
1503         else if (value <= E_INPUT_FINGER_SCROLL_THRESHOLD * (-1))
1504           ret = -1;
1505         else
1506           ret = 0;
1507         break;
1508       default:
1509         break;
1510      }
1511
1512    return ret;
1513 }
1514
1515 static void
1516 _device_handle_axis_v120(struct libinput_device *device, struct libinput_event_pointer *event, E_Input_Axis_Source source)
1517 {
1518    E_Input_Evdev *edev;
1519    E_Input_Backend *input;
1520    Ecore_Event_Mouse_Wheel *ev;
1521    uint32_t timestamp;
1522    enum libinput_pointer_axis axis;
1523    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
1524    Eina_List *l;
1525    E_Comp_Config *comp_conf = NULL;
1526    int direction = 0, z = 0;
1527
1528    if (!(edev = libinput_device_get_user_data(device)))
1529      {
1530         return;
1531      }
1532    if (!(input = edev->seat->input))
1533      {
1534         return;
1535      }
1536
1537    ecore_thread_main_loop_begin();
1538
1539    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1540    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1541      {
1542         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1543           {
1544              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
1545                {
1546                   ecore_dev = data;
1547                   break;
1548                }
1549              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
1550                {
1551                   detent_data = data;
1552                }
1553           }
1554         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
1555           {
1556              ecore_dev = detent_data;
1557           }
1558      }
1559    else
1560      {
1561         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
1562         ecore_dev = edev->ecore_dev;
1563      }
1564
1565    if (!ecore_dev)
1566      {
1567         ERR("Failed to get source ecore device from event !\n");
1568         goto end;
1569      }
1570
1571    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
1572    if (libinput_event_pointer_has_axis(event, axis))
1573      z = _scroll_value_get(event, axis, source);
1574
1575    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
1576    if (libinput_event_pointer_has_axis(event, axis))
1577      {
1578         direction = 1;
1579         z = _scroll_value_get(event, axis, source);
1580      }
1581
1582    if (z == 0)
1583      {
1584         ELOGF("Mouse", "Scroll event is ignored since it has zero value", NULL);
1585         goto end;
1586      }
1587
1588    comp_conf = e_comp_config_get();
1589    if (comp_conf && comp_conf->e_wheel_click_angle)
1590      {
1591         z = (int)(z * comp_conf->e_wheel_click_angle);
1592      }
1593
1594    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1595        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1596      {
1597         g_rec_mutex_lock(&edev->seat->dev->blocked_client_mutex);
1598         if (detent_data)
1599           ELOGF("Mouse", "Detent (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1600                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
1601         else
1602           ELOGF("Mouse", "Wheel (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1603                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
1604
1605         g_rec_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
1606         goto end;
1607      }
1608
1609    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
1610      {
1611         goto end;
1612      }
1613
1614    timestamp = libinput_event_pointer_get_time(event);
1615
1616    ev->window = (Ecore_Window)input->dev->window;
1617    ev->event_window = (Ecore_Window)input->dev->window;
1618    ev->root_window = (Ecore_Window)input->dev->window;
1619    ev->timestamp = timestamp;
1620    ev->same_screen = 1;
1621
1622    _device_modifiers_update(edev);
1623    ev->modifiers = edev->xkb.modifiers;
1624
1625    ev->x = edev->seat->ptr.ix;
1626    ev->y = edev->seat->ptr.iy;
1627    ev->root.x = ev->x;
1628    ev->root.y = ev->y;
1629    ev->z = z;
1630    ev->direction = direction;
1631
1632    if (comp_conf && comp_conf->input_log_enable)
1633      {
1634         if (detent_data)
1635           ELOGF("Mouse", "Detent (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1636         else
1637           ELOGF("Mouse", "Wheel (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1638      }
1639
1640    ev->dev = ecore_device_ref(ecore_dev);
1641    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
1642
1643 end:
1644    ecore_thread_main_loop_end();
1645 }
1646 #endif
1647
1648 static void
1649 _device_handle_touch_event_send(E_Input_Evdev *edev, struct libinput_event_touch *event, int state)
1650 {
1651    E_Input_Backend *input;
1652    Ecore_Event_Mouse_Button *ev;
1653    uint32_t timestamp, button = 0;
1654    Ecore_Device *ecore_dev = NULL, *data;
1655    Eina_List *l;
1656
1657    if (!edev) return;
1658    if (!(input = edev->seat->input)) return;
1659
1660    ecore_thread_main_loop_begin();
1661
1662    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1663    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1664      {
1665         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1666           {
1667              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1668                {
1669                   ecore_dev = data;
1670                   break;
1671                }
1672           }
1673      }
1674    else
1675      {
1676         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1677         ecore_dev = edev->ecore_dev;
1678      }
1679
1680    if (!ecore_dev)
1681      {
1682         ERR("Failed to get source ecore device from event !\n");
1683         goto end;
1684      }
1685
1686    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) goto end;
1687
1688    timestamp = libinput_event_touch_get_time(event);
1689
1690    ev->window = (Ecore_Window)input->dev->window;
1691    ev->event_window = (Ecore_Window)input->dev->window;
1692    ev->root_window = (Ecore_Window)input->dev->window;
1693    ev->timestamp = timestamp;
1694    ev->same_screen = 1;
1695
1696    _device_modifiers_update(edev);
1697    ev->modifiers = edev->xkb.modifiers;
1698
1699    ev->x = edev->seat->ptr.ix;
1700    ev->y = edev->seat->ptr.iy;
1701    ev->root.x = ev->x;
1702    ev->root.y = ev->y;
1703
1704    ev->multi.device = edev->mt_slot;
1705    ev->multi.radius = 1;
1706    ev->multi.radius_x = 1;
1707    ev->multi.radius_y = 1;
1708    ev->multi.pressure = 1.0;
1709    ev->multi.angle = 0.0;
1710 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1711    if (libinput_event_get_type(libinput_event_touch_get_base_event(event))
1712        == LIBINPUT_EVENT_TOUCH_DOWN)
1713      {
1714         if (libinput_event_touch_has_minor(event))
1715           ev->multi.radius_x = libinput_event_touch_get_minor(event);
1716         if (libinput_event_touch_has_major(event))
1717           ev->multi.radius_y = libinput_event_touch_get_major(event);
1718         if (libinput_event_touch_has_pressure(event))
1719           ev->multi.pressure = libinput_event_touch_get_pressure(event);
1720         if (libinput_event_touch_has_orientation(event))
1721           ev->multi.angle = libinput_event_touch_get_orientation(event);
1722      }
1723 #endif
1724    ev->multi.x = ev->x;
1725    ev->multi.y = ev->y;
1726    ev->multi.root.x = ev->x;
1727    ev->multi.root.y = ev->y;
1728    ev->dev = ecore_device_ref(ecore_dev);
1729
1730    if (state == ECORE_EVENT_MOUSE_BUTTON_DOWN)
1731      {
1732         unsigned int current;
1733
1734         current = timestamp;
1735         edev->mouse.did_double = EINA_FALSE;
1736         edev->mouse.did_triple = EINA_FALSE;
1737
1738         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
1739             (button == edev->mouse.prev_button))
1740           {
1741              edev->mouse.did_double = EINA_TRUE;
1742              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
1743                  (button == edev->mouse.last_button))
1744                {
1745                   edev->mouse.did_triple = EINA_TRUE;
1746                   edev->mouse.prev = 0;
1747                   edev->mouse.last = 0;
1748                   current = 0;
1749                }
1750           }
1751
1752         edev->mouse.last = edev->mouse.prev;
1753         edev->mouse.prev = current;
1754         edev->mouse.last_button = edev->mouse.prev_button;
1755         edev->mouse.prev_button = button;
1756         edev->touch.pressed |= (1 << ev->multi.device);
1757      }
1758    else
1759      {
1760         edev->touch.pressed &= ~(1 << ev->multi.device);
1761      }
1762
1763    ev->buttons = ((button & 0x00F) + 1);
1764
1765    if (edev->mouse.did_double)
1766      ev->double_click = 1;
1767    if (edev->mouse.did_triple)
1768      ev->triple_click = 1;
1769
1770    ecore_event_add(state, ev, _e_input_event_mouse_button_cb_free, NULL);
1771
1772 end:
1773    ecore_thread_main_loop_end();
1774 }
1775
1776 static void
1777 _device_handle_touch_motion_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1778 {
1779    E_Input_Backend *input;
1780    Ecore_Event_Mouse_Move *ev;
1781    Ecore_Device *ecore_dev = NULL, *data;
1782    Eina_List *l;
1783
1784    if (!edev) return;
1785    if (!(input = edev->seat->input)) return;
1786
1787    ecore_thread_main_loop_begin();
1788
1789    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1790    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1791      {
1792         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1793           {
1794              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1795                {
1796                   ecore_dev = data;
1797                   break;
1798                }
1799           }
1800      }
1801    else
1802      {
1803         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1804         ecore_dev = edev->ecore_dev;
1805      }
1806
1807    if (!ecore_dev)
1808      {
1809         ERR("Failed to get source ecore device from event !\n");
1810         goto end;
1811      }
1812
1813    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) goto end;
1814
1815    ev->window = (Ecore_Window)input->dev->window;
1816    ev->event_window = (Ecore_Window)input->dev->window;
1817    ev->root_window = (Ecore_Window)input->dev->window;
1818    ev->timestamp = libinput_event_touch_get_time(event);
1819    ev->same_screen = 1;
1820
1821    _device_modifiers_update(edev);
1822    ev->modifiers = edev->xkb.modifiers;
1823
1824    ev->x = edev->seat->ptr.ix;
1825    ev->y = edev->seat->ptr.iy;
1826    ev->root.x = ev->x;
1827    ev->root.y = ev->y;
1828
1829    ev->multi.device = edev->mt_slot;
1830    ev->multi.radius = 1;
1831    ev->multi.radius_x = 1;
1832    ev->multi.radius_y = 1;
1833    ev->multi.pressure = 1.0;
1834    ev->multi.angle = 0.0;
1835 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1836    if (libinput_event_touch_has_minor(event))
1837      ev->multi.radius_x = libinput_event_touch_get_minor(event);
1838    if (libinput_event_touch_has_major(event))
1839      ev->multi.radius_y = libinput_event_touch_get_major(event);
1840    if (libinput_event_touch_has_pressure(event))
1841      ev->multi.pressure = libinput_event_touch_get_pressure(event);
1842    if (libinput_event_touch_has_orientation(event))
1843      ev->multi.angle = libinput_event_touch_get_orientation(event);
1844 #endif
1845    ev->multi.x = ev->x;
1846    ev->multi.y = ev->y;
1847    ev->multi.root.x = ev->x;
1848    ev->multi.root.y = ev->y;
1849    ev->dev = ecore_device_ref(ecore_dev);
1850
1851    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
1852
1853 end:
1854    ecore_thread_main_loop_end();
1855 }
1856
1857 static void
1858 _device_handle_touch_cancel_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1859 {
1860    E_Input_Backend *input;
1861    Ecore_Event_Mouse_Button *ev;
1862    uint32_t timestamp, button = 0;
1863    Ecore_Device *ecore_dev = NULL, *data;
1864    Eina_List *l;
1865
1866    if (!edev) return;
1867    if (!(input = edev->seat->input)) return;
1868
1869    ecore_thread_main_loop_begin();
1870
1871    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1872    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1873      {
1874         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1875           {
1876              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1877                {
1878                   ecore_dev = data;
1879                   break;
1880                }
1881           }
1882      }
1883    else
1884      {
1885         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1886         ecore_dev = edev->ecore_dev;
1887      }
1888
1889    if (!ecore_dev)
1890      {
1891         ERR("Failed to get source ecore device from event !\n");
1892         goto end;
1893      }
1894
1895    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) goto end;
1896
1897    timestamp = libinput_event_touch_get_time(event);
1898
1899    ev->window = (Ecore_Window)input->dev->window;
1900    ev->event_window = (Ecore_Window)input->dev->window;
1901    ev->root_window = (Ecore_Window)input->dev->window;
1902    ev->timestamp = timestamp;
1903    ev->same_screen = 1;
1904
1905    ev->x = edev->seat->ptr.ix;
1906    ev->y = edev->seat->ptr.iy;
1907    ev->root.x = ev->x;
1908    ev->root.y = ev->y;
1909
1910    ev->multi.device = edev->mt_slot;
1911    ev->multi.radius = 1;
1912    ev->multi.radius_x = 1;
1913    ev->multi.radius_y = 1;
1914    ev->multi.pressure = 1.0;
1915    ev->multi.angle = 0.0;
1916    ev->multi.x = ev->x;
1917    ev->multi.y = ev->y;
1918    ev->multi.root.x = ev->x;
1919    ev->multi.root.y = ev->y;
1920
1921    edev->touch.pressed &= ~(1 << ev->multi.device);
1922
1923    ev->buttons = ((button & 0x00F) + 1);
1924    ev->dev = ecore_device_ref(ecore_dev);
1925
1926    ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_CANCEL, ev, _e_input_event_mouse_button_cb_free, NULL);
1927
1928 end:
1929    ecore_thread_main_loop_end();
1930 }
1931
1932 static void
1933 _device_configured_size_get(E_Input_Evdev *edev, int *x, int *y, int *w, int *h)
1934 {
1935    E_Output *output = NULL;
1936
1937    EINA_SAFETY_ON_NULL_RETURN(edev);
1938
1939    if (!edev->output_configured)
1940      {
1941         if (edev->output_name)
1942           {
1943              output = e_output_find(edev->output_name);
1944              if (output)
1945                {
1946                   edev->mouse.minx = output->config.geom.x;
1947                   edev->mouse.miny = output->config.geom.y;
1948                   edev->mouse.maxw = output->config.geom.w;
1949                   edev->mouse.maxh = output->config.geom.h;
1950                   if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
1951                     {
1952                        edev->seat->ptr.dx = (double)(edev->mouse.maxw / 2);
1953                        edev->seat->ptr.dy = (double)(edev->mouse.maxh / 2);
1954                        edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
1955                        edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
1956                        edev->mouse.dx = edev->seat->ptr.dx;
1957                        edev->mouse.dy = edev->seat->ptr.dy;
1958                     }
1959                }
1960           }
1961
1962           edev->output_configured = EINA_TRUE;
1963           ELOGF("E_INPUT_EVDEV", "Device is configured by output x:%d,y:%d (w:%d, h:%d)",
1964                 NULL, edev->mouse.minx, edev->mouse.miny, edev->mouse.maxw, edev->mouse.maxh);
1965      }
1966    if (x) *x = edev->mouse.minx;
1967    if (y) *y = edev->mouse.miny;
1968    if (w) *w = edev->mouse.maxw;
1969    if (h) *h = edev->mouse.maxh;
1970 }
1971
1972 static void
1973 _device_handle_touch_down(struct libinput_device *device, struct libinput_event_touch *event)
1974 {
1975    E_Input_Evdev *edev;
1976    int x = 0, y = 0, w = 0, h = 0;
1977    E_Comp_Config *comp_conf = NULL;
1978
1979    if (!(edev = libinput_device_get_user_data(device)))
1980      {
1981         return;
1982      }
1983
1984    _device_configured_size_get(edev, &x, &y, &w, &h);
1985
1986    edev->mouse.dx = edev->seat->ptr.ix = edev->seat->ptr.dx =
1987      x + libinput_event_touch_get_x_transformed(event, w);
1988    edev->mouse.dy = edev->seat->ptr.iy = edev->seat->ptr.dy =
1989      y + libinput_event_touch_get_y_transformed(event, h);
1990
1991    edev->mt_slot = libinput_event_touch_get_slot(event);
1992    if (edev->mt_slot < 0)
1993      {
1994         /* FIXME: The single touch device return slot id -1
1995          *        But currently we have no API to distinguish multi touch or single touch
1996          *        After libinput 1.11 version, libinput provides get_touch_count API,
1997          *        so we can distinguish multi touch device or single touch device.
1998          */
1999         if (edev->mt_slot == -1)
2000           edev->mt_slot = 0;
2001         else
2002           {
2003              WRN("%d slot touch down events are not supported\n", edev->mt_slot);
2004              return;
2005           }
2006      }
2007
2008    if (edev->mt_slot < e_input_touch_max_count_get())
2009      {
2010         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
2011         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
2012      }
2013
2014    comp_conf = e_comp_config_get();
2015    if (comp_conf && comp_conf->input_log_enable)
2016      ELOGF("Touch", "Down (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
2017
2018    g_rec_mutex_lock(&edev->touch.raw_pressed_mutex);
2019    edev->touch.raw_pressed |= (1 << edev->mt_slot);
2020    g_rec_mutex_unlock(&edev->touch.raw_pressed_mutex);
2021
2022    g_rec_mutex_lock(&edev->touch.blocked_mutex);
2023    if (edev->touch.blocked)
2024      {
2025         g_rec_mutex_lock(&edev->seat->dev->blocked_client_mutex);
2026         ELOGF("Touch", "Down (id: %d, x: %d, y: %d) is blocked by %p, server: 0x%x", NULL,
2027               edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, edev->seat->dev->blocked_client,
2028               edev->seat->dev->server_blocked);
2029         g_rec_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
2030         g_rec_mutex_unlock(&edev->touch.blocked_mutex);
2031         return;
2032      }
2033
2034    g_rec_mutex_unlock(&edev->touch.blocked_mutex);
2035
2036    _device_handle_touch_motion_send(edev, event);
2037    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_DOWN);
2038 }
2039
2040 static void
2041 _device_handle_touch_motion(struct libinput_device *device, struct libinput_event_touch *event)
2042 {
2043    E_Input_Evdev *edev;
2044    int x = 0, y = 0, w = 0, h = 0;
2045
2046    if (!(edev = libinput_device_get_user_data(device)))
2047      {
2048         return;
2049      }
2050
2051    _device_configured_size_get(edev, &x, &y, &w, &h);
2052
2053    edev->mouse.dx = edev->seat->ptr.dx =
2054      x + libinput_event_touch_get_x_transformed(event, w);
2055    edev->mouse.dy = edev->seat->ptr.dy =
2056      y + libinput_event_touch_get_y_transformed(event, h);
2057
2058    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
2059        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
2060      {
2061         return;
2062      }
2063
2064    edev->seat->ptr.ix = edev->seat->ptr.dx;
2065    edev->seat->ptr.iy = edev->seat->ptr.dy;
2066
2067    edev->mt_slot = libinput_event_touch_get_slot(event);
2068    if (edev->mt_slot < 0)
2069      {
2070         /* FIXME: The single touch device return slot id -1
2071          *        But currently we have no API to distinguish multi touch or single touch
2072          *        After libinput 1.11 version, libinput provides get_touch_count API,
2073          *        so we can distinguish multi touch device or single touch device.
2074          */
2075         if (edev->mt_slot == -1)
2076           edev->mt_slot = 0;
2077         else
2078           {
2079              WRN("%d slot touch motion events are not supported\n", edev->mt_slot);
2080              return;
2081           }
2082      }
2083
2084    if (edev->mt_slot < e_input_touch_max_count_get())
2085      {
2086         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
2087         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
2088      }
2089
2090    if (!(edev->touch.pressed & (1 << edev->mt_slot)))
2091      {
2092         g_rec_mutex_lock(&edev->touch.blocked_mutex);
2093         if (edev->touch.blocked)
2094           {
2095              g_rec_mutex_unlock(&edev->touch.blocked_mutex);
2096              return;
2097           }
2098         g_rec_mutex_unlock(&edev->touch.blocked_mutex);
2099      }
2100
2101    _device_handle_touch_motion_send(edev, event);
2102 }
2103
2104 static void
2105 _device_handle_touch_up(struct libinput_device *device, struct libinput_event_touch *event)
2106 {
2107    E_Input_Evdev *edev;
2108    E_Comp_Config *comp_conf = NULL;
2109
2110    if (!(edev = libinput_device_get_user_data(device)))
2111      {
2112         return;
2113      }
2114
2115    edev->mt_slot = libinput_event_touch_get_slot(event);
2116    if (edev->mt_slot < 0)
2117      {
2118         /* FIXME: The single touch device return slot id -1
2119          *        But currently we have no API to distinguish multi touch or single touch
2120          *        After libinput 1.11 version, libinput provides get_touch_count API,
2121          *        so we can distinguish multi touch device or single touch device.
2122          */
2123         if (edev->mt_slot == -1)
2124           edev->mt_slot = 0;
2125         else
2126           {
2127              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
2128              return;
2129           }
2130      }
2131
2132    if (edev->mt_slot < e_input_touch_max_count_get())
2133      {
2134         edev->mouse.dx = edev->seat->ptr.dx = edev->seat->ptr.ix =
2135           edev->touch.coords[edev->mt_slot].x;
2136         edev->mouse.dy = edev->seat->ptr.dy = edev->seat->ptr.iy =
2137           edev->touch.coords[edev->mt_slot].y;
2138      }
2139
2140    comp_conf = e_comp_config_get();
2141    if (comp_conf && comp_conf->input_log_enable)
2142      ELOGF("Touch", "Up (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
2143
2144    g_rec_mutex_lock(&edev->touch.raw_pressed_mutex);
2145    edev->touch.raw_pressed &= ~(1 << edev->mt_slot);
2146    g_rec_mutex_unlock(&edev->touch.raw_pressed_mutex);
2147
2148    g_rec_mutex_lock(&edev->touch.blocked_mutex);
2149    if (edev->touch.blocked)
2150      {
2151         if (!(edev->touch.pressed & (1 << edev->mt_slot)))
2152           {
2153              g_rec_mutex_lock(&edev->seat->dev->blocked_client_mutex);
2154              ELOGF("Touch", "Up (id: %d, x: %d, y: %d) is blocked by %p, server(0x%x)", NULL,
2155                    edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, edev->seat->dev->blocked_client,
2156                    edev->seat->dev->server_blocked);
2157              g_rec_mutex_unlock(&edev->seat->dev->blocked_client_mutex);
2158
2159              g_rec_mutex_lock(&edev->touch.raw_pressed_mutex);
2160              if (edev->touch.raw_pressed == 0x0)
2161                {
2162                   edev->touch.blocked = EINA_FALSE;
2163                }
2164              g_rec_mutex_unlock(&edev->touch.raw_pressed_mutex);
2165              g_rec_mutex_unlock(&edev->touch.blocked_mutex);
2166              return;
2167           }
2168      }
2169
2170    g_rec_mutex_unlock(&edev->touch.blocked_mutex);
2171    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_UP);
2172 }
2173
2174 static void
2175 _device_handle_touch_cancel(struct libinput_device *device, struct libinput_event_touch *event)
2176 {
2177    E_Input_Evdev *edev;
2178    E_Comp_Config *comp_conf = NULL;
2179
2180    if (!(edev = libinput_device_get_user_data(device)))
2181      {
2182         return;
2183      }
2184
2185    edev->mt_slot = libinput_event_touch_get_slot(event);
2186    if (edev->mt_slot < 0)
2187      {
2188         /* FIXME: The single touch device return slot id -1
2189          *        But currently we have no API to distinguish multi touch or single touch
2190          *        After libinput 1.11 version, libinput provides get_touch_count API,
2191          *        so we can distinguish multi touch device or single touch device.
2192          */
2193         if (edev->mt_slot == -1)
2194           edev->mt_slot = 0;
2195         else
2196           {
2197              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
2198              return;
2199           }
2200      }
2201
2202    comp_conf = e_comp_config_get();
2203    if (comp_conf && comp_conf->input_log_enable)
2204      ELOGF("Touch", "cancel (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
2205
2206    _device_handle_touch_cancel_send(edev, event);
2207 }
2208
2209
2210 static void
2211 _device_handle_touch_frame(struct libinput_device *device EINA_UNUSED, struct libinput_event_touch *event EINA_UNUSED)
2212 {
2213    /* DBG("Unhandled Touch Frame Event"); */
2214 }
2215
2216 static void
2217 _e_input_aux_data_event_free(void *user_data EINA_UNUSED, void *ev)
2218 {
2219    Ecore_Event_Axis_Update *e = (Ecore_Event_Axis_Update *)ev;
2220
2221    if (e->axis) free(e->axis);
2222    if (e->dev) ecore_device_unref(e->dev);
2223
2224    free(e);
2225 }
2226
2227 static void
2228 _device_handle_touch_aux_data(struct libinput_device *device, struct libinput_event_touch_aux_data *event)
2229 {
2230    E_Input_Evdev *edev;
2231    E_Input_Backend *input;
2232    Ecore_Event_Axis_Update *ev;
2233    Ecore_Axis *axis;
2234    Ecore_Device *ecore_dev = NULL, *data;
2235    Eina_List *l;
2236    E_Comp_Config *comp_conf;
2237
2238    ecore_thread_main_loop_begin();
2239
2240    if (libinput_event_touch_aux_data_get_type(event) != LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM &&
2241        libinput_event_touch_aux_data_get_value(event) > 0)
2242       goto end;
2243
2244    if (!(edev = libinput_device_get_user_data(device))) goto end;
2245    if (!(input = edev->seat->input)) goto end;
2246
2247    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
2248    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
2249      {
2250         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
2251           {
2252              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
2253                {
2254                   ecore_dev = data;
2255                   break;
2256                }
2257           }
2258      }
2259    else
2260      {
2261         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
2262         ecore_dev = edev->ecore_dev;
2263      }
2264
2265    if (!ecore_dev)
2266      {
2267         ERR("Failed to get source ecore device from event !\n");
2268         goto end;
2269      }
2270
2271    if (!(ev = calloc(1, sizeof(Ecore_Event_Axis_Update)))) goto end;
2272
2273    ev->window = (Ecore_Window)input->dev->window;
2274    ev->event_window = (Ecore_Window)input->dev->window;
2275    ev->root_window = (Ecore_Window)input->dev->window;
2276    ev->timestamp = libinput_event_touch_aux_data_get_time(event);
2277
2278    axis = (Ecore_Axis *)calloc(1, sizeof(Ecore_Axis));
2279    if (axis)
2280      {
2281         axis->label = ECORE_AXIS_LABEL_TOUCH_PALM;
2282         axis->value = libinput_event_touch_aux_data_get_value(event);
2283         ev->naxis = 1;
2284      }
2285    ev->axis = axis;
2286
2287    comp_conf = e_comp_config_get();
2288    if (comp_conf && comp_conf->input_log_enable)
2289      ELOGF("Touch", "Axis (label: %d, value: %lf)", NULL, axis?axis->label:-1, axis?axis->value:0.0);
2290
2291    ev->dev = ecore_device_ref(ecore_dev);
2292
2293    ecore_event_add(ECORE_EVENT_AXIS_UPDATE, ev, _e_input_aux_data_event_free, NULL);
2294
2295 end:
2296    ecore_thread_main_loop_end();
2297 }
2298
2299 E_Input_Evdev *
2300 _e_input_evdev_device_create(E_Input_Seat *seat, struct libinput_device *device)
2301 {
2302    E_Input_Evdev *edev;
2303    E_Input_Backend *b_input;
2304    const char *output_name;
2305
2306    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
2307
2308    E_Input *e_input = e_input_get();
2309
2310    /* try to allocate space for new evdev */
2311    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
2312
2313    edev->seat = seat;
2314    edev->device = device;
2315    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
2316
2317    g_rec_mutex_init(&edev->touch.raw_pressed_mutex);
2318    g_rec_mutex_init(&edev->touch.blocked_mutex);
2319
2320    if (e_input)
2321      g_rec_mutex_lock(&e_input->libinput_mutex);
2322
2323    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
2324      {
2325         edev->caps |= E_INPUT_SEAT_KEYBOARD;
2326         _device_keyboard_setup(edev);
2327      }
2328
2329    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
2330      {
2331         edev->caps |= E_INPUT_SEAT_POINTER;
2332
2333         /* TODO: make this configurable */
2334         edev->mouse.threshold = 250;
2335
2336         b_input = seat->input;
2337         if (b_input->left_handed == EINA_TRUE)
2338           {
2339              if (libinput_device_config_left_handed_set(device, 1) !=
2340                  LIBINPUT_CONFIG_STATUS_SUCCESS)
2341                {
2342                   WRN("Failed to set left hand mode about device: %s\n",
2343                       libinput_device_get_name(device));
2344                }
2345           }
2346      }
2347
2348    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
2349      {
2350         int palm_code;
2351         edev->caps |= E_INPUT_SEAT_TOUCH;
2352         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
2353         if (libinput_device_touch_has_aux_data(device, palm_code))
2354           {
2355              libinput_device_touch_set_aux_data(device, palm_code);
2356           }
2357
2358         _device_touch_count_update(edev);
2359
2360         edev->touch.coords = calloc(1, sizeof(E_Input_Coord)*e_input_touch_max_count_get());
2361
2362         if  (!edev->touch.coords)
2363           ERR("Failed to allocate memory for touch coords !\n");
2364      }
2365
2366    output_name = libinput_device_get_output_name(device);
2367    if (output_name)
2368      {
2369         eina_stringshare_replace(&edev->output_name, output_name);
2370         ELOGF("E_INPUT_EVDEV", "Device has output_name:%s", NULL, output_name);
2371      }
2372
2373    libinput_device_set_user_data(device, edev);
2374    libinput_device_ref(device);
2375
2376    /* configure device */
2377    _device_configure(edev);
2378
2379    if (e_input)
2380      g_rec_mutex_unlock(&e_input->libinput_mutex);
2381
2382    return edev;
2383 }
2384
2385 void
2386 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
2387 {
2388    Ecore_Device *dev;
2389    E_Input *e_input = e_input_get();
2390
2391    EINA_SAFETY_ON_NULL_RETURN(edev);
2392
2393    ecore_thread_main_loop_begin();
2394
2395    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
2396      {
2397         g_mutex_lock(&edev->xkb.state_mutex);
2398         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
2399         g_mutex_unlock(&edev->xkb.state_mutex);
2400
2401         g_mutex_lock(&edev->xkb.keymap_mutex);
2402         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
2403         g_mutex_unlock(&edev->xkb.keymap_mutex);
2404      }
2405
2406    if (edev->ecore_dev) ecore_device_del(edev->ecore_dev);
2407    if (edev->ecore_dev_list)
2408      EINA_LIST_FREE(edev->ecore_dev_list, dev)
2409        {
2410           ecore_device_del(dev);
2411        }
2412
2413    if (edev->e_dev) g_object_unref(edev->e_dev);
2414    if (edev->e_dev_list)
2415      {
2416         GList *glist = edev->e_dev_list;
2417         E_Device *e_dev;
2418         while (glist)
2419           {
2420              e_dev = glist->data;
2421              g_object_unref(e_dev);
2422
2423              glist = g_list_next(glist);
2424           }
2425      }
2426    if (edev->path) eina_stringshare_del(edev->path);
2427    if (e_input) g_rec_mutex_lock(&e_input->libinput_mutex);
2428    if (edev->device) libinput_device_unref(edev->device);
2429    if (e_input) g_rec_mutex_unlock(&e_input->libinput_mutex);
2430    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
2431    if (edev->touch.coords)
2432      {
2433         free(edev->touch.coords);
2434         edev->touch.coords = NULL;
2435      }
2436    g_rec_mutex_clear(&edev->touch.raw_pressed_mutex);
2437    g_rec_mutex_clear(&edev->touch.blocked_mutex);
2438    eina_stringshare_del(edev->output_name);
2439
2440    ecore_thread_main_loop_end();
2441
2442    free(edev);
2443 }
2444
2445 Eina_Bool
2446 _e_input_evdev_event_process(struct libinput_event *event)
2447 {
2448    struct libinput_device *device;
2449    Eina_Bool ret = EINA_TRUE;
2450
2451    device = libinput_event_get_device(event);
2452    switch (libinput_event_get_type(event))
2453      {
2454       case LIBINPUT_EVENT_KEYBOARD_KEY:
2455         _device_handle_key(device, libinput_event_get_keyboard_event(event));
2456         break;
2457       case LIBINPUT_EVENT_POINTER_MOTION:
2458         _device_handle_pointer_motion(device,
2459                                       libinput_event_get_pointer_event(event));
2460         break;
2461       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
2462         _device_handle_pointer_motion_absolute(device,
2463                                                libinput_event_get_pointer_event(event));
2464         break;
2465       case LIBINPUT_EVENT_POINTER_BUTTON:
2466         _device_handle_button(device, libinput_event_get_pointer_event(event));
2467         break;
2468       case LIBINPUT_EVENT_POINTER_AXIS:
2469 #if !LIBINPUT_HAVE_SCROLL_VALUE_V120
2470         _device_handle_axis(device, libinput_event_get_pointer_event(event));
2471 #endif
2472         break;
2473 #if LIBINPUT_HAVE_SCROLL_VALUE_V120
2474       case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
2475         _device_handle_axis_v120(device, libinput_event_get_pointer_event(event), E_INPUT_AXIS_SOURCE_WHEEL);
2476         break;
2477       case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
2478         _device_handle_axis_v120(device, libinput_event_get_pointer_event(event), E_INPUT_AXIS_SOURCE_FINGER);
2479         break;
2480       case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
2481         _device_handle_axis_v120(device, libinput_event_get_pointer_event(event), E_INPUT_AXIS_SOURCE_CONTINUOUS);
2482         break;
2483 #endif
2484       case LIBINPUT_EVENT_TOUCH_DOWN:
2485         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
2486         break;
2487       case LIBINPUT_EVENT_TOUCH_MOTION:
2488         _device_handle_touch_motion(device,
2489                                     libinput_event_get_touch_event(event));
2490         break;
2491       case LIBINPUT_EVENT_TOUCH_UP:
2492         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
2493         break;
2494       case LIBINPUT_EVENT_TOUCH_CANCEL:
2495         _device_handle_touch_cancel(device, libinput_event_get_touch_event(event));
2496         break;
2497       case LIBINPUT_EVENT_TOUCH_FRAME:
2498         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
2499         break;
2500       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
2501         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
2502         break;
2503       default:
2504         ret = EINA_FALSE;
2505         break;
2506      }
2507
2508    return ret;
2509 }
2510
2511 /**
2512  * @brief Set the axis size of the given device.
2513  *
2514  * @param dev The device to set the axis size to.
2515  * @param w The width of the axis.
2516  * @param h The height of the axis.
2517  *
2518  * This function sets set the width @p w and height @p h of the axis
2519  * of device @p dev. If @p dev is a relative input device, a width and
2520  * height must set for it. If its absolute set the ioctl correctly, if
2521  * not, unsupported device.
2522  */
2523 EINTERN void
2524 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
2525 {
2526    const char *sysname;
2527    float cal[6];
2528    const char *device;
2529    Eina_List *devices;
2530    const char *vals;
2531    enum libinput_config_status status;
2532
2533    EINA_SAFETY_ON_NULL_RETURN(edev);
2534    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
2535
2536    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
2537        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
2538      return;
2539
2540    sysname = libinput_device_get_sysname(edev->device);
2541
2542    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
2543    if (eina_list_count(devices) < 1) return;
2544
2545    EINA_LIST_FREE(devices, device)
2546      {
2547         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
2548         if ((!vals) ||
2549             (sscanf(vals, "%f %f %f %f %f %f",
2550                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
2551           goto cont;
2552
2553         cal[2] /= w;
2554         cal[5] /= h;
2555
2556         status =
2557           libinput_device_config_calibration_set_matrix(edev->device, cal);
2558
2559         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
2560           ERR("Failed to apply calibration");
2561
2562 cont:
2563         eina_stringshare_del(device);
2564         continue;
2565      }
2566 }
2567
2568 EINTERN const char *
2569 e_input_evdev_name_get(E_Input_Evdev *evdev)
2570 {
2571    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
2572    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
2573
2574    return libinput_device_get_name(evdev->device);
2575 }
2576
2577 EINTERN const char *
2578 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
2579 {
2580    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
2581    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
2582
2583    return libinput_device_get_sysname(evdev->device);
2584 }
2585
2586 EINTERN Eina_Bool
2587 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
2588 {
2589    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2590    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2591
2592    edev->key_remap_enabled = enable;
2593
2594    if (enable == EINA_FALSE && edev->key_remap_hash)
2595      {
2596         eina_hash_free(edev->key_remap_hash);
2597         edev->key_remap_hash = NULL;
2598      }
2599
2600    return EINA_TRUE;
2601 }
2602
2603 EINTERN Eina_Bool
2604 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
2605 {
2606    int i;
2607
2608    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2609    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2610    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
2611    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
2612    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
2613    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
2614
2615    if (edev->key_remap_hash == NULL)
2616      edev->key_remap_hash = eina_hash_int32_new(NULL);
2617
2618    if (edev->key_remap_hash == NULL)
2619      {
2620         ERR("Failed to set remap key information : creating a hash is failed.");
2621         return EINA_FALSE;
2622      }
2623
2624    for (i = 0; i < num ; i++)
2625      {
2626         if (!from_keys[i] || !to_keys[i])
2627           {
2628              ERR("Failed to set remap key information : given arguments are invalid.");
2629              return EINA_FALSE;
2630           }
2631      }
2632
2633    for (i = 0; i < num ; i++)
2634      {
2635         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
2636      }
2637
2638    return EINA_TRUE;
2639 }
2640
2641 EINTERN int
2642 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
2643 {
2644    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
2645    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
2646 }
2647
2648 EINTERN Eina_Bool
2649 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
2650 {
2651    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2652    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2653
2654    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
2655        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
2656      return EINA_FALSE;
2657
2658    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
2659        LIBINPUT_CONFIG_STATUS_SUCCESS)
2660      {
2661         WRN("Failed to set input transformation about device: %s\n",
2662             libinput_device_get_name(edev->device));
2663         return EINA_FALSE;
2664      }
2665
2666    return EINA_TRUE;
2667 }
2668
2669 EINTERN Eina_Bool
2670 e_input_evdev_mouse_accel_speed_set(E_Input_Evdev *edev, double speed)
2671 {
2672    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2673    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2674
2675    if (!libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
2676      return EINA_FALSE;
2677
2678    if (!libinput_device_config_accel_is_available(edev->device))
2679      return EINA_FALSE;
2680
2681    if (libinput_device_config_accel_set_speed(edev->device, speed) !=
2682        LIBINPUT_CONFIG_STATUS_SUCCESS)
2683      {
2684         WRN("Failed to set mouse accel about device: %s\n",
2685             libinput_device_get_name(edev->device));
2686         return EINA_FALSE;
2687      }
2688
2689    return EINA_TRUE;
2690 }
2691
2692 EINTERN unsigned int
2693 e_input_evdev_touch_pressed_get(E_Input_Evdev *edev)
2694 {
2695    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, 0x0);
2696
2697    return edev->touch.pressed;
2698 }
2699
2700 const char *
2701 e_input_evdev_seatname_get(E_Input_Evdev *evdev)
2702 {
2703    struct libinput_seat *libinput_seat;
2704    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
2705    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
2706
2707    libinput_seat = libinput_device_get_seat(evdev->device);
2708
2709    return libinput_seat_get_logical_name(libinput_seat);
2710 }
2711
2712 Eina_Bool
2713 e_input_evdev_seatname_set(E_Input_Evdev *evdev, const char *seatname)
2714 {
2715    Eina_Bool res = EINA_FALSE;
2716    E_Input_Backend *input;
2717    E_Input *e_input = e_input_get();
2718
2719    EINA_SAFETY_ON_NULL_RETURN_VAL(seatname, EINA_FALSE);
2720    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, EINA_FALSE);
2721    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, EINA_FALSE);
2722
2723    if (libinput_device_set_seat_logical_name(evdev->device, seatname) == 0)
2724      {
2725         input = evdev->seat->input;
2726         if (!input) return EINA_FALSE;
2727
2728         if (e_input)
2729           g_rec_mutex_lock(&e_input->libinput_mutex);
2730
2731         if (libinput_dispatch(input->libinput) != 0)
2732           {
2733              ERR("Failed to dispatch libinput events: %m");
2734
2735              if (e_input)
2736                g_rec_mutex_unlock(&e_input->libinput_mutex);
2737
2738              return EINA_FALSE;
2739           }
2740
2741         if (e_input)
2742           g_rec_mutex_unlock(&e_input->libinput_mutex);
2743
2744         /* process pending events */
2745         _input_events_process(input);
2746         res = EINA_TRUE;
2747      }
2748
2749    return res;
2750 }