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