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