e_input: add func to set a input device to a new seat
[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 static void  _device_configured_size_get(E_Input_Evdev *edev, int *x, int *y, int *w, int *h);
6
7 void
8 _device_calibration_set(E_Input_Evdev *edev)
9 {
10    E_Output *output;
11    int w = 0, h = 0;
12    int temp;
13    const char *output_name;
14
15    output_name = libinput_device_get_output_name(edev->device);
16    if (output_name)
17      {
18         E_FREE(edev->output_name);
19         edev->output_name = strdup(output_name);
20         ELOGF("E_INPUT_EVDEV", "Device has output_name:%s", NULL, output_name);
21      }
22
23    output = e_comp_screen_primary_output_get(e_comp->e_comp_screen);
24    e_output_size_get(output, &w, &h);
25
26    if (output)
27      {
28         edev->mouse.minx = edev->mouse.miny = 0;
29         edev->mouse.maxw = w;
30         edev->mouse.maxh = h;
31
32         if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
33           {
34              edev->seat->ptr.dx = (double)(w / 2);
35              edev->seat->ptr.dy = (double)(h / 2);
36              edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
37              edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
38              edev->mouse.dx = edev->seat->ptr.dx;
39              edev->mouse.dy = edev->seat->ptr.dy;
40
41              if (output->config.rotation == 90 || output->config.rotation == 270)
42                {
43                   temp = edev->mouse.minx;
44                   edev->mouse.minx = edev->mouse.miny;
45                   edev->mouse.miny = temp;
46
47                   temp = edev->mouse.maxw;
48                   edev->mouse.maxw = edev->mouse.maxh;
49                   edev->mouse.maxh = temp;
50                }
51           }
52      }
53
54 //LCOV_EXCL_START
55 #ifdef _F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
56    const char *sysname;
57    float cal[6];
58    const char *device;
59    Eina_List *devices;
60
61    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
62        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
63      return;
64
65    sysname = libinput_device_get_sysname(edev->device);
66
67    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
68    if (eina_list_count(devices) < 1) return;
69
70 #ifdef _F_E_INPUT_USE_WL_CALIBRATION_
71    const char *vals;
72    enum libinput_config_status status;
73
74    EINA_LIST_FREE(devices, device)
75      {
76         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
77         if ((!vals) ||
78             (sscanf(vals, "%f %f %f %f %f %f",
79                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
80           goto cont;
81
82         cal[2] /= w;
83         cal[5] /= h;
84
85         status =
86           libinput_device_config_calibration_set_matrix(edev->device, cal);
87
88         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
89           ERR("Failed to apply calibration");
90
91 cont:
92         eina_stringshare_del(device);
93         continue;
94      }
95 #endif//_F_E_INPUT_USE_WL_CALIBRATION_
96 #endif//_F_E_INPUT_ENABLE_DEVICE_CALIBRATION_
97 //LCOV_EXCL_STOP
98 }
99
100 static void
101 _device_touch_count_update(E_Input_Evdev *edev)
102 {
103    unsigned int device_touch_count = 0;
104    static unsigned int _max_device_touch_count = 0;
105
106    E_Input *ei = e_input_get();
107    EINA_SAFETY_ON_NULL_RETURN(ei);
108
109    device_touch_count = libinput_device_touch_get_touch_count(edev->device);
110
111    if (_max_device_touch_count < device_touch_count)
112      _max_device_touch_count = device_touch_count;
113
114    if (e_config->configured_max_touch.use)
115      {
116         if (_max_device_touch_count > 0 && e_config->configured_max_touch.count > _max_device_touch_count)
117           {
118              ELOGF("E_INPUT_EVDEV_TOUCH", "Touch max count(%d) must be less or equal to device's touch count(%d) !\nTouch max count has been shrinken to %d.\n",
119                          NULL, e_config->configured_max_touch.count, _max_device_touch_count, _max_device_touch_count);
120              ei->touch_max_count = _max_device_touch_count;
121           }
122         else
123           {
124              if (ei->touch_max_count != e_config->configured_max_touch.count)
125              {
126                 ELOGF("E_INPUT_EVDEV_TOUCH", "Touch_max_count has been updated to %d -> %d.\n",
127                            NULL, ei->touch_max_count, e_config->configured_max_touch.count);
128                 ei->touch_max_count = e_config->configured_max_touch.count;
129              }
130           }
131      }
132    else
133      {
134         if (ei->touch_max_count < _max_device_touch_count)
135           {
136              ELOGF("E_INPUT_EVDEV_TOUCH", "Touch_max_count has been updated to %d -> %d.\n", NULL,
137                    ei->touch_max_count, _max_device_touch_count);
138              ei->touch_max_count = _max_device_touch_count;
139           }
140      }
141
142    ei->touch_device_count++;
143    ELOGF("E_INPUT_EVDEV_TOUCH", "Touch device count is %d.\n", NULL, ei->touch_device_count);
144 }
145
146 static void
147 _device_configure(E_Input_Evdev *edev)
148 {
149    if (libinput_device_config_tap_get_finger_count(edev->device) > 0)
150      {
151         Eina_Bool tap = EINA_FALSE;
152
153         tap = libinput_device_config_tap_get_default_enabled(edev->device);
154         libinput_device_config_tap_set_enabled(edev->device, tap);
155      }
156
157    _device_calibration_set(edev);
158 }
159
160 static void
161 _device_keyboard_setup(E_Input_Evdev *edev)
162 {
163    E_Input_Backend *input;
164    xkb_mod_index_t xkb_idx;
165
166    if ((!edev) || (!edev->seat)) return;
167    if (!(input = edev->seat->input)) return;
168    if (!input->dev->xkb_ctx) return;
169
170    /* create keymap from xkb context */
171    edev->xkb.keymap = _e_input_device_cached_keymap_get(input->dev->xkb_ctx, NULL, 0);
172    if (!edev->xkb.keymap)
173      {
174         ERR("Failed to create keymap: %m");
175         return;
176      }
177
178    /* create xkb state */
179    if (!(edev->xkb.state = xkb_state_new(edev->xkb.keymap)))
180      {
181         ERR("Failed to create xkb state: %m");
182         return;
183      }
184
185    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CTRL);
186    if (xkb_idx != XKB_MOD_INVALID)
187      edev->xkb.ctrl_mask = 1 << xkb_idx;
188    else
189      edev->xkb.ctrl_mask = 0;
190
191    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_ALT);
192    if (xkb_idx != XKB_MOD_INVALID)
193      edev->xkb.alt_mask = 1 << xkb_idx;
194    else
195      edev->xkb.alt_mask = 0;
196
197    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_SHIFT);
198    if (xkb_idx != XKB_MOD_INVALID)
199      edev->xkb.shift_mask = 1 << xkb_idx;
200    else
201      edev->xkb.shift_mask = 0;
202
203    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_LOGO);
204    if (xkb_idx != XKB_MOD_INVALID)
205      edev->xkb.win_mask = 1 << xkb_idx;
206    else
207      edev->xkb.win_mask = 0;
208
209    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_SCROLL);
210    if (xkb_idx != XKB_MOD_INVALID)
211      edev->xkb.scroll_mask = 1 << xkb_idx;
212    else
213      edev->xkb.scroll_mask = 0;
214
215    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_LED_NAME_NUM);
216    if (xkb_idx != XKB_MOD_INVALID)
217      edev->xkb.num_mask = 1 << xkb_idx;
218    else
219      edev->xkb.num_mask = 0;
220
221    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, XKB_MOD_NAME_CAPS);
222    if (xkb_idx != XKB_MOD_INVALID)
223      edev->xkb.caps_mask = 1 << xkb_idx;
224    else
225      edev->xkb.caps_mask = 0;
226
227    xkb_idx = xkb_map_mod_get_index(edev->xkb.keymap, "ISO_Level3_Shift");
228    if (xkb_idx != XKB_MOD_INVALID)
229      edev->xkb.altgr_mask = 1 << xkb_idx;
230    else
231      edev->xkb.altgr_mask = 0;
232 }
233
234 static int
235 _device_keysym_translate(xkb_keysym_t keysym, unsigned int modifiers, char *buffer, int bytes)
236 {
237    unsigned long hbytes = 0;
238    unsigned char c;
239
240    if (!keysym) return 0;
241    hbytes = (keysym >> 8);
242
243    if (!(bytes &&
244          ((hbytes == 0) ||
245           ((hbytes == 0xFF) &&
246            (((keysym >= XKB_KEY_BackSpace) && (keysym <= XKB_KEY_Clear)) ||
247             (keysym == XKB_KEY_Return) || (keysym == XKB_KEY_Escape) ||
248             (keysym == XKB_KEY_KP_Space) || (keysym == XKB_KEY_KP_Tab) ||
249             (keysym == XKB_KEY_KP_Enter) ||
250             ((keysym >= XKB_KEY_KP_Multiply) && (keysym <= XKB_KEY_KP_9)) ||
251             (keysym == XKB_KEY_KP_Equal) || (keysym == XKB_KEY_Delete))))))
252      return 0;
253
254    if (keysym == XKB_KEY_KP_Space)
255      c = (XKB_KEY_space & 0x7F);
256    else if (hbytes == 0xFF)
257      c = (keysym & 0x7F);
258    else
259      c = (keysym & 0xFF);
260
261    if (modifiers & ECORE_EVENT_MODIFIER_CTRL)
262      {
263         if (((c >= '@') && (c < '\177')) || c == ' ')
264           c &= 0x1F;
265         else if (c == '2')
266           c = '\000';
267         else if ((c >= '3') && (c <= '7'))
268           c -= ('3' - '\033');
269         else if (c == '8')
270           c = '\177';
271         else if (c == '/')
272           c = '_' & 0x1F;
273      }
274    buffer[0] = c;
275    return 1;
276 }
277
278 static void
279 _device_modifiers_update_device(E_Input_Evdev *edev, E_Input_Evdev *from)
280 {
281    xkb_mod_mask_t mask;
282
283    edev->xkb.depressed =
284      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_DEPRESSED);
285    edev->xkb.latched =
286      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LATCHED);
287    edev->xkb.locked =
288      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_LOCKED);
289    edev->xkb.group =
290      xkb_state_serialize_mods(from->xkb.state, XKB_STATE_EFFECTIVE);
291
292    mask = (edev->xkb.depressed | edev->xkb.latched);
293
294    if (mask & from->xkb.ctrl_mask)
295      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_CTRL;
296    if (mask & from->xkb.alt_mask)
297      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALT;
298    if (mask & from->xkb.shift_mask)
299      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
300    if (mask & from->xkb.win_mask)
301      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_WIN;
302    if (mask & from->xkb.scroll_mask)
303      edev->xkb.modifiers |= ECORE_EVENT_LOCK_SCROLL;
304    if (mask & from->xkb.num_mask)
305      edev->xkb.modifiers |= ECORE_EVENT_LOCK_NUM;
306    if (mask & from->xkb.caps_mask)
307      edev->xkb.modifiers |= ECORE_EVENT_LOCK_CAPS;
308    if (mask & from->xkb.altgr_mask)
309      edev->xkb.modifiers |= ECORE_EVENT_MODIFIER_ALTGR;
310 }
311
312 static void
313 _device_modifiers_update(E_Input_Evdev *edev)
314 {
315    Eina_List *l;
316    E_Input_Evdev *ed;
317
318    edev->xkb.modifiers = 0;
319
320    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
321      _device_modifiers_update_device(edev, edev);
322    else
323      {
324         EINA_LIST_FOREACH(edev->seat->devices, l, ed)
325           {
326              if (!(ed->caps & E_INPUT_SEAT_KEYBOARD)) continue;
327              _device_modifiers_update_device(edev, ed);
328           }
329      }
330
331 }
332
333 static int
334 _device_remapped_key_get(E_Input_Evdev *edev, int code)
335 {
336    void *ret = NULL;
337
338    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, code);
339    if (!edev->key_remap_enabled) return code;
340    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->key_remap_hash, code);
341
342    ret = eina_hash_find(edev->key_remap_hash, &code);
343
344    if (ret) code = (int)(intptr_t)ret;
345
346    return code;
347 }
348
349 E_API Ecore_Device *
350 e_input_evdev_get_ecore_device(const char *path, Ecore_Device_Class clas)
351 {
352    const Eina_List *dev_list = NULL;
353    const Eina_List *l;
354    Ecore_Device *dev = NULL;
355    const char *identifier;
356
357    if (!path) return NULL;
358
359    dev_list = ecore_device_list();
360    if (!dev_list) return NULL;
361    EINA_LIST_FOREACH(dev_list, l, dev)
362      {
363         if (!dev) continue;
364         identifier = ecore_device_identifier_get(dev);
365         if (!identifier) continue;
366         if ((ecore_device_class_get(dev) == clas) && !(strcmp(identifier, path)))
367           return dev;
368      }
369    return NULL;
370 }
371
372 static void
373 _e_input_event_mouse_move_cb_free(void *data EINA_UNUSED, void *event)
374 {
375    Ecore_Event_Mouse_Move *ev = event;
376
377    if (ev->dev) ecore_device_unref(ev->dev);
378
379    free(ev);
380 }
381
382 static void
383 _e_input_event_mouse_wheel_cb_free(void *data EINA_UNUSED, void *event)
384 {
385    Ecore_Event_Mouse_Wheel *ev = event;
386
387    if (ev->dev) ecore_device_unref(ev->dev);
388
389    free(ev);
390 }
391
392 static void
393 _e_input_event_mouse_button_cb_free(void *data EINA_UNUSED, void *event)
394 {
395    Ecore_Event_Mouse_Button *ev = event;
396
397    if (ev->dev) ecore_device_unref(ev->dev);
398
399    free(ev);
400 }
401
402 static void
403 _e_input_event_key_cb_free(void *data EINA_UNUSED, void *event)
404 {
405    Ecore_Event_Key *ev = event;
406
407    if (ev->dev) ecore_device_unref(ev->dev);
408    if (ev->data) E_FREE(ev->data);
409
410    free(ev);
411 }
412
413 static void
414 _device_handle_key(struct libinput_device *device, struct libinput_event_keyboard *event)
415 {
416    E_Input_Evdev *edev;
417    E_Input_Backend *input;
418    uint32_t timestamp;
419    uint32_t code, nsyms;
420    const xkb_keysym_t *syms;
421    enum libinput_key_state state;
422    int key_count;
423    xkb_keysym_t sym = XKB_KEY_NoSymbol;
424    char key[256] = {0, }, keyname[256] = {0, }, compose_buffer[256] = {0, };
425    Ecore_Event_Key *e;
426    char *tmp = NULL, *compose = NULL;
427    E_Keyrouter_Event_Data *key_data;
428    Ecore_Device *ecore_dev = NULL, *data;
429    Eina_List *l, *l_next;
430    E_Comp_Config *comp_conf = NULL;
431    int *pressed_keycode = NULL, *idata = NULL;
432    Eina_Bool dup_found = EINA_FALSE;
433
434    if (!(edev = libinput_device_get_user_data(device)))
435      {
436         return;
437      }
438
439    if (!(input = edev->seat->input))
440      {
441         return;
442      }
443
444    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
445    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
446      {
447         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
448           {
449              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_KEYBOARD)
450                {
451                   ecore_dev = data;
452                   break;
453                }
454           }
455      }
456    else
457      {
458         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_KEYBOARD);
459         ecore_dev = edev->ecore_dev;
460      }
461
462    if (!ecore_dev)
463      {
464         ERR("Failed to get source ecore device from event !\n");
465         return;
466      }
467
468    timestamp = libinput_event_keyboard_get_time(event);
469    code = libinput_event_keyboard_get_key(event);
470    code = _device_remapped_key_get(edev, code + 8);
471    state = libinput_event_keyboard_get_key_state(event);
472    key_count = libinput_event_keyboard_get_seat_key_count(event);
473
474    if (state == LIBINPUT_KEY_STATE_PRESSED)
475      {
476         if ((edev->seat->dev->blocked & E_INPUT_SEAT_KEYBOARD) ||
477             (edev->seat->dev->server_blocked & E_INPUT_SEAT_KEYBOARD))
478           {
479              ELOGF("Key", "Press (keycode: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
480                    code, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
481                    edev->seat->dev->server_blocked);
482              return;
483           }
484
485         /* FIXME: Currently to maintain press/release event pair during input block,
486          *        used Eina_List and this is method used in devicemgr module.
487          *        But we need to consider which way is better to maintain key press/release pair.
488          */
489
490         EINA_LIST_FOREACH(edev->xkb.pressed_keys, l, idata)
491           {
492              if (*idata == code)
493                {
494                   dup_found = EINA_TRUE;
495                   break;
496                }
497           }
498         if (!dup_found)
499           {
500              pressed_keycode = E_NEW(int, 1);
501              EINA_SAFETY_ON_NULL_RETURN(pressed_keycode);
502              *pressed_keycode = code;
503
504              edev->xkb.pressed_keys = eina_list_append(edev->xkb.pressed_keys, pressed_keycode);
505           }
506      }
507    else
508      {
509         dup_found = EINA_FALSE;
510         EINA_LIST_FOREACH_SAFE(edev->xkb.pressed_keys, l, l_next, idata)
511           {
512              if (code == *idata)
513                {
514                   edev->xkb.pressed_keys = eina_list_remove_list(edev->xkb.pressed_keys, l);
515                   E_FREE(idata);
516                   dup_found = EINA_TRUE;
517                   break;
518                }
519           }
520         if (!dup_found)
521           {
522              ELOGF("Key", "Release (keycode: %d, device: %s) is blocked by %p", NULL,
523                    code, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client);
524              return;
525           }
526      }
527
528    /* ignore key events that are not seat wide state changes */
529    if (((state == LIBINPUT_KEY_STATE_PRESSED) && (key_count != 1)) ||
530        ((state == LIBINPUT_KEY_STATE_RELEASED) && (key_count != 0)))
531      {
532         return;
533      }
534
535    xkb_state_update_key(edev->xkb.state, code,
536                         (state ? XKB_KEY_DOWN : XKB_KEY_UP));
537
538    /* get the keysym for this code */
539    nsyms = xkb_key_get_syms(edev->xkb.state, code, &syms);
540    if (nsyms == 1) sym = syms[0];
541
542    /* If no keysym was found, name it "Keycode-NNN" */
543    if (sym == XKB_KEY_NoSymbol)
544      snprintf(key, sizeof(key), "Keycode-%u", code);
545    else
546      {
547         /* get the keyname for this sym */
548         xkb_keysym_get_name(sym, key, sizeof(key));
549      }
550
551    if (key[0] == '\0')
552      {
553         /* If no keyname was found, name it "Keycode-NNN" */
554         snprintf(keyname, sizeof(keyname), "Keycode-%u", code);
555      }
556    else
557      {
558         memcpy(keyname, key, sizeof(keyname));
559      }
560
561    /* if shift is active, we need to transform the key to lower */
562    if (xkb_state_mod_index_is_active(edev->xkb.state,
563                                      xkb_map_mod_get_index(edev->xkb.keymap,
564                                      XKB_MOD_NAME_SHIFT),
565                                      XKB_STATE_MODS_EFFECTIVE))
566      {
567         if (keyname[0] != '\0')
568           keyname[0] = tolower(keyname[0]);
569      }
570
571    if (_device_keysym_translate(sym, edev->xkb.modifiers,
572                                 compose_buffer, sizeof(compose_buffer)))
573      {
574         compose = eina_str_convert("ISO8859-1", "UTF-8", compose_buffer);
575         if (!compose)
576           {
577              ERR("E Input cannot convert input key string '%s' to UTF-8. "
578                  "Is Eina built with iconv support?", compose_buffer);
579           }
580         else
581           tmp = compose;
582      }
583
584    if (!compose) compose = compose_buffer;
585
586    e = calloc(1, sizeof(Ecore_Event_Key) + strlen(key) + strlen(keyname) +
587               ((compose[0] != '\0') ? strlen(compose) : 0) + 3);
588    if (!e)
589      {
590         E_FREE(tmp);
591         return;
592      }
593    key_data = E_NEW(E_Keyrouter_Event_Data, 1);
594    if (!key_data)
595      {
596         E_FREE(tmp);
597         E_FREE(e);
598         return;
599      }
600
601    e->keyname = (char *)(e + 1);
602    e->key = e->keyname + strlen(keyname) + 1;
603    e->compose = strlen(compose) ? e->key + strlen(key) + 1 : NULL;
604    e->string = e->compose;
605
606    strncpy((char *)e->keyname, keyname, strlen(keyname));
607    strncpy((char *)e->key, key, strlen(key));
608    if (strlen(compose)) strncpy((char *)e->compose, compose, strlen(compose));
609
610    e->window = (Ecore_Window)input->dev->window;
611    e->event_window = (Ecore_Window)input->dev->window;
612    e->root_window = (Ecore_Window)input->dev->window;
613    e->timestamp = timestamp;
614    e->same_screen = 1;
615    e->keycode = code;
616    e->data = key_data;
617
618    _device_modifiers_update(edev);
619
620    e->modifiers = edev->xkb.modifiers;
621    e->dev = ecore_device_ref(ecore_dev);
622
623    comp_conf = e_comp_config_get();
624    if (comp_conf && comp_conf->input_log_enable)
625      ELOGF("Key", "%s (keyname: %s, keycode: %d, device: %s)", NULL, state?"Press":"Release", e->keyname, e->keycode, ecore_device_name_get(e->dev));
626
627    if (state)
628      ecore_event_add(ECORE_EVENT_KEY_DOWN, e, _e_input_event_key_cb_free, NULL);
629    else
630      ecore_event_add(ECORE_EVENT_KEY_UP, e, _e_input_event_key_cb_free, NULL);
631
632    if (tmp) free(tmp);
633 }
634
635 static void
636 _device_pointer_motion(E_Input_Evdev *edev, struct libinput_event_pointer *event)
637 {
638    E_Input_Backend *input;
639    Ecore_Event_Mouse_Move *ev;
640    Ecore_Device *ecore_dev = NULL, *data, *detent_data = NULL;
641    Eina_List *l;
642    int x = 0, y = 0, w = 0, h = 0;
643
644    if (!(input = edev->seat->input)) return;
645
646    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
647    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
648      {
649         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
650           {
651              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
652                {
653                   ecore_dev = data;
654                   break;
655                }
656              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
657                {
658                   detent_data = data;
659                }
660           }
661         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
662           {
663              ecore_dev = detent_data;
664           }
665      }
666    else
667      {
668         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
669         ecore_dev = edev->ecore_dev;
670      }
671
672    if (!ecore_dev)
673      {
674         ERR("Failed to get source ecore device from event !\n");
675         return;
676      }
677    else if ((detent_data == ecore_dev) || e_devicemgr_detent_is_detent(ecore_device_name_get(ecore_dev)))
678      {
679         /* Do not process detent device's move events. */
680         return;
681      }
682
683    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
684
685    _device_configured_size_get(edev, &x, &y, &w, &h);
686
687    if (edev->seat->ptr.ix < x)
688      edev->seat->ptr.dx = edev->seat->ptr.ix = x;
689    else if (edev->seat->ptr.ix >= (x + w))
690      edev->seat->ptr.dx = edev->seat->ptr.ix = (x + w - 1);
691
692    if (edev->seat->ptr.iy < y)
693      edev->seat->ptr.dy = edev->seat->ptr.iy = y;
694    else if (edev->seat->ptr.iy >= (y + h))
695      edev->seat->ptr.dy = edev->seat->ptr.iy = (y + h - 1);
696
697    edev->mouse.dx = edev->seat->ptr.dx;
698    edev->mouse.dy = edev->seat->ptr.dy;
699
700    ev->window = (Ecore_Window)input->dev->window;
701    ev->event_window = (Ecore_Window)input->dev->window;
702    ev->root_window = (Ecore_Window)input->dev->window;
703    if (event) ev->timestamp = libinput_event_pointer_get_time(event);
704    ev->same_screen = 1;
705
706    _device_modifiers_update(edev);
707    ev->modifiers = edev->xkb.modifiers;
708
709    ev->x = edev->seat->ptr.ix;
710    ev->y = edev->seat->ptr.iy;
711    ev->root.x = ev->x;
712    ev->root.y = ev->y;
713
714    ev->multi.device = edev->mt_slot;
715    ev->multi.radius = 1;
716    ev->multi.radius_x = 1;
717    ev->multi.radius_y = 1;
718    ev->multi.pressure = 1.0;
719    ev->multi.angle = 0.0;
720    ev->multi.x = ev->x;
721    ev->multi.y = ev->y;
722    ev->multi.root.x = ev->x;
723    ev->multi.root.y = ev->y;
724    ev->dev = ecore_device_ref(ecore_dev);
725
726    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
727 }
728
729 void
730 _e_input_pointer_motion_post(E_Input_Evdev *edev)
731 {
732    _device_pointer_motion(edev, NULL);
733 }
734
735 static void
736 _device_handle_pointer_motion(struct libinput_device *device, struct libinput_event_pointer *event)
737 {
738    E_Input_Evdev *edev;
739    double dx, dy, temp;
740
741    if (!(edev = libinput_device_get_user_data(device)))
742      {
743         return;
744      }
745
746    dx = libinput_event_pointer_get_dx(event);
747    dy = libinput_event_pointer_get_dy(event);
748
749    if (edev->seat->ptr.swap)
750      {
751          temp = dx;
752          dx = dy;
753          dy = temp;
754      }
755    if (edev->seat->ptr.invert_x)
756      dx *= -1;
757    if (edev->seat->ptr.invert_y)
758      dy *= -1;
759
760    edev->seat->ptr.dx += dx;
761    edev->seat->ptr.dy += dy;
762
763    edev->mouse.dx = edev->seat->ptr.dx;
764    edev->mouse.dy = edev->seat->ptr.dy;
765
766    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
767        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
768      {
769         return;
770      }
771
772    edev->seat->ptr.ix = edev->seat->ptr.dx;
773    edev->seat->ptr.iy = edev->seat->ptr.dy;
774
775    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
776        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
777      {
778         return;
779      }
780
781   _device_pointer_motion(edev, event);
782 }
783
784 static void
785 _device_handle_pointer_motion_absolute(struct libinput_device *device, struct libinput_event_pointer *event)
786 {
787    E_Input_Evdev *edev;
788    int w = 0, h = 0;
789
790    if (!(edev = libinput_device_get_user_data(device)))
791      {
792         return;
793      }
794
795    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &w, &h);
796
797    edev->mouse.dx = edev->seat->ptr.dx =
798      libinput_event_pointer_get_absolute_x_transformed(event, w);
799    edev->mouse.dy = edev->seat->ptr.dy =
800      libinput_event_pointer_get_absolute_y_transformed(event, h);
801
802    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
803        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
804      {
805         return;
806      }
807
808    edev->seat->ptr.ix = edev->seat->ptr.dx;
809    edev->seat->ptr.iy = edev->seat->ptr.dy;
810
811    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
812        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
813      {
814         return;
815      }
816
817    _device_pointer_motion(edev, event);
818 }
819
820 static void
821 _device_handle_button(struct libinput_device *device, struct libinput_event_pointer *event)
822 {
823    E_Input_Evdev *edev;
824    E_Input_Backend *input;
825    Ecore_Event_Mouse_Button *ev;
826    enum libinput_button_state state;
827    uint32_t button, timestamp;
828    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
829    Eina_List *l;
830    E_Comp_Config *comp_conf = NULL;
831
832    if (!(edev = libinput_device_get_user_data(device)))
833      {
834         return;
835      }
836    if (!(input = edev->seat->input))
837      {
838         return;
839      }
840
841    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
842    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
843      {
844         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
845           {
846              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
847                {
848                   ecore_dev = data;
849                   break;
850                }
851              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
852                {
853                   detent_data = data;
854                }
855           }
856         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
857           {
858              ecore_dev = detent_data;
859           }
860      }
861    else
862      {
863         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
864         ecore_dev = edev->ecore_dev;
865      }
866
867    if (!ecore_dev)
868      {
869         ERR("Failed to get source ecore device from event !\n");
870         return;
871      }
872
873    state = libinput_event_pointer_get_button_state(event);
874    button = libinput_event_pointer_get_button(event);
875    timestamp = libinput_event_pointer_get_time(event);
876
877    button = ((button & 0x00F) + 1);
878    if (button == 3) button = 2;
879    else if (button == 2) button = 3;
880
881    if (state)
882      {
883         if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
884             (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
885           {
886              ELOGF("Mouse", "Button Press (btn: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
887                    button, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
888                    edev->seat->dev->server_blocked);
889              return;
890           }
891         else
892           {
893              edev->mouse.pressed_button |= (1 << button);
894           }
895      }
896    else
897      {
898         if (!(edev->mouse.pressed_button & (1 << button)))
899           {
900              ELOGF("Mouse", "Button Release (btn: %d, device: %s) is blocked by %p, server: 0x%x", NULL,
901                    button, ecore_device_name_get(ecore_dev), edev->seat->dev->blocked_client,
902                    edev->seat->dev->server_blocked);
903              return;
904           }
905         edev->mouse.pressed_button &= ~(1 << button);
906      }
907
908    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button))))
909      {
910         return;
911      }
912
913    ev->window = (Ecore_Window)input->dev->window;
914    ev->event_window = (Ecore_Window)input->dev->window;
915    ev->root_window = (Ecore_Window)input->dev->window;
916    ev->timestamp = timestamp;
917    ev->same_screen = 1;
918
919    _device_modifiers_update(edev);
920    ev->modifiers = edev->xkb.modifiers;
921
922    ev->x = edev->seat->ptr.ix;
923    ev->y = edev->seat->ptr.iy;
924    ev->root.x = ev->x;
925    ev->root.y = ev->y;
926
927    ev->multi.device = edev->mt_slot;
928    ev->multi.radius = 1;
929    ev->multi.radius_x = 1;
930    ev->multi.radius_y = 1;
931    ev->multi.pressure = 1.0;
932    ev->multi.angle = 0.0;
933    ev->multi.x = ev->x;
934    ev->multi.y = ev->y;
935    ev->multi.root.x = ev->x;
936    ev->multi.root.y = ev->y;
937    ev->dev = ecore_device_ref(ecore_dev);
938
939    if (state)
940      {
941         unsigned int current;
942
943         current = timestamp;
944         edev->mouse.did_double = EINA_FALSE;
945         edev->mouse.did_triple = EINA_FALSE;
946
947         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
948             (button == edev->mouse.prev_button))
949           {
950              edev->mouse.did_double = EINA_TRUE;
951              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
952                  (button == edev->mouse.last_button))
953                {
954                   edev->mouse.did_triple = EINA_TRUE;
955                   edev->mouse.prev = 0;
956                   edev->mouse.last = 0;
957                   current = 0;
958                }
959           }
960
961         edev->mouse.last = edev->mouse.prev;
962         edev->mouse.prev = current;
963         edev->mouse.last_button = edev->mouse.prev_button;
964         edev->mouse.prev_button = button;
965      }
966
967    ev->buttons = button;
968
969    if (edev->mouse.did_double)
970      ev->double_click = 1;
971    if (edev->mouse.did_triple)
972      ev->triple_click = 1;
973
974    comp_conf = e_comp_config_get();
975    if (comp_conf && comp_conf->input_log_enable)
976      ELOGF("Mouse", "Button %s (btn: %d, device: %s)", NULL, state?"Press":"Release", button, ecore_device_name_get(ev->dev));
977
978    if (state)
979      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, _e_input_event_mouse_button_cb_free, NULL);
980    else
981      ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, _e_input_event_mouse_button_cb_free, NULL);
982 }
983
984 static void
985 _device_handle_axis(struct libinput_device *device, struct libinput_event_pointer *event)
986 {
987    E_Input_Evdev *edev;
988    E_Input_Backend *input;
989    Ecore_Event_Mouse_Wheel *ev;
990    uint32_t timestamp;
991    enum libinput_pointer_axis axis;
992    Ecore_Device *ecore_dev = NULL, *detent_data = NULL, *data;
993    Eina_List *l;
994    E_Comp_Config *comp_conf = NULL;
995    int direction = 0, z = 0;
996
997    if (!(edev = libinput_device_get_user_data(device)))
998      {
999         return;
1000      }
1001    if (!(input = edev->seat->input))
1002      {
1003         return;
1004      }
1005
1006    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1007    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1008      {
1009         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1010           {
1011              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_MOUSE)
1012                {
1013                   ecore_dev = data;
1014                   break;
1015                }
1016              else if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_NONE)
1017                {
1018                   detent_data = data;
1019                }
1020           }
1021         if (!ecore_dev && e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
1022           {
1023              ecore_dev = detent_data;
1024           }
1025      }
1026    else
1027      {
1028         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_MOUSE);
1029         ecore_dev = edev->ecore_dev;
1030      }
1031
1032    if (!ecore_dev)
1033      {
1034         ERR("Failed to get source ecore device from event !\n");
1035         return;
1036      }
1037
1038    axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
1039    if (libinput_event_pointer_has_axis(event, axis))
1040      z = libinput_event_pointer_get_axis_value_discrete(event, axis);
1041
1042    axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
1043    if (libinput_event_pointer_has_axis(event, axis))
1044      {
1045         direction = 1;
1046         z = libinput_event_pointer_get_axis_value_discrete(event, axis);
1047      }
1048
1049    comp_conf = e_comp_config_get();
1050    if (comp_conf && comp_conf->e_wheel_click_angle)
1051      {
1052         z = (int)(z * comp_conf->e_wheel_click_angle);
1053      }
1054
1055    if ((edev->seat->dev->blocked & E_INPUT_SEAT_POINTER) ||
1056        (edev->seat->dev->server_blocked & E_INPUT_SEAT_POINTER))
1057      {
1058         if (detent_data)
1059           ELOGF("Mouse", "Detent (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1060                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
1061         else
1062           ELOGF("Mouse", "Wheel (direction: %d, value: %d) is blocked by %p, server: 0x%x", NULL,
1063                 direction, z, edev->seat->dev->blocked_client, edev->seat->dev->server_blocked);
1064         return;
1065      }
1066
1067    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Wheel))))
1068      {
1069         return;
1070      }
1071
1072    timestamp = libinput_event_pointer_get_time(event);
1073
1074    ev->window = (Ecore_Window)input->dev->window;
1075    ev->event_window = (Ecore_Window)input->dev->window;
1076    ev->root_window = (Ecore_Window)input->dev->window;
1077    ev->timestamp = timestamp;
1078    ev->same_screen = 1;
1079
1080    _device_modifiers_update(edev);
1081    ev->modifiers = edev->xkb.modifiers;
1082
1083    ev->x = edev->seat->ptr.ix;
1084    ev->y = edev->seat->ptr.iy;
1085    ev->root.x = ev->x;
1086    ev->root.y = ev->y;
1087    ev->dev = ecore_device_ref(ecore_dev);
1088
1089    ev->z = z;
1090    ev->direction = direction;
1091
1092    if (comp_conf && comp_conf->input_log_enable)
1093      {
1094         if (detent_data)
1095           ELOGF("Mouse", "Detent (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1096         else
1097           ELOGF("Mouse", "Wheel (direction: %d, value: %d)", NULL, ev->direction, ev->z);
1098      }
1099
1100    ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, _e_input_event_mouse_wheel_cb_free, NULL);
1101 }
1102
1103 static void
1104 _device_handle_touch_event_send(E_Input_Evdev *edev, struct libinput_event_touch *event, int state)
1105 {
1106    E_Input_Backend *input;
1107    Ecore_Event_Mouse_Button *ev;
1108    uint32_t timestamp, button = 0;
1109    Ecore_Device *ecore_dev = NULL, *data;
1110    Eina_List *l;
1111
1112    if (!edev) return;
1113    if (!(input = edev->seat->input)) return;
1114
1115    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1116    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1117      {
1118         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1119           {
1120              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1121                {
1122                   ecore_dev = data;
1123                   break;
1124                }
1125           }
1126      }
1127    else
1128      {
1129         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1130         ecore_dev = edev->ecore_dev;
1131      }
1132
1133    if (!ecore_dev)
1134      {
1135         ERR("Failed to get source ecore device from event !\n");
1136         return;
1137      }
1138
1139    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) return;
1140
1141    timestamp = libinput_event_touch_get_time(event);
1142
1143    ev->window = (Ecore_Window)input->dev->window;
1144    ev->event_window = (Ecore_Window)input->dev->window;
1145    ev->root_window = (Ecore_Window)input->dev->window;
1146    ev->timestamp = timestamp;
1147    ev->same_screen = 1;
1148
1149    _device_modifiers_update(edev);
1150    ev->modifiers = edev->xkb.modifiers;
1151
1152    ev->x = edev->seat->ptr.ix;
1153    ev->y = edev->seat->ptr.iy;
1154    ev->root.x = ev->x;
1155    ev->root.y = ev->y;
1156
1157    ev->multi.device = edev->mt_slot;
1158    ev->multi.radius = 1;
1159    ev->multi.radius_x = 1;
1160    ev->multi.radius_y = 1;
1161    ev->multi.pressure = 1.0;
1162    ev->multi.angle = 0.0;
1163 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1164    if (libinput_event_get_type(libinput_event_touch_get_base_event(event))
1165        == LIBINPUT_EVENT_TOUCH_DOWN)
1166      {
1167         if (libinput_event_touch_has_minor(event))
1168           ev->multi.radius_x = libinput_event_touch_get_minor(event);
1169         if (libinput_event_touch_has_major(event))
1170           ev->multi.radius_y = libinput_event_touch_get_major(event);
1171         if (libinput_event_touch_has_pressure(event))
1172           ev->multi.pressure = libinput_event_touch_get_pressure(event);
1173         if (libinput_event_touch_has_orientation(event))
1174           ev->multi.angle = libinput_event_touch_get_orientation(event);
1175      }
1176 #endif
1177    ev->multi.x = ev->x;
1178    ev->multi.y = ev->y;
1179    ev->multi.root.x = ev->x;
1180    ev->multi.root.y = ev->y;
1181    ev->dev = ecore_device_ref(ecore_dev);
1182
1183    if (state == ECORE_EVENT_MOUSE_BUTTON_DOWN)
1184      {
1185         unsigned int current;
1186
1187         current = timestamp;
1188         edev->mouse.did_double = EINA_FALSE;
1189         edev->mouse.did_triple = EINA_FALSE;
1190
1191         if (((current - edev->mouse.prev) <= edev->mouse.threshold) &&
1192             (button == edev->mouse.prev_button))
1193           {
1194              edev->mouse.did_double = EINA_TRUE;
1195              if (((current - edev->mouse.last) <= (2 * edev->mouse.threshold)) &&
1196                  (button == edev->mouse.last_button))
1197                {
1198                   edev->mouse.did_triple = EINA_TRUE;
1199                   edev->mouse.prev = 0;
1200                   edev->mouse.last = 0;
1201                   current = 0;
1202                }
1203           }
1204
1205         edev->mouse.last = edev->mouse.prev;
1206         edev->mouse.prev = current;
1207         edev->mouse.last_button = edev->mouse.prev_button;
1208         edev->mouse.prev_button = button;
1209         edev->touch.pressed |= (1 << ev->multi.device);
1210      }
1211    else
1212      {
1213         edev->touch.pressed &= ~(1 << ev->multi.device);
1214      }
1215
1216    ev->buttons = ((button & 0x00F) + 1);
1217
1218    if (edev->mouse.did_double)
1219      ev->double_click = 1;
1220    if (edev->mouse.did_triple)
1221      ev->triple_click = 1;
1222
1223    ecore_event_add(state, ev, _e_input_event_mouse_button_cb_free, NULL);
1224 }
1225
1226 static void
1227 _device_handle_touch_motion_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1228 {
1229    E_Input_Backend *input;
1230    Ecore_Event_Mouse_Move *ev;
1231    Ecore_Device *ecore_dev = NULL, *data;
1232    Eina_List *l;
1233
1234    if (!edev) return;
1235    if (!(input = edev->seat->input)) return;
1236
1237    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1238    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1239      {
1240         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1241           {
1242              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1243                {
1244                   ecore_dev = data;
1245                   break;
1246                }
1247           }
1248      }
1249    else
1250      {
1251         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1252         ecore_dev = edev->ecore_dev;
1253      }
1254
1255    if (!ecore_dev)
1256      {
1257         ERR("Failed to get source ecore device from event !\n");
1258         return;
1259      }
1260
1261    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)))) return;
1262
1263    ev->window = (Ecore_Window)input->dev->window;
1264    ev->event_window = (Ecore_Window)input->dev->window;
1265    ev->root_window = (Ecore_Window)input->dev->window;
1266    ev->timestamp = libinput_event_touch_get_time(event);
1267    ev->same_screen = 1;
1268
1269    _device_modifiers_update(edev);
1270    ev->modifiers = edev->xkb.modifiers;
1271
1272    ev->x = edev->seat->ptr.ix;
1273    ev->y = edev->seat->ptr.iy;
1274    ev->root.x = ev->x;
1275    ev->root.y = ev->y;
1276
1277    ev->multi.device = edev->mt_slot;
1278    ev->multi.radius = 1;
1279    ev->multi.radius_x = 1;
1280    ev->multi.radius_y = 1;
1281    ev->multi.pressure = 1.0;
1282    ev->multi.angle = 0.0;
1283 #if LIBINPUT_SUPPORT_EXTRA_TOUCH_EVENT
1284    if (libinput_event_touch_has_minor(event))
1285      ev->multi.radius_x = libinput_event_touch_get_minor(event);
1286    if (libinput_event_touch_has_major(event))
1287      ev->multi.radius_y = libinput_event_touch_get_major(event);
1288    if (libinput_event_touch_has_pressure(event))
1289      ev->multi.pressure = libinput_event_touch_get_pressure(event);
1290    if (libinput_event_touch_has_orientation(event))
1291      ev->multi.angle = libinput_event_touch_get_orientation(event);
1292 #endif
1293    ev->multi.x = ev->x;
1294    ev->multi.y = ev->y;
1295    ev->multi.root.x = ev->x;
1296    ev->multi.root.y = ev->y;
1297    ev->dev = ecore_device_ref(ecore_dev);
1298
1299    ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, _e_input_event_mouse_move_cb_free, NULL);
1300 }
1301
1302 static void
1303 _device_handle_touch_cancel_send(E_Input_Evdev *edev, struct libinput_event_touch *event)
1304 {
1305    E_Input_Backend *input;
1306    Ecore_Event_Mouse_Button *ev;
1307    uint32_t timestamp, button = 0;
1308    Ecore_Device *ecore_dev = NULL, *data;
1309    Eina_List *l;
1310
1311    if (!edev) return;
1312    if (!(input = edev->seat->input)) return;
1313
1314    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1315    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1316      {
1317         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1318           {
1319              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1320                {
1321                   ecore_dev = data;
1322                   break;
1323                }
1324           }
1325      }
1326    else
1327      {
1328         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1329         ecore_dev = edev->ecore_dev;
1330      }
1331
1332    if (!ecore_dev)
1333      {
1334         ERR("Failed to get source ecore device from event !\n");
1335         return;
1336      }
1337
1338    if (!(ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)))) return;
1339
1340    timestamp = libinput_event_touch_get_time(event);
1341
1342    ev->window = (Ecore_Window)input->dev->window;
1343    ev->event_window = (Ecore_Window)input->dev->window;
1344    ev->root_window = (Ecore_Window)input->dev->window;
1345    ev->timestamp = timestamp;
1346    ev->same_screen = 1;
1347
1348    ev->x = edev->seat->ptr.ix;
1349    ev->y = edev->seat->ptr.iy;
1350    ev->root.x = ev->x;
1351    ev->root.y = ev->y;
1352
1353    ev->multi.device = edev->mt_slot;
1354    ev->multi.radius = 1;
1355    ev->multi.radius_x = 1;
1356    ev->multi.radius_y = 1;
1357    ev->multi.pressure = 1.0;
1358    ev->multi.angle = 0.0;
1359    ev->multi.x = ev->x;
1360    ev->multi.y = ev->y;
1361    ev->multi.root.x = ev->x;
1362    ev->multi.root.y = ev->y;
1363    ev->dev = ecore_device_ref(ecore_dev);
1364
1365    edev->touch.pressed &= ~(1 << ev->multi.device);
1366
1367    ev->buttons = ((button & 0x00F) + 1);
1368
1369    ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_CANCEL, ev, _e_input_event_mouse_button_cb_free, NULL);
1370 }
1371
1372 static void
1373 _device_configured_size_get(E_Input_Evdev *edev, int *x, int *y, int *w, int *h)
1374 {
1375    E_Output *output = NULL;
1376
1377    EINA_SAFETY_ON_NULL_RETURN(edev);
1378
1379    if (!edev->output_configured)
1380      {
1381         if (edev->output_name)
1382           {
1383              output = e_output_find(edev->output_name);
1384              if (output)
1385                {
1386                   edev->mouse.minx = output->config.geom.x;
1387                   edev->mouse.miny = output->config.geom.y;
1388                   edev->mouse.maxw = output->config.geom.w;
1389                   edev->mouse.maxh = output->config.geom.h;
1390                   if (libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
1391                     {
1392                        edev->seat->ptr.dx = (double)(edev->mouse.maxw / 2);
1393                        edev->seat->ptr.dy = (double)(edev->mouse.maxh / 2);
1394                        edev->seat->ptr.ix = (int)edev->seat->ptr.dx;
1395                        edev->seat->ptr.iy = (int)edev->seat->ptr.dy;
1396                        edev->mouse.dx = edev->seat->ptr.dx;
1397                        edev->mouse.dy = edev->seat->ptr.dy;
1398                     }
1399                }
1400           }
1401
1402           edev->output_configured = EINA_TRUE;
1403           ELOGF("E_INPUT_EVDEV", "Device is configured by output x:%d,y:%d (w:%d, h:%d)",
1404                 NULL, edev->mouse.minx, edev->mouse.miny, edev->mouse.maxw, edev->mouse.maxh);
1405      }
1406    if (x) *x = edev->mouse.minx;
1407    if (y) *y = edev->mouse.miny;
1408    if (w) *w = edev->mouse.maxw;
1409    if (h) *h = edev->mouse.maxh;
1410 }
1411
1412 static void
1413 _device_handle_touch_down(struct libinput_device *device, struct libinput_event_touch *event)
1414 {
1415    E_Input_Evdev *edev;
1416    int x = 0, y = 0, w = 0, h = 0;
1417    E_Comp_Config *comp_conf = NULL;
1418
1419    if (!(edev = libinput_device_get_user_data(device)))
1420      {
1421         return;
1422      }
1423
1424    _device_configured_size_get(edev, &x, &y, &w, &h);
1425
1426    edev->mouse.dx = edev->seat->ptr.ix = edev->seat->ptr.dx =
1427      x + libinput_event_touch_get_x_transformed(event, w);
1428    edev->mouse.dy = edev->seat->ptr.iy = edev->seat->ptr.dy =
1429      y + libinput_event_touch_get_y_transformed(event, h);
1430
1431    edev->mt_slot = libinput_event_touch_get_slot(event);
1432    if (edev->mt_slot < 0)
1433      {
1434         /* FIXME: The single touch device return slot id -1
1435          *        But currently we have no API to distinguish multi touch or single touch
1436          *        After libinput 1.11 version, libinput provides get_touch_count API,
1437          *        so we can distinguish multi touch device or single touch device.
1438          */
1439         if (edev->mt_slot == -1)
1440           edev->mt_slot = 0;
1441         else
1442           {
1443              WRN("%d slot touch down events are not supported\n", edev->mt_slot);
1444              return;
1445           }
1446      }
1447
1448    if (edev->mt_slot < e_input_touch_max_count_get())
1449      {
1450         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1451         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1452      }
1453
1454    comp_conf = e_comp_config_get();
1455    if (comp_conf && comp_conf->input_log_enable)
1456      ELOGF("Touch", "Down (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
1457
1458    edev->touch.raw_pressed |= (1 << edev->mt_slot);
1459
1460    if (edev->touch.blocked)
1461      {
1462         ELOGF("Touch", "Down (id: %d, x: %d, y: %d) is blocked by %p, server: 0x%x", NULL,
1463               edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, edev->seat->dev->blocked_client,
1464               edev->seat->dev->server_blocked);
1465         return;
1466      }
1467
1468    _device_handle_touch_motion_send(edev, event);
1469    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_DOWN);
1470 }
1471
1472 static void
1473 _device_handle_touch_motion(struct libinput_device *device, struct libinput_event_touch *event)
1474 {
1475    E_Input_Evdev *edev;
1476    int x = 0, y = 0, w = 0, h = 0;
1477
1478    if (!(edev = libinput_device_get_user_data(device)))
1479      {
1480         return;
1481      }
1482
1483    _device_configured_size_get(edev, &x, &y, &w, &h);
1484
1485    edev->mouse.dx = edev->seat->ptr.dx =
1486      x + libinput_event_touch_get_x_transformed(event, w);
1487    edev->mouse.dy = edev->seat->ptr.dy =
1488      y + libinput_event_touch_get_y_transformed(event, h);
1489
1490    if (floor(edev->seat->ptr.dx) == edev->seat->ptr.ix &&
1491        floor(edev->seat->ptr.dy) == edev->seat->ptr.iy)
1492      {
1493         return;
1494      }
1495
1496    edev->seat->ptr.ix = edev->seat->ptr.dx;
1497    edev->seat->ptr.iy = edev->seat->ptr.dy;
1498
1499    edev->mt_slot = libinput_event_touch_get_slot(event);
1500    if (edev->mt_slot < 0)
1501      {
1502         /* FIXME: The single touch device return slot id -1
1503          *        But currently we have no API to distinguish multi touch or single touch
1504          *        After libinput 1.11 version, libinput provides get_touch_count API,
1505          *        so we can distinguish multi touch device or single touch device.
1506          */
1507         if (edev->mt_slot == -1)
1508           edev->mt_slot = 0;
1509         else
1510           {
1511              WRN("%d slot touch motion events are not supported\n", edev->mt_slot);
1512              return;
1513           }
1514      }
1515
1516    if (edev->mt_slot < e_input_touch_max_count_get())
1517      {
1518         edev->touch.coords[edev->mt_slot].x = edev->seat->ptr.ix;
1519         edev->touch.coords[edev->mt_slot].y = edev->seat->ptr.iy;
1520      }
1521
1522    if (!(edev->touch.pressed & (1 << edev->mt_slot)))
1523      {
1524         if (edev->touch.blocked)
1525           {
1526              return;
1527           }
1528      }
1529
1530    _device_handle_touch_motion_send(edev, event);
1531 }
1532
1533 static void
1534 _device_handle_touch_up(struct libinput_device *device, struct libinput_event_touch *event)
1535 {
1536    E_Input_Evdev *edev;
1537    E_Comp_Config *comp_conf = NULL;
1538
1539    if (!(edev = libinput_device_get_user_data(device)))
1540      {
1541         return;
1542      }
1543
1544    edev->mt_slot = libinput_event_touch_get_slot(event);
1545    if (edev->mt_slot < 0)
1546      {
1547         /* FIXME: The single touch device return slot id -1
1548          *        But currently we have no API to distinguish multi touch or single touch
1549          *        After libinput 1.11 version, libinput provides get_touch_count API,
1550          *        so we can distinguish multi touch device or single touch device.
1551          */
1552         if (edev->mt_slot == -1)
1553           edev->mt_slot = 0;
1554         else
1555           {
1556              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
1557              return;
1558           }
1559      }
1560
1561    if (edev->mt_slot < e_input_touch_max_count_get())
1562      {
1563         edev->mouse.dx = edev->seat->ptr.dx = edev->seat->ptr.ix =
1564           edev->touch.coords[edev->mt_slot].x;
1565         edev->mouse.dy = edev->seat->ptr.dy = edev->seat->ptr.iy =
1566           edev->touch.coords[edev->mt_slot].y;
1567      }
1568
1569    comp_conf = e_comp_config_get();
1570    if (comp_conf && comp_conf->input_log_enable)
1571      ELOGF("Touch", "Up (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
1572
1573    edev->touch.raw_pressed &= ~(1 << edev->mt_slot);
1574
1575    if (edev->touch.blocked)
1576      {
1577         if (!(edev->touch.pressed & (1 << edev->mt_slot)))
1578           {
1579              ELOGF("Touch", "Up (id: %d, x: %d, y: %d) is blocked by %p, server(0x%x)", NULL,
1580                    edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy, edev->seat->dev->blocked_client,
1581                    edev->seat->dev->server_blocked);
1582              if (edev->touch.raw_pressed == 0x0)
1583                {
1584                   edev->touch.blocked = EINA_FALSE;
1585                }
1586              return;
1587           }
1588      }
1589
1590    _device_handle_touch_event_send(edev, event, ECORE_EVENT_MOUSE_BUTTON_UP);
1591 }
1592
1593 static void
1594 _device_handle_touch_cancel(struct libinput_device *device, struct libinput_event_touch *event)
1595 {
1596    E_Input_Evdev *edev;
1597    E_Comp_Config *comp_conf = NULL;
1598
1599    if (!(edev = libinput_device_get_user_data(device)))
1600      {
1601         return;
1602      }
1603
1604    edev->mt_slot = libinput_event_touch_get_slot(event);
1605    if (edev->mt_slot < 0)
1606      {
1607         /* FIXME: The single touch device return slot id -1
1608          *        But currently we have no API to distinguish multi touch or single touch
1609          *        After libinput 1.11 version, libinput provides get_touch_count API,
1610          *        so we can distinguish multi touch device or single touch device.
1611          */
1612         if (edev->mt_slot == -1)
1613           edev->mt_slot = 0;
1614         else
1615           {
1616              WRN("%d slot touch up events are not supported\n", edev->mt_slot);
1617              return;
1618           }
1619      }
1620
1621    comp_conf = e_comp_config_get();
1622    if (comp_conf && comp_conf->input_log_enable)
1623      ELOGF("Touch", "cancel (id: %d, x: %d, y: %d)", NULL, edev->mt_slot, edev->seat->ptr.ix, edev->seat->ptr.iy);
1624
1625    _device_handle_touch_cancel_send(edev, event);
1626 }
1627
1628
1629 static void
1630 _device_handle_touch_frame(struct libinput_device *device EINA_UNUSED, struct libinput_event_touch *event EINA_UNUSED)
1631 {
1632    /* DBG("Unhandled Touch Frame Event"); */
1633 }
1634
1635 static void
1636 _e_input_aux_data_event_free(void *user_data EINA_UNUSED, void *ev)
1637 {
1638    Ecore_Event_Axis_Update *e = (Ecore_Event_Axis_Update *)ev;
1639
1640    if (e->axis) free(e->axis);
1641    if (e->dev) ecore_device_unref(e->dev);
1642
1643    free(e);
1644 }
1645
1646 static void
1647 _device_handle_touch_aux_data(struct libinput_device *device, struct libinput_event_touch_aux_data *event)
1648 {
1649    E_Input_Evdev *edev;
1650    E_Input_Backend *input;
1651    Ecore_Event_Axis_Update *ev;
1652    Ecore_Axis *axis;
1653    Ecore_Device *ecore_dev = NULL, *data;
1654    Eina_List *l;
1655    E_Comp_Config *comp_conf;
1656
1657    if (libinput_event_touch_aux_data_get_type(event) != LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM &&
1658        libinput_event_touch_aux_data_get_value(event) > 0)
1659       goto end;
1660
1661    if (!(edev = libinput_device_get_user_data(device))) goto end;
1662    if (!(input = edev->seat->input)) goto end;
1663
1664    if (edev->ecore_dev) ecore_dev = edev->ecore_dev;
1665    else if (edev->ecore_dev_list && eina_list_count(edev->ecore_dev_list) > 0)
1666      {
1667         EINA_LIST_FOREACH(edev->ecore_dev_list, l, data)
1668           {
1669              if (ecore_device_class_get(data) == ECORE_DEVICE_CLASS_TOUCH)
1670                {
1671                   ecore_dev = data;
1672                   break;
1673                }
1674           }
1675      }
1676    else
1677      {
1678         edev->ecore_dev = e_input_evdev_get_ecore_device(edev->path, ECORE_DEVICE_CLASS_TOUCH);
1679         ecore_dev = edev->ecore_dev;
1680      }
1681
1682    if (!ecore_dev)
1683      {
1684         ERR("Failed to get source ecore device from event !\n");
1685         goto end;
1686      }
1687
1688    if (!(ev = calloc(1, sizeof(Ecore_Event_Axis_Update))))goto end;
1689
1690    ev->window = (Ecore_Window)input->dev->window;
1691    ev->event_window = (Ecore_Window)input->dev->window;
1692    ev->root_window = (Ecore_Window)input->dev->window;
1693    ev->timestamp = libinput_event_touch_aux_data_get_time(event);
1694
1695    axis = (Ecore_Axis *)calloc(1, sizeof(Ecore_Axis));
1696    if (axis)
1697      {
1698         axis->label = ECORE_AXIS_LABEL_TOUCH_PALM;
1699         axis->value = libinput_event_touch_aux_data_get_value(event);
1700         ev->naxis = 1;
1701      }
1702    ev->axis = axis;
1703    ev->dev = ecore_device_ref(ecore_dev);
1704
1705    comp_conf = e_comp_config_get();
1706    if (comp_conf && comp_conf->input_log_enable)
1707      ELOGF("Touch", "Axis (label: %d, value: %lf)", NULL, axis?axis->label:-1, axis?axis->value:0.0);
1708
1709    ecore_event_add(ECORE_EVENT_AXIS_UPDATE, ev, _e_input_aux_data_event_free, NULL);
1710
1711 end:
1712    ;
1713 }
1714
1715 E_Input_Evdev *
1716 _e_input_evdev_device_create(E_Input_Seat *seat, struct libinput_device *device)
1717 {
1718    E_Input_Evdev *edev;
1719    E_Input_Backend *b_input;
1720
1721    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
1722
1723    /* try to allocate space for new evdev */
1724    if (!(edev = calloc(1, sizeof(E_Input_Evdev)))) return NULL;
1725
1726    edev->seat = seat;
1727    edev->device = device;
1728    edev->path = eina_stringshare_printf("%s/%s", e_input_base_dir_get(), libinput_device_get_sysname(device));
1729
1730    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
1731      {
1732         edev->caps |= E_INPUT_SEAT_KEYBOARD;
1733         _device_keyboard_setup(edev);
1734      }
1735
1736    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))
1737      {
1738         edev->caps |= E_INPUT_SEAT_POINTER;
1739
1740         /* TODO: make this configurable */
1741         edev->mouse.threshold = 250;
1742
1743         b_input = seat->input;
1744         if (b_input->left_handed == EINA_TRUE)
1745           {
1746              if (libinput_device_config_left_handed_set(device, 1) !=
1747                  LIBINPUT_CONFIG_STATUS_SUCCESS)
1748                {
1749                   WRN("Failed to set left hand mode about device: %s\n",
1750                       libinput_device_get_name(device));
1751                }
1752           }
1753      }
1754
1755    if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH))
1756      {
1757         int palm_code;
1758         edev->caps |= E_INPUT_SEAT_TOUCH;
1759         palm_code = libinput_device_touch_aux_data_get_code(LIBINPUT_TOUCH_AUX_DATA_TYPE_PALM);
1760         if (libinput_device_touch_has_aux_data(device, palm_code))
1761           {
1762              libinput_device_touch_set_aux_data(device, palm_code);
1763           }
1764
1765         _device_touch_count_update(edev);
1766
1767         edev->touch.coords = calloc(1, sizeof(E_Input_Coord)*e_input_touch_max_count_get());
1768
1769         if  (!edev->touch.coords)
1770           ERR("Failed to allocate memory for touch coords !\n");
1771      }
1772
1773    libinput_device_set_user_data(device, edev);
1774    libinput_device_ref(device);
1775
1776    /* configure device */
1777    _device_configure(edev);
1778
1779    return edev;
1780 }
1781
1782 void
1783 _e_input_evdev_device_destroy(E_Input_Evdev *edev)
1784 {
1785    Ecore_Device *dev;
1786
1787    EINA_SAFETY_ON_NULL_RETURN(edev);
1788
1789    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
1790      {
1791         if (edev->xkb.state) xkb_state_unref(edev->xkb.state);
1792         if (edev->xkb.keymap) xkb_map_unref(edev->xkb.keymap);
1793      }
1794
1795    if (edev->ecore_dev) ecore_device_del(edev->ecore_dev);
1796    if (edev->ecore_dev_list)
1797      EINA_LIST_FREE(edev->ecore_dev_list, dev)
1798        {
1799           ecore_device_del(dev);
1800        }
1801    if (edev->path) eina_stringshare_del(edev->path);
1802    if (edev->device) libinput_device_unref(edev->device);
1803    if (edev->key_remap_hash) eina_hash_free(edev->key_remap_hash);
1804    if (edev->touch.coords)
1805      {
1806         free(edev->touch.coords);
1807         edev->touch.coords = NULL;
1808      }
1809    E_FREE(edev->output_name);
1810
1811    free(edev);
1812 }
1813
1814 Eina_Bool
1815 _e_input_evdev_event_process(struct libinput_event *event)
1816 {
1817    struct libinput_device *device;
1818    Eina_Bool ret = EINA_TRUE;
1819
1820    device = libinput_event_get_device(event);
1821    switch (libinput_event_get_type(event))
1822      {
1823       case LIBINPUT_EVENT_KEYBOARD_KEY:
1824         _device_handle_key(device, libinput_event_get_keyboard_event(event));
1825         break;
1826       case LIBINPUT_EVENT_POINTER_MOTION:
1827         _device_handle_pointer_motion(device,
1828                                       libinput_event_get_pointer_event(event));
1829         break;
1830       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
1831         _device_handle_pointer_motion_absolute(device,
1832                                                libinput_event_get_pointer_event(event));
1833         break;
1834       case LIBINPUT_EVENT_POINTER_BUTTON:
1835         _device_handle_button(device, libinput_event_get_pointer_event(event));
1836         break;
1837       case LIBINPUT_EVENT_POINTER_AXIS:
1838         _device_handle_axis(device, libinput_event_get_pointer_event(event));
1839         break;
1840       case LIBINPUT_EVENT_TOUCH_DOWN:
1841         _device_handle_touch_down(device, libinput_event_get_touch_event(event));
1842         break;
1843       case LIBINPUT_EVENT_TOUCH_MOTION:
1844         _device_handle_touch_motion(device,
1845                                     libinput_event_get_touch_event(event));
1846         break;
1847       case LIBINPUT_EVENT_TOUCH_UP:
1848         _device_handle_touch_up(device, libinput_event_get_touch_event(event));
1849         break;
1850       case LIBINPUT_EVENT_TOUCH_CANCEL:
1851         _device_handle_touch_cancel(device, libinput_event_get_touch_event(event));
1852         break;
1853       case LIBINPUT_EVENT_TOUCH_FRAME:
1854         _device_handle_touch_frame(device, libinput_event_get_touch_event(event));
1855         break;
1856       case LIBINPUT_EVENT_TOUCH_AUX_DATA:
1857         _device_handle_touch_aux_data(device, libinput_event_get_touch_aux_data(event));
1858         break;
1859       default:
1860         ret = EINA_FALSE;
1861         break;
1862      }
1863
1864    return ret;
1865 }
1866
1867 /**
1868  * @brief Set the axis size of the given device.
1869  *
1870  * @param dev The device to set the axis size to.
1871  * @param w The width of the axis.
1872  * @param h The height of the axis.
1873  *
1874  * This function sets set the width @p w and height @p h of the axis
1875  * of device @p dev. If @p dev is a relative input device, a width and
1876  * height must set for it. If its absolute set the ioctl correctly, if
1877  * not, unsupported device.
1878  */
1879 EINTERN void
1880 e_input_evdev_axis_size_set(E_Input_Evdev *edev, int w, int h)
1881 {
1882    const char *sysname;
1883    float cal[6];
1884    const char *device;
1885    Eina_List *devices;
1886    const char *vals;
1887    enum libinput_config_status status;
1888
1889    EINA_SAFETY_ON_NULL_RETURN(edev);
1890    EINA_SAFETY_ON_TRUE_RETURN((w == 0) || (h == 0));
1891
1892    if ((!libinput_device_config_calibration_has_matrix(edev->device)) ||
1893        (libinput_device_config_calibration_get_default_matrix(edev->device, cal) != 0))
1894      return;
1895
1896    sysname = libinput_device_get_sysname(edev->device);
1897
1898    devices = eeze_udev_find_by_subsystem_sysname("input", sysname);
1899    if (eina_list_count(devices) < 1) return;
1900
1901    EINA_LIST_FREE(devices, device)
1902      {
1903         vals = eeze_udev_syspath_get_property(device, "WL_CALIBRATION");
1904         if ((!vals) ||
1905             (sscanf(vals, "%f %f %f %f %f %f",
1906                     &cal[0], &cal[1], &cal[2], &cal[3], &cal[4], &cal[5]) != 6))
1907           goto cont;
1908
1909         cal[2] /= w;
1910         cal[5] /= h;
1911
1912         status =
1913           libinput_device_config_calibration_set_matrix(edev->device, cal);
1914
1915         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
1916           ERR("Failed to apply calibration");
1917
1918 cont:
1919         eina_stringshare_del(device);
1920         continue;
1921      }
1922 }
1923
1924 E_API const char *
1925 e_input_evdev_name_get(E_Input_Evdev *evdev)
1926 {
1927    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1928    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1929
1930    return libinput_device_get_name(evdev->device);
1931 }
1932
1933 EINTERN const char *
1934 e_input_evdev_sysname_get(E_Input_Evdev *evdev)
1935 {
1936    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
1937    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
1938
1939    return libinput_device_get_sysname(evdev->device);
1940 }
1941
1942 EINTERN Eina_Bool
1943 e_input_evdev_key_remap_enable(E_Input_Evdev *edev, Eina_Bool enable)
1944 {
1945    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1946    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1947
1948    edev->key_remap_enabled = enable;
1949
1950    if (enable == EINA_FALSE && edev->key_remap_hash)
1951      {
1952         eina_hash_free(edev->key_remap_hash);
1953         edev->key_remap_hash = NULL;
1954      }
1955
1956    return EINA_TRUE;
1957 }
1958
1959 EINTERN Eina_Bool
1960 e_input_evdev_key_remap_set(E_Input_Evdev *edev, int *from_keys, int *to_keys, int num)
1961 {
1962    int i;
1963
1964    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
1965    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
1966    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
1967    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
1968    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
1969    EINA_SAFETY_ON_TRUE_RETURN_VAL(!edev->key_remap_enabled, EINA_FALSE);
1970
1971    if (edev->key_remap_hash == NULL)
1972      edev->key_remap_hash = eina_hash_int32_new(NULL);
1973
1974    if (edev->key_remap_hash == NULL)
1975      {
1976         ERR("Failed to set remap key information : creating a hash is failed.");
1977         return EINA_FALSE;
1978      }
1979
1980    for (i = 0; i < num ; i++)
1981      {
1982         if (!from_keys[i] || !to_keys[i])
1983           {
1984              ERR("Failed to set remap key information : given arguments are invalid.");
1985              return EINA_FALSE;
1986           }
1987      }
1988
1989    for (i = 0; i < num ; i++)
1990      {
1991         eina_hash_add(edev->key_remap_hash, &from_keys[i], (void *)(intptr_t)to_keys[i]);
1992      }
1993
1994    return EINA_TRUE;
1995 }
1996
1997 E_API int
1998 e_input_evdev_wheel_click_angle_get(E_Input_Evdev *dev)
1999 {
2000    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, -1);
2001    return libinput_device_config_scroll_get_wheel_click_angle(dev->device);
2002 }
2003
2004 EINTERN Eina_Bool
2005 e_input_evdev_touch_calibration_set(E_Input_Evdev *edev, float matrix[6])
2006 {
2007    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2008    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2009
2010    if (!libinput_device_config_calibration_has_matrix(edev->device) ||
2011        !libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_TOUCH))
2012      return EINA_FALSE;
2013
2014    if (libinput_device_config_calibration_set_matrix(edev->device, matrix) !=
2015        LIBINPUT_CONFIG_STATUS_SUCCESS)
2016      {
2017         WRN("Failed to set input transformation about device: %s\n",
2018             libinput_device_get_name(edev->device));
2019         return EINA_FALSE;
2020      }
2021
2022    return EINA_TRUE;
2023 }
2024
2025 EINTERN Eina_Bool
2026 e_input_evdev_mouse_accel_speed_set(E_Input_Evdev *edev, double speed)
2027 {
2028    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, EINA_FALSE);
2029    EINA_SAFETY_ON_NULL_RETURN_VAL(edev->device, EINA_FALSE);
2030
2031    if (!libinput_device_has_capability(edev->device, LIBINPUT_DEVICE_CAP_POINTER))
2032      return EINA_FALSE;
2033
2034    if (!libinput_device_config_accel_is_available(edev->device))
2035      return EINA_FALSE;
2036
2037    if (libinput_device_config_accel_set_speed(edev->device, speed) !=
2038        LIBINPUT_CONFIG_STATUS_SUCCESS)
2039      {
2040         WRN("Failed to set mouse accel about device: %s\n",
2041             libinput_device_get_name(edev->device));
2042         return EINA_FALSE;
2043      }
2044
2045    return EINA_TRUE;
2046 }
2047
2048 EINTERN unsigned int
2049 e_input_evdev_touch_pressed_get(E_Input_Evdev *edev)
2050 {
2051    EINA_SAFETY_ON_NULL_RETURN_VAL(edev, 0x0);
2052
2053    return edev->touch.pressed;
2054 }
2055
2056 const char *
2057 e_input_evdev_seatname_get(E_Input_Evdev *evdev)
2058 {
2059    struct libinput_seat *libinput_seat;
2060    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, NULL);
2061    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, NULL);
2062
2063    libinput_seat = libinput_device_get_seat(evdev->device);
2064
2065    return libinput_seat_get_logical_name(libinput_seat);
2066 }
2067
2068 Eina_Bool
2069 e_input_evdev_seatname_set(E_Input_Evdev *evdev, const char *seatname)
2070 {
2071    Eina_Bool res = EINA_FALSE;
2072    E_Input_Backend *input;
2073
2074    EINA_SAFETY_ON_NULL_RETURN_VAL(seatname, EINA_FALSE);
2075    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev, EINA_FALSE);
2076    EINA_SAFETY_ON_NULL_RETURN_VAL(evdev->device, EINA_FALSE);
2077
2078    if (libinput_device_set_seat_logical_name(evdev->device, seatname) == 0)
2079      {
2080         input = evdev->seat->input;
2081         if (!input) return EINA_FALSE;
2082         if (libinput_dispatch(input->libinput) != 0)
2083           {
2084              ERR("Failed to dispatch libinput events: %m");
2085              return EINA_FALSE;
2086           }
2087
2088         /* process pending events */
2089         _input_events_process(input);
2090         res = EINA_TRUE;
2091      }
2092
2093    return res;
2094 }