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;
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_device_register_capability {
77 struct libinput_event base;
78 enum libinput_device_capability capability;
81 struct libinput_event_device_unregister_capability {
82 struct libinput_event base;
83 enum libinput_device_capability capability;
86 struct libinput_event_keyboard_key {
87 struct libinput_event base;
90 enum libinput_keyboard_key_state state;
93 struct libinput_event_pointer_motion {
94 struct libinput_event base;
100 struct libinput_event_pointer_motion_absolute {
101 struct libinput_event base;
107 struct libinput_event_pointer_button {
108 struct libinput_event base;
111 enum libinput_pointer_button_state state;
114 struct libinput_event_pointer_axis {
115 struct libinput_event base;
117 enum libinput_pointer_axis axis;
121 struct libinput_event_touch_touch {
122 struct libinput_event base;
127 enum libinput_touch_type touch_type;
131 libinput_post_event(struct libinput *libinput,
132 struct libinput_event *event);
134 LIBINPUT_EXPORT enum libinput_event_type
135 libinput_event_get_type(struct libinput_event *event)
140 LIBINPUT_EXPORT union libinput_event_target
141 libinput_event_get_target(struct libinput_event *event)
143 return event->target;
146 LIBINPUT_EXPORT struct libinput_seat *
147 libinput_event_added_seat_get_seat(struct libinput_event_added_seat *event)
152 LIBINPUT_EXPORT struct libinput_seat *
153 libinput_event_removed_seat_get_seat(struct libinput_event_removed_seat *event)
158 LIBINPUT_EXPORT struct libinput_device *
159 libinput_event_added_device_get_device(
160 struct libinput_event_added_device *event)
162 return event->device;
165 LIBINPUT_EXPORT struct libinput_device *
166 libinput_event_removed_device_get_device(
167 struct libinput_event_removed_device *event)
169 return event->device;
172 LIBINPUT_EXPORT enum libinput_device_capability
173 libinput_event_device_register_capability_get_capability(
174 struct libinput_event_device_register_capability *event)
176 return event->capability;
179 LIBINPUT_EXPORT enum libinput_device_capability
180 libinput_event_device_unregister_capability_get_capability(
181 struct libinput_event_device_unregister_capability *event)
183 return event->capability;
186 LIBINPUT_EXPORT uint32_t
187 libinput_event_keyboard_key_get_time(
188 struct libinput_event_keyboard_key *event)
193 LIBINPUT_EXPORT uint32_t
194 libinput_event_keyboard_key_get_key(
195 struct libinput_event_keyboard_key *event)
200 LIBINPUT_EXPORT enum libinput_keyboard_key_state
201 libinput_event_keyboard_key_get_state(
202 struct libinput_event_keyboard_key *event)
207 LIBINPUT_EXPORT uint32_t
208 libinput_event_pointer_motion_get_time(
209 struct libinput_event_pointer_motion *event)
214 LIBINPUT_EXPORT li_fixed_t
215 libinput_event_pointer_motion_get_dx(
216 struct libinput_event_pointer_motion *event)
221 LIBINPUT_EXPORT li_fixed_t
222 libinput_event_pointer_motion_get_dy(
223 struct libinput_event_pointer_motion *event)
228 LIBINPUT_EXPORT uint32_t
229 libinput_event_pointer_motion_absolute_get_time(
230 struct libinput_event_pointer_motion_absolute *event)
235 LIBINPUT_EXPORT li_fixed_t
236 libinput_event_pointer_motion_absolute_get_x(
237 struct libinput_event_pointer_motion_absolute *event)
242 LIBINPUT_EXPORT li_fixed_t
243 libinput_event_pointer_motion_absolute_get_y(
244 struct libinput_event_pointer_motion_absolute *event)
249 LIBINPUT_EXPORT uint32_t
250 libinput_event_pointer_button_get_time(
251 struct libinput_event_pointer_button *event)
256 LIBINPUT_EXPORT uint32_t
257 libinput_event_pointer_button_get_button(
258 struct libinput_event_pointer_button *event)
260 return event->button;
263 LIBINPUT_EXPORT enum libinput_pointer_button_state
264 libinput_event_pointer_button_get_state(
265 struct libinput_event_pointer_button *event)
270 LIBINPUT_EXPORT uint32_t
271 libinput_event_pointer_axis_get_time(
272 struct libinput_event_pointer_axis *event)
277 LIBINPUT_EXPORT enum libinput_pointer_axis
278 libinput_event_pointer_axis_get_axis(
279 struct libinput_event_pointer_axis *event)
284 LIBINPUT_EXPORT li_fixed_t
285 libinput_event_pointer_axis_get_value(
286 struct libinput_event_pointer_axis *event)
291 LIBINPUT_EXPORT uint32_t
292 libinput_event_touch_touch_get_time(
293 struct libinput_event_touch_touch *event)
298 LIBINPUT_EXPORT uint32_t
299 libinput_event_touch_touch_get_slot(
300 struct libinput_event_touch_touch *event)
305 LIBINPUT_EXPORT li_fixed_t
306 libinput_event_touch_touch_get_x(
307 struct libinput_event_touch_touch *event)
312 LIBINPUT_EXPORT li_fixed_t
313 libinput_event_touch_touch_get_y(
314 struct libinput_event_touch_touch *event)
319 LIBINPUT_EXPORT enum libinput_touch_type
320 libinput_event_touch_touch_get_touch_type(
321 struct libinput_event_touch_touch *event)
323 return event->touch_type;
326 struct libinput_source *
327 libinput_add_fd(struct libinput *libinput,
329 libinput_source_dispatch_t dispatch,
332 struct libinput_source *source;
333 struct epoll_event ep;
335 source = malloc(sizeof *source);
339 source->dispatch = dispatch;
340 source->user_data = user_data;
343 memset(&ep, 0, sizeof ep);
345 ep.data.ptr = source;
347 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
357 libinput_remove_source(struct libinput *libinput,
358 struct libinput_source *source)
360 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
363 list_insert(&libinput->source_destroy_list, &source->link);
367 libinput_init(struct libinput *libinput,
368 const struct libinput_interface *interface,
371 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
372 if (libinput->epoll_fd < 0)
375 libinput->interface = interface;
376 libinput->user_data = user_data;
377 list_init(&libinput->source_destroy_list);
378 list_init(&libinput->seat_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 while ((event = libinput_get_event(libinput)))
391 libinput_event_destroy(event);
392 free(libinput->events);
394 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
395 list_for_each_safe(device, next_device,
398 libinput_device_unref(device);
400 libinput_seat_unref(seat);
403 close(libinput->epoll_fd);
407 static enum libinput_event_class
408 libinput_event_get_class(struct libinput_event *event)
410 switch (event->type) {
411 case LIBINPUT_EVENT_ADDED_SEAT:
412 case LIBINPUT_EVENT_REMOVED_SEAT:
413 case LIBINPUT_EVENT_ADDED_DEVICE:
414 case LIBINPUT_EVENT_REMOVED_DEVICE:
415 return LIBINPUT_EVENT_CLASS_BASE;
417 case LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY:
418 case LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY:
419 case LIBINPUT_EVENT_KEYBOARD_KEY:
420 case LIBINPUT_EVENT_POINTER_MOTION:
421 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
422 case LIBINPUT_EVENT_POINTER_BUTTON:
423 case LIBINPUT_EVENT_POINTER_AXIS:
424 case LIBINPUT_EVENT_TOUCH_TOUCH:
425 return LIBINPUT_EVENT_CLASS_DEVICE;
428 /* We should never end up here. */
433 libinput_event_destroy(struct libinput_event *event)
438 switch (libinput_event_get_class(event)) {
439 case LIBINPUT_EVENT_CLASS_BASE:
441 case LIBINPUT_EVENT_CLASS_SEAT:
442 libinput_seat_unref(event->target.seat);
444 case LIBINPUT_EVENT_CLASS_DEVICE:
445 libinput_device_unref(event->target.device);
453 open_restricted(struct libinput *libinput,
454 const char *path, int flags)
456 return libinput->interface->open_restricted(path,
458 libinput->user_data);
462 close_restricted(struct libinput *libinput, int fd)
464 return libinput->interface->close_restricted(fd, libinput->user_data);
468 libinput_seat_init(struct libinput_seat *seat,
469 struct libinput *libinput,
473 seat->libinput = libinput;
474 seat->name = strdup(name);
475 list_init(&seat->devices_list);
479 libinput_seat_ref(struct libinput_seat *seat)
485 libinput_seat_unref(struct libinput_seat *seat)
488 if (seat->refcount == 0) {
490 udev_seat_destroy((struct udev_seat *) seat);
495 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
497 seat->user_data = user_data;
500 LIBINPUT_EXPORT void *
501 libinput_seat_get_user_data(struct libinput_seat *seat)
503 return seat->user_data;
506 LIBINPUT_EXPORT const char *
507 libinput_seat_get_name(struct libinput_seat *seat)
513 libinput_device_init(struct libinput_device *device,
514 struct libinput_seat *seat)
517 device->refcount = 1;
521 libinput_device_ref(struct libinput_device *device)
527 libinput_device_unref(struct libinput_device *device)
530 if (device->refcount == 0)
531 evdev_device_destroy((struct evdev_device *) device);
535 libinput_get_fd(struct libinput *libinput)
537 return libinput->epoll_fd;
541 libinput_dispatch(struct libinput *libinput)
543 struct libinput_source *source, *next;
544 struct epoll_event ep[32];
547 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
551 for (i = 0; i < count; ++i) {
552 source = ep[i].data.ptr;
553 if (source->fd == -1)
556 source->dispatch(source->user_data);
559 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
561 list_init(&libinput->source_destroy_list);
567 init_event_base(struct libinput_event *event,
568 enum libinput_event_type type,
569 union libinput_event_target target)
572 event->target = target;
576 post_base_event(struct libinput *libinput,
577 enum libinput_event_type type,
578 struct libinput_event *event)
580 init_event_base(event, type,
581 (union libinput_event_target) { .libinput = libinput });
582 libinput_post_event(libinput, event);
586 post_device_event(struct libinput_device *device,
587 enum libinput_event_type type,
588 struct libinput_event *event)
590 init_event_base(event, type,
591 (union libinput_event_target) { .device = device });
592 libinput_post_event(device->seat->libinput, event);
596 notify_added_seat(struct libinput_seat *seat)
598 struct libinput_event_added_seat *added_seat_event;
600 added_seat_event = malloc(sizeof *added_seat_event);
601 if (!added_seat_event)
604 *added_seat_event = (struct libinput_event_added_seat) {
608 post_base_event(seat->libinput,
609 LIBINPUT_EVENT_ADDED_SEAT,
610 &added_seat_event->base);
614 notify_removed_seat(struct libinput_seat *seat)
616 struct libinput_event_removed_seat *removed_seat_event;
618 removed_seat_event = malloc(sizeof *removed_seat_event);
619 if (!removed_seat_event)
622 *removed_seat_event = (struct libinput_event_removed_seat) {
626 post_base_event(seat->libinput,
627 LIBINPUT_EVENT_REMOVED_SEAT,
628 &removed_seat_event->base);
632 notify_added_device(struct libinput_device *device)
634 struct libinput_event_added_device *added_device_event;
636 added_device_event = malloc(sizeof *added_device_event);
637 if (!added_device_event)
640 *added_device_event = (struct libinput_event_added_device) {
644 post_base_event(device->seat->libinput,
645 LIBINPUT_EVENT_ADDED_DEVICE,
646 &added_device_event->base);
650 notify_removed_device(struct libinput_device *device)
652 struct libinput_event_removed_device *removed_device_event;
654 removed_device_event = malloc(sizeof *removed_device_event);
655 if (!removed_device_event)
658 *removed_device_event = (struct libinput_event_removed_device) {
662 post_base_event(device->seat->libinput,
663 LIBINPUT_EVENT_REMOVED_DEVICE,
664 &removed_device_event->base);
668 device_register_capability(struct libinput_device *device,
669 enum libinput_device_capability capability)
671 struct libinput_event_device_register_capability *capability_event;
673 capability_event = malloc(sizeof *capability_event);
675 *capability_event = (struct libinput_event_device_register_capability) {
676 .capability = capability,
679 post_device_event(device,
680 LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
681 &capability_event->base);
685 device_unregister_capability(struct libinput_device *device,
686 enum libinput_device_capability capability)
688 struct libinput_event_device_unregister_capability *capability_event;
690 capability_event = malloc(sizeof *capability_event);
692 *capability_event = (struct libinput_event_device_unregister_capability) {
693 .capability = capability,
696 post_device_event(device,
697 LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
698 &capability_event->base);
702 keyboard_notify_key(struct libinput_device *device,
705 enum libinput_keyboard_key_state state)
707 struct libinput_event_keyboard_key *key_event;
709 key_event = malloc(sizeof *key_event);
713 *key_event = (struct libinput_event_keyboard_key) {
719 post_device_event(device,
720 LIBINPUT_EVENT_KEYBOARD_KEY,
725 pointer_notify_motion(struct libinput_device *device,
730 struct libinput_event_pointer_motion *motion_event;
732 motion_event = malloc(sizeof *motion_event);
736 *motion_event = (struct libinput_event_pointer_motion) {
742 post_device_event(device,
743 LIBINPUT_EVENT_POINTER_MOTION,
744 &motion_event->base);
748 pointer_notify_motion_absolute(struct libinput_device *device,
753 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
755 motion_absolute_event = malloc(sizeof *motion_absolute_event);
756 if (!motion_absolute_event)
759 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
765 post_device_event(device,
766 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
767 &motion_absolute_event->base);
771 pointer_notify_button(struct libinput_device *device,
774 enum libinput_pointer_button_state state)
776 struct libinput_event_pointer_button *button_event;
778 button_event = malloc(sizeof *button_event);
782 *button_event = (struct libinput_event_pointer_button) {
788 post_device_event(device,
789 LIBINPUT_EVENT_POINTER_BUTTON,
790 &button_event->base);
794 pointer_notify_axis(struct libinput_device *device,
796 enum libinput_pointer_axis axis,
799 struct libinput_event_pointer_axis *axis_event;
801 axis_event = malloc(sizeof *axis_event);
805 *axis_event = (struct libinput_event_pointer_axis) {
811 post_device_event(device,
812 LIBINPUT_EVENT_POINTER_AXIS,
817 touch_notify_touch(struct libinput_device *device,
822 enum libinput_touch_type touch_type)
824 struct libinput_event_touch_touch *touch_event;
826 touch_event = malloc(sizeof *touch_event);
830 *touch_event = (struct libinput_event_touch_touch) {
835 .touch_type = touch_type,
838 post_device_event(device,
839 LIBINPUT_EVENT_TOUCH_TOUCH,
844 libinput_post_event(struct libinput *libinput,
845 struct libinput_event *event)
847 struct libinput_event **events = libinput->events;
848 size_t events_len = libinput->events_len;
849 size_t events_count = libinput->events_count;
854 if (events_count > events_len) {
859 events = realloc(events, events_len * sizeof *events);
861 fprintf(stderr, "Failed to reallocate event ring "
866 if (libinput->events_count > 0 && libinput->events_in == 0) {
867 libinput->events_in = libinput->events_len;
868 } else if (libinput->events_count > 0 &&
869 libinput->events_out >= libinput->events_in) {
870 move_len = libinput->events_len - libinput->events_out;
871 new_out = events_len - move_len;
872 memmove(events + new_out,
873 libinput->events + libinput->events_out,
874 move_len * sizeof *events);
875 libinput->events_out = new_out;
878 libinput->events = events;
879 libinput->events_len = events_len;
882 switch (libinput_event_get_class(event)) {
883 case LIBINPUT_EVENT_CLASS_BASE:
885 case LIBINPUT_EVENT_CLASS_SEAT:
886 libinput_seat_ref(event->target.seat);
888 case LIBINPUT_EVENT_CLASS_DEVICE:
889 libinput_device_ref(event->target.device);
893 libinput->events_count = events_count;
894 events[libinput->events_in] = event;
895 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
898 LIBINPUT_EXPORT struct libinput_event *
899 libinput_get_event(struct libinput *libinput)
901 struct libinput_event *event;
903 if (libinput->events_count == 0)
906 event = libinput->events[libinput->events_out];
907 libinput->events_out =
908 (libinput->events_out + 1) % libinput->events_len;
909 libinput->events_count--;
914 LIBINPUT_EXPORT void *
915 libinput_get_user_data(struct libinput *libinput)
917 return libinput->user_data;
921 libinput_resume(struct libinput *libinput)
923 return udev_input_enable((struct udev_input *) libinput);
927 libinput_suspend(struct libinput *libinput)
929 udev_input_disable((struct udev_input *) libinput);
933 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
935 device->user_data = user_data;
938 LIBINPUT_EXPORT void *
939 libinput_device_get_user_data(struct libinput_device *device)
941 return device->user_data;
944 LIBINPUT_EXPORT const char *
945 libinput_device_get_sysname(struct libinput_device *device)
947 return evdev_device_get_sysname((struct evdev_device *) device);
950 LIBINPUT_EXPORT const char *
951 libinput_device_get_output_name(struct libinput_device *device)
953 return evdev_device_get_output((struct evdev_device *) device);
956 LIBINPUT_EXPORT struct libinput_seat *
957 libinput_device_get_seat(struct libinput_device *device)
963 libinput_device_led_update(struct libinput_device *device,
964 enum libinput_led leds)
966 evdev_device_led_update((struct evdev_device *) device, leds);
970 libinput_device_get_keys(struct libinput_device *device,
971 char *keys, size_t size)
973 return evdev_device_get_keys((struct evdev_device *) device,
979 libinput_device_calibrate(struct libinput_device *device,
980 float calibration[6])
982 evdev_device_calibrate((struct evdev_device *) device, calibration);
986 libinput_device_has_capability(struct libinput_device *device,
987 enum libinput_device_capability capability)
989 return evdev_device_has_capability((struct evdev_device *) device,