e_input: close input device's fd in close restricted function only
[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
375    if (!(edev = libinput_device_get_user_data(device)))
376      {
377         return;
378      }
379
380    if (!(input = edev->seat->input))
381      {
382         return;
383      }
384
385    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
386    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
387      {
388         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
389           {
390              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_KEYBOARD)
391                {
392                   ecore_dev = data;
393                   break;
394                }
395           }
396      }
397    else
398      {
399         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_KEYBOARD);
400         ecore_dev = edev->ecore_dev;
401      }
402
403    if (!ecore_dev)
404      {
405         ERR("Failed to get source ecore device from event !\n");
406         return;
407      }
408
409    timestamp = libinput_event_keyboard_get_time(event);
410    code = libinput_event_keyboard_get_key(event);
411    code = _device_remapped_key_get(edev, code) + 8;
412    state = libinput_event_keyboard_get_key_state(event);
413    key_count = libinput_event_keyboard_get_seat_key_count(event);
414
415    /* ignore key events that are not seat wide state changes */
416    if (((state == LIBINPUT_KEY_STATE_PRESSED) && (key_count != 1)) ||
417        ((state == LIBINPUT_KEY_STATE_RELEASED) && (key_count != 0)))
418      {
419         return;
420      }
421
422    xkb_state_update_key(edev->xkb.state, code,
423                         (state ? XKB_KEY_DOWN : XKB_KEY_UP));
424
425    /* get the keysym for this code */
426    nsyms = xkb_key_get_syms(edev->xkb.state, code, &syms);
427    if (nsyms == 1) sym = syms[0];
428
429    /* get the keyname for this sym */
430    memset(key, 0, sizeof(key));
431    xkb_keysym_get_name(sym, key, sizeof(key));
432
433    memset(keyname, 0, sizeof(keyname));
434    memcpy(keyname, key, sizeof(keyname));
435
436    if (keyname[0] == '\0')
437      snprintf(keyname, sizeof(keyname), "Keycode-%u", code);
438
439    /* if shift is active, we need to transform the key to lower */
440    if (xkb_state_mod_index_is_active(edev->xkb.state,
441                                      xkb_map_mod_get_index(edev->xkb.keymap,
442                                      XKB_MOD_NAME_SHIFT),
443                                      XKB_STATE_MODS_EFFECTIVE))
444      {
445         if (keyname[0] != '\0')
446           keyname[0] = tolower(keyname[0]);
447      }
448
449    memset(compose_buffer, 0, sizeof(compose_buffer));
450    if (_device_keysym_translate(sym, edev->xkb.modifiers,
451                                 compose_buffer, sizeof(compose_buffer)))
452      {
453         compose = eina_str_convert("ISO8859-1", "UTF-8", compose_buffer);
454         if (!compose)
455           {
456              ERR("E Input cannot convert input key string '%s' to UTF-8. "
457                  "Is Eina built with iconv support?", compose_buffer);
458           }
459         else
460           tmp = compose;
461      }
462
463    if (!compose) compose = compose_buffer;
464
465    e = calloc(1, sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
466               ((compose[0] != '\0') ? strlen(compose) : 0) + 3);
467    if (!e)
468      {
469         E_FREE(tmp);
470         return;
471      }
472    key_data = E_NEW(E_Keyrouter_Event_Data, 1);
473    if (!key_data)
474      {
475         E_FREE(tmp);
476         E_FREE(e);
477         return;
478      }
479
480    e->keyname = (char *)(e + 1);
481    e->key = e->keyname + strlen(keyname) + 1;
482    e->compose = strlen(compose) ? e->key + strlen(key) + 1 : NULL;
483    e->string = e->compose;
484
485    strncpy((char *)e->keyname, keyname, strlen(keyname));
486    strncpy((char *)e->key, key, strlen(key));
487    if (strlen(compose)) strncpy((char *)e->compose, compose, strlen(compose));
488
489    e->window = (Ecore_Window)input->dev->window;
490    e->event_window = (Ecore_Window)input->dev->window;
491    e->root_window = (Ecore_Window)input->dev->window;
492    e->timestamp = timestamp;
493    e->same_screen = 1;
494    e->keycode = code;
495    e->data = key_data;
496
497    _device_modifiers_update(edev);
498
499    e->modifiers = edev->xkb.modifiers;
500    e->dev = ecore_device_ref(ecore_dev);
501
502    if (state)
503      ecore_event_add(ECORE_EVENT_KEY_DOWN, e, _e_input_event_key_cb_free, NULL);
504    else
505      ecore_event_add(ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
506
507    if (tmp) free(tmp);
508 }
509
510 static void
511 _device_pointer_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event)
512 {
513    E_Input_Backend *input;
514    Ecore_Event_Mouse_Move *ev;
515    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
516    Eina_List *l;
517
518    if (!(input = edev->seat->input)) return;
519
520    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
521    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
522      {
523         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
524           {
525              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
526                {
527                   ecore_dev = data;
528                   break;
529                }
530              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
531                {
532                   detent_data = data;
533                }
534           }
535         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
536           {
537              ecore_dev = detent_data;
538           }
539      }
540    else
541      {
542         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
543         ecore_dev = edev->ecore_dev;
544      }
545
546    if (!ecore_dev)
547      {
548         ERR("Failed to get source ecore device from event !\n");
549         return;
550      }
551    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
552      {
553         /* Do not process detent device's move events. */
554         return;
555      }
556
557    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
558
559    if (edev->seat->ptr.ix < edev->mouse.minx)
560      edev->seat->ptr.dx = edev->seat->ptr.ix = edev->mouse.minx;
561    else if (edev->seat->ptr.ix >= (edev->mouse.minx + edev->mouse.maxw))
562      edev->seat->ptr.dx = edev->seat->ptr.ix = (edev->mouse.minx + edev->mouse.maxw - 1);
563
564    if (edev->seat->ptr.iy < edev->mouse.miny)
565      edev->seat->ptr.dy = edev->seat->ptr.iy = edev->mouse.miny;
566    else if (edev->seat->ptr.iy >= (edev->mouse.miny + edev->mouse.maxh))
567      edev->seat->ptr.dy = edev->seat->ptr.iy = (edev->mouse.miny + edev->mouse.maxh - 1);
568
569    edev->mouse.dx = edev->seat->ptr.dx;
570    edev->mouse.dy = edev->seat->ptr.dy;
571
572    ev->window = (Ecore_Window)input->dev->window;
573    ev->event_window = (Ecore_Window)input->dev->window;
574    ev->root_window = (Ecore_Window)input->dev->window;
575    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
576    ev->same_screen = 1;
577
578    _device_modifiers_update(edev);
579    ev->modifiers = edev->xkb.modifiers;
580
581    ev->x = edev->seat->ptr.ix;
582    ev->y = edev->seat->ptr.iy;
583    ev->root.x = ev->x;
584    ev->root.y = ev->y;
585
586    ev->multi.device = edev->mt_slot;
587    ev->multi.radius = 1;
588    ev->multi.radius_x = 1;
589    ev->multi.radius_y = 1;
590    ev->multi.pressure = 1.0;
591    ev->multi.angle = 0.0;
592    ev->multi.x = ev->x;
593    ev->multi.y = ev->y;
594    ev->multi.root.x = ev->x;
595    ev->multi.root.y = ev->y;
596    ev->dev = ecore_device_ref(ecore_dev);
597
598    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
599 }
600
601 void
602 _e_input_pointer_motion_post(E_Input_Evdev *edev)
603 {
604    _device_pointer_motion(edev, NULL);
605 }
606
607 static void
608 _device_handle_pointer_motion(struct libinput_device *device, struct libinput_event_pointer *event)
609 {
610    E_Input_Evdev *edev;
611    double dx, dy, temp;
612
613    if (!(edev = libinput_device_get_user_data(device)))
614      {
615         return;
616      }
617
618    dx = libinput_event_pointer_get_dx(event);
619    dy = libinput_event_pointer_get_dy(event);
620
621    if (edev->seat->ptr.swap)
622      {
623          temp = dx;
624          dx = dy;
625          dy = temp;
626      }
627    if (edev->seat->ptr.invert_x)
628      dx *= -1;
629    if (edev->seat->ptr.invert_y)
630      dy *= -1;
631
632    edev->seat->ptr.dx += dx;
633    edev->seat->ptr.dy += dy;
634
635    edev->mouse.dx = edev->seat->ptr.dx;
636    edev->mouse.dy = edev->seat->ptr.dy;
637
638    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
639        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
640      {
641         return;
642      }
643
644    edev->seat->ptr.ix = edev->seat->ptr.dx;
645    edev->seat->ptr.iy = edev->seat->ptr.dy;
646
647   _device_pointer_motion(edev, event);
648 }
649
650 static void
651 _device_handle_pointer_motion_absolute(struct libinput_device *device, struct libinput_event_pointer *event)
652 {
653    E_Input_Evdev *edev;
654    int w = 0, h = 0;
655
656    if (!(edev = libinput_device_get_user_data(device)))
657      {
658         return;
659      }
660
661    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
662
663    edev->mouse.dx = edev->seat->ptr.dx =
664      libinput_event_pointer_get_absolute_x_transformed(event, w);
665    edev->mouse.dy = edev->seat->ptr.dy =
666      libinput_event_pointer_get_absolute_y_transformed(event, h);
667
668    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
669        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
670      {
671         return;
672      }
673
674    edev->seat->ptr.ix = edev->seat->ptr.dx;
675    edev->seat->ptr.iy = edev->seat->ptr.dy;
676    _device_pointer_motion(edev, event);
677 }
678
679 static void
680 _device_handle_button(struct libinput_device *device, struct libinput_event_pointer *event)
681 {
682    E_Input_Evdev *edev;
683    E_Input_Backend *input;
684    Ecore_Event_Mouse_Button *ev;
685    enum libinput_button_state state;
686    uint32_t button, timestamp;
687    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
688    Eina_List *l;
689
690    if (!(edev = libinput_device_get_user_data(device)))
691      {
692         return;
693      }
694    if (!(input = edev->seat->input))
695      {
696         return;
697      }
698
699    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
700    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
701      {
702         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
703           {
704              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
705                {
706                   ecore_dev = data;
707                   break;
708                }
709              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
710                {
711                   detent_data = data;
712                }
713           }
714         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
715           {
716              ecore_dev = detent_data;
717           }
718      }
719    else
720      {
721         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
722         ecore_dev = edev->ecore_dev;
723      }
724
725    if (!ecore_dev)
726      {
727         ERR("Failed to get source ecore device from event !\n");
728         return;
729      }
730
731    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button))))
732      {
733         return;
734      }
735
736    state = libinput_event_pointer_get_button_state(event);
737    button = libinput_event_pointer_get_button(event);
738    timestamp = libinput_event_pointer_get_time(event);
739
740    button = ((button & 0x00F) + 1);
741    if (button == 3) button = 2;
742    else if (button == 2) button = 3;
743
744    ev->window = (Ecore_Window)input->dev->window;
745    ev->event_window = (Ecore_Window)input->dev->window;
746    ev->root_window = (Ecore_Window)input->dev->window;
747    ev->timestamp = timestamp;
748    ev->same_screen = 1;
749
750    _device_modifiers_update(edev);
751    ev->modifiers = edev->xkb.modifiers;
752
753    ev->x = edev->seat->ptr.ix;
754    ev->y = edev->seat->ptr.iy;
755    ev->root.x = ev->x;
756    ev->root.y = ev->y;
757
758    ev->multi.device = edev->mt_slot;
759    ev->multi.radius = 1;
760    ev->multi.radius_x = 1;
761    ev->multi.radius_y = 1;
762    ev->multi.pressure = 1.0;
763    ev->multi.angle = 0.0;
764    ev->multi.x = ev->x;
765    ev->multi.y = ev->y;
766    ev->multi.root.x = ev->x;
767    ev->multi.root.y = ev->y;
768    ev->dev = ecore_device_ref(ecore_dev);
769
770    if (state)
771      {
772         unsigned int current;
773
774         current = timestamp;
775         edev->mouse.did_double = EINA_FALSE;
776         edev->mouse.did_triple = EINA_FALSE;
777
778         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
779             (button == edev->mouse.prev_button))
780           {
781              edev->mouse.did_double = EINA_TRUE;
782              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
783                  (button == edev->mouse.last_button))
784                {
785                   edev->mouse.did_triple = EINA_TRUE;
786                   edev->mouse.prev = 0;
787                   edev->mouse.last = 0;
788                   current = 0;
789                }
790           }
791
792         edev->mouse.last = edev->mouse.prev;
793         edev->mouse.prev = current;
794         edev->mouse.last_button = edev->mouse.prev_button;
795         edev->mouse.prev_button = button;
796      }
797
798    ev->buttons = button;
799
800    if (edev->mouse.did_double)
801      ev->double_click = 1;
802    if (edev->mouse.did_triple)
803      ev->triple_click = 1;
804
805    if (state)
806      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, _e_input_event_mouse_button_cb_free, NULL);
807    else
808      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, _e_input_event_mouse_button_cb_free, NULL);
809 }
810
811 static void
812 _device_handle_axis(struct libinput_device *device, struct libinput_event_pointer *event)
813 {
814    E_Input_Evdev *edev;
815    E_Input_Backend *input;
816    Ecore_Event_Mouse_Wheel *ev;
817    uint32_t timestamp;
818    enum libinput_pointer_axis axis;
819    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
820    Eina_List *l;
821
822    if (!(edev = libinput_device_get_user_data(device)))
823      {
824         return;
825      }
826    if (!(input = edev->seat->input))
827      {
828         return;
829      }
830
831    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
832    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
833      {
834         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
835           {
836              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
837                {
838                   ecore_dev = data;
839                   break;
840                }
841              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
842                {
843                   detent_data = data;
844                }
845           }
846         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
847           {
848              ecore_dev = detent_data;
849           }
850      }
851    else
852      {
853         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
854         ecore_dev = edev->ecore_dev;
855      }
856
857    if (!ecore_dev)
858      {
859         ERR("Failed to get source ecore device from event !\n");
860         return;
861      }
862
863    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
864      {
865         return;
866      }
867
868    timestamp = libinput_event_pointer_get_time(event);
869
870    ev->window = (Ecore_Window)input->dev->window;
871    ev->event_window = (Ecore_Window)input->dev->window;
872    ev->root_window = (Ecore_Window)input->dev->window;
873    ev->timestamp = timestamp;
874    ev->same_screen = 1;
875
876    _device_modifiers_update(edev);
877    ev->modifiers = edev->xkb.modifiers;
878
879    ev->x = edev->seat->ptr.ix;
880    ev->y = edev->seat->ptr.iy;
881    ev->root.x = ev->x;
882    ev->root.y = ev->y;
883    ev->dev = ecore_device_ref(ecore_dev);
884
885    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
886    if (libinput_event_pointer_has_axis(event, axis))
887      ev->z = libinput_event_pointer_get_axis_value(event, axis);
888
889    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
890    if (libinput_event_pointer_has_axis(event, axis))
891      {
892         ev->direction = 1;
893         ev->z = libinput_event_pointer_get_axis_value(event, axis);
894      }
895
896    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
897 }
898
899 static void
900 _device_handle_touch_event_send(E_Input_Evdev *edev, struct libinput_event_touch *event, int state)
901 {
902    E_Input_Backend *input;
903    Ecore_Event_Mouse_Button *ev;
904    uint32_t timestamp, button = 0;
905    Ecore_Device *ecore_dev = NULL, *data;
906    Eina_List *l;
907
908    if (!edev) return;
909    if (!(input = edev->seat->input)) return;
910
911    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
912    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
913      {
914         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
915           {
916              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
917                {
918                   ecore_dev = data;
919                   break;
920                }
921           }
922      }
923    else
924      {
925         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
926         ecore_dev = edev->ecore_dev;
927      }
928
929    if (!ecore_dev)
930      {
931         ERR("Failed to get source ecore device from event !\n");
932         return;
933      }
934
935    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) return;
936
937    timestamp = libinput_event_touch_get_time(event);
938
939    ev->window = (Ecore_Window)input->dev->window;
940    ev->event_window = (Ecore_Window)input->dev->window;
941    ev->root_window = (Ecore_Window)input->dev->window;
942    ev->timestamp = timestamp;
943    ev->same_screen = 1;
944
945    _device_modifiers_update(edev);
946    ev->modifiers = edev->xkb.modifiers;
947
948    ev->x = edev->seat->ptr.ix;
949    ev->y = edev->seat->ptr.iy;
950    ev->root.x = ev->x;
951    ev->root.y = ev->y;
952
953    ev->multi.device = edev->mt_slot;
954    ev->multi.radius = 1;
955    ev->multi.radius_x = 1;
956    ev->multi.radius_y = 1;
957    ev->multi.pressure = 1.0;
958    ev->multi.angle = 0.0;
959 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
960    if (libinput_event_get_type(libinput_event_touch_get_base_event(event))
961        == LIBINPUT_EVENT_TOUCH_DOWN)
962      {
963         if (libinput_event_touch_has_minor(event))
964           ev->multi.radius_x = libinput_event_touch_get_minor(event);
965         if (libinput_event_touch_has_major(event))
966           ev->multi.radius_y = libinput_event_touch_get_major(event);
967         if (libinput_event_touch_has_pressure(event))
968           ev->multi.pressure = libinput_event_touch_get_pressure(event);
969         if (libinput_event_touch_has_orientation(event))
970           ev->multi.angle = libinput_event_touch_get_orientation(event);
971      }
972 #endif
973    ev->multi.x = ev->x;
974    ev->multi.y = ev->y;
975    ev->multi.root.x = ev->x;
976    ev->multi.root.y = ev->y;
977    ev->dev = ecore_device_ref(ecore_dev);
978
979    if (state == ECORE_EVENT_MOUSE_BUTTON_DOWN)
980      {
981         unsigned int current;
982
983         current = timestamp;
984         edev->mouse.did_double = EINA_FALSE;
985         edev->mouse.did_triple = EINA_FALSE;
986
987         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
988             (button == edev->mouse.prev_button))
989           {
990              edev->mouse.did_double = EINA_TRUE;
991              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
992                  (button == edev->mouse.last_button))
993                {
994                   edev->mouse.did_triple = EINA_TRUE;
995                   edev->mouse.prev = 0;
996                   edev->mouse.last = 0;
997                   current = 0;
998                }
999           }
1000
1001         edev->mouse.last = edev->mouse.prev;
1002         edev->mouse.prev = current;
1003         edev->mouse.last_button = edev->mouse.prev_button;
1004         edev->mouse.prev_button = button;
1005         edev->touch.pressed |= (1 << ev->multi.device);
1006      }
1007    else
1008      {
1009         edev->touch.pressed &= ~(1 << ev->multi.device);
1010      }
1011
1012    ev->buttons = ((button & 0x00F) + 1);
1013
1014    if (edev->mouse.did_double)
1015      ev->double_click = 1;
1016    if (edev->mouse.did_triple)
1017      ev->triple_click = 1;
1018
1019    ecore_event_add(state, ev, _e_input_event_mouse_button_cb_free, NULL);
1020 }
1021
1022 static void
1023 _device_handle_touch_motion_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1024 {
1025    E_Input_Backend *input;
1026    Ecore_Event_Mouse_Move *ev;
1027    Ecore_Device *ecore_dev = NULL, *data;
1028    Eina_List *l;
1029
1030    if (!edev) return;
1031    if (!(input = edev->seat->input)) return;
1032
1033    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1034    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1035      {
1036         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1037           {
1038              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1039                {
1040                   ecore_dev = data;
1041                   break;
1042                }
1043           }
1044      }
1045    else
1046      {
1047         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1048         ecore_dev = edev->ecore_dev;
1049      }
1050
1051    if (!ecore_dev)
1052      {
1053         ERR("Failed to get source ecore device from event !\n");
1054         return;
1055      }
1056
1057    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
1058
1059    ev->window = (Ecore_Window)input->dev->window;
1060    ev->event_window = (Ecore_Window)input->dev->window;
1061    ev->root_window = (Ecore_Window)input->dev->window;
1062    ev->timestamp = libinput_event_touch_get_time(event);
1063    ev->same_screen = 1;
1064
1065    _device_modifiers_update(edev);
1066    ev->modifiers = edev->xkb.modifiers;
1067
1068    ev->x = edev->seat->ptr.ix;
1069    ev->y = edev->seat->ptr.iy;
1070    ev->root.x = ev->x;
1071    ev->root.y = ev->y;
1072
1073    ev->multi.device = edev->mt_slot;
1074    ev->multi.radius = 1;
1075    ev->multi.radius_x = 1;
1076    ev->multi.radius_y = 1;
1077    ev->multi.pressure = 1.0;
1078    ev->multi.angle = 0.0;
1079 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1080    if (libinput_event_touch_has_minor(event))
1081      ev->multi.radius_x = libinput_event_touch_get_minor(event);
1082    if (libinput_event_touch_has_major(event))
1083      ev->multi.radius_y = libinput_event_touch_get_major(event);
1084    if (libinput_event_touch_has_pressure(event))
1085      ev->multi.pressure = libinput_event_touch_get_pressure(event);
1086    if (libinput_event_touch_has_orientation(event))
1087      ev->multi.angle = libinput_event_touch_get_orientation(event);
1088 #endif
1089    ev->multi.x = ev->x;
1090    ev->multi.y = ev->y;
1091    ev->multi.root.x = ev->x;
1092    ev->multi.root.y = ev->y;
1093    ev->dev = ecore_device_ref(ecore_dev);
1094
1095    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
1096 }
1097
1098 static void
1099 _device_handle_touch_down(struct libinput_device *device, struct libinput_event_touch *event)
1100 {
1101    E_Input_Evdev *edev;
1102    int w = 0, h = 0;
1103
1104    if (!(edev = libinput_device_get_user_data(device)))
1105      {
1106         return;
1107      }
1108
1109    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1110
1111    edev->mouse.dx = edev->seat->ptr.ix = edev->seat->ptr.dx =
1112      libinput_event_touch_get_x_transformed(event, w);
1113    edev->mouse.dy = edev->seat->ptr.iy = edev->seat->ptr.dy =
1114      libinput_event_touch_get_y_transformed(event, h);
1115
1116    edev->mt_slot = libinput_event_touch_get_slot(event);
1117
1118    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1119      {
1120         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1121         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1122      }
1123
1124    _device_handle_touch_motion_send(edev, event);
1125    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_DOWN);
1126 }
1127
1128 static void
1129 _device_handle_touch_motion(struct libinput_device *device, struct libinput_event_touch *event)
1130 {
1131    E_Input_Evdev *edev;
1132    int w = 0, h = 0;
1133
1134    if (!(edev = libinput_device_get_user_data(device)))
1135      {
1136         return;
1137      }
1138
1139    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1140
1141    edev->mouse.dx = edev->seat->ptr.dx =
1142      libinput_event_touch_get_x_transformed(event, w);
1143    edev->mouse.dy = edev->seat->ptr.dy =
1144      libinput_event_touch_get_y_transformed(event, h);
1145
1146    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1147        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1148      {
1149         return;
1150      }
1151
1152    edev->seat->ptr.ix = edev->seat->ptr.dx;
1153    edev->seat->ptr.iy = edev->seat->ptr.dy;
1154
1155    edev->mt_slot = libinput_event_touch_get_slot(event);
1156
1157    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1158      {
1159         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1160         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1161      }
1162
1163    _device_handle_touch_motion_send(edev, event);
1164 }
1165
1166 static void
1167 _device_handle_touch_up(struct libinput_device *device, struct libinput_event_touch *event)
1168 {
1169    E_Input_Evdev *edev;
1170
1171    if (!(edev = libinput_device_get_user_data(device)))
1172      {
1173         return;
1174      }
1175
1176    edev->mt_slot = libinput_event_touch_get_slot(event);
1177
1178    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1179      {
1180         edev->mouse.dx = edev->seat->ptr.dx = edev->seat->ptr.ix =
1181           edev->touch.coords[edev->mt_slot].x;
1182         edev->mouse.dy = edev->seat->ptr.dy = edev->seat->ptr.iy =
1183           edev->touch.coords[edev->mt_slot].y;
1184      }
1185
1186    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_UP);
1187 }
1188
1189 static void
1190 _device_handle_touch_frame(struct libinput_device *device EINA_UNUSED, struct libinput_event_touch *event EINA_UNUSED)
1191 {
1192    /* DBG("Unhandled Touch Frame Event"); */
1193 }
1194
1195 static void
1196 _e_input_aux_data_event_free(void *user_data EINA_UNUSED, void *ev)
1197 {
1198    Ecore_Event_Axis_Update *e = (Ecore_Event_Axis_Update *)ev;
1199
1200    if (e->axis) free(e->axis);
1201    if (e->dev) ecore_device_unref(e->dev);
1202
1203    free(e);
1204 }
1205
1206 static void
1207 _device_handle_touch_aux_data(struct libinput_device *device, struct libinput_event_touch_aux_data *event)
1208 {
1209    E_Input_Evdev *edev;
1210    E_Input_Backend *input;
1211    Ecore_Event_Axis_Update *ev;
1212    Ecore_Axis *axis;
1213    Ecore_Device *ecore_dev = NULL, *data;
1214    Eina_List *l;
1215
1216    if (libinput_event_touch_aux_data_get_type(event) != LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM &&
1217        libinput_event_touch_aux_data_get_value(event) > 0)
1218       goto end;
1219
1220    if (!(edev = libinput_device_get_user_data(device))) goto end;
1221    if (!(input = edev->seat->input)) goto end;
1222
1223    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1224    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1225      {
1226         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1227           {
1228              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1229                {
1230                   ecore_dev = data;
1231                   break;
1232                }
1233           }
1234      }
1235    else
1236      {
1237         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1238         ecore_dev = edev->ecore_dev;
1239      }
1240
1241    if (!ecore_dev)
1242      {
1243         ERR("Failed to get source ecore device from event !\n");
1244         goto end;
1245      }
1246
1247    if (!(ev = calloc(1, sizeof(Ecore_Event_Axis_Update))))goto end;
1248
1249    ev->window = (Ecore_Window)input->dev->window;
1250    ev->event_window = (Ecore_Window)input->dev->window;
1251    ev->root_window = (Ecore_Window)input->dev->window;
1252    ev->timestamp = libinput_event_touch_aux_data_get_time(event);
1253
1254    axis = (Ecore_Axis *)calloc(1, sizeof(Ecore_Axis));
1255    if (axis)
1256      {
1257         axis->label = ECORE_AXIS_LABEL_TOUCH_PALM;
1258         axis->value = libinput_event_touch_aux_data_get_value(event);
1259         ev->naxis = 1;
1260      }
1261    ev->axis = axis;
1262    ev->dev = ecore_device_ref(ecore_dev);
1263
1264    ecore_event_add(ECORE_EVENT_AXIS_UPDATE, ev, _e_input_aux_data_event_free, NULL);
1265
1266 end:
1267    ;
1268 }
1269
1270 E_Input_Evdev *
1271 _e_input_evdev_device_create(E_Input_Seat *seat, struct libinput_device *device)
1272 {
1273    E_Input_Evdev *edev;
1274    E_Input_Backend *b_input;
1275
1276    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
1277
1278    /* try to allocate space for new evdev */
1279    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
1280
1281    edev->seat = seat;
1282    edev->device = device;
1283    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
1284
1285    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
1286      {
1287         edev->caps |= E_INPUT_SEAT_KEYBOARD;
1288         _device_keyboard_setup(edev);
1289      }
1290
1291    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
1292      {
1293         edev->caps |= E_INPUT_SEAT_POINTER;
1294
1295         /* TODO: make this configurable */
1296         edev->mouse.threshold = 250;
1297
1298         b_input = seat->input;
1299         if (b_input->left_handed == EINA_TRUE)
1300           {
1301              if (libinput_device_config_left_handed_set(device, 1) !=
1302                  LIBINPUT_CONFIG_STATUS_SUCCESS)
1303                {
1304                   WRN("Failed to set left hand mode about device: %s\n",
1305                       libinput_device_get_name(device));
1306                }
1307           }
1308      }
1309
1310    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
1311      {
1312         int palm_code;
1313         edev->caps |= E_INPUT_SEAT_TOUCH;
1314         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
1315         if (libinput_device_touch_has_aux_data(device, palm_code))
1316           {
1317              libinput_device_touch_set_aux_data(device, palm_code);
1318           }
1319      }
1320
1321    libinput_device_set_user_data(device, edev);
1322    libinput_device_ref(device);
1323
1324    /* configure device */
1325    _device_configure(edev);
1326
1327    return edev;
1328 }
1329
1330 void
1331 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
1332 {
1333    Ecore_Device *dev;
1334
1335    EINA_SAFETY_ON_NULL_RETURN(edev);
1336
1337    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
1338      {
1339         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
1340         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
1341      }
1342
1343    if (edev->ecore_dev) ecore_device_del(edev->ecore_dev);
1344    if (edev->ecore_dev_list)
1345      EINA_LIST_FREE(edev->ecore_dev_list, dev)
1346        {
1347           ecore_device_del(dev);
1348        }
1349    if (edev->path) eina_stringshare_del(edev->path);
1350    if (edev->device) libinput_device_unref(edev->device);
1351    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
1352
1353    free(edev);
1354 }
1355
1356 Eina_Bool
1357 _e_input_evdev_event_process(struct libinput_event *event)
1358 {
1359    struct libinput_device *device;
1360    Eina_Bool ret = EINA_TRUE;
1361
1362    device = libinput_event_get_device(event);
1363    switch (libinput_event_get_type(event))
1364      {
1365       case LIBINPUT_EVENT_KEYBOARD_KEY:
1366         _device_handle_key(device, libinput_event_get_keyboard_event(event));
1367         break;
1368       case LIBINPUT_EVENT_POINTER_MOTION:
1369         _device_handle_pointer_motion(device,
1370                                       libinput_event_get_pointer_event(event));
1371         break;
1372       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
1373         _device_handle_pointer_motion_absolute(device,
1374                                                libinput_event_get_pointer_event(event));
1375         break;
1376       case LIBINPUT_EVENT_POINTER_BUTTON:
1377         _device_handle_button(device, libinput_event_get_pointer_event(event));
1378         break;
1379       case LIBINPUT_EVENT_POINTER_AXIS:
1380         _device_handle_axis(device, libinput_event_get_pointer_event(event));
1381         break;
1382       case LIBINPUT_EVENT_TOUCH_DOWN:
1383         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
1384         break;
1385       case LIBINPUT_EVENT_TOUCH_MOTION:
1386         _device_handle_touch_motion(device,
1387                                     libinput_event_get_touch_event(event));
1388         break;
1389       case LIBINPUT_EVENT_TOUCH_UP:
1390         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
1391         break;
1392       case LIBINPUT_EVENT_TOUCH_FRAME:
1393         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
1394         break;
1395       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
1396         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
1397         break;
1398       default:
1399         ret = EINA_FALSE;
1400         break;
1401      }
1402
1403    return ret;
1404 }
1405
1406 /**
1407  * @brief Set the axis size of the given device.
1408  *
1409  * @param dev The device to set the axis size to.
1410  * @param w The width of the axis.
1411  * @param h The height of the axis.
1412  *
1413  * This function sets set the width @p w and height @p h of the axis
1414  * of device @p dev. If @p dev is a relative input device, a width and
1415  * height must set for it. If its absolute set the ioctl correctly, if
1416  * not, unsupported device.
1417  */
1418 EINTERN void
1419 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
1420 {
1421    const char *sysname;
1422    float cal[6];
1423    const char *device;
1424    Eina_List *devices;
1425    const char *vals;
1426    enum libinput_config_status status;
1427
1428    EINA_SAFETY_ON_NULL_RETURN(edev);
1429    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
1430
1431    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
1432        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
1433      return;
1434
1435    sysname = libinput_device_get_sysname(edev->device);
1436
1437    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
1438    if (eina_list_count(devices) < 1) return;
1439
1440    EINA_LIST_FREE(devices, device)
1441      {
1442         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
1443         if ((!vals) ||
1444             (sscanf(vals, "%f %f %f %f %f %f",
1445                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
1446           goto cont;
1447
1448         cal[2] /= w;
1449         cal[5] /= h;
1450
1451         status =
1452           libinput_device_config_calibration_set_matrix(edev->device, cal);
1453
1454         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
1455           ERR("Failed to apply calibration");
1456
1457 cont:
1458         eina_stringshare_del(device);
1459         continue;
1460      }
1461 }
1462
1463 E_API const char *
1464 e_input_evdev_name_get(E_Input_Evdev *evdev)
1465 {
1466    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1467    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1468
1469    return libinput_device_get_name(evdev->device);
1470 }
1471
1472 EINTERN const char *
1473 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
1474 {
1475    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1476    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1477
1478    return libinput_device_get_sysname(evdev->device);
1479 }
1480
1481 EINTERN Eina_Bool
1482 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
1483 {
1484    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1485    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1486
1487    edev->key_remap_enabled = enable;
1488
1489    if (enable == EINA_FALSE && edev->key_remap_hash)
1490      {
1491         eina_hash_free(edev->key_remap_hash);
1492         edev->key_remap_hash = NULL;
1493      }
1494
1495    return EINA_TRUE;
1496 }
1497
1498 EINTERN Eina_Bool
1499 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
1500 {
1501    int i;
1502
1503    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1504    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1505    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
1506    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
1507    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
1508    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
1509
1510    if (edev->key_remap_hash == NULL)
1511      edev->key_remap_hash = eina_hash_int32_new(NULL);
1512
1513    if (edev->key_remap_hash == NULL)
1514      {
1515         ERR("Failed to set remap key information : creating a hash is failed.");
1516         return EINA_FALSE;
1517      }
1518
1519    for (i = 0; i < num ; i++)
1520      {
1521         if (!from_keys[i] || !to_keys[i])
1522           {
1523              ERR("Failed to set remap key information : given arguments are invalid.");
1524              return EINA_FALSE;
1525           }
1526      }
1527
1528    for (i = 0; i < num ; i++)
1529      {
1530         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
1531      }
1532
1533    return EINA_TRUE;
1534 }
1535
1536 E_API int
1537 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
1538 {
1539    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
1540    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
1541 }
1542
1543 EINTERN Eina_Bool
1544 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
1545 {
1546    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1547    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1548
1549    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
1550        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
1551      return EINA_FALSE;
1552
1553    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
1554        LIBINPUT_CONFIG_STATUS_SUCCESS)
1555      {
1556         WRN("Failed to set input transformation about device: %s\n",
1557             libinput_device_get_name(edev->device));
1558         return EINA_FALSE;
1559      }
1560
1561    return EINA_TRUE;
1562 }
1563
1564 EAPI Eina_Bool
1565 e_input_evdev_mouse_accel_speed_set(E_Input_Evdev *edev, double speed)
1566 {
1567    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1568    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1569
1570    if (!libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
1571      return EINA_FALSE;
1572
1573    if (!libinput_device_config_accel_is_available(edev->device))
1574      return EINA_FALSE;
1575
1576    if (libinput_device_config_accel_set_speed(edev->device, speed) !=
1577        LIBINPUT_CONFIG_STATUS_SUCCESS)
1578      {
1579         WRN("Failed to set mouse accel about device: %s\n",
1580             libinput_device_get_name(edev->device));
1581         return EINA_FALSE;
1582      }
1583
1584    return EINA_TRUE;
1585 }
1586
1587 EINTERN unsigned int
1588 e_input_evdev_touch_pressed_get(E_Input_Evdev *edev)
1589 {
1590    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, 0x0);
1591
1592    return edev->touch.pressed;
1593 }