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 union libinput_event_target target;
56 struct libinput_event_added_seat {
57 struct libinput_event base;
58 struct libinput_seat *seat;
61 struct libinput_event_removed_seat {
62 struct libinput_event base;
63 struct libinput_seat *seat;
66 struct libinput_event_added_device {
67 struct libinput_event base;
68 struct libinput_device *device;
71 struct libinput_event_removed_device {
72 struct libinput_event base;
73 struct libinput_device *device;
76 struct libinput_event_keyboard_key {
77 struct libinput_event base;
80 enum libinput_keyboard_key_state state;
83 struct libinput_event_pointer_motion {
84 struct libinput_event base;
90 struct libinput_event_pointer_motion_absolute {
91 struct libinput_event base;
97 struct libinput_event_pointer_button {
98 struct libinput_event base;
101 enum libinput_pointer_button_state state;
104 struct libinput_event_pointer_axis {
105 struct libinput_event base;
107 enum libinput_pointer_axis axis;
111 struct libinput_event_touch_touch {
112 struct libinput_event base;
117 enum libinput_touch_type touch_type;
121 libinput_post_event(struct libinput *libinput,
122 struct libinput_event *event);
124 LIBINPUT_EXPORT enum libinput_event_type
125 libinput_event_get_type(struct libinput_event *event)
130 LIBINPUT_EXPORT union libinput_event_target
131 libinput_event_get_target(struct libinput_event *event)
133 return event->target;
136 LIBINPUT_EXPORT struct libinput_seat *
137 libinput_event_added_seat_get_seat(struct libinput_event_added_seat *event)
142 LIBINPUT_EXPORT struct libinput_seat *
143 libinput_event_removed_seat_get_seat(struct libinput_event_removed_seat *event)
148 LIBINPUT_EXPORT struct libinput_device *
149 libinput_event_added_device_get_device(
150 struct libinput_event_added_device *event)
152 return event->device;
155 LIBINPUT_EXPORT struct libinput_device *
156 libinput_event_removed_device_get_device(
157 struct libinput_event_removed_device *event)
159 return event->device;
162 LIBINPUT_EXPORT uint32_t
163 libinput_event_keyboard_key_get_time(
164 struct libinput_event_keyboard_key *event)
169 LIBINPUT_EXPORT uint32_t
170 libinput_event_keyboard_key_get_key(
171 struct libinput_event_keyboard_key *event)
176 LIBINPUT_EXPORT enum libinput_keyboard_key_state
177 libinput_event_keyboard_key_get_state(
178 struct libinput_event_keyboard_key *event)
183 LIBINPUT_EXPORT uint32_t
184 libinput_event_pointer_motion_get_time(
185 struct libinput_event_pointer_motion *event)
190 LIBINPUT_EXPORT li_fixed_t
191 libinput_event_pointer_motion_get_dx(
192 struct libinput_event_pointer_motion *event)
197 LIBINPUT_EXPORT li_fixed_t
198 libinput_event_pointer_motion_get_dy(
199 struct libinput_event_pointer_motion *event)
204 LIBINPUT_EXPORT uint32_t
205 libinput_event_pointer_motion_absolute_get_time(
206 struct libinput_event_pointer_motion_absolute *event)
211 LIBINPUT_EXPORT li_fixed_t
212 libinput_event_pointer_motion_absolute_get_x(
213 struct libinput_event_pointer_motion_absolute *event)
218 LIBINPUT_EXPORT li_fixed_t
219 libinput_event_pointer_motion_absolute_get_y(
220 struct libinput_event_pointer_motion_absolute *event)
225 LIBINPUT_EXPORT uint32_t
226 libinput_event_pointer_button_get_time(
227 struct libinput_event_pointer_button *event)
232 LIBINPUT_EXPORT uint32_t
233 libinput_event_pointer_button_get_button(
234 struct libinput_event_pointer_button *event)
236 return event->button;
239 LIBINPUT_EXPORT enum libinput_pointer_button_state
240 libinput_event_pointer_button_get_state(
241 struct libinput_event_pointer_button *event)
246 LIBINPUT_EXPORT uint32_t
247 libinput_event_pointer_axis_get_time(
248 struct libinput_event_pointer_axis *event)
253 LIBINPUT_EXPORT enum libinput_pointer_axis
254 libinput_event_pointer_axis_get_axis(
255 struct libinput_event_pointer_axis *event)
260 LIBINPUT_EXPORT li_fixed_t
261 libinput_event_pointer_axis_get_value(
262 struct libinput_event_pointer_axis *event)
267 LIBINPUT_EXPORT uint32_t
268 libinput_event_touch_touch_get_time(
269 struct libinput_event_touch_touch *event)
274 LIBINPUT_EXPORT uint32_t
275 libinput_event_touch_touch_get_slot(
276 struct libinput_event_touch_touch *event)
281 LIBINPUT_EXPORT li_fixed_t
282 libinput_event_touch_touch_get_x(
283 struct libinput_event_touch_touch *event)
288 LIBINPUT_EXPORT li_fixed_t
289 libinput_event_touch_touch_get_y(
290 struct libinput_event_touch_touch *event)
295 LIBINPUT_EXPORT enum libinput_touch_type
296 libinput_event_touch_touch_get_touch_type(
297 struct libinput_event_touch_touch *event)
299 return event->touch_type;
302 struct libinput_source *
303 libinput_add_fd(struct libinput *libinput,
305 libinput_source_dispatch_t dispatch,
308 struct libinput_source *source;
309 struct epoll_event ep;
311 source = malloc(sizeof *source);
315 source->dispatch = dispatch;
316 source->user_data = user_data;
319 memset(&ep, 0, sizeof ep);
321 ep.data.ptr = source;
323 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
333 libinput_remove_source(struct libinput *libinput,
334 struct libinput_source *source)
336 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
339 list_insert(&libinput->source_destroy_list, &source->link);
343 libinput_init(struct libinput *libinput,
344 const struct libinput_interface *interface,
345 const struct libinput_interface_backend *interface_backend,
348 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
349 if (libinput->epoll_fd < 0)
352 libinput->events_len = 4;
353 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
354 if (!libinput->events) {
355 close(libinput->epoll_fd);
359 libinput->interface = interface;
360 libinput->interface_backend = interface_backend;
361 libinput->user_data = user_data;
362 list_init(&libinput->source_destroy_list);
363 list_init(&libinput->seat_list);
369 libinput_device_destroy(struct libinput_device *device);
372 libinput_seat_destroy(struct libinput_seat *seat);
375 libinput_drop_destroyed_sources(struct libinput *libinput)
377 struct libinput_source *source, *next;
379 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
381 list_init(&libinput->source_destroy_list);
385 libinput_destroy(struct libinput *libinput)
387 struct libinput_event *event;
388 struct libinput_device *device, *next_device;
389 struct libinput_seat *seat, *next_seat;
391 if (libinput == NULL)
394 libinput_suspend(libinput);
396 libinput->interface_backend->destroy(libinput);
398 while ((event = libinput_get_event(libinput)))
399 libinput_event_destroy(event);
401 libinput_drop_destroyed_sources(libinput);
403 free(libinput->events);
405 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
406 list_for_each_safe(device, next_device,
409 libinput_device_destroy(device);
411 libinput_seat_destroy(seat);
414 close(libinput->epoll_fd);
418 static enum libinput_event_class
419 libinput_event_get_class(struct libinput_event *event)
421 switch (event->type) {
422 case LIBINPUT_EVENT_NONE:
423 return LIBINPUT_EVENT_CLASS_NONE;
425 case LIBINPUT_EVENT_ADDED_SEAT:
426 case LIBINPUT_EVENT_REMOVED_SEAT:
427 case LIBINPUT_EVENT_ADDED_DEVICE:
428 case LIBINPUT_EVENT_REMOVED_DEVICE:
429 return LIBINPUT_EVENT_CLASS_BASE;
431 case LIBINPUT_EVENT_KEYBOARD_KEY:
432 case LIBINPUT_EVENT_POINTER_MOTION:
433 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
434 case LIBINPUT_EVENT_POINTER_BUTTON:
435 case LIBINPUT_EVENT_POINTER_AXIS:
436 case LIBINPUT_EVENT_TOUCH_TOUCH:
437 return LIBINPUT_EVENT_CLASS_DEVICE;
440 /* We should never end up here. */
445 libinput_event_destroy(struct libinput_event *event)
450 switch (libinput_event_get_class(event)) {
451 case LIBINPUT_EVENT_CLASS_NONE:
452 case LIBINPUT_EVENT_CLASS_BASE:
454 case LIBINPUT_EVENT_CLASS_SEAT:
455 libinput_seat_unref(event->target.seat);
457 case LIBINPUT_EVENT_CLASS_DEVICE:
458 libinput_device_unref(event->target.device);
462 if (libinput_event_get_type(event) == LIBINPUT_EVENT_ADDED_SEAT ||
463 libinput_event_get_type(event) == LIBINPUT_EVENT_REMOVED_SEAT)
464 libinput_seat_unref(((struct libinput_event_added_seat*)event)->seat);
470 open_restricted(struct libinput *libinput,
471 const char *path, int flags)
473 return libinput->interface->open_restricted(path,
475 libinput->user_data);
479 close_restricted(struct libinput *libinput, int fd)
481 return libinput->interface->close_restricted(fd, libinput->user_data);
485 libinput_seat_init(struct libinput_seat *seat,
486 struct libinput *libinput,
488 libinput_seat_destroy_func destroy)
491 seat->libinput = libinput;
492 seat->name = strdup(name);
493 seat->destroy = destroy;
494 list_init(&seat->devices_list);
498 libinput_seat_ref(struct libinput_seat *seat)
504 libinput_seat_destroy(struct libinput_seat *seat)
506 list_remove(&seat->link);
512 libinput_seat_unref(struct libinput_seat *seat)
514 assert(seat->refcount > 0);
516 if (seat->refcount == 0)
517 libinput_seat_destroy(seat);
521 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
523 seat->user_data = user_data;
526 LIBINPUT_EXPORT void *
527 libinput_seat_get_user_data(struct libinput_seat *seat)
529 return seat->user_data;
532 LIBINPUT_EXPORT const char *
533 libinput_seat_get_name(struct libinput_seat *seat)
539 libinput_device_init(struct libinput_device *device,
540 struct libinput_seat *seat)
543 device->refcount = 1;
547 libinput_device_ref(struct libinput_device *device)
553 libinput_device_destroy(struct libinput_device *device)
555 evdev_device_destroy((struct evdev_device *) device);
559 libinput_device_unref(struct libinput_device *device)
561 assert(device->refcount > 0);
563 if (device->refcount == 0)
564 libinput_device_destroy(device);
568 libinput_get_fd(struct libinput *libinput)
570 return libinput->epoll_fd;
574 libinput_dispatch(struct libinput *libinput)
576 struct libinput_source *source;
577 struct epoll_event ep[32];
580 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
584 for (i = 0; i < count; ++i) {
585 source = ep[i].data.ptr;
586 if (source->fd == -1)
589 source->dispatch(source->user_data);
592 libinput_drop_destroyed_sources(libinput);
598 init_event_base(struct libinput_event *event,
599 enum libinput_event_type type,
600 union libinput_event_target target)
603 event->target = target;
607 post_base_event(struct libinput *libinput,
608 enum libinput_event_type type,
609 struct libinput_event *event)
611 init_event_base(event, type,
612 (union libinput_event_target) { .libinput = libinput });
613 libinput_post_event(libinput, event);
617 post_device_event(struct libinput_device *device,
618 enum libinput_event_type type,
619 struct libinput_event *event)
621 init_event_base(event, type,
622 (union libinput_event_target) { .device = device });
623 libinput_post_event(device->seat->libinput, event);
627 notify_added_seat(struct libinput_seat *seat)
629 struct libinput_event_added_seat *added_seat_event;
631 added_seat_event = malloc(sizeof *added_seat_event);
632 if (!added_seat_event)
635 libinput_seat_ref(seat);
637 *added_seat_event = (struct libinput_event_added_seat) {
641 post_base_event(seat->libinput,
642 LIBINPUT_EVENT_ADDED_SEAT,
643 &added_seat_event->base);
647 notify_removed_seat(struct libinput_seat *seat)
649 struct libinput_event_removed_seat *removed_seat_event;
651 removed_seat_event = malloc(sizeof *removed_seat_event);
652 if (!removed_seat_event)
655 libinput_seat_ref(seat);
657 *removed_seat_event = (struct libinput_event_removed_seat) {
661 post_base_event(seat->libinput,
662 LIBINPUT_EVENT_REMOVED_SEAT,
663 &removed_seat_event->base);
667 notify_added_device(struct libinput_device *device)
669 struct libinput_event_added_device *added_device_event;
671 added_device_event = malloc(sizeof *added_device_event);
672 if (!added_device_event)
675 *added_device_event = (struct libinput_event_added_device) {
679 post_base_event(device->seat->libinput,
680 LIBINPUT_EVENT_ADDED_DEVICE,
681 &added_device_event->base);
685 notify_removed_device(struct libinput_device *device)
687 struct libinput_event_removed_device *removed_device_event;
689 removed_device_event = malloc(sizeof *removed_device_event);
690 if (!removed_device_event)
693 *removed_device_event = (struct libinput_event_removed_device) {
697 post_base_event(device->seat->libinput,
698 LIBINPUT_EVENT_REMOVED_DEVICE,
699 &removed_device_event->base);
703 keyboard_notify_key(struct libinput_device *device,
706 enum libinput_keyboard_key_state state)
708 struct libinput_event_keyboard_key *key_event;
710 key_event = malloc(sizeof *key_event);
714 *key_event = (struct libinput_event_keyboard_key) {
720 post_device_event(device,
721 LIBINPUT_EVENT_KEYBOARD_KEY,
726 pointer_notify_motion(struct libinput_device *device,
731 struct libinput_event_pointer_motion *motion_event;
733 motion_event = malloc(sizeof *motion_event);
737 *motion_event = (struct libinput_event_pointer_motion) {
743 post_device_event(device,
744 LIBINPUT_EVENT_POINTER_MOTION,
745 &motion_event->base);
749 pointer_notify_motion_absolute(struct libinput_device *device,
754 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
756 motion_absolute_event = malloc(sizeof *motion_absolute_event);
757 if (!motion_absolute_event)
760 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
766 post_device_event(device,
767 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
768 &motion_absolute_event->base);
772 pointer_notify_button(struct libinput_device *device,
775 enum libinput_pointer_button_state state)
777 struct libinput_event_pointer_button *button_event;
779 button_event = malloc(sizeof *button_event);
783 *button_event = (struct libinput_event_pointer_button) {
789 post_device_event(device,
790 LIBINPUT_EVENT_POINTER_BUTTON,
791 &button_event->base);
795 pointer_notify_axis(struct libinput_device *device,
797 enum libinput_pointer_axis axis,
800 struct libinput_event_pointer_axis *axis_event;
802 axis_event = malloc(sizeof *axis_event);
806 *axis_event = (struct libinput_event_pointer_axis) {
812 post_device_event(device,
813 LIBINPUT_EVENT_POINTER_AXIS,
818 touch_notify_touch(struct libinput_device *device,
823 enum libinput_touch_type touch_type)
825 struct libinput_event_touch_touch *touch_event;
827 touch_event = malloc(sizeof *touch_event);
831 *touch_event = (struct libinput_event_touch_touch) {
836 .touch_type = touch_type,
839 post_device_event(device,
840 LIBINPUT_EVENT_TOUCH_TOUCH,
845 libinput_post_event(struct libinput *libinput,
846 struct libinput_event *event)
848 struct libinput_event **events = libinput->events;
849 size_t events_len = libinput->events_len;
850 size_t events_count = libinput->events_count;
855 if (events_count > events_len) {
857 events = realloc(events, events_len * sizeof *events);
859 fprintf(stderr, "Failed to reallocate event ring "
864 if (libinput->events_count > 0 && libinput->events_in == 0) {
865 libinput->events_in = libinput->events_len;
866 } else if (libinput->events_count > 0 &&
867 libinput->events_out >= libinput->events_in) {
868 move_len = libinput->events_len - libinput->events_out;
869 new_out = events_len - move_len;
870 memmove(events + new_out,
871 events + libinput->events_out,
872 move_len * sizeof *events);
873 libinput->events_out = new_out;
876 libinput->events = events;
877 libinput->events_len = events_len;
880 switch (libinput_event_get_class(event)) {
881 case LIBINPUT_EVENT_CLASS_NONE:
882 case LIBINPUT_EVENT_CLASS_BASE:
884 case LIBINPUT_EVENT_CLASS_SEAT:
885 libinput_seat_ref(event->target.seat);
887 case LIBINPUT_EVENT_CLASS_DEVICE:
888 libinput_device_ref(event->target.device);
892 libinput->events_count = events_count;
893 events[libinput->events_in] = event;
894 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
897 LIBINPUT_EXPORT struct libinput_event *
898 libinput_get_event(struct libinput *libinput)
900 struct libinput_event *event;
902 if (libinput->events_count == 0)
905 event = libinput->events[libinput->events_out];
906 libinput->events_out =
907 (libinput->events_out + 1) % libinput->events_len;
908 libinput->events_count--;
913 LIBINPUT_EXPORT enum libinput_event_type
914 libinput_next_event_type(struct libinput *libinput)
916 struct libinput_event *event;
918 if (libinput->events_count == 0)
919 return LIBINPUT_EVENT_NONE;
921 event = libinput->events[libinput->events_out];
925 LIBINPUT_EXPORT void *
926 libinput_get_user_data(struct libinput *libinput)
928 return libinput->user_data;
932 libinput_resume(struct libinput *libinput)
934 return libinput->interface_backend->resume(libinput);
938 libinput_suspend(struct libinput *libinput)
940 libinput->interface_backend->suspend(libinput);
944 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
946 device->user_data = user_data;
949 LIBINPUT_EXPORT void *
950 libinput_device_get_user_data(struct libinput_device *device)
952 return device->user_data;
955 LIBINPUT_EXPORT const char *
956 libinput_device_get_sysname(struct libinput_device *device)
958 return evdev_device_get_sysname((struct evdev_device *) device);
961 LIBINPUT_EXPORT const char *
962 libinput_device_get_output_name(struct libinput_device *device)
964 return evdev_device_get_output((struct evdev_device *) device);
967 LIBINPUT_EXPORT struct libinput_seat *
968 libinput_device_get_seat(struct libinput_device *device)
974 libinput_device_led_update(struct libinput_device *device,
975 enum libinput_led leds)
977 evdev_device_led_update((struct evdev_device *) device, leds);
981 libinput_device_get_keys(struct libinput_device *device,
982 char *keys, size_t size)
984 return evdev_device_get_keys((struct evdev_device *) device,
990 libinput_device_calibrate(struct libinput_device *device,
991 float calibration[6])
993 evdev_device_calibrate((struct evdev_device *) device, calibration);
997 libinput_device_has_capability(struct libinput_device *device,
998 enum libinput_device_capability capability)
1000 return evdev_device_has_capability((struct evdev_device *) device,