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