e_input: fix not to do unnecessary udev-lookups and loops
[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 static void
7 _device_calibration_set(E_Input_Evdev *edev)
8 {
9    const char *sysname;
10    float cal[6];
11    const char *device;
12    Eina_List *devices;
13    const char *vals;
14    enum libinput_config_status status;
15    E_Output *output;
16    int w = 0, h = 0;
17    int temp;
18
19    output = e_comp_screen_primary_output_get(e_comp->e_comp_screen);
20    e_output_size_get(output, &w, &h);
21
22    if (output)
23      {
24         edev->mouse.minx = edev->mouse.miny = 0;
25         edev->mouse.maxw = w;
26         edev->mouse.maxh = h;
27
28         if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
29           {
30              edev->seat->ptr.dx = (double)(w / 2);
31              edev->seat->ptr.dy = (double)(h / 2);
32              edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
33              edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
34              edev->mouse.dx = edev->seat->ptr.dx;
35              edev->mouse.dy = edev->seat->ptr.dy;
36
37              if (output->config.rotation == 90 || output->config.rotation == 270)
38                {
39                   temp = edev->mouse.minx;
40                   edev->mouse.minx = edev->mouse.miny;
41                   edev->mouse.miny = temp;
42
43                   temp = edev->mouse.maxw;
44                   edev->mouse.maxw = edev->mouse.maxh;
45                   edev->mouse.maxh = temp;
46                }
47           }
48      }
49
50 //LCOV_EXCL_START
51 #ifdef _F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
52    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
53        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
54      return;
55
56    sysname = libinput_device_get_sysname(edev->device);
57
58    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
59    if (eina_list_count(devices) < 1) return;
60
61 #ifdef _F_E_INPUT_USE_WL_CALIBRATION_
62    EINA_LIST_FREE(devices, device)
63      {
64         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
65         if ((!vals) ||
66             (sscanf(vals, "%f %f %f %f %f %f",
67                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
68           goto cont;
69
70         cal[2] /= w;
71         cal[5] /= h;
72
73         status =
74           libinput_device_config_calibration_set_matrix(edev->device, cal);
75
76         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
77           ERR("Failed to apply calibration");
78
79 cont:
80         eina_stringshare_del(device);
81         continue;
82      }
83 #endif//_F_E_INPUT_USE_WL_CALIBRATION_
84 #endif//_F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
85 //LCOV_EXCL_STOP
86 }
87
88 static void
89 _device_configure(E_Input_Evdev *edev)
90 {
91    if (libinput_device_config_tap_get_finger_count(edev->device) > 0)
92      {
93         Eina_Bool tap = EINA_FALSE;
94
95         tap = libinput_device_config_tap_get_default_enabled(edev->device);
96         libinput_device_config_tap_set_enabled(edev->device, tap);
97      }
98
99    _device_calibration_set(edev);
100 }
101
102 static void
103 _device_keyboard_setup(E_Input_Evdev *edev)
104 {
105    E_Input_Backend *input;
106
107    if ((!edev) || (!edev->seat)) return;
108    if (!(input = edev->seat->input)) return;
109    if (!input->dev->xkb_ctx) return;
110
111    /* create keymap from xkb context */
112    edev->xkb.keymap = _e_input_device_cached_keymap_get(input->dev->xkb_ctx, NULL, 0);
113    if (!edev->xkb.keymap)
114      {
115         ERR("Failed to create keymap: %m");
116         return;
117      }
118
119    /* create xkb state */
120    if (!(edev->xkb.state = xkb_state_new(edev->xkb.keymap)))
121      {
122         ERR("Failed to create xkb state: %m");
123         return;
124      }
125
126    edev->xkb.ctrl_mask =
127      1 << xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CTRL);
128    edev->xkb.alt_mask =
129      1 << xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_ALT);
130    edev->xkb.shift_mask =
131      1 << xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_SHIFT);
132    edev->xkb.win_mask =
133      1 << xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_LOGO);
134    edev->xkb.scroll_mask =
135      1 << xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_SCROLL);
136    edev->xkb.num_mask =
137      1 << xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_NUM);
138    edev->xkb.caps_mask =
139      1 << xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CAPS);
140    edev->xkb.altgr_mask =
141      1 << xkb_map_mod_get_index(edev->xkb.keymap, "ISO_Level3_Shift");
142 }
143
144 static int
145 _device_keysym_translate(xkb_keysym_t keysym, unsigned int modifiers, char *buffer, int bytes)
146 {
147    unsigned long hbytes = 0;
148    unsigned char c;
149
150    if (!keysym) return 0;
151    hbytes = (keysym >> 8);
152
153    if (!(bytes &&
154          ((hbytes == 0) ||
155           ((hbytes == 0xFF) &&
156            (((keysym >= XKB_KEY_BackSpace) && (keysym <= XKB_KEY_Clear)) ||
157             (keysym == XKB_KEY_Return) || (keysym == XKB_KEY_Escape) ||
158             (keysym == XKB_KEY_KP_Space) || (keysym == XKB_KEY_KP_Tab) ||
159             (keysym == XKB_KEY_KP_Enter) ||
160             ((keysym >= XKB_KEY_KP_Multiply) && (keysym <= XKB_KEY_KP_9)) ||
161             (keysym == XKB_KEY_KP_Equal) || (keysym == XKB_KEY_Delete))))))
162      return 0;
163
164    if (keysym == XKB_KEY_KP_Space)
165      c = (XKB_KEY_space & 0x7F);
166    else if (hbytes == 0xFF)
167      c = (keysym & 0x7F);
168    else
169      c = (keysym & 0xFF);
170
171    if (modifiers & ECORE_EVENT_MODIFIER_CTRL)
172      {
173         if (((c >= '@') && (c < '\177')) || c == ' ')
174           c &= 0x1F;
175         else if (c == '2')
176           c = '\000';
177         else if ((c >= '3') && (c <= '7'))
178           c -= ('3' - '\033');
179         else if (c == '8')
180           c = '\177';
181         else if (c == '/')
182           c = '_' & 0x1F;
183      }
184    buffer[0] = c;
185    return 1;
186 }
187
188 static void
189 _device_modifiers_update_device(E_Input_Evdev *edev, E_Input_Evdev *from)
190 {
191    xkb_mod_mask_t mask;
192
193    edev->xkb.depressed =
194      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_DEPRESSED);
195    edev->xkb.latched =
196      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LATCHED);
197    edev->xkb.locked =
198      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LOCKED);
199    edev->xkb.group =
200      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_EFFECTIVE);
201
202    mask = (edev->xkb.depressed | edev->xkb.latched);
203
204    if (mask & from->xkb.ctrl_mask)
205      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_CTRL;
206    if (mask & from->xkb.alt_mask)
207      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALT;
208    if (mask & from->xkb.shift_mask)
209      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
210    if (mask & from->xkb.win_mask)
211      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_WIN;
212    if (mask & from->xkb.scroll_mask)
213      edev->xkb.modifiers |= ECORE_EVENT_LOCK_SCROLL;
214    if (mask & from->xkb.num_mask)
215      edev->xkb.modifiers |= ECORE_EVENT_LOCK_NUM;
216    if (mask & from->xkb.caps_mask)
217      edev->xkb.modifiers |= ECORE_EVENT_LOCK_CAPS;
218    if (mask & from->xkb.altgr_mask)
219      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALTGR;
220 }
221
222 static void
223 _device_modifiers_update(E_Input_Evdev *edev)
224 {
225    edev->xkb.modifiers = 0;
226
227    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
228      _device_modifiers_update_device(edev, edev);
229    else
230      {
231         Eina_List *l;
232         E_Input_Evdev *ed;
233
234         EINA_LIST_FOREACH(edev->seat->devices, l, ed)
235           {
236              if (!(ed->caps & E_INPUT_SEAT_KEYBOARD)) continue;
237              _device_modifiers_update_device(edev, ed);
238           }
239      }
240
241 }
242
243 static int
244 _device_remapped_key_get(E_Input_Evdev *edev, int code)
245 {
246    void *ret = NULL;
247
248    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, code);
249    if (!edev->key_remap_enabled) return code;
250    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->key_remap_hash, code);
251
252    ret = eina_hash_find(edev->key_remap_hash, &code);
253
254    if (ret) code = (int)(intptr_t)ret;
255
256    return code;
257 }
258
259 E_API Ecore_Device *
260 e_input_evdev_get_ecore_device(const char *path, Ecore_Device_Class clas)
261 {
262    const Eina_List *dev_list = NULL;
263    const Eina_List *l;
264    Ecore_Device *dev = NULL;
265    const char *identifier;
266
267    if (!path) return NULL;
268
269    dev_list = ecore_device_list();
270    if (!dev_list) return NULL;
271    EINA_LIST_FOREACH(dev_list, l, dev)
272      {
273         if (!dev) continue;
274         identifier = ecore_device_identifier_get(dev);
275         if (!identifier) continue;
276         if ((ecore_device_class_get(dev) == clas) && !(strcmp(identifier, path)))
277           return dev;
278      }
279    return NULL;
280 }
281
282 static void
283 _device_handle_key(struct libinput_device *device, struct libinput_event_keyboard *event)
284 {
285    E_Input_Evdev *edev;
286    E_Input_Backend *input;
287    uint32_t timestamp;
288    uint32_t code, nsyms;
289    const xkb_keysym_t *syms;
290    enum libinput_key_state state;
291    int key_count;
292    xkb_keysym_t sym = XKB_KEY_NoSymbol;
293    char key[256], keyname[256], compose_buffer[256];
294    Ecore_Event_Key *e;
295    char *tmp = NULL, *compose = NULL;
296
297    if (!(edev = libinput_device_get_user_data(device)))
298      {
299         return;
300      }
301
302    if (!(input = edev->seat->input))
303      {
304         return;
305      }
306
307    timestamp = libinput_event_keyboard_get_time(event);
308    code = libinput_event_keyboard_get_key(event);
309    code = _device_remapped_key_get(edev, code) + 8;
310    state = libinput_event_keyboard_get_key_state(event);
311    key_count = libinput_event_keyboard_get_seat_key_count(event);
312
313    /* ignore key events that are not seat wide state changes */
314    if (((state == LIBINPUT_KEY_STATE_PRESSED) && (key_count != 1)) ||
315        ((state == LIBINPUT_KEY_STATE_RELEASED) && (key_count != 0)))
316      {
317         return;
318      }
319
320    xkb_state_update_key(edev->xkb.state, code,
321                         (state ? XKB_KEY_DOWN : XKB_KEY_UP));
322
323    /* get the keysym for this code */
324    nsyms = xkb_key_get_syms(edev->xkb.state, code, &syms);
325    if (nsyms == 1) sym = syms[0];
326
327    /* get the keyname for this sym */
328    memset(key, 0, sizeof(key));
329    xkb_keysym_get_name(sym, key, sizeof(key));
330
331    memset(keyname, 0, sizeof(keyname));
332    memcpy(keyname, key, sizeof(keyname));
333
334    if (keyname[0] == '\0')
335      snprintf(keyname, sizeof(keyname), "Keycode-%u", code);
336
337    /* if shift is active, we need to transform the key to lower */
338    if (xkb_state_mod_index_is_active(edev->xkb.state,
339                                      xkb_map_mod_get_index(edev->xkb.keymap,
340                                      XKB_MOD_NAME_SHIFT),
341                                      XKB_STATE_MODS_EFFECTIVE))
342      {
343         if (keyname[0] != '\0')
344           keyname[0] = tolower(keyname[0]);
345      }
346
347    memset(compose_buffer, 0, sizeof(compose_buffer));
348    if (_device_keysym_translate(sym, edev->xkb.modifiers,
349                                 compose_buffer, sizeof(compose_buffer)))
350      {
351         compose = eina_str_convert("ISO8859-1", "UTF-8", compose_buffer);
352         if (!compose)
353           {
354              ERR("E Input cannot convert input key string '%s' to UTF-8. "
355                  "Is Eina built with iconv support?", compose_buffer);
356           }
357         else
358           tmp = compose;
359      }
360
361    if (!compose) compose = compose_buffer;
362
363    e = calloc(1, sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
364               ((compose[0] != '\0') ? strlen(compose) : 0) + 3);
365    if (!e)
366      {
367         return;
368      }
369
370    e->keyname = (char *)(e + 1);
371    e->key = e->keyname + strlen(keyname) + 1;
372    e->compose = strlen(compose) ? e->key + strlen(key) + 1 : NULL;
373    e->string = e->compose;
374
375    strncpy((char *)e->keyname, keyname, strlen(keyname));
376    strncpy((char *)e->key, key, strlen(key));
377    if (strlen(compose)) strncpy((char *)e->compose, compose, strlen(compose));
378
379    e->window = (Ecore_Window)input->dev->window;
380    e->event_window = (Ecore_Window)input->dev->window;
381    e->root_window = (Ecore_Window)input->dev->window;
382    e->timestamp = timestamp;
383    e->same_screen = 1;
384    e->keycode = code;
385    e->data = NULL;
386
387    _device_modifiers_update(edev);
388
389    e->modifiers = edev->xkb.modifiers;
390    e->dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_KEYBOARD);
391
392    if (state)
393      ecore_event_add(ECORE_EVENT_KEY_DOWN, e, NULL, NULL);
394    else
395      ecore_event_add(ECORE_EVENT_KEY_UP, e, NULL, NULL);
396
397    if (tmp) free(tmp);
398 }
399
400 static void
401 _device_pointer_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event)
402 {
403    E_Input_Backend *input;
404    Ecore_Event_Mouse_Move *ev;
405
406    if (!(input = edev->seat->input)) return;
407
408    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
409
410    if (edev->seat->ptr.ix < edev->mouse.minx)
411      edev->seat->ptr.dx = edev->seat->ptr.ix = edev->mouse.minx;
412    else if (edev->seat->ptr.ix >= (edev->mouse.minx + edev->mouse.maxw))
413      edev->seat->ptr.dx = edev->seat->ptr.ix = (edev->mouse.minx + edev->mouse.maxw - 1);
414
415    if (edev->seat->ptr.iy < edev->mouse.miny)
416      edev->seat->ptr.dy = edev->seat->ptr.iy = edev->mouse.miny;
417    else if (edev->seat->ptr.iy >= (edev->mouse.miny + edev->mouse.maxh))
418      edev->seat->ptr.dy = edev->seat->ptr.iy = (edev->mouse.miny + edev->mouse.maxh - 1);
419
420    edev->mouse.dx = edev->seat->ptr.dx;
421    edev->mouse.dy = edev->seat->ptr.dy;
422
423    ev->window = (Ecore_Window)input->dev->window;
424    ev->event_window = (Ecore_Window)input->dev->window;
425    ev->root_window = (Ecore_Window)input->dev->window;
426    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
427    ev->same_screen = 1;
428
429    _device_modifiers_update(edev);
430    ev->modifiers = edev->xkb.modifiers;
431
432    ev->x = edev->seat->ptr.ix;
433    ev->y = edev->seat->ptr.iy;
434    ev->root.x = ev->x;
435    ev->root.y = ev->y;
436
437    ev->multi.device = edev->mt_slot;
438    ev->multi.radius = 1;
439    ev->multi.radius_x = 1;
440    ev->multi.radius_y = 1;
441    ev->multi.pressure = 1.0;
442    ev->multi.angle = 0.0;
443    ev->multi.x = ev->x;
444    ev->multi.y = ev->y;
445    ev->multi.root.x = ev->x;
446    ev->multi.root.y = ev->y;
447    ev->dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
448
449    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
450 }
451
452 void
453 _e_input_pointer_motion_post(E_Input_Evdev *edev)
454 {
455    _device_pointer_motion(edev, NULL);
456 }
457
458 static void
459 _device_handle_pointer_motion(struct libinput_device *device, struct libinput_event_pointer *event)
460 {
461    E_Input_Evdev *edev;
462    double dx, dy, temp;
463
464    if (!(edev = libinput_device_get_user_data(device)))
465      {
466         return;
467      }
468
469    dx = libinput_event_pointer_get_dx(event);
470    dy = libinput_event_pointer_get_dy(event);
471
472    if (edev->seat->ptr.swap)
473      {
474          temp = dx;
475          dx = dy;
476          dy = temp;
477      }
478    if (edev->seat->ptr.invert_x)
479      dx *= -1;
480    if (edev->seat->ptr.invert_y)
481      dy *= -1;
482
483    edev->seat->ptr.dx += dx;
484    edev->seat->ptr.dy += dy;
485
486    edev->mouse.dx = edev->seat->ptr.dx;
487    edev->mouse.dy = edev->seat->ptr.dy;
488
489    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
490        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
491      {
492         return;
493      }
494
495    edev->seat->ptr.ix = edev->seat->ptr.dx;
496    edev->seat->ptr.iy = edev->seat->ptr.dy;
497
498   _device_pointer_motion(edev, event);
499 }
500
501 static void
502 _device_handle_pointer_motion_absolute(struct libinput_device *device, struct libinput_event_pointer *event)
503 {
504    E_Input_Evdev *edev;
505    int w = 0, h = 0;
506
507    if (!(edev = libinput_device_get_user_data(device)))
508      {
509         return;
510      }
511
512    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
513
514    edev->mouse.dx = edev->seat->ptr.dx =
515      libinput_event_pointer_get_absolute_x_transformed(event, w);
516    edev->mouse.dy = edev->seat->ptr.dy =
517      libinput_event_pointer_get_absolute_y_transformed(event, h);
518
519    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
520        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
521      {
522         return;
523      }
524
525    edev->seat->ptr.ix = edev->seat->ptr.dx;
526    edev->seat->ptr.iy = edev->seat->ptr.dy;
527    _device_pointer_motion(edev, event);
528 }
529
530 static void
531 _device_handle_button(struct libinput_device *device, struct libinput_event_pointer *event)
532 {
533    E_Input_Evdev *edev;
534    E_Input_Backend *input;
535    Ecore_Event_Mouse_Button *ev;
536    enum libinput_button_state state;
537    uint32_t button, timestamp;
538
539    if (!(edev = libinput_device_get_user_data(device)))
540      {
541         return;
542      }
543    if (!(input = edev->seat->input))
544      {
545         return;
546      }
547
548    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button))))
549      {
550         return;
551      }
552
553    state = libinput_event_pointer_get_button_state(event);
554    button = libinput_event_pointer_get_button(event);
555    timestamp = libinput_event_pointer_get_time(event);
556
557    button = ((button & 0x00F) + 1);
558    if (button == 3) button = 2;
559    else if (button == 2) button = 3;
560
561    ev->window = (Ecore_Window)input->dev->window;
562    ev->event_window = (Ecore_Window)input->dev->window;
563    ev->root_window = (Ecore_Window)input->dev->window;
564    ev->timestamp = timestamp;
565    ev->same_screen = 1;
566
567    _device_modifiers_update(edev);
568    ev->modifiers = edev->xkb.modifiers;
569
570    ev->x = edev->seat->ptr.ix;
571    ev->y = edev->seat->ptr.iy;
572    ev->root.x = ev->x;
573    ev->root.y = ev->y;
574
575    ev->multi.device = edev->mt_slot;
576    ev->multi.radius = 1;
577    ev->multi.radius_x = 1;
578    ev->multi.radius_y = 1;
579    ev->multi.pressure = 1.0;
580    ev->multi.angle = 0.0;
581    ev->multi.x = ev->x;
582    ev->multi.y = ev->y;
583    ev->multi.root.x = ev->x;
584    ev->multi.root.y = ev->y;
585    ev->dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
586
587    if (state)
588      {
589         unsigned int current;
590
591         current = timestamp;
592         edev->mouse.did_double = EINA_FALSE;
593         edev->mouse.did_triple = EINA_FALSE;
594
595         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
596             (button == edev->mouse.prev_button))
597           {
598              edev->mouse.did_double = EINA_TRUE;
599              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
600                  (button == edev->mouse.last_button))
601                {
602                   edev->mouse.did_triple = EINA_TRUE;
603                   edev->mouse.prev = 0;
604                   edev->mouse.last = 0;
605                   current = 0;
606                }
607           }
608
609         edev->mouse.last = edev->mouse.prev;
610         edev->mouse.prev = current;
611         edev->mouse.last_button = edev->mouse.prev_button;
612         edev->mouse.prev_button = button;
613      }
614
615    ev->buttons = button;
616
617    if (edev->mouse.did_double)
618      ev->double_click = 1;
619    if (edev->mouse.did_triple)
620      ev->triple_click = 1;
621
622    if (state)
623      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
624    else
625      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
626 }
627
628 static void
629 _device_handle_axis(struct libinput_device *device, struct libinput_event_pointer *event)
630 {
631    E_Input_Evdev *edev;
632    E_Input_Backend *input;
633    Ecore_Event_Mouse_Wheel *ev;
634    uint32_t timestamp;
635    enum libinput_pointer_axis axis;
636
637    if (!(edev = libinput_device_get_user_data(device)))
638      {
639         return;
640      }
641    if (!(input = edev->seat->input))
642      {
643         return;
644      }
645
646    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
647      {
648         return;
649      }
650
651    timestamp = libinput_event_pointer_get_time(event);
652
653    ev->window = (Ecore_Window)input->dev->window;
654    ev->event_window = (Ecore_Window)input->dev->window;
655    ev->root_window = (Ecore_Window)input->dev->window;
656    ev->timestamp = timestamp;
657    ev->same_screen = 1;
658
659    _device_modifiers_update(edev);
660    ev->modifiers = edev->xkb.modifiers;
661
662    ev->x = edev->seat->ptr.ix;
663    ev->y = edev->seat->ptr.iy;
664    ev->root.x = ev->x;
665    ev->root.y = ev->y;
666    ev->dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
667
668    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
669    if (libinput_event_pointer_has_axis(event, axis))
670      ev->z = libinput_event_pointer_get_axis_value(event, axis);
671
672    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
673    if (libinput_event_pointer_has_axis(event, axis))
674      {
675         ev->direction = 1;
676         ev->z = libinput_event_pointer_get_axis_value(event, axis);
677      }
678
679    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
680 }
681
682 static void
683 _device_handle_touch_event_send(E_Input_Evdev *edev, struct libinput_event_touch *event, int state)
684 {
685    E_Input_Backend *input;
686    Ecore_Event_Mouse_Button *ev;
687    uint32_t timestamp, button = 0;
688
689    if (!edev) return;
690    if (!(input = edev->seat->input)) return;
691
692    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) return;
693
694    timestamp = libinput_event_touch_get_time(event);
695
696    ev->window = (Ecore_Window)input->dev->window;
697    ev->event_window = (Ecore_Window)input->dev->window;
698    ev->root_window = (Ecore_Window)input->dev->window;
699    ev->timestamp = timestamp;
700    ev->same_screen = 1;
701
702    _device_modifiers_update(edev);
703    ev->modifiers = edev->xkb.modifiers;
704
705    ev->x = edev->seat->ptr.ix;
706    ev->y = edev->seat->ptr.iy;
707    ev->root.x = ev->x;
708    ev->root.y = ev->y;
709
710    ev->multi.device = edev->mt_slot;
711    ev->multi.radius = 1;
712    ev->multi.radius_x = 1;
713    ev->multi.radius_y = 1;
714    ev->multi.pressure = 1.0;
715    ev->multi.angle = 0.0;
716 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
717    if (libinput_event_get_type(libinput_event_touch_get_base_event(event))
718        == LIBINPUT_EVENT_TOUCH_DOWN)
719      {
720         if (libinput_event_touch_has_minor(event))
721           ev->multi.radius_x = libinput_event_touch_get_minor(event);
722         if (libinput_event_touch_has_major(event))
723           ev->multi.radius_y = libinput_event_touch_get_major(event);
724         if (libinput_event_touch_has_pressure(event))
725           ev->multi.pressure = libinput_event_touch_get_pressure(event);
726         if (libinput_event_touch_has_orientation(event))
727           ev->multi.angle = libinput_event_touch_get_orientation(event);
728      }
729 #endif
730    ev->multi.x = ev->x;
731    ev->multi.y = ev->y;
732    ev->multi.root.x = ev->x;
733    ev->multi.root.y = ev->y;
734    ev->dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
735
736    if (state == ECORE_EVENT_MOUSE_BUTTON_DOWN)
737      {
738         unsigned int current;
739
740         current = timestamp;
741         edev->mouse.did_double = EINA_FALSE;
742         edev->mouse.did_triple = EINA_FALSE;
743
744         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
745             (button == edev->mouse.prev_button))
746           {
747              edev->mouse.did_double = EINA_TRUE;
748              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
749                  (button == edev->mouse.last_button))
750                {
751                   edev->mouse.did_triple = EINA_TRUE;
752                   edev->mouse.prev = 0;
753                   edev->mouse.last = 0;
754                   current = 0;
755                }
756           }
757
758         edev->mouse.last = edev->mouse.prev;
759         edev->mouse.prev = current;
760         edev->mouse.last_button = edev->mouse.prev_button;
761         edev->mouse.prev_button = button;
762      }
763
764    ev->buttons = ((button & 0x00F) + 1);
765
766    if (edev->mouse.did_double)
767      ev->double_click = 1;
768    if (edev->mouse.did_triple)
769      ev->triple_click = 1;
770
771    ecore_event_add(state, ev, NULL, NULL);
772 }
773
774 static void
775 _device_handle_touch_motion_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
776 {
777    E_Input_Backend *input;
778    Ecore_Event_Mouse_Move *ev;
779
780    if (!edev) return;
781    if (!(input = edev->seat->input)) return;
782
783    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
784
785    ev->window = (Ecore_Window)input->dev->window;
786    ev->event_window = (Ecore_Window)input->dev->window;
787    ev->root_window = (Ecore_Window)input->dev->window;
788    ev->timestamp = libinput_event_touch_get_time(event);
789    ev->same_screen = 1;
790
791    _device_modifiers_update(edev);
792    ev->modifiers = edev->xkb.modifiers;
793    ev->modifiers = 0;
794
795    ev->x = edev->seat->ptr.ix;
796    ev->y = edev->seat->ptr.iy;
797    ev->root.x = ev->x;
798    ev->root.y = ev->y;
799
800    ev->multi.device = edev->mt_slot;
801    ev->multi.radius = 1;
802    ev->multi.radius_x = 1;
803    ev->multi.radius_y = 1;
804    ev->multi.pressure = 1.0;
805    ev->multi.angle = 0.0;
806 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
807    if (libinput_event_touch_has_minor(event))
808      ev->multi.radius_x = libinput_event_touch_get_minor(event);
809    if (libinput_event_touch_has_major(event))
810      ev->multi.radius_y = libinput_event_touch_get_major(event);
811    if (libinput_event_touch_has_pressure(event))
812      ev->multi.pressure = libinput_event_touch_get_pressure(event);
813    if (libinput_event_touch_has_orientation(event))
814      ev->multi.angle = libinput_event_touch_get_orientation(event);
815 #endif
816    ev->multi.x = ev->x;
817    ev->multi.y = ev->y;
818    ev->multi.root.x = ev->x;
819    ev->multi.root.y = ev->y;
820    ev->dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
821
822    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
823 }
824
825 static void
826 _device_handle_touch_down(struct libinput_device *device, struct libinput_event_touch *event)
827 {
828    E_Input_Evdev *edev;
829    int w = 0, h = 0;
830
831    if (!(edev = libinput_device_get_user_data(device)))
832      {
833         return;
834      }
835
836    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
837
838    edev->mouse.dx = edev->seat->ptr.ix = edev->seat->ptr.dx =
839      libinput_event_touch_get_x_transformed(event, w);
840    edev->mouse.dy = edev->seat->ptr.iy = edev->seat->ptr.dy =
841      libinput_event_touch_get_y_transformed(event, h);
842
843    edev->mt_slot = libinput_event_touch_get_slot(event);
844
845    _device_handle_touch_motion_send(edev, event);
846    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_DOWN);
847 }
848
849 static void
850 _device_handle_touch_motion(struct libinput_device *device, struct libinput_event_touch *event)
851 {
852    E_Input_Evdev *edev;
853    int w = 0, h = 0;
854
855    if (!(edev = libinput_device_get_user_data(device)))
856      {
857         return;
858      }
859
860    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
861
862    edev->mouse.dx = edev->seat->ptr.dx =
863      libinput_event_touch_get_x_transformed(event, w);
864    edev->mouse.dy = edev->seat->ptr.dy =
865      libinput_event_touch_get_y_transformed(event, h);
866
867    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
868        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
869      {
870         return;
871      }
872
873    edev->seat->ptr.ix = edev->seat->ptr.dx;
874    edev->seat->ptr.iy = edev->seat->ptr.dy;
875
876    edev->mt_slot = libinput_event_touch_get_slot(event);
877
878    _device_handle_touch_motion_send(edev, event);
879 }
880
881 static void
882 _device_handle_touch_up(struct libinput_device *device, struct libinput_event_touch *event)
883 {
884    E_Input_Evdev *edev;
885
886    if (!(edev = libinput_device_get_user_data(device)))
887      {
888         return;
889      }
890
891    edev->mt_slot = libinput_event_touch_get_slot(event);
892    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_UP);
893 }
894
895 static void
896 _device_handle_touch_frame(struct libinput_device *device EINA_UNUSED, struct libinput_event_touch *event EINA_UNUSED)
897 {
898    /* DBG("Unhandled Touch Frame Event"); */
899 }
900
901 static void
902 _e_input_aux_data_event_free(void *user_data EINA_UNUSED, void *ev)
903 {
904    Ecore_Event_Axis_Update *e = (Ecore_Event_Axis_Update *)ev;
905    if (e->axis)
906      free(e->axis);
907    free(e);
908 }
909
910 static void
911 _device_handle_touch_aux_data(struct libinput_device *device, struct libinput_event_touch_aux_data *event)
912 {
913    E_Input_Evdev *edev;
914    E_Input_Backend *input;
915    Ecore_Event_Axis_Update *ev;
916    Ecore_Axis *axis;
917
918    if (libinput_event_touch_aux_data_get_type(event) != LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM &&
919        libinput_event_touch_aux_data_get_value(event) > 0)
920       goto end;
921
922    if (!(edev = libinput_device_get_user_data(device))) goto end;
923    if (!(input = edev->seat->input)) goto end;
924    if (!(ev = calloc(1, sizeof(Ecore_Event_Axis_Update))))goto end;
925
926    ev->window = (Ecore_Window)input->dev->window;
927    ev->event_window = (Ecore_Window)input->dev->window;
928    ev->root_window = (Ecore_Window)input->dev->window;
929    ev->timestamp = libinput_event_touch_aux_data_get_time(event);
930
931    axis = (Ecore_Axis *)calloc(1, sizeof(Ecore_Axis));
932    if (axis)
933      {
934         axis->label = ECORE_AXIS_LABEL_TOUCH_PALM;
935         axis->value = libinput_event_touch_aux_data_get_value(event);
936         ev->naxis = 1;
937      }
938    ev->axis = axis;
939
940    ecore_event_add(ECORE_EVENT_AXIS_UPDATE, ev, _e_input_aux_data_event_free, NULL);
941
942 end:
943    ;
944 }
945
946 E_Input_Evdev *
947 _e_input_evdev_device_create(E_Input_Seat *seat, struct libinput_device *device)
948 {
949    E_Input_Evdev *edev;
950    Eina_List *devices;
951    E_Input_Backend *b_input;
952
953    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
954
955    /* try to allocate space for new evdev */
956    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
957
958    edev->seat = seat;
959    edev->device = device;
960    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
961    edev->fd = -1;
962
963    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
964      {
965         edev->caps |= E_INPUT_SEAT_KEYBOARD;
966         _device_keyboard_setup(edev);
967      }
968
969    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
970      {
971         edev->caps |= E_INPUT_SEAT_POINTER;
972
973         /* TODO: make this configurable */
974         edev->mouse.threshold = 250;
975
976         b_input = seat->input;
977         if (b_input->left_handed == EINA_TRUE)
978           {
979              if (libinput_device_config_left_handed_set(device, 1) !=
980                  LIBINPUT_CONFIG_STATUS_SUCCESS)
981                {
982                   WRN("Failed to set left hand mode about device: %s\n",
983                       libinput_device_get_name(device));
984                }
985           }
986      }
987
988    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
989      {
990         int palm_code;
991         edev->caps |= E_INPUT_SEAT_TOUCH;
992         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
993         if (libinput_device_touch_has_aux_data(device, palm_code))
994           {
995              libinput_device_touch_set_aux_data(device, palm_code);
996           }
997      }
998
999    libinput_device_set_user_data(device, edev);
1000    libinput_device_ref(device);
1001
1002    /* configure device */
1003    _device_configure(edev);
1004
1005    return edev;
1006 }
1007
1008 void
1009 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
1010 {
1011    EINA_SAFETY_ON_NULL_RETURN(edev);
1012
1013    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
1014      {
1015         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
1016         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
1017      }
1018
1019    if (edev->path) eina_stringshare_del(edev->path);
1020    if (edev->device) libinput_device_unref(edev->device);
1021    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
1022
1023    free(edev);
1024 }
1025
1026 Eina_Bool
1027 _e_input_evdev_event_process(struct libinput_event *event)
1028 {
1029    struct libinput_device *device;
1030    Eina_Bool ret = EINA_TRUE;
1031
1032    device = libinput_event_get_device(event);
1033    switch (libinput_event_get_type(event))
1034      {
1035       case LIBINPUT_EVENT_KEYBOARD_KEY:
1036         _device_handle_key(device, libinput_event_get_keyboard_event(event));
1037         break;
1038       case LIBINPUT_EVENT_POINTER_MOTION:
1039         _device_handle_pointer_motion(device,
1040                                       libinput_event_get_pointer_event(event));
1041         break;
1042       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
1043         _device_handle_pointer_motion_absolute(device,
1044                                                libinput_event_get_pointer_event(event));
1045         break;
1046       case LIBINPUT_EVENT_POINTER_BUTTON:
1047         _device_handle_button(device, libinput_event_get_pointer_event(event));
1048         break;
1049       case LIBINPUT_EVENT_POINTER_AXIS:
1050         _device_handle_axis(device, libinput_event_get_pointer_event(event));
1051         break;
1052       case LIBINPUT_EVENT_TOUCH_DOWN:
1053         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
1054         break;
1055       case LIBINPUT_EVENT_TOUCH_MOTION:
1056         _device_handle_touch_motion(device,
1057                                     libinput_event_get_touch_event(event));
1058         break;
1059       case LIBINPUT_EVENT_TOUCH_UP:
1060         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
1061         break;
1062       case LIBINPUT_EVENT_TOUCH_FRAME:
1063         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
1064         break;
1065       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
1066         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
1067         break;
1068       default:
1069         ret = EINA_FALSE;
1070         break;
1071      }
1072
1073    return ret;
1074 }
1075
1076 /**
1077  * @brief Set the axis size of the given device.
1078  *
1079  * @param dev The device to set the axis size to.
1080  * @param w The width of the axis.
1081  * @param h The height of the axis.
1082  *
1083  * This function sets set the width @p w and height @p h of the axis
1084  * of device @p dev. If @p dev is a relative input device, a width and
1085  * height must set for it. If its absolute set the ioctl correctly, if
1086  * not, unsupported device.
1087  */
1088 EINTERN void
1089 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
1090 {
1091    const char *sysname;
1092    float cal[6];
1093    const char *device;
1094    Eina_List *devices;
1095    const char *vals;
1096    enum libinput_config_status status;
1097
1098    EINA_SAFETY_ON_NULL_RETURN(edev);
1099    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
1100
1101    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
1102        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
1103      return;
1104
1105    sysname = libinput_device_get_sysname(edev->device);
1106
1107    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
1108    if (eina_list_count(devices) < 1) return;
1109
1110    EINA_LIST_FREE(devices, device)
1111      {
1112         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
1113         if ((!vals) ||
1114             (sscanf(vals, "%f %f %f %f %f %f",
1115                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
1116           goto cont;
1117
1118         cal[2] /= w;
1119         cal[5] /= h;
1120
1121         status =
1122           libinput_device_config_calibration_set_matrix(edev->device, cal);
1123
1124         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
1125           ERR("Failed to apply calibration");
1126
1127 cont:
1128         eina_stringshare_del(device);
1129         continue;
1130      }
1131 }
1132
1133 E_API const char *
1134 e_input_evdev_name_get(E_Input_Evdev *evdev)
1135 {
1136    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1137    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1138
1139    return libinput_device_get_name(evdev->device);
1140 }
1141
1142 EINTERN const char *
1143 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
1144 {
1145    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1146    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1147
1148    return libinput_device_get_sysname(evdev->device);
1149 }
1150
1151 EINTERN Eina_Bool
1152 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
1153 {
1154    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1155    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1156
1157    edev->key_remap_enabled = enable;
1158
1159    if (enable == EINA_FALSE && edev->key_remap_hash)
1160      {
1161         eina_hash_free(edev->key_remap_hash);
1162         edev->key_remap_hash = NULL;
1163      }
1164
1165    return EINA_TRUE;
1166 }
1167
1168 EINTERN Eina_Bool
1169 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
1170 {
1171    int i;
1172
1173    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1174    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1175    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
1176    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
1177    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
1178    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
1179
1180    if (edev->key_remap_hash == NULL)
1181      edev->key_remap_hash = eina_hash_int32_new(NULL);
1182
1183    if (edev->key_remap_hash == NULL)
1184      {
1185         ERR("Failed to set remap key information : creating a hash is failed.");
1186         return EINA_FALSE;
1187      }
1188
1189    for (i = 0; i < num ; i++)
1190      {
1191         if (!from_keys[i] || !to_keys[i])
1192           {
1193              ERR("Failed to set remap key information : given arguments are invalid.");
1194              return EINA_FALSE;
1195           }
1196      }
1197
1198    for (i = 0; i < num ; i++)
1199      {
1200         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
1201      }
1202
1203    return EINA_TRUE;
1204 }
1205
1206 E_API int
1207 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
1208 {
1209    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
1210    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
1211 }
1212
1213 EINTERN Eina_Bool
1214 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
1215 {
1216    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1217    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1218
1219    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
1220        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
1221      return EINA_FALSE;
1222
1223    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
1224        LIBINPUT_CONFIG_STATUS_SUCCESS)
1225      {
1226         WRN("Failed to set input transformation about device: %s\n",
1227             libinput_device_get_name(edev->device));
1228         return EINA_FALSE;
1229      }
1230
1231    return EINA_TRUE;
1232 }
1233