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