e_input_evdev: fix a svace issue
[platform/upstream/enlightenment.git] / src / bin / e_input_evdev.c
1 #include "e.h"
2 #include "e_input_private.h"
3
4 static void  _device_modifiers_update(E_Input_Evdev *edev);
5
6 void
7 _device_calibration_set(E_Input_Evdev *edev)
8 {
9    E_Output *output;
10    int w = 0, h = 0;
11    int temp;
12
13    output = e_comp_screen_primary_output_get(e_comp->e_comp_screen);
14    e_output_size_get(output, &w, &h);
15
16    if (output)
17      {
18         edev->mouse.minx = edev->mouse.miny = 0;
19         edev->mouse.maxw = w;
20         edev->mouse.maxh = h;
21
22         if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
23           {
24              edev->seat->ptr.dx = (double)(w / 2);
25              edev->seat->ptr.dy = (double)(h / 2);
26              edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
27              edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
28              edev->mouse.dx = edev->seat->ptr.dx;
29              edev->mouse.dy = edev->seat->ptr.dy;
30
31              if (output->config.rotation == 90 || output->config.rotation == 270)
32                {
33                   temp = edev->mouse.minx;
34                   edev->mouse.minx = edev->mouse.miny;
35                   edev->mouse.miny = temp;
36
37                   temp = edev->mouse.maxw;
38                   edev->mouse.maxw = edev->mouse.maxh;
39                   edev->mouse.maxh = temp;
40                }
41           }
42      }
43
44 //LCOV_EXCL_START
45 #ifdef _F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
46    const char *sysname;
47    float cal[6];
48    const char *device;
49    Eina_List *devices;
50
51    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
52        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
53      return;
54
55    sysname = libinput_device_get_sysname(edev->device);
56
57    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
58    if (eina_list_count(devices) < 1) return;
59
60 #ifdef _F_E_INPUT_USE_WL_CALIBRATION_
61    const char *vals;
62    enum libinput_config_status status;
63
64    EINA_LIST_FREE(devices, device)
65      {
66         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
67         if ((!vals) ||
68             (sscanf(vals, "%f %f %f %f %f %f",
69                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
70           goto cont;
71
72         cal[2] /= w;
73         cal[5] /= h;
74
75         status =
76           libinput_device_config_calibration_set_matrix(edev->device, cal);
77
78         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
79           ERR("Failed to apply calibration");
80
81 cont:
82         eina_stringshare_del(device);
83         continue;
84      }
85 #endif//_F_E_INPUT_USE_WL_CALIBRATION_
86 #endif//_F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
87 //LCOV_EXCL_STOP
88 }
89
90 static void
91 _device_configure(E_Input_Evdev *edev)
92 {
93    if (libinput_device_config_tap_get_finger_count(edev->device) > 0)
94      {
95         Eina_Bool tap = EINA_FALSE;
96
97         tap = libinput_device_config_tap_get_default_enabled(edev->device);
98         libinput_device_config_tap_set_enabled(edev->device, tap);
99      }
100
101    _device_calibration_set(edev);
102 }
103
104 static void
105 _device_keyboard_setup(E_Input_Evdev *edev)
106 {
107    E_Input_Backend *input;
108    xkb_mod_index_t xkb_idx;
109
110    if ((!edev) || (!edev->seat)) return;
111    if (!(input = edev->seat->input)) return;
112    if (!input->dev->xkb_ctx) return;
113
114    /* create keymap from xkb context */
115    edev->xkb.keymap = _e_input_device_cached_keymap_get(input->dev->xkb_ctx, NULL, 0);
116    if (!edev->xkb.keymap)
117      {
118         ERR("Failed to create keymap: %m");
119         return;
120      }
121
122    /* create xkb state */
123    if (!(edev->xkb.state = xkb_state_new(edev->xkb.keymap)))
124      {
125         ERR("Failed to create xkb state: %m");
126         return;
127      }
128
129    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CTRL);
130    if (xkb_idx != XKB_MOD_INVALID)
131      edev->xkb.ctrl_mask = 1 << xkb_idx;
132    else
133      edev->xkb.ctrl_mask = 0;
134
135    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_ALT);
136    if (xkb_idx != XKB_MOD_INVALID)
137      edev->xkb.alt_mask = 1 << xkb_idx;
138    else
139      edev->xkb.alt_mask = 0;
140
141    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_SHIFT);
142    if (xkb_idx != XKB_MOD_INVALID)
143      edev->xkb.shift_mask = 1 << xkb_idx;
144    else
145      edev->xkb.shift_mask = 0;
146
147    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_LOGO);
148    if (xkb_idx != XKB_MOD_INVALID)
149      edev->xkb.win_mask = 1 << xkb_idx;
150    else
151      edev->xkb.win_mask = 0;
152
153    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_SCROLL);
154    if (xkb_idx != XKB_MOD_INVALID)
155      edev->xkb.scroll_mask = 1 << xkb_idx;
156    else
157      edev->xkb.scroll_mask = 0;
158
159    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_NUM);
160    if (xkb_idx != XKB_MOD_INVALID)
161      edev->xkb.num_mask = 1 << xkb_idx;
162    else
163      edev->xkb.num_mask = 0;
164
165    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CAPS);
166    if (xkb_idx != XKB_MOD_INVALID)
167      edev->xkb.caps_mask = 1 << xkb_idx;
168    else
169      edev->xkb.caps_mask = 0;
170
171    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, "ISO_Level3_Shift");
172    if (xkb_idx != XKB_MOD_INVALID)
173      edev->xkb.altgr_mask = 1 << xkb_idx;
174    else
175      edev->xkb.altgr_mask = 0;
176 }
177
178 static int
179 _device_keysym_translate(xkb_keysym_t keysym, unsigned int modifiers, char *buffer, int bytes)
180 {
181    unsigned long hbytes = 0;
182    unsigned char c;
183
184    if (!keysym) return 0;
185    hbytes = (keysym >> 8);
186
187    if (!(bytes &&
188          ((hbytes == 0) ||
189           ((hbytes == 0xFF) &&
190            (((keysym >= XKB_KEY_BackSpace) && (keysym <= XKB_KEY_Clear)) ||
191             (keysym == XKB_KEY_Return) || (keysym == XKB_KEY_Escape) ||
192             (keysym == XKB_KEY_KP_Space) || (keysym == XKB_KEY_KP_Tab) ||
193             (keysym == XKB_KEY_KP_Enter) ||
194             ((keysym >= XKB_KEY_KP_Multiply) && (keysym <= XKB_KEY_KP_9)) ||
195             (keysym == XKB_KEY_KP_Equal) || (keysym == XKB_KEY_Delete))))))
196      return 0;
197
198    if (keysym == XKB_KEY_KP_Space)
199      c = (XKB_KEY_space & 0x7F);
200    else if (hbytes == 0xFF)
201      c = (keysym & 0x7F);
202    else
203      c = (keysym & 0xFF);
204
205    if (modifiers & ECORE_EVENT_MODIFIER_CTRL)
206      {
207         if (((c >= '@') && (c < '\177')) || c == ' ')
208           c &= 0x1F;
209         else if (c == '2')
210           c = '\000';
211         else if ((c >= '3') && (c <= '7'))
212           c -= ('3' - '\033');
213         else if (c == '8')
214           c = '\177';
215         else if (c == '/')
216           c = '_' & 0x1F;
217      }
218    buffer[0] = c;
219    return 1;
220 }
221
222 static void
223 _device_modifiers_update_device(E_Input_Evdev *edev, E_Input_Evdev *from)
224 {
225    xkb_mod_mask_t mask;
226
227    edev->xkb.depressed =
228      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_DEPRESSED);
229    edev->xkb.latched =
230      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LATCHED);
231    edev->xkb.locked =
232      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LOCKED);
233    edev->xkb.group =
234      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_EFFECTIVE);
235
236    mask = (edev->xkb.depressed | edev->xkb.latched);
237
238    if (mask & from->xkb.ctrl_mask)
239      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_CTRL;
240    if (mask & from->xkb.alt_mask)
241      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALT;
242    if (mask & from->xkb.shift_mask)
243      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
244    if (mask & from->xkb.win_mask)
245      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_WIN;
246    if (mask & from->xkb.scroll_mask)
247      edev->xkb.modifiers |= ECORE_EVENT_LOCK_SCROLL;
248    if (mask & from->xkb.num_mask)
249      edev->xkb.modifiers |= ECORE_EVENT_LOCK_NUM;
250    if (mask & from->xkb.caps_mask)
251      edev->xkb.modifiers |= ECORE_EVENT_LOCK_CAPS;
252    if (mask & from->xkb.altgr_mask)
253      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALTGR;
254 }
255
256 static void
257 _device_modifiers_update(E_Input_Evdev *edev)
258 {
259    Eina_List *l;
260    E_Input_Evdev *ed;
261
262    edev->xkb.modifiers = 0;
263
264    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
265      _device_modifiers_update_device(edev, edev);
266    else
267      {
268         EINA_LIST_FOREACH(edev->seat->devices, l, ed)
269           {
270              if (!(ed->caps & E_INPUT_SEAT_KEYBOARD)) continue;
271              _device_modifiers_update_device(edev, ed);
272           }
273      }
274
275 }
276
277 static int
278 _device_remapped_key_get(E_Input_Evdev *edev, int code)
279 {
280    void *ret = NULL;
281
282    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, code);
283    if (!edev->key_remap_enabled) return code;
284    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->key_remap_hash, code);
285
286    ret = eina_hash_find(edev->key_remap_hash, &code);
287
288    if (ret) code = (int)(intptr_t)ret;
289
290    return code;
291 }
292
293 E_API Ecore_Device *
294 e_input_evdev_get_ecore_device(const char *path, Ecore_Device_Class clas)
295 {
296    const Eina_List *dev_list = NULL;
297    const Eina_List *l;
298    Ecore_Device *dev = NULL;
299    const char *identifier;
300
301    if (!path) return NULL;
302
303    dev_list = ecore_device_list();
304    if (!dev_list) return NULL;
305    EINA_LIST_FOREACH(dev_list, l, dev)
306      {
307         if (!dev) continue;
308         identifier = ecore_device_identifier_get(dev);
309         if (!identifier) continue;
310         if ((ecore_device_class_get(dev) == clas) && !(strcmp(identifier, path)))
311           return dev;
312      }
313    return NULL;
314 }
315
316 static void
317 _e_input_event_mouse_move_cb_free(void *data EINA_UNUSED, void *event)
318 {
319    Ecore_Event_Mouse_Move *ev = event;
320
321    if (ev->dev) ecore_device_unref(ev->dev);
322
323    free(ev);
324 }
325
326 static void
327 _e_input_event_mouse_wheel_cb_free(void *data EINA_UNUSED, void *event)
328 {
329    Ecore_Event_Mouse_Wheel *ev = event;
330
331    if (ev->dev) ecore_device_unref(ev->dev);
332
333    free(ev);
334 }
335
336 static void
337 _e_input_event_mouse_button_cb_free(void *data EINA_UNUSED, void *event)
338 {
339    Ecore_Event_Mouse_Button *ev = event;
340
341    if (ev->dev) ecore_device_unref(ev->dev);
342
343    free(ev);
344 }
345
346 static void
347 _e_input_event_key_cb_free(void *data EINA_UNUSED, void *event)
348 {
349    Ecore_Event_Key *ev = event;
350
351    if (ev->dev) ecore_device_unref(ev->dev);
352    if (ev->data) E_FREE(ev->data);
353
354    free(ev);
355 }
356
357 static void
358 _device_handle_key(struct libinput_device *device, struct libinput_event_keyboard *event)
359 {
360    E_Input_Evdev *edev;
361    E_Input_Backend *input;
362    uint32_t timestamp;
363    uint32_t code, nsyms;
364    const xkb_keysym_t *syms;
365    enum libinput_key_state state;
366    int key_count;
367    xkb_keysym_t sym = XKB_KEY_NoSymbol;
368    char key[256], keyname[256], compose_buffer[256];
369    Ecore_Event_Key *e;
370    char *tmp = NULL, *compose = NULL;
371    E_Keyrouter_Event_Data *key_data;
372    Ecore_Device *ecore_dev = NULL, *data;
373    Eina_List *l;
374    E_Comp_Config *comp_conf = NULL;
375
376    if (!(edev = libinput_device_get_user_data(device)))
377      {
378         return;
379      }
380
381    if (!(input = edev->seat->input))
382      {
383         return;
384      }
385
386    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
387    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
388      {
389         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
390           {
391              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_KEYBOARD)
392                {
393                   ecore_dev = data;
394                   break;
395                }
396           }
397      }
398    else
399      {
400         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_KEYBOARD);
401         ecore_dev = edev->ecore_dev;
402      }
403
404    if (!ecore_dev)
405      {
406         ERR("Failed to get source ecore device from event !\n");
407         return;
408      }
409
410    timestamp = libinput_event_keyboard_get_time(event);
411    code = libinput_event_keyboard_get_key(event);
412    code = _device_remapped_key_get(edev, code + 8);
413    state = libinput_event_keyboard_get_key_state(event);
414    key_count = libinput_event_keyboard_get_seat_key_count(event);
415
416    /* ignore key events that are not seat wide state changes */
417    if (((state == LIBINPUT_KEY_STATE_PRESSED) && (key_count != 1)) ||
418        ((state == LIBINPUT_KEY_STATE_RELEASED) && (key_count != 0)))
419      {
420         return;
421      }
422
423    xkb_state_update_key(edev->xkb.state, code,
424                         (state ? XKB_KEY_DOWN : XKB_KEY_UP));
425
426    /* get the keysym for this code */
427    nsyms = xkb_key_get_syms(edev->xkb.state, code, &syms);
428    if (nsyms == 1) sym = syms[0];
429
430    /* get the keyname for this sym */
431    memset(key, 0, sizeof(key));
432    xkb_keysym_get_name(sym, key, sizeof(key));
433
434    memset(keyname, 0, sizeof(keyname));
435    memcpy(keyname, key, sizeof(keyname));
436
437    if (keyname[0] == '\0')
438      snprintf(keyname, sizeof(keyname), "Keycode-%u", code);
439
440    /* if shift is active, we need to transform the key to lower */
441    if (xkb_state_mod_index_is_active(edev->xkb.state,
442                                      xkb_map_mod_get_index(edev->xkb.keymap,
443                                      XKB_MOD_NAME_SHIFT),
444                                      XKB_STATE_MODS_EFFECTIVE))
445      {
446         if (keyname[0] != '\0')
447           keyname[0] = tolower(keyname[0]);
448      }
449
450    memset(compose_buffer, 0, sizeof(compose_buffer));
451    if (_device_keysym_translate(sym, edev->xkb.modifiers,
452                                 compose_buffer, sizeof(compose_buffer)))
453      {
454         compose = eina_str_convert("ISO8859-1", "UTF-8", compose_buffer);
455         if (!compose)
456           {
457              ERR("E Input cannot convert input key string '%s' to UTF-8. "
458                  "Is Eina built with iconv support?", compose_buffer);
459           }
460         else
461           tmp = compose;
462      }
463
464    if (!compose) compose = compose_buffer;
465
466    e = calloc(1, sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
467               ((compose[0] != '\0') ? strlen(compose) : 0) + 3);
468    if (!e)
469      {
470         E_FREE(tmp);
471         return;
472      }
473    key_data = E_NEW(E_Keyrouter_Event_Data, 1);
474    if (!key_data)
475      {
476         E_FREE(tmp);
477         E_FREE(e);
478         return;
479      }
480
481    e->keyname = (char *)(e + 1);
482    e->key = e->keyname + strlen(keyname) + 1;
483    e->compose = strlen(compose) ? e->key + strlen(key) + 1 : NULL;
484    e->string = e->compose;
485
486    strncpy((char *)e->keyname, keyname, strlen(keyname));
487    strncpy((char *)e->key, key, strlen(key));
488    if (strlen(compose)) strncpy((char *)e->compose, compose, strlen(compose));
489
490    e->window = (Ecore_Window)input->dev->window;
491    e->event_window = (Ecore_Window)input->dev->window;
492    e->root_window = (Ecore_Window)input->dev->window;
493    e->timestamp = timestamp;
494    e->same_screen = 1;
495    e->keycode = code;
496    e->data = key_data;
497
498    _device_modifiers_update(edev);
499
500    e->modifiers = edev->xkb.modifiers;
501    e->dev = ecore_device_ref(ecore_dev);
502
503    comp_conf = e_comp_config_get();
504    if (comp_conf && comp_conf->input_log_enable)
505      ELOGF("Key", "%s (keyname: %s, keycode: %d, device: %s)", NULL, state?"Press":"Release", e->keyname, e->keycode, ecore_device_name_get(e->dev));
506
507    if (state)
508      ecore_event_add(ECORE_EVENT_KEY_DOWN, e, _e_input_event_key_cb_free, NULL);
509    else
510      ecore_event_add(ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
511
512    if (tmp) free(tmp);
513 }
514
515 static void
516 _device_pointer_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event)
517 {
518    E_Input_Backend *input;
519    Ecore_Event_Mouse_Move *ev;
520    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
521    Eina_List *l;
522
523    if (!(input = edev->seat->input)) return;
524
525    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
526    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
527      {
528         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
529           {
530              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
531                {
532                   ecore_dev = data;
533                   break;
534                }
535              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
536                {
537                   detent_data = data;
538                }
539           }
540         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
541           {
542              ecore_dev = detent_data;
543           }
544      }
545    else
546      {
547         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
548         ecore_dev = edev->ecore_dev;
549      }
550
551    if (!ecore_dev)
552      {
553         ERR("Failed to get source ecore device from event !\n");
554         return;
555      }
556    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
557      {
558         /* Do not process detent device's move events. */
559         return;
560      }
561
562    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
563
564    if (edev->seat->ptr.ix < edev->mouse.minx)
565      edev->seat->ptr.dx = edev->seat->ptr.ix = edev->mouse.minx;
566    else if (edev->seat->ptr.ix >= (edev->mouse.minx + edev->mouse.maxw))
567      edev->seat->ptr.dx = edev->seat->ptr.ix = (edev->mouse.minx + edev->mouse.maxw - 1);
568
569    if (edev->seat->ptr.iy < edev->mouse.miny)
570      edev->seat->ptr.dy = edev->seat->ptr.iy = edev->mouse.miny;
571    else if (edev->seat->ptr.iy >= (edev->mouse.miny + edev->mouse.maxh))
572      edev->seat->ptr.dy = edev->seat->ptr.iy = (edev->mouse.miny + edev->mouse.maxh - 1);
573
574    edev->mouse.dx = edev->seat->ptr.dx;
575    edev->mouse.dy = edev->seat->ptr.dy;
576
577    ev->window = (Ecore_Window)input->dev->window;
578    ev->event_window = (Ecore_Window)input->dev->window;
579    ev->root_window = (Ecore_Window)input->dev->window;
580    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
581    ev->same_screen = 1;
582
583    _device_modifiers_update(edev);
584    ev->modifiers = edev->xkb.modifiers;
585
586    ev->x = edev->seat->ptr.ix;
587    ev->y = edev->seat->ptr.iy;
588    ev->root.x = ev->x;
589    ev->root.y = ev->y;
590
591    ev->multi.device = edev->mt_slot;
592    ev->multi.radius = 1;
593    ev->multi.radius_x = 1;
594    ev->multi.radius_y = 1;
595    ev->multi.pressure = 1.0;
596    ev->multi.angle = 0.0;
597    ev->multi.x = ev->x;
598    ev->multi.y = ev->y;
599    ev->multi.root.x = ev->x;
600    ev->multi.root.y = ev->y;
601    ev->dev = ecore_device_ref(ecore_dev);
602
603    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
604 }
605
606 void
607 _e_input_pointer_motion_post(E_Input_Evdev *edev)
608 {
609    _device_pointer_motion(edev, NULL);
610 }
611
612 static void
613 _device_handle_pointer_motion(struct libinput_device *device, struct libinput_event_pointer *event)
614 {
615    E_Input_Evdev *edev;
616    double dx, dy, temp;
617
618    if (!(edev = libinput_device_get_user_data(device)))
619      {
620         return;
621      }
622
623    dx = libinput_event_pointer_get_dx(event);
624    dy = libinput_event_pointer_get_dy(event);
625
626    if (edev->seat->ptr.swap)
627      {
628          temp = dx;
629          dx = dy;
630          dy = temp;
631      }
632    if (edev->seat->ptr.invert_x)
633      dx *= -1;
634    if (edev->seat->ptr.invert_y)
635      dy *= -1;
636
637    edev->seat->ptr.dx += dx;
638    edev->seat->ptr.dy += dy;
639
640    edev->mouse.dx = edev->seat->ptr.dx;
641    edev->mouse.dy = edev->seat->ptr.dy;
642
643    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
644        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
645      {
646         return;
647      }
648
649    edev->seat->ptr.ix = edev->seat->ptr.dx;
650    edev->seat->ptr.iy = edev->seat->ptr.dy;
651
652   _device_pointer_motion(edev, event);
653 }
654
655 static void
656 _device_handle_pointer_motion_absolute(struct libinput_device *device, struct libinput_event_pointer *event)
657 {
658    E_Input_Evdev *edev;
659    int w = 0, h = 0;
660
661    if (!(edev = libinput_device_get_user_data(device)))
662      {
663         return;
664      }
665
666    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
667
668    edev->mouse.dx = edev->seat->ptr.dx =
669      libinput_event_pointer_get_absolute_x_transformed(event, w);
670    edev->mouse.dy = edev->seat->ptr.dy =
671      libinput_event_pointer_get_absolute_y_transformed(event, h);
672
673    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
674        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
675      {
676         return;
677      }
678
679    edev->seat->ptr.ix = edev->seat->ptr.dx;
680    edev->seat->ptr.iy = edev->seat->ptr.dy;
681    _device_pointer_motion(edev, event);
682 }
683
684 static void
685 _device_handle_button(struct libinput_device *device, struct libinput_event_pointer *event)
686 {
687    E_Input_Evdev *edev;
688    E_Input_Backend *input;
689    Ecore_Event_Mouse_Button *ev;
690    enum libinput_button_state state;
691    uint32_t button, timestamp;
692    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
693    Eina_List *l;
694    E_Comp_Config *comp_conf = NULL;
695
696    if (!(edev = libinput_device_get_user_data(device)))
697      {
698         return;
699      }
700    if (!(input = edev->seat->input))
701      {
702         return;
703      }
704
705    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
706    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
707      {
708         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
709           {
710              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
711                {
712                   ecore_dev = data;
713                   break;
714                }
715              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
716                {
717                   detent_data = data;
718                }
719           }
720         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
721           {
722              ecore_dev = detent_data;
723           }
724      }
725    else
726      {
727         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
728         ecore_dev = edev->ecore_dev;
729      }
730
731    if (!ecore_dev)
732      {
733         ERR("Failed to get source ecore device from event !\n");
734         return;
735      }
736
737    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button))))
738      {
739         return;
740      }
741
742    state = libinput_event_pointer_get_button_state(event);
743    button = libinput_event_pointer_get_button(event);
744    timestamp = libinput_event_pointer_get_time(event);
745
746    button = ((button & 0x00F) + 1);
747    if (button == 3) button = 2;
748    else if (button == 2) button = 3;
749
750    ev->window = (Ecore_Window)input->dev->window;
751    ev->event_window = (Ecore_Window)input->dev->window;
752    ev->root_window = (Ecore_Window)input->dev->window;
753    ev->timestamp = timestamp;
754    ev->same_screen = 1;
755
756    _device_modifiers_update(edev);
757    ev->modifiers = edev->xkb.modifiers;
758
759    ev->x = edev->seat->ptr.ix;
760    ev->y = edev->seat->ptr.iy;
761    ev->root.x = ev->x;
762    ev->root.y = ev->y;
763
764    ev->multi.device = edev->mt_slot;
765    ev->multi.radius = 1;
766    ev->multi.radius_x = 1;
767    ev->multi.radius_y = 1;
768    ev->multi.pressure = 1.0;
769    ev->multi.angle = 0.0;
770    ev->multi.x = ev->x;
771    ev->multi.y = ev->y;
772    ev->multi.root.x = ev->x;
773    ev->multi.root.y = ev->y;
774    ev->dev = ecore_device_ref(ecore_dev);
775
776    if (state)
777      {
778         unsigned int current;
779
780         current = timestamp;
781         edev->mouse.did_double = EINA_FALSE;
782         edev->mouse.did_triple = EINA_FALSE;
783
784         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
785             (button == edev->mouse.prev_button))
786           {
787              edev->mouse.did_double = EINA_TRUE;
788              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
789                  (button == edev->mouse.last_button))
790                {
791                   edev->mouse.did_triple = EINA_TRUE;
792                   edev->mouse.prev = 0;
793                   edev->mouse.last = 0;
794                   current = 0;
795                }
796           }
797
798         edev->mouse.last = edev->mouse.prev;
799         edev->mouse.prev = current;
800         edev->mouse.last_button = edev->mouse.prev_button;
801         edev->mouse.prev_button = button;
802      }
803
804    ev->buttons = button;
805
806    if (edev->mouse.did_double)
807      ev->double_click = 1;
808    if (edev->mouse.did_triple)
809      ev->triple_click = 1;
810
811    comp_conf = e_comp_config_get();
812    if (comp_conf && comp_conf->input_log_enable)
813      ELOGF("Mouse", "Button %s (btn: %d, device: %s)", NULL, state?"Press":"Release", button, ecore_device_name_get(ev->dev));
814
815    if (state)
816      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, _e_input_event_mouse_button_cb_free, NULL);
817    else
818      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, _e_input_event_mouse_button_cb_free, NULL);
819 }
820
821 static void
822 _device_handle_axis(struct libinput_device *device, struct libinput_event_pointer *event)
823 {
824    E_Input_Evdev *edev;
825    E_Input_Backend *input;
826    Ecore_Event_Mouse_Wheel *ev;
827    uint32_t timestamp;
828    enum libinput_pointer_axis axis;
829    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
830    Eina_List *l;
831    E_Comp_Config *comp_conf = NULL;
832
833    if (!(edev = libinput_device_get_user_data(device)))
834      {
835         return;
836      }
837    if (!(input = edev->seat->input))
838      {
839         return;
840      }
841
842    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
843    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
844      {
845         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
846           {
847              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
848                {
849                   ecore_dev = data;
850                   break;
851                }
852              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
853                {
854                   detent_data = data;
855                }
856           }
857         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
858           {
859              ecore_dev = detent_data;
860           }
861      }
862    else
863      {
864         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
865         ecore_dev = edev->ecore_dev;
866      }
867
868    if (!ecore_dev)
869      {
870         ERR("Failed to get source ecore device from event !\n");
871         return;
872      }
873
874    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
875      {
876         return;
877      }
878
879    timestamp = libinput_event_pointer_get_time(event);
880
881    ev->window = (Ecore_Window)input->dev->window;
882    ev->event_window = (Ecore_Window)input->dev->window;
883    ev->root_window = (Ecore_Window)input->dev->window;
884    ev->timestamp = timestamp;
885    ev->same_screen = 1;
886
887    _device_modifiers_update(edev);
888    ev->modifiers = edev->xkb.modifiers;
889
890    ev->x = edev->seat->ptr.ix;
891    ev->y = edev->seat->ptr.iy;
892    ev->root.x = ev->x;
893    ev->root.y = ev->y;
894    ev->dev = ecore_device_ref(ecore_dev);
895
896    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
897    if (libinput_event_pointer_has_axis(event, axis))
898      ev->z = libinput_event_pointer_get_axis_value(event, axis);
899
900    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
901    if (libinput_event_pointer_has_axis(event, axis))
902      {
903         ev->direction = 1;
904         ev->z = libinput_event_pointer_get_axis_value(event, axis);
905      }
906
907    comp_conf = e_comp_config_get();
908    if (comp_conf && comp_conf->input_log_enable)
909      {
910         if (detent_data)
911           ELOGF("Mouse", "Detent (direction: %d, value: %d)", NULL, ev->direction, ev->z);
912         else
913           ELOGF("Mouse", "Wheel (direction: %d, value: %d)", NULL, ev->direction, ev->z);
914      }
915
916    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
917 }
918
919 static void
920 _device_handle_touch_event_send(E_Input_Evdev *edev, struct libinput_event_touch *event, int state)
921 {
922    E_Input_Backend *input;
923    Ecore_Event_Mouse_Button *ev;
924    uint32_t timestamp, button = 0;
925    Ecore_Device *ecore_dev = NULL, *data;
926    Eina_List *l;
927
928    if (!edev) return;
929    if (!(input = edev->seat->input)) return;
930
931    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
932    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
933      {
934         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
935           {
936              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
937                {
938                   ecore_dev = data;
939                   break;
940                }
941           }
942      }
943    else
944      {
945         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
946         ecore_dev = edev->ecore_dev;
947      }
948
949    if (!ecore_dev)
950      {
951         ERR("Failed to get source ecore device from event !\n");
952         return;
953      }
954
955    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) return;
956
957    timestamp = libinput_event_touch_get_time(event);
958
959    ev->window = (Ecore_Window)input->dev->window;
960    ev->event_window = (Ecore_Window)input->dev->window;
961    ev->root_window = (Ecore_Window)input->dev->window;
962    ev->timestamp = timestamp;
963    ev->same_screen = 1;
964
965    _device_modifiers_update(edev);
966    ev->modifiers = edev->xkb.modifiers;
967
968    ev->x = edev->seat->ptr.ix;
969    ev->y = edev->seat->ptr.iy;
970    ev->root.x = ev->x;
971    ev->root.y = ev->y;
972
973    ev->multi.device = edev->mt_slot;
974    ev->multi.radius = 1;
975    ev->multi.radius_x = 1;
976    ev->multi.radius_y = 1;
977    ev->multi.pressure = 1.0;
978    ev->multi.angle = 0.0;
979 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
980    if (libinput_event_get_type(libinput_event_touch_get_base_event(event))
981        == LIBINPUT_EVENT_TOUCH_DOWN)
982      {
983         if (libinput_event_touch_has_minor(event))
984           ev->multi.radius_x = libinput_event_touch_get_minor(event);
985         if (libinput_event_touch_has_major(event))
986           ev->multi.radius_y = libinput_event_touch_get_major(event);
987         if (libinput_event_touch_has_pressure(event))
988           ev->multi.pressure = libinput_event_touch_get_pressure(event);
989         if (libinput_event_touch_has_orientation(event))
990           ev->multi.angle = libinput_event_touch_get_orientation(event);
991      }
992 #endif
993    ev->multi.x = ev->x;
994    ev->multi.y = ev->y;
995    ev->multi.root.x = ev->x;
996    ev->multi.root.y = ev->y;
997    ev->dev = ecore_device_ref(ecore_dev);
998
999    if (state == ECORE_EVENT_MOUSE_BUTTON_DOWN)
1000      {
1001         unsigned int current;
1002
1003         current = timestamp;
1004         edev->mouse.did_double = EINA_FALSE;
1005         edev->mouse.did_triple = EINA_FALSE;
1006
1007         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
1008             (button == edev->mouse.prev_button))
1009           {
1010              edev->mouse.did_double = EINA_TRUE;
1011              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
1012                  (button == edev->mouse.last_button))
1013                {
1014                   edev->mouse.did_triple = EINA_TRUE;
1015                   edev->mouse.prev = 0;
1016                   edev->mouse.last = 0;
1017                   current = 0;
1018                }
1019           }
1020
1021         edev->mouse.last = edev->mouse.prev;
1022         edev->mouse.prev = current;
1023         edev->mouse.last_button = edev->mouse.prev_button;
1024         edev->mouse.prev_button = button;
1025         edev->touch.pressed |= (1 << ev->multi.device);
1026      }
1027    else
1028      {
1029         edev->touch.pressed &= ~(1 << ev->multi.device);
1030      }
1031
1032    ev->buttons = ((button & 0x00F) + 1);
1033
1034    if (edev->mouse.did_double)
1035      ev->double_click = 1;
1036    if (edev->mouse.did_triple)
1037      ev->triple_click = 1;
1038
1039    ecore_event_add(state, ev, _e_input_event_mouse_button_cb_free, NULL);
1040 }
1041
1042 static void
1043 _device_handle_touch_motion_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1044 {
1045    E_Input_Backend *input;
1046    Ecore_Event_Mouse_Move *ev;
1047    Ecore_Device *ecore_dev = NULL, *data;
1048    Eina_List *l;
1049
1050    if (!edev) return;
1051    if (!(input = edev->seat->input)) return;
1052
1053    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1054    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1055      {
1056         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1057           {
1058              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1059                {
1060                   ecore_dev = data;
1061                   break;
1062                }
1063           }
1064      }
1065    else
1066      {
1067         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1068         ecore_dev = edev->ecore_dev;
1069      }
1070
1071    if (!ecore_dev)
1072      {
1073         ERR("Failed to get source ecore device from event !\n");
1074         return;
1075      }
1076
1077    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
1078
1079    ev->window = (Ecore_Window)input->dev->window;
1080    ev->event_window = (Ecore_Window)input->dev->window;
1081    ev->root_window = (Ecore_Window)input->dev->window;
1082    ev->timestamp = libinput_event_touch_get_time(event);
1083    ev->same_screen = 1;
1084
1085    _device_modifiers_update(edev);
1086    ev->modifiers = edev->xkb.modifiers;
1087
1088    ev->x = edev->seat->ptr.ix;
1089    ev->y = edev->seat->ptr.iy;
1090    ev->root.x = ev->x;
1091    ev->root.y = ev->y;
1092
1093    ev->multi.device = edev->mt_slot;
1094    ev->multi.radius = 1;
1095    ev->multi.radius_x = 1;
1096    ev->multi.radius_y = 1;
1097    ev->multi.pressure = 1.0;
1098    ev->multi.angle = 0.0;
1099 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1100    if (libinput_event_touch_has_minor(event))
1101      ev->multi.radius_x = libinput_event_touch_get_minor(event);
1102    if (libinput_event_touch_has_major(event))
1103      ev->multi.radius_y = libinput_event_touch_get_major(event);
1104    if (libinput_event_touch_has_pressure(event))
1105      ev->multi.pressure = libinput_event_touch_get_pressure(event);
1106    if (libinput_event_touch_has_orientation(event))
1107      ev->multi.angle = libinput_event_touch_get_orientation(event);
1108 #endif
1109    ev->multi.x = ev->x;
1110    ev->multi.y = ev->y;
1111    ev->multi.root.x = ev->x;
1112    ev->multi.root.y = ev->y;
1113    ev->dev = ecore_device_ref(ecore_dev);
1114
1115    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
1116 }
1117
1118 static void
1119 _device_handle_touch_down(struct libinput_device *device, struct libinput_event_touch *event)
1120 {
1121    E_Input_Evdev *edev;
1122    int w = 0, h = 0;
1123    E_Comp_Config *comp_conf = NULL;
1124
1125    if (!(edev = libinput_device_get_user_data(device)))
1126      {
1127         return;
1128      }
1129
1130    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1131
1132    edev->mouse.dx = edev->seat->ptr.ix = edev->seat->ptr.dx =
1133      libinput_event_touch_get_x_transformed(event, w);
1134    edev->mouse.dy = edev->seat->ptr.iy = edev->seat->ptr.dy =
1135      libinput_event_touch_get_y_transformed(event, h);
1136
1137    edev->mt_slot = libinput_event_touch_get_slot(event);
1138    if (edev->mt_slot < 0)
1139      {
1140         /* FIXME: The single touch device return slot id -1
1141          *        But currently we have no API to distinguish multi touch or single touch
1142          *        After libinput 1.11 version, libinput provides get_touch_count API,
1143          *        so we can distinguish multi touch device or single touch device.
1144          */
1145         if (edev->mt_slot == -1)
1146           edev->mt_slot = 0;
1147         else
1148           {
1149              WRN("%d slot touch down events are not supported\n", edev->mt_slot);
1150              return;
1151           }
1152      }
1153
1154    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1155      {
1156         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1157         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1158      }
1159
1160    comp_conf = e_comp_config_get();
1161    if (comp_conf && comp_conf->input_log_enable)
1162      ELOGF("Touch", "Down (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
1163
1164    _device_handle_touch_motion_send(edev, event);
1165    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_DOWN);
1166 }
1167
1168 static void
1169 _device_handle_touch_motion(struct libinput_device *device, struct libinput_event_touch *event)
1170 {
1171    E_Input_Evdev *edev;
1172    int w = 0, h = 0;
1173
1174    if (!(edev = libinput_device_get_user_data(device)))
1175      {
1176         return;
1177      }
1178
1179    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1180
1181    edev->mouse.dx = edev->seat->ptr.dx =
1182      libinput_event_touch_get_x_transformed(event, w);
1183    edev->mouse.dy = edev->seat->ptr.dy =
1184      libinput_event_touch_get_y_transformed(event, h);
1185
1186    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1187        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1188      {
1189         return;
1190      }
1191
1192    edev->seat->ptr.ix = edev->seat->ptr.dx;
1193    edev->seat->ptr.iy = edev->seat->ptr.dy;
1194
1195    edev->mt_slot = libinput_event_touch_get_slot(event);
1196    if (edev->mt_slot < 0)
1197      {
1198         /* FIXME: The single touch device return slot id -1
1199          *        But currently we have no API to distinguish multi touch or single touch
1200          *        After libinput 1.11 version, libinput provides get_touch_count API,
1201          *        so we can distinguish multi touch device or single touch device.
1202          */
1203         if (edev->mt_slot == -1)
1204           edev->mt_slot = 0;
1205         else
1206           {
1207              WRN("%d slot touch motion events are not supported\n", edev->mt_slot);
1208              return;
1209           }
1210      }
1211
1212    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1213      {
1214         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1215         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1216      }
1217
1218    _device_handle_touch_motion_send(edev, event);
1219 }
1220
1221 static void
1222 _device_handle_touch_up(struct libinput_device *device, struct libinput_event_touch *event)
1223 {
1224    E_Input_Evdev *edev;
1225    E_Comp_Config *comp_conf = NULL;
1226
1227    if (!(edev = libinput_device_get_user_data(device)))
1228      {
1229         return;
1230      }
1231
1232    edev->mt_slot = libinput_event_touch_get_slot(event);
1233    if (edev->mt_slot < 0)
1234      {
1235         /* FIXME: The single touch device return slot id -1
1236          *        But currently we have no API to distinguish multi touch or single touch
1237          *        After libinput 1.11 version, libinput provides get_touch_count API,
1238          *        so we can distinguish multi touch device or single touch device.
1239          */
1240         if (edev->mt_slot == -1)
1241           edev->mt_slot = 0;
1242         else
1243           {
1244              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
1245              return;
1246           }
1247      }
1248
1249    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1250      {
1251         edev->mouse.dx = edev->seat->ptr.dx = edev->seat->ptr.ix =
1252           edev->touch.coords[edev->mt_slot].x;
1253         edev->mouse.dy = edev->seat->ptr.dy = edev->seat->ptr.iy =
1254           edev->touch.coords[edev->mt_slot].y;
1255      }
1256
1257    comp_conf = e_comp_config_get();
1258    if (comp_conf && comp_conf->input_log_enable)
1259      ELOGF("Touch", "Up (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
1260
1261    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_UP);
1262 }
1263
1264 static void
1265 _device_handle_touch_frame(struct libinput_device *device EINA_UNUSED, struct libinput_event_touch *event EINA_UNUSED)
1266 {
1267    /* DBG("Unhandled Touch Frame Event"); */
1268 }
1269
1270 static void
1271 _e_input_aux_data_event_free(void *user_data EINA_UNUSED, void *ev)
1272 {
1273    Ecore_Event_Axis_Update *e = (Ecore_Event_Axis_Update *)ev;
1274
1275    if (e->axis) free(e->axis);
1276    if (e->dev) ecore_device_unref(e->dev);
1277
1278    free(e);
1279 }
1280
1281 static void
1282 _device_handle_touch_aux_data(struct libinput_device *device, struct libinput_event_touch_aux_data *event)
1283 {
1284    E_Input_Evdev *edev;
1285    E_Input_Backend *input;
1286    Ecore_Event_Axis_Update *ev;
1287    Ecore_Axis *axis;
1288    Ecore_Device *ecore_dev = NULL, *data;
1289    Eina_List *l;
1290    E_Comp_Config *comp_conf;
1291
1292    if (libinput_event_touch_aux_data_get_type(event) != LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM &&
1293        libinput_event_touch_aux_data_get_value(event) > 0)
1294       goto end;
1295
1296    if (!(edev = libinput_device_get_user_data(device))) goto end;
1297    if (!(input = edev->seat->input)) goto end;
1298
1299    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1300    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1301      {
1302         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1303           {
1304              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1305                {
1306                   ecore_dev = data;
1307                   break;
1308                }
1309           }
1310      }
1311    else
1312      {
1313         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1314         ecore_dev = edev->ecore_dev;
1315      }
1316
1317    if (!ecore_dev)
1318      {
1319         ERR("Failed to get source ecore device from event !\n");
1320         goto end;
1321      }
1322
1323    if (!(ev = calloc(1, sizeof(Ecore_Event_Axis_Update))))goto end;
1324
1325    ev->window = (Ecore_Window)input->dev->window;
1326    ev->event_window = (Ecore_Window)input->dev->window;
1327    ev->root_window = (Ecore_Window)input->dev->window;
1328    ev->timestamp = libinput_event_touch_aux_data_get_time(event);
1329
1330    axis = (Ecore_Axis *)calloc(1, sizeof(Ecore_Axis));
1331    if (axis)
1332      {
1333         axis->label = ECORE_AXIS_LABEL_TOUCH_PALM;
1334         axis->value = libinput_event_touch_aux_data_get_value(event);
1335         ev->naxis = 1;
1336      }
1337    ev->axis = axis;
1338    ev->dev = ecore_device_ref(ecore_dev);
1339
1340    comp_conf = e_comp_config_get();
1341    if (comp_conf && comp_conf->input_log_enable)
1342      ELOGF("Touch", "Axis (label: %d, value: %lf)", NULL, axis?axis->label:-1, axis?axis->value:0.0);
1343
1344    ecore_event_add(ECORE_EVENT_AXIS_UPDATE, ev, _e_input_aux_data_event_free, NULL);
1345
1346 end:
1347    ;
1348 }
1349
1350 E_Input_Evdev *
1351 _e_input_evdev_device_create(E_Input_Seat *seat, struct libinput_device *device)
1352 {
1353    E_Input_Evdev *edev;
1354    E_Input_Backend *b_input;
1355
1356    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
1357
1358    /* try to allocate space for new evdev */
1359    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
1360
1361    edev->seat = seat;
1362    edev->device = device;
1363    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
1364
1365    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
1366      {
1367         edev->caps |= E_INPUT_SEAT_KEYBOARD;
1368         _device_keyboard_setup(edev);
1369      }
1370
1371    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
1372      {
1373         edev->caps |= E_INPUT_SEAT_POINTER;
1374
1375         /* TODO: make this configurable */
1376         edev->mouse.threshold = 250;
1377
1378         b_input = seat->input;
1379         if (b_input->left_handed == EINA_TRUE)
1380           {
1381              if (libinput_device_config_left_handed_set(device, 1) !=
1382                  LIBINPUT_CONFIG_STATUS_SUCCESS)
1383                {
1384                   WRN("Failed to set left hand mode about device: %s\n",
1385                       libinput_device_get_name(device));
1386                }
1387           }
1388      }
1389
1390    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
1391      {
1392         int palm_code;
1393         edev->caps |= E_INPUT_SEAT_TOUCH;
1394         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
1395         if (libinput_device_touch_has_aux_data(device, palm_code))
1396           {
1397              libinput_device_touch_set_aux_data(device, palm_code);
1398           }
1399      }
1400
1401    libinput_device_set_user_data(device, edev);
1402    libinput_device_ref(device);
1403
1404    /* configure device */
1405    _device_configure(edev);
1406
1407    return edev;
1408 }
1409
1410 void
1411 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
1412 {
1413    Ecore_Device *dev;
1414
1415    EINA_SAFETY_ON_NULL_RETURN(edev);
1416
1417    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
1418      {
1419         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
1420         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
1421      }
1422
1423    if (edev->ecore_dev) ecore_device_del(edev->ecore_dev);
1424    if (edev->ecore_dev_list)
1425      EINA_LIST_FREE(edev->ecore_dev_list, dev)
1426        {
1427           ecore_device_del(dev);
1428        }
1429    if (edev->path) eina_stringshare_del(edev->path);
1430    if (edev->device) libinput_device_unref(edev->device);
1431    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
1432
1433    free(edev);
1434 }
1435
1436 Eina_Bool
1437 _e_input_evdev_event_process(struct libinput_event *event)
1438 {
1439    struct libinput_device *device;
1440    Eina_Bool ret = EINA_TRUE;
1441
1442    device = libinput_event_get_device(event);
1443    switch (libinput_event_get_type(event))
1444      {
1445       case LIBINPUT_EVENT_KEYBOARD_KEY:
1446         _device_handle_key(device, libinput_event_get_keyboard_event(event));
1447         break;
1448       case LIBINPUT_EVENT_POINTER_MOTION:
1449         _device_handle_pointer_motion(device,
1450                                       libinput_event_get_pointer_event(event));
1451         break;
1452       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
1453         _device_handle_pointer_motion_absolute(device,
1454                                                libinput_event_get_pointer_event(event));
1455         break;
1456       case LIBINPUT_EVENT_POINTER_BUTTON:
1457         _device_handle_button(device, libinput_event_get_pointer_event(event));
1458         break;
1459       case LIBINPUT_EVENT_POINTER_AXIS:
1460         _device_handle_axis(device, libinput_event_get_pointer_event(event));
1461         break;
1462       case LIBINPUT_EVENT_TOUCH_DOWN:
1463         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
1464         break;
1465       case LIBINPUT_EVENT_TOUCH_MOTION:
1466         _device_handle_touch_motion(device,
1467                                     libinput_event_get_touch_event(event));
1468         break;
1469       case LIBINPUT_EVENT_TOUCH_UP:
1470         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
1471         break;
1472       case LIBINPUT_EVENT_TOUCH_FRAME:
1473         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
1474         break;
1475       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
1476         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
1477         break;
1478       default:
1479         ret = EINA_FALSE;
1480         break;
1481      }
1482
1483    return ret;
1484 }
1485
1486 /**
1487  * @brief Set the axis size of the given device.
1488  *
1489  * @param dev The device to set the axis size to.
1490  * @param w The width of the axis.
1491  * @param h The height of the axis.
1492  *
1493  * This function sets set the width @p w and height @p h of the axis
1494  * of device @p dev. If @p dev is a relative input device, a width and
1495  * height must set for it. If its absolute set the ioctl correctly, if
1496  * not, unsupported device.
1497  */
1498 EINTERN void
1499 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
1500 {
1501    const char *sysname;
1502    float cal[6];
1503    const char *device;
1504    Eina_List *devices;
1505    const char *vals;
1506    enum libinput_config_status status;
1507
1508    EINA_SAFETY_ON_NULL_RETURN(edev);
1509    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
1510
1511    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
1512        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
1513      return;
1514
1515    sysname = libinput_device_get_sysname(edev->device);
1516
1517    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
1518    if (eina_list_count(devices) < 1) return;
1519
1520    EINA_LIST_FREE(devices, device)
1521      {
1522         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
1523         if ((!vals) ||
1524             (sscanf(vals, "%f %f %f %f %f %f",
1525                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
1526           goto cont;
1527
1528         cal[2] /= w;
1529         cal[5] /= h;
1530
1531         status =
1532           libinput_device_config_calibration_set_matrix(edev->device, cal);
1533
1534         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
1535           ERR("Failed to apply calibration");
1536
1537 cont:
1538         eina_stringshare_del(device);
1539         continue;
1540      }
1541 }
1542
1543 E_API const char *
1544 e_input_evdev_name_get(E_Input_Evdev *evdev)
1545 {
1546    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1547    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1548
1549    return libinput_device_get_name(evdev->device);
1550 }
1551
1552 EINTERN const char *
1553 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
1554 {
1555    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1556    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1557
1558    return libinput_device_get_sysname(evdev->device);
1559 }
1560
1561 EINTERN Eina_Bool
1562 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
1563 {
1564    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1565    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1566
1567    edev->key_remap_enabled = enable;
1568
1569    if (enable == EINA_FALSE && edev->key_remap_hash)
1570      {
1571         eina_hash_free(edev->key_remap_hash);
1572         edev->key_remap_hash = NULL;
1573      }
1574
1575    return EINA_TRUE;
1576 }
1577
1578 EINTERN Eina_Bool
1579 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
1580 {
1581    int i;
1582
1583    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1584    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1585    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
1586    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
1587    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
1588    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
1589
1590    if (edev->key_remap_hash == NULL)
1591      edev->key_remap_hash = eina_hash_int32_new(NULL);
1592
1593    if (edev->key_remap_hash == NULL)
1594      {
1595         ERR("Failed to set remap key information : creating a hash is failed.");
1596         return EINA_FALSE;
1597      }
1598
1599    for (i = 0; i < num ; i++)
1600      {
1601         if (!from_keys[i] || !to_keys[i])
1602           {
1603              ERR("Failed to set remap key information : given arguments are invalid.");
1604              return EINA_FALSE;
1605           }
1606      }
1607
1608    for (i = 0; i < num ; i++)
1609      {
1610         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
1611      }
1612
1613    return EINA_TRUE;
1614 }
1615
1616 E_API int
1617 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
1618 {
1619    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
1620    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
1621 }
1622
1623 EINTERN Eina_Bool
1624 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
1625 {
1626    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1627    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1628
1629    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
1630        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
1631      return EINA_FALSE;
1632
1633    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
1634        LIBINPUT_CONFIG_STATUS_SUCCESS)
1635      {
1636         WRN("Failed to set input transformation about device: %s\n",
1637             libinput_device_get_name(edev->device));
1638         return EINA_FALSE;
1639      }
1640
1641    return EINA_TRUE;
1642 }
1643
1644 EINTERN Eina_Bool
1645 e_input_evdev_mouse_accel_speed_set(E_Input_Evdev *edev, double speed)
1646 {
1647    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1648    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1649
1650    if (!libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
1651      return EINA_FALSE;
1652
1653    if (!libinput_device_config_accel_is_available(edev->device))
1654      return EINA_FALSE;
1655
1656    if (libinput_device_config_accel_set_speed(edev->device, speed) !=
1657        LIBINPUT_CONFIG_STATUS_SUCCESS)
1658      {
1659         WRN("Failed to set mouse accel about device: %s\n",
1660             libinput_device_get_name(edev->device));
1661         return EINA_FALSE;
1662      }
1663
1664    return EINA_TRUE;
1665 }
1666
1667 EINTERN unsigned int
1668 e_input_evdev_touch_pressed_get(E_Input_Evdev *edev)
1669 {
1670    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, 0x0);
1671
1672    return edev->touch.pressed;
1673 }