2 * Copyright © 2013 Jonas Ådahl
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <sys/epoll.h>
34 #include "libinput-private.h"
36 #include "udev-seat.h"
38 enum libinput_event_class {
39 LIBINPUT_EVENT_CLASS_BASE,
40 LIBINPUT_EVENT_CLASS_SEAT,
41 LIBINPUT_EVENT_CLASS_DEVICE,
44 struct libinput_source {
45 libinput_source_dispatch_t dispatch;
52 libinput_post_event(struct libinput *libinput,
53 struct libinput_event *event);
55 struct libinput_source *
56 libinput_add_fd(struct libinput *libinput,
58 libinput_source_dispatch_t dispatch,
61 struct libinput_source *source;
62 struct epoll_event ep;
64 source = malloc(sizeof *source);
68 source->dispatch = dispatch;
69 source->user_data = user_data;
72 memset(&ep, 0, sizeof ep);
76 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
86 libinput_remove_source(struct libinput *libinput,
87 struct libinput_source *source)
89 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
92 list_insert(&libinput->source_destroy_list, &source->link);
96 libinput_init(struct libinput *libinput,
97 const struct libinput_interface *interface,
100 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
101 if (libinput->epoll_fd < 0)
104 libinput->interface = interface;
105 libinput->user_data = user_data;
106 list_init(&libinput->source_destroy_list);
107 list_init(&libinput->seat_list);
113 libinput_destroy(struct libinput *libinput)
115 struct libinput_event *event;
117 while ((event = libinput_get_event(libinput)))
119 free(libinput->events);
121 close(libinput->epoll_fd);
125 static enum libinput_event_class
126 libinput_event_get_class(struct libinput_event *event)
128 switch (event->type) {
129 case LIBINPUT_EVENT_ADDED_SEAT:
130 case LIBINPUT_EVENT_REMOVED_SEAT:
131 case LIBINPUT_EVENT_ADDED_DEVICE:
132 case LIBINPUT_EVENT_REMOVED_DEVICE:
133 return LIBINPUT_EVENT_CLASS_BASE;
135 case LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY:
136 case LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY:
137 case LIBINPUT_EVENT_KEYBOARD_KEY:
138 case LIBINPUT_EVENT_POINTER_MOTION:
139 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
140 case LIBINPUT_EVENT_POINTER_BUTTON:
141 case LIBINPUT_EVENT_POINTER_AXIS:
142 case LIBINPUT_EVENT_TOUCH_TOUCH:
143 return LIBINPUT_EVENT_CLASS_DEVICE;
146 /* We should never end up here. */
151 libinput_event_destroy(struct libinput_event *event)
153 switch (libinput_event_get_class(event)) {
154 case LIBINPUT_EVENT_CLASS_BASE:
156 case LIBINPUT_EVENT_CLASS_SEAT:
157 libinput_seat_unref(event->target.seat);
159 case LIBINPUT_EVENT_CLASS_DEVICE:
160 libinput_device_unref(event->target.device);
168 open_restricted(struct libinput *libinput,
169 const char *path, int flags)
171 return libinput->interface->open_restricted(path,
173 libinput->user_data);
177 close_restricted(struct libinput *libinput, int fd)
179 return libinput->interface->close_restricted(fd, libinput->user_data);
183 libinput_seat_init(struct libinput_seat *seat,
184 struct libinput *libinput,
188 seat->libinput = libinput;
189 seat->name = strdup(name);
190 list_init(&seat->devices_list);
194 libinput_seat_ref(struct libinput_seat *seat)
200 libinput_seat_unref(struct libinput_seat *seat)
203 if (seat->refcount == 0) {
205 udev_seat_destroy((struct udev_seat *) seat);
210 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
212 seat->user_data = user_data;
215 LIBINPUT_EXPORT void *
216 libinput_seat_get_user_data(struct libinput_seat *seat)
218 return seat->user_data;
221 LIBINPUT_EXPORT const char *
222 libinput_seat_get_name(struct libinput_seat *seat)
228 libinput_device_init(struct libinput_device *device,
229 struct libinput_seat *seat)
232 device->refcount = 1;
236 libinput_device_ref(struct libinput_device *device)
242 libinput_device_unref(struct libinput_device *device)
245 if (device->refcount == 0)
246 evdev_device_destroy((struct evdev_device *) device);
250 libinput_get_fd(struct libinput *libinput)
252 return libinput->epoll_fd;
256 libinput_dispatch(struct libinput *libinput)
258 struct libinput_source *source, *next;
259 struct epoll_event ep[32];
262 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
266 for (i = 0; i < count; ++i) {
267 source = ep[i].data.ptr;
268 if (source->fd == -1)
271 source->dispatch(source->user_data);
274 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
276 list_init(&libinput->source_destroy_list);
278 return libinput->events_count > 0 ? 0 : -EAGAIN;
282 init_event_base(struct libinput_event *event,
283 enum libinput_event_type type,
284 union libinput_event_target target)
287 event->target = target;
291 post_base_event(struct libinput *libinput,
292 enum libinput_event_type type,
293 struct libinput_event *event)
295 init_event_base(event, type,
296 (union libinput_event_target) { .libinput = libinput });
297 libinput_post_event(libinput, event);
301 post_device_event(struct libinput_device *device,
302 enum libinput_event_type type,
303 struct libinput_event *event)
305 init_event_base(event, type,
306 (union libinput_event_target) { .device = device });
307 libinput_post_event(device->seat->libinput, event);
311 notify_added_seat(struct libinput_seat *seat)
313 struct libinput_event_added_seat *added_seat_event;
315 added_seat_event = malloc(sizeof *added_seat_event);
316 if (!added_seat_event)
319 *added_seat_event = (struct libinput_event_added_seat) {
323 post_base_event(seat->libinput,
324 LIBINPUT_EVENT_ADDED_SEAT,
325 &added_seat_event->base);
329 notify_removed_seat(struct libinput_seat *seat)
331 struct libinput_event_removed_seat *removed_seat_event;
333 removed_seat_event = malloc(sizeof *removed_seat_event);
334 if (!removed_seat_event)
337 *removed_seat_event = (struct libinput_event_removed_seat) {
341 post_base_event(seat->libinput,
342 LIBINPUT_EVENT_REMOVED_SEAT,
343 &removed_seat_event->base);
347 notify_added_device(struct libinput_device *device)
349 struct libinput_event_added_device *added_device_event;
351 added_device_event = malloc(sizeof *added_device_event);
352 if (!added_device_event)
355 *added_device_event = (struct libinput_event_added_device) {
359 post_base_event(device->seat->libinput,
360 LIBINPUT_EVENT_ADDED_DEVICE,
361 &added_device_event->base);
365 notify_removed_device(struct libinput_device *device)
367 struct libinput_event_removed_device *removed_device_event;
369 removed_device_event = malloc(sizeof *removed_device_event);
370 if (!removed_device_event)
373 *removed_device_event = (struct libinput_event_removed_device) {
377 post_base_event(device->seat->libinput,
378 LIBINPUT_EVENT_REMOVED_DEVICE,
379 &removed_device_event->base);
383 device_register_capability(struct libinput_device *device,
384 enum libinput_device_capability capability)
386 struct libinput_event_device_register_capability *capability_event;
388 capability_event = malloc(sizeof *capability_event);
390 *capability_event = (struct libinput_event_device_register_capability) {
391 .capability = capability,
394 post_device_event(device,
395 LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
396 &capability_event->base);
400 device_unregister_capability(struct libinput_device *device,
401 enum libinput_device_capability capability)
403 struct libinput_event_device_unregister_capability *capability_event;
405 capability_event = malloc(sizeof *capability_event);
407 *capability_event = (struct libinput_event_device_unregister_capability) {
408 .capability = capability,
411 post_device_event(device,
412 LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
413 &capability_event->base);
417 keyboard_notify_key(struct libinput_device *device,
420 enum libinput_keyboard_key_state state)
422 struct libinput_event_keyboard_key *key_event;
424 key_event = malloc(sizeof *key_event);
428 *key_event = (struct libinput_event_keyboard_key) {
434 post_device_event(device,
435 LIBINPUT_EVENT_KEYBOARD_KEY,
440 pointer_notify_motion(struct libinput_device *device,
445 struct libinput_event_pointer_motion *motion_event;
447 motion_event = malloc(sizeof *motion_event);
451 *motion_event = (struct libinput_event_pointer_motion) {
457 post_device_event(device,
458 LIBINPUT_EVENT_POINTER_MOTION,
459 &motion_event->base);
463 pointer_notify_motion_absolute(struct libinput_device *device,
468 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
470 motion_absolute_event = malloc(sizeof *motion_absolute_event);
471 if (!motion_absolute_event)
474 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
480 post_device_event(device,
481 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
482 &motion_absolute_event->base);
486 pointer_notify_button(struct libinput_device *device,
489 enum libinput_pointer_button_state state)
491 struct libinput_event_pointer_button *button_event;
493 button_event = malloc(sizeof *button_event);
497 *button_event = (struct libinput_event_pointer_button) {
503 post_device_event(device,
504 LIBINPUT_EVENT_POINTER_BUTTON,
505 &button_event->base);
509 pointer_notify_axis(struct libinput_device *device,
511 enum libinput_pointer_axis axis,
514 struct libinput_event_pointer_axis *axis_event;
516 axis_event = malloc(sizeof *axis_event);
520 *axis_event = (struct libinput_event_pointer_axis) {
526 post_device_event(device,
527 LIBINPUT_EVENT_POINTER_AXIS,
532 touch_notify_touch(struct libinput_device *device,
537 enum libinput_touch_type touch_type)
539 struct libinput_event_touch_touch *touch_event;
541 touch_event = malloc(sizeof *touch_event);
545 *touch_event = (struct libinput_event_touch_touch) {
550 .touch_type = touch_type,
553 post_device_event(device,
554 LIBINPUT_EVENT_TOUCH_TOUCH,
559 libinput_post_event(struct libinput *libinput,
560 struct libinput_event *event)
562 struct libinput_event **events = libinput->events;
563 size_t events_len = libinput->events_len;
564 size_t events_count = libinput->events_count;
569 if (events_count > events_len) {
574 events = realloc(events, events_len * sizeof *events);
576 fprintf(stderr, "Failed to reallocate event ring "
581 if (libinput->events_count > 0 && libinput->events_in == 0) {
582 libinput->events_in = libinput->events_len;
583 } else if (libinput->events_count > 0 &&
584 libinput->events_out >= libinput->events_in) {
585 move_len = libinput->events_len - libinput->events_out;
586 new_out = events_len - move_len;
587 memmove(events + new_out,
588 libinput->events + libinput->events_out,
589 move_len * sizeof *events);
590 libinput->events_out = new_out;
593 libinput->events = events;
594 libinput->events_len = events_len;
597 switch (libinput_event_get_class(event)) {
598 case LIBINPUT_EVENT_CLASS_BASE:
600 case LIBINPUT_EVENT_CLASS_SEAT:
601 libinput_seat_ref(event->target.seat);
603 case LIBINPUT_EVENT_CLASS_DEVICE:
604 libinput_device_ref(event->target.device);
608 libinput->events_count = events_count;
609 events[libinput->events_in] = event;
610 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
613 LIBINPUT_EXPORT struct libinput_event *
614 libinput_get_event(struct libinput *libinput)
616 struct libinput_event *event;
618 if (libinput->events_count == 0)
621 event = libinput->events[libinput->events_out];
622 libinput->events_out =
623 (libinput->events_out + 1) % libinput->events_len;
624 libinput->events_count--;
629 LIBINPUT_EXPORT void *
630 libinput_get_user_data(struct libinput *libinput)
632 return libinput->user_data;
636 libinput_resume(struct libinput *libinput)
638 return udev_input_enable((struct udev_input *) libinput);
642 libinput_suspend(struct libinput *libinput)
644 udev_input_disable((struct udev_input *) libinput);
648 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
650 device->user_data = user_data;
653 LIBINPUT_EXPORT void *
654 libinput_device_get_user_data(struct libinput_device *device)
656 return device->user_data;
659 LIBINPUT_EXPORT const char *
660 libinput_device_get_output_name(struct libinput_device *device)
662 return evdev_device_get_output((struct evdev_device *) device);
665 LIBINPUT_EXPORT struct libinput_seat *
666 libinput_device_get_seat(struct libinput_device *device)
672 libinput_device_led_update(struct libinput_device *device,
673 enum libinput_led leds)
675 evdev_device_led_update((struct evdev_device *) device, leds);
679 libinput_device_get_keys(struct libinput_device *device,
680 char *keys, size_t size)
682 return evdev_device_get_keys((struct evdev_device *) device,
688 libinput_device_calibrate(struct libinput_device *device,
689 float calibration[6])
691 evdev_device_calibrate((struct evdev_device *) device, calibration);