e_input: Use c11 atomic instead of mutex
[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_input_thread_mode_get())
803      {
804         e->dev = (Eo *)g_object_ref(e_dev);
805         e_input_event_add(input->event_source, state ? ECORE_EVENT_KEY_DOWN : ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
806      }
807    else
808      {
809         e->dev = ecore_device_ref(ecore_dev);
810         ecore_event_add(state ? ECORE_EVENT_KEY_DOWN : ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
811      }
812
813    if (tmp) free(tmp);
814 }
815
816 static void
817 _device_pointer_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event)
818 {
819    E_Input_Backend *input;
820    Ecore_Event_Mouse_Move *ev;
821    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
822    Eina_List *l;
823    int x = 0, y = 0, w = 0, h = 0;
824
825    if (!(input = edev->seat->input)) return;
826
827    ecore_thread_main_loop_begin();
828
829    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
830    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
831      {
832         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
833           {
834              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
835                {
836                   ecore_dev = data;
837                   break;
838                }
839              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
840                {
841                   detent_data = data;
842                }
843           }
844         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
845           {
846              ecore_dev = detent_data;
847           }
848      }
849    else
850      {
851         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
852         ecore_dev = edev->ecore_dev;
853      }
854
855    if (!ecore_dev)
856      {
857         ERR("Failed to get source ecore device from event !\n");
858         goto end;
859      }
860    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
861      {
862         /* Do not process detent device's move events. */
863         goto end;
864      }
865
866    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
867
868    _device_configured_size_get(edev, &x, &y, &w, &h);
869
870    if (edev->seat->ptr.ix < x)
871      edev->seat->ptr.dx = edev->seat->ptr.ix = x;
872    else if (edev->seat->ptr.ix >= (x + w))
873      edev->seat->ptr.dx = edev->seat->ptr.ix = (x + w - 1);
874
875    if (edev->seat->ptr.iy < y)
876      edev->seat->ptr.dy = edev->seat->ptr.iy = y;
877    else if (edev->seat->ptr.iy >= (y + h))
878      edev->seat->ptr.dy = edev->seat->ptr.iy = (y + h - 1);
879
880    edev->mouse.dx = edev->seat->ptr.dx;
881    edev->mouse.dy = edev->seat->ptr.dy;
882
883    ev->window = (Ecore_Window)input->dev->window;
884    ev->event_window = (Ecore_Window)input->dev->window;
885    ev->root_window = (Ecore_Window)input->dev->window;
886    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
887    ev->same_screen = 1;
888
889    _device_modifiers_update(edev);
890    ev->modifiers = edev->xkb.modifiers;
891
892    ev->x = edev->seat->ptr.ix;
893    ev->y = edev->seat->ptr.iy;
894    ev->root.x = ev->x;
895    ev->root.y = ev->y;
896
897    ev->multi.device = edev->mt_slot;
898    ev->multi.radius = 1;
899    ev->multi.radius_x = 1;
900    ev->multi.radius_y = 1;
901    ev->multi.pressure = 1.0;
902    ev->multi.angle = 0.0;
903    ev->multi.x = ev->x;
904    ev->multi.y = ev->y;
905    ev->multi.root.x = ev->x;
906    ev->multi.root.y = ev->y;
907
908    ev->dev = ecore_device_ref(ecore_dev);
909
910    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
911
912 end:
913    ecore_thread_main_loop_end();
914 }
915
916 void
917 _e_input_pointer_motion_post(E_Input_Evdev *edev)
918 {
919    _device_pointer_motion(edev, NULL);
920 }
921
922 static void
923 _device_pointer_relative_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event, double dx[2], double dy[2])
924 {
925    E_Input_Backend *input;
926    Ecore_Event_Mouse_Relative_Move *ev;
927    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
928    Eina_List *l;
929    E_Comp_Config *comp_conf;
930
931    if (!(input = edev->seat->input)) return;
932
933    ecore_thread_main_loop_begin();
934
935    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
936    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
937      {
938         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
939           {
940              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
941                {
942                   ecore_dev = data;
943                   break;
944                }
945              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
946                {
947                   detent_data = data;
948                }
949           }
950         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
951           {
952              ecore_dev = detent_data;
953           }
954      }
955    else
956      {
957         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
958         ecore_dev = edev->ecore_dev;
959      }
960
961    if (!ecore_dev)
962      {
963         ERR("Failed to get source ecore device from event !\n");
964         goto end;
965      }
966    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
967      {
968         /* Do not process detent device's move events. */
969         goto end;
970      }
971
972    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Relative_Move)))) return;
973
974    ev->window = (Ecore_Window)input->dev->window;
975    ev->event_window = (Ecore_Window)input->dev->window;
976    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
977
978    ev->modifiers = edev->xkb.modifiers;
979
980    ev->dx = (int)dx[0];
981    ev->dy = (int)dy[0];
982    ev->dx_unaccel = (int)dx[1];
983    ev->dy_unaccel = (int)dy[1];
984
985    ev->dev = ecore_device_ref(ecore_dev);
986
987    comp_conf = e_comp_config_get();
988    if (comp_conf && comp_conf->input_log_enable)
989      ELOGF("Mouse", "Relative Move (dx: %d, dy: %d, unaccel(%d, %d) device: %s)", NULL, ev->dx, ev->dx, ev->dx_unaccel, ev->dy_unaccel, ecore_device_name_get(ev->dev));
990
991    ecore_event_add(ECORE_EVENT_MOUSE_RELATIVE_MOVE, ev, _e_input_event_mouse_relative_move_cb_free, NULL);
992
993 end:
994    ecore_thread_main_loop_end();
995 }
996
997 static void
998 _device_pointer_motion_send(E_Input_Evdev *edev, struct libinput_event_pointer *event, double dx[2], double dy[2])
999 {
1000    e_input_relative_motion_cb func = e_input_relative_motion_handler_get();
1001    if (func)
1002      {
1003         //func(dx, dy, time_us);
1004         _device_pointer_relative_motion(edev, event, dx, dy);
1005      }
1006    else
1007      {
1008         double seat_dx, seat_dy, temp;
1009         if (edev->disable_acceleration)
1010           {
1011               seat_dx = dx[1];
1012               seat_dy = dy[1];
1013           }
1014         else
1015           {
1016               seat_dx = dx[0];
1017               seat_dy = dy[0];
1018           }
1019
1020         if (edev->seat->ptr.swap)
1021           {
1022               temp = seat_dx;
1023               seat_dx = seat_dy;
1024               seat_dy = temp;
1025           }
1026         if (edev->seat->ptr.invert_x)
1027           seat_dx *= -1;
1028         if (edev->seat->ptr.invert_y)
1029           seat_dy *= -1;
1030
1031         edev->seat->ptr.dx += seat_dx;
1032         edev->seat->ptr.dy += seat_dy;
1033
1034         edev->mouse.dx = edev->seat->ptr.dx;
1035         edev->mouse.dy = edev->seat->ptr.dy;
1036
1037         if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1038             floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1039           {
1040               return;
1041           }
1042
1043         edev->seat->ptr.ix = edev->seat->ptr.dx;
1044         edev->seat->ptr.iy = edev->seat->ptr.dy;
1045
1046         if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1047             (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1048           {
1049               return;
1050           }
1051
1052         _device_pointer_motion(edev, event);
1053      }
1054 }
1055
1056 static void
1057 _device_handle_pointer_motion(struct libinput_device *device, struct libinput_event_pointer *event)
1058 {
1059    E_Input_Evdev *edev;
1060    double delta_x[2]; /* delta_x[0] for accelerated, delta_x[1] for unaccelerated */
1061    double delta_y[2]; /* delta_y[0] for accelerated, delta_y[1] for unaccelerated */
1062
1063    if (!(edev = libinput_device_get_user_data(device)))
1064      {
1065         return;
1066      }
1067
1068    delta_x[0] = libinput_event_pointer_get_dx(event);
1069    delta_x[1] = libinput_event_pointer_get_dx_unaccelerated(event);
1070    delta_y[0] = libinput_event_pointer_get_dy(event);
1071    delta_y[1] = libinput_event_pointer_get_dy_unaccelerated(event);
1072
1073    _device_pointer_motion_send(edev, event, &delta_x[0], &delta_y[0]);
1074 }
1075
1076 static void
1077 _device_handle_pointer_motion_absolute(struct libinput_device *device, struct libinput_event_pointer *event)
1078 {
1079    E_Input_Evdev *edev;
1080    int w = 0, h = 0;
1081
1082    if (!(edev = libinput_device_get_user_data(device)))
1083      {
1084         return;
1085      }
1086
1087    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1088
1089    edev->mouse.dx = edev->seat->ptr.dx =
1090      libinput_event_pointer_get_absolute_x_transformed(event, w);
1091    edev->mouse.dy = edev->seat->ptr.dy =
1092      libinput_event_pointer_get_absolute_y_transformed(event, h);
1093
1094    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1095        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1096      {
1097         return;
1098      }
1099
1100    edev->seat->ptr.ix = edev->seat->ptr.dx;
1101    edev->seat->ptr.iy = edev->seat->ptr.dy;
1102
1103    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1104        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1105      {
1106         return;
1107      }
1108
1109    _device_pointer_motion(edev, event);
1110 }
1111
1112 static void
1113 _device_handle_button(struct libinput_device *device, struct libinput_event_pointer *event)
1114 {
1115    E_Input_Evdev *edev;
1116    E_Input_Backend *input;
1117    Ecore_Event_Mouse_Button *ev;
1118    enum libinput_button_state state;
1119    uint32_t button, timestamp;
1120    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
1121    Eina_List *l;
1122    E_Comp_Config *comp_conf = NULL;
1123
1124    if (!(edev = libinput_device_get_user_data(device)))
1125      {
1126         return;
1127      }
1128    if (!(input = edev->seat->input))
1129      {
1130         return;
1131      }
1132
1133    ecore_thread_main_loop_begin();
1134
1135    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1136    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1137      {
1138         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1139           {
1140              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
1141                {
1142                   ecore_dev = data;
1143                   break;
1144                }
1145              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
1146                {
1147                   detent_data = data;
1148                }
1149           }
1150         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
1151           {
1152              ecore_dev = detent_data;
1153           }
1154      }
1155    else
1156      {
1157         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
1158         ecore_dev = edev->ecore_dev;
1159      }
1160
1161    if (!ecore_dev)
1162      {
1163         ERR("Failed to get source ecore device from event !\n");
1164         goto end;
1165      }
1166
1167    state = libinput_event_pointer_get_button_state(event);
1168    button = libinput_event_pointer_get_button(event);
1169    timestamp = libinput_event_pointer_get_time(event);
1170
1171    button = ((button & 0x00F) + 1);
1172    if (button == 3) button = 2;
1173    else if (button == 2) button = 3;
1174
1175    void *blocked_client = atomic_load(&edev->seat->dev->blocked_client);
1176
1177    if (state)
1178      {
1179         if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1180             (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1181           {
1182
1183              ELOGF("Mouse", "Button Press (btn: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
1184                    button, ecore_device_name_get(ecore_dev), blocked_client,
1185                    edev->seat->dev->server_blocked);
1186              goto end;
1187           }
1188         else
1189           {
1190              edev->mouse.pressed_button |= (1 << button);
1191           }
1192      }
1193    else
1194      {
1195         if (!(edev->mouse.pressed_button & (1 << button)))
1196           {
1197              ELOGF("Mouse", "Button Release (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         edev->mouse.pressed_button &= ~(1 << button);
1203      }
1204
1205    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button))))
1206      {
1207         goto end;
1208      }
1209
1210    ev->window = (Ecore_Window)input->dev->window;
1211    ev->event_window = (Ecore_Window)input->dev->window;
1212    ev->root_window = (Ecore_Window)input->dev->window;
1213    ev->timestamp = timestamp;
1214    ev->same_screen = 1;
1215
1216    _device_modifiers_update(edev);
1217    ev->modifiers = edev->xkb.modifiers;
1218
1219    ev->x = edev->seat->ptr.ix;
1220    ev->y = edev->seat->ptr.iy;
1221    ev->root.x = ev->x;
1222    ev->root.y = ev->y;
1223
1224    ev->multi.device = edev->mt_slot;
1225    ev->multi.radius = 1;
1226    ev->multi.radius_x = 1;
1227    ev->multi.radius_y = 1;
1228    ev->multi.pressure = 1.0;
1229    ev->multi.angle = 0.0;
1230    ev->multi.x = ev->x;
1231    ev->multi.y = ev->y;
1232    ev->multi.root.x = ev->x;
1233    ev->multi.root.y = ev->y;
1234    ev->dev = ecore_device_ref(ecore_dev);
1235
1236    if (state)
1237      {
1238         unsigned int current;
1239
1240         current = timestamp;
1241         edev->mouse.did_double = EINA_FALSE;
1242         edev->mouse.did_triple = EINA_FALSE;
1243
1244         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
1245             (button == edev->mouse.prev_button))
1246           {
1247              edev->mouse.did_double = EINA_TRUE;
1248              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
1249                  (button == edev->mouse.last_button))
1250                {
1251                   edev->mouse.did_triple = EINA_TRUE;
1252                   edev->mouse.prev = 0;
1253                   edev->mouse.last = 0;
1254                   current = 0;
1255                }
1256           }
1257
1258         edev->mouse.last = edev->mouse.prev;
1259         edev->mouse.prev = current;
1260         edev->mouse.last_button = edev->mouse.prev_button;
1261         edev->mouse.prev_button = button;
1262      }
1263
1264    ev->buttons = button;
1265
1266    if (edev->mouse.did_double)
1267      ev->double_click = 1;
1268    if (edev->mouse.did_triple)
1269      ev->triple_click = 1;
1270
1271    comp_conf = e_comp_config_get();
1272    if (comp_conf && comp_conf->input_log_enable)
1273      ELOGF("Mouse", "Button %s (btn: %d, device: %s)", NULL, state?"Press":"Release", button, ecore_device_name_get(ev->dev));
1274
1275    if (state)
1276      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, _e_input_event_mouse_button_cb_free, NULL);
1277    else
1278      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, _e_input_event_mouse_button_cb_free, NULL);
1279
1280 end:
1281    ecore_thread_main_loop_end();
1282 }
1283
1284 #if !LIBINPUT_HAVE_SCROLL_VALUE_V120
1285 static int
1286 _axis_value_get(struct libinput_event_pointer *pointer_event, enum libinput_pointer_axis axis)
1287 {
1288    enum libinput_pointer_axis_source source;
1289    double value = 0.0, value_discrete = 0.0;
1290    int ret = 0;
1291    E_Comp_Config *comp_conf = NULL;
1292
1293    comp_conf = e_comp_config_get();
1294    source = libinput_event_pointer_get_axis_source(pointer_event);
1295    switch (source)
1296      {
1297       case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
1298         value_discrete = libinput_event_pointer_get_axis_value_discrete(pointer_event, axis);
1299         value = libinput_event_pointer_get_axis_value(pointer_event, axis);
1300         if (comp_conf && comp_conf->input_log_enable)
1301           {
1302              ELOGF("Axis", "SOURCE_WHEEL discrete: %lf (cf. value: %lf)", NULL, value_discrete, value);
1303           }
1304         ret = (int)value_discrete;
1305         break;
1306       case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
1307       case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
1308         value = libinput_event_pointer_get_axis_value(pointer_event, axis);
1309         if (comp_conf && comp_conf->input_log_enable)
1310           {
1311              ELOGF("Axis", "SOURCE_FINGER/CONTINUOUS value: %lf", NULL, value);
1312           }
1313         if (value >= E_INPUT_FINGER_SCROLL_THRESHOLD)
1314           ret = 1;
1315         else if (value <= E_INPUT_FINGER_SCROLL_THRESHOLD * (-1))
1316           ret = -1;
1317         else
1318           ret = 0;
1319         break;
1320       default:
1321         break;
1322      }
1323
1324    return ret;
1325 }
1326
1327 static void
1328 _device_handle_axis(struct libinput_device *device, struct libinput_event_pointer *event)
1329 {
1330    E_Input_Evdev *edev;
1331    E_Input_Backend *input;
1332    Ecore_Event_Mouse_Wheel *ev;
1333    uint32_t timestamp;
1334    enum libinput_pointer_axis axis;
1335    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
1336    Eina_List *l;
1337    E_Comp_Config *comp_conf = NULL;
1338    int direction = 0, z = 0;
1339
1340    if (!(edev = libinput_device_get_user_data(device)))
1341      {
1342         return;
1343      }
1344    if (!(input = edev->seat->input))
1345      {
1346         return;
1347      }
1348
1349    ecore_thread_main_loop_begin();
1350
1351    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1352    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1353      {
1354         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1355           {
1356              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
1357                {
1358                   ecore_dev = data;
1359                   break;
1360                }
1361              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
1362                {
1363                   detent_data = data;
1364                }
1365           }
1366         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
1367           {
1368              ecore_dev = detent_data;
1369           }
1370      }
1371    else
1372      {
1373         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
1374         ecore_dev = edev->ecore_dev;
1375      }
1376
1377    if (!ecore_dev)
1378      {
1379         ERR("Failed to get source ecore device from event !\n");
1380         goto end;
1381      }
1382
1383    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
1384    if (libinput_event_pointer_has_axis(event, axis))
1385      z = _axis_value_get(event, axis);
1386
1387    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
1388    if (libinput_event_pointer_has_axis(event, axis))
1389      {
1390         direction = 1;
1391         z = _axis_value_get(event, axis);
1392      }
1393
1394   if (z == 0)
1395     {
1396        ELOGF("Mouse", "Axis event is ignored since it has zero value", NULL);
1397        goto end;
1398     }
1399
1400    comp_conf = e_comp_config_get();
1401    if (comp_conf && comp_conf->e_wheel_click_angle)
1402      {
1403         z = (int)(z * comp_conf->e_wheel_click_angle);
1404      }
1405
1406    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1407        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1408      {
1409         void *blocked_client = atomic_load(&edev->seat->dev->blocked_client);
1410
1411         if (detent_data)
1412           ELOGF("Mouse", "Detent (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1413                 direction, z, blocked_client, edev->seat->dev->server_blocked);
1414         else
1415           ELOGF("Mouse", "Wheel (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1416                 direction, z, blocked_client, edev->seat->dev->server_blocked);
1417
1418         goto end;
1419      }
1420
1421    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
1422      {
1423         goto end;
1424      }
1425
1426    timestamp = libinput_event_pointer_get_time(event);
1427
1428    ev->window = (Ecore_Window)input->dev->window;
1429    ev->event_window = (Ecore_Window)input->dev->window;
1430    ev->root_window = (Ecore_Window)input->dev->window;
1431    ev->timestamp = timestamp;
1432    ev->same_screen = 1;
1433
1434    _device_modifiers_update(edev);
1435    ev->modifiers = edev->xkb.modifiers;
1436
1437    ev->x = edev->seat->ptr.ix;
1438    ev->y = edev->seat->ptr.iy;
1439    ev->root.x = ev->x;
1440    ev->root.y = ev->y;
1441    ev->z = z;
1442    ev->direction = direction;
1443
1444    if (comp_conf && comp_conf->input_log_enable)
1445      {
1446         if (detent_data)
1447           ELOGF("Mouse", "Detent (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1448         else
1449           ELOGF("Mouse", "Wheel (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1450      }
1451
1452
1453    ev->dev = ecore_device_ref(ecore_dev);
1454    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
1455
1456 end:
1457    ecore_thread_main_loop_end();
1458 }
1459 #endif
1460
1461 #if LIBINPUT_HAVE_SCROLL_VALUE_V120
1462 static int
1463 _scroll_value_get(struct libinput_event_pointer *pointer_event, enum libinput_pointer_axis axis, E_Input_Axis_Source source)
1464 {
1465    double value = 0.0;
1466    double value_v120 = 0.0;
1467    int ret;
1468    E_Comp_Config *comp_conf = NULL;
1469
1470    comp_conf = e_comp_config_get();
1471    switch (source)
1472      {
1473       case E_INPUT_AXIS_SOURCE_WHEEL:
1474         value_v120 = libinput_event_pointer_get_scroll_value_v120(pointer_event, axis);
1475         value = libinput_event_pointer_get_scroll_value(pointer_event, axis);
1476         if (comp_conf && comp_conf->input_log_enable)
1477           {
1478              ELOGF("Scroll", "SOURCE_WHEEL value_v120: %lf (cf. value: %lf)", NULL, value_v120, value);
1479           }
1480         if (((int)value_v120 % E_INPUT_POINTER_AXIS_DISCRETE_STEP) != 0)
1481           {
1482              ERR("Wheel movements should be multiples (or fractions) of 120!\n");
1483              ret = 0;
1484           }
1485         else
1486           {
1487              ret = value_v120 / E_INPUT_POINTER_AXIS_DISCRETE_STEP;
1488           }
1489         break;
1490       case E_INPUT_AXIS_SOURCE_FINGER:
1491       case E_INPUT_AXIS_SOURCE_CONTINUOUS:
1492         value = libinput_event_pointer_get_scroll_value(pointer_event, axis);
1493         if (comp_conf && comp_conf->input_log_enable)
1494           {
1495              ELOGF("Scroll", "SOURCE_FINGER/CONTINUOUS value: %lf", NULL, value);
1496           }
1497         if (value >= E_INPUT_FINGER_SCROLL_THRESHOLD)
1498           ret = 1;
1499         else if (value <= E_INPUT_FINGER_SCROLL_THRESHOLD * (-1))
1500           ret = -1;
1501         else
1502           ret = 0;
1503         break;
1504       default:
1505         break;
1506      }
1507
1508    return ret;
1509 }
1510
1511 static void
1512 _device_handle_axis_v120(struct libinput_device *device, struct libinput_event_pointer *event, E_Input_Axis_Source source)
1513 {
1514    E_Input_Evdev *edev;
1515    E_Input_Backend *input;
1516    Ecore_Event_Mouse_Wheel *ev;
1517    uint32_t timestamp;
1518    enum libinput_pointer_axis axis;
1519    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
1520    Eina_List *l;
1521    E_Comp_Config *comp_conf = NULL;
1522    int direction = 0, z = 0;
1523
1524    if (!(edev = libinput_device_get_user_data(device)))
1525      {
1526         return;
1527      }
1528    if (!(input = edev->seat->input))
1529      {
1530         return;
1531      }
1532
1533    ecore_thread_main_loop_begin();
1534
1535    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1536    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1537      {
1538         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1539           {
1540              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
1541                {
1542                   ecore_dev = data;
1543                   break;
1544                }
1545              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
1546                {
1547                   detent_data = data;
1548                }
1549           }
1550         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
1551           {
1552              ecore_dev = detent_data;
1553           }
1554      }
1555    else
1556      {
1557         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
1558         ecore_dev = edev->ecore_dev;
1559      }
1560
1561    if (!ecore_dev)
1562      {
1563         ERR("Failed to get source ecore device from event !\n");
1564         goto end;
1565      }
1566
1567    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
1568    if (libinput_event_pointer_has_axis(event, axis))
1569      z = _scroll_value_get(event, axis, source);
1570
1571    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
1572    if (libinput_event_pointer_has_axis(event, axis))
1573      {
1574         direction = 1;
1575         z = _scroll_value_get(event, axis, source);
1576      }
1577
1578    if (z == 0)
1579      {
1580         ELOGF("Mouse", "Scroll event is ignored since it has zero value", NULL);
1581         goto end;
1582      }
1583
1584    comp_conf = e_comp_config_get();
1585    if (comp_conf && comp_conf->e_wheel_click_angle)
1586      {
1587         z = (int)(z * comp_conf->e_wheel_click_angle);
1588      }
1589
1590    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1591        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1592      {
1593         void *blocked_client = atomic_load(&edev->seat->dev->blocked_client);
1594         if (detent_data)
1595           ELOGF("Mouse", "Detent (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1596                 direction, z, blocked_client, edev->seat->dev->server_blocked);
1597         else
1598           ELOGF("Mouse", "Wheel (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1599                 direction, z, blocked_client, edev->seat->dev->server_blocked);
1600
1601         goto end;
1602      }
1603
1604    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
1605      {
1606         goto end;
1607      }
1608
1609    timestamp = libinput_event_pointer_get_time(event);
1610
1611    ev->window = (Ecore_Window)input->dev->window;
1612    ev->event_window = (Ecore_Window)input->dev->window;
1613    ev->root_window = (Ecore_Window)input->dev->window;
1614    ev->timestamp = timestamp;
1615    ev->same_screen = 1;
1616
1617    _device_modifiers_update(edev);
1618    ev->modifiers = edev->xkb.modifiers;
1619
1620    ev->x = edev->seat->ptr.ix;
1621    ev->y = edev->seat->ptr.iy;
1622    ev->root.x = ev->x;
1623    ev->root.y = ev->y;
1624    ev->z = z;
1625    ev->direction = direction;
1626
1627    if (comp_conf && comp_conf->input_log_enable)
1628      {
1629         if (detent_data)
1630           ELOGF("Mouse", "Detent (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1631         else
1632           ELOGF("Mouse", "Wheel (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1633      }
1634
1635    ev->dev = ecore_device_ref(ecore_dev);
1636    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
1637
1638 end:
1639    ecore_thread_main_loop_end();
1640 }
1641 #endif
1642
1643 static void
1644 _device_handle_touch_event_send(E_Input_Evdev *edev, struct libinput_event_touch *event, int state)
1645 {
1646    E_Input_Backend *input;
1647    Ecore_Event_Mouse_Button *ev;
1648    uint32_t timestamp, button = 0;
1649    Ecore_Device *ecore_dev = NULL, *data;
1650    Eina_List *l;
1651
1652    if (!edev) return;
1653    if (!(input = edev->seat->input)) return;
1654
1655    ecore_thread_main_loop_begin();
1656
1657    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1658    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1659      {
1660         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1661           {
1662              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1663                {
1664                   ecore_dev = data;
1665                   break;
1666                }
1667           }
1668      }
1669    else
1670      {
1671         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1672         ecore_dev = edev->ecore_dev;
1673      }
1674
1675    if (!ecore_dev)
1676      {
1677         ERR("Failed to get source ecore device from event !\n");
1678         goto end;
1679      }
1680
1681    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) goto end;
1682
1683    timestamp = libinput_event_touch_get_time(event);
1684
1685    ev->window = (Ecore_Window)input->dev->window;
1686    ev->event_window = (Ecore_Window)input->dev->window;
1687    ev->root_window = (Ecore_Window)input->dev->window;
1688    ev->timestamp = timestamp;
1689    ev->same_screen = 1;
1690
1691    _device_modifiers_update(edev);
1692    ev->modifiers = edev->xkb.modifiers;
1693
1694    ev->x = edev->seat->ptr.ix;
1695    ev->y = edev->seat->ptr.iy;
1696    ev->root.x = ev->x;
1697    ev->root.y = ev->y;
1698
1699    ev->multi.device = edev->mt_slot;
1700    ev->multi.radius = 1;
1701    ev->multi.radius_x = 1;
1702    ev->multi.radius_y = 1;
1703    ev->multi.pressure = 1.0;
1704    ev->multi.angle = 0.0;
1705 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1706    if (libinput_event_get_type(libinput_event_touch_get_base_event(event))
1707        == LIBINPUT_EVENT_TOUCH_DOWN)
1708      {
1709         if (libinput_event_touch_has_minor(event))
1710           ev->multi.radius_x = libinput_event_touch_get_minor(event);
1711         if (libinput_event_touch_has_major(event))
1712           ev->multi.radius_y = libinput_event_touch_get_major(event);
1713         if (libinput_event_touch_has_pressure(event))
1714           ev->multi.pressure = libinput_event_touch_get_pressure(event);
1715         if (libinput_event_touch_has_orientation(event))
1716           ev->multi.angle = libinput_event_touch_get_orientation(event);
1717      }
1718 #endif
1719    ev->multi.x = ev->x;
1720    ev->multi.y = ev->y;
1721    ev->multi.root.x = ev->x;
1722    ev->multi.root.y = ev->y;
1723    ev->dev = ecore_device_ref(ecore_dev);
1724
1725    if (state == ECORE_EVENT_MOUSE_BUTTON_DOWN)
1726      {
1727         unsigned int current;
1728
1729         current = timestamp;
1730         edev->mouse.did_double = EINA_FALSE;
1731         edev->mouse.did_triple = EINA_FALSE;
1732
1733         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
1734             (button == edev->mouse.prev_button))
1735           {
1736              edev->mouse.did_double = EINA_TRUE;
1737              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
1738                  (button == edev->mouse.last_button))
1739                {
1740                   edev->mouse.did_triple = EINA_TRUE;
1741                   edev->mouse.prev = 0;
1742                   edev->mouse.last = 0;
1743                   current = 0;
1744                }
1745           }
1746
1747         edev->mouse.last = edev->mouse.prev;
1748         edev->mouse.prev = current;
1749         edev->mouse.last_button = edev->mouse.prev_button;
1750         edev->mouse.prev_button = button;
1751         edev->touch.pressed |= (1 << ev->multi.device);
1752      }
1753    else
1754      {
1755         edev->touch.pressed &= ~(1 << ev->multi.device);
1756      }
1757
1758    ev->buttons = ((button & 0x00F) + 1);
1759
1760    if (edev->mouse.did_double)
1761      ev->double_click = 1;
1762    if (edev->mouse.did_triple)
1763      ev->triple_click = 1;
1764
1765    ecore_event_add(state, ev, _e_input_event_mouse_button_cb_free, NULL);
1766
1767 end:
1768    ecore_thread_main_loop_end();
1769 }
1770
1771 static void
1772 _device_handle_touch_motion_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1773 {
1774    E_Input_Backend *input;
1775    Ecore_Event_Mouse_Move *ev;
1776    Ecore_Device *ecore_dev = NULL, *data;
1777    Eina_List *l;
1778
1779    if (!edev) return;
1780    if (!(input = edev->seat->input)) return;
1781
1782    ecore_thread_main_loop_begin();
1783
1784    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1785    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1786      {
1787         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1788           {
1789              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1790                {
1791                   ecore_dev = data;
1792                   break;
1793                }
1794           }
1795      }
1796    else
1797      {
1798         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1799         ecore_dev = edev->ecore_dev;
1800      }
1801
1802    if (!ecore_dev)
1803      {
1804         ERR("Failed to get source ecore device from event !\n");
1805         goto end;
1806      }
1807
1808    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) goto end;
1809
1810    ev->window = (Ecore_Window)input->dev->window;
1811    ev->event_window = (Ecore_Window)input->dev->window;
1812    ev->root_window = (Ecore_Window)input->dev->window;
1813    ev->timestamp = libinput_event_touch_get_time(event);
1814    ev->same_screen = 1;
1815
1816    _device_modifiers_update(edev);
1817    ev->modifiers = edev->xkb.modifiers;
1818
1819    ev->x = edev->seat->ptr.ix;
1820    ev->y = edev->seat->ptr.iy;
1821    ev->root.x = ev->x;
1822    ev->root.y = ev->y;
1823
1824    ev->multi.device = edev->mt_slot;
1825    ev->multi.radius = 1;
1826    ev->multi.radius_x = 1;
1827    ev->multi.radius_y = 1;
1828    ev->multi.pressure = 1.0;
1829    ev->multi.angle = 0.0;
1830 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1831    if (libinput_event_touch_has_minor(event))
1832      ev->multi.radius_x = libinput_event_touch_get_minor(event);
1833    if (libinput_event_touch_has_major(event))
1834      ev->multi.radius_y = libinput_event_touch_get_major(event);
1835    if (libinput_event_touch_has_pressure(event))
1836      ev->multi.pressure = libinput_event_touch_get_pressure(event);
1837    if (libinput_event_touch_has_orientation(event))
1838      ev->multi.angle = libinput_event_touch_get_orientation(event);
1839 #endif
1840    ev->multi.x = ev->x;
1841    ev->multi.y = ev->y;
1842    ev->multi.root.x = ev->x;
1843    ev->multi.root.y = ev->y;
1844    ev->dev = ecore_device_ref(ecore_dev);
1845
1846    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
1847
1848 end:
1849    ecore_thread_main_loop_end();
1850 }
1851
1852 static void
1853 _device_handle_touch_cancel_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1854 {
1855    E_Input_Backend *input;
1856    Ecore_Event_Mouse_Button *ev;
1857    uint32_t timestamp, button = 0;
1858    Ecore_Device *ecore_dev = NULL, *data;
1859    Eina_List *l;
1860
1861    if (!edev) return;
1862    if (!(input = edev->seat->input)) return;
1863
1864    ecore_thread_main_loop_begin();
1865
1866    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1867    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1868      {
1869         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1870           {
1871              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1872                {
1873                   ecore_dev = data;
1874                   break;
1875                }
1876           }
1877      }
1878    else
1879      {
1880         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1881         ecore_dev = edev->ecore_dev;
1882      }
1883
1884    if (!ecore_dev)
1885      {
1886         ERR("Failed to get source ecore device from event !\n");
1887         goto end;
1888      }
1889
1890    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) goto end;
1891
1892    timestamp = libinput_event_touch_get_time(event);
1893
1894    ev->window = (Ecore_Window)input->dev->window;
1895    ev->event_window = (Ecore_Window)input->dev->window;
1896    ev->root_window = (Ecore_Window)input->dev->window;
1897    ev->timestamp = timestamp;
1898    ev->same_screen = 1;
1899
1900    ev->x = edev->seat->ptr.ix;
1901    ev->y = edev->seat->ptr.iy;
1902    ev->root.x = ev->x;
1903    ev->root.y = ev->y;
1904
1905    ev->multi.device = edev->mt_slot;
1906    ev->multi.radius = 1;
1907    ev->multi.radius_x = 1;
1908    ev->multi.radius_y = 1;
1909    ev->multi.pressure = 1.0;
1910    ev->multi.angle = 0.0;
1911    ev->multi.x = ev->x;
1912    ev->multi.y = ev->y;
1913    ev->multi.root.x = ev->x;
1914    ev->multi.root.y = ev->y;
1915
1916    edev->touch.pressed &= ~(1 << ev->multi.device);
1917
1918    ev->buttons = ((button & 0x00F) + 1);
1919    ev->dev = ecore_device_ref(ecore_dev);
1920
1921    ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_CANCEL, ev, _e_input_event_mouse_button_cb_free, NULL);
1922
1923 end:
1924    ecore_thread_main_loop_end();
1925 }
1926
1927 static void
1928 _device_configured_size_get(E_Input_Evdev *edev, int *x, int *y, int *w, int *h)
1929 {
1930    E_Output *output = NULL;
1931
1932    EINA_SAFETY_ON_NULL_RETURN(edev);
1933
1934    if (!edev->output_configured)
1935      {
1936         if (edev->output_name)
1937           {
1938              output = e_output_find(edev->output_name);
1939              if (output)
1940                {
1941                   edev->mouse.minx = output->config.geom.x;
1942                   edev->mouse.miny = output->config.geom.y;
1943                   edev->mouse.maxw = output->config.geom.w;
1944                   edev->mouse.maxh = output->config.geom.h;
1945                   if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
1946                     {
1947                        edev->seat->ptr.dx = (double)(edev->mouse.maxw / 2);
1948                        edev->seat->ptr.dy = (double)(edev->mouse.maxh / 2);
1949                        edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
1950                        edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
1951                        edev->mouse.dx = edev->seat->ptr.dx;
1952                        edev->mouse.dy = edev->seat->ptr.dy;
1953                     }
1954                }
1955           }
1956
1957           edev->output_configured = EINA_TRUE;
1958           ELOGF("E_INPUT_EVDEV", "Device is configured by output x:%d,y:%d (w:%d, h:%d)",
1959                 NULL, edev->mouse.minx, edev->mouse.miny, edev->mouse.maxw, edev->mouse.maxh);
1960      }
1961    if (x) *x = edev->mouse.minx;
1962    if (y) *y = edev->mouse.miny;
1963    if (w) *w = edev->mouse.maxw;
1964    if (h) *h = edev->mouse.maxh;
1965 }
1966
1967 static void
1968 _device_handle_touch_down(struct libinput_device *device, struct libinput_event_touch *event)
1969 {
1970    E_Input_Evdev *edev;
1971    int x = 0, y = 0, w = 0, h = 0;
1972    E_Comp_Config *comp_conf = NULL;
1973
1974    if (!(edev = libinput_device_get_user_data(device)))
1975      {
1976         return;
1977      }
1978
1979    _device_configured_size_get(edev, &x, &y, &w, &h);
1980
1981    edev->mouse.dx = edev->seat->ptr.ix = edev->seat->ptr.dx =
1982      x + libinput_event_touch_get_x_transformed(event, w);
1983    edev->mouse.dy = edev->seat->ptr.iy = edev->seat->ptr.dy =
1984      y + libinput_event_touch_get_y_transformed(event, h);
1985
1986    edev->mt_slot = libinput_event_touch_get_slot(event);
1987    if (edev->mt_slot < 0)
1988      {
1989         /* FIXME: The single touch device return slot id -1
1990          *        But currently we have no API to distinguish multi touch or single touch
1991          *        After libinput 1.11 version, libinput provides get_touch_count API,
1992          *        so we can distinguish multi touch device or single touch device.
1993          */
1994         if (edev->mt_slot == -1)
1995           edev->mt_slot = 0;
1996         else
1997           {
1998              WRN("%d slot touch down events are not supported\n", edev->mt_slot);
1999              return;
2000           }
2001      }
2002
2003    if (edev->mt_slot < e_input_touch_max_count_get())
2004      {
2005         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
2006         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
2007      }
2008
2009    comp_conf = e_comp_config_get();
2010    if (comp_conf && comp_conf->input_log_enable)
2011      ELOGF("Touch", "Down (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
2012
2013    atomic_fetch_or(&edev->touch.raw_pressed, (1 << edev->mt_slot));
2014
2015    if (edev->touch.blocked)
2016      {
2017         void *blocked_client = atomic_load(&edev->seat->dev->blocked_client);
2018         ELOGF("Touch", "Down (id: %d, x: %d, y: %d) is blocked by %p, server: 0x%x", NULL,
2019               edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, blocked_client,
2020               edev->seat->dev->server_blocked);
2021         return;
2022      }
2023
2024    _device_handle_touch_motion_send(edev, event);
2025    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_DOWN);
2026 }
2027
2028 static void
2029 _device_handle_touch_motion(struct libinput_device *device, struct libinput_event_touch *event)
2030 {
2031    E_Input_Evdev *edev;
2032    int x = 0, y = 0, w = 0, h = 0;
2033
2034    if (!(edev = libinput_device_get_user_data(device)))
2035      {
2036         return;
2037      }
2038
2039    _device_configured_size_get(edev, &x, &y, &w, &h);
2040
2041    edev->mouse.dx = edev->seat->ptr.dx =
2042      x + libinput_event_touch_get_x_transformed(event, w);
2043    edev->mouse.dy = edev->seat->ptr.dy =
2044      y + libinput_event_touch_get_y_transformed(event, h);
2045
2046    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
2047        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
2048      {
2049         return;
2050      }
2051
2052    edev->seat->ptr.ix = edev->seat->ptr.dx;
2053    edev->seat->ptr.iy = edev->seat->ptr.dy;
2054
2055    edev->mt_slot = libinput_event_touch_get_slot(event);
2056    if (edev->mt_slot < 0)
2057      {
2058         /* FIXME: The single touch device return slot id -1
2059          *        But currently we have no API to distinguish multi touch or single touch
2060          *        After libinput 1.11 version, libinput provides get_touch_count API,
2061          *        so we can distinguish multi touch device or single touch device.
2062          */
2063         if (edev->mt_slot == -1)
2064           edev->mt_slot = 0;
2065         else
2066           {
2067              WRN("%d slot touch motion events are not supported\n", edev->mt_slot);
2068              return;
2069           }
2070      }
2071
2072    if (edev->mt_slot < e_input_touch_max_count_get())
2073      {
2074         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
2075         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
2076      }
2077
2078    if (!(edev->touch.pressed & (1 << edev->mt_slot)))
2079      {
2080         if (edev->touch.blocked)
2081           {
2082              return;
2083           }
2084      }
2085
2086    _device_handle_touch_motion_send(edev, event);
2087 }
2088
2089 static void
2090 _device_handle_touch_up(struct libinput_device *device, struct libinput_event_touch *event)
2091 {
2092    E_Input_Evdev *edev;
2093    E_Comp_Config *comp_conf = NULL;
2094
2095    if (!(edev = libinput_device_get_user_data(device)))
2096      {
2097         return;
2098      }
2099
2100    edev->mt_slot = libinput_event_touch_get_slot(event);
2101    if (edev->mt_slot < 0)
2102      {
2103         /* FIXME: The single touch device return slot id -1
2104          *        But currently we have no API to distinguish multi touch or single touch
2105          *        After libinput 1.11 version, libinput provides get_touch_count API,
2106          *        so we can distinguish multi touch device or single touch device.
2107          */
2108         if (edev->mt_slot == -1)
2109           edev->mt_slot = 0;
2110         else
2111           {
2112              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
2113              return;
2114           }
2115      }
2116
2117    if (edev->mt_slot < e_input_touch_max_count_get())
2118      {
2119         edev->mouse.dx = edev->seat->ptr.dx = edev->seat->ptr.ix =
2120           edev->touch.coords[edev->mt_slot].x;
2121         edev->mouse.dy = edev->seat->ptr.dy = edev->seat->ptr.iy =
2122           edev->touch.coords[edev->mt_slot].y;
2123      }
2124
2125    comp_conf = e_comp_config_get();
2126    if (comp_conf && comp_conf->input_log_enable)
2127      ELOGF("Touch", "Up (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
2128
2129    atomic_fetch_and(&edev->touch.raw_pressed, ~(1 << edev->mt_slot));
2130
2131    if (edev->touch.blocked)
2132      {
2133         if (!(edev->touch.pressed & (1 << edev->mt_slot)))
2134           {
2135              void *blocked_client = atomic_load(&edev->seat->dev->blocked_client);
2136              ELOGF("Touch", "Up (id: %d, x: %d, y: %d) is blocked by %p, server(0x%x)", NULL,
2137                    edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, blocked_client,
2138                    edev->seat->dev->server_blocked);
2139
2140              if (edev->touch.raw_pressed == 0x0)
2141                {
2142                   edev->touch.blocked = EINA_FALSE;
2143                }
2144              return;
2145           }
2146      }
2147
2148    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_UP);
2149 }
2150
2151 static void
2152 _device_handle_touch_cancel(struct libinput_device *device, struct libinput_event_touch *event)
2153 {
2154    E_Input_Evdev *edev;
2155    E_Comp_Config *comp_conf = NULL;
2156
2157    if (!(edev = libinput_device_get_user_data(device)))
2158      {
2159         return;
2160      }
2161
2162    edev->mt_slot = libinput_event_touch_get_slot(event);
2163    if (edev->mt_slot < 0)
2164      {
2165         /* FIXME: The single touch device return slot id -1
2166          *        But currently we have no API to distinguish multi touch or single touch
2167          *        After libinput 1.11 version, libinput provides get_touch_count API,
2168          *        so we can distinguish multi touch device or single touch device.
2169          */
2170         if (edev->mt_slot == -1)
2171           edev->mt_slot = 0;
2172         else
2173           {
2174              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
2175              return;
2176           }
2177      }
2178
2179    comp_conf = e_comp_config_get();
2180    if (comp_conf && comp_conf->input_log_enable)
2181      ELOGF("Touch", "cancel (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
2182
2183    _device_handle_touch_cancel_send(edev, event);
2184 }
2185
2186
2187 static void
2188 _device_handle_touch_frame(struct libinput_device *device EINA_UNUSED, struct libinput_event_touch *event EINA_UNUSED)
2189 {
2190    /* DBG("Unhandled Touch Frame Event"); */
2191 }
2192
2193 static void
2194 _e_input_aux_data_event_free(void *user_data EINA_UNUSED, void *ev)
2195 {
2196    Ecore_Event_Axis_Update *e = (Ecore_Event_Axis_Update *)ev;
2197
2198    if (e->axis) free(e->axis);
2199    if (e->dev) ecore_device_unref(e->dev);
2200
2201    free(e);
2202 }
2203
2204 static void
2205 _device_handle_touch_aux_data(struct libinput_device *device, struct libinput_event_touch_aux_data *event)
2206 {
2207    E_Input_Evdev *edev;
2208    E_Input_Backend *input;
2209    Ecore_Event_Axis_Update *ev;
2210    Ecore_Axis *axis;
2211    Ecore_Device *ecore_dev = NULL, *data;
2212    Eina_List *l;
2213    E_Comp_Config *comp_conf;
2214
2215    ecore_thread_main_loop_begin();
2216
2217    if (libinput_event_touch_aux_data_get_type(event) != LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM &&
2218        libinput_event_touch_aux_data_get_value(event) > 0)
2219       goto end;
2220
2221    if (!(edev = libinput_device_get_user_data(device))) goto end;
2222    if (!(input = edev->seat->input)) goto end;
2223
2224    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
2225    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
2226      {
2227         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
2228           {
2229              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
2230                {
2231                   ecore_dev = data;
2232                   break;
2233                }
2234           }
2235      }
2236    else
2237      {
2238         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
2239         ecore_dev = edev->ecore_dev;
2240      }
2241
2242    if (!ecore_dev)
2243      {
2244         ERR("Failed to get source ecore device from event !\n");
2245         goto end;
2246      }
2247
2248    if (!(ev = calloc(1, sizeof(Ecore_Event_Axis_Update)))) goto end;
2249
2250    ev->window = (Ecore_Window)input->dev->window;
2251    ev->event_window = (Ecore_Window)input->dev->window;
2252    ev->root_window = (Ecore_Window)input->dev->window;
2253    ev->timestamp = libinput_event_touch_aux_data_get_time(event);
2254
2255    axis = (Ecore_Axis *)calloc(1, sizeof(Ecore_Axis));
2256    if (axis)
2257      {
2258         axis->label = ECORE_AXIS_LABEL_TOUCH_PALM;
2259         axis->value = libinput_event_touch_aux_data_get_value(event);
2260         ev->naxis = 1;
2261      }
2262    ev->axis = axis;
2263
2264    comp_conf = e_comp_config_get();
2265    if (comp_conf && comp_conf->input_log_enable)
2266      ELOGF("Touch", "Axis (label: %d, value: %lf)", NULL, axis?axis->label:-1, axis?axis->value:0.0);
2267
2268    ev->dev = ecore_device_ref(ecore_dev);
2269
2270    ecore_event_add(ECORE_EVENT_AXIS_UPDATE, ev, _e_input_aux_data_event_free, NULL);
2271
2272 end:
2273    ecore_thread_main_loop_end();
2274 }
2275
2276 E_Input_Evdev *
2277 _e_input_evdev_device_create(E_Input_Seat *seat, struct libinput_device *device)
2278 {
2279    E_Input_Evdev *edev;
2280    E_Input_Backend *b_input;
2281    const char *output_name;
2282
2283    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
2284
2285    E_Input *e_input = e_input_get();
2286
2287    /* try to allocate space for new evdev */
2288    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
2289
2290    edev->seat = seat;
2291    edev->device = device;
2292    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
2293
2294    if (e_input)
2295      g_rec_mutex_lock(&e_input->libinput_mutex);
2296
2297    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
2298      {
2299         edev->caps |= E_INPUT_SEAT_KEYBOARD;
2300         _device_keyboard_setup(edev);
2301      }
2302
2303    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
2304      {
2305         edev->caps |= E_INPUT_SEAT_POINTER;
2306
2307         /* TODO: make this configurable */
2308         edev->mouse.threshold = 250;
2309
2310         b_input = seat->input;
2311         if (b_input->left_handed == EINA_TRUE)
2312           {
2313              if (libinput_device_config_left_handed_set(device, 1) !=
2314                  LIBINPUT_CONFIG_STATUS_SUCCESS)
2315                {
2316                   WRN("Failed to set left hand mode about device: %s\n",
2317                       libinput_device_get_name(device));
2318                }
2319           }
2320      }
2321
2322    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
2323      {
2324         int palm_code;
2325         edev->caps |= E_INPUT_SEAT_TOUCH;
2326         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
2327         if (libinput_device_touch_has_aux_data(device, palm_code))
2328           {
2329              libinput_device_touch_set_aux_data(device, palm_code);
2330           }
2331
2332         _device_touch_count_update(edev);
2333
2334         edev->touch.coords = calloc(1, sizeof(E_Input_Coord)*e_input_touch_max_count_get());
2335
2336         if  (!edev->touch.coords)
2337           ERR("Failed to allocate memory for touch coords !\n");
2338      }
2339
2340    output_name = libinput_device_get_output_name(device);
2341    if (output_name)
2342      {
2343         eina_stringshare_replace(&edev->output_name, output_name);
2344         ELOGF("E_INPUT_EVDEV", "Device has output_name:%s", NULL, output_name);
2345      }
2346
2347    libinput_device_set_user_data(device, edev);
2348    libinput_device_ref(device);
2349
2350    /* configure device */
2351    _device_configure(edev);
2352
2353    if (e_input)
2354      g_rec_mutex_unlock(&e_input->libinput_mutex);
2355
2356    return edev;
2357 }
2358
2359 void
2360 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
2361 {
2362    Ecore_Device *dev;
2363    E_Input *e_input = e_input_get();
2364
2365    EINA_SAFETY_ON_NULL_RETURN(edev);
2366
2367    ecore_thread_main_loop_begin();
2368
2369    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
2370      {
2371         g_mutex_lock(&edev->xkb.state_mutex);
2372         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
2373         g_mutex_unlock(&edev->xkb.state_mutex);
2374
2375         g_mutex_lock(&edev->xkb.keymap_mutex);
2376         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
2377         g_mutex_unlock(&edev->xkb.keymap_mutex);
2378      }
2379
2380    if (edev->ecore_dev) ecore_device_del(edev->ecore_dev);
2381    if (edev->ecore_dev_list)
2382      EINA_LIST_FREE(edev->ecore_dev_list, dev)
2383        {
2384           ecore_device_del(dev);
2385        }
2386
2387    if (edev->e_dev) g_object_unref(edev->e_dev);
2388    if (edev->e_dev_list)
2389      {
2390         GList *glist = edev->e_dev_list;
2391         E_Device *e_dev;
2392         while (glist)
2393           {
2394              e_dev = glist->data;
2395              g_object_unref(e_dev);
2396
2397              glist = g_list_next(glist);
2398           }
2399      }
2400    if (edev->path) eina_stringshare_del(edev->path);
2401    if (e_input) g_rec_mutex_lock(&e_input->libinput_mutex);
2402    if (edev->device) libinput_device_unref(edev->device);
2403    if (e_input) g_rec_mutex_unlock(&e_input->libinput_mutex);
2404    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
2405    if (edev->touch.coords)
2406      {
2407         free(edev->touch.coords);
2408         edev->touch.coords = NULL;
2409      }
2410    eina_stringshare_del(edev->output_name);
2411
2412    ecore_thread_main_loop_end();
2413
2414    free(edev);
2415 }
2416
2417 Eina_Bool
2418 _e_input_evdev_event_process(struct libinput_event *event)
2419 {
2420    struct libinput_device *device;
2421    Eina_Bool ret = EINA_TRUE;
2422
2423    device = libinput_event_get_device(event);
2424    switch (libinput_event_get_type(event))
2425      {
2426       case LIBINPUT_EVENT_KEYBOARD_KEY:
2427         _device_handle_key(device, libinput_event_get_keyboard_event(event));
2428         break;
2429       case LIBINPUT_EVENT_POINTER_MOTION:
2430         _device_handle_pointer_motion(device,
2431                                       libinput_event_get_pointer_event(event));
2432         break;
2433       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
2434         _device_handle_pointer_motion_absolute(device,
2435                                                libinput_event_get_pointer_event(event));
2436         break;
2437       case LIBINPUT_EVENT_POINTER_BUTTON:
2438         _device_handle_button(device, libinput_event_get_pointer_event(event));
2439         break;
2440       case LIBINPUT_EVENT_POINTER_AXIS:
2441 #if !LIBINPUT_HAVE_SCROLL_VALUE_V120
2442         _device_handle_axis(device, libinput_event_get_pointer_event(event));
2443 #endif
2444         break;
2445 #if LIBINPUT_HAVE_SCROLL_VALUE_V120
2446       case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
2447         _device_handle_axis_v120(device, libinput_event_get_pointer_event(event), E_INPUT_AXIS_SOURCE_WHEEL);
2448         break;
2449       case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
2450         _device_handle_axis_v120(device, libinput_event_get_pointer_event(event), E_INPUT_AXIS_SOURCE_FINGER);
2451         break;
2452       case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
2453         _device_handle_axis_v120(device, libinput_event_get_pointer_event(event), E_INPUT_AXIS_SOURCE_CONTINUOUS);
2454         break;
2455 #endif
2456       case LIBINPUT_EVENT_TOUCH_DOWN:
2457         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
2458         break;
2459       case LIBINPUT_EVENT_TOUCH_MOTION:
2460         _device_handle_touch_motion(device,
2461                                     libinput_event_get_touch_event(event));
2462         break;
2463       case LIBINPUT_EVENT_TOUCH_UP:
2464         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
2465         break;
2466       case LIBINPUT_EVENT_TOUCH_CANCEL:
2467         _device_handle_touch_cancel(device, libinput_event_get_touch_event(event));
2468         break;
2469       case LIBINPUT_EVENT_TOUCH_FRAME:
2470         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
2471         break;
2472       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
2473         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
2474         break;
2475       default:
2476         ret = EINA_FALSE;
2477         break;
2478      }
2479
2480    return ret;
2481 }
2482
2483 /**
2484  * @brief Set the axis size of the given device.
2485  *
2486  * @param dev The device to set the axis size to.
2487  * @param w The width of the axis.
2488  * @param h The height of the axis.
2489  *
2490  * This function sets set the width @p w and height @p h of the axis
2491  * of device @p dev. If @p dev is a relative input device, a width and
2492  * height must set for it. If its absolute set the ioctl correctly, if
2493  * not, unsupported device.
2494  */
2495 EINTERN void
2496 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
2497 {
2498    const char *sysname;
2499    float cal[6];
2500    const char *device;
2501    Eina_List *devices;
2502    const char *vals;
2503    enum libinput_config_status status;
2504
2505    EINA_SAFETY_ON_NULL_RETURN(edev);
2506    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
2507
2508    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
2509        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
2510      return;
2511
2512    sysname = libinput_device_get_sysname(edev->device);
2513
2514    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
2515    if (eina_list_count(devices) < 1) return;
2516
2517    EINA_LIST_FREE(devices, device)
2518      {
2519         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
2520         if ((!vals) ||
2521             (sscanf(vals, "%f %f %f %f %f %f",
2522                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
2523           goto cont;
2524
2525         cal[2] /= w;
2526         cal[5] /= h;
2527
2528         status =
2529           libinput_device_config_calibration_set_matrix(edev->device, cal);
2530
2531         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
2532           ERR("Failed to apply calibration");
2533
2534 cont:
2535         eina_stringshare_del(device);
2536         continue;
2537      }
2538 }
2539
2540 EINTERN const char *
2541 e_input_evdev_name_get(E_Input_Evdev *evdev)
2542 {
2543    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
2544    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
2545
2546    return libinput_device_get_name(evdev->device);
2547 }
2548
2549 EINTERN const char *
2550 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
2551 {
2552    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
2553    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
2554
2555    return libinput_device_get_sysname(evdev->device);
2556 }
2557
2558 EINTERN Eina_Bool
2559 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
2560 {
2561    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2562    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2563
2564    edev->key_remap_enabled = enable;
2565
2566    if (enable == EINA_FALSE && edev->key_remap_hash)
2567      {
2568         eina_hash_free(edev->key_remap_hash);
2569         edev->key_remap_hash = NULL;
2570      }
2571
2572    return EINA_TRUE;
2573 }
2574
2575 EINTERN Eina_Bool
2576 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
2577 {
2578    int i;
2579
2580    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2581    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2582    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
2583    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
2584    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
2585    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
2586
2587    if (edev->key_remap_hash == NULL)
2588      edev->key_remap_hash = eina_hash_int32_new(NULL);
2589
2590    if (edev->key_remap_hash == NULL)
2591      {
2592         ERR("Failed to set remap key information : creating a hash is failed.");
2593         return EINA_FALSE;
2594      }
2595
2596    for (i = 0; i < num ; i++)
2597      {
2598         if (!from_keys[i] || !to_keys[i])
2599           {
2600              ERR("Failed to set remap key information : given arguments are invalid.");
2601              return EINA_FALSE;
2602           }
2603      }
2604
2605    for (i = 0; i < num ; i++)
2606      {
2607         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
2608      }
2609
2610    return EINA_TRUE;
2611 }
2612
2613 EINTERN int
2614 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
2615 {
2616    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
2617    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
2618 }
2619
2620 EINTERN Eina_Bool
2621 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
2622 {
2623    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2624    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2625
2626    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
2627        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
2628      return EINA_FALSE;
2629
2630    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
2631        LIBINPUT_CONFIG_STATUS_SUCCESS)
2632      {
2633         WRN("Failed to set input transformation about device: %s\n",
2634             libinput_device_get_name(edev->device));
2635         return EINA_FALSE;
2636      }
2637
2638    return EINA_TRUE;
2639 }
2640
2641 EINTERN Eina_Bool
2642 e_input_evdev_mouse_accel_speed_set(E_Input_Evdev *edev, double speed)
2643 {
2644    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2645    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2646
2647    if (!libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
2648      return EINA_FALSE;
2649
2650    if (!libinput_device_config_accel_is_available(edev->device))
2651      return EINA_FALSE;
2652
2653    if (libinput_device_config_accel_set_speed(edev->device, speed) !=
2654        LIBINPUT_CONFIG_STATUS_SUCCESS)
2655      {
2656         WRN("Failed to set mouse accel about device: %s\n",
2657             libinput_device_get_name(edev->device));
2658         return EINA_FALSE;
2659      }
2660
2661    return EINA_TRUE;
2662 }
2663
2664 EINTERN unsigned int
2665 e_input_evdev_touch_pressed_get(E_Input_Evdev *edev)
2666 {
2667    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, 0x0);
2668
2669    return edev->touch.pressed;
2670 }
2671
2672 const char *
2673 e_input_evdev_seatname_get(E_Input_Evdev *evdev)
2674 {
2675    struct libinput_seat *libinput_seat;
2676    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
2677    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
2678
2679    libinput_seat = libinput_device_get_seat(evdev->device);
2680
2681    return libinput_seat_get_logical_name(libinput_seat);
2682 }
2683
2684 Eina_Bool
2685 e_input_evdev_seatname_set(E_Input_Evdev *evdev, const char *seatname)
2686 {
2687    Eina_Bool res = EINA_FALSE;
2688    E_Input_Backend *input;
2689    E_Input *e_input = e_input_get();
2690
2691    EINA_SAFETY_ON_NULL_RETURN_VAL(seatname, EINA_FALSE);
2692    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, EINA_FALSE);
2693    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, EINA_FALSE);
2694
2695    if (libinput_device_set_seat_logical_name(evdev->device, seatname) == 0)
2696      {
2697         input = evdev->seat->input;
2698         if (!input) return EINA_FALSE;
2699
2700         if (e_input)
2701           g_rec_mutex_lock(&e_input->libinput_mutex);
2702
2703         if (libinput_dispatch(input->libinput) != 0)
2704           {
2705              ERR("Failed to dispatch libinput events: %m");
2706
2707              if (e_input)
2708                g_rec_mutex_unlock(&e_input->libinput_mutex);
2709
2710              return EINA_FALSE;
2711           }
2712
2713         if (e_input)
2714           g_rec_mutex_unlock(&e_input->libinput_mutex);
2715
2716         /* process pending events */
2717         _input_events_process(input);
2718         res = EINA_TRUE;
2719      }
2720
2721    return res;
2722 }