e_input: unref udev_device after using it
[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    if (!udev_device)
448      {
449         ERR("no udev_device");
450         return subclas;
451      }
452
453    devpath = udev_device_get_property_value(udev_device, "DEVPATH");
454    if (devpath && strstr(devpath, "virtual/input"))
455      {
456         if (clas == ECORE_DEVICE_CLASS_MOUSE)
457           subclas = ECORE_DEVICE_SUBCLASS_VIRTUAL_MOUSE;
458         else if (clas == ECORE_DEVICE_CLASS_KEYBOARD)
459           subclas = ECORE_DEVICE_SUBCLASS_VIRTUAL_REMOCON;
460      }
461    udev_device_unref(udev_device);
462
463    return subclas;
464 }
465
466 Eina_Bool
467 _e_input_device_add(E_Input_Evdev *edev)
468 {
469    Eina_Bool ret = EINA_FALSE;
470    Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
471    Ecore_Device_Subclass subclas = ECORE_DEVICE_SUBCLASS_NONE;
472
473    if (edev->caps & E_INPUT_SEAT_POINTER)
474      {
475         if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
476           {
477              clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
478              subclas = _e_input_device_get_ecore_device_subclass(edev, clas);
479           }
480         ret = _e_input_add_ecore_device(edev, clas, subclas);
481      }
482    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
483      {
484         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
485         subclas = _e_input_device_get_ecore_device_subclass(edev, clas);
486         ret = _e_input_add_ecore_device(edev, clas, subclas);
487      }
488    if (edev->caps & E_INPUT_SEAT_TOUCH)
489      {
490         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
491         ret = _e_input_add_ecore_device(edev, clas, subclas);
492      }
493
494    return ret;
495 }
496
497 void
498 _e_input_device_remove(E_Input_Evdev *edev)
499 {
500    Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
501    Ecore_Device *data;
502
503    if (edev->caps & E_INPUT_SEAT_POINTER)
504      {
505         if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
506           clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
507         _e_input_remove_ecore_device(edev, clas);
508      }
509    if (edev->caps & E_INPUT_SEAT_KEYBOARD)
510      {
511         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
512         _e_input_remove_ecore_device(edev, clas);
513      }
514    if (edev->caps & E_INPUT_SEAT_TOUCH)
515      {
516         clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
517         _e_input_remove_ecore_device(edev, clas);
518      }
519
520    if (edev->ecore_dev_list)
521      {
522         if (eina_list_count(edev->ecore_dev_list) > 0)
523           {
524              EINA_LIST_FREE(edev->ecore_dev_list, data)
525                {
526                   WRN("Invalid device is left. name: %s, identifier: %s, clas: %s\n",
527                       ecore_device_name_get(data), ecore_device_identifier_get(data),
528                       _e_input_ecore_device_class_to_string(ecore_device_class_get(data)));
529
530                   ecore_device_unref(data);
531                   ecore_device_del(data);
532                }
533           }
534         edev->ecore_dev_list = NULL;
535      }
536 }
537
538 static void
539 _device_added(E_Input_Backend *input, struct libinput_device *device)
540 {
541    struct libinput_seat *libinput_seat;
542    const char *seat_name;
543    E_Input_Seat *seat;
544    E_Input_Evdev *edev;
545
546    ecore_thread_main_loop_begin();
547    libinput_seat = libinput_device_get_seat(device);
548    seat_name = libinput_seat_get_logical_name(libinput_seat);
549
550    /* try to get a seat */
551    if (!(seat = _seat_get(input, seat_name)))
552      {
553         ERR("Could not get matching seat: %s", seat_name);
554         ecore_thread_main_loop_end();
555         return;
556      }
557
558    /* try to create a new evdev device */
559    if (!(edev = _e_input_evdev_device_create(seat, device)))
560      {
561         ERR("Failed to create new evdev device");
562         ecore_thread_main_loop_end();
563         return;
564      }
565
566    /* append this device to the seat */
567    seat->devices = eina_list_append(seat->devices, edev);
568
569    if (EINA_FALSE == _e_input_device_add(edev))
570      {
571         ERR("Failed to create evas device !\n");
572         ecore_thread_main_loop_end();
573         return;
574      }
575
576    ecore_thread_main_loop_end();
577 }
578
579 static void
580 _device_removed(E_Input_Backend *input, struct libinput_device *device)
581 {
582    E_Input_Evdev *edev;
583
584    ecore_thread_main_loop_begin();
585
586    /* try to get the evdev structure */
587    if (!(edev = libinput_device_get_user_data(device)))
588      {
589         ecore_thread_main_loop_end();
590         return;
591      }
592
593    _e_input_device_remove(edev);
594
595    /* remove this evdev from the seat's list of devices */
596    edev->seat->devices = eina_list_remove(edev->seat->devices, edev);
597
598    /* destroy this evdev */
599    _e_input_evdev_device_destroy(edev);
600
601    ecore_thread_main_loop_end();
602 }
603
604 static int
605 _udev_event_process(struct libinput_event *event)
606 {
607    struct libinput *libinput;
608    struct libinput_device *device;
609    E_Input_Backend *input;
610    Eina_Bool ret = EINA_TRUE;
611
612    libinput = libinput_event_get_context(event);
613    input = libinput_get_user_data(libinput);
614    device = libinput_event_get_device(event);
615
616    switch (libinput_event_get_type(event))
617      {
618       case LIBINPUT_EVENT_DEVICE_ADDED:
619         if (e_config->key_input_ttrace_enable)
620           {
621              TRACE_INPUT_DEVICE_BEGIN(_device_added);
622              ELOGF("INPUT", "_device_added|B|", NULL);
623           }
624
625         _device_added(input, device);
626
627         if (e_config->key_input_ttrace_enable)
628           {
629              TRACE_INPUT_DEVICE_END();
630              ELOGF("INPUT", "_device_added|E|", NULL);
631           }
632         break;
633       case LIBINPUT_EVENT_DEVICE_REMOVED:
634         if (e_config->key_input_ttrace_enable)
635           {
636              TRACE_INPUT_DEVICE_BEGIN(_device_removed);
637              ELOGF("INPUT", "_device_removed|B|", NULL);
638           }
639
640         _device_removed(input, device);
641
642         if (e_config->key_input_ttrace_enable)
643           {
644              TRACE_INPUT_DEVICE_END();
645              ELOGF("INPUT", "_device_removed|E|", NULL);
646           }
647         break;
648       default:
649         ret = EINA_FALSE;
650      }
651
652    return ret;
653 }
654
655 static void
656 _input_event_process(struct libinput_event *event)
657 {
658    if (_udev_event_process(event)) return;
659    if (_e_input_evdev_event_process(event)) return;
660 }
661
662 void
663 _input_events_process(E_Input_Backend *input)
664 {
665    struct libinput_event *event;
666    while ((event = libinput_get_event(input->libinput)))
667      {
668         _input_event_process(event);
669         libinput_event_destroy(event);
670      }
671 }
672
673 static Eina_Bool
674 _cb_input_dispatch(void *data, Ecore_Fd_Handler *hdlr EINA_UNUSED)
675 {
676    E_Input_Backend *input;
677
678    if (!(input = data)) return EINA_TRUE;
679
680    if (e_config->key_input_ttrace_enable)
681      {
682         TRACE_INPUT_DEVICE_BEGIN(_cb_input_dispatch);
683         ELOGF("INPUT", "_cb_input_dispatch|B|", NULL);
684      }
685
686    if (libinput_dispatch(input->libinput) != 0)
687      ERR("Failed to dispatch libinput events: %m");
688
689    /* process pending events */
690    _input_events_process(input);
691
692    if (e_config->key_input_ttrace_enable)
693      {
694         TRACE_INPUT_DEVICE_END();
695         ELOGF("INPUT", "_cb_input_dispatch|E|", NULL);
696      }
697
698    return EINA_TRUE;
699 }
700
701 static void
702 _e_input_delayed_key_events_print()
703 {
704    struct timespec tp;
705    unsigned int time;
706
707    clock_gettime(CLOCK_MONOTONIC, &tp);
708    time = (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
709
710    GList *key_list = g_list_first(_key_event_list);
711    while (key_list)
712      {
713         Ecore_Event_Key *key = (Ecore_Event_Key *)key_list->data;
714         if (!key) continue;
715
716         if (e_config->key_input_time_limit <= (time - key->timestamp))
717           ERR("Delayed key event : keyname(%s), keycode(%u), timestamp(%u), elapsed_time(%u ms)", key->keyname, key->keycode, key->timestamp, time - key->timestamp);
718
719         if (key->keyname)
720           eina_stringshare_del(key->keyname);
721         E_FREE(key);
722
723         GList *next = key_list->next;
724         _key_event_list = g_list_delete_link(_key_event_list, key_list);
725         key_list = next;
726      }
727
728    _key_event_list = NULL;
729 }
730
731 static gboolean
732 input_thread_prepare(GSource *source, gint *time)
733 {
734    if (e_config->key_input_ttrace_enable)
735      {
736         TRACE_INPUT_DEVICE_BEGIN(e_comp_wl_focused_client_flush);
737         ELOGF("INPUT", "e_comp_wl_focused_client_flush|B|", NULL);
738      }
739
740    /* flush only focused client events */
741    e_comp_wl_focused_client_flush();
742
743    if (_key_event_list)
744      _e_input_delayed_key_events_print();
745
746    if (e_config->key_input_ttrace_enable)
747      {
748         TRACE_INPUT_DEVICE_END();
749         ELOGF("INPUT", "e_comp_wl_focused_client_flush|E|", NULL);
750      }
751
752    if (time)
753      *time = -1;
754
755    return FALSE;
756 }
757
758 static gboolean
759 input_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
760 {
761    InputEventSource *src = (InputEventSource *)source;
762    E_Input_Backend *input = (E_Input_Backend *)user_data;
763    if (!src) return G_SOURCE_REMOVE;
764    if (!input) return G_SOURCE_REMOVE;
765
766    if (e_config->key_input_ttrace_enable)
767      {
768         TRACE_INPUT_DEVICE_BEGIN(input_dispatch);
769         ELOGF("INPUT", "input_dispatch|B|", NULL);
770      }
771
772    GIOCondition cond;
773    cond = g_source_query_unix_fd(source, src->tag);
774
775    if (cond & G_IO_ERR || cond & G_IO_HUP || cond & G_IO_NVAL)
776      {
777         INF("error cond(%d)\n", cond);
778         return G_SOURCE_CONTINUE;
779      }
780
781    if (!input->libinput)
782      return G_SOURCE_REMOVE;
783
784    if (e_config->key_input_ttrace_enable)
785      {
786         TRACE_INPUT_DEVICE_BEGIN(libinput_dispatch);
787         ELOGF("INPUT", "libinput_dispatch|B|", NULL);
788      }
789
790    if (libinput_dispatch(input->libinput) != 0)
791      ERR("Failed to dispatch libinput events: %m");
792
793    if (e_config->key_input_ttrace_enable)
794      {
795         TRACE_INPUT_DEVICE_END();
796         ELOGF("INPUT", "libinput_dispatch|E|", NULL);
797      }
798
799    if (e_config->key_input_ttrace_enable)
800      {
801         TRACE_INPUT_DEVICE_BEGIN(_input_events_process);
802         ELOGF("INPUT", "_input_events_process|B|", NULL);
803      }
804
805    /* process pending events */
806    _input_events_process(input);
807
808    if (e_config->key_input_ttrace_enable)
809      {
810         TRACE_INPUT_DEVICE_END();
811         ELOGF("INPUT", "_input_events_process|E|", NULL);
812
813         TRACE_INPUT_DEVICE_END();
814         ELOGF("INPUT", "input_dispatch|E|", NULL);
815      }
816
817    e_input_event_process((GSource *)input->event_source);
818
819    return G_SOURCE_CONTINUE;
820 }
821
822 static gboolean
823 _pipe_recv_handler(GIOChannel *source, GIOCondition condition, gpointer user_data)
824 {
825    E_Input_Backend *input = user_data;
826    char *data;
827    int len;
828    E_Input_Thread_Request_Data order;
829
830    if (condition == G_IO_IN)
831      {
832         len = read(input->main_to_input_thread_pipe_fd[0], &order, sizeof(E_Input_Thread_Request_Data));
833         if (len <= 0)
834           return TRUE;
835
836         data = calloc(1, sizeof(char)*order.data_length);
837         len = read(input->main_to_input_thread_pipe_fd[0], data, order.data_length);
838         if (len <= 0)
839           {
840              free(data);
841              return TRUE;
842           }
843
844         if (order.safe_call_cb)
845           order.safe_call_cb(data);
846
847         free(data);
848      }
849    else if (condition == G_IO_ERR || condition == G_IO_HUP)
850      ERR("Error or hangup\n");
851
852    return TRUE;
853 }
854
855 static void
856 input_thread_start(void *data, Ecore_Thread *th)
857 {
858    E_Input_Device *dev;
859    E_Input_Backend *input;
860    GMainContext *context = NULL;
861    InputEventSource *input_event_source = NULL;
862    GIOChannel *_pipe_iochannel = NULL;
863    GSource *pipe_source = NULL;
864
865    if (!(input = data)) return;
866
867    eina_thread_name_set(eina_thread_self(), "input-thread");
868
869    e_input_libinput_context_create(input);
870
871    e_comp_wl_input_keymap_init();
872    e_keyrouter_query_tizen_key_table();
873
874    INF("write wm_ready");
875    if (e_config->create_wm_ready)
876      e_main_create_wm_ready();
877
878 #ifdef HAVE_SYSTEMD
879    TSM("[WM] Send start-up completion");
880    sd_notify(0, "READY=1");
881 #else
882    TSM("[WM] Skip sending start-up completion. (no systemd)");
883 #endif
884
885    /* enable this input */
886    if (!e_input_enable_input(input))
887      {
888         ERR("Failed to enable input");
889         return;
890      }
891
892    /* append this input */
893    dev = input->dev;
894    dev->inputs = eina_list_append(dev->inputs, input);
895
896    //create a context
897    context = g_main_context_new();
898    g_main_context_push_thread_default(context);
899
900    input_event_source = (InputEventSource *)g_source_new(&input_event_funcs, sizeof(InputEventSource));
901    input_event_source->tag = g_source_add_unix_fd(&input_event_source->gsource, input->fd, G_IO_IN);
902
903    if (!input->event_source)
904      e_input_create_event_source(input);
905
906    //create main loop
907    input->input_thread_loop = g_main_loop_new(context, FALSE);
908
909    g_source_attach((GSource *)input->event_source, context);
910
911    //set the callback for this source
912    g_source_set_callback(&input_event_source->gsource, NULL, input, NULL);
913    g_source_attach(&input_event_source->gsource, context);
914
915    _pipe_iochannel = g_io_channel_unix_new(input->main_to_input_thread_pipe_fd[0]);
916    pipe_source = g_io_create_watch(_pipe_iochannel, G_IO_IN);
917    g_source_set_callback(pipe_source, (GSourceFunc)_pipe_recv_handler, input, NULL);
918    g_source_attach(pipe_source, context);
919
920    e_keyrouter_input_handler_add();
921
922    e_input_thread_id_set(gettid());
923    e_input_main_thread_id_set(getpid());
924
925    _e_input_hook_call(E_INPUT_HOOK_INPUT_THREAD_START, NULL);
926
927    INF("input thread start tid(%d) pid(%d)", e_input_thread_id_get(), e_input_main_thread_id_get());
928
929    g_main_loop_run(input->input_thread_loop);
930 }
931
932 static void
933 input_thread_feedback(void *data, Ecore_Thread *th, void *msgdata)
934 {
935    E_Input_Backend *input;
936
937    INF("input thread start");
938
939    if (!(input = data)) return;
940 }
941
942 static void
943 input_thread_end(void *data, Ecore_Thread *th)
944 {
945    E_Input_Backend *input;
946
947    if (!(input = data)) return;
948    INF("input thread complete");
949
950    e_input_event_source_destroy(input->event_source);
951    input->event_source = NULL;
952
953    g_main_loop_quit(input->input_thread_loop);
954    g_main_loop_unref(input->input_thread_loop);
955    input->input_thread_loop = NULL;
956
957    if (th == input->input_thread)
958      input->input_thread = NULL;
959 }
960
961 static void
962 input_thread_cancel(void *data, Ecore_Thread *th)
963 {
964    E_Input_Backend *input;
965
966    if (!(input = data)) return;
967
968    INF("input thread cancel");
969
970    if (th == input->input_thread)
971      input->input_thread = NULL;
972 }
973
974 void
975 _e_input_key_event_list_add(Ecore_Event_Key *key)
976 {
977    Ecore_Event_Key *clone = NULL;
978
979    if (!key) return;
980
981    clone = E_NEW(Ecore_Event_Key, 1);
982    if (!clone) return;
983
984    if (key->keyname)
985      clone->keyname = (char *)eina_stringshare_add(key->keyname);
986
987    clone->keycode = key->keycode;
988    clone->timestamp = key->timestamp;
989
990    _key_event_list = g_list_append(_key_event_list, clone);
991 }
992
993 EINTERN Eina_Bool
994 e_input_enable_input(E_Input_Backend *input)
995 {
996    EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
997    EINA_SAFETY_ON_NULL_RETURN_VAL(input->libinput, EINA_FALSE);
998
999    input->fd = libinput_get_fd(input->libinput);
1000
1001    if (!e_input_thread_mode_get())
1002      {
1003         if (!input->hdlr)
1004           {
1005              input->hdlr =
1006                 ecore_main_fd_handler_add(input->fd, ECORE_FD_READ,
1007                                           _cb_input_dispatch, input, NULL, NULL);
1008           }
1009      }
1010
1011    if (input->suspended)
1012      {
1013         if (libinput_resume(input->libinput) != 0)
1014           goto err;
1015
1016         input->suspended = EINA_FALSE;
1017
1018         /* process pending events */
1019         _input_events_process(input);
1020      }
1021
1022    input->enabled = EINA_TRUE;
1023    input->suspended = EINA_FALSE;
1024
1025    return EINA_TRUE;
1026
1027 err:
1028    input->enabled = EINA_FALSE;
1029    if (!e_input_thread_mode_get())
1030      {
1031         if (input->hdlr)
1032           ecore_main_fd_handler_del(input->hdlr);
1033         input->hdlr = NULL;
1034      }
1035
1036    return EINA_FALSE;
1037 }
1038
1039 EINTERN void
1040 e_input_disable_input(E_Input_Backend *input)
1041 {
1042    EINA_SAFETY_ON_NULL_RETURN(input);
1043    EINA_SAFETY_ON_TRUE_RETURN(input->suspended);
1044
1045    /* suspend this input */
1046    libinput_suspend(input->libinput);
1047
1048    /* process pending events */
1049    _input_events_process(input);
1050
1051    input->suspended = EINA_TRUE;
1052
1053    if (e_input_thread_mode_get())
1054      {
1055         if (input->input_thread && !ecore_thread_check(input->input_thread))
1056           ecore_thread_cancel(input->input_thread);
1057      }
1058 }
1059
1060 EINTERN Eina_List *
1061 e_input_seat_evdev_list_get(E_Input_Seat *seat)
1062 {
1063    EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
1064    return seat->devices;
1065 }
1066
1067 E_API E_Input_Event_Source *
1068 e_input_event_source_get()
1069 {
1070    return g_input_event_source;
1071 }
1072
1073 E_API Eina_Bool
1074 e_input_thread_mode_get()
1075 {
1076    if (e_config)
1077      return e_config->input_thread_mode;
1078    else
1079      return EINA_TRUE;
1080 }
1081
1082 EINTERN void
1083 e_input_create_event_source(E_Input_Backend *input)
1084 {
1085    input->event_source = e_input_event_source_create();
1086
1087    g_input_event_source = input->event_source;
1088 }
1089
1090 EINTERN void e_input_thread_start()
1091 {
1092    EINA_SAFETY_ON_NULL_RETURN(g_input_backend);
1093
1094    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);
1095 }
1096
1097 EINTERN void e_input_thread_init()
1098 {
1099    if (pipe(main_to_input_thread_pipe_fd) != 0)
1100      ERR("error in pipe\n");
1101
1102    if (g_input_backend)
1103      {
1104         g_input_backend->main_to_input_thread_pipe_fd[0] = main_to_input_thread_pipe_fd[0];
1105         g_input_backend->main_to_input_thread_pipe_fd[1] = main_to_input_thread_pipe_fd[1];
1106      }
1107 }
1108
1109 EINTERN void e_input_thread_shutdown()
1110 {
1111    EINA_SAFETY_ON_NULL_RETURN(g_input_backend);
1112
1113    close(g_input_backend->main_to_input_thread_pipe_fd[0]);
1114    close(g_input_backend->main_to_input_thread_pipe_fd[1]);
1115 }
1116
1117 EINTERN void e_input_thread_input_backend_set(E_Input_Backend *input)
1118 {
1119    g_input_backend = input;
1120
1121    if (g_input_backend->main_to_input_thread_pipe_fd[0] == 0)
1122      {
1123         g_input_backend->main_to_input_thread_pipe_fd[0] = main_to_input_thread_pipe_fd[0];
1124         g_input_backend->main_to_input_thread_pipe_fd[1] = main_to_input_thread_pipe_fd[1];
1125      }
1126 }
1127
1128 EINTERN void
1129 e_input_thread_safe_call(E_Input_Thread_Safe_Call_Cb cb, void *data, size_t data_length)
1130 {
1131    E_Input_Thread_Request_Data order;
1132    order.safe_call_cb = cb;
1133    order.data_length = data_length;
1134
1135    if (!cb) return;
1136    if (!g_input_backend)
1137      {
1138         ERR("Not yet initialized\n");
1139         return;
1140      }
1141
1142    if (write(g_input_backend->main_to_input_thread_pipe_fd[1], &order, sizeof(E_Input_Thread_Request_Data)) == -1)
1143      ERR("error when writing header in pipe\n");
1144
1145    if (write(g_input_backend->main_to_input_thread_pipe_fd[1], data, data_length) == -1)
1146      ERR("error when writing data in pipe\n");
1147 }