e_input_evdev: fix warnings
[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    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
47        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
48      return;
49
50    sysname = libinput_device_get_sysname(edev->device);
51
52    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
53    if (eina_list_count(devices) < 1) return;
54
55 #ifdef _F_E_INPUT_USE_WL_CALIBRATION_
56    EINA_LIST_FREE(devices, device)
57      {
58         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
59         if ((!vals) ||
60             (sscanf(vals, "%f %f %f %f %f %f",
61                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
62           goto cont;
63
64         cal[2] /= w;
65         cal[5] /= h;
66
67         status =
68           libinput_device_config_calibration_set_matrix(edev->device, cal);
69
70         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
71           ERR("Failed to apply calibration");
72
73 cont:
74         eina_stringshare_del(device);
75         continue;
76      }
77 #endif//_F_E_INPUT_USE_WL_CALIBRATION_
78 #endif//_F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
79 //LCOV_EXCL_STOP
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    E_Input_Backend *b_input;
945
946    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
947
948    /* try to allocate space for new evdev */
949    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
950
951    edev->seat = seat;
952    edev->device = device;
953    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
954    edev->fd = -1;
955
956    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
957      {
958         edev->caps |= E_INPUT_SEAT_KEYBOARD;
959         _device_keyboard_setup(edev);
960      }
961
962    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
963      {
964         edev->caps |= E_INPUT_SEAT_POINTER;
965
966         /* TODO: make this configurable */
967         edev->mouse.threshold = 250;
968
969         b_input = seat->input;
970         if (b_input->left_handed == EINA_TRUE)
971           {
972              if (libinput_device_config_left_handed_set(device, 1) !=
973                  LIBINPUT_CONFIG_STATUS_SUCCESS)
974                {
975                   WRN("Failed to set left hand mode about device: %s\n",
976                       libinput_device_get_name(device));
977                }
978           }
979      }
980
981    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
982      {
983         int palm_code;
984         edev->caps |= E_INPUT_SEAT_TOUCH;
985         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
986         if (libinput_device_touch_has_aux_data(device, palm_code))
987           {
988              libinput_device_touch_set_aux_data(device, palm_code);
989           }
990      }
991
992    libinput_device_set_user_data(device, edev);
993    libinput_device_ref(device);
994
995    /* configure device */
996    _device_configure(edev);
997
998    return edev;
999 }
1000
1001 void
1002 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
1003 {
1004    EINA_SAFETY_ON_NULL_RETURN(edev);
1005
1006    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
1007      {
1008         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
1009         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
1010      }
1011
1012    if (edev->path) eina_stringshare_del(edev->path);
1013    if (edev->device) libinput_device_unref(edev->device);
1014    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
1015
1016    free(edev);
1017 }
1018
1019 Eina_Bool
1020 _e_input_evdev_event_process(struct libinput_event *event)
1021 {
1022    struct libinput_device *device;
1023    Eina_Bool ret = EINA_TRUE;
1024
1025    device = libinput_event_get_device(event);
1026    switch (libinput_event_get_type(event))
1027      {
1028       case LIBINPUT_EVENT_KEYBOARD_KEY:
1029         _device_handle_key(device, libinput_event_get_keyboard_event(event));
1030         break;
1031       case LIBINPUT_EVENT_POINTER_MOTION:
1032         _device_handle_pointer_motion(device,
1033                                       libinput_event_get_pointer_event(event));
1034         break;
1035       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
1036         _device_handle_pointer_motion_absolute(device,
1037                                                libinput_event_get_pointer_event(event));
1038         break;
1039       case LIBINPUT_EVENT_POINTER_BUTTON:
1040         _device_handle_button(device, libinput_event_get_pointer_event(event));
1041         break;
1042       case LIBINPUT_EVENT_POINTER_AXIS:
1043         _device_handle_axis(device, libinput_event_get_pointer_event(event));
1044         break;
1045       case LIBINPUT_EVENT_TOUCH_DOWN:
1046         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
1047         break;
1048       case LIBINPUT_EVENT_TOUCH_MOTION:
1049         _device_handle_touch_motion(device,
1050                                     libinput_event_get_touch_event(event));
1051         break;
1052       case LIBINPUT_EVENT_TOUCH_UP:
1053         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
1054         break;
1055       case LIBINPUT_EVENT_TOUCH_FRAME:
1056         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
1057         break;
1058       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
1059         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
1060         break;
1061       default:
1062         ret = EINA_FALSE;
1063         break;
1064      }
1065
1066    return ret;
1067 }
1068
1069 /**
1070  * @brief Set the axis size of the given device.
1071  *
1072  * @param dev The device to set the axis size to.
1073  * @param w The width of the axis.
1074  * @param h The height of the axis.
1075  *
1076  * This function sets set the width @p w and height @p h of the axis
1077  * of device @p dev. If @p dev is a relative input device, a width and
1078  * height must set for it. If its absolute set the ioctl correctly, if
1079  * not, unsupported device.
1080  */
1081 EINTERN void
1082 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
1083 {
1084    const char *sysname;
1085    float cal[6];
1086    const char *device;
1087    Eina_List *devices;
1088    const char *vals;
1089    enum libinput_config_status status;
1090
1091    EINA_SAFETY_ON_NULL_RETURN(edev);
1092    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
1093
1094    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
1095        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
1096      return;
1097
1098    sysname = libinput_device_get_sysname(edev->device);
1099
1100    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
1101    if (eina_list_count(devices) < 1) return;
1102
1103    EINA_LIST_FREE(devices, device)
1104      {
1105         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
1106         if ((!vals) ||
1107             (sscanf(vals, "%f %f %f %f %f %f",
1108                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
1109           goto cont;
1110
1111         cal[2] /= w;
1112         cal[5] /= h;
1113
1114         status =
1115           libinput_device_config_calibration_set_matrix(edev->device, cal);
1116
1117         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
1118           ERR("Failed to apply calibration");
1119
1120 cont:
1121         eina_stringshare_del(device);
1122         continue;
1123      }
1124 }
1125
1126 E_API const char *
1127 e_input_evdev_name_get(E_Input_Evdev *evdev)
1128 {
1129    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1130    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1131
1132    return libinput_device_get_name(evdev->device);
1133 }
1134
1135 EINTERN const char *
1136 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
1137 {
1138    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1139    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1140
1141    return libinput_device_get_sysname(evdev->device);
1142 }
1143
1144 EINTERN Eina_Bool
1145 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
1146 {
1147    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1148    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1149
1150    edev->key_remap_enabled = enable;
1151
1152    if (enable == EINA_FALSE && edev->key_remap_hash)
1153      {
1154         eina_hash_free(edev->key_remap_hash);
1155         edev->key_remap_hash = NULL;
1156      }
1157
1158    return EINA_TRUE;
1159 }
1160
1161 EINTERN Eina_Bool
1162 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
1163 {
1164    int i;
1165
1166    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1167    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1168    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
1169    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
1170    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
1171    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
1172
1173    if (edev->key_remap_hash == NULL)
1174      edev->key_remap_hash = eina_hash_int32_new(NULL);
1175
1176    if (edev->key_remap_hash == NULL)
1177      {
1178         ERR("Failed to set remap key information : creating a hash is failed.");
1179         return EINA_FALSE;
1180      }
1181
1182    for (i = 0; i < num ; i++)
1183      {
1184         if (!from_keys[i] || !to_keys[i])
1185           {
1186              ERR("Failed to set remap key information : given arguments are invalid.");
1187              return EINA_FALSE;
1188           }
1189      }
1190
1191    for (i = 0; i < num ; i++)
1192      {
1193         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
1194      }
1195
1196    return EINA_TRUE;
1197 }
1198
1199 E_API int
1200 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
1201 {
1202    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
1203    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
1204 }
1205
1206 EINTERN Eina_Bool
1207 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
1208 {
1209    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1210    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1211
1212    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
1213        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
1214      return EINA_FALSE;
1215
1216    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
1217        LIBINPUT_CONFIG_STATUS_SUCCESS)
1218      {
1219         WRN("Failed to set input transformation about device: %s\n",
1220             libinput_device_get_name(edev->device));
1221         return EINA_FALSE;
1222      }
1223
1224    return EINA_TRUE;
1225 }
1226