2 #include "e_input_private.h"
5 _e_input_ecore_device_class_to_string(Ecore_Device_Class clas)
9 case ECORE_DEVICE_CLASS_NONE:
12 case ECORE_DEVICE_CLASS_SEAT:
15 case ECORE_DEVICE_CLASS_KEYBOARD:
18 case ECORE_DEVICE_CLASS_MOUSE:
21 case ECORE_DEVICE_CLASS_TOUCH:
24 case ECORE_DEVICE_CLASS_PEN:
27 case ECORE_DEVICE_CLASS_WAND:
30 case ECORE_DEVICE_CLASS_GAMEPAD:
38 static Ecore_Device_Class
39 _e_input_seat_cap_to_ecore_device_class(unsigned int cap)
43 case E_INPUT_SEAT_POINTER:
44 return ECORE_DEVICE_CLASS_MOUSE;
45 case E_INPUT_SEAT_KEYBOARD:
46 return ECORE_DEVICE_CLASS_KEYBOARD;
47 case E_INPUT_SEAT_TOUCH:
48 return ECORE_DEVICE_CLASS_TOUCH;
50 return ECORE_DEVICE_CLASS_NONE;
52 return ECORE_DEVICE_CLASS_NONE;
56 _e_input_ecore_device_info_free(void *data EINA_UNUSED, void *ev)
58 Ecore_Event_Device_Info *e;
61 eina_stringshare_del(e->name);
62 eina_stringshare_del(e->identifier);
63 eina_stringshare_del(e->seatname);
69 _e_input_ecore_device_event(Ecore_Device *dev, const char* seat_name, Eina_Bool flag)
71 Ecore_Event_Device_Info *e;
73 const char *name, *identifier;
75 if (!(name = ecore_device_name_get(dev))) return;
76 if (!(identifier = ecore_device_identifier_get(dev))) return;
78 if (!(e = calloc(1, sizeof(Ecore_Event_Device_Info)))) return;
80 e_input = e_input_get();
82 e->window = e_input?e_input->window:(Ecore_Window)0;
83 e->name = eina_stringshare_add(name);
84 e->identifier = eina_stringshare_add(identifier);
85 if (seat_name && strlen(seat_name))
86 e->seatname = eina_stringshare_add(seat_name);
88 e->seatname = eina_stringshare_add(name);
89 e->clas = ecore_device_class_get(dev);
90 e->subclas = ecore_device_subclass_get(dev);
93 ecore_event_add(ECORE_EVENT_DEVICE_ADD, e, _e_input_ecore_device_info_free, NULL);
95 ecore_event_add(ECORE_EVENT_DEVICE_DEL, e, _e_input_ecore_device_info_free, NULL);
99 _seat_create(E_Input_Backend *input, const char *seat)
102 Ecore_Device *ecore_dev = NULL;
104 /* create an evas device of a seat */
105 ecore_dev = ecore_device_add();
108 ERR("Failed to create an ecore device for a seat !\n");
112 ecore_device_name_set(ecore_dev, seat);
113 ecore_device_identifier_set(ecore_dev, "Enlightenment seat");
114 ecore_device_class_set(ecore_dev, ECORE_DEVICE_CLASS_SEAT);
115 ecore_device_subclass_set(ecore_dev, ECORE_DEVICE_SUBCLASS_NONE);
117 /* try to allocate space for new seat */
118 if (!(s = calloc(1, sizeof(E_Input_Seat))))
120 ecore_device_del(ecore_dev);
125 s->name = eina_stringshare_add(seat);
126 s->ecore_dev = ecore_dev;
128 /* add this new seat to list */
129 input->dev->seats = eina_list_append(input->dev->seats, s);
132 ecore_event_add(E_INPUT_EVENT_SEAT_ADD, NULL, NULL, NULL);
134 _e_input_ecore_device_event(ecore_dev, seat, EINA_TRUE);
139 static E_Input_Seat *
140 _seat_get(E_Input_Backend *input, const char *seat)
145 /* search for this name in existing seats */
146 EINA_LIST_FOREACH(input->dev->seats, l, s)
147 if (!strcmp(s->name, seat))
150 return _seat_create(input, seat);
154 _e_input_add_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
156 const Eina_List *dev_list = NULL;
158 Ecore_Device *dev = NULL;
159 const char *identifier;
161 if (!edev || !edev->path) return EINA_FALSE;
163 dev_list = ecore_device_list();
166 EINA_LIST_FOREACH(dev_list, l, dev)
169 identifier = ecore_device_identifier_get(dev);
170 if (!identifier) continue;
171 if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
176 dev = ecore_device_add();
179 edev->ecore_dev = NULL;
183 ecore_device_name_set(dev, libinput_device_get_name(edev->device));
184 ecore_device_identifier_set(dev, edev->path);
185 ecore_device_class_set(dev, clas);
186 ecore_device_subclass_set(dev, ECORE_DEVICE_SUBCLASS_NONE);
188 if (!edev->ecore_dev)
190 if (!edev->ecore_dev_list || (eina_list_count(edev->ecore_dev_list) == 0))
192 /* 1st Ecore_Device is added */
193 edev->ecore_dev = ecore_device_ref(dev);
197 /* 3rd or more Ecore_Device is added */
198 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
203 /* 2nd Ecore_Device is added */
204 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, edev->ecore_dev);
205 edev->ecore_dev = NULL;
207 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
210 _e_input_ecore_device_event(dev, edev->seat ? edev->seat->name : NULL, EINA_TRUE);
216 _e_input_remove_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
218 const Eina_List *dev_list = NULL, *l;
219 Eina_List *ll, *ll_next;
220 Ecore_Device *dev = NULL, *data;
221 const char *identifier;
223 if (!edev->path) return EINA_FALSE;
225 dev_list = ecore_device_list();
226 if (!dev_list) return EINA_FALSE;
227 EINA_LIST_FOREACH(dev_list, l, dev)
230 identifier = ecore_device_identifier_get(dev);
231 if (!identifier) continue;
232 if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
236 ecore_device_unref(dev);
237 edev->ecore_dev = NULL;
239 else if (edev->ecore_dev_list)
241 EINA_LIST_FOREACH_SAFE(edev->ecore_dev_list, ll, ll_next, data)
245 ecore_device_unref(dev);
246 edev->ecore_dev_list = eina_list_remove_list(edev->ecore_dev_list, ll);
250 _e_input_ecore_device_event(dev, edev->seat ? edev->seat->name : NULL, EINA_FALSE);
251 ecore_device_del(dev);
259 _e_input_device_add(E_Input_Evdev *edev)
261 Eina_Bool ret = EINA_FALSE;
262 Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
264 if (edev->caps & E_INPUT_SEAT_POINTER)
266 if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
267 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
268 ret = _e_input_add_ecore_device(edev, clas);
270 if (edev->caps & E_INPUT_SEAT_KEYBOARD)
272 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
273 ret = _e_input_add_ecore_device(edev, clas);
275 if (edev->caps & E_INPUT_SEAT_TOUCH)
277 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
278 ret = _e_input_add_ecore_device(edev, clas);
285 _e_input_device_remove(E_Input_Evdev *edev)
287 Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
290 if (edev->caps & E_INPUT_SEAT_POINTER)
292 if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
293 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
294 _e_input_remove_ecore_device(edev, clas);
296 if (edev->caps & E_INPUT_SEAT_KEYBOARD)
298 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
299 _e_input_remove_ecore_device(edev, clas);
301 if (edev->caps & E_INPUT_SEAT_TOUCH)
303 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
304 _e_input_remove_ecore_device(edev, clas);
307 if (edev->ecore_dev_list)
309 if (eina_list_count(edev->ecore_dev_list) > 0)
311 EINA_LIST_FREE(edev->ecore_dev_list, data)
313 WRN("Invalid device is left. name: %s, identifier: %s, clas: %s\n",
314 ecore_device_name_get(data), ecore_device_identifier_get(data),
315 _e_input_ecore_device_class_to_string(ecore_device_class_get(data)));
316 ecore_device_unref(data);
317 ecore_device_del(data);
320 edev->ecore_dev_list = NULL;
325 _device_added(E_Input_Backend *input, struct libinput_device *device)
327 struct libinput_seat *libinput_seat;
328 const char *seat_name;
332 libinput_seat = libinput_device_get_seat(device);
333 seat_name = libinput_seat_get_logical_name(libinput_seat);
335 /* try to get a seat */
336 if (!(seat = _seat_get(input, seat_name)))
338 ERR("Could not get matching seat: %s", seat_name);
342 /* try to create a new evdev device */
343 if (!(edev = _e_input_evdev_device_create(seat, device)))
345 ERR("Failed to create new evdev device");
349 /* append this device to the seat */
350 seat->devices = eina_list_append(seat->devices, edev);
352 if (EINA_FALSE == _e_input_device_add(edev))
354 ERR("Failed to create evas device !\n");
360 _device_removed(E_Input_Backend *input, struct libinput_device *device)
364 /* try to get the evdev structure */
365 if (!(edev = libinput_device_get_user_data(device)))
370 _e_input_device_remove(edev);
372 /* remove this evdev from the seat's list of devices */
373 edev->seat->devices = eina_list_remove(edev->seat->devices, edev);
375 /* destroy this evdev */
376 _e_input_evdev_device_destroy(edev);
380 _udev_event_process(struct libinput_event *event)
382 struct libinput *libinput;
383 struct libinput_device *device;
384 E_Input_Backend *input;
385 Eina_Bool ret = EINA_TRUE;
387 libinput = libinput_event_get_context(event);
388 input = libinput_get_user_data(libinput);
389 device = libinput_event_get_device(event);
391 switch (libinput_event_get_type(event))
393 case LIBINPUT_EVENT_DEVICE_ADDED:
394 _device_added(input, device);
396 case LIBINPUT_EVENT_DEVICE_REMOVED:
397 _device_removed(input, device);
407 _input_event_process(struct libinput_event *event)
409 if (_udev_event_process(event)) return;
410 if (_e_input_evdev_event_process(event)) return;
414 _input_events_process(E_Input_Backend *input)
416 struct libinput_event *event;
418 while ((event = libinput_get_event(input->libinput)))
420 _input_event_process(event);
421 libinput_event_destroy(event);
426 _cb_input_dispatch(void *data, Ecore_Fd_Handler *hdlr EINA_UNUSED)
428 E_Input_Backend *input;
430 if (!(input = data)) return EINA_TRUE;
432 if (libinput_dispatch(input->libinput) != 0)
433 ERR("Failed to dispatch libinput events: %m");
435 /* process pending events */
436 _input_events_process(input);
442 e_input_enable_input(E_Input_Backend *input)
444 EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
445 EINA_SAFETY_ON_NULL_RETURN_VAL(input->libinput, EINA_FALSE);
447 input->fd = libinput_get_fd(input->libinput);
452 ecore_main_fd_handler_add(input->fd, ECORE_FD_READ,
453 _cb_input_dispatch, input, NULL, NULL);
456 if (input->suspended)
458 if (libinput_resume(input->libinput) != 0)
461 input->suspended = EINA_FALSE;
463 /* process pending events */
464 _input_events_process(input);
467 input->enabled = EINA_TRUE;
468 input->suspended = EINA_FALSE;
473 input->enabled = EINA_FALSE;
475 ecore_main_fd_handler_del(input->hdlr);
482 e_input_disable_input(E_Input_Backend *input)
484 EINA_SAFETY_ON_NULL_RETURN(input);
485 EINA_SAFETY_ON_TRUE_RETURN(input->suspended);
487 /* suspend this input */
488 libinput_suspend(input->libinput);
490 /* process pending events */
491 _input_events_process(input);
493 input->suspended = EINA_TRUE;
497 e_input_seat_evdev_list_get(E_Input_Seat *seat)
499 EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
500 return seat->devices;