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_device_register_capability {
76 struct libinput_event base;
77 enum libinput_device_capability capability;
80 struct libinput_event_device_unregister_capability {
81 struct libinput_event base;
82 enum libinput_device_capability capability;
85 struct libinput_event_keyboard_key {
86 struct libinput_event base;
89 enum libinput_keyboard_key_state state;
92 struct libinput_event_pointer_motion {
93 struct libinput_event base;
99 struct libinput_event_pointer_motion_absolute {
100 struct libinput_event base;
106 struct libinput_event_pointer_button {
107 struct libinput_event base;
110 enum libinput_pointer_button_state state;
113 struct libinput_event_pointer_axis {
114 struct libinput_event base;
116 enum libinput_pointer_axis axis;
120 struct libinput_event_touch_touch {
121 struct libinput_event base;
126 enum libinput_touch_type touch_type;
130 libinput_post_event(struct libinput *libinput,
131 struct libinput_event *event);
133 LIBINPUT_EXPORT enum libinput_event_type
134 libinput_event_get_type(struct libinput_event *event)
139 LIBINPUT_EXPORT union libinput_event_target
140 libinput_event_get_target(struct libinput_event *event)
142 return event->target;
145 LIBINPUT_EXPORT struct libinput_seat *
146 libinput_event_added_seat_get_seat(struct libinput_event_added_seat *event)
151 LIBINPUT_EXPORT struct libinput_seat *
152 libinput_event_removed_seat_get_seat(struct libinput_event_removed_seat *event)
157 LIBINPUT_EXPORT struct libinput_device *
158 libinput_event_added_device_get_device(
159 struct libinput_event_added_device *event)
161 return event->device;
164 LIBINPUT_EXPORT struct libinput_device *
165 libinput_event_removed_device_get_device(
166 struct libinput_event_removed_device *event)
168 return event->device;
171 LIBINPUT_EXPORT enum libinput_device_capability
172 libinput_event_device_register_capability_get_capability(
173 struct libinput_event_device_register_capability *event)
175 return event->capability;
178 LIBINPUT_EXPORT enum libinput_device_capability
179 libinput_event_device_unregister_capability_get_capability(
180 struct libinput_event_device_unregister_capability *event)
182 return event->capability;
185 LIBINPUT_EXPORT uint32_t
186 libinput_event_keyboard_key_get_time(
187 struct libinput_event_keyboard_key *event)
192 LIBINPUT_EXPORT uint32_t
193 libinput_event_keyboard_key_get_key(
194 struct libinput_event_keyboard_key *event)
199 LIBINPUT_EXPORT enum libinput_keyboard_key_state
200 libinput_event_keyboard_key_get_state(
201 struct libinput_event_keyboard_key *event)
206 LIBINPUT_EXPORT uint32_t
207 libinput_event_pointer_motion_get_time(
208 struct libinput_event_pointer_motion *event)
213 LIBINPUT_EXPORT li_fixed_t
214 libinput_event_pointer_motion_get_dx(
215 struct libinput_event_pointer_motion *event)
220 LIBINPUT_EXPORT li_fixed_t
221 libinput_event_pointer_motion_get_dy(
222 struct libinput_event_pointer_motion *event)
227 LIBINPUT_EXPORT uint32_t
228 libinput_event_pointer_motion_absolute_get_time(
229 struct libinput_event_pointer_motion_absolute *event)
234 LIBINPUT_EXPORT li_fixed_t
235 libinput_event_pointer_motion_absolute_get_x(
236 struct libinput_event_pointer_motion_absolute *event)
241 LIBINPUT_EXPORT li_fixed_t
242 libinput_event_pointer_motion_absolute_get_y(
243 struct libinput_event_pointer_motion_absolute *event)
248 LIBINPUT_EXPORT uint32_t
249 libinput_event_pointer_button_get_time(
250 struct libinput_event_pointer_button *event)
255 LIBINPUT_EXPORT uint32_t
256 libinput_event_pointer_button_get_button(
257 struct libinput_event_pointer_button *event)
259 return event->button;
262 LIBINPUT_EXPORT enum libinput_pointer_button_state
263 libinput_event_pointer_button_get_state(
264 struct libinput_event_pointer_button *event)
269 LIBINPUT_EXPORT uint32_t
270 libinput_event_pointer_axis_get_time(
271 struct libinput_event_pointer_axis *event)
276 LIBINPUT_EXPORT enum libinput_pointer_axis
277 libinput_event_pointer_axis_get_axis(
278 struct libinput_event_pointer_axis *event)
283 LIBINPUT_EXPORT li_fixed_t
284 libinput_event_pointer_axis_get_value(
285 struct libinput_event_pointer_axis *event)
290 LIBINPUT_EXPORT uint32_t
291 libinput_event_touch_touch_get_time(
292 struct libinput_event_touch_touch *event)
297 LIBINPUT_EXPORT uint32_t
298 libinput_event_touch_touch_get_slot(
299 struct libinput_event_touch_touch *event)
304 LIBINPUT_EXPORT li_fixed_t
305 libinput_event_touch_touch_get_x(
306 struct libinput_event_touch_touch *event)
311 LIBINPUT_EXPORT li_fixed_t
312 libinput_event_touch_touch_get_y(
313 struct libinput_event_touch_touch *event)
318 LIBINPUT_EXPORT enum libinput_touch_type
319 libinput_event_touch_touch_get_touch_type(
320 struct libinput_event_touch_touch *event)
322 return event->touch_type;
325 struct libinput_source *
326 libinput_add_fd(struct libinput *libinput,
328 libinput_source_dispatch_t dispatch,
331 struct libinput_source *source;
332 struct epoll_event ep;
334 source = malloc(sizeof *source);
338 source->dispatch = dispatch;
339 source->user_data = user_data;
342 memset(&ep, 0, sizeof ep);
344 ep.data.ptr = source;
346 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
356 libinput_remove_source(struct libinput *libinput,
357 struct libinput_source *source)
359 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
362 list_insert(&libinput->source_destroy_list, &source->link);
366 libinput_init(struct libinput *libinput,
367 const struct libinput_interface *interface,
368 const struct libinput_interface_backend *interface_backend,
371 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
372 if (libinput->epoll_fd < 0)
375 libinput->events_len = 4;
376 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
377 if (!libinput->events) {
378 close(libinput->epoll_fd);
382 libinput->interface = interface;
383 libinput->interface_backend = interface_backend;
384 libinput->user_data = user_data;
385 list_init(&libinput->source_destroy_list);
386 list_init(&libinput->seat_list);
392 libinput_device_destroy(struct libinput_device *device);
395 libinput_seat_destroy(struct libinput_seat *seat);
398 libinput_drop_destroyed_sources(struct libinput *libinput)
400 struct libinput_source *source, *next;
402 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
404 list_init(&libinput->source_destroy_list);
408 libinput_destroy(struct libinput *libinput)
410 struct libinput_event *event;
411 struct libinput_device *device, *next_device;
412 struct libinput_seat *seat, *next_seat;
414 if (libinput == NULL)
417 libinput->interface_backend->destroy(libinput);
419 while ((event = libinput_get_event(libinput)))
420 libinput_event_destroy(event);
422 libinput_drop_destroyed_sources(libinput);
424 free(libinput->events);
426 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
427 list_for_each_safe(device, next_device,
430 libinput_device_destroy(device);
432 libinput_seat_destroy(seat);
435 close(libinput->epoll_fd);
439 static enum libinput_event_class
440 libinput_event_get_class(struct libinput_event *event)
442 switch (event->type) {
443 case LIBINPUT_EVENT_ADDED_SEAT:
444 case LIBINPUT_EVENT_REMOVED_SEAT:
445 case LIBINPUT_EVENT_ADDED_DEVICE:
446 case LIBINPUT_EVENT_REMOVED_DEVICE:
447 return LIBINPUT_EVENT_CLASS_BASE;
449 case LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY:
450 case LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY:
451 case LIBINPUT_EVENT_KEYBOARD_KEY:
452 case LIBINPUT_EVENT_POINTER_MOTION:
453 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
454 case LIBINPUT_EVENT_POINTER_BUTTON:
455 case LIBINPUT_EVENT_POINTER_AXIS:
456 case LIBINPUT_EVENT_TOUCH_TOUCH:
457 return LIBINPUT_EVENT_CLASS_DEVICE;
460 /* We should never end up here. */
465 libinput_event_destroy(struct libinput_event *event)
470 switch (libinput_event_get_class(event)) {
471 case LIBINPUT_EVENT_CLASS_BASE:
473 case LIBINPUT_EVENT_CLASS_SEAT:
474 libinput_seat_unref(event->target.seat);
476 case LIBINPUT_EVENT_CLASS_DEVICE:
477 libinput_device_unref(event->target.device);
481 if (libinput_event_get_type(event) == LIBINPUT_EVENT_ADDED_SEAT ||
482 libinput_event_get_type(event) == LIBINPUT_EVENT_REMOVED_SEAT)
483 libinput_seat_unref(((struct libinput_event_added_seat*)event)->seat);
489 open_restricted(struct libinput *libinput,
490 const char *path, int flags)
492 return libinput->interface->open_restricted(path,
494 libinput->user_data);
498 close_restricted(struct libinput *libinput, int fd)
500 return libinput->interface->close_restricted(fd, libinput->user_data);
504 libinput_seat_init(struct libinput_seat *seat,
505 struct libinput *libinput,
507 libinput_seat_destroy_func destroy)
510 seat->libinput = libinput;
511 seat->name = strdup(name);
512 seat->destroy = destroy;
513 list_init(&seat->devices_list);
517 libinput_seat_ref(struct libinput_seat *seat)
523 libinput_seat_destroy(struct libinput_seat *seat)
525 list_remove(&seat->link);
531 libinput_seat_unref(struct libinput_seat *seat)
533 assert(seat->refcount > 0);
535 if (seat->refcount == 0)
536 libinput_seat_destroy(seat);
540 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
542 seat->user_data = user_data;
545 LIBINPUT_EXPORT void *
546 libinput_seat_get_user_data(struct libinput_seat *seat)
548 return seat->user_data;
551 LIBINPUT_EXPORT const char *
552 libinput_seat_get_name(struct libinput_seat *seat)
558 libinput_device_init(struct libinput_device *device,
559 struct libinput_seat *seat)
562 device->refcount = 1;
566 libinput_device_ref(struct libinput_device *device)
572 libinput_device_destroy(struct libinput_device *device)
574 evdev_device_destroy((struct evdev_device *) device);
578 libinput_device_unref(struct libinput_device *device)
580 assert(device->refcount > 0);
582 if (device->refcount == 0)
583 libinput_device_destroy(device);
587 libinput_get_fd(struct libinput *libinput)
589 return libinput->epoll_fd;
593 libinput_dispatch(struct libinput *libinput)
595 struct libinput_source *source;
596 struct epoll_event ep[32];
599 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
603 for (i = 0; i < count; ++i) {
604 source = ep[i].data.ptr;
605 if (source->fd == -1)
608 source->dispatch(source->user_data);
611 libinput_drop_destroyed_sources(libinput);
617 init_event_base(struct libinput_event *event,
618 enum libinput_event_type type,
619 union libinput_event_target target)
622 event->target = target;
626 post_base_event(struct libinput *libinput,
627 enum libinput_event_type type,
628 struct libinput_event *event)
630 init_event_base(event, type,
631 (union libinput_event_target) { .libinput = libinput });
632 libinput_post_event(libinput, event);
636 post_device_event(struct libinput_device *device,
637 enum libinput_event_type type,
638 struct libinput_event *event)
640 init_event_base(event, type,
641 (union libinput_event_target) { .device = device });
642 libinput_post_event(device->seat->libinput, event);
646 notify_added_seat(struct libinput_seat *seat)
648 struct libinput_event_added_seat *added_seat_event;
650 added_seat_event = malloc(sizeof *added_seat_event);
651 if (!added_seat_event)
654 libinput_seat_ref(seat);
656 *added_seat_event = (struct libinput_event_added_seat) {
660 post_base_event(seat->libinput,
661 LIBINPUT_EVENT_ADDED_SEAT,
662 &added_seat_event->base);
666 notify_removed_seat(struct libinput_seat *seat)
668 struct libinput_event_removed_seat *removed_seat_event;
670 removed_seat_event = malloc(sizeof *removed_seat_event);
671 if (!removed_seat_event)
674 libinput_seat_ref(seat);
676 *removed_seat_event = (struct libinput_event_removed_seat) {
680 post_base_event(seat->libinput,
681 LIBINPUT_EVENT_REMOVED_SEAT,
682 &removed_seat_event->base);
686 notify_added_device(struct libinput_device *device)
688 struct libinput_event_added_device *added_device_event;
690 added_device_event = malloc(sizeof *added_device_event);
691 if (!added_device_event)
694 *added_device_event = (struct libinput_event_added_device) {
698 post_base_event(device->seat->libinput,
699 LIBINPUT_EVENT_ADDED_DEVICE,
700 &added_device_event->base);
704 notify_removed_device(struct libinput_device *device)
706 struct libinput_event_removed_device *removed_device_event;
708 removed_device_event = malloc(sizeof *removed_device_event);
709 if (!removed_device_event)
712 *removed_device_event = (struct libinput_event_removed_device) {
716 post_base_event(device->seat->libinput,
717 LIBINPUT_EVENT_REMOVED_DEVICE,
718 &removed_device_event->base);
722 device_register_capability(struct libinput_device *device,
723 enum libinput_device_capability capability)
725 struct libinput_event_device_register_capability *capability_event;
727 capability_event = malloc(sizeof *capability_event);
729 *capability_event = (struct libinput_event_device_register_capability) {
730 .capability = capability,
733 post_device_event(device,
734 LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
735 &capability_event->base);
739 device_unregister_capability(struct libinput_device *device,
740 enum libinput_device_capability capability)
742 struct libinput_event_device_unregister_capability *capability_event;
744 capability_event = malloc(sizeof *capability_event);
746 *capability_event = (struct libinput_event_device_unregister_capability) {
747 .capability = capability,
750 post_device_event(device,
751 LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
752 &capability_event->base);
756 keyboard_notify_key(struct libinput_device *device,
759 enum libinput_keyboard_key_state state)
761 struct libinput_event_keyboard_key *key_event;
763 key_event = malloc(sizeof *key_event);
767 *key_event = (struct libinput_event_keyboard_key) {
773 post_device_event(device,
774 LIBINPUT_EVENT_KEYBOARD_KEY,
779 pointer_notify_motion(struct libinput_device *device,
784 struct libinput_event_pointer_motion *motion_event;
786 motion_event = malloc(sizeof *motion_event);
790 *motion_event = (struct libinput_event_pointer_motion) {
796 post_device_event(device,
797 LIBINPUT_EVENT_POINTER_MOTION,
798 &motion_event->base);
802 pointer_notify_motion_absolute(struct libinput_device *device,
807 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
809 motion_absolute_event = malloc(sizeof *motion_absolute_event);
810 if (!motion_absolute_event)
813 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
819 post_device_event(device,
820 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
821 &motion_absolute_event->base);
825 pointer_notify_button(struct libinput_device *device,
828 enum libinput_pointer_button_state state)
830 struct libinput_event_pointer_button *button_event;
832 button_event = malloc(sizeof *button_event);
836 *button_event = (struct libinput_event_pointer_button) {
842 post_device_event(device,
843 LIBINPUT_EVENT_POINTER_BUTTON,
844 &button_event->base);
848 pointer_notify_axis(struct libinput_device *device,
850 enum libinput_pointer_axis axis,
853 struct libinput_event_pointer_axis *axis_event;
855 axis_event = malloc(sizeof *axis_event);
859 *axis_event = (struct libinput_event_pointer_axis) {
865 post_device_event(device,
866 LIBINPUT_EVENT_POINTER_AXIS,
871 touch_notify_touch(struct libinput_device *device,
876 enum libinput_touch_type touch_type)
878 struct libinput_event_touch_touch *touch_event;
880 touch_event = malloc(sizeof *touch_event);
884 *touch_event = (struct libinput_event_touch_touch) {
889 .touch_type = touch_type,
892 post_device_event(device,
893 LIBINPUT_EVENT_TOUCH_TOUCH,
898 libinput_post_event(struct libinput *libinput,
899 struct libinput_event *event)
901 struct libinput_event **events = libinput->events;
902 size_t events_len = libinput->events_len;
903 size_t events_count = libinput->events_count;
908 if (events_count > events_len) {
910 events = realloc(events, events_len * sizeof *events);
912 fprintf(stderr, "Failed to reallocate event ring "
917 if (libinput->events_count > 0 && libinput->events_in == 0) {
918 libinput->events_in = libinput->events_len;
919 } else if (libinput->events_count > 0 &&
920 libinput->events_out >= libinput->events_in) {
921 move_len = libinput->events_len - libinput->events_out;
922 new_out = events_len - move_len;
923 memmove(events + new_out,
924 events + libinput->events_out,
925 move_len * sizeof *events);
926 libinput->events_out = new_out;
929 libinput->events = events;
930 libinput->events_len = events_len;
933 switch (libinput_event_get_class(event)) {
934 case LIBINPUT_EVENT_CLASS_BASE:
936 case LIBINPUT_EVENT_CLASS_SEAT:
937 libinput_seat_ref(event->target.seat);
939 case LIBINPUT_EVENT_CLASS_DEVICE:
940 libinput_device_ref(event->target.device);
944 libinput->events_count = events_count;
945 events[libinput->events_in] = event;
946 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
949 LIBINPUT_EXPORT struct libinput_event *
950 libinput_get_event(struct libinput *libinput)
952 struct libinput_event *event;
954 if (libinput->events_count == 0)
957 event = libinput->events[libinput->events_out];
958 libinput->events_out =
959 (libinput->events_out + 1) % libinput->events_len;
960 libinput->events_count--;
965 LIBINPUT_EXPORT void *
966 libinput_get_user_data(struct libinput *libinput)
968 return libinput->user_data;
972 libinput_resume(struct libinput *libinput)
974 return libinput->interface_backend->resume(libinput);
978 libinput_suspend(struct libinput *libinput)
980 libinput->interface_backend->suspend(libinput);
984 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
986 device->user_data = user_data;
989 LIBINPUT_EXPORT void *
990 libinput_device_get_user_data(struct libinput_device *device)
992 return device->user_data;
995 LIBINPUT_EXPORT const char *
996 libinput_device_get_sysname(struct libinput_device *device)
998 return evdev_device_get_sysname((struct evdev_device *) device);
1001 LIBINPUT_EXPORT const char *
1002 libinput_device_get_output_name(struct libinput_device *device)
1004 return evdev_device_get_output((struct evdev_device *) device);
1007 LIBINPUT_EXPORT struct libinput_seat *
1008 libinput_device_get_seat(struct libinput_device *device)
1010 return device->seat;
1013 LIBINPUT_EXPORT void
1014 libinput_device_led_update(struct libinput_device *device,
1015 enum libinput_led leds)
1017 evdev_device_led_update((struct evdev_device *) device, leds);
1021 libinput_device_get_keys(struct libinput_device *device,
1022 char *keys, size_t size)
1024 return evdev_device_get_keys((struct evdev_device *) device,
1029 LIBINPUT_EXPORT void
1030 libinput_device_calibrate(struct libinput_device *device,
1031 float calibration[6])
1033 evdev_device_calibrate((struct evdev_device *) device, calibration);
1037 libinput_device_has_capability(struct libinput_device *device,
1038 enum libinput_device_capability capability)
1040 return evdev_device_has_capability((struct evdev_device *) device,