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