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