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