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