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_BASE,
39 LIBINPUT_EVENT_CLASS_SEAT,
40 LIBINPUT_EVENT_CLASS_DEVICE,
43 struct libinput_source {
44 libinput_source_dispatch_t dispatch;
50 struct libinput_event {
51 enum libinput_event_type type;
52 union libinput_event_target target;
55 struct libinput_event_added_seat {
56 struct libinput_event base;
57 struct libinput_seat *seat;
60 struct libinput_event_removed_seat {
61 struct libinput_event base;
62 struct libinput_seat *seat;
65 struct libinput_event_added_device {
66 struct libinput_event base;
67 struct libinput_device *device;
70 struct libinput_event_removed_device {
71 struct libinput_event base;
72 struct libinput_device *device;
75 struct libinput_event_keyboard_key {
76 struct libinput_event base;
79 enum libinput_keyboard_key_state state;
82 struct libinput_event_pointer_motion {
83 struct libinput_event base;
89 struct libinput_event_pointer_motion_absolute {
90 struct libinput_event base;
96 struct libinput_event_pointer_button {
97 struct libinput_event base;
100 enum libinput_pointer_button_state state;
103 struct libinput_event_pointer_axis {
104 struct libinput_event base;
106 enum libinput_pointer_axis axis;
110 struct libinput_event_touch_touch {
111 struct libinput_event base;
116 enum libinput_touch_type touch_type;
120 libinput_post_event(struct libinput *libinput,
121 struct libinput_event *event);
123 LIBINPUT_EXPORT enum libinput_event_type
124 libinput_event_get_type(struct libinput_event *event)
129 LIBINPUT_EXPORT union libinput_event_target
130 libinput_event_get_target(struct libinput_event *event)
132 return event->target;
135 LIBINPUT_EXPORT struct libinput_seat *
136 libinput_event_added_seat_get_seat(struct libinput_event_added_seat *event)
141 LIBINPUT_EXPORT struct libinput_seat *
142 libinput_event_removed_seat_get_seat(struct libinput_event_removed_seat *event)
147 LIBINPUT_EXPORT struct libinput_device *
148 libinput_event_added_device_get_device(
149 struct libinput_event_added_device *event)
151 return event->device;
154 LIBINPUT_EXPORT struct libinput_device *
155 libinput_event_removed_device_get_device(
156 struct libinput_event_removed_device *event)
158 return event->device;
161 LIBINPUT_EXPORT uint32_t
162 libinput_event_keyboard_key_get_time(
163 struct libinput_event_keyboard_key *event)
168 LIBINPUT_EXPORT uint32_t
169 libinput_event_keyboard_key_get_key(
170 struct libinput_event_keyboard_key *event)
175 LIBINPUT_EXPORT enum libinput_keyboard_key_state
176 libinput_event_keyboard_key_get_state(
177 struct libinput_event_keyboard_key *event)
182 LIBINPUT_EXPORT uint32_t
183 libinput_event_pointer_motion_get_time(
184 struct libinput_event_pointer_motion *event)
189 LIBINPUT_EXPORT li_fixed_t
190 libinput_event_pointer_motion_get_dx(
191 struct libinput_event_pointer_motion *event)
196 LIBINPUT_EXPORT li_fixed_t
197 libinput_event_pointer_motion_get_dy(
198 struct libinput_event_pointer_motion *event)
203 LIBINPUT_EXPORT uint32_t
204 libinput_event_pointer_motion_absolute_get_time(
205 struct libinput_event_pointer_motion_absolute *event)
210 LIBINPUT_EXPORT li_fixed_t
211 libinput_event_pointer_motion_absolute_get_x(
212 struct libinput_event_pointer_motion_absolute *event)
217 LIBINPUT_EXPORT li_fixed_t
218 libinput_event_pointer_motion_absolute_get_y(
219 struct libinput_event_pointer_motion_absolute *event)
224 LIBINPUT_EXPORT uint32_t
225 libinput_event_pointer_button_get_time(
226 struct libinput_event_pointer_button *event)
231 LIBINPUT_EXPORT uint32_t
232 libinput_event_pointer_button_get_button(
233 struct libinput_event_pointer_button *event)
235 return event->button;
238 LIBINPUT_EXPORT enum libinput_pointer_button_state
239 libinput_event_pointer_button_get_state(
240 struct libinput_event_pointer_button *event)
245 LIBINPUT_EXPORT uint32_t
246 libinput_event_pointer_axis_get_time(
247 struct libinput_event_pointer_axis *event)
252 LIBINPUT_EXPORT enum libinput_pointer_axis
253 libinput_event_pointer_axis_get_axis(
254 struct libinput_event_pointer_axis *event)
259 LIBINPUT_EXPORT li_fixed_t
260 libinput_event_pointer_axis_get_value(
261 struct libinput_event_pointer_axis *event)
266 LIBINPUT_EXPORT uint32_t
267 libinput_event_touch_touch_get_time(
268 struct libinput_event_touch_touch *event)
273 LIBINPUT_EXPORT uint32_t
274 libinput_event_touch_touch_get_slot(
275 struct libinput_event_touch_touch *event)
280 LIBINPUT_EXPORT li_fixed_t
281 libinput_event_touch_touch_get_x(
282 struct libinput_event_touch_touch *event)
287 LIBINPUT_EXPORT li_fixed_t
288 libinput_event_touch_touch_get_y(
289 struct libinput_event_touch_touch *event)
294 LIBINPUT_EXPORT enum libinput_touch_type
295 libinput_event_touch_touch_get_touch_type(
296 struct libinput_event_touch_touch *event)
298 return event->touch_type;
301 struct libinput_source *
302 libinput_add_fd(struct libinput *libinput,
304 libinput_source_dispatch_t dispatch,
307 struct libinput_source *source;
308 struct epoll_event ep;
310 source = malloc(sizeof *source);
314 source->dispatch = dispatch;
315 source->user_data = user_data;
318 memset(&ep, 0, sizeof ep);
320 ep.data.ptr = source;
322 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
332 libinput_remove_source(struct libinput *libinput,
333 struct libinput_source *source)
335 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
338 list_insert(&libinput->source_destroy_list, &source->link);
342 libinput_init(struct libinput *libinput,
343 const struct libinput_interface *interface,
344 const struct libinput_interface_backend *interface_backend,
347 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
348 if (libinput->epoll_fd < 0)
351 libinput->events_len = 4;
352 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
353 if (!libinput->events) {
354 close(libinput->epoll_fd);
358 libinput->interface = interface;
359 libinput->interface_backend = interface_backend;
360 libinput->user_data = user_data;
361 list_init(&libinput->source_destroy_list);
362 list_init(&libinput->seat_list);
368 libinput_device_destroy(struct libinput_device *device);
371 libinput_seat_destroy(struct libinput_seat *seat);
374 libinput_drop_destroyed_sources(struct libinput *libinput)
376 struct libinput_source *source, *next;
378 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
380 list_init(&libinput->source_destroy_list);
384 libinput_destroy(struct libinput *libinput)
386 struct libinput_event *event;
387 struct libinput_device *device, *next_device;
388 struct libinput_seat *seat, *next_seat;
390 if (libinput == NULL)
393 libinput_suspend(libinput);
395 libinput->interface_backend->destroy(libinput);
397 while ((event = libinput_get_event(libinput)))
398 libinput_event_destroy(event);
400 libinput_drop_destroyed_sources(libinput);
402 free(libinput->events);
404 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
405 list_for_each_safe(device, next_device,
408 libinput_device_destroy(device);
410 libinput_seat_destroy(seat);
413 close(libinput->epoll_fd);
417 static enum libinput_event_class
418 libinput_event_get_class(struct libinput_event *event)
420 switch (event->type) {
421 case LIBINPUT_EVENT_ADDED_SEAT:
422 case LIBINPUT_EVENT_REMOVED_SEAT:
423 case LIBINPUT_EVENT_ADDED_DEVICE:
424 case LIBINPUT_EVENT_REMOVED_DEVICE:
425 return LIBINPUT_EVENT_CLASS_BASE;
427 case LIBINPUT_EVENT_KEYBOARD_KEY:
428 case LIBINPUT_EVENT_POINTER_MOTION:
429 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
430 case LIBINPUT_EVENT_POINTER_BUTTON:
431 case LIBINPUT_EVENT_POINTER_AXIS:
432 case LIBINPUT_EVENT_TOUCH_TOUCH:
433 return LIBINPUT_EVENT_CLASS_DEVICE;
436 /* We should never end up here. */
441 libinput_event_destroy(struct libinput_event *event)
446 switch (libinput_event_get_class(event)) {
447 case LIBINPUT_EVENT_CLASS_BASE:
449 case LIBINPUT_EVENT_CLASS_SEAT:
450 libinput_seat_unref(event->target.seat);
452 case LIBINPUT_EVENT_CLASS_DEVICE:
453 libinput_device_unref(event->target.device);
457 if (libinput_event_get_type(event) == LIBINPUT_EVENT_ADDED_SEAT ||
458 libinput_event_get_type(event) == LIBINPUT_EVENT_REMOVED_SEAT)
459 libinput_seat_unref(((struct libinput_event_added_seat*)event)->seat);
465 open_restricted(struct libinput *libinput,
466 const char *path, int flags)
468 return libinput->interface->open_restricted(path,
470 libinput->user_data);
474 close_restricted(struct libinput *libinput, int fd)
476 return libinput->interface->close_restricted(fd, libinput->user_data);
480 libinput_seat_init(struct libinput_seat *seat,
481 struct libinput *libinput,
483 libinput_seat_destroy_func destroy)
486 seat->libinput = libinput;
487 seat->name = strdup(name);
488 seat->destroy = destroy;
489 list_init(&seat->devices_list);
493 libinput_seat_ref(struct libinput_seat *seat)
499 libinput_seat_destroy(struct libinput_seat *seat)
501 list_remove(&seat->link);
507 libinput_seat_unref(struct libinput_seat *seat)
509 assert(seat->refcount > 0);
511 if (seat->refcount == 0)
512 libinput_seat_destroy(seat);
516 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
518 seat->user_data = user_data;
521 LIBINPUT_EXPORT void *
522 libinput_seat_get_user_data(struct libinput_seat *seat)
524 return seat->user_data;
527 LIBINPUT_EXPORT const char *
528 libinput_seat_get_name(struct libinput_seat *seat)
534 libinput_device_init(struct libinput_device *device,
535 struct libinput_seat *seat)
538 device->refcount = 1;
542 libinput_device_ref(struct libinput_device *device)
548 libinput_device_destroy(struct libinput_device *device)
550 evdev_device_destroy((struct evdev_device *) device);
554 libinput_device_unref(struct libinput_device *device)
556 assert(device->refcount > 0);
558 if (device->refcount == 0)
559 libinput_device_destroy(device);
563 libinput_get_fd(struct libinput *libinput)
565 return libinput->epoll_fd;
569 libinput_dispatch(struct libinput *libinput)
571 struct libinput_source *source;
572 struct epoll_event ep[32];
575 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
579 for (i = 0; i < count; ++i) {
580 source = ep[i].data.ptr;
581 if (source->fd == -1)
584 source->dispatch(source->user_data);
587 libinput_drop_destroyed_sources(libinput);
593 init_event_base(struct libinput_event *event,
594 enum libinput_event_type type,
595 union libinput_event_target target)
598 event->target = target;
602 post_base_event(struct libinput *libinput,
603 enum libinput_event_type type,
604 struct libinput_event *event)
606 init_event_base(event, type,
607 (union libinput_event_target) { .libinput = libinput });
608 libinput_post_event(libinput, event);
612 post_device_event(struct libinput_device *device,
613 enum libinput_event_type type,
614 struct libinput_event *event)
616 init_event_base(event, type,
617 (union libinput_event_target) { .device = device });
618 libinput_post_event(device->seat->libinput, event);
622 notify_added_seat(struct libinput_seat *seat)
624 struct libinput_event_added_seat *added_seat_event;
626 added_seat_event = malloc(sizeof *added_seat_event);
627 if (!added_seat_event)
630 libinput_seat_ref(seat);
632 *added_seat_event = (struct libinput_event_added_seat) {
636 post_base_event(seat->libinput,
637 LIBINPUT_EVENT_ADDED_SEAT,
638 &added_seat_event->base);
642 notify_removed_seat(struct libinput_seat *seat)
644 struct libinput_event_removed_seat *removed_seat_event;
646 removed_seat_event = malloc(sizeof *removed_seat_event);
647 if (!removed_seat_event)
650 libinput_seat_ref(seat);
652 *removed_seat_event = (struct libinput_event_removed_seat) {
656 post_base_event(seat->libinput,
657 LIBINPUT_EVENT_REMOVED_SEAT,
658 &removed_seat_event->base);
662 notify_added_device(struct libinput_device *device)
664 struct libinput_event_added_device *added_device_event;
666 added_device_event = malloc(sizeof *added_device_event);
667 if (!added_device_event)
670 *added_device_event = (struct libinput_event_added_device) {
674 post_base_event(device->seat->libinput,
675 LIBINPUT_EVENT_ADDED_DEVICE,
676 &added_device_event->base);
680 notify_removed_device(struct libinput_device *device)
682 struct libinput_event_removed_device *removed_device_event;
684 removed_device_event = malloc(sizeof *removed_device_event);
685 if (!removed_device_event)
688 *removed_device_event = (struct libinput_event_removed_device) {
692 post_base_event(device->seat->libinput,
693 LIBINPUT_EVENT_REMOVED_DEVICE,
694 &removed_device_event->base);
698 keyboard_notify_key(struct libinput_device *device,
701 enum libinput_keyboard_key_state state)
703 struct libinput_event_keyboard_key *key_event;
705 key_event = malloc(sizeof *key_event);
709 *key_event = (struct libinput_event_keyboard_key) {
715 post_device_event(device,
716 LIBINPUT_EVENT_KEYBOARD_KEY,
721 pointer_notify_motion(struct libinput_device *device,
726 struct libinput_event_pointer_motion *motion_event;
728 motion_event = malloc(sizeof *motion_event);
732 *motion_event = (struct libinput_event_pointer_motion) {
738 post_device_event(device,
739 LIBINPUT_EVENT_POINTER_MOTION,
740 &motion_event->base);
744 pointer_notify_motion_absolute(struct libinput_device *device,
749 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
751 motion_absolute_event = malloc(sizeof *motion_absolute_event);
752 if (!motion_absolute_event)
755 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
761 post_device_event(device,
762 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
763 &motion_absolute_event->base);
767 pointer_notify_button(struct libinput_device *device,
770 enum libinput_pointer_button_state state)
772 struct libinput_event_pointer_button *button_event;
774 button_event = malloc(sizeof *button_event);
778 *button_event = (struct libinput_event_pointer_button) {
784 post_device_event(device,
785 LIBINPUT_EVENT_POINTER_BUTTON,
786 &button_event->base);
790 pointer_notify_axis(struct libinput_device *device,
792 enum libinput_pointer_axis axis,
795 struct libinput_event_pointer_axis *axis_event;
797 axis_event = malloc(sizeof *axis_event);
801 *axis_event = (struct libinput_event_pointer_axis) {
807 post_device_event(device,
808 LIBINPUT_EVENT_POINTER_AXIS,
813 touch_notify_touch(struct libinput_device *device,
818 enum libinput_touch_type touch_type)
820 struct libinput_event_touch_touch *touch_event;
822 touch_event = malloc(sizeof *touch_event);
826 *touch_event = (struct libinput_event_touch_touch) {
831 .touch_type = touch_type,
834 post_device_event(device,
835 LIBINPUT_EVENT_TOUCH_TOUCH,
840 libinput_post_event(struct libinput *libinput,
841 struct libinput_event *event)
843 struct libinput_event **events = libinput->events;
844 size_t events_len = libinput->events_len;
845 size_t events_count = libinput->events_count;
850 if (events_count > events_len) {
852 events = realloc(events, events_len * sizeof *events);
854 fprintf(stderr, "Failed to reallocate event ring "
859 if (libinput->events_count > 0 && libinput->events_in == 0) {
860 libinput->events_in = libinput->events_len;
861 } else if (libinput->events_count > 0 &&
862 libinput->events_out >= libinput->events_in) {
863 move_len = libinput->events_len - libinput->events_out;
864 new_out = events_len - move_len;
865 memmove(events + new_out,
866 events + libinput->events_out,
867 move_len * sizeof *events);
868 libinput->events_out = new_out;
871 libinput->events = events;
872 libinput->events_len = events_len;
875 switch (libinput_event_get_class(event)) {
876 case LIBINPUT_EVENT_CLASS_BASE:
878 case LIBINPUT_EVENT_CLASS_SEAT:
879 libinput_seat_ref(event->target.seat);
881 case LIBINPUT_EVENT_CLASS_DEVICE:
882 libinput_device_ref(event->target.device);
886 libinput->events_count = events_count;
887 events[libinput->events_in] = event;
888 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
891 LIBINPUT_EXPORT struct libinput_event *
892 libinput_get_event(struct libinput *libinput)
894 struct libinput_event *event;
896 if (libinput->events_count == 0)
899 event = libinput->events[libinput->events_out];
900 libinput->events_out =
901 (libinput->events_out + 1) % libinput->events_len;
902 libinput->events_count--;
907 LIBINPUT_EXPORT void *
908 libinput_get_user_data(struct libinput *libinput)
910 return libinput->user_data;
914 libinput_resume(struct libinput *libinput)
916 return libinput->interface_backend->resume(libinput);
920 libinput_suspend(struct libinput *libinput)
922 libinput->interface_backend->suspend(libinput);
926 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
928 device->user_data = user_data;
931 LIBINPUT_EXPORT void *
932 libinput_device_get_user_data(struct libinput_device *device)
934 return device->user_data;
937 LIBINPUT_EXPORT const char *
938 libinput_device_get_sysname(struct libinput_device *device)
940 return evdev_device_get_sysname((struct evdev_device *) device);
943 LIBINPUT_EXPORT const char *
944 libinput_device_get_output_name(struct libinput_device *device)
946 return evdev_device_get_output((struct evdev_device *) device);
949 LIBINPUT_EXPORT struct libinput_seat *
950 libinput_device_get_seat(struct libinput_device *device)
956 libinput_device_led_update(struct libinput_device *device,
957 enum libinput_led leds)
959 evdev_device_led_update((struct evdev_device *) device, leds);
963 libinput_device_get_keys(struct libinput_device *device,
964 char *keys, size_t size)
966 return evdev_device_get_keys((struct evdev_device *) device,
972 libinput_device_calibrate(struct libinput_device *device,
973 float calibration[6])
975 evdev_device_calibrate((struct evdev_device *) device, calibration);
979 libinput_device_has_capability(struct libinput_device *device,
980 enum libinput_device_capability capability)
982 return evdev_device_has_capability((struct evdev_device *) device,