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