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