2 #include "e_input_private.h"
3 #include "e_input_event.h"
4 #include "e_keyrouter.h"
8 static gboolean input_dispatch(GSource *source, GSourceFunc callback, gpointer user_data);
9 static gboolean input_thread_prepare(GSource *source, gint *time);
11 static E_Input_Event_Source *g_input_event_source = NULL;
13 GSourceFuncs input_event_funcs = {
14 .prepare = input_thread_prepare,
16 .dispatch = input_dispatch,
27 _e_input_ecore_device_class_to_string(Ecore_Device_Class clas)
31 case ECORE_DEVICE_CLASS_NONE:
34 case ECORE_DEVICE_CLASS_SEAT:
37 case ECORE_DEVICE_CLASS_KEYBOARD:
40 case ECORE_DEVICE_CLASS_MOUSE:
43 case ECORE_DEVICE_CLASS_TOUCH:
46 case ECORE_DEVICE_CLASS_PEN:
49 case ECORE_DEVICE_CLASS_WAND:
52 case ECORE_DEVICE_CLASS_GAMEPAD:
60 static Ecore_Device_Class
61 _e_input_seat_cap_to_ecore_device_class(unsigned int cap)
65 case E_INPUT_SEAT_POINTER:
66 return ECORE_DEVICE_CLASS_MOUSE;
67 case E_INPUT_SEAT_KEYBOARD:
68 return ECORE_DEVICE_CLASS_KEYBOARD;
69 case E_INPUT_SEAT_TOUCH:
70 return ECORE_DEVICE_CLASS_TOUCH;
72 return ECORE_DEVICE_CLASS_NONE;
74 return ECORE_DEVICE_CLASS_NONE;
78 _e_input_ecore_device_info_free(void *data EINA_UNUSED, void *ev)
80 Ecore_Event_Device_Info *e;
83 eina_stringshare_del(e->name);
84 eina_stringshare_del(e->identifier);
85 eina_stringshare_del(e->seatname);
91 _e_input_ecore_device_event(Ecore_Device *dev, const char* seat_name, Eina_Bool flag)
93 Ecore_Event_Device_Info *e;
95 const char *name, *identifier;
97 ecore_thread_main_loop_begin();
99 if (!(name = ecore_device_name_get(dev))) goto end;
100 if (!(identifier = ecore_device_identifier_get(dev))) goto end;
102 if (!(e = calloc(1, sizeof(Ecore_Event_Device_Info)))) goto end;
104 e_input = e_input_get();
106 e->window = e_input?e_input->window:(Ecore_Window)0;
107 e->name = eina_stringshare_add(name);
108 e->identifier = eina_stringshare_add(identifier);
109 if (seat_name && strlen(seat_name))
110 e->seatname = eina_stringshare_add(seat_name);
112 e->seatname = eina_stringshare_add(name);
113 e->clas = ecore_device_class_get(dev);
114 e->subclas = ecore_device_subclass_get(dev);
117 ecore_event_add(ECORE_EVENT_DEVICE_ADD, e, _e_input_ecore_device_info_free, NULL);
119 ecore_event_add(ECORE_EVENT_DEVICE_DEL, e, _e_input_ecore_device_info_free, NULL);
122 ecore_thread_main_loop_end();
125 static E_Input_Seat *
126 _seat_create(E_Input_Backend *input, const char *seat)
129 Ecore_Device *ecore_dev = NULL;
130 E_Device *e_dev = NULL;
132 ecore_thread_main_loop_begin();
134 /* create an evas device of a seat */
135 ecore_dev = ecore_device_add();
138 ERR("Failed to create an ecore device for a seat !\n");
139 ecore_thread_main_loop_end();
143 ecore_device_name_set(ecore_dev, seat);
144 ecore_device_identifier_set(ecore_dev, "Enlightenment seat");
145 ecore_device_class_set(ecore_dev, ECORE_DEVICE_CLASS_SEAT);
146 ecore_device_subclass_set(ecore_dev, ECORE_DEVICE_SUBCLASS_NONE);
148 /* create an e device of a seat */
149 e_dev = e_device_new();
152 ERR("Failed to create an ecore device for a seat !\n");
153 ecore_thread_main_loop_end();
157 e_device_name_set(e_dev, seat);
158 e_device_identifier_set(e_dev, "Enlightenment seat");
159 e_device_class_set(e_dev, ECORE_DEVICE_CLASS_SEAT);
160 e_device_subclass_set(e_dev, ECORE_DEVICE_SUBCLASS_NONE);
162 /* try to allocate space for new seat */
163 if (!(s = calloc(1, sizeof(E_Input_Seat))))
165 ecore_device_del(ecore_dev);
166 ecore_thread_main_loop_end();
171 s->name = eina_stringshare_add(seat);
172 s->ecore_dev = ecore_dev;
175 /* add this new seat to list */
176 input->dev->seats = eina_list_append(input->dev->seats, s);
179 ecore_event_add(E_INPUT_EVENT_SEAT_ADD, NULL, NULL, NULL);
180 ecore_thread_main_loop_end();
182 _e_input_ecore_device_event(ecore_dev, seat, EINA_TRUE);
187 static E_Input_Seat *
188 _seat_get(E_Input_Backend *input, const char *seat)
193 /* search for this name in existing seats */
194 EINA_LIST_FOREACH(input->dev->seats, l, s)
195 if (!strcmp(s->name, seat))
198 return _seat_create(input, seat);
202 _e_input_add_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
204 const Eina_List *dev_list = NULL;
206 Ecore_Device *dev = NULL;
207 E_Device *e_dev = NULL;
208 const char *identifier;
210 if (!edev || !edev->path) return EINA_FALSE;
212 dev_list = ecore_device_list();
215 EINA_LIST_FOREACH(dev_list, l, dev)
218 identifier = ecore_device_identifier_get(dev);
219 if (!identifier) continue;
220 if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
222 ERR("Found same device in device list");
228 // create ecore device info
229 dev = ecore_device_add();
232 ERR("Failed to create ecore device");
233 edev->ecore_dev = NULL;
237 ecore_device_name_set(dev, libinput_device_get_name(edev->device));
238 ecore_device_identifier_set(dev, edev->path);
239 ecore_device_class_set(dev, clas);
240 ecore_device_subclass_set(dev, ECORE_DEVICE_SUBCLASS_NONE);
242 if (!edev->ecore_dev)
244 if (!edev->ecore_dev_list || (eina_list_count(edev->ecore_dev_list) == 0))
246 /* 1st Ecore_Device is added */
247 edev->ecore_dev = ecore_device_ref(dev);
251 /* 3rd or more Ecore_Device is added */
252 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
257 /* 2nd Ecore_Device is added */
258 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, edev->ecore_dev);
259 edev->ecore_dev = NULL;
261 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
264 const GList *device_list = e_device_list_get();
265 const gchar *device_identifier;
266 for (GList *list = g_list_first((GList *)device_list); list; list = list->next)
268 E_Device *device = (E_Device *)list->data;
269 if (!device) continue;
270 device_identifier = e_device_identifier_get(device);
271 if (!device_identifier) continue;
273 if ((e_device_class_get(device) == clas) && (!strcmp(device_identifier, edev->path)))
282 // create E_Device info
283 e_dev = e_device_new();
286 ERR("Failed to add e device");
292 e_device_name_set(e_dev, libinput_device_get_name(edev->device));
293 e_device_identifier_set(e_dev, edev->path);
294 e_device_class_set(e_dev, clas);
295 e_device_subclass_set(e_dev, ECORE_DEVICE_SUBCLASS_NONE);
299 if (!edev->e_dev_list || (g_list_length(edev->e_dev_list) == 0))
301 /* 1st Ecore_Device is added */
302 edev->e_dev = g_object_ref(e_dev);
306 /* 3rd or more Ecore_Device is added */
307 edev->e_dev_list = g_list_append(edev->e_dev_list, g_object_ref(e_dev));
312 /* 2nd Ecore_Device is added */
313 edev->e_dev_list = g_list_append(edev->e_dev_list, edev->e_dev);
316 edev->e_dev_list = g_list_append(edev->e_dev_list, g_object_ref(e_dev));
319 _e_input_ecore_device_event(dev, edev->seat ? edev->seat->name : NULL, EINA_TRUE);
325 _e_input_remove_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
327 const Eina_List *dev_list = NULL, *l;
328 Eina_List *ll, *ll_next;
329 Ecore_Device *dev = NULL, *data;
330 const char *identifier;
332 if (!edev->path) return EINA_FALSE;
334 dev_list = ecore_device_list();
335 if (!dev_list) return EINA_FALSE;
337 EINA_LIST_FOREACH(dev_list, l, dev)
340 identifier = ecore_device_identifier_get(dev);
341 if (!identifier) continue;
342 if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
346 ecore_device_unref(dev);
347 edev->ecore_dev = NULL;
349 else if (edev->ecore_dev_list)
351 EINA_LIST_FOREACH_SAFE(edev->ecore_dev_list, ll, ll_next, data)
355 ecore_device_unref(dev);
356 edev->ecore_dev_list = eina_list_remove_list(edev->ecore_dev_list, ll);
360 _e_input_ecore_device_event(dev, edev->seat ? edev->seat->name : NULL, EINA_FALSE);
361 ecore_device_del(dev);
370 _e_input_device_add(E_Input_Evdev *edev)
372 Eina_Bool ret = EINA_FALSE;
373 Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
375 if (edev->caps & E_INPUT_SEAT_POINTER)
377 if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
378 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
379 ret = _e_input_add_ecore_device(edev, clas);
381 if (edev->caps & E_INPUT_SEAT_KEYBOARD)
383 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
384 ret = _e_input_add_ecore_device(edev, clas);
386 if (edev->caps & E_INPUT_SEAT_TOUCH)
388 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
389 ret = _e_input_add_ecore_device(edev, clas);
396 _e_input_device_remove(E_Input_Evdev *edev)
398 Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
401 if (edev->caps & E_INPUT_SEAT_POINTER)
403 if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
404 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
405 _e_input_remove_ecore_device(edev, clas);
407 if (edev->caps & E_INPUT_SEAT_KEYBOARD)
409 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
410 _e_input_remove_ecore_device(edev, clas);
412 if (edev->caps & E_INPUT_SEAT_TOUCH)
414 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
415 _e_input_remove_ecore_device(edev, clas);
418 if (edev->ecore_dev_list)
420 if (eina_list_count(edev->ecore_dev_list) > 0)
422 EINA_LIST_FREE(edev->ecore_dev_list, data)
424 WRN("Invalid device is left. name: %s, identifier: %s, clas: %s\n",
425 ecore_device_name_get(data), ecore_device_identifier_get(data),
426 _e_input_ecore_device_class_to_string(ecore_device_class_get(data)));
428 ecore_device_unref(data);
429 ecore_device_del(data);
432 edev->ecore_dev_list = NULL;
437 _device_added(E_Input_Backend *input, struct libinput_device *device)
439 struct libinput_seat *libinput_seat;
440 const char *seat_name;
444 ecore_thread_main_loop_begin();
445 libinput_seat = libinput_device_get_seat(device);
446 seat_name = libinput_seat_get_logical_name(libinput_seat);
448 /* try to get a seat */
449 if (!(seat = _seat_get(input, seat_name)))
451 ERR("Could not get matching seat: %s", seat_name);
452 ecore_thread_main_loop_end();
456 /* try to create a new evdev device */
457 if (!(edev = _e_input_evdev_device_create(seat, device)))
459 ERR("Failed to create new evdev device");
460 ecore_thread_main_loop_end();
464 /* append this device to the seat */
465 seat->devices = eina_list_append(seat->devices, edev);
467 if (EINA_FALSE == _e_input_device_add(edev))
469 ERR("Failed to create evas device !\n");
470 ecore_thread_main_loop_end();
474 ecore_thread_main_loop_end();
478 _device_removed(E_Input_Backend *input, struct libinput_device *device)
482 ecore_thread_main_loop_begin();
484 /* try to get the evdev structure */
485 if (!(edev = libinput_device_get_user_data(device)))
487 ecore_thread_main_loop_end();
491 _e_input_device_remove(edev);
493 /* remove this evdev from the seat's list of devices */
494 edev->seat->devices = eina_list_remove(edev->seat->devices, edev);
496 /* destroy this evdev */
497 _e_input_evdev_device_destroy(edev);
499 ecore_thread_main_loop_end();
503 _udev_event_process(struct libinput_event *event)
505 struct libinput *libinput;
506 struct libinput_device *device;
507 E_Input_Backend *input;
508 Eina_Bool ret = EINA_TRUE;
510 libinput = libinput_event_get_context(event);
511 input = libinput_get_user_data(libinput);
512 device = libinput_event_get_device(event);
514 switch (libinput_event_get_type(event))
516 case LIBINPUT_EVENT_DEVICE_ADDED:
517 _device_added(input, device);
519 case LIBINPUT_EVENT_DEVICE_REMOVED:
520 _device_removed(input, device);
530 _input_event_process(struct libinput_event *event)
532 if (_udev_event_process(event)) return;
533 if (_e_input_evdev_event_process(event)) return;
537 _input_events_process(E_Input_Backend *input)
539 struct libinput_event *event;
540 E_Input *e_input = e_input_get();
543 g_rec_mutex_lock(&e_input->libinput_mutex);
545 while ((event = libinput_get_event(input->libinput)))
547 _input_event_process(event);
548 libinput_event_destroy(event);
552 g_rec_mutex_unlock(&e_input->libinput_mutex);
556 _cb_input_dispatch(void *data, Ecore_Fd_Handler *hdlr EINA_UNUSED)
558 E_Input_Backend *input;
560 if (!(input = data)) return EINA_TRUE;
562 if (libinput_dispatch(input->libinput) != 0)
563 ERR("Failed to dispatch libinput events: %m");
565 /* process pending events */
566 _input_events_process(input);
572 input_thread_prepare(GSource *source, gint *time)
574 /* flush only focused client events */
575 e_comp_wl_focused_client_flush();
584 input_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
586 InputEventSource *src = (InputEventSource *)source;
587 E_Input_Backend *input = (E_Input_Backend *)user_data;
588 if (!src) return G_SOURCE_REMOVE;
589 if (!input) return G_SOURCE_REMOVE;
591 E_Input *e_input = e_input_get();
594 cond = g_source_query_unix_fd(source, src->tag);
596 if (cond & G_IO_ERR || cond & G_IO_HUP || cond & G_IO_NVAL)
598 INF("error cond(%d)\n", cond);
599 return G_SOURCE_CONTINUE;
602 if (!input->libinput)
603 return G_SOURCE_REMOVE;
606 g_rec_mutex_lock(&e_input->libinput_mutex);
608 if (libinput_dispatch(input->libinput) != 0)
609 ERR("Failed to dispatch libinput events: %m");
612 g_rec_mutex_unlock(&e_input->libinput_mutex);
614 /* process pending events */
615 _input_events_process(input);
617 e_input_event_process((GSource *)input->event_source);
619 return G_SOURCE_CONTINUE;
623 input_thread_start(void *data, Ecore_Thread *th)
625 E_Input_Backend *input;
626 GMainContext *context = NULL;
627 InputEventSource *input_event_source = NULL;
629 INF("input thread start");
631 if (!(input = data)) return;
633 eina_thread_name_set(eina_thread_self(), "input-thread");
636 context = g_main_context_new();
637 g_main_context_push_thread_default(context);
639 input_event_source = (InputEventSource *)g_source_new(&input_event_funcs, sizeof(InputEventSource));
640 input_event_source->tag = g_source_add_unix_fd(&input_event_source->gsource, input->fd, G_IO_IN);
642 if (!input->event_source)
643 e_input_create_event_source(input);
646 input->input_thread_loop = g_main_loop_new(context, FALSE);
648 g_source_attach((GSource *)input->event_source, context);
650 //set the callback for this source
651 g_source_set_callback(&input_event_source->gsource, NULL, input, NULL);
652 g_source_attach(&input_event_source->gsource, context);
654 input->enabled = EINA_TRUE;
655 input->suspended = EINA_FALSE;
657 e_keyrouter_input_handler_add();
659 g_main_loop_run(input->input_thread_loop);
663 input_thread_feedback(void *data, Ecore_Thread *th, void *msgdata)
665 E_Input_Backend *input;
667 INF("input thread start");
669 if (!(input = data)) return;
673 input_thread_end(void *data, Ecore_Thread *th)
675 E_Input_Backend *input;
677 if (!(input = data)) return;
678 INF("input thread complete");
680 e_input_event_source_destroy(input->event_source);
681 input->event_source = NULL;
683 g_main_loop_quit(input->input_thread_loop);
684 g_main_loop_unref(input->input_thread_loop);
685 input->input_thread_loop = NULL;
687 if (th == input->input_thread)
688 input->input_thread = NULL;
692 input_thread_cancel(void *data, Ecore_Thread *th)
694 E_Input_Backend *input;
696 if (!(input = data)) return;
698 INF("input thread cancel");
700 if (th == input->input_thread)
701 input->input_thread = NULL;
705 e_input_enable_input(E_Input_Backend *input)
707 EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
708 EINA_SAFETY_ON_NULL_RETURN_VAL(input->libinput, EINA_FALSE);
710 input->fd = libinput_get_fd(input->libinput);
712 if (!e_input_thread_mode_get())
717 ecore_main_fd_handler_add(input->fd, ECORE_FD_READ,
718 _cb_input_dispatch, input, NULL, NULL);
721 if (input->suspended)
723 if (libinput_resume(input->libinput) != 0)
726 input->suspended = EINA_FALSE;
728 /* process pending events */
729 _input_events_process(input);
732 input->enabled = EINA_TRUE;
733 input->suspended = EINA_FALSE;
737 input->input_thread = ecore_thread_feedback_run(input_thread_start, input_thread_feedback, input_thread_end, input_thread_cancel, input, EINA_FALSE);
743 input->enabled = EINA_FALSE;
744 if (!e_input_thread_mode_get())
747 ecore_main_fd_handler_del(input->hdlr);
755 e_input_disable_input(E_Input_Backend *input)
757 EINA_SAFETY_ON_NULL_RETURN(input);
758 EINA_SAFETY_ON_TRUE_RETURN(input->suspended);
760 /* suspend this input */
761 libinput_suspend(input->libinput);
763 /* process pending events */
764 _input_events_process(input);
766 input->suspended = EINA_TRUE;
768 if (e_input_thread_mode_get())
770 if (input->input_thread && !ecore_thread_check(input->input_thread))
771 ecore_thread_cancel(input->input_thread);
776 e_input_seat_evdev_list_get(E_Input_Seat *seat)
778 EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
779 return seat->devices;
782 E_API E_Input_Event_Source *
783 e_input_event_source_get()
785 return g_input_event_source;
789 e_input_thread_mode_get()
792 return e_config->input_thread_mode;
798 e_input_create_event_source(E_Input_Backend *input)
800 input->event_source = e_input_event_source_create();
802 g_input_event_source = input->event_source;