e_input: block input events in e_input layer insteads of devicemgr
[platform/upstream/enlightenment.git] / src / bin / e_input_evdev.c
1 #include "e.h"
2 #include "e_input_private.h"
3
4 static void  _device_modifiers_update(E_Input_Evdev *edev);
5
6 void
7 _device_calibration_set(E_Input_Evdev *edev)
8 {
9    E_Output *output;
10    int w = 0, h = 0;
11    int temp;
12
13    output = e_comp_screen_primary_output_get(e_comp->e_comp_screen);
14    e_output_size_get(output, &w, &h);
15
16    if (output)
17      {
18         edev->mouse.minx = edev->mouse.miny = 0;
19         edev->mouse.maxw = w;
20         edev->mouse.maxh = h;
21
22         if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
23           {
24              edev->seat->ptr.dx = (double)(w / 2);
25              edev->seat->ptr.dy = (double)(h / 2);
26              edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
27              edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
28              edev->mouse.dx = edev->seat->ptr.dx;
29              edev->mouse.dy = edev->seat->ptr.dy;
30
31              if (output->config.rotation == 90 || output->config.rotation == 270)
32                {
33                   temp = edev->mouse.minx;
34                   edev->mouse.minx = edev->mouse.miny;
35                   edev->mouse.miny = temp;
36
37                   temp = edev->mouse.maxw;
38                   edev->mouse.maxw = edev->mouse.maxh;
39                   edev->mouse.maxh = temp;
40                }
41           }
42      }
43
44 //LCOV_EXCL_START
45 #ifdef _F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
46    const char *sysname;
47    float cal[6];
48    const char *device;
49    Eina_List *devices;
50
51    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
52        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
53      return;
54
55    sysname = libinput_device_get_sysname(edev->device);
56
57    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
58    if (eina_list_count(devices) < 1) return;
59
60 #ifdef _F_E_INPUT_USE_WL_CALIBRATION_
61    const char *vals;
62    enum libinput_config_status status;
63
64    EINA_LIST_FREE(devices, device)
65      {
66         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
67         if ((!vals) ||
68             (sscanf(vals, "%f %f %f %f %f %f",
69                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
70           goto cont;
71
72         cal[2] /= w;
73         cal[5] /= h;
74
75         status =
76           libinput_device_config_calibration_set_matrix(edev->device, cal);
77
78         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
79           ERR("Failed to apply calibration");
80
81 cont:
82         eina_stringshare_del(device);
83         continue;
84      }
85 #endif//_F_E_INPUT_USE_WL_CALIBRATION_
86 #endif//_F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
87 //LCOV_EXCL_STOP
88 }
89
90 static void
91 _device_configure(E_Input_Evdev *edev)
92 {
93    if (libinput_device_config_tap_get_finger_count(edev->device) > 0)
94      {
95         Eina_Bool tap = EINA_FALSE;
96
97         tap = libinput_device_config_tap_get_default_enabled(edev->device);
98         libinput_device_config_tap_set_enabled(edev->device, tap);
99      }
100
101    _device_calibration_set(edev);
102 }
103
104 static void
105 _device_keyboard_setup(E_Input_Evdev *edev)
106 {
107    E_Input_Backend *input;
108    xkb_mod_index_t xkb_idx;
109
110    if ((!edev) || (!edev->seat)) return;
111    if (!(input = edev->seat->input)) return;
112    if (!input->dev->xkb_ctx) return;
113
114    /* create keymap from xkb context */
115    edev->xkb.keymap = _e_input_device_cached_keymap_get(input->dev->xkb_ctx, NULL, 0);
116    if (!edev->xkb.keymap)
117      {
118         ERR("Failed to create keymap: %m");
119         return;
120      }
121
122    /* create xkb state */
123    if (!(edev->xkb.state = xkb_state_new(edev->xkb.keymap)))
124      {
125         ERR("Failed to create xkb state: %m");
126         return;
127      }
128
129    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CTRL);
130    if (xkb_idx != XKB_MOD_INVALID)
131      edev->xkb.ctrl_mask = 1 << xkb_idx;
132    else
133      edev->xkb.ctrl_mask = 0;
134
135    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_ALT);
136    if (xkb_idx != XKB_MOD_INVALID)
137      edev->xkb.alt_mask = 1 << xkb_idx;
138    else
139      edev->xkb.alt_mask = 0;
140
141    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_SHIFT);
142    if (xkb_idx != XKB_MOD_INVALID)
143      edev->xkb.shift_mask = 1 << xkb_idx;
144    else
145      edev->xkb.shift_mask = 0;
146
147    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_LOGO);
148    if (xkb_idx != XKB_MOD_INVALID)
149      edev->xkb.win_mask = 1 << xkb_idx;
150    else
151      edev->xkb.win_mask = 0;
152
153    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_SCROLL);
154    if (xkb_idx != XKB_MOD_INVALID)
155      edev->xkb.scroll_mask = 1 << xkb_idx;
156    else
157      edev->xkb.scroll_mask = 0;
158
159    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_NUM);
160    if (xkb_idx != XKB_MOD_INVALID)
161      edev->xkb.num_mask = 1 << xkb_idx;
162    else
163      edev->xkb.num_mask = 0;
164
165    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CAPS);
166    if (xkb_idx != XKB_MOD_INVALID)
167      edev->xkb.caps_mask = 1 << xkb_idx;
168    else
169      edev->xkb.caps_mask = 0;
170
171    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, "ISO_Level3_Shift");
172    if (xkb_idx != XKB_MOD_INVALID)
173      edev->xkb.altgr_mask = 1 << xkb_idx;
174    else
175      edev->xkb.altgr_mask = 0;
176 }
177
178 static int
179 _device_keysym_translate(xkb_keysym_t keysym, unsigned int modifiers, char *buffer, int bytes)
180 {
181    unsigned long hbytes = 0;
182    unsigned char c;
183
184    if (!keysym) return 0;
185    hbytes = (keysym >> 8);
186
187    if (!(bytes &&
188          ((hbytes == 0) ||
189           ((hbytes == 0xFF) &&
190            (((keysym >= XKB_KEY_BackSpace) && (keysym <= XKB_KEY_Clear)) ||
191             (keysym == XKB_KEY_Return) || (keysym == XKB_KEY_Escape) ||
192             (keysym == XKB_KEY_KP_Space) || (keysym == XKB_KEY_KP_Tab) ||
193             (keysym == XKB_KEY_KP_Enter) ||
194             ((keysym >= XKB_KEY_KP_Multiply) && (keysym <= XKB_KEY_KP_9)) ||
195             (keysym == XKB_KEY_KP_Equal) || (keysym == XKB_KEY_Delete))))))
196      return 0;
197
198    if (keysym == XKB_KEY_KP_Space)
199      c = (XKB_KEY_space & 0x7F);
200    else if (hbytes == 0xFF)
201      c = (keysym & 0x7F);
202    else
203      c = (keysym & 0xFF);
204
205    if (modifiers & ECORE_EVENT_MODIFIER_CTRL)
206      {
207         if (((c >= '@') && (c < '\177')) || c == ' ')
208           c &= 0x1F;
209         else if (c == '2')
210           c = '\000';
211         else if ((c >= '3') && (c <= '7'))
212           c -= ('3' - '\033');
213         else if (c == '8')
214           c = '\177';
215         else if (c == '/')
216           c = '_' & 0x1F;
217      }
218    buffer[0] = c;
219    return 1;
220 }
221
222 static void
223 _device_modifiers_update_device(E_Input_Evdev *edev, E_Input_Evdev *from)
224 {
225    xkb_mod_mask_t mask;
226
227    edev->xkb.depressed =
228      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_DEPRESSED);
229    edev->xkb.latched =
230      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LATCHED);
231    edev->xkb.locked =
232      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LOCKED);
233    edev->xkb.group =
234      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_EFFECTIVE);
235
236    mask = (edev->xkb.depressed | edev->xkb.latched);
237
238    if (mask & from->xkb.ctrl_mask)
239      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_CTRL;
240    if (mask & from->xkb.alt_mask)
241      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALT;
242    if (mask & from->xkb.shift_mask)
243      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
244    if (mask & from->xkb.win_mask)
245      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_WIN;
246    if (mask & from->xkb.scroll_mask)
247      edev->xkb.modifiers |= ECORE_EVENT_LOCK_SCROLL;
248    if (mask & from->xkb.num_mask)
249      edev->xkb.modifiers |= ECORE_EVENT_LOCK_NUM;
250    if (mask & from->xkb.caps_mask)
251      edev->xkb.modifiers |= ECORE_EVENT_LOCK_CAPS;
252    if (mask & from->xkb.altgr_mask)
253      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALTGR;
254 }
255
256 static void
257 _device_modifiers_update(E_Input_Evdev *edev)
258 {
259    Eina_List *l;
260    E_Input_Evdev *ed;
261
262    edev->xkb.modifiers = 0;
263
264    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
265      _device_modifiers_update_device(edev, edev);
266    else
267      {
268         EINA_LIST_FOREACH(edev->seat->devices, l, ed)
269           {
270              if (!(ed->caps & E_INPUT_SEAT_KEYBOARD)) continue;
271              _device_modifiers_update_device(edev, ed);
272           }
273      }
274
275 }
276
277 static int
278 _device_remapped_key_get(E_Input_Evdev *edev, int code)
279 {
280    void *ret = NULL;
281
282    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, code);
283    if (!edev->key_remap_enabled) return code;
284    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->key_remap_hash, code);
285
286    ret = eina_hash_find(edev->key_remap_hash, &code);
287
288    if (ret) code = (int)(intptr_t)ret;
289
290    return code;
291 }
292
293 E_API Ecore_Device *
294 e_input_evdev_get_ecore_device(const char *path, Ecore_Device_Class clas)
295 {
296    const Eina_List *dev_list = NULL;
297    const Eina_List *l;
298    Ecore_Device *dev = NULL;
299    const char *identifier;
300
301    if (!path) return NULL;
302
303    dev_list = ecore_device_list();
304    if (!dev_list) return NULL;
305    EINA_LIST_FOREACH(dev_list, l, dev)
306      {
307         if (!dev) continue;
308         identifier = ecore_device_identifier_get(dev);
309         if (!identifier) continue;
310         if ((ecore_device_class_get(dev) == clas) && !(strcmp(identifier, path)))
311           return dev;
312      }
313    return NULL;
314 }
315
316 static void
317 _e_input_event_mouse_move_cb_free(void *data EINA_UNUSED, void *event)
318 {
319    Ecore_Event_Mouse_Move *ev = event;
320
321    if (ev->dev) ecore_device_unref(ev->dev);
322
323    free(ev);
324 }
325
326 static void
327 _e_input_event_mouse_wheel_cb_free(void *data EINA_UNUSED, void *event)
328 {
329    Ecore_Event_Mouse_Wheel *ev = event;
330
331    if (ev->dev) ecore_device_unref(ev->dev);
332
333    free(ev);
334 }
335
336 static void
337 _e_input_event_mouse_button_cb_free(void *data EINA_UNUSED, void *event)
338 {
339    Ecore_Event_Mouse_Button *ev = event;
340
341    if (ev->dev) ecore_device_unref(ev->dev);
342
343    free(ev);
344 }
345
346 static void
347 _e_input_event_key_cb_free(void *data EINA_UNUSED, void *event)
348 {
349    Ecore_Event_Key *ev = event;
350
351    if (ev->dev) ecore_device_unref(ev->dev);
352    if (ev->data) E_FREE(ev->data);
353
354    free(ev);
355 }
356
357 static void
358 _device_handle_key(struct libinput_device *device, struct libinput_event_keyboard *event)
359 {
360    E_Input_Evdev *edev;
361    E_Input_Backend *input;
362    uint32_t timestamp;
363    uint32_t code, nsyms;
364    const xkb_keysym_t *syms;
365    enum libinput_key_state state;
366    int key_count;
367    xkb_keysym_t sym = XKB_KEY_NoSymbol;
368    char key[256] = {0, }, keyname[256] = {0, }, compose_buffer[256] = {0, };
369    Ecore_Event_Key *e;
370    char *tmp = NULL, *compose = NULL;
371    E_Keyrouter_Event_Data *key_data;
372    Ecore_Device *ecore_dev = NULL, *data;
373    Eina_List *l, *l_next;
374    E_Comp_Config *comp_conf = NULL;
375    int *pressed_keycode = NULL, *idata = NULL;
376    Eina_Bool dup_found = EINA_FALSE;
377
378    if (!(edev = libinput_device_get_user_data(device)))
379      {
380         return;
381      }
382
383    if (!(input = edev->seat->input))
384      {
385         return;
386      }
387
388    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
389    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
390      {
391         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
392           {
393              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_KEYBOARD)
394                {
395                   ecore_dev = data;
396                   break;
397                }
398           }
399      }
400    else
401      {
402         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_KEYBOARD);
403         ecore_dev = edev->ecore_dev;
404      }
405
406    if (!ecore_dev)
407      {
408         ERR("Failed to get source ecore device from event !\n");
409         return;
410      }
411
412    timestamp = libinput_event_keyboard_get_time(event);
413    code = libinput_event_keyboard_get_key(event);
414    code = _device_remapped_key_get(edev, code + 8);
415    state = libinput_event_keyboard_get_key_state(event);
416    key_count = libinput_event_keyboard_get_seat_key_count(event);
417
418    if (state == LIBINPUT_KEY_STATE_PRESSED)
419      {
420         if ((edev->seat->dev->blocked & E_INPUT_SEAT_KEYBOARD) ||
421             (edev->seat->dev->server_blocked & E_INPUT_SEAT_KEYBOARD))
422           {
423              ELOGF("Key", "Press (keycode: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
424                    code, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
425                    edev->seat->dev->server_blocked);
426              return;
427           }
428
429         /* FIXME: Currently to maintain press/release event pair during input block,
430          *        used Eina_List and this is method used in devicemgr module.
431          *        But we need to consider which way is better to maintain key press/release pair.
432          */
433
434         EINA_LIST_FOREACH(edev->xkb.pressed_keys, l, idata)
435           {
436              if (*idata == code)
437                {
438                   dup_found = EINA_TRUE;
439                   break;
440                }
441           }
442         if (!dup_found)
443           {
444              pressed_keycode = E_NEW(int, 1);
445              EINA_SAFETY_ON_NULL_RETURN(pressed_keycode);
446              *pressed_keycode = code;
447
448              edev->xkb.pressed_keys = eina_list_append(edev->xkb.pressed_keys, pressed_keycode);
449           }
450      }
451    else
452      {
453         dup_found = EINA_FALSE;
454         EINA_LIST_FOREACH_SAFE(edev->xkb.pressed_keys, l, l_next, idata)
455           {
456              if (code == *idata)
457                {
458                   edev->xkb.pressed_keys = eina_list_remove_list(edev->xkb.pressed_keys, l);
459                   E_FREE(idata);
460                   dup_found = EINA_TRUE;
461                   break;
462                }
463           }
464         if (!dup_found)
465           {
466              ELOGF("Key", "Release (keycode: %d, device: %s) is blocked by %p", NULL,
467                    code, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client);
468              return;
469           }
470      }
471
472    /* ignore key events that are not seat wide state changes */
473    if (((state == LIBINPUT_KEY_STATE_PRESSED) && (key_count != 1)) ||
474        ((state == LIBINPUT_KEY_STATE_RELEASED) && (key_count != 0)))
475      {
476         return;
477      }
478
479    xkb_state_update_key(edev->xkb.state, code,
480                         (state ? XKB_KEY_DOWN : XKB_KEY_UP));
481
482    /* get the keysym for this code */
483    nsyms = xkb_key_get_syms(edev->xkb.state, code, &syms);
484    if (nsyms == 1) sym = syms[0];
485
486    /* If no keysym was found, name it "Keycode-NNN" */
487    if (sym == XKB_KEY_NoSymbol)
488      snprintf(key, sizeof(key), "Keycode-%u", code);
489    else
490      {
491         /* get the keyname for this sym */
492         xkb_keysym_get_name(sym, key, sizeof(key));
493      }
494
495    if (key[0] == '\0')
496      {
497         /* If no keyname was found, name it "Keycode-NNN" */
498         snprintf(keyname, sizeof(keyname), "Keycode-%u", code);
499      }
500    else
501      {
502         memcpy(keyname, key, sizeof(keyname));
503      }
504
505    /* if shift is active, we need to transform the key to lower */
506    if (xkb_state_mod_index_is_active(edev->xkb.state,
507                                      xkb_map_mod_get_index(edev->xkb.keymap,
508                                      XKB_MOD_NAME_SHIFT),
509                                      XKB_STATE_MODS_EFFECTIVE))
510      {
511         if (keyname[0] != '\0')
512           keyname[0] = tolower(keyname[0]);
513      }
514
515    if (_device_keysym_translate(sym, edev->xkb.modifiers,
516                                 compose_buffer, sizeof(compose_buffer)))
517      {
518         compose = eina_str_convert("ISO8859-1", "UTF-8", compose_buffer);
519         if (!compose)
520           {
521              ERR("E Input cannot convert input key string '%s' to UTF-8. "
522                  "Is Eina built with iconv support?", compose_buffer);
523           }
524         else
525           tmp = compose;
526      }
527
528    if (!compose) compose = compose_buffer;
529
530    e = calloc(1, sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
531               ((compose[0] != '\0') ? strlen(compose) : 0) + 3);
532    if (!e)
533      {
534         E_FREE(tmp);
535         return;
536      }
537    key_data = E_NEW(E_Keyrouter_Event_Data, 1);
538    if (!key_data)
539      {
540         E_FREE(tmp);
541         E_FREE(e);
542         return;
543      }
544
545    e->keyname = (char *)(e + 1);
546    e->key = e->keyname + strlen(keyname) + 1;
547    e->compose = strlen(compose) ? e->key + strlen(key) + 1 : NULL;
548    e->string = e->compose;
549
550    strncpy((char *)e->keyname, keyname, strlen(keyname));
551    strncpy((char *)e->key, key, strlen(key));
552    if (strlen(compose)) strncpy((char *)e->compose, compose, strlen(compose));
553
554    e->window = (Ecore_Window)input->dev->window;
555    e->event_window = (Ecore_Window)input->dev->window;
556    e->root_window = (Ecore_Window)input->dev->window;
557    e->timestamp = timestamp;
558    e->same_screen = 1;
559    e->keycode = code;
560    e->data = key_data;
561
562    _device_modifiers_update(edev);
563
564    e->modifiers = edev->xkb.modifiers;
565    e->dev = ecore_device_ref(ecore_dev);
566
567    comp_conf = e_comp_config_get();
568    if (comp_conf && comp_conf->input_log_enable)
569      ELOGF("Key", "%s (keyname: %s, keycode: %d, device: %s)", NULL, state?"Press":"Release", e->keyname, e->keycode, ecore_device_name_get(e->dev));
570
571    if (state)
572      ecore_event_add(ECORE_EVENT_KEY_DOWN, e, _e_input_event_key_cb_free, NULL);
573    else
574      ecore_event_add(ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
575
576    if (tmp) free(tmp);
577 }
578
579 static void
580 _device_pointer_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event)
581 {
582    E_Input_Backend *input;
583    Ecore_Event_Mouse_Move *ev;
584    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
585    Eina_List *l;
586
587    if (!(input = edev->seat->input)) return;
588
589    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
590    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
591      {
592         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
593           {
594              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
595                {
596                   ecore_dev = data;
597                   break;
598                }
599              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
600                {
601                   detent_data = data;
602                }
603           }
604         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
605           {
606              ecore_dev = detent_data;
607           }
608      }
609    else
610      {
611         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
612         ecore_dev = edev->ecore_dev;
613      }
614
615    if (!ecore_dev)
616      {
617         ERR("Failed to get source ecore device from event !\n");
618         return;
619      }
620    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
621      {
622         /* Do not process detent device's move events. */
623         return;
624      }
625
626    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
627
628    if (edev->seat->ptr.ix < edev->mouse.minx)
629      edev->seat->ptr.dx = edev->seat->ptr.ix = edev->mouse.minx;
630    else if (edev->seat->ptr.ix >= (edev->mouse.minx + edev->mouse.maxw))
631      edev->seat->ptr.dx = edev->seat->ptr.ix = (edev->mouse.minx + edev->mouse.maxw - 1);
632
633    if (edev->seat->ptr.iy < edev->mouse.miny)
634      edev->seat->ptr.dy = edev->seat->ptr.iy = edev->mouse.miny;
635    else if (edev->seat->ptr.iy >= (edev->mouse.miny + edev->mouse.maxh))
636      edev->seat->ptr.dy = edev->seat->ptr.iy = (edev->mouse.miny + edev->mouse.maxh - 1);
637
638    edev->mouse.dx = edev->seat->ptr.dx;
639    edev->mouse.dy = edev->seat->ptr.dy;
640
641    ev->window = (Ecore_Window)input->dev->window;
642    ev->event_window = (Ecore_Window)input->dev->window;
643    ev->root_window = (Ecore_Window)input->dev->window;
644    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
645    ev->same_screen = 1;
646
647    _device_modifiers_update(edev);
648    ev->modifiers = edev->xkb.modifiers;
649
650    ev->x = edev->seat->ptr.ix;
651    ev->y = edev->seat->ptr.iy;
652    ev->root.x = ev->x;
653    ev->root.y = ev->y;
654
655    ev->multi.device = edev->mt_slot;
656    ev->multi.radius = 1;
657    ev->multi.radius_x = 1;
658    ev->multi.radius_y = 1;
659    ev->multi.pressure = 1.0;
660    ev->multi.angle = 0.0;
661    ev->multi.x = ev->x;
662    ev->multi.y = ev->y;
663    ev->multi.root.x = ev->x;
664    ev->multi.root.y = ev->y;
665    ev->dev = ecore_device_ref(ecore_dev);
666
667    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
668 }
669
670 void
671 _e_input_pointer_motion_post(E_Input_Evdev *edev)
672 {
673    _device_pointer_motion(edev, NULL);
674 }
675
676 static void
677 _device_handle_pointer_motion(struct libinput_device *device, struct libinput_event_pointer *event)
678 {
679    E_Input_Evdev *edev;
680    double dx, dy, temp;
681
682    if (!(edev = libinput_device_get_user_data(device)))
683      {
684         return;
685      }
686
687    dx = libinput_event_pointer_get_dx(event);
688    dy = libinput_event_pointer_get_dy(event);
689
690    if (edev->seat->ptr.swap)
691      {
692          temp = dx;
693          dx = dy;
694          dy = temp;
695      }
696    if (edev->seat->ptr.invert_x)
697      dx *= -1;
698    if (edev->seat->ptr.invert_y)
699      dy *= -1;
700
701    edev->seat->ptr.dx += dx;
702    edev->seat->ptr.dy += dy;
703
704    edev->mouse.dx = edev->seat->ptr.dx;
705    edev->mouse.dy = edev->seat->ptr.dy;
706
707    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
708        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
709      {
710         return;
711      }
712
713    edev->seat->ptr.ix = edev->seat->ptr.dx;
714    edev->seat->ptr.iy = edev->seat->ptr.dy;
715
716    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
717        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
718      {
719         return;
720      }
721
722   _device_pointer_motion(edev, event);
723 }
724
725 static void
726 _device_handle_pointer_motion_absolute(struct libinput_device *device, struct libinput_event_pointer *event)
727 {
728    E_Input_Evdev *edev;
729    int w = 0, h = 0;
730
731    if (!(edev = libinput_device_get_user_data(device)))
732      {
733         return;
734      }
735
736    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
737
738    edev->mouse.dx = edev->seat->ptr.dx =
739      libinput_event_pointer_get_absolute_x_transformed(event, w);
740    edev->mouse.dy = edev->seat->ptr.dy =
741      libinput_event_pointer_get_absolute_y_transformed(event, h);
742
743    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
744        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
745      {
746         return;
747      }
748
749    edev->seat->ptr.ix = edev->seat->ptr.dx;
750    edev->seat->ptr.iy = edev->seat->ptr.dy;
751
752    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
753        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
754      {
755         return;
756      }
757
758    _device_pointer_motion(edev, event);
759 }
760
761 static void
762 _device_handle_button(struct libinput_device *device, struct libinput_event_pointer *event)
763 {
764    E_Input_Evdev *edev;
765    E_Input_Backend *input;
766    Ecore_Event_Mouse_Button *ev;
767    enum libinput_button_state state;
768    uint32_t button, timestamp;
769    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
770    Eina_List *l;
771    E_Comp_Config *comp_conf = NULL;
772
773    if (!(edev = libinput_device_get_user_data(device)))
774      {
775         return;
776      }
777    if (!(input = edev->seat->input))
778      {
779         return;
780      }
781
782    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
783    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
784      {
785         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
786           {
787              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
788                {
789                   ecore_dev = data;
790                   break;
791                }
792              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
793                {
794                   detent_data = data;
795                }
796           }
797         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
798           {
799              ecore_dev = detent_data;
800           }
801      }
802    else
803      {
804         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
805         ecore_dev = edev->ecore_dev;
806      }
807
808    if (!ecore_dev)
809      {
810         ERR("Failed to get source ecore device from event !\n");
811         return;
812      }
813
814    state = libinput_event_pointer_get_button_state(event);
815    button = libinput_event_pointer_get_button(event);
816    timestamp = libinput_event_pointer_get_time(event);
817
818    button = ((button & 0x00F) + 1);
819    if (button == 3) button = 2;
820    else if (button == 2) button = 3;
821
822    if (state)
823      {
824         if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
825             (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
826           {
827              ELOGF("Mouse", "Button Press (btn: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
828                    button, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
829                    edev->seat->dev->server_blocked);
830              return;
831           }
832         else
833           {
834              edev->mouse.pressed_button |= (1 << button);
835           }
836      }
837    else
838      {
839         if (!(edev->mouse.pressed_button & (1 << button)))
840           {
841              ELOGF("Mouse", "Button Release (btn: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
842                    button, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
843                    edev->seat->dev->server_blocked);
844              return;
845           }
846         edev->mouse.pressed_button &= ~(1 << button);
847      }
848
849    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button))))
850      {
851         return;
852      }
853
854    ev->window = (Ecore_Window)input->dev->window;
855    ev->event_window = (Ecore_Window)input->dev->window;
856    ev->root_window = (Ecore_Window)input->dev->window;
857    ev->timestamp = timestamp;
858    ev->same_screen = 1;
859
860    _device_modifiers_update(edev);
861    ev->modifiers = edev->xkb.modifiers;
862
863    ev->x = edev->seat->ptr.ix;
864    ev->y = edev->seat->ptr.iy;
865    ev->root.x = ev->x;
866    ev->root.y = ev->y;
867
868    ev->multi.device = edev->mt_slot;
869    ev->multi.radius = 1;
870    ev->multi.radius_x = 1;
871    ev->multi.radius_y = 1;
872    ev->multi.pressure = 1.0;
873    ev->multi.angle = 0.0;
874    ev->multi.x = ev->x;
875    ev->multi.y = ev->y;
876    ev->multi.root.x = ev->x;
877    ev->multi.root.y = ev->y;
878    ev->dev = ecore_device_ref(ecore_dev);
879
880    if (state)
881      {
882         unsigned int current;
883
884         current = timestamp;
885         edev->mouse.did_double = EINA_FALSE;
886         edev->mouse.did_triple = EINA_FALSE;
887
888         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
889             (button == edev->mouse.prev_button))
890           {
891              edev->mouse.did_double = EINA_TRUE;
892              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
893                  (button == edev->mouse.last_button))
894                {
895                   edev->mouse.did_triple = EINA_TRUE;
896                   edev->mouse.prev = 0;
897                   edev->mouse.last = 0;
898                   current = 0;
899                }
900           }
901
902         edev->mouse.last = edev->mouse.prev;
903         edev->mouse.prev = current;
904         edev->mouse.last_button = edev->mouse.prev_button;
905         edev->mouse.prev_button = button;
906      }
907
908    ev->buttons = button;
909
910    if (edev->mouse.did_double)
911      ev->double_click = 1;
912    if (edev->mouse.did_triple)
913      ev->triple_click = 1;
914
915    comp_conf = e_comp_config_get();
916    if (comp_conf && comp_conf->input_log_enable)
917      ELOGF("Mouse", "Button %s (btn: %d, device: %s)", NULL, state?"Press":"Release", button, ecore_device_name_get(ev->dev));
918
919    if (state)
920      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, _e_input_event_mouse_button_cb_free, NULL);
921    else
922      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, _e_input_event_mouse_button_cb_free, NULL);
923 }
924
925 static void
926 _device_handle_axis(struct libinput_device *device, struct libinput_event_pointer *event)
927 {
928    E_Input_Evdev *edev;
929    E_Input_Backend *input;
930    Ecore_Event_Mouse_Wheel *ev;
931    uint32_t timestamp;
932    enum libinput_pointer_axis axis;
933    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
934    Eina_List *l;
935    E_Comp_Config *comp_conf = NULL;
936    int direction = 0, z = 0;
937
938    if (!(edev = libinput_device_get_user_data(device)))
939      {
940         return;
941      }
942    if (!(input = edev->seat->input))
943      {
944         return;
945      }
946
947    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
948    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
949      {
950         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
951           {
952              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
953                {
954                   ecore_dev = data;
955                   break;
956                }
957              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
958                {
959                   detent_data = data;
960                }
961           }
962         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
963           {
964              ecore_dev = detent_data;
965           }
966      }
967    else
968      {
969         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
970         ecore_dev = edev->ecore_dev;
971      }
972
973    if (!ecore_dev)
974      {
975         ERR("Failed to get source ecore device from event !\n");
976         return;
977      }
978
979    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
980    if (libinput_event_pointer_has_axis(event, axis))
981      z = libinput_event_pointer_get_axis_value(event, axis);
982
983    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
984    if (libinput_event_pointer_has_axis(event, axis))
985      {
986         direction = 1;
987         z = libinput_event_pointer_get_axis_value(event, axis);
988      }
989
990    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
991        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
992      {
993         if (detent_data)
994           ELOGF("Mouse", "Detent (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
995                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
996         else
997           ELOGF("Mouse", "Wheel (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
998                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
999         return;
1000      }
1001
1002    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
1003      {
1004         return;
1005      }
1006
1007    timestamp = libinput_event_pointer_get_time(event);
1008
1009    ev->window = (Ecore_Window)input->dev->window;
1010    ev->event_window = (Ecore_Window)input->dev->window;
1011    ev->root_window = (Ecore_Window)input->dev->window;
1012    ev->timestamp = timestamp;
1013    ev->same_screen = 1;
1014
1015    _device_modifiers_update(edev);
1016    ev->modifiers = edev->xkb.modifiers;
1017
1018    ev->x = edev->seat->ptr.ix;
1019    ev->y = edev->seat->ptr.iy;
1020    ev->root.x = ev->x;
1021    ev->root.y = ev->y;
1022    ev->dev = ecore_device_ref(ecore_dev);
1023
1024    ev->z = z;
1025    ev->direction = direction;
1026
1027    comp_conf = e_comp_config_get();
1028    if (comp_conf && comp_conf->input_log_enable)
1029      {
1030         if (detent_data)
1031           ELOGF("Mouse", "Detent (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1032         else
1033           ELOGF("Mouse", "Wheel (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1034      }
1035
1036    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
1037 }
1038
1039 static void
1040 _device_handle_touch_event_send(E_Input_Evdev *edev, struct libinput_event_touch *event, int state)
1041 {
1042    E_Input_Backend *input;
1043    Ecore_Event_Mouse_Button *ev;
1044    uint32_t timestamp, button = 0;
1045    Ecore_Device *ecore_dev = NULL, *data;
1046    Eina_List *l;
1047
1048    if (!edev) return;
1049    if (!(input = edev->seat->input)) return;
1050
1051    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1052    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1053      {
1054         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1055           {
1056              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1057                {
1058                   ecore_dev = data;
1059                   break;
1060                }
1061           }
1062      }
1063    else
1064      {
1065         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1066         ecore_dev = edev->ecore_dev;
1067      }
1068
1069    if (!ecore_dev)
1070      {
1071         ERR("Failed to get source ecore device from event !\n");
1072         return;
1073      }
1074
1075    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) return;
1076
1077    timestamp = libinput_event_touch_get_time(event);
1078
1079    ev->window = (Ecore_Window)input->dev->window;
1080    ev->event_window = (Ecore_Window)input->dev->window;
1081    ev->root_window = (Ecore_Window)input->dev->window;
1082    ev->timestamp = timestamp;
1083    ev->same_screen = 1;
1084
1085    _device_modifiers_update(edev);
1086    ev->modifiers = edev->xkb.modifiers;
1087
1088    ev->x = edev->seat->ptr.ix;
1089    ev->y = edev->seat->ptr.iy;
1090    ev->root.x = ev->x;
1091    ev->root.y = ev->y;
1092
1093    ev->multi.device = edev->mt_slot;
1094    ev->multi.radius = 1;
1095    ev->multi.radius_x = 1;
1096    ev->multi.radius_y = 1;
1097    ev->multi.pressure = 1.0;
1098    ev->multi.angle = 0.0;
1099 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1100    if (libinput_event_get_type(libinput_event_touch_get_base_event(event))
1101        == LIBINPUT_EVENT_TOUCH_DOWN)
1102      {
1103         if (libinput_event_touch_has_minor(event))
1104           ev->multi.radius_x = libinput_event_touch_get_minor(event);
1105         if (libinput_event_touch_has_major(event))
1106           ev->multi.radius_y = libinput_event_touch_get_major(event);
1107         if (libinput_event_touch_has_pressure(event))
1108           ev->multi.pressure = libinput_event_touch_get_pressure(event);
1109         if (libinput_event_touch_has_orientation(event))
1110           ev->multi.angle = libinput_event_touch_get_orientation(event);
1111      }
1112 #endif
1113    ev->multi.x = ev->x;
1114    ev->multi.y = ev->y;
1115    ev->multi.root.x = ev->x;
1116    ev->multi.root.y = ev->y;
1117    ev->dev = ecore_device_ref(ecore_dev);
1118
1119    if (state == ECORE_EVENT_MOUSE_BUTTON_DOWN)
1120      {
1121         unsigned int current;
1122
1123         current = timestamp;
1124         edev->mouse.did_double = EINA_FALSE;
1125         edev->mouse.did_triple = EINA_FALSE;
1126
1127         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
1128             (button == edev->mouse.prev_button))
1129           {
1130              edev->mouse.did_double = EINA_TRUE;
1131              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
1132                  (button == edev->mouse.last_button))
1133                {
1134                   edev->mouse.did_triple = EINA_TRUE;
1135                   edev->mouse.prev = 0;
1136                   edev->mouse.last = 0;
1137                   current = 0;
1138                }
1139           }
1140
1141         edev->mouse.last = edev->mouse.prev;
1142         edev->mouse.prev = current;
1143         edev->mouse.last_button = edev->mouse.prev_button;
1144         edev->mouse.prev_button = button;
1145         edev->touch.pressed |= (1 << ev->multi.device);
1146      }
1147    else
1148      {
1149         edev->touch.pressed &= ~(1 << ev->multi.device);
1150      }
1151
1152    ev->buttons = ((button & 0x00F) + 1);
1153
1154    if (edev->mouse.did_double)
1155      ev->double_click = 1;
1156    if (edev->mouse.did_triple)
1157      ev->triple_click = 1;
1158
1159    ecore_event_add(state, ev, _e_input_event_mouse_button_cb_free, NULL);
1160 }
1161
1162 static void
1163 _device_handle_touch_motion_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1164 {
1165    E_Input_Backend *input;
1166    Ecore_Event_Mouse_Move *ev;
1167    Ecore_Device *ecore_dev = NULL, *data;
1168    Eina_List *l;
1169
1170    if (!edev) return;
1171    if (!(input = edev->seat->input)) return;
1172
1173    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1174    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1175      {
1176         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1177           {
1178              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1179                {
1180                   ecore_dev = data;
1181                   break;
1182                }
1183           }
1184      }
1185    else
1186      {
1187         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1188         ecore_dev = edev->ecore_dev;
1189      }
1190
1191    if (!ecore_dev)
1192      {
1193         ERR("Failed to get source ecore device from event !\n");
1194         return;
1195      }
1196
1197    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
1198
1199    ev->window = (Ecore_Window)input->dev->window;
1200    ev->event_window = (Ecore_Window)input->dev->window;
1201    ev->root_window = (Ecore_Window)input->dev->window;
1202    ev->timestamp = libinput_event_touch_get_time(event);
1203    ev->same_screen = 1;
1204
1205    _device_modifiers_update(edev);
1206    ev->modifiers = edev->xkb.modifiers;
1207
1208    ev->x = edev->seat->ptr.ix;
1209    ev->y = edev->seat->ptr.iy;
1210    ev->root.x = ev->x;
1211    ev->root.y = ev->y;
1212
1213    ev->multi.device = edev->mt_slot;
1214    ev->multi.radius = 1;
1215    ev->multi.radius_x = 1;
1216    ev->multi.radius_y = 1;
1217    ev->multi.pressure = 1.0;
1218    ev->multi.angle = 0.0;
1219 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1220    if (libinput_event_touch_has_minor(event))
1221      ev->multi.radius_x = libinput_event_touch_get_minor(event);
1222    if (libinput_event_touch_has_major(event))
1223      ev->multi.radius_y = libinput_event_touch_get_major(event);
1224    if (libinput_event_touch_has_pressure(event))
1225      ev->multi.pressure = libinput_event_touch_get_pressure(event);
1226    if (libinput_event_touch_has_orientation(event))
1227      ev->multi.angle = libinput_event_touch_get_orientation(event);
1228 #endif
1229    ev->multi.x = ev->x;
1230    ev->multi.y = ev->y;
1231    ev->multi.root.x = ev->x;
1232    ev->multi.root.y = ev->y;
1233    ev->dev = ecore_device_ref(ecore_dev);
1234
1235    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
1236 }
1237
1238 static void
1239 _device_handle_touch_down(struct libinput_device *device, struct libinput_event_touch *event)
1240 {
1241    E_Input_Evdev *edev;
1242    int w = 0, h = 0;
1243    E_Comp_Config *comp_conf = NULL;
1244
1245    if (!(edev = libinput_device_get_user_data(device)))
1246      {
1247         return;
1248      }
1249
1250    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1251
1252    edev->mouse.dx = edev->seat->ptr.ix = edev->seat->ptr.dx =
1253      libinput_event_touch_get_x_transformed(event, w);
1254    edev->mouse.dy = edev->seat->ptr.iy = edev->seat->ptr.dy =
1255      libinput_event_touch_get_y_transformed(event, h);
1256
1257    edev->mt_slot = libinput_event_touch_get_slot(event);
1258    if (edev->mt_slot < 0)
1259      {
1260         /* FIXME: The single touch device return slot id -1
1261          *        But currently we have no API to distinguish multi touch or single touch
1262          *        After libinput 1.11 version, libinput provides get_touch_count API,
1263          *        so we can distinguish multi touch device or single touch device.
1264          */
1265         if (edev->mt_slot == -1)
1266           edev->mt_slot = 0;
1267         else
1268           {
1269              WRN("%d slot touch down events are not supported\n", edev->mt_slot);
1270              return;
1271           }
1272      }
1273
1274    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1275      {
1276         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1277         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1278      }
1279
1280    comp_conf = e_comp_config_get();
1281    if (comp_conf && comp_conf->input_log_enable)
1282      ELOGF("Touch", "Down (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
1283
1284    edev->touch.raw_pressed |= (1 << edev->mt_slot);
1285
1286    if (edev->touch.blocked)
1287      {
1288         ELOGF("Touch", "Down (id: %d, x: %d, y: %d) is blocked by %p, server: 0x%x", NULL,
1289               edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, edev->seat->dev->blocked_client,
1290               edev->seat->dev->server_blocked);
1291         return;
1292      }
1293
1294    _device_handle_touch_motion_send(edev, event);
1295    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_DOWN);
1296 }
1297
1298 static void
1299 _device_handle_touch_motion(struct libinput_device *device, struct libinput_event_touch *event)
1300 {
1301    E_Input_Evdev *edev;
1302    int w = 0, h = 0;
1303
1304    if (!(edev = libinput_device_get_user_data(device)))
1305      {
1306         return;
1307      }
1308
1309    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
1310
1311    edev->mouse.dx = edev->seat->ptr.dx =
1312      libinput_event_touch_get_x_transformed(event, w);
1313    edev->mouse.dy = edev->seat->ptr.dy =
1314      libinput_event_touch_get_y_transformed(event, h);
1315
1316    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1317        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1318      {
1319         return;
1320      }
1321
1322    edev->seat->ptr.ix = edev->seat->ptr.dx;
1323    edev->seat->ptr.iy = edev->seat->ptr.dy;
1324
1325    edev->mt_slot = libinput_event_touch_get_slot(event);
1326    if (edev->mt_slot < 0)
1327      {
1328         /* FIXME: The single touch device return slot id -1
1329          *        But currently we have no API to distinguish multi touch or single touch
1330          *        After libinput 1.11 version, libinput provides get_touch_count API,
1331          *        so we can distinguish multi touch device or single touch device.
1332          */
1333         if (edev->mt_slot == -1)
1334           edev->mt_slot = 0;
1335         else
1336           {
1337              WRN("%d slot touch motion events are not supported\n", edev->mt_slot);
1338              return;
1339           }
1340      }
1341
1342    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1343      {
1344         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1345         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1346      }
1347
1348    if (!(edev->touch.pressed & (1 << edev->mt_slot)))
1349      {
1350         if (edev->touch.blocked)
1351           {
1352              return;
1353           }
1354      }
1355
1356    _device_handle_touch_motion_send(edev, event);
1357 }
1358
1359 static void
1360 _device_handle_touch_up(struct libinput_device *device, struct libinput_event_touch *event)
1361 {
1362    E_Input_Evdev *edev;
1363    E_Comp_Config *comp_conf = NULL;
1364
1365    if (!(edev = libinput_device_get_user_data(device)))
1366      {
1367         return;
1368      }
1369
1370    edev->mt_slot = libinput_event_touch_get_slot(event);
1371    if (edev->mt_slot < 0)
1372      {
1373         /* FIXME: The single touch device return slot id -1
1374          *        But currently we have no API to distinguish multi touch or single touch
1375          *        After libinput 1.11 version, libinput provides get_touch_count API,
1376          *        so we can distinguish multi touch device or single touch device.
1377          */
1378         if (edev->mt_slot == -1)
1379           edev->mt_slot = 0;
1380         else
1381           {
1382              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
1383              return;
1384           }
1385      }
1386
1387    if (edev->mt_slot < E_INPUT_MAX_SLOTS)
1388      {
1389         edev->mouse.dx = edev->seat->ptr.dx = edev->seat->ptr.ix =
1390           edev->touch.coords[edev->mt_slot].x;
1391         edev->mouse.dy = edev->seat->ptr.dy = edev->seat->ptr.iy =
1392           edev->touch.coords[edev->mt_slot].y;
1393      }
1394
1395    comp_conf = e_comp_config_get();
1396    if (comp_conf && comp_conf->input_log_enable)
1397      ELOGF("Touch", "Up (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
1398
1399    edev->touch.raw_pressed &= ~(1 << edev->mt_slot);
1400
1401    if (edev->touch.blocked)
1402      {
1403         if (!(edev->touch.pressed & (1 << edev->mt_slot)))
1404           {
1405              ELOGF("Touch", "Up (id: %d, x: %d, y: %d) is blocked by %p, server(0x%x)", NULL,
1406                    edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, edev->seat->dev->blocked_client,
1407                    edev->seat->dev->server_blocked);
1408              if (edev->touch.raw_pressed == 0x0)
1409                {
1410                   edev->touch.blocked = EINA_FALSE;
1411                }
1412              return;
1413           }
1414      }
1415
1416    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_UP);
1417 }
1418
1419 static void
1420 _device_handle_touch_frame(struct libinput_device *device EINA_UNUSED, struct libinput_event_touch *event EINA_UNUSED)
1421 {
1422    /* DBG("Unhandled Touch Frame Event"); */
1423 }
1424
1425 static void
1426 _e_input_aux_data_event_free(void *user_data EINA_UNUSED, void *ev)
1427 {
1428    Ecore_Event_Axis_Update *e = (Ecore_Event_Axis_Update *)ev;
1429
1430    if (e->axis) free(e->axis);
1431    if (e->dev) ecore_device_unref(e->dev);
1432
1433    free(e);
1434 }
1435
1436 static void
1437 _device_handle_touch_aux_data(struct libinput_device *device, struct libinput_event_touch_aux_data *event)
1438 {
1439    E_Input_Evdev *edev;
1440    E_Input_Backend *input;
1441    Ecore_Event_Axis_Update *ev;
1442    Ecore_Axis *axis;
1443    Ecore_Device *ecore_dev = NULL, *data;
1444    Eina_List *l;
1445    E_Comp_Config *comp_conf;
1446
1447    if (libinput_event_touch_aux_data_get_type(event) != LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM &&
1448        libinput_event_touch_aux_data_get_value(event) > 0)
1449       goto end;
1450
1451    if (!(edev = libinput_device_get_user_data(device))) goto end;
1452    if (!(input = edev->seat->input)) goto end;
1453
1454    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1455    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1456      {
1457         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1458           {
1459              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1460                {
1461                   ecore_dev = data;
1462                   break;
1463                }
1464           }
1465      }
1466    else
1467      {
1468         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1469         ecore_dev = edev->ecore_dev;
1470      }
1471
1472    if (!ecore_dev)
1473      {
1474         ERR("Failed to get source ecore device from event !\n");
1475         goto end;
1476      }
1477
1478    if (!(ev = calloc(1, sizeof(Ecore_Event_Axis_Update))))goto end;
1479
1480    ev->window = (Ecore_Window)input->dev->window;
1481    ev->event_window = (Ecore_Window)input->dev->window;
1482    ev->root_window = (Ecore_Window)input->dev->window;
1483    ev->timestamp = libinput_event_touch_aux_data_get_time(event);
1484
1485    axis = (Ecore_Axis *)calloc(1, sizeof(Ecore_Axis));
1486    if (axis)
1487      {
1488         axis->label = ECORE_AXIS_LABEL_TOUCH_PALM;
1489         axis->value = libinput_event_touch_aux_data_get_value(event);
1490         ev->naxis = 1;
1491      }
1492    ev->axis = axis;
1493    ev->dev = ecore_device_ref(ecore_dev);
1494
1495    comp_conf = e_comp_config_get();
1496    if (comp_conf && comp_conf->input_log_enable)
1497      ELOGF("Touch", "Axis (label: %d, value: %lf)", NULL, axis?axis->label:-1, axis?axis->value:0.0);
1498
1499    ecore_event_add(ECORE_EVENT_AXIS_UPDATE, ev, _e_input_aux_data_event_free, NULL);
1500
1501 end:
1502    ;
1503 }
1504
1505 E_Input_Evdev *
1506 _e_input_evdev_device_create(E_Input_Seat *seat, struct libinput_device *device)
1507 {
1508    E_Input_Evdev *edev;
1509    E_Input_Backend *b_input;
1510
1511    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
1512
1513    /* try to allocate space for new evdev */
1514    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
1515
1516    edev->seat = seat;
1517    edev->device = device;
1518    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
1519
1520    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
1521      {
1522         edev->caps |= E_INPUT_SEAT_KEYBOARD;
1523         _device_keyboard_setup(edev);
1524      }
1525
1526    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
1527      {
1528         edev->caps |= E_INPUT_SEAT_POINTER;
1529
1530         /* TODO: make this configurable */
1531         edev->mouse.threshold = 250;
1532
1533         b_input = seat->input;
1534         if (b_input->left_handed == EINA_TRUE)
1535           {
1536              if (libinput_device_config_left_handed_set(device, 1) !=
1537                  LIBINPUT_CONFIG_STATUS_SUCCESS)
1538                {
1539                   WRN("Failed to set left hand mode about device: %s\n",
1540                       libinput_device_get_name(device));
1541                }
1542           }
1543      }
1544
1545    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
1546      {
1547         int palm_code;
1548         edev->caps |= E_INPUT_SEAT_TOUCH;
1549         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
1550         if (libinput_device_touch_has_aux_data(device, palm_code))
1551           {
1552              libinput_device_touch_set_aux_data(device, palm_code);
1553           }
1554      }
1555
1556    libinput_device_set_user_data(device, edev);
1557    libinput_device_ref(device);
1558
1559    /* configure device */
1560    _device_configure(edev);
1561
1562    return edev;
1563 }
1564
1565 void
1566 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
1567 {
1568    Ecore_Device *dev;
1569
1570    EINA_SAFETY_ON_NULL_RETURN(edev);
1571
1572    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
1573      {
1574         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
1575         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
1576      }
1577
1578    if (edev->ecore_dev) ecore_device_del(edev->ecore_dev);
1579    if (edev->ecore_dev_list)
1580      EINA_LIST_FREE(edev->ecore_dev_list, dev)
1581        {
1582           ecore_device_del(dev);
1583        }
1584    if (edev->path) eina_stringshare_del(edev->path);
1585    if (edev->device) libinput_device_unref(edev->device);
1586    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
1587
1588    free(edev);
1589 }
1590
1591 Eina_Bool
1592 _e_input_evdev_event_process(struct libinput_event *event)
1593 {
1594    struct libinput_device *device;
1595    Eina_Bool ret = EINA_TRUE;
1596
1597    device = libinput_event_get_device(event);
1598    switch (libinput_event_get_type(event))
1599      {
1600       case LIBINPUT_EVENT_KEYBOARD_KEY:
1601         _device_handle_key(device, libinput_event_get_keyboard_event(event));
1602         break;
1603       case LIBINPUT_EVENT_POINTER_MOTION:
1604         _device_handle_pointer_motion(device,
1605                                       libinput_event_get_pointer_event(event));
1606         break;
1607       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
1608         _device_handle_pointer_motion_absolute(device,
1609                                                libinput_event_get_pointer_event(event));
1610         break;
1611       case LIBINPUT_EVENT_POINTER_BUTTON:
1612         _device_handle_button(device, libinput_event_get_pointer_event(event));
1613         break;
1614       case LIBINPUT_EVENT_POINTER_AXIS:
1615         _device_handle_axis(device, libinput_event_get_pointer_event(event));
1616         break;
1617       case LIBINPUT_EVENT_TOUCH_DOWN:
1618         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
1619         break;
1620       case LIBINPUT_EVENT_TOUCH_MOTION:
1621         _device_handle_touch_motion(device,
1622                                     libinput_event_get_touch_event(event));
1623         break;
1624       case LIBINPUT_EVENT_TOUCH_UP:
1625         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
1626         break;
1627       case LIBINPUT_EVENT_TOUCH_FRAME:
1628         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
1629         break;
1630       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
1631         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
1632         break;
1633       default:
1634         ret = EINA_FALSE;
1635         break;
1636      }
1637
1638    return ret;
1639 }
1640
1641 /**
1642  * @brief Set the axis size of the given device.
1643  *
1644  * @param dev The device to set the axis size to.
1645  * @param w The width of the axis.
1646  * @param h The height of the axis.
1647  *
1648  * This function sets set the width @p w and height @p h of the axis
1649  * of device @p dev. If @p dev is a relative input device, a width and
1650  * height must set for it. If its absolute set the ioctl correctly, if
1651  * not, unsupported device.
1652  */
1653 EINTERN void
1654 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
1655 {
1656    const char *sysname;
1657    float cal[6];
1658    const char *device;
1659    Eina_List *devices;
1660    const char *vals;
1661    enum libinput_config_status status;
1662
1663    EINA_SAFETY_ON_NULL_RETURN(edev);
1664    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
1665
1666    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
1667        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
1668      return;
1669
1670    sysname = libinput_device_get_sysname(edev->device);
1671
1672    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
1673    if (eina_list_count(devices) < 1) return;
1674
1675    EINA_LIST_FREE(devices, device)
1676      {
1677         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
1678         if ((!vals) ||
1679             (sscanf(vals, "%f %f %f %f %f %f",
1680                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
1681           goto cont;
1682
1683         cal[2] /= w;
1684         cal[5] /= h;
1685
1686         status =
1687           libinput_device_config_calibration_set_matrix(edev->device, cal);
1688
1689         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
1690           ERR("Failed to apply calibration");
1691
1692 cont:
1693         eina_stringshare_del(device);
1694         continue;
1695      }
1696 }
1697
1698 E_API const char *
1699 e_input_evdev_name_get(E_Input_Evdev *evdev)
1700 {
1701    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1702    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1703
1704    return libinput_device_get_name(evdev->device);
1705 }
1706
1707 EINTERN const char *
1708 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
1709 {
1710    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1711    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1712
1713    return libinput_device_get_sysname(evdev->device);
1714 }
1715
1716 EINTERN Eina_Bool
1717 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
1718 {
1719    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1720    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1721
1722    edev->key_remap_enabled = enable;
1723
1724    if (enable == EINA_FALSE && edev->key_remap_hash)
1725      {
1726         eina_hash_free(edev->key_remap_hash);
1727         edev->key_remap_hash = NULL;
1728      }
1729
1730    return EINA_TRUE;
1731 }
1732
1733 EINTERN Eina_Bool
1734 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
1735 {
1736    int i;
1737
1738    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1739    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1740    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
1741    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
1742    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
1743    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
1744
1745    if (edev->key_remap_hash == NULL)
1746      edev->key_remap_hash = eina_hash_int32_new(NULL);
1747
1748    if (edev->key_remap_hash == NULL)
1749      {
1750         ERR("Failed to set remap key information : creating a hash is failed.");
1751         return EINA_FALSE;
1752      }
1753
1754    for (i = 0; i < num ; i++)
1755      {
1756         if (!from_keys[i] || !to_keys[i])
1757           {
1758              ERR("Failed to set remap key information : given arguments are invalid.");
1759              return EINA_FALSE;
1760           }
1761      }
1762
1763    for (i = 0; i < num ; i++)
1764      {
1765         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
1766      }
1767
1768    return EINA_TRUE;
1769 }
1770
1771 E_API int
1772 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
1773 {
1774    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
1775    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
1776 }
1777
1778 EINTERN Eina_Bool
1779 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
1780 {
1781    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1782    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1783
1784    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
1785        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
1786      return EINA_FALSE;
1787
1788    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
1789        LIBINPUT_CONFIG_STATUS_SUCCESS)
1790      {
1791         WRN("Failed to set input transformation about device: %s\n",
1792             libinput_device_get_name(edev->device));
1793         return EINA_FALSE;
1794      }
1795
1796    return EINA_TRUE;
1797 }
1798
1799 EINTERN Eina_Bool
1800 e_input_evdev_mouse_accel_speed_set(E_Input_Evdev *edev, double speed)
1801 {
1802    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1803    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1804
1805    if (!libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
1806      return EINA_FALSE;
1807
1808    if (!libinput_device_config_accel_is_available(edev->device))
1809      return EINA_FALSE;
1810
1811    if (libinput_device_config_accel_set_speed(edev->device, speed) !=
1812        LIBINPUT_CONFIG_STATUS_SUCCESS)
1813      {
1814         WRN("Failed to set mouse accel about device: %s\n",
1815             libinput_device_get_name(edev->device));
1816         return EINA_FALSE;
1817      }
1818
1819    return EINA_TRUE;
1820 }
1821
1822 EINTERN unsigned int
1823 e_input_evdev_touch_pressed_get(E_Input_Evdev *edev)
1824 {
1825    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, 0x0);
1826
1827    return edev->touch.pressed;
1828 }