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