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