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, Eina_Bool flag)
71 Ecore_Event_Device_Info *e;
74 if (!(e = calloc(1, sizeof(Ecore_Event_Device_Info)))) return;
76 e_input = e_input_get();
78 e->window = e_input?e_input->window:(Ecore_Window)0;
79 e->name = eina_stringshare_add(ecore_device_name_get(dev));
80 e->identifier = eina_stringshare_add(ecore_device_identifier_get(dev));
81 e->seatname = eina_stringshare_add(ecore_device_name_get(dev));
82 e->clas = ecore_device_class_get(dev);
83 e->subclas = ecore_device_subclass_get(dev);
86 ecore_event_add(ECORE_EVENT_DEVICE_ADD, e, _e_input_ecore_device_info_free, NULL);
88 ecore_event_add(ECORE_EVENT_DEVICE_DEL, e, _e_input_ecore_device_info_free, NULL);
92 _seat_create(E_Input_Backend *input, const char *seat)
95 Ecore_Device *ecore_dev = NULL;
97 /* create an evas device of a seat */
98 ecore_dev = ecore_device_add();
101 ERR("Failed to create an ecore device for a seat !\n");
105 ecore_device_name_set(ecore_dev, seat);
106 ecore_device_identifier_set(ecore_dev, "Enlightenment seat");
107 ecore_device_class_set(ecore_dev, ECORE_DEVICE_CLASS_SEAT);
108 ecore_device_subclass_set(ecore_dev, ECORE_DEVICE_SUBCLASS_NONE);
110 /* try to allocate space for new seat */
111 if (!(s = calloc(1, sizeof(E_Input_Seat))))
113 ecore_device_del(ecore_dev);
118 s->name = eina_stringshare_add(seat);
119 s->ecore_dev = ecore_dev;
121 /* add this new seat to list */
122 input->dev->seats = eina_list_append(input->dev->seats, s);
124 ecore_event_add(E_INPUT_EVENT_SEAT_ADD, NULL, NULL, NULL);
126 _e_input_ecore_device_event(ecore_dev, EINA_TRUE);
131 static E_Input_Seat *
132 _seat_get(E_Input_Backend *input, const char *seat)
137 /* search for this name in existing seats */
138 EINA_LIST_FOREACH(input->dev->seats, l, s)
139 if (!strcmp(s->name, seat))
142 return _seat_create(input, seat);
146 _e_input_add_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
148 const Eina_List *dev_list = NULL;
150 Ecore_Device *dev = NULL;
151 const char *identifier;
153 if (!edev || !edev->path) return EINA_FALSE;
155 dev_list = ecore_device_list();
158 EINA_LIST_FOREACH(dev_list, l, dev)
161 identifier = ecore_device_description_get(dev);
162 if (!identifier) continue;
163 if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
168 dev = ecore_device_add();
171 edev->ecore_dev = NULL;
175 ecore_device_name_set(dev, libinput_device_get_name(edev->device));
176 ecore_device_identifier_set(dev, edev->path);
177 ecore_device_class_set(dev, clas);
178 ecore_device_subclass_set(dev, ECORE_DEVICE_SUBCLASS_NONE);
180 if (!edev->ecore_dev)
182 if (!edev->ecore_dev_list || (eina_list_count(edev->ecore_dev_list) == 0))
184 /* 1st Ecore_Device is added */
185 edev->ecore_dev = ecore_device_ref(dev);
189 /* 3rd or more Ecore_Device is added */
190 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
195 /* 2nd Ecore_Device is added */
196 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, edev->ecore_dev);
197 edev->ecore_dev = NULL;
199 edev->ecore_dev_list = eina_list_append(edev->ecore_dev_list, ecore_device_ref(dev));
202 _e_input_ecore_device_event(dev, EINA_TRUE);
208 _e_input_remove_ecore_device(E_Input_Evdev *edev, Ecore_Device_Class clas)
210 const Eina_List *dev_list = NULL, *l;
211 Eina_List *ll, *ll_next;
212 Ecore_Device *dev = NULL, *data;
213 const char *identifier;
215 if (!edev->path) return EINA_FALSE;
217 dev_list = ecore_device_list();
218 if (!dev_list) return EINA_FALSE;
219 EINA_LIST_FOREACH(dev_list, l, dev)
222 identifier = ecore_device_description_get(dev);
223 if (!identifier) continue;
224 if ((ecore_device_class_get(dev) == clas) && (!strcmp(identifier, edev->path)))
226 if (edev->ecore_dev) edev->ecore_dev = NULL;
227 else if (edev->ecore_dev_list)
229 EINA_LIST_FOREACH_SAFE(edev->ecore_dev_list, ll, ll_next, data)
233 edev->ecore_dev_list = eina_list_remove_list(edev->ecore_dev_list, ll);
237 ecore_device_del(dev);
238 _e_input_ecore_device_event(dev, EINA_FALSE);
246 _e_input_device_add(E_Input_Evdev *edev)
248 Eina_Bool ret = EINA_FALSE;
249 Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
251 if (edev->caps & E_INPUT_SEAT_POINTER)
253 if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
254 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
255 ret = _e_input_add_ecore_device(edev, clas);
257 if (edev->caps & E_INPUT_SEAT_KEYBOARD)
259 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
260 ret = _e_input_add_ecore_device(edev, clas);
262 if (edev->caps & E_INPUT_SEAT_TOUCH)
264 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
265 ret = _e_input_add_ecore_device(edev, clas);
272 _e_input_device_remove(E_Input_Evdev *edev)
274 Ecore_Device_Class clas = ECORE_DEVICE_CLASS_NONE;
277 if (edev->caps & E_INPUT_SEAT_POINTER)
279 if (!e_devicemgr_detent_is_detent(libinput_device_get_name(edev->device)))
280 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_POINTER);
281 _e_input_remove_ecore_device(edev, clas);
283 if (edev->caps & E_INPUT_SEAT_KEYBOARD)
285 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_KEYBOARD);
286 _e_input_remove_ecore_device(edev, clas);
288 if (edev->caps & E_INPUT_SEAT_TOUCH)
290 clas = _e_input_seat_cap_to_ecore_device_class(E_INPUT_SEAT_TOUCH);
291 _e_input_remove_ecore_device(edev, clas);
294 if (edev->ecore_dev_list)
296 if (eina_list_count(edev->ecore_dev_list) > 0)
298 EINA_LIST_FREE(edev->ecore_dev_list, data)
300 WRN("Invalid device is left. name: %s, identifier: %s, clas: %s\n",
301 ecore_device_name_get(data), ecore_device_description_get(data),
302 _e_input_ecore_device_class_to_string(ecore_device_class_get(data)));
303 ecore_device_del(data);
306 edev->ecore_dev_list = NULL;
311 _device_added(E_Input_Backend *input, struct libinput_device *device)
313 struct libinput_seat *libinput_seat;
314 const char *seat_name;
318 libinput_seat = libinput_device_get_seat(device);
319 seat_name = libinput_seat_get_logical_name(libinput_seat);
321 /* try to get a seat */
322 if (!(seat = _seat_get(input, seat_name)))
324 ERR("Could not get matching seat: %s", seat_name);
328 /* try to create a new evdev device */
329 if (!(edev = _e_input_evdev_device_create(seat, device)))
331 ERR("Failed to create new evdev device");
335 /* append this device to the seat */
336 seat->devices = eina_list_append(seat->devices, edev);
338 if (EINA_FALSE == _e_input_device_add(edev))
340 ERR("Failed to create evas device !\n");
346 _device_removed(E_Input_Backend *input, struct libinput_device *device)
350 /* try to get the evdev structure */
351 if (!(edev = libinput_device_get_user_data(device)))
356 _e_input_device_remove(edev);
358 /* remove this evdev from the seat's list of devices */
359 edev->seat->devices = eina_list_remove(edev->seat->devices, edev);
361 /* destroy this evdev */
362 _e_input_evdev_device_destroy(edev);
366 _udev_event_process(struct libinput_event *event)
368 struct libinput *libinput;
369 struct libinput_device *device;
370 E_Input_Backend *input;
371 Eina_Bool ret = EINA_TRUE;
373 libinput = libinput_event_get_context(event);
374 input = libinput_get_user_data(libinput);
375 device = libinput_event_get_device(event);
377 switch (libinput_event_get_type(event))
379 case LIBINPUT_EVENT_DEVICE_ADDED:
380 _device_added(input, device);
382 case LIBINPUT_EVENT_DEVICE_REMOVED:
383 _device_removed(input, device);
393 _input_event_process(struct libinput_event *event)
395 if (_udev_event_process(event)) return;
396 if (_e_input_evdev_event_process(event)) return;
400 _input_events_process(E_Input_Backend *input)
402 struct libinput_event *event;
404 while ((event = libinput_get_event(input->libinput)))
406 _input_event_process(event);
407 libinput_event_destroy(event);
412 _cb_input_dispatch(void *data, Ecore_Fd_Handler *hdlr EINA_UNUSED)
414 E_Input_Backend *input;
416 if (!(input = data)) return EINA_TRUE;
418 if (libinput_dispatch(input->libinput) != 0)
419 ERR("Failed to dispatch libinput events: %m");
421 /* process pending events */
422 _input_events_process(input);
428 e_input_enable_input(E_Input_Backend *input)
430 EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
431 EINA_SAFETY_ON_NULL_RETURN_VAL(input->libinput, EINA_FALSE);
433 input->fd = libinput_get_fd(input->libinput);
438 ecore_main_fd_handler_add(input->fd, ECORE_FD_READ,
439 _cb_input_dispatch, input, NULL, NULL);
442 if (input->suspended)
444 if (libinput_resume(input->libinput) != 0)
447 input->suspended = EINA_FALSE;
449 /* process pending events */
450 _input_events_process(input);
453 input->enabled = EINA_TRUE;
454 input->suspended = EINA_FALSE;
459 input->enabled = EINA_FALSE;
461 ecore_main_fd_handler_del(input->hdlr);
468 e_input_disable_input(E_Input_Backend *input)
470 EINA_SAFETY_ON_NULL_RETURN(input);
471 EINA_SAFETY_ON_TRUE_RETURN(input->suspended);
473 /* suspend this input */
474 libinput_suspend(input->libinput);
476 /* process pending events */
477 _input_events_process(input);
479 input->suspended = EINA_TRUE;
483 e_input_seat_evdev_list_get(E_Input_Seat *seat)
485 EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
486 return seat->devices;