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 struct libinput_source {
38 libinput_source_dispatch_t dispatch;
44 struct libinput_event {
45 enum libinput_event_type type;
46 struct libinput_device *device;
49 struct libinput_event_device_notify {
50 struct libinput_event base;
53 struct libinput_event_keyboard {
54 struct libinput_event base;
57 enum libinput_keyboard_key_state state;
60 struct libinput_event_pointer {
61 struct libinput_event base;
66 enum libinput_pointer_button_state state;
67 enum libinput_pointer_axis axis;
71 struct libinput_event_touch {
72 struct libinput_event base;
77 enum libinput_touch_type touch_type;
81 libinput_default_log_func(enum libinput_log_priority priority,
83 const char *format, va_list args)
88 case LIBINPUT_LOG_PRIORITY_DEBUG: prefix = "debug"; break;
89 case LIBINPUT_LOG_PRIORITY_INFO: prefix = "info"; break;
90 case LIBINPUT_LOG_PRIORITY_ERROR: prefix = "error"; break;
91 default: prefix="<invalid priority>"; break;
94 fprintf(stderr, "libinput %s: ", prefix);
95 vfprintf(stderr, format, args);
99 enum libinput_log_priority priority;
100 libinput_log_handler handler;
104 static struct log_data log_data = {
105 .priority = LIBINPUT_LOG_PRIORITY_ERROR,
106 .handler = libinput_default_log_func,
111 log_msg(enum libinput_log_priority priority, const char *format, ...)
115 if (log_data.handler && log_data.priority <= priority) {
116 va_start(args, format);
117 log_data.handler(priority, log_data.user_data, format, args);
123 libinput_log_set_priority(enum libinput_log_priority priority)
125 log_data.priority = priority;
128 LIBINPUT_EXPORT enum libinput_log_priority
129 libinput_log_get_priority(void)
131 return log_data.priority;
135 libinput_log_set_handler(libinput_log_handler log_handler,
138 log_data.handler = log_handler;
139 log_data.user_data = user_data;
143 libinput_post_event(struct libinput *libinput,
144 struct libinput_event *event);
146 LIBINPUT_EXPORT enum libinput_event_type
147 libinput_event_get_type(struct libinput_event *event)
152 LIBINPUT_EXPORT struct libinput *
153 libinput_event_get_context(struct libinput_event *event)
155 return event->device->seat->libinput;
158 LIBINPUT_EXPORT struct libinput_device *
159 libinput_event_get_device(struct libinput_event *event)
161 return event->device;
164 LIBINPUT_EXPORT struct libinput_event_pointer *
165 libinput_event_get_pointer_event(struct libinput_event *event)
167 switch (event->type) {
168 case LIBINPUT_EVENT_NONE:
169 abort(); /* not used as actual event type */
170 case LIBINPUT_EVENT_DEVICE_ADDED:
171 case LIBINPUT_EVENT_DEVICE_REMOVED:
172 case LIBINPUT_EVENT_KEYBOARD_KEY:
174 case LIBINPUT_EVENT_POINTER_MOTION:
175 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
176 case LIBINPUT_EVENT_POINTER_BUTTON:
177 case LIBINPUT_EVENT_POINTER_AXIS:
178 return (struct libinput_event_pointer *) event;
179 case LIBINPUT_EVENT_TOUCH_TOUCH:
180 case LIBINPUT_EVENT_TOUCH_FRAME:
187 LIBINPUT_EXPORT struct libinput_event_keyboard *
188 libinput_event_get_keyboard_event(struct libinput_event *event)
190 switch (event->type) {
191 case LIBINPUT_EVENT_NONE:
192 abort(); /* not used as actual event type */
193 case LIBINPUT_EVENT_DEVICE_ADDED:
194 case LIBINPUT_EVENT_DEVICE_REMOVED:
196 case LIBINPUT_EVENT_KEYBOARD_KEY:
197 return (struct libinput_event_keyboard *) event;
198 case LIBINPUT_EVENT_POINTER_MOTION:
199 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
200 case LIBINPUT_EVENT_POINTER_BUTTON:
201 case LIBINPUT_EVENT_POINTER_AXIS:
202 case LIBINPUT_EVENT_TOUCH_TOUCH:
203 case LIBINPUT_EVENT_TOUCH_FRAME:
210 LIBINPUT_EXPORT struct libinput_event_touch *
211 libinput_event_get_touch_event(struct libinput_event *event)
213 switch (event->type) {
214 case LIBINPUT_EVENT_NONE:
215 abort(); /* not used as actual event type */
216 case LIBINPUT_EVENT_DEVICE_ADDED:
217 case LIBINPUT_EVENT_DEVICE_REMOVED:
218 case LIBINPUT_EVENT_KEYBOARD_KEY:
219 case LIBINPUT_EVENT_POINTER_MOTION:
220 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
221 case LIBINPUT_EVENT_POINTER_BUTTON:
222 case LIBINPUT_EVENT_POINTER_AXIS:
224 case LIBINPUT_EVENT_TOUCH_TOUCH:
225 case LIBINPUT_EVENT_TOUCH_FRAME:
226 return (struct libinput_event_touch *) event;
232 LIBINPUT_EXPORT struct libinput_event_device_notify *
233 libinput_event_get_device_notify_event(struct libinput_event *event)
235 switch (event->type) {
236 case LIBINPUT_EVENT_NONE:
237 abort(); /* not used as actual event type */
238 case LIBINPUT_EVENT_DEVICE_ADDED:
239 case LIBINPUT_EVENT_DEVICE_REMOVED:
240 return (struct libinput_event_device_notify *) event;
241 case LIBINPUT_EVENT_KEYBOARD_KEY:
242 case LIBINPUT_EVENT_POINTER_MOTION:
243 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
244 case LIBINPUT_EVENT_POINTER_BUTTON:
245 case LIBINPUT_EVENT_POINTER_AXIS:
246 case LIBINPUT_EVENT_TOUCH_TOUCH:
247 case LIBINPUT_EVENT_TOUCH_FRAME:
254 LIBINPUT_EXPORT uint32_t
255 libinput_event_keyboard_get_time(struct libinput_event_keyboard *event)
260 LIBINPUT_EXPORT uint32_t
261 libinput_event_keyboard_get_key(struct libinput_event_keyboard *event)
266 LIBINPUT_EXPORT enum libinput_keyboard_key_state
267 libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event)
272 LIBINPUT_EXPORT uint32_t
273 libinput_event_pointer_get_time(struct libinput_event_pointer *event)
278 LIBINPUT_EXPORT li_fixed_t
279 libinput_event_pointer_get_dx(struct libinput_event_pointer *event)
284 LIBINPUT_EXPORT li_fixed_t
285 libinput_event_pointer_get_dy(struct libinput_event_pointer *event)
290 LIBINPUT_EXPORT li_fixed_t
291 libinput_event_pointer_get_absolute_x(struct libinput_event_pointer *event)
296 LIBINPUT_EXPORT li_fixed_t
297 libinput_event_pointer_get_absolute_y(struct libinput_event_pointer *event)
302 LIBINPUT_EXPORT li_fixed_t
303 libinput_event_pointer_get_absolute_x_transformed(
304 struct libinput_event_pointer *event,
307 struct evdev_device *device =
308 (struct evdev_device *) event->base.device;
310 return evdev_device_transform_x(device, event->x, width);
313 LIBINPUT_EXPORT li_fixed_t
314 libinput_event_pointer_get_absolute_y_transformed(
315 struct libinput_event_pointer *event,
318 struct evdev_device *device =
319 (struct evdev_device *) event->base.device;
321 return evdev_device_transform_y(device, event->y, height);
324 LIBINPUT_EXPORT uint32_t
325 libinput_event_pointer_get_button(struct libinput_event_pointer *event)
327 return event->button;
330 LIBINPUT_EXPORT enum libinput_pointer_button_state
331 libinput_event_pointer_get_button_state(struct libinput_event_pointer *event)
336 LIBINPUT_EXPORT enum libinput_pointer_axis
337 libinput_event_pointer_get_axis(struct libinput_event_pointer *event)
342 LIBINPUT_EXPORT li_fixed_t
343 libinput_event_pointer_get_axis_value(struct libinput_event_pointer *event)
348 LIBINPUT_EXPORT uint32_t
349 libinput_event_touch_get_time(struct libinput_event_touch *event)
354 LIBINPUT_EXPORT int32_t
355 libinput_event_touch_get_slot(struct libinput_event_touch *event)
360 LIBINPUT_EXPORT li_fixed_t
361 libinput_event_touch_get_x(struct libinput_event_touch *event)
366 LIBINPUT_EXPORT li_fixed_t
367 libinput_event_touch_get_x_transformed(struct libinput_event_touch *event,
370 struct evdev_device *device =
371 (struct evdev_device *) event->base.device;
373 return evdev_device_transform_x(device, event->x, width);
376 LIBINPUT_EXPORT li_fixed_t
377 libinput_event_touch_get_y_transformed(struct libinput_event_touch *event,
380 struct evdev_device *device =
381 (struct evdev_device *) event->base.device;
383 return evdev_device_transform_y(device, event->y, height);
386 LIBINPUT_EXPORT li_fixed_t
387 libinput_event_touch_get_y(struct libinput_event_touch *event)
392 LIBINPUT_EXPORT enum libinput_touch_type
393 libinput_event_touch_get_touch_type(struct libinput_event_touch *event)
395 return event->touch_type;
398 struct libinput_source *
399 libinput_add_fd(struct libinput *libinput,
401 libinput_source_dispatch_t dispatch,
404 struct libinput_source *source;
405 struct epoll_event ep;
407 source = malloc(sizeof *source);
411 source->dispatch = dispatch;
412 source->user_data = user_data;
415 memset(&ep, 0, sizeof ep);
417 ep.data.ptr = source;
419 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
429 libinput_remove_source(struct libinput *libinput,
430 struct libinput_source *source)
432 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
435 list_insert(&libinput->source_destroy_list, &source->link);
439 libinput_init(struct libinput *libinput,
440 const struct libinput_interface *interface,
441 const struct libinput_interface_backend *interface_backend,
444 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
445 if (libinput->epoll_fd < 0)
448 libinput->events_len = 4;
449 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
450 if (!libinput->events) {
451 close(libinput->epoll_fd);
455 libinput->interface = interface;
456 libinput->interface_backend = interface_backend;
457 libinput->user_data = user_data;
458 list_init(&libinput->source_destroy_list);
459 list_init(&libinput->seat_list);
465 libinput_device_destroy(struct libinput_device *device);
468 libinput_seat_destroy(struct libinput_seat *seat);
471 libinput_drop_destroyed_sources(struct libinput *libinput)
473 struct libinput_source *source, *next;
475 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
477 list_init(&libinput->source_destroy_list);
481 libinput_destroy(struct libinput *libinput)
483 struct libinput_event *event;
484 struct libinput_device *device, *next_device;
485 struct libinput_seat *seat, *next_seat;
487 if (libinput == NULL)
490 libinput_suspend(libinput);
492 libinput->interface_backend->destroy(libinput);
494 while ((event = libinput_get_event(libinput)))
495 libinput_event_destroy(event);
497 libinput_drop_destroyed_sources(libinput);
499 free(libinput->events);
501 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
502 list_for_each_safe(device, next_device,
505 libinput_device_destroy(device);
507 libinput_seat_destroy(seat);
510 close(libinput->epoll_fd);
515 libinput_event_destroy(struct libinput_event *event)
521 libinput_device_unref(event->device);
527 open_restricted(struct libinput *libinput,
528 const char *path, int flags)
530 return libinput->interface->open_restricted(path,
532 libinput->user_data);
536 close_restricted(struct libinput *libinput, int fd)
538 return libinput->interface->close_restricted(fd, libinput->user_data);
542 libinput_seat_init(struct libinput_seat *seat,
543 struct libinput *libinput,
544 const char *physical_name,
545 const char *logical_name,
546 libinput_seat_destroy_func destroy)
549 seat->libinput = libinput;
550 seat->physical_name = strdup(physical_name);
551 seat->logical_name = strdup(logical_name);
552 seat->destroy = destroy;
553 list_init(&seat->devices_list);
554 list_insert(&libinput->seat_list, &seat->link);
558 libinput_seat_ref(struct libinput_seat *seat)
564 libinput_seat_destroy(struct libinput_seat *seat)
566 list_remove(&seat->link);
567 free(seat->logical_name);
568 free(seat->physical_name);
573 libinput_seat_unref(struct libinput_seat *seat)
575 assert(seat->refcount > 0);
577 if (seat->refcount == 0)
578 libinput_seat_destroy(seat);
582 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
584 seat->user_data = user_data;
587 LIBINPUT_EXPORT void *
588 libinput_seat_get_user_data(struct libinput_seat *seat)
590 return seat->user_data;
593 LIBINPUT_EXPORT const char *
594 libinput_seat_get_physical_name(struct libinput_seat *seat)
596 return seat->physical_name;
599 LIBINPUT_EXPORT const char *
600 libinput_seat_get_logical_name(struct libinput_seat *seat)
602 return seat->logical_name;
606 libinput_device_init(struct libinput_device *device,
607 struct libinput_seat *seat)
610 device->refcount = 1;
614 libinput_device_ref(struct libinput_device *device)
620 libinput_device_destroy(struct libinput_device *device)
622 evdev_device_destroy((struct evdev_device *) device);
626 libinput_device_unref(struct libinput_device *device)
628 assert(device->refcount > 0);
630 if (device->refcount == 0)
631 libinput_device_destroy(device);
635 libinput_get_fd(struct libinput *libinput)
637 return libinput->epoll_fd;
641 libinput_dispatch(struct libinput *libinput)
643 struct libinput_source *source;
644 struct epoll_event ep[32];
647 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
651 for (i = 0; i < count; ++i) {
652 source = ep[i].data.ptr;
653 if (source->fd == -1)
656 source->dispatch(source->user_data);
659 libinput_drop_destroyed_sources(libinput);
665 init_event_base(struct libinput_event *event,
666 struct libinput_device *device,
667 enum libinput_event_type type)
670 event->device = device;
674 post_base_event(struct libinput_device *device,
675 enum libinput_event_type type,
676 struct libinput_event *event)
678 struct libinput *libinput = device->seat->libinput;
679 init_event_base(event, device, type);
680 libinput_post_event(libinput, event);
684 post_device_event(struct libinput_device *device,
685 enum libinput_event_type type,
686 struct libinput_event *event)
688 init_event_base(event, device, type);
689 libinput_post_event(device->seat->libinput, event);
693 notify_added_device(struct libinput_device *device)
695 struct libinput_event_device_notify *added_device_event;
697 added_device_event = zalloc(sizeof *added_device_event);
698 if (!added_device_event)
701 post_base_event(device,
702 LIBINPUT_EVENT_DEVICE_ADDED,
703 &added_device_event->base);
707 notify_removed_device(struct libinput_device *device)
709 struct libinput_event_device_notify *removed_device_event;
711 removed_device_event = zalloc(sizeof *removed_device_event);
712 if (!removed_device_event)
715 post_base_event(device,
716 LIBINPUT_EVENT_DEVICE_REMOVED,
717 &removed_device_event->base);
721 keyboard_notify_key(struct libinput_device *device,
724 enum libinput_keyboard_key_state state)
726 struct libinput_event_keyboard *key_event;
728 key_event = zalloc(sizeof *key_event);
732 *key_event = (struct libinput_event_keyboard) {
738 post_device_event(device,
739 LIBINPUT_EVENT_KEYBOARD_KEY,
744 pointer_notify_motion(struct libinput_device *device,
749 struct libinput_event_pointer *motion_event;
751 motion_event = zalloc(sizeof *motion_event);
755 *motion_event = (struct libinput_event_pointer) {
761 post_device_event(device,
762 LIBINPUT_EVENT_POINTER_MOTION,
763 &motion_event->base);
767 pointer_notify_motion_absolute(struct libinput_device *device,
772 struct libinput_event_pointer *motion_absolute_event;
774 motion_absolute_event = zalloc(sizeof *motion_absolute_event);
775 if (!motion_absolute_event)
778 *motion_absolute_event = (struct libinput_event_pointer) {
784 post_device_event(device,
785 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
786 &motion_absolute_event->base);
790 pointer_notify_button(struct libinput_device *device,
793 enum libinput_pointer_button_state state)
795 struct libinput_event_pointer *button_event;
797 button_event = zalloc(sizeof *button_event);
801 *button_event = (struct libinput_event_pointer) {
807 post_device_event(device,
808 LIBINPUT_EVENT_POINTER_BUTTON,
809 &button_event->base);
813 pointer_notify_axis(struct libinput_device *device,
815 enum libinput_pointer_axis axis,
818 struct libinput_event_pointer *axis_event;
820 axis_event = zalloc(sizeof *axis_event);
824 *axis_event = (struct libinput_event_pointer) {
830 post_device_event(device,
831 LIBINPUT_EVENT_POINTER_AXIS,
836 touch_notify_touch(struct libinput_device *device,
841 enum libinput_touch_type touch_type)
843 struct libinput_event_touch *touch_event;
845 touch_event = zalloc(sizeof *touch_event);
849 *touch_event = (struct libinput_event_touch) {
854 .touch_type = touch_type,
857 post_device_event(device,
858 LIBINPUT_EVENT_TOUCH_TOUCH,
863 touch_notify_frame(struct libinput_device *device,
866 struct libinput_event_touch *touch_event;
868 touch_event = zalloc(sizeof *touch_event);
872 *touch_event = (struct libinput_event_touch) {
876 post_device_event(device,
877 LIBINPUT_EVENT_TOUCH_FRAME,
883 libinput_post_event(struct libinput *libinput,
884 struct libinput_event *event)
886 struct libinput_event **events = libinput->events;
887 size_t events_len = libinput->events_len;
888 size_t events_count = libinput->events_count;
893 if (events_count > events_len) {
895 events = realloc(events, events_len * sizeof *events);
897 fprintf(stderr, "Failed to reallocate event ring "
902 if (libinput->events_count > 0 && libinput->events_in == 0) {
903 libinput->events_in = libinput->events_len;
904 } else if (libinput->events_count > 0 &&
905 libinput->events_out >= libinput->events_in) {
906 move_len = libinput->events_len - libinput->events_out;
907 new_out = events_len - move_len;
908 memmove(events + new_out,
909 events + libinput->events_out,
910 move_len * sizeof *events);
911 libinput->events_out = new_out;
914 libinput->events = events;
915 libinput->events_len = events_len;
919 libinput_device_ref(event->device);
921 libinput->events_count = events_count;
922 events[libinput->events_in] = event;
923 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
926 LIBINPUT_EXPORT struct libinput_event *
927 libinput_get_event(struct libinput *libinput)
929 struct libinput_event *event;
931 if (libinput->events_count == 0)
934 event = libinput->events[libinput->events_out];
935 libinput->events_out =
936 (libinput->events_out + 1) % libinput->events_len;
937 libinput->events_count--;
942 LIBINPUT_EXPORT enum libinput_event_type
943 libinput_next_event_type(struct libinput *libinput)
945 struct libinput_event *event;
947 if (libinput->events_count == 0)
948 return LIBINPUT_EVENT_NONE;
950 event = libinput->events[libinput->events_out];
954 LIBINPUT_EXPORT void *
955 libinput_get_user_data(struct libinput *libinput)
957 return libinput->user_data;
961 libinput_resume(struct libinput *libinput)
963 return libinput->interface_backend->resume(libinput);
967 libinput_suspend(struct libinput *libinput)
969 libinput->interface_backend->suspend(libinput);
973 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
975 device->user_data = user_data;
978 LIBINPUT_EXPORT void *
979 libinput_device_get_user_data(struct libinput_device *device)
981 return device->user_data;
984 LIBINPUT_EXPORT const char *
985 libinput_device_get_sysname(struct libinput_device *device)
987 return evdev_device_get_sysname((struct evdev_device *) device);
990 LIBINPUT_EXPORT const char *
991 libinput_device_get_output_name(struct libinput_device *device)
993 return evdev_device_get_output((struct evdev_device *) device);
996 LIBINPUT_EXPORT struct libinput_seat *
997 libinput_device_get_seat(struct libinput_device *device)
1002 LIBINPUT_EXPORT void
1003 libinput_device_led_update(struct libinput_device *device,
1004 enum libinput_led leds)
1006 evdev_device_led_update((struct evdev_device *) device, leds);
1010 libinput_device_get_keys(struct libinput_device *device,
1011 char *keys, size_t size)
1013 return evdev_device_get_keys((struct evdev_device *) device,
1018 LIBINPUT_EXPORT void
1019 libinput_device_calibrate(struct libinput_device *device,
1020 float calibration[6])
1022 evdev_device_calibrate((struct evdev_device *) device, calibration);
1026 libinput_device_has_capability(struct libinput_device *device,
1027 enum libinput_device_capability capability)
1029 return evdev_device_has_capability((struct evdev_device *) device,