64201c2066077aa0621671d84ceafde76ca5dd34
[platform/upstream/enlightenment.git] / src / bin / e_input_inputs.c
1 #include "e.h"
2 #include "e_input_intern.h"
3 #include "e_input_private.h"
4 #include "e_input_event_intern.h"
5 #include "e_keyrouter_intern.h"
6 #include "e_comp_wl_intern.h"
7
8 #include <glib.h>
9
10 static gboolean input_dispatch(GSource *source, GSourceFunc callback, gpointer user_data);
11 static gboolean input_thread_prepare(GSource *source, gint *time);
12
13 static E_Input_Event_Source *g_input_event_source = NULL;
14 static GList *_key_event_list = NULL;
15
16 GSourceFuncs input_event_funcs = {
17    .prepare = input_thread_prepare,
18    .check = NULL,
19    .dispatch = input_dispatch,
20    .finalize = NULL
21 };
22
23 typedef struct
24 {
25    GSource gsource;
26    gpointer tag;
27 } InputEventSource;
28
29 static char *
30 _e_input_ecore_device_class_to_string(Ecore_Device_Class clas)
31 {
32    switch (clas)
33      {
34         case ECORE_DEVICE_CLASS_NONE:
35           return "None";
36           break;
37         case ECORE_DEVICE_CLASS_SEAT:
38           return "Seat";
39           break;
40         case ECORE_DEVICE_CLASS_KEYBOARD:
41           return "Keyboard";
42           break;
43         case ECORE_DEVICE_CLASS_MOUSE:
44           return "Mouse";
45           break;
46         case ECORE_DEVICE_CLASS_TOUCH:
47           return "Touch";
48           break;
49         case ECORE_DEVICE_CLASS_PEN:
50           return "Pen";
51           break;
52         case ECORE_DEVICE_CLASS_WAND:
53           return "Wand";
54           break;
55         case ECORE_DEVICE_CLASS_GAMEPAD:
56           return "Gamepad";
57           break;
58         default:
59           return "Unknown";
60      }
61 }
62
63 static Ecore_Device_Class
64 _e_input_seat_cap_to_ecore_device_class(unsigned int cap)
65 {
66    switch(cap)
67      {
68       case E_INPUT_SEAT_POINTER:
69          return ECORE_DEVICE_CLASS_MOUSE;
70       case E_INPUT_SEAT_KEYBOARD:
71          return ECORE_DEVICE_CLASS_KEYBOARD;
72       case E_INPUT_SEAT_TOUCH:
73          return ECORE_DEVICE_CLASS_TOUCH;
74       default:
75          return ECORE_DEVICE_CLASS_NONE;
76      }
77    return ECORE_DEVICE_CLASS_NONE;
78 }
79
80 static void
81 _e_input_ecore_device_info_free(void *data EINA_UNUSED, void *ev)
82 {
83    Ecore_Event_Device_Info *e;
84
85    e = ev;
86    eina_stringshare_del(e->name);
87    eina_stringshare_del(e->identifier);
88    eina_stringshare_del(e->seatname);
89
90    free(e);
91 }
92
93 void
94 _e_input_ecore_device_event(Ecore_Device *dev, const char* seat_name, Eina_Bool flag)
95 {
96    Ecore_Event_Device_Info *e;
97    E_Input *e_input;
98    const char *name, *identifier;
99
100    ecore_thread_main_loop_begin();
101
102    if (!(name = ecore_device_name_get(dev))) goto end;
103    if (!(identifier = ecore_device_identifier_get(dev))) goto end;
104
105    if (!(e = calloc(1, sizeof(Ecore_Event_Device_Info)))) goto end;
106
107    e_input = e_input_get();
108
109    e->window = e_input?e_input->window:(Ecore_Window)0;
110    e->name = eina_stringshare_add(name);
111    e->identifier = eina_stringshare_add(identifier);
112    if (seat_name && strlen(seat_name))
113      e->seatname = eina_stringshare_add(seat_name);
114    else
115      e->seatname = eina_stringshare_add(name);
116    e->clas = ecore_device_class_get(dev);
117    e->subclas = ecore_device_subclass_get(dev);
118
119    if (flag)
120      ecore_event_add(ECORE_EVENT_DEVICE_ADD, e, _e_input_ecore_device_info_free, NULL);
121    else
122      ecore_event_add(ECORE_EVENT_DEVICE_DEL, e, _e_input_ecore_device_info_free, NULL);
123
124 end:
125    ecore_thread_main_loop_end();
126 }
127
128 static E_Input_Seat *
129 _seat_create(E_Input_Backend *input, const char *seat)
130 {
131    E_Input_Seat *s;
132    Ecore_Device *ecore_dev = NULL;
133    E_Device *e_dev = NULL;
134
135    ecore_thread_main_loop_begin();
136
137    /* create an evas device of a seat */
138    ecore_dev = ecore_device_add();
139    if (!ecore_dev)
140      {
141         ERR("Failed to create an ecore device for a seat !\n");
142         ecore_thread_main_loop_end();
143                 return NULL;
144      }
145
146    ecore_device_name_set(ecore_dev, seat);
147    ecore_device_identifier_set(ecore_dev, "Enlightenment seat");
148    ecore_device_class_set(ecore_dev, ECORE_DEVICE_CLASS_SEAT);
149    ecore_device_subclass_set(ecore_dev, ECORE_DEVICE_SUBCLASS_NONE);
150
151    /* create an e device of a seat */
152    e_dev = e_device_new();
153    if (!e_dev)
154      {
155         ERR("Failed to create an ecore device for a seat !\n");
156         ecore_thread_main_loop_end();
157                 return NULL;
158      }
159
160    e_device_name_set(e_dev, seat);
161    e_device_identifier_set(e_dev, "Enlightenment seat");
162    e_device_class_set(e_dev, ECORE_DEVICE_CLASS_SEAT);
163    e_device_subclass_set(e_dev, ECORE_DEVICE_SUBCLASS_NONE);
164
165    /* try to allocate space for new seat */
166    if (!(s = calloc(1, sizeof(E_Input_Seat))))
167      {
168         ecore_device_del(ecore_dev);
169         ecore_thread_main_loop_end();
170         return NULL;
171      }
172
173    s->input = input;
174    s->name = eina_stringshare_add(seat);
175    s->ecore_dev = ecore_dev;
176    s->e_dev = e_dev;
177
178    /* add this new seat to list */
179    input->dev->seats = eina_list_append(input->dev->seats, s);
180    s->dev = input->dev;
181
182    ecore_event_add(E_INPUT_EVENT_SEAT_ADD, NULL, NULL, NULL);
183    ecore_thread_main_loop_end();
184
185    _e_input_ecore_device_event(ecore_dev, seat, EINA_TRUE);
186
187    return s;
188 }
189
190 static E_Input_Seat *
191 _seat_get(E_Input_Backend *input, const char *seat)
192 {
193    E_Input_Seat *s;
194    Eina_List *l;
195
196    /* search for this name in existing seats */
197    EINA_LIST_FOREACH(input->dev->seats, l, s)
198      if (!strcmp(s->name, seat))
199        return s;
200
201    return _seat_create(input, seat);
202 }
203
204 static Eina_Bool
205 _e_input_add_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
206 {
207    const Eina_List *dev_list = NULL;
208    const Eina_List *l;
209    Ecore_Device *dev = NULL;
210    E_Device *e_dev = NULL;
211    const char *identifier;
212
213    if (!edev || !edev->path) return EINA_FALSE;
214
215    dev_list = ecore_device_list();
216    if (dev_list)
217      {
218         EINA_LIST_FOREACH(dev_list, l, dev)
219           {
220              if (!dev) continue;
221              identifier = ecore_device_identifier_get(dev);
222              if (!identifier) continue;
223              if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
224                {
225                   ERR("Found same device in device list");
226                   return EINA_FALSE;
227                }
228           }
229      }
230
231    // create ecore device info
232    dev = ecore_device_add();
233    if (!dev)
234      {
235         ERR("Failed to create ecore device");
236         edev->ecore_dev = NULL;
237         return EINA_FALSE;
238      }
239
240    ecore_device_name_set(dev, libinput_device_get_name(edev->device));
241    ecore_device_identifier_set(dev, edev->path);
242    ecore_device_class_set(dev, clas);
243    ecore_device_subclass_set(dev, ECORE_DEVICE_SUBCLASS_NONE);
244
245    if (!edev->ecore_dev)
246      {
247         if (!edev->ecore_dev_list || (eina_list_count(edev->ecore_dev_list) == 0))
248           {
249              /* 1st Ecore_Device is added */
250              edev->ecore_dev = ecore_device_ref(dev);
251           }
252         else
253           {
254              /* 3rd or more Ecore_Device is added */
255              edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
256           }
257      }
258    else
259      {
260         /* 2nd Ecore_Device is added */
261         edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, edev->ecore_dev);
262         edev->ecore_dev = NULL;
263
264         edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
265      }
266
267    const GList *device_list = e_device_list_get();
268    const gchar *device_identifier;
269    for (GList *list = g_list_first((GList *)device_list); list; list = list->next)
270      {
271         E_Device *device = (E_Device *)list->data;
272         if (!device) continue;
273         device_identifier = e_device_identifier_get(device);
274         if (!device_identifier) continue;
275
276         if ((e_device_class_get(device) == clas) && (!strcmp(device_identifier, edev->path)))
277           {
278              e_dev = device;
279              break;
280           }
281      }
282
283    if (!e_dev)
284      {
285         // create E_Device info
286         e_dev = e_device_new();
287         if (!e_dev)
288           {
289              ERR("Failed to add e device");
290              edev->e_dev = NULL;
291              return EINA_FALSE;
292           }
293      }
294
295    e_device_name_set(e_dev, libinput_device_get_name(edev->device));
296    e_device_identifier_set(e_dev, edev->path);
297    e_device_class_set(e_dev, clas);
298    e_device_subclass_set(e_dev, ECORE_DEVICE_SUBCLASS_NONE);
299
300    if (!edev->e_dev)
301      {
302         if (!edev->e_dev_list || (g_list_length(edev->e_dev_list) == 0))
303           {
304              /* 1st Ecore_Device is added */
305              edev->e_dev = g_object_ref(e_dev);
306           }
307         else
308           {
309              /* 3rd or more Ecore_Device is added */
310              edev->e_dev_list = g_list_append(edev->e_dev_list, g_object_ref(e_dev));
311           }
312      }
313    else
314      {
315         /* 2nd Ecore_Device is added */
316         edev->e_dev_list = g_list_append(edev->e_dev_list, edev->e_dev);
317         edev->e_dev = NULL;
318
319         edev->e_dev_list = g_list_append(edev->e_dev_list, g_object_ref(e_dev));
320      }
321
322    _e_input_ecore_device_event(dev, edev->seat ? edev->seat->name : NULL, EINA_TRUE);
323
324    INF("[Add Device] device name(%s), identifier(%s), class(%s)", e_device_name_get(e_dev), edev->path, _e_input_ecore_device_class_to_string(clas));
325
326    return EINA_TRUE;
327 }
328
329 static Eina_Bool
330 _e_input_remove_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
331 {
332    Eina_Bool ret = EINA_FALSE;
333    const Eina_List *dev_list = NULL, *l;
334    const GList *e_dev_list = NULL;
335    Eina_List *ll, *ll_next;
336    Ecore_Device *dev = NULL, *data;
337    const char *identifier;
338    const gchar *device_identifier;
339    const char *device_remove_log = NULL;
340
341    if (!edev->path) return EINA_FALSE;
342
343    dev_list = ecore_device_list();
344    if (!dev_list) return EINA_FALSE;
345
346    EINA_LIST_FOREACH(dev_list, l, dev)
347       {
348          if (!dev) continue;
349          identifier = ecore_device_identifier_get(dev);
350          if (!identifier) continue;
351          if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
352            {
353               if (edev->ecore_dev)
354                 {
355                    ecore_device_unref(dev);
356                    edev->ecore_dev = NULL;
357                 }
358               else if (edev->ecore_dev_list)
359                 {
360                    EINA_LIST_FOREACH_SAFE(edev->ecore_dev_list, ll, ll_next, data)
361                      {
362                         if (data == dev)
363                           {
364                              ecore_device_unref(dev);
365                              edev->ecore_dev_list = eina_list_remove_list(edev->ecore_dev_list, ll);
366                           }
367                      }
368                 }
369               _e_input_ecore_device_event(dev, edev->seat ? edev->seat->name : NULL, EINA_FALSE);
370               ecore_device_del(dev);
371               ret = EINA_TRUE;
372            }
373       }
374
375    e_dev_list = e_device_list_get();
376    if (!e_dev_list)
377      {
378         ERR("Failed to get e device list");
379         return EINA_FALSE;
380      }
381
382    for (GList *list = g_list_first((GList *)e_dev_list); list; list = list->next)
383      {
384         E_Device *device = (E_Device *)list->data;
385         if (!device) continue;
386         device_identifier = e_device_identifier_get(device);
387         if (!device_identifier) continue;
388
389         if ((e_device_class_get(device) == clas) && (!strcmp(device_identifier, edev->path)))
390           {
391              device_remove_log = eina_stringshare_printf("[Remove Device] device name(%s), identifier(%s), class(%s)",
392                                                          e_device_name_get(device),
393                                                          device_identifier,
394                                                          _e_input_ecore_device_class_to_string(clas));
395
396              if (edev->e_dev)
397                {
398                   g_object_unref(device);
399                   edev->e_dev = NULL;
400                }
401              else if (edev->e_dev_list)
402                {
403                   GList *del_list = g_list_find(edev->e_dev_list, device);
404                   if (del_list)
405                     {
406                        edev->e_dev_list = g_list_delete_link(edev->e_dev_list, del_list);
407                     }
408                }
409              ret = EINA_TRUE;
410           }
411      }
412
413    if (device_remove_log)
414      {
415         INF("%s", device_remove_log);
416         eina_stringshare_del(device_remove_log);
417      }
418
419    return ret;
420 }
421
422 Eina_Bool
423 _e_input_device_add(E_Input_Evdev *edev)
424 {
425    Eina_Bool ret = EINA_FALSE;
426    Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
427
428    if (edev->caps & E_INPUT_SEAT_POINTER)
429      {
430         if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
431           clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
432         ret = _e_input_add_ecore_device(edev, clas);
433      }
434    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
435      {
436         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
437         ret = _e_input_add_ecore_device(edev, clas);
438      }
439    if (edev->caps & E_INPUT_SEAT_TOUCH)
440      {
441         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
442         ret = _e_input_add_ecore_device(edev, clas);
443      }
444
445    return ret;
446 }
447
448 void
449 _e_input_device_remove(E_Input_Evdev *edev)
450 {
451    Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
452    Ecore_Device *data;
453
454    if (edev->caps & E_INPUT_SEAT_POINTER)
455      {
456         if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
457           clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
458         _e_input_remove_ecore_device(edev, clas);
459      }
460    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
461      {
462         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
463         _e_input_remove_ecore_device(edev, clas);
464      }
465    if (edev->caps & E_INPUT_SEAT_TOUCH)
466      {
467         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
468         _e_input_remove_ecore_device(edev, clas);
469      }
470
471    if (edev->ecore_dev_list)
472      {
473         if (eina_list_count(edev->ecore_dev_list) > 0)
474           {
475              EINA_LIST_FREE(edev->ecore_dev_list, data)
476                {
477                   WRN("Invalid device is left. name: %s, identifier: %s, clas: %s\n",
478                       ecore_device_name_get(data), ecore_device_identifier_get(data),
479                       _e_input_ecore_device_class_to_string(ecore_device_class_get(data)));
480
481                   ecore_device_unref(data);
482                   ecore_device_del(data);
483                }
484           }
485         edev->ecore_dev_list = NULL;
486      }
487 }
488
489 static void
490 _device_added(E_Input_Backend *input, struct libinput_device *device)
491 {
492    struct libinput_seat *libinput_seat;
493    const char *seat_name;
494    E_Input_Seat *seat;
495    E_Input_Evdev *edev;
496
497    ecore_thread_main_loop_begin();
498    libinput_seat = libinput_device_get_seat(device);
499    seat_name = libinput_seat_get_logical_name(libinput_seat);
500
501    /* try to get a seat */
502    if (!(seat = _seat_get(input, seat_name)))
503      {
504         ERR("Could not get matching seat: %s", seat_name);
505         ecore_thread_main_loop_end();
506         return;
507      }
508
509    /* try to create a new evdev device */
510    if (!(edev = _e_input_evdev_device_create(seat, device)))
511      {
512         ERR("Failed to create new evdev device");
513         ecore_thread_main_loop_end();
514         return;
515      }
516
517    /* append this device to the seat */
518    seat->devices = eina_list_append(seat->devices, edev);
519
520    if (EINA_FALSE == _e_input_device_add(edev))
521      {
522         ERR("Failed to create evas device !\n");
523         ecore_thread_main_loop_end();
524         return;
525      }
526
527    ecore_thread_main_loop_end();
528 }
529
530 static void
531 _device_removed(E_Input_Backend *input, struct libinput_device *device)
532 {
533    E_Input_Evdev *edev;
534
535    ecore_thread_main_loop_begin();
536
537    /* try to get the evdev structure */
538    if (!(edev = libinput_device_get_user_data(device)))
539      {
540         ecore_thread_main_loop_end();
541         return;
542      }
543
544    _e_input_device_remove(edev);
545
546    /* remove this evdev from the seat's list of devices */
547    edev->seat->devices = eina_list_remove(edev->seat->devices, edev);
548
549    /* destroy this evdev */
550    _e_input_evdev_device_destroy(edev);
551
552    ecore_thread_main_loop_end();
553 }
554
555 static int
556 _udev_event_process(struct libinput_event *event)
557 {
558    struct libinput *libinput;
559    struct libinput_device *device;
560    E_Input_Backend *input;
561    Eina_Bool ret = EINA_TRUE;
562
563    libinput = libinput_event_get_context(event);
564    input = libinput_get_user_data(libinput);
565    device = libinput_event_get_device(event);
566
567    switch (libinput_event_get_type(event))
568      {
569       case LIBINPUT_EVENT_DEVICE_ADDED:
570         if (e_config->key_input_ttrace_enable)
571           {
572              TRACE_INPUT_BEGIN(_device_added);
573              ELOGF("INPUT", "_device_added|B|", NULL);
574           }
575
576         _device_added(input, device);
577
578         if (e_config->key_input_ttrace_enable)
579           {
580              TRACE_INPUT_END();
581              ELOGF("INPUT", "_device_added|E|", NULL);
582           }
583         break;
584       case LIBINPUT_EVENT_DEVICE_REMOVED:
585         if (e_config->key_input_ttrace_enable)
586           {
587              TRACE_INPUT_BEGIN(_device_removed);
588              ELOGF("INPUT", "_device_removed|B|", NULL);
589           }
590
591         _device_removed(input, device);
592
593         if (e_config->key_input_ttrace_enable)
594           {
595              TRACE_INPUT_END();
596              ELOGF("INPUT", "_device_removed|E|", NULL);
597           }
598         break;
599       default:
600         ret = EINA_FALSE;
601      }
602
603    return ret;
604 }
605
606 static void
607 _input_event_process(struct libinput_event *event)
608 {
609    if (_udev_event_process(event)) return;
610    if (_e_input_evdev_event_process(event)) return;
611 }
612
613 void
614 _input_events_process(E_Input_Backend *input)
615 {
616    struct libinput_event *event;
617    while ((event = libinput_get_event(input->libinput)))
618      {
619         _input_event_process(event);
620         libinput_event_destroy(event);
621      }
622 }
623
624 static Eina_Bool
625 _cb_input_dispatch(void *data, Ecore_Fd_Handler *hdlr EINA_UNUSED)
626 {
627    E_Input_Backend *input;
628
629    if (!(input = data)) return EINA_TRUE;
630
631    if (e_config->key_input_ttrace_enable)
632      {
633         TRACE_INPUT_BEGIN(_cb_input_dispatch);
634         ELOGF("INPUT", "_cb_input_dispatch|B|", NULL);
635      }
636
637    if (libinput_dispatch(input->libinput) != 0)
638      ERR("Failed to dispatch libinput events: %m");
639
640    /* process pending events */
641    _input_events_process(input);
642
643    if (e_config->key_input_ttrace_enable)
644      {
645         TRACE_INPUT_END();
646         ELOGF("INPUT", "_cb_input_dispatch|E|", NULL);
647      }
648
649    return EINA_TRUE;
650 }
651
652 static void
653 _e_input_delayed_key_events_print()
654 {
655    struct timespec tp;
656    unsigned int time;
657
658    clock_gettime(CLOCK_MONOTONIC, &tp);
659    time = (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
660
661    GList *key_list = g_list_first(_key_event_list);
662    while (key_list)
663      {
664         Ecore_Event_Key *key = (Ecore_Event_Key *)key_list->data;
665         if (!key) continue;
666
667         if (e_config->key_input_time_limit <= (time - key->timestamp))
668           ERR("Delayed key event : keyname(%s), keycode(%u), timestamp(%u), elapsed_time(%u ms)", key->keyname, key->keycode, key->timestamp, time - key->timestamp);
669
670         if (key->keyname)
671           eina_stringshare_del(key->keyname);
672         E_FREE(key);
673
674         GList *next = key_list->next;
675         _key_event_list = g_list_delete_link(_key_event_list, key_list);
676         key_list = next;
677      }
678
679    _key_event_list = NULL;
680 }
681
682 static gboolean
683 input_thread_prepare(GSource *source, gint *time)
684 {
685    if (e_config->key_input_ttrace_enable)
686      {
687         TRACE_INPUT_BEGIN(e_comp_wl_focused_client_flush);
688         ELOGF("INPUT", "e_comp_wl_focused_client_flush|B|", NULL);
689      }
690
691    /* flush only focused client events */
692    e_comp_wl_focused_client_flush();
693
694    if (_key_event_list)
695      _e_input_delayed_key_events_print();
696
697    if (e_config->key_input_ttrace_enable)
698      {
699         TRACE_INPUT_END();
700         ELOGF("INPUT", "e_comp_wl_focused_client_flush|E|", NULL);
701      }
702
703    if (time)
704      *time = -1;
705
706    return FALSE;
707 }
708
709 static gboolean
710 input_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
711 {
712    InputEventSource *src = (InputEventSource *)source;
713    E_Input_Backend *input = (E_Input_Backend *)user_data;
714    if (!src) return G_SOURCE_REMOVE;
715    if (!input) return G_SOURCE_REMOVE;
716
717    if (e_config->key_input_ttrace_enable)
718      {
719         TRACE_INPUT_BEGIN(input_dispatch);
720         ELOGF("INPUT", "input_dispatch|B|", NULL);
721      }
722
723    GIOCondition cond;
724    cond = g_source_query_unix_fd(source, src->tag);
725
726    if (cond & G_IO_ERR || cond & G_IO_HUP || cond & G_IO_NVAL)
727      {
728         INF("error cond(%d)\n", cond);
729         return G_SOURCE_CONTINUE;
730      }
731
732    if (!input->libinput)
733      return G_SOURCE_REMOVE;
734
735    if (e_config->key_input_ttrace_enable)
736      {
737         TRACE_INPUT_BEGIN(libinput_dispatch);
738         ELOGF("INPUT", "libinput_dispatch|B|", NULL);
739      }
740
741    if (libinput_dispatch(input->libinput) != 0)
742      ERR("Failed to dispatch libinput events: %m");
743
744    if (e_config->key_input_ttrace_enable)
745      {
746         TRACE_INPUT_END();
747         ELOGF("INPUT", "libinput_dispatch|E|", NULL);
748      }
749
750    if (e_config->key_input_ttrace_enable)
751      {
752         TRACE_INPUT_BEGIN(_input_events_process);
753         ELOGF("INPUT", "_input_events_process|B|", NULL);
754      }
755
756    /* process pending events */
757    _input_events_process(input);
758
759    if (e_config->key_input_ttrace_enable)
760      {
761         TRACE_INPUT_END();
762         ELOGF("INPUT", "_input_events_process|E|", NULL);
763
764         TRACE_INPUT_END();
765         ELOGF("INPUT", "input_dispatch|E|", NULL);
766      }
767
768    e_input_event_process((GSource *)input->event_source);
769
770    return G_SOURCE_CONTINUE;
771 }
772
773 static void
774 input_thread_start(void *data, Ecore_Thread *th)
775 {
776    E_Input_Device *dev;
777    E_Input_Backend *input;
778    GMainContext *context = NULL;
779    InputEventSource *input_event_source = NULL;
780
781    if (!(input = data)) return;
782
783    eina_thread_name_set(eina_thread_self(), "input-thread");
784
785    e_input_libinput_context_create(input);
786
787    /* enable this input */
788    if (!e_input_enable_input(input))
789      {
790         ERR("Failed to enable input");
791         return;
792      }
793
794    /* append this input */
795    dev = input->dev;
796    dev->inputs = eina_list_append(dev->inputs, input);
797
798    //create a context
799    context = g_main_context_new();
800    g_main_context_push_thread_default(context);
801
802    input_event_source = (InputEventSource *)g_source_new(&input_event_funcs, sizeof(InputEventSource));
803    input_event_source->tag = g_source_add_unix_fd(&input_event_source->gsource, input->fd, G_IO_IN);
804
805    if (!input->event_source)
806      e_input_create_event_source(input);
807
808    //create main loop
809    input->input_thread_loop = g_main_loop_new(context, FALSE);
810
811    g_source_attach((GSource *)input->event_source, context);
812
813    //set the callback for this source
814    g_source_set_callback(&input_event_source->gsource, NULL, input, NULL);
815    g_source_attach(&input_event_source->gsource, context);
816
817    e_keyrouter_input_handler_add();
818
819    e_input_thread_id_set(gettid());
820    e_input_main_thread_id_set(getpid());
821
822    _e_input_hook_call(E_INPUT_HOOK_INPUT_THREAD_START, NULL);
823
824    INF("input thread start tid(%d) pid(%d)", e_input_thread_id_get(), e_input_main_thread_id_get());
825
826    g_main_loop_run(input->input_thread_loop);
827 }
828
829 static void
830 input_thread_feedback(void *data, Ecore_Thread *th, void *msgdata)
831 {
832    E_Input_Backend *input;
833
834    INF("input thread start");
835
836    if (!(input = data)) return;
837 }
838
839 static void
840 input_thread_end(void *data, Ecore_Thread *th)
841 {
842    E_Input_Backend *input;
843
844    if (!(input = data)) return;
845    INF("input thread complete");
846
847    e_input_event_source_destroy(input->event_source);
848    input->event_source = NULL;
849
850    g_main_loop_quit(input->input_thread_loop);
851    g_main_loop_unref(input->input_thread_loop);
852    input->input_thread_loop = NULL;
853
854    if (th == input->input_thread)
855      input->input_thread = NULL;
856 }
857
858 static void
859 input_thread_cancel(void *data, Ecore_Thread *th)
860 {
861    E_Input_Backend *input;
862
863    if (!(input = data)) return;
864
865    INF("input thread cancel");
866
867    if (th == input->input_thread)
868      input->input_thread = NULL;
869 }
870
871 void
872 _e_input_key_event_list_add(Ecore_Event_Key *key)
873 {
874    Ecore_Event_Key *clone = NULL;
875
876    if (!key) return;
877
878    clone = E_NEW(Ecore_Event_Key, 1);
879    if (!clone) return;
880
881    if (key->keyname)
882      clone->keyname = (char *)eina_stringshare_add(key->keyname);
883
884    clone->keycode = key->keycode;
885    clone->timestamp = key->timestamp;
886
887    _key_event_list = g_list_append(_key_event_list, clone);
888 }
889
890 EINTERN Eina_Bool
891 e_input_enable_input(E_Input_Backend *input)
892 {
893    EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
894    EINA_SAFETY_ON_NULL_RETURN_VAL(input->libinput, EINA_FALSE);
895
896    input->fd = libinput_get_fd(input->libinput);
897
898    if (!e_input_thread_mode_get())
899      {
900         if (!input->hdlr)
901           {
902              input->hdlr =
903                 ecore_main_fd_handler_add(input->fd, ECORE_FD_READ,
904                                           _cb_input_dispatch, input, NULL, NULL);
905           }
906      }
907
908    if (input->suspended)
909      {
910         if (libinput_resume(input->libinput) != 0)
911           goto err;
912
913         input->suspended = EINA_FALSE;
914
915         /* process pending events */
916         _input_events_process(input);
917      }
918
919    input->enabled = EINA_TRUE;
920    input->suspended = EINA_FALSE;
921
922    return EINA_TRUE;
923
924 err:
925    input->enabled = EINA_FALSE;
926    if (!e_input_thread_mode_get())
927      {
928         if (input->hdlr)
929           ecore_main_fd_handler_del(input->hdlr);
930         input->hdlr = NULL;
931      }
932
933    return EINA_FALSE;
934 }
935
936 EINTERN void
937 e_input_disable_input(E_Input_Backend *input)
938 {
939    EINA_SAFETY_ON_NULL_RETURN(input);
940    EINA_SAFETY_ON_TRUE_RETURN(input->suspended);
941
942    /* suspend this input */
943    libinput_suspend(input->libinput);
944
945    /* process pending events */
946    _input_events_process(input);
947
948    input->suspended = EINA_TRUE;
949
950    if (e_input_thread_mode_get())
951      {
952         if (input->input_thread && !ecore_thread_check(input->input_thread))
953           ecore_thread_cancel(input->input_thread);
954      }
955 }
956
957 EINTERN Eina_List *
958 e_input_seat_evdev_list_get(E_Input_Seat *seat)
959 {
960    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
961    return seat->devices;
962 }
963
964 E_API E_Input_Event_Source *
965 e_input_event_source_get()
966 {
967    return g_input_event_source;
968 }
969
970 E_API Eina_Bool
971 e_input_thread_mode_get()
972 {
973    if (e_config)
974      return e_config->input_thread_mode;
975    else
976      return EINA_TRUE;
977 }
978
979 EINTERN void
980 e_input_create_event_source(E_Input_Backend *input)
981 {
982    input->event_source = e_input_event_source_create();
983
984    g_input_event_source = input->event_source;
985 }
986
987 EINTERN void e_input_thread_run(E_Input_Backend *input)
988 {
989    input->input_thread = ecore_thread_feedback_run(input_thread_start, input_thread_feedback, input_thread_end, input_thread_cancel, input, EINA_FALSE);
990 }