89aba2709e58ed0e66325211799dbb8764248d2f
[platform/upstream/enlightenment.git] / src / bin / e_input_device.c
1 #include "e.h"
2 #include "e_input_private.h"
3
4 /* e_input_device private variable */
5 static Eina_List *einput_devices;
6 static E_Input_Device *e_input_device_default = NULL;
7
8 struct xkb_keymap *cached_keymap;
9 struct xkb_context *cached_context;
10
11 static int
12 _device_open_no_pending(const char *device, int flags)
13 {
14    int fd = -1;
15    struct stat s;
16
17    fd = open(device, flags | O_CLOEXEC);
18
19    if (fd < 0) return fd;
20    if (fstat(fd, &s) == -1)
21      {
22         close(fd);
23         return -1;
24      }
25
26    return fd;
27 }
28
29 /* local functions */
30 static int
31 _e_input_device_cb_open_restricted(const char *path, int flags, void *data)
32 {
33    E_Input_Backend *input = (E_Input_Backend *)data;
34    int fd = -1;
35
36    EINA_SAFETY_ON_NULL_RETURN_VAL(input, -1);
37
38    /* try to open the device */
39    fd = _device_open_no_pending(path, flags);
40
41    if (fd < 0)
42      {
43         ERR("Could not open device");
44         return -1;
45      }
46
47    return fd;
48 }
49
50 static void
51 _e_input_device_cb_close_restricted(int fd, void *data)
52 {
53    if (fd >= 0) close(fd);
54 }
55
56 const struct libinput_interface _input_interface =
57 {
58    _e_input_device_cb_open_restricted,
59    _e_input_device_cb_close_restricted,
60 };
61
62 static E_Input_Device *
63 _e_input_device_default_get(void)
64 {
65    return e_input_device_default;
66 }
67
68 struct xkb_context *
69 _e_input_device_cached_context_get(enum xkb_context_flags flags)
70 {
71    if (!cached_context)
72      return xkb_context_new(flags);
73    else
74      return xkb_context_ref(cached_context);
75 }
76
77 struct xkb_keymap *
78 _e_input_device_cached_keymap_get(struct xkb_context *ctx,
79                        const struct xkb_rule_names *names,
80                        enum xkb_keymap_compile_flags flags)
81 {
82    EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
83
84    if (!cached_keymap)
85      return xkb_map_new_from_names(ctx, names, flags);
86    else
87      return xkb_map_ref(cached_keymap);
88 }
89
90 void
91 _e_input_device_cached_context_update(struct xkb_context *ctx)
92 {
93    Eina_List *l;
94    E_Input_Device *dev;
95
96    EINA_LIST_FOREACH(einput_devices, l, dev)
97      {
98         xkb_context_unref(dev->xkb_ctx);
99         dev->xkb_ctx = xkb_context_ref(ctx);
100      }
101 }
102
103 void
104 _e_input_device_cached_keymap_update(struct xkb_keymap *map)
105 {
106    Eina_List *l, *l2, *l3;
107    E_Input_Device *dev;
108    E_Input_Seat *seat;
109    E_Input_Evdev *edev;
110
111    EINA_LIST_FOREACH(einput_devices, l, dev)
112      EINA_LIST_FOREACH(dev->seats, l2, seat)
113        EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l3, edev)
114          {
115             xkb_keymap_unref(edev->xkb.keymap);
116             edev->xkb.keymap = xkb_keymap_ref(map);
117             xkb_state_unref(edev->xkb.state);
118             edev->xkb.state = xkb_state_new(map);
119          }
120 }
121
122 EINTERN void
123 e_input_device_keyboard_cached_context_set(struct xkb_context *ctx)
124 {
125    EINA_SAFETY_ON_NULL_RETURN(ctx);
126
127    if (cached_context == ctx) return;
128
129    if (cached_context)
130      _e_input_device_cached_context_update(ctx);
131
132    cached_context = ctx;
133 }
134
135 EINTERN void
136 e_input_device_keyboard_cached_keymap_set(struct xkb_keymap *map)
137 {
138    EINA_SAFETY_ON_NULL_RETURN(map);
139
140    if (cached_keymap == map) return;
141
142    if (cached_keymap)
143       _e_input_device_cached_keymap_update(map);
144
145    cached_keymap = map;
146 }
147
148 static void
149 e_input_device_destroy(E_Input_Device *dev)
150 {
151    E_Input_Backend *input;
152    E_Input_Seat *seat;
153    E_Input_Evdev *edev;
154
155    EINA_SAFETY_ON_NULL_RETURN(dev);
156
157    EINA_LIST_FREE(dev->seats, seat)
158      {
159         EINA_LIST_FREE(seat->devices, edev)
160           {
161              libinput_device_config_send_events_set_mode(edev->device,
162                                                          LIBINPUT_CONFIG_SEND_EVENTS_DISABLED);
163              _e_input_evdev_device_destroy(edev);
164           }
165
166         if (seat->name)
167           eina_stringshare_del(seat->name);
168         free(seat);
169      }
170
171    EINA_LIST_FREE(dev->inputs, input)
172      {
173         if (input->hdlr)
174           ecore_main_fd_handler_del(input->hdlr);
175         if (input->libinput)
176           libinput_unref(input->libinput);
177         free(input);
178      }
179
180    eina_stringshare_del(dev->seat);
181    xkb_context_unref(dev->xkb_ctx);
182    eina_hash_free(dev->fd_hash);
183    dev->fd_hash = NULL;
184
185    if (dev == e_input_device_default)
186      e_input_device_default = NULL;
187
188    free(dev);
189 }
190
191 static void
192 _e_input_device_add_list(E_Input_Device *dev)
193 {
194    Eina_List *l;
195    E_Input_Device *dev_data;
196
197    EINA_LIST_FOREACH(einput_devices, l, dev_data)
198      {
199         if (dev_data == dev) return;
200      }
201
202    einput_devices = eina_list_append(einput_devices, dev);
203 }
204
205 static void
206 _e_input_device_remove_list(E_Input_Device *dev)
207 {
208    Eina_List *l, *l_next;
209    E_Input_Device *dev_data;
210
211    EINA_LIST_FOREACH_SAFE(einput_devices, l, l_next, dev_data)
212      {
213         if (dev == dev_data)
214           einput_devices = eina_list_remove_list(einput_devices, l);
215      }
216 }
217
218 EINTERN E_Input_Device *
219 e_input_device_open(void)
220 {
221    E_Input_Device *dev = NULL;
222
223    dev = (E_Input_Device *)calloc(1, sizeof(E_Input_Device));
224
225    if (!dev)
226      {
227         EINA_LOG_ERR("Failed to alloc memory for E_Input_Device\n");
228         return NULL;
229      }
230
231    dev->seat = eina_stringshare_add("seat0");
232    dev->fd_hash = eina_hash_string_superfast_new(NULL);
233
234    /* try to create xkb context */
235    if (!(dev->xkb_ctx = _e_input_device_cached_context_get(0)))
236      {
237         ERR("Failed to create xkb context: %m");
238         goto err;
239      }
240
241    if (!e_input_device_default)
242      e_input_device_default = dev;
243
244    _e_input_device_add_list(dev);
245
246    return dev;
247
248 err:
249    if (dev)
250      {
251         eina_stringshare_del(dev->seat);
252         xkb_context_unref(dev->xkb_ctx);
253         free(dev);
254      }
255
256    return NULL;
257 }
258
259 EINTERN Eina_Bool
260 e_input_device_close(E_Input_Device *dev)
261 {
262    /* check for valid device */
263    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
264
265    _e_input_device_remove_list(dev);
266    e_input_device_destroy(dev);
267
268    return EINA_TRUE;
269 }
270
271 EINTERN void
272 e_input_device_window_set(E_Input_Device *dev, Ecore_Window window)
273 {
274    /* check for valid device */
275    EINA_SAFETY_ON_TRUE_RETURN(!dev);
276
277    /* TODO : Must update window of ecore/evas device when the given window */
278    /*        is not equal to the existing window. */
279
280    dev->window = window;
281 }
282
283 EINTERN void
284 e_input_device_pointer_xy_get(E_Input_Device *dev, int *x, int *y)
285 {
286    E_Input_Seat *seat;
287    E_Input_Evdev *edev;
288    Eina_List *l, *ll;
289
290    if (x) *x = 0;
291    if (y) *y = 0;
292
293    if (!dev)
294      dev = _e_input_device_default_get();
295
296    /* check for valid device */
297    EINA_SAFETY_ON_TRUE_RETURN(!dev);
298    EINA_LIST_FOREACH(dev->seats, l, seat)
299      {
300         EINA_LIST_FOREACH(seat->devices, ll, edev)
301           {
302              if (!libinput_device_has_capability(edev->device,
303                                                  LIBINPUT_DEVICE_CAP_POINTER))
304                continue;
305
306              if (x) *x = seat->ptr.dx;
307              if (y) *y = seat->ptr.dy;
308
309              return;
310           }
311      }
312 }
313
314 E_API Eina_Bool
315 e_input_device_pointer_warp(E_Input_Device *dev, int x, int y)
316 {
317    E_Input_Seat *seat;
318    E_Input_Evdev *edev;
319    Eina_List *l, *ll;
320    Eina_Bool found = EINA_FALSE;
321
322    if (!dev)
323      dev = _e_input_device_default_get();
324
325    /* check for valid device */
326    EINA_SAFETY_ON_TRUE_RETURN_VAL(!dev, EINA_FALSE);
327    EINA_LIST_FOREACH(dev->seats, l, seat)
328      {
329         EINA_LIST_FOREACH(seat->devices, ll, edev)
330           {
331              if (!libinput_device_has_capability(edev->device,
332                                                  LIBINPUT_DEVICE_CAP_POINTER))
333                continue;
334
335              seat->ptr.dx = seat->ptr.ix = x;
336              seat->ptr.dy = seat->ptr.iy = y;
337              _e_input_pointer_motion_post(edev);
338
339              found = EINA_TRUE;
340           }
341      }
342
343    if (found)
344      return EINA_TRUE;
345
346    return EINA_FALSE;
347 }
348
349 EINTERN Eina_Bool
350 e_input_device_pointer_left_handed_set(E_Input_Device *dev, Eina_Bool left_handed)
351 {
352    E_Input_Seat *seat = NULL;
353    E_Input_Evdev *edev = NULL;
354    Eina_List *l = NULL, *l2 = NULL;
355
356    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
357    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
358
359    if (dev->left_handed == left_handed)
360      return EINA_TRUE;
361    dev->left_handed = left_handed;
362
363    EINA_LIST_FOREACH(dev->seats, l, seat)
364      {
365         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
366           {
367              if (libinput_device_has_capability(edev->device,
368                                                 LIBINPUT_DEVICE_CAP_POINTER))
369                {
370                   if (libinput_device_config_left_handed_set(edev->device, (int)left_handed) !=
371                       LIBINPUT_CONFIG_STATUS_SUCCESS)
372                     {
373                        WRN("Failed to set left hand mode about device: %s\n",
374                            libinput_device_get_name(edev->device));
375                        continue;
376                     }
377                }
378           }
379      }
380    return EINA_TRUE;
381 }
382
383
384 EINTERN Eina_Bool
385 e_input_device_pointer_rotation_set(E_Input_Device *dev, int rotation)
386 {
387    E_Input_Seat *seat = NULL;
388    Eina_List *l = NULL;
389
390    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
391    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
392
393    if ((rotation % 90 != 0) || (rotation / 90 > 3) || (rotation < 0)) return EINA_FALSE;
394
395    EINA_LIST_FOREACH(dev->seats, l, seat)
396      {
397         switch (rotation)
398           {
399            case 90:
400               seat->ptr.swap = EINA_TRUE;
401               seat->ptr.invert_x = EINA_FALSE;
402               seat->ptr.invert_y = EINA_TRUE;
403               break;
404            case 180:
405               seat->ptr.swap = EINA_FALSE;
406               seat->ptr.invert_x = EINA_TRUE;
407               seat->ptr.invert_y = EINA_TRUE;
408               break;
409            case 270:
410               seat->ptr.swap = EINA_TRUE;
411               seat->ptr.invert_x = EINA_TRUE;
412               seat->ptr.invert_y = EINA_FALSE;
413                 break;
414            case 0:
415               seat->ptr.swap = EINA_FALSE;
416               seat->ptr.invert_x = EINA_FALSE;
417               seat->ptr.invert_y = EINA_FALSE;
418               break;
419            default:
420               break;
421           }
422      }
423    return EINA_TRUE;
424 }
425
426 EINTERN void
427 e_input_device_rotation_set(E_Input_Device *dev, unsigned int rotation)
428 {
429    E_Input_Seat *seat = NULL;
430    E_Input_Evdev *edev = NULL;
431    Eina_List *l = NULL, *l2 = NULL;
432    int temp;
433
434    EINA_SAFETY_ON_NULL_RETURN(dev);
435    EINA_SAFETY_ON_NULL_RETURN(dev->seats);
436
437    EINA_LIST_FOREACH(dev->seats, l, seat)
438      {
439         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
440           {
441              if (libinput_device_has_capability(edev->device,
442                                       LIBINPUT_DEVICE_CAP_POINTER))
443                {
444                   edev->mouse.minx = edev->mouse.miny = 0;
445                   e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen),
446                                        &edev->mouse.maxw, &edev->mouse.maxh);
447
448                   if (rotation == 90 || rotation == 270)
449                     {
450                        temp = edev->mouse.minx;
451                        edev->mouse.minx = edev->mouse.miny;
452                        edev->mouse.miny = temp;
453
454                        temp = edev->mouse.maxw;
455                        edev->mouse.maxw = edev->mouse.maxh;
456                        edev->mouse.maxh = temp;
457                     }
458                }
459           }
460      }
461 }
462
463 static void
464 _e_input_device_touch_matrix_identify(float result[6])
465 {
466    result[0] = 1.0;
467    result[1] = 0.0;
468    result[2] = 0.0;
469    result[3] = 0.0;
470    result[4] = 1.0;
471    result[5] = 0.0;
472 }
473
474 static void
475 _e_input_device_touch_matrix_mulifly(float result[6], float m1[6], float m2[6])
476 {
477    result[0] = m1[0] * m2 [0] + m1[1] * m2[3];
478    result[1] = m1[0] * m2 [1] + m1[1] * m2[4];
479    result[2] = m1[0] * m2 [2] + m1[1] * m2[5] + m1[2];
480    result[3] = m1[3] * m2 [0] + m1[4] * m2[3];
481    result[4] = m1[3] * m2 [1] + m1[4] * m2[4];
482    result[5] = m1[3] * m2 [2] + m1[4] * m2[5] + m1[5];
483 }
484
485 static void
486 _e_input_device_touch_matrix_rotation_get(float result[6], int degree, float w, float h)
487 {
488    if (w == 0.0) w = 1.0;
489    if (h == 0.0) h = 1.0;
490
491    switch (degree)
492      {
493         case 90:
494           result[0] = 0.0;
495           result[1] = -h/w;
496           result[2] = h/w;
497           result[3] = w/h;
498           result[4] = 0.0;
499           result[5] = 0.0;
500           break;
501         case 180:
502           result[0] = -1.0;
503           result[1] = 0.0;
504           result[2] = 1.0;
505           result[3] = 0.0;
506           result[4] = -1.0;
507           result[5] = 1.0;
508           break;
509         case 270:
510           result[0] = 0.0;
511           result[1] = h/w;
512           result[2] = 0.0;
513           result[3] = -w/h;
514           result[4] = 0.0;
515           result[5] = w/h;
516           break;
517         case 0:
518           _e_input_device_touch_matrix_identify(result);
519           break;
520         default:
521           WRN("Please input valid angle(%d)\n", degree);
522      }
523 }
524
525 static void
526 _e_input_device_touch_matrix_translate_get(float result[6], float x, float y, float w, float h, float default_w, float default_h)
527 {
528    if (default_w == 0.0) default_w = 1.0;
529    if (default_h == 0.0) default_h = 1.0;
530
531    result[0] = w / default_w;
532    result[4] = h / default_h;
533    result[2] = x / default_w;
534    result[5] = y / default_h;
535 }
536
537 EINTERN Eina_Bool
538 e_input_device_touch_rotation_set(E_Input_Device *dev, unsigned int rotation)
539 {
540    E_Input_Seat *seat = NULL;
541    E_Input_Evdev *edev = NULL;
542    Eina_List *l = NULL, *l2 = NULL;
543    float mat_translate[6] = {0.0, }, mat_rotation[6] = {0.0, }, result[6] = {0.0, };
544    float default_w = 0.0, default_h = 0.0;
545    Eina_Bool res = EINA_TRUE;
546    int output_w = 0, output_h = 0;
547
548    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
549    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
550
551    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &output_w, &output_h);
552    default_w = (float)output_w;
553    default_h = (float)output_h;
554
555    EINA_LIST_FOREACH(dev->seats, l, seat)
556      {
557         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
558           {
559              if (edev->caps & E_INPUT_SEAT_TOUCH)
560                {
561                   _e_input_device_touch_matrix_identify(mat_translate);
562                   _e_input_device_touch_matrix_identify(mat_rotation);
563                   _e_input_device_touch_matrix_identify(result);
564
565                   if (edev->touch.transform.x || edev->touch.transform.y ||
566                       edev->touch.transform.w || edev->touch.transform.h)
567                     {
568                        _e_input_device_touch_matrix_translate_get(mat_translate,
569                                                                     (float)edev->touch.transform.x,
570                                                                     (float)edev->touch.transform.y,
571                                                                     (float)edev->touch.transform.w,
572                                                                     (float)edev->touch.transform.h,
573                                                                     default_w, default_h);
574
575                     }
576
577                   _e_input_device_touch_matrix_rotation_get(mat_rotation, rotation, default_w, default_h);
578
579                   _e_input_device_touch_matrix_mulifly(result, mat_translate, mat_rotation);
580
581                   if (!e_input_evdev_touch_calibration_set(edev, result))
582                     {
583                        res = EINA_FALSE;
584                        continue;
585                     }
586                   else
587                     {
588                        edev->touch.transform.rotation = rotation;
589                     }
590                }
591           }
592      }
593
594    return res;
595 }
596
597 EINTERN Eina_Bool
598 e_input_device_touch_transformation_set(E_Input_Device *dev, int offset_x, int offset_y, int w, int h)
599 {
600    E_Input_Seat *seat = NULL;
601    E_Input_Evdev *edev = NULL;
602    Eina_List *l = NULL, *l2 = NULL;
603    float mat_translate[6] = {0.0, }, mat_rotation[6] = {0.0 }, result[6] = {0.0, };
604    float default_w = 0.0, default_h = 0.0;
605    Eina_Bool res = EINA_TRUE;
606    int output_w = 0, output_h = 0;
607
608    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
609    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
610    EINA_SAFETY_ON_TRUE_RETURN_VAL((w == 0) || (h == 0), EINA_FALSE);
611
612    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &output_w, &output_h);
613    default_w = (float)output_w;
614    default_h = (float)output_h;
615
616    EINA_LIST_FOREACH(dev->seats, l, seat)
617      {
618         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
619           {
620              if (edev->caps & E_INPUT_SEAT_TOUCH)
621                {
622                   _e_input_device_touch_matrix_identify(mat_translate);
623                   _e_input_device_touch_matrix_identify(mat_rotation);
624                   _e_input_device_touch_matrix_identify(result);
625
626                   _e_input_device_touch_matrix_translate_get(mat_translate,
627                                                                (float)offset_x, (float)offset_y,
628                                                                (float)w, (float)h, default_w, default_h);
629
630                   if (edev->touch.transform.rotation)
631                     {
632                        _e_input_device_touch_matrix_rotation_get(mat_rotation,
633                                                                    edev->touch.transform.rotation,
634                                                                    default_w, default_h);
635                     }
636
637                   _e_input_device_touch_matrix_mulifly(result, mat_translate, mat_rotation);
638
639                   if (!e_input_evdev_touch_calibration_set(edev, result))
640                     {
641                        res = EINA_FALSE;
642                        continue;
643                     }
644                   else
645                     {
646                        edev->touch.transform.x = offset_x;
647                        edev->touch.transform.y = offset_y;
648                        edev->touch.transform.w = w;
649                        edev->touch.transform.h = h;
650                     }
651                }
652           }
653      }
654    return res;
655 }
656
657 static void
658 e_input_device_libinput_log_handler(struct libinput *libinput EINA_UNUSED,
659                                enum libinput_log_priority priority,
660                                const char *format, va_list args)
661 {
662    char buf[1024] = {0,};
663
664    vsnprintf(buf, 1024, format, args);
665    switch (priority)
666      {
667         case LIBINPUT_LOG_PRIORITY_DEBUG:
668            DBG("%s", buf);
669            break;
670         case LIBINPUT_LOG_PRIORITY_INFO:
671            INF("%s", buf);
672            break;
673         case LIBINPUT_LOG_PRIORITY_ERROR:
674            ERR("%s", buf);
675            break;
676         default:
677            break;
678      }
679 }
680
681 EINTERN Eina_Bool
682 e_input_device_input_backend_create(E_Input_Device *dev, E_Input_Libinput_Backend backend)
683 {
684    Eina_Bool res = EINA_FALSE;
685
686    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
687
688    if (backend == E_INPUT_LIBINPUT_BACKEND_UDEV)
689      {
690         TRACE_INPUT_BEGIN(e_input_device_input_create_libinput_udev);
691         res = e_input_device_input_create_libinput_udev(dev);
692         TRACE_INPUT_END();
693      }
694    else if (backend == E_INPUT_LIBINPUT_BACKEND_PATH)
695      {
696         TRACE_INPUT_BEGIN(e_input_device_input_create_libinput_path);
697         res = e_input_device_input_create_libinput_path(dev);
698         TRACE_INPUT_END();
699      }
700
701    return res;
702 }
703
704 static void
705 _einput_device_input_thread_udev_backend_heavy(void *data, Ecore_Thread *th, void *msg_data)
706 {
707    E_Input_Backend *input = (E_Input_Backend *)data;
708
709    EINA_SAFETY_ON_NULL_RETURN(input);
710    EINA_SAFETY_ON_NULL_RETURN(input->dev);
711    EINA_SAFETY_ON_NULL_RETURN(input->dev->seat);
712
713    /* try to create libinput context */
714    input->libinput =
715      libinput_udev_create_context(&_input_interface, input, eeze_udev_get());
716
717    if (!input->libinput)
718      {
719         free(input);
720
721         ERR("Could not create libinput context: %m");
722         return;
723      }
724
725    if (input->log_disable)
726      libinput_log_set_handler(input->libinput, NULL);
727    else
728      {
729         if (input->log_use_eina)
730           libinput_log_set_handler(input->libinput, e_input_device_libinput_log_handler);
731         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
732      }
733
734    TRACE_INPUT_BEGIN(libinput_udev_assign_seat);
735    /* assign udev seat */
736    if (libinput_udev_assign_seat(input->libinput, input->dev->seat) != 0)
737      {
738         ERR("Failed to assign seat: %m");
739         TRACE_INPUT_END();
740         return;
741      }
742    TRACE_INPUT_END();
743
744    return;
745 }
746
747 static void
748 _einput_device_input_thread_udev_backend_notify(void *data, Ecore_Thread *th, void *msg_data)
749 {
750    //TODO : do if there is something to do in main thread
751 }
752
753 static void
754 _einput_device_input_thread_udev_backend_end(void *data, Ecore_Thread *th, void *msg_data)
755 {
756    E_Input_Backend *input = (E_Input_Backend *)data;
757    E_Input_Device *dev = NULL;
758
759    EINA_SAFETY_ON_NULL_RETURN(input);
760    EINA_SAFETY_ON_NULL_RETURN(input->dev);
761
762    input->thread = NULL;
763
764    /* enable this input */
765    if (!e_input_enable_input(input))
766      {
767         ERR("Failed to enable input");
768         return;
769      }
770
771    /* append this input */
772    dev = input->dev;
773    dev->inputs = eina_list_append(dev->inputs, input);
774
775    /* process pending events */
776    _input_events_process(input);
777
778 }
779
780 static void
781 _einput_device_input_thread_udev_backend_cancel(void *data, Ecore_Thread *th, void *msg_data)
782 {
783    E_Input_Backend *input = (E_Input_Backend *)data;
784
785    EINA_SAFETY_ON_NULL_RETURN(input);
786
787    input->thread = NULL;
788 }
789
790 static Eina_Bool
791 _e_input_device_input_thread_init_udev_backend(E_Input_Backend *input)
792 {
793    EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
794
795    input->thread = ecore_thread_feedback_run((Ecore_Thread_Cb)_einput_device_input_thread_udev_backend_heavy,
796                                              (Ecore_Thread_Notify_Cb)_einput_device_input_thread_udev_backend_notify,
797                                              (Ecore_Thread_Cb)_einput_device_input_thread_udev_backend_end,
798                                              (Ecore_Thread_Cb)_einput_device_input_thread_udev_backend_cancel, input, 1);
799    return !!(input->thread);
800 }
801
802 /* public functions */
803 EINTERN Eina_Bool
804 e_input_device_input_create_libinput_udev(E_Input_Device *dev)
805 {
806    E_Input_Backend *input;
807    char *env = NULL;
808    int buf_size = 0;
809    int res = 0;
810
811    /* check for valid device */
812    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
813
814    /* try to allocate space for new input structure */
815    if (!(input = calloc(1, sizeof(E_Input_Backend))))
816      {
817         return EINA_FALSE;
818      }
819
820    /* set reference for parent device */
821    input->dev = dev;
822
823    input->backend = E_INPUT_LIBINPUT_BACKEND_UDEV;
824    input->log_disable = EINA_FALSE;
825    input->log_use_eina = EINA_FALSE;
826
827    env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_DISABLE);
828    if ((env) && (atoi(env) == 1))
829      input->log_disable = EINA_TRUE;
830    else
831      {
832         if (env) E_FREE(env);
833
834         env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG);
835         if ((env) && (atoi(env) == 1))
836           input->log_use_eina = EINA_TRUE;
837      }
838    E_FREE(env);
839
840    env = e_util_env_get("UDEV_MONITOR_EVENT_SOURCE");
841
842    if (env)
843      {
844         libinput_udev_set_udev_monitor_event_source(env);
845      }
846    E_FREE(env);
847
848    env = e_util_env_get("UDEV_MONITOR_BUFFER_SIZE");
849
850    if ((env) && (buf_size = atoi(env)))
851      {
852         res = libinput_udev_set_udev_monitor_buffer_size(buf_size);
853         if (res)
854           ERR("Wrong buffer size for udev monitor : %d\n", buf_size);
855      }
856    E_FREE(env);
857
858    if (e_input_thread_enabled_get())
859      {
860         /* intialize libinput udev backend within an ecore thread */
861         if (!_e_input_device_input_thread_init_udev_backend(input))
862           {
863              ERR("Failed to initialize e_input backend (libinput udev backend) !");
864              goto err;
865         }
866
867         return EINA_TRUE;
868      }
869
870    /* try to create libinput context */
871    input->libinput =
872      libinput_udev_create_context(&_input_interface, input, eeze_udev_get());
873    if (!input->libinput)
874      {
875         ERR("Could not create libinput context: %m");
876         goto err;
877      }
878
879    if (input->log_disable)
880      libinput_log_set_handler(input->libinput, NULL);
881    else
882      {
883         if (input->log_use_eina)
884           libinput_log_set_handler(input->libinput, e_input_device_libinput_log_handler);
885         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
886      }
887
888    /* assign udev seat */
889    TRACE_INPUT_BEGIN(libinput_udev_assign_seat);
890    if (libinput_udev_assign_seat(input->libinput, dev->seat) != 0)
891      {
892         ERR("Failed to assign seat: %m");
893         TRACE_INPUT_END();
894         goto err;
895      }
896    TRACE_INPUT_END();
897
898    /* enable this input */
899    if (!e_input_enable_input(input))
900      {
901         ERR("Failed to enable input");
902         goto err;
903      }
904
905    /* append this input */
906    dev->inputs = eina_list_append(dev->inputs, input);
907
908    /* process pending events */
909    _input_events_process(input);
910
911    return EINA_TRUE;
912
913 err:
914    if (input->libinput) libinput_unref(input->libinput);
915    free(input);
916
917    return EINA_FALSE;
918 }
919
920 static void
921 _einput_device_input_thread_path_backend_heavy(void *data, Ecore_Thread *th, void *msg_data)
922 {
923    char *env = NULL;
924    struct libinput_device *device;
925    E_Input_Backend *input = (E_Input_Backend *)data;
926
927    EINA_SAFETY_ON_NULL_RETURN(input);
928    EINA_SAFETY_ON_NULL_RETURN(input->dev);
929    EINA_SAFETY_ON_NULL_RETURN(input->dev->seat);
930
931    /* try to create libinput context */
932    input->libinput =
933      libinput_path_create_context(&_input_interface, input);
934    if (!input->libinput)
935      {
936         free(input);
937
938         ERR("Could not create libinput path context: %m");
939         return;
940      }
941
942    if (input->log_disable)
943      libinput_log_set_handler(input->libinput, NULL);
944    else
945      {
946         if (input->log_use_eina)
947           libinput_log_set_handler(input->libinput, e_input_device_libinput_log_handler);
948         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
949      }
950
951    TRACE_INPUT_BEGIN(libinput_path_add_device_loop);
952    for (int i = 0; i < input->path_ndevices; i++)
953      {
954         char buf[1024] = "PATH_DEVICE_";
955         eina_convert_itoa(i + 1, buf + 12);
956         env = e_util_env_get(buf);
957
958         if (env)
959           {
960              device = libinput_path_add_device(input->libinput, env);
961              if (!device)
962                ERR("Failed to initialized device %s", env);
963              else
964                INF("libinput_path created input device %s", env);
965              E_FREE(env);
966           }
967      }
968    TRACE_INPUT_END();
969
970    return;
971 }
972
973 static void
974 _einput_device_input_thread_path_backend_notify(void *data, Ecore_Thread *th, void *msg_data)
975 {
976    //TODO : do if there is something to do in main thread
977 }
978
979 static void
980 _einput_device_input_thread_path_backend_end(void *data, Ecore_Thread *th, void *msg_data)
981 {
982    E_Input_Backend *input = (E_Input_Backend *)data;
983    E_Input_Device *dev = NULL;
984
985    EINA_SAFETY_ON_NULL_RETURN(input);
986    EINA_SAFETY_ON_NULL_RETURN(input->dev);
987
988    input->thread = NULL;
989
990    /* enable this input */
991    if (!e_input_enable_input(input))
992      {
993         ERR("Failed to enable input");
994         return;
995      }
996
997    /* append this input */
998    dev = input->dev;
999    dev->inputs = eina_list_append(dev->inputs, input);
1000
1001    /* process pending events */
1002    _input_events_process(input);
1003 }
1004
1005 static void
1006 _einput_device_input_thread_path_backend_cancel(void *data, Ecore_Thread *th, void *msg_data)
1007 {
1008    E_Input_Backend *input = (E_Input_Backend *)data;
1009
1010    EINA_SAFETY_ON_NULL_RETURN(input);
1011
1012    input->thread = NULL;
1013 }
1014
1015 static Eina_Bool
1016 _e_input_device_input_thread_init_path_backend(E_Input_Backend *input)
1017 {
1018    EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
1019
1020    input->thread = ecore_thread_feedback_run((Ecore_Thread_Cb)_einput_device_input_thread_path_backend_heavy,
1021                                              (Ecore_Thread_Notify_Cb)_einput_device_input_thread_path_backend_notify,
1022                                              (Ecore_Thread_Cb)_einput_device_input_thread_path_backend_end,
1023                                              (Ecore_Thread_Cb)_einput_device_input_thread_path_backend_cancel, input, 1);
1024    return !!(input->thread);
1025 }
1026
1027 EINTERN Eina_Bool
1028 e_input_device_input_create_libinput_path(E_Input_Device *dev)
1029 {
1030    E_Input_Backend *input;
1031    struct libinput_device *device;
1032    int ndevices = 0;
1033    char *env;
1034
1035    /* check for valid device */
1036    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1037
1038    env = e_util_env_get("PATH_DEVICES_NUM");
1039    if (env)
1040      {
1041         ndevices = atoi(env);
1042         E_FREE(env);
1043      }
1044
1045    if (ndevices <= 0 || ndevices >= INT_MAX)
1046      {
1047         return EINA_TRUE;
1048      }
1049
1050    INF("PATH_DEVICES_NUM : %d", ndevices);
1051
1052    /* try to allocate space for new input structure */
1053    if (!(input = calloc(1, sizeof(E_Input_Backend))))
1054      {
1055         return EINA_FALSE;
1056      }
1057
1058    /* set reference for parent device */
1059    input->dev = dev;
1060
1061    input->backend = E_INPUT_LIBINPUT_BACKEND_PATH;
1062    input->path_ndevices = ndevices;
1063    input->log_disable = EINA_FALSE;
1064    input->log_use_eina = EINA_FALSE;
1065
1066    env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_DISABLE);
1067    if ((env) && (atoi(env) == 1))
1068      input->log_disable = EINA_TRUE;
1069    else
1070      {
1071         if (env) E_FREE(env);
1072
1073         env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG);
1074         if ((env) && (atoi(env) == 1))
1075           input->log_use_eina = EINA_TRUE;
1076      }
1077    E_FREE(env);
1078
1079    if (e_input_thread_enabled_get())
1080      {
1081         /* intialize libinput path backend within an ecore thread */
1082         if (!_e_input_device_input_thread_init_path_backend(input))
1083           {
1084              ERR("Failed to initialize e_input backend (libinput path backend) !");
1085              goto err;
1086         }
1087
1088         return EINA_TRUE;
1089      }
1090
1091    /* try to create libinput context */
1092    input->libinput =
1093      libinput_path_create_context(&_input_interface, input);
1094    if (!input->libinput)
1095      {
1096         ERR("Could not create libinput path context: %m");
1097         goto err;
1098      }
1099
1100    if (input->log_disable)
1101      libinput_log_set_handler(input->libinput, NULL);
1102    else
1103      {
1104         if (input->log_use_eina)
1105           libinput_log_set_handler(input->libinput, e_input_device_libinput_log_handler);
1106         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
1107      }
1108
1109    TRACE_INPUT_BEGIN(libinput_path_add_device_loop);
1110    for (int i = 0; i < ndevices; i++)
1111      {
1112         char buf[1024] = "PATH_DEVICE_";
1113         eina_convert_itoa(i + 1, buf + 12);
1114         env = e_util_env_get(buf);
1115         if (env)
1116           {
1117              device = libinput_path_add_device(input->libinput, env);
1118              if (!device)
1119                ERR("Failed to initialized device %s", env);
1120              else
1121                INF("libinput_path created input device %s", env);
1122              E_FREE(env);
1123           }
1124      }
1125    TRACE_INPUT_END();
1126
1127    /* enable this input */
1128    if (!e_input_enable_input(input))
1129      {
1130         ERR("Failed to enable input");
1131         goto err;
1132      }
1133
1134    /* append this input */
1135    dev->inputs = eina_list_append(dev->inputs, input);
1136
1137    /* process pending events */
1138    _input_events_process(input);
1139
1140    return EINA_TRUE;
1141
1142 err:
1143    if (input->libinput) libinput_unref(input->libinput);
1144    free(input);
1145
1146    return EINA_FALSE;
1147 }
1148
1149 void
1150 e_input_device_output_changed(E_Input_Device *dev)
1151 {
1152    E_Input_Seat *seat = NULL;
1153    E_Input_Evdev *edev = NULL;
1154    Eina_List *l = NULL, *l2 = NULL;
1155
1156    EINA_SAFETY_ON_NULL_RETURN(dev);
1157    EINA_SAFETY_ON_NULL_RETURN(dev->seats);
1158
1159    EINA_LIST_FOREACH(dev->seats, l, seat)
1160      {
1161         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1162           {
1163              _device_calibration_set(edev);
1164           }
1165      }
1166 }
1167
1168 E_API const Eina_List *
1169 e_input_devices_get(void)
1170 {
1171    return einput_devices;
1172 }
1173
1174 EAPI Eina_Bool
1175 e_input_device_mouse_accel_speed_set(E_Input_Device *dev, double speed)
1176 {
1177    E_Input_Seat *seat = NULL;
1178    E_Input_Evdev *edev = NULL;
1179    Eina_List *l = NULL, *l2 = NULL;
1180    Eina_Bool res = EINA_TRUE, ret = EINA_TRUE;
1181
1182    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1183    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
1184
1185    EINA_LIST_FOREACH(dev->seats, l, seat)
1186      {
1187         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1188           {
1189              if (edev->caps & E_INPUT_SEAT_POINTER)
1190                res = e_input_evdev_mouse_accel_speed_set(edev, speed);
1191              if (!res) ret = EINA_FALSE;
1192           }
1193      }
1194
1195    return ret;
1196 }
1197
1198 E_API unsigned int
1199 e_input_device_touch_pressed_get(E_Input_Device *dev)
1200 {
1201    E_Input_Seat *seat = NULL;
1202    E_Input_Evdev *edev = NULL;
1203    Eina_List *l = NULL, *l2 = NULL;
1204    unsigned int pressed = 0x0;
1205
1206    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1207
1208    EINA_LIST_FOREACH(dev->seats, l, seat)
1209      {
1210         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1211           {
1212              if (edev->caps & E_INPUT_SEAT_TOUCH)
1213                pressed |= e_input_evdev_touch_pressed_get(edev);
1214           }
1215      }
1216
1217    return pressed;
1218 }