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"
37 enum libinput_event_class {
38 LIBINPUT_EVENT_CLASS_NONE,
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;
51 struct libinput_event {
52 enum libinput_event_type type;
53 struct libinput *libinput;
54 union libinput_event_target target;
57 struct libinput_event_added_seat {
58 struct libinput_event base;
59 struct libinput_seat *seat;
62 struct libinput_event_removed_seat {
63 struct libinput_event base;
64 struct libinput_seat *seat;
67 struct libinput_event_added_device {
68 struct libinput_event base;
69 struct libinput_device *device;
72 struct libinput_event_removed_device {
73 struct libinput_event base;
74 struct libinput_device *device;
77 struct libinput_event_keyboard_key {
78 struct libinput_event base;
81 enum libinput_keyboard_key_state state;
84 struct libinput_event_pointer_motion {
85 struct libinput_event base;
91 struct libinput_event_pointer_motion_absolute {
92 struct libinput_event base;
98 struct libinput_event_pointer_button {
99 struct libinput_event base;
102 enum libinput_pointer_button_state state;
105 struct libinput_event_pointer_axis {
106 struct libinput_event base;
108 enum libinput_pointer_axis axis;
112 struct libinput_event_touch_touch {
113 struct libinput_event base;
118 enum libinput_touch_type touch_type;
122 libinput_post_event(struct libinput *libinput,
123 struct libinput_event *event);
125 LIBINPUT_EXPORT enum libinput_event_type
126 libinput_event_get_type(struct libinput_event *event)
131 LIBINPUT_EXPORT union libinput_event_target
132 libinput_event_get_target(struct libinput_event *event)
134 return event->target;
137 LIBINPUT_EXPORT struct libinput*
138 libinput_event_get_context(struct libinput_event *event)
140 return event->libinput;
143 LIBINPUT_EXPORT struct libinput_seat *
144 libinput_event_added_seat_get_seat(struct libinput_event_added_seat *event)
149 LIBINPUT_EXPORT struct libinput_seat *
150 libinput_event_removed_seat_get_seat(struct libinput_event_removed_seat *event)
155 LIBINPUT_EXPORT struct libinput_device *
156 libinput_event_added_device_get_device(
157 struct libinput_event_added_device *event)
159 return event->device;
162 LIBINPUT_EXPORT struct libinput_device *
163 libinput_event_removed_device_get_device(
164 struct libinput_event_removed_device *event)
166 return event->device;
169 LIBINPUT_EXPORT uint32_t
170 libinput_event_keyboard_key_get_time(
171 struct libinput_event_keyboard_key *event)
176 LIBINPUT_EXPORT uint32_t
177 libinput_event_keyboard_key_get_key(
178 struct libinput_event_keyboard_key *event)
183 LIBINPUT_EXPORT enum libinput_keyboard_key_state
184 libinput_event_keyboard_key_get_state(
185 struct libinput_event_keyboard_key *event)
190 LIBINPUT_EXPORT uint32_t
191 libinput_event_pointer_motion_get_time(
192 struct libinput_event_pointer_motion *event)
197 LIBINPUT_EXPORT li_fixed_t
198 libinput_event_pointer_motion_get_dx(
199 struct libinput_event_pointer_motion *event)
204 LIBINPUT_EXPORT li_fixed_t
205 libinput_event_pointer_motion_get_dy(
206 struct libinput_event_pointer_motion *event)
211 LIBINPUT_EXPORT uint32_t
212 libinput_event_pointer_motion_absolute_get_time(
213 struct libinput_event_pointer_motion_absolute *event)
218 LIBINPUT_EXPORT li_fixed_t
219 libinput_event_pointer_motion_absolute_get_x(
220 struct libinput_event_pointer_motion_absolute *event)
225 LIBINPUT_EXPORT li_fixed_t
226 libinput_event_pointer_motion_absolute_get_y(
227 struct libinput_event_pointer_motion_absolute *event)
232 LIBINPUT_EXPORT uint32_t
233 libinput_event_pointer_button_get_time(
234 struct libinput_event_pointer_button *event)
239 LIBINPUT_EXPORT uint32_t
240 libinput_event_pointer_button_get_button(
241 struct libinput_event_pointer_button *event)
243 return event->button;
246 LIBINPUT_EXPORT enum libinput_pointer_button_state
247 libinput_event_pointer_button_get_state(
248 struct libinput_event_pointer_button *event)
253 LIBINPUT_EXPORT uint32_t
254 libinput_event_pointer_axis_get_time(
255 struct libinput_event_pointer_axis *event)
260 LIBINPUT_EXPORT enum libinput_pointer_axis
261 libinput_event_pointer_axis_get_axis(
262 struct libinput_event_pointer_axis *event)
267 LIBINPUT_EXPORT li_fixed_t
268 libinput_event_pointer_axis_get_value(
269 struct libinput_event_pointer_axis *event)
274 LIBINPUT_EXPORT uint32_t
275 libinput_event_touch_touch_get_time(
276 struct libinput_event_touch_touch *event)
281 LIBINPUT_EXPORT uint32_t
282 libinput_event_touch_touch_get_slot(
283 struct libinput_event_touch_touch *event)
288 LIBINPUT_EXPORT li_fixed_t
289 libinput_event_touch_touch_get_x(
290 struct libinput_event_touch_touch *event)
295 LIBINPUT_EXPORT li_fixed_t
296 libinput_event_touch_touch_get_y(
297 struct libinput_event_touch_touch *event)
302 LIBINPUT_EXPORT enum libinput_touch_type
303 libinput_event_touch_touch_get_touch_type(
304 struct libinput_event_touch_touch *event)
306 return event->touch_type;
309 struct libinput_source *
310 libinput_add_fd(struct libinput *libinput,
312 libinput_source_dispatch_t dispatch,
315 struct libinput_source *source;
316 struct epoll_event ep;
318 source = malloc(sizeof *source);
322 source->dispatch = dispatch;
323 source->user_data = user_data;
326 memset(&ep, 0, sizeof ep);
328 ep.data.ptr = source;
330 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
340 libinput_remove_source(struct libinput *libinput,
341 struct libinput_source *source)
343 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
346 list_insert(&libinput->source_destroy_list, &source->link);
350 libinput_init(struct libinput *libinput,
351 const struct libinput_interface *interface,
352 const struct libinput_interface_backend *interface_backend,
355 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
356 if (libinput->epoll_fd < 0)
359 libinput->events_len = 4;
360 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
361 if (!libinput->events) {
362 close(libinput->epoll_fd);
366 libinput->interface = interface;
367 libinput->interface_backend = interface_backend;
368 libinput->user_data = user_data;
369 list_init(&libinput->source_destroy_list);
370 list_init(&libinput->seat_list);
376 libinput_device_destroy(struct libinput_device *device);
379 libinput_seat_destroy(struct libinput_seat *seat);
382 libinput_drop_destroyed_sources(struct libinput *libinput)
384 struct libinput_source *source, *next;
386 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
388 list_init(&libinput->source_destroy_list);
392 libinput_destroy(struct libinput *libinput)
394 struct libinput_event *event;
395 struct libinput_device *device, *next_device;
396 struct libinput_seat *seat, *next_seat;
398 if (libinput == NULL)
401 libinput_suspend(libinput);
403 libinput->interface_backend->destroy(libinput);
405 while ((event = libinput_get_event(libinput)))
406 libinput_event_destroy(event);
408 libinput_drop_destroyed_sources(libinput);
410 free(libinput->events);
412 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
413 list_for_each_safe(device, next_device,
416 libinput_device_destroy(device);
418 libinput_seat_destroy(seat);
421 close(libinput->epoll_fd);
425 static enum libinput_event_class
426 libinput_event_get_class(struct libinput_event *event)
428 switch (event->type) {
429 case LIBINPUT_EVENT_NONE:
430 return LIBINPUT_EVENT_CLASS_NONE;
432 case LIBINPUT_EVENT_ADDED_SEAT:
433 case LIBINPUT_EVENT_REMOVED_SEAT:
434 case LIBINPUT_EVENT_ADDED_DEVICE:
435 case LIBINPUT_EVENT_REMOVED_DEVICE:
436 return LIBINPUT_EVENT_CLASS_BASE;
438 case LIBINPUT_EVENT_KEYBOARD_KEY:
439 case LIBINPUT_EVENT_POINTER_MOTION:
440 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
441 case LIBINPUT_EVENT_POINTER_BUTTON:
442 case LIBINPUT_EVENT_POINTER_AXIS:
443 case LIBINPUT_EVENT_TOUCH_TOUCH:
444 return LIBINPUT_EVENT_CLASS_DEVICE;
447 /* We should never end up here. */
452 libinput_event_destroy(struct libinput_event *event)
457 switch (libinput_event_get_class(event)) {
458 case LIBINPUT_EVENT_CLASS_NONE:
459 case LIBINPUT_EVENT_CLASS_BASE:
461 case LIBINPUT_EVENT_CLASS_SEAT:
462 libinput_seat_unref(event->target.seat);
464 case LIBINPUT_EVENT_CLASS_DEVICE:
465 libinput_device_unref(event->target.device);
469 if (libinput_event_get_type(event) == LIBINPUT_EVENT_ADDED_SEAT ||
470 libinput_event_get_type(event) == LIBINPUT_EVENT_REMOVED_SEAT)
471 libinput_seat_unref(((struct libinput_event_added_seat*)event)->seat);
477 open_restricted(struct libinput *libinput,
478 const char *path, int flags)
480 return libinput->interface->open_restricted(path,
482 libinput->user_data);
486 close_restricted(struct libinput *libinput, int fd)
488 return libinput->interface->close_restricted(fd, libinput->user_data);
492 libinput_seat_init(struct libinput_seat *seat,
493 struct libinput *libinput,
495 libinput_seat_destroy_func destroy)
498 seat->libinput = libinput;
499 seat->name = strdup(name);
500 seat->destroy = destroy;
501 list_init(&seat->devices_list);
505 libinput_seat_ref(struct libinput_seat *seat)
511 libinput_seat_destroy(struct libinput_seat *seat)
513 list_remove(&seat->link);
519 libinput_seat_unref(struct libinput_seat *seat)
521 assert(seat->refcount > 0);
523 if (seat->refcount == 0)
524 libinput_seat_destroy(seat);
528 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
530 seat->user_data = user_data;
533 LIBINPUT_EXPORT void *
534 libinput_seat_get_user_data(struct libinput_seat *seat)
536 return seat->user_data;
539 LIBINPUT_EXPORT const char *
540 libinput_seat_get_name(struct libinput_seat *seat)
546 libinput_device_init(struct libinput_device *device,
547 struct libinput_seat *seat)
550 device->refcount = 1;
554 libinput_device_ref(struct libinput_device *device)
560 libinput_device_destroy(struct libinput_device *device)
562 evdev_device_destroy((struct evdev_device *) device);
566 libinput_device_unref(struct libinput_device *device)
568 assert(device->refcount > 0);
570 if (device->refcount == 0)
571 libinput_device_destroy(device);
575 libinput_get_fd(struct libinput *libinput)
577 return libinput->epoll_fd;
581 libinput_dispatch(struct libinput *libinput)
583 struct libinput_source *source;
584 struct epoll_event ep[32];
587 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
591 for (i = 0; i < count; ++i) {
592 source = ep[i].data.ptr;
593 if (source->fd == -1)
596 source->dispatch(source->user_data);
599 libinput_drop_destroyed_sources(libinput);
605 init_event_base(struct libinput_event *event,
606 struct libinput *libinput,
607 enum libinput_event_type type,
608 union libinput_event_target target)
611 event->libinput = libinput;
612 event->target = target;
616 post_base_event(struct libinput *libinput,
617 enum libinput_event_type type,
618 struct libinput_event *event)
620 init_event_base(event, libinput, type,
621 (union libinput_event_target) { .libinput = libinput });
622 libinput_post_event(libinput, event);
626 post_device_event(struct libinput_device *device,
627 enum libinput_event_type type,
628 struct libinput_event *event)
630 init_event_base(event, device->seat->libinput, type,
631 (union libinput_event_target) { .device = device });
632 libinput_post_event(device->seat->libinput, event);
636 notify_added_seat(struct libinput_seat *seat)
638 struct libinput_event_added_seat *added_seat_event;
640 added_seat_event = malloc(sizeof *added_seat_event);
641 if (!added_seat_event)
644 libinput_seat_ref(seat);
646 *added_seat_event = (struct libinput_event_added_seat) {
650 post_base_event(seat->libinput,
651 LIBINPUT_EVENT_ADDED_SEAT,
652 &added_seat_event->base);
656 notify_removed_seat(struct libinput_seat *seat)
658 struct libinput_event_removed_seat *removed_seat_event;
660 removed_seat_event = malloc(sizeof *removed_seat_event);
661 if (!removed_seat_event)
664 libinput_seat_ref(seat);
666 *removed_seat_event = (struct libinput_event_removed_seat) {
670 post_base_event(seat->libinput,
671 LIBINPUT_EVENT_REMOVED_SEAT,
672 &removed_seat_event->base);
676 notify_added_device(struct libinput_device *device)
678 struct libinput_event_added_device *added_device_event;
680 added_device_event = malloc(sizeof *added_device_event);
681 if (!added_device_event)
684 *added_device_event = (struct libinput_event_added_device) {
688 post_base_event(device->seat->libinput,
689 LIBINPUT_EVENT_ADDED_DEVICE,
690 &added_device_event->base);
694 notify_removed_device(struct libinput_device *device)
696 struct libinput_event_removed_device *removed_device_event;
698 removed_device_event = malloc(sizeof *removed_device_event);
699 if (!removed_device_event)
702 *removed_device_event = (struct libinput_event_removed_device) {
706 post_base_event(device->seat->libinput,
707 LIBINPUT_EVENT_REMOVED_DEVICE,
708 &removed_device_event->base);
712 keyboard_notify_key(struct libinput_device *device,
715 enum libinput_keyboard_key_state state)
717 struct libinput_event_keyboard_key *key_event;
719 key_event = malloc(sizeof *key_event);
723 *key_event = (struct libinput_event_keyboard_key) {
729 post_device_event(device,
730 LIBINPUT_EVENT_KEYBOARD_KEY,
735 pointer_notify_motion(struct libinput_device *device,
740 struct libinput_event_pointer_motion *motion_event;
742 motion_event = malloc(sizeof *motion_event);
746 *motion_event = (struct libinput_event_pointer_motion) {
752 post_device_event(device,
753 LIBINPUT_EVENT_POINTER_MOTION,
754 &motion_event->base);
758 pointer_notify_motion_absolute(struct libinput_device *device,
763 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
765 motion_absolute_event = malloc(sizeof *motion_absolute_event);
766 if (!motion_absolute_event)
769 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
775 post_device_event(device,
776 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
777 &motion_absolute_event->base);
781 pointer_notify_button(struct libinput_device *device,
784 enum libinput_pointer_button_state state)
786 struct libinput_event_pointer_button *button_event;
788 button_event = malloc(sizeof *button_event);
792 *button_event = (struct libinput_event_pointer_button) {
798 post_device_event(device,
799 LIBINPUT_EVENT_POINTER_BUTTON,
800 &button_event->base);
804 pointer_notify_axis(struct libinput_device *device,
806 enum libinput_pointer_axis axis,
809 struct libinput_event_pointer_axis *axis_event;
811 axis_event = malloc(sizeof *axis_event);
815 *axis_event = (struct libinput_event_pointer_axis) {
821 post_device_event(device,
822 LIBINPUT_EVENT_POINTER_AXIS,
827 touch_notify_touch(struct libinput_device *device,
832 enum libinput_touch_type touch_type)
834 struct libinput_event_touch_touch *touch_event;
836 touch_event = malloc(sizeof *touch_event);
840 *touch_event = (struct libinput_event_touch_touch) {
845 .touch_type = touch_type,
848 post_device_event(device,
849 LIBINPUT_EVENT_TOUCH_TOUCH,
854 libinput_post_event(struct libinput *libinput,
855 struct libinput_event *event)
857 struct libinput_event **events = libinput->events;
858 size_t events_len = libinput->events_len;
859 size_t events_count = libinput->events_count;
864 if (events_count > events_len) {
866 events = realloc(events, events_len * sizeof *events);
868 fprintf(stderr, "Failed to reallocate event ring "
873 if (libinput->events_count > 0 && libinput->events_in == 0) {
874 libinput->events_in = libinput->events_len;
875 } else if (libinput->events_count > 0 &&
876 libinput->events_out >= libinput->events_in) {
877 move_len = libinput->events_len - libinput->events_out;
878 new_out = events_len - move_len;
879 memmove(events + new_out,
880 events + libinput->events_out,
881 move_len * sizeof *events);
882 libinput->events_out = new_out;
885 libinput->events = events;
886 libinput->events_len = events_len;
889 switch (libinput_event_get_class(event)) {
890 case LIBINPUT_EVENT_CLASS_NONE:
891 case LIBINPUT_EVENT_CLASS_BASE:
893 case LIBINPUT_EVENT_CLASS_SEAT:
894 libinput_seat_ref(event->target.seat);
896 case LIBINPUT_EVENT_CLASS_DEVICE:
897 libinput_device_ref(event->target.device);
901 libinput->events_count = events_count;
902 events[libinput->events_in] = event;
903 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
906 LIBINPUT_EXPORT struct libinput_event *
907 libinput_get_event(struct libinput *libinput)
909 struct libinput_event *event;
911 if (libinput->events_count == 0)
914 event = libinput->events[libinput->events_out];
915 libinput->events_out =
916 (libinput->events_out + 1) % libinput->events_len;
917 libinput->events_count--;
922 LIBINPUT_EXPORT enum libinput_event_type
923 libinput_next_event_type(struct libinput *libinput)
925 struct libinput_event *event;
927 if (libinput->events_count == 0)
928 return LIBINPUT_EVENT_NONE;
930 event = libinput->events[libinput->events_out];
934 LIBINPUT_EXPORT void *
935 libinput_get_user_data(struct libinput *libinput)
937 return libinput->user_data;
941 libinput_resume(struct libinput *libinput)
943 return libinput->interface_backend->resume(libinput);
947 libinput_suspend(struct libinput *libinput)
949 libinput->interface_backend->suspend(libinput);
953 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
955 device->user_data = user_data;
958 LIBINPUT_EXPORT void *
959 libinput_device_get_user_data(struct libinput_device *device)
961 return device->user_data;
964 LIBINPUT_EXPORT const char *
965 libinput_device_get_sysname(struct libinput_device *device)
967 return evdev_device_get_sysname((struct evdev_device *) device);
970 LIBINPUT_EXPORT const char *
971 libinput_device_get_output_name(struct libinput_device *device)
973 return evdev_device_get_output((struct evdev_device *) device);
976 LIBINPUT_EXPORT struct libinput_seat *
977 libinput_device_get_seat(struct libinput_device *device)
983 libinput_device_led_update(struct libinput_device *device,
984 enum libinput_led leds)
986 evdev_device_led_update((struct evdev_device *) device, leds);
990 libinput_device_get_keys(struct libinput_device *device,
991 char *keys, size_t size)
993 return evdev_device_get_keys((struct evdev_device *) device,
999 libinput_device_calibrate(struct libinput_device *device,
1000 float calibration[6])
1002 evdev_device_calibrate((struct evdev_device *) device, calibration);
1006 libinput_device_has_capability(struct libinput_device *device,
1007 enum libinput_device_capability capability)
1009 return evdev_device_has_capability((struct evdev_device *) device,