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