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