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;
78 enum libinput_touch_type touch_type;
82 libinput_default_log_func(enum libinput_log_priority priority,
84 const char *format, va_list args)
89 case LIBINPUT_LOG_PRIORITY_DEBUG: prefix = "debug"; break;
90 case LIBINPUT_LOG_PRIORITY_INFO: prefix = "info"; break;
91 case LIBINPUT_LOG_PRIORITY_ERROR: prefix = "error"; break;
92 default: prefix="<invalid priority>"; break;
95 fprintf(stderr, "libinput %s: ", prefix);
96 vfprintf(stderr, format, args);
100 enum libinput_log_priority priority;
101 libinput_log_handler handler;
105 static struct log_data log_data = {
106 .priority = LIBINPUT_LOG_PRIORITY_ERROR,
107 .handler = libinput_default_log_func,
112 log_msg(enum libinput_log_priority priority, const char *format, ...)
116 if (log_data.handler && log_data.priority <= priority) {
117 va_start(args, format);
118 log_data.handler(priority, log_data.user_data, format, args);
124 libinput_log_set_priority(enum libinput_log_priority priority)
126 log_data.priority = priority;
129 LIBINPUT_EXPORT enum libinput_log_priority
130 libinput_log_get_priority(void)
132 return log_data.priority;
136 libinput_log_set_handler(libinput_log_handler log_handler,
139 log_data.handler = log_handler;
140 log_data.user_data = user_data;
144 libinput_post_event(struct libinput *libinput,
145 struct libinput_event *event);
147 LIBINPUT_EXPORT enum libinput_event_type
148 libinput_event_get_type(struct libinput_event *event)
153 LIBINPUT_EXPORT struct libinput *
154 libinput_event_get_context(struct libinput_event *event)
156 return event->device->seat->libinput;
159 LIBINPUT_EXPORT struct libinput_device *
160 libinput_event_get_device(struct libinput_event *event)
162 return event->device;
165 LIBINPUT_EXPORT struct libinput_event_pointer *
166 libinput_event_get_pointer_event(struct libinput_event *event)
168 switch (event->type) {
169 case LIBINPUT_EVENT_NONE:
170 abort(); /* not used as actual event type */
171 case LIBINPUT_EVENT_DEVICE_ADDED:
172 case LIBINPUT_EVENT_DEVICE_REMOVED:
173 case LIBINPUT_EVENT_KEYBOARD_KEY:
175 case LIBINPUT_EVENT_POINTER_MOTION:
176 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
177 case LIBINPUT_EVENT_POINTER_BUTTON:
178 case LIBINPUT_EVENT_POINTER_AXIS:
179 return (struct libinput_event_pointer *) event;
180 case LIBINPUT_EVENT_TOUCH_TOUCH:
181 case LIBINPUT_EVENT_TOUCH_FRAME:
188 LIBINPUT_EXPORT struct libinput_event_keyboard *
189 libinput_event_get_keyboard_event(struct libinput_event *event)
191 switch (event->type) {
192 case LIBINPUT_EVENT_NONE:
193 abort(); /* not used as actual event type */
194 case LIBINPUT_EVENT_DEVICE_ADDED:
195 case LIBINPUT_EVENT_DEVICE_REMOVED:
197 case LIBINPUT_EVENT_KEYBOARD_KEY:
198 return (struct libinput_event_keyboard *) event;
199 case LIBINPUT_EVENT_POINTER_MOTION:
200 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
201 case LIBINPUT_EVENT_POINTER_BUTTON:
202 case LIBINPUT_EVENT_POINTER_AXIS:
203 case LIBINPUT_EVENT_TOUCH_TOUCH:
204 case LIBINPUT_EVENT_TOUCH_FRAME:
211 LIBINPUT_EXPORT struct libinput_event_touch *
212 libinput_event_get_touch_event(struct libinput_event *event)
214 switch (event->type) {
215 case LIBINPUT_EVENT_NONE:
216 abort(); /* not used as actual event type */
217 case LIBINPUT_EVENT_DEVICE_ADDED:
218 case LIBINPUT_EVENT_DEVICE_REMOVED:
219 case LIBINPUT_EVENT_KEYBOARD_KEY:
220 case LIBINPUT_EVENT_POINTER_MOTION:
221 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
222 case LIBINPUT_EVENT_POINTER_BUTTON:
223 case LIBINPUT_EVENT_POINTER_AXIS:
225 case LIBINPUT_EVENT_TOUCH_TOUCH:
226 case LIBINPUT_EVENT_TOUCH_FRAME:
227 return (struct libinput_event_touch *) event;
233 LIBINPUT_EXPORT struct libinput_event_device_notify *
234 libinput_event_get_device_notify_event(struct libinput_event *event)
236 switch (event->type) {
237 case LIBINPUT_EVENT_NONE:
238 abort(); /* not used as actual event type */
239 case LIBINPUT_EVENT_DEVICE_ADDED:
240 case LIBINPUT_EVENT_DEVICE_REMOVED:
241 return (struct libinput_event_device_notify *) event;
242 case LIBINPUT_EVENT_KEYBOARD_KEY:
243 case LIBINPUT_EVENT_POINTER_MOTION:
244 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
245 case LIBINPUT_EVENT_POINTER_BUTTON:
246 case LIBINPUT_EVENT_POINTER_AXIS:
247 case LIBINPUT_EVENT_TOUCH_TOUCH:
248 case LIBINPUT_EVENT_TOUCH_FRAME:
255 LIBINPUT_EXPORT uint32_t
256 libinput_event_keyboard_get_time(struct libinput_event_keyboard *event)
261 LIBINPUT_EXPORT uint32_t
262 libinput_event_keyboard_get_key(struct libinput_event_keyboard *event)
267 LIBINPUT_EXPORT enum libinput_keyboard_key_state
268 libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event)
273 LIBINPUT_EXPORT uint32_t
274 libinput_event_pointer_get_time(struct libinput_event_pointer *event)
279 LIBINPUT_EXPORT li_fixed_t
280 libinput_event_pointer_get_dx(struct libinput_event_pointer *event)
285 LIBINPUT_EXPORT li_fixed_t
286 libinput_event_pointer_get_dy(struct libinput_event_pointer *event)
291 LIBINPUT_EXPORT li_fixed_t
292 libinput_event_pointer_get_absolute_x(struct libinput_event_pointer *event)
297 LIBINPUT_EXPORT li_fixed_t
298 libinput_event_pointer_get_absolute_y(struct libinput_event_pointer *event)
303 LIBINPUT_EXPORT li_fixed_t
304 libinput_event_pointer_get_absolute_x_transformed(
305 struct libinput_event_pointer *event,
308 struct evdev_device *device =
309 (struct evdev_device *) event->base.device;
311 return evdev_device_transform_x(device, event->x, width);
314 LIBINPUT_EXPORT li_fixed_t
315 libinput_event_pointer_get_absolute_y_transformed(
316 struct libinput_event_pointer *event,
319 struct evdev_device *device =
320 (struct evdev_device *) event->base.device;
322 return evdev_device_transform_y(device, event->y, height);
325 LIBINPUT_EXPORT uint32_t
326 libinput_event_pointer_get_button(struct libinput_event_pointer *event)
328 return event->button;
331 LIBINPUT_EXPORT enum libinput_pointer_button_state
332 libinput_event_pointer_get_button_state(struct libinput_event_pointer *event)
337 LIBINPUT_EXPORT enum libinput_pointer_axis
338 libinput_event_pointer_get_axis(struct libinput_event_pointer *event)
343 LIBINPUT_EXPORT li_fixed_t
344 libinput_event_pointer_get_axis_value(struct libinput_event_pointer *event)
349 LIBINPUT_EXPORT uint32_t
350 libinput_event_touch_get_time(struct libinput_event_touch *event)
355 LIBINPUT_EXPORT int32_t
356 libinput_event_touch_get_slot(struct libinput_event_touch *event)
361 LIBINPUT_EXPORT int32_t
362 libinput_event_touch_get_seat_slot(struct libinput_event_touch *event)
364 return event->seat_slot;
367 LIBINPUT_EXPORT li_fixed_t
368 libinput_event_touch_get_x(struct libinput_event_touch *event)
373 LIBINPUT_EXPORT li_fixed_t
374 libinput_event_touch_get_x_transformed(struct libinput_event_touch *event,
377 struct evdev_device *device =
378 (struct evdev_device *) event->base.device;
380 return evdev_device_transform_x(device, event->x, width);
383 LIBINPUT_EXPORT li_fixed_t
384 libinput_event_touch_get_y_transformed(struct libinput_event_touch *event,
387 struct evdev_device *device =
388 (struct evdev_device *) event->base.device;
390 return evdev_device_transform_y(device, event->y, height);
393 LIBINPUT_EXPORT li_fixed_t
394 libinput_event_touch_get_y(struct libinput_event_touch *event)
399 LIBINPUT_EXPORT enum libinput_touch_type
400 libinput_event_touch_get_touch_type(struct libinput_event_touch *event)
402 return event->touch_type;
405 struct libinput_source *
406 libinput_add_fd(struct libinput *libinput,
408 libinput_source_dispatch_t dispatch,
411 struct libinput_source *source;
412 struct epoll_event ep;
414 source = malloc(sizeof *source);
418 source->dispatch = dispatch;
419 source->user_data = user_data;
422 memset(&ep, 0, sizeof ep);
424 ep.data.ptr = source;
426 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
436 libinput_remove_source(struct libinput *libinput,
437 struct libinput_source *source)
439 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
442 list_insert(&libinput->source_destroy_list, &source->link);
446 libinput_init(struct libinput *libinput,
447 const struct libinput_interface *interface,
448 const struct libinput_interface_backend *interface_backend,
451 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
452 if (libinput->epoll_fd < 0)
455 libinput->events_len = 4;
456 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
457 if (!libinput->events) {
458 close(libinput->epoll_fd);
462 libinput->interface = interface;
463 libinput->interface_backend = interface_backend;
464 libinput->user_data = user_data;
465 list_init(&libinput->source_destroy_list);
466 list_init(&libinput->seat_list);
472 libinput_device_destroy(struct libinput_device *device);
475 libinput_seat_destroy(struct libinput_seat *seat);
478 libinput_drop_destroyed_sources(struct libinput *libinput)
480 struct libinput_source *source, *next;
482 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
484 list_init(&libinput->source_destroy_list);
488 libinput_destroy(struct libinput *libinput)
490 struct libinput_event *event;
491 struct libinput_device *device, *next_device;
492 struct libinput_seat *seat, *next_seat;
494 if (libinput == NULL)
497 libinput_suspend(libinput);
499 libinput->interface_backend->destroy(libinput);
501 while ((event = libinput_get_event(libinput)))
502 libinput_event_destroy(event);
504 libinput_drop_destroyed_sources(libinput);
506 free(libinput->events);
508 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
509 list_for_each_safe(device, next_device,
512 libinput_device_destroy(device);
514 libinput_seat_destroy(seat);
517 close(libinput->epoll_fd);
522 libinput_event_destroy(struct libinput_event *event)
528 libinput_device_unref(event->device);
534 open_restricted(struct libinput *libinput,
535 const char *path, int flags)
537 return libinput->interface->open_restricted(path,
539 libinput->user_data);
543 close_restricted(struct libinput *libinput, int fd)
545 return libinput->interface->close_restricted(fd, libinput->user_data);
549 libinput_seat_init(struct libinput_seat *seat,
550 struct libinput *libinput,
551 const char *physical_name,
552 const char *logical_name,
553 libinput_seat_destroy_func destroy)
556 seat->libinput = libinput;
557 seat->physical_name = strdup(physical_name);
558 seat->logical_name = strdup(logical_name);
559 seat->destroy = destroy;
560 list_init(&seat->devices_list);
561 list_insert(&libinput->seat_list, &seat->link);
565 libinput_seat_ref(struct libinput_seat *seat)
571 libinput_seat_destroy(struct libinput_seat *seat)
573 list_remove(&seat->link);
574 free(seat->logical_name);
575 free(seat->physical_name);
580 libinput_seat_unref(struct libinput_seat *seat)
582 assert(seat->refcount > 0);
584 if (seat->refcount == 0)
585 libinput_seat_destroy(seat);
589 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
591 seat->user_data = user_data;
594 LIBINPUT_EXPORT void *
595 libinput_seat_get_user_data(struct libinput_seat *seat)
597 return seat->user_data;
600 LIBINPUT_EXPORT const char *
601 libinput_seat_get_physical_name(struct libinput_seat *seat)
603 return seat->physical_name;
606 LIBINPUT_EXPORT const char *
607 libinput_seat_get_logical_name(struct libinput_seat *seat)
609 return seat->logical_name;
613 libinput_device_init(struct libinput_device *device,
614 struct libinput_seat *seat)
617 device->refcount = 1;
621 libinput_device_ref(struct libinput_device *device)
627 libinput_device_destroy(struct libinput_device *device)
629 evdev_device_destroy((struct evdev_device *) device);
633 libinput_device_unref(struct libinput_device *device)
635 assert(device->refcount > 0);
637 if (device->refcount == 0)
638 libinput_device_destroy(device);
642 libinput_get_fd(struct libinput *libinput)
644 return libinput->epoll_fd;
648 libinput_dispatch(struct libinput *libinput)
650 struct libinput_source *source;
651 struct epoll_event ep[32];
654 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
658 for (i = 0; i < count; ++i) {
659 source = ep[i].data.ptr;
660 if (source->fd == -1)
663 source->dispatch(source->user_data);
666 libinput_drop_destroyed_sources(libinput);
672 init_event_base(struct libinput_event *event,
673 struct libinput_device *device,
674 enum libinput_event_type type)
677 event->device = device;
681 post_base_event(struct libinput_device *device,
682 enum libinput_event_type type,
683 struct libinput_event *event)
685 struct libinput *libinput = device->seat->libinput;
686 init_event_base(event, device, type);
687 libinput_post_event(libinput, event);
691 post_device_event(struct libinput_device *device,
692 enum libinput_event_type type,
693 struct libinput_event *event)
695 init_event_base(event, device, type);
696 libinput_post_event(device->seat->libinput, event);
700 notify_added_device(struct libinput_device *device)
702 struct libinput_event_device_notify *added_device_event;
704 added_device_event = zalloc(sizeof *added_device_event);
705 if (!added_device_event)
708 post_base_event(device,
709 LIBINPUT_EVENT_DEVICE_ADDED,
710 &added_device_event->base);
714 notify_removed_device(struct libinput_device *device)
716 struct libinput_event_device_notify *removed_device_event;
718 removed_device_event = zalloc(sizeof *removed_device_event);
719 if (!removed_device_event)
722 post_base_event(device,
723 LIBINPUT_EVENT_DEVICE_REMOVED,
724 &removed_device_event->base);
728 keyboard_notify_key(struct libinput_device *device,
731 enum libinput_keyboard_key_state state)
733 struct libinput_event_keyboard *key_event;
735 key_event = zalloc(sizeof *key_event);
739 *key_event = (struct libinput_event_keyboard) {
745 post_device_event(device,
746 LIBINPUT_EVENT_KEYBOARD_KEY,
751 pointer_notify_motion(struct libinput_device *device,
756 struct libinput_event_pointer *motion_event;
758 motion_event = zalloc(sizeof *motion_event);
762 *motion_event = (struct libinput_event_pointer) {
768 post_device_event(device,
769 LIBINPUT_EVENT_POINTER_MOTION,
770 &motion_event->base);
774 pointer_notify_motion_absolute(struct libinput_device *device,
779 struct libinput_event_pointer *motion_absolute_event;
781 motion_absolute_event = zalloc(sizeof *motion_absolute_event);
782 if (!motion_absolute_event)
785 *motion_absolute_event = (struct libinput_event_pointer) {
791 post_device_event(device,
792 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
793 &motion_absolute_event->base);
797 pointer_notify_button(struct libinput_device *device,
800 enum libinput_pointer_button_state state)
802 struct libinput_event_pointer *button_event;
804 button_event = zalloc(sizeof *button_event);
808 *button_event = (struct libinput_event_pointer) {
814 post_device_event(device,
815 LIBINPUT_EVENT_POINTER_BUTTON,
816 &button_event->base);
820 pointer_notify_axis(struct libinput_device *device,
822 enum libinput_pointer_axis axis,
825 struct libinput_event_pointer *axis_event;
827 axis_event = zalloc(sizeof *axis_event);
831 *axis_event = (struct libinput_event_pointer) {
837 post_device_event(device,
838 LIBINPUT_EVENT_POINTER_AXIS,
843 touch_notify_touch(struct libinput_device *device,
849 enum libinput_touch_type touch_type)
851 struct libinput_event_touch *touch_event;
853 touch_event = zalloc(sizeof *touch_event);
857 *touch_event = (struct libinput_event_touch) {
860 .seat_slot = seat_slot,
863 .touch_type = touch_type,
866 post_device_event(device,
867 LIBINPUT_EVENT_TOUCH_TOUCH,
872 touch_notify_frame(struct libinput_device *device,
875 struct libinput_event_touch *touch_event;
877 touch_event = zalloc(sizeof *touch_event);
881 *touch_event = (struct libinput_event_touch) {
885 post_device_event(device,
886 LIBINPUT_EVENT_TOUCH_FRAME,
892 libinput_post_event(struct libinput *libinput,
893 struct libinput_event *event)
895 struct libinput_event **events = libinput->events;
896 size_t events_len = libinput->events_len;
897 size_t events_count = libinput->events_count;
902 if (events_count > events_len) {
904 events = realloc(events, events_len * sizeof *events);
906 fprintf(stderr, "Failed to reallocate event ring "
911 if (libinput->events_count > 0 && libinput->events_in == 0) {
912 libinput->events_in = libinput->events_len;
913 } else if (libinput->events_count > 0 &&
914 libinput->events_out >= libinput->events_in) {
915 move_len = libinput->events_len - libinput->events_out;
916 new_out = events_len - move_len;
917 memmove(events + new_out,
918 events + libinput->events_out,
919 move_len * sizeof *events);
920 libinput->events_out = new_out;
923 libinput->events = events;
924 libinput->events_len = events_len;
928 libinput_device_ref(event->device);
930 libinput->events_count = events_count;
931 events[libinput->events_in] = event;
932 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
935 LIBINPUT_EXPORT struct libinput_event *
936 libinput_get_event(struct libinput *libinput)
938 struct libinput_event *event;
940 if (libinput->events_count == 0)
943 event = libinput->events[libinput->events_out];
944 libinput->events_out =
945 (libinput->events_out + 1) % libinput->events_len;
946 libinput->events_count--;
951 LIBINPUT_EXPORT enum libinput_event_type
952 libinput_next_event_type(struct libinput *libinput)
954 struct libinput_event *event;
956 if (libinput->events_count == 0)
957 return LIBINPUT_EVENT_NONE;
959 event = libinput->events[libinput->events_out];
963 LIBINPUT_EXPORT void *
964 libinput_get_user_data(struct libinput *libinput)
966 return libinput->user_data;
970 libinput_resume(struct libinput *libinput)
972 return libinput->interface_backend->resume(libinput);
976 libinput_suspend(struct libinput *libinput)
978 libinput->interface_backend->suspend(libinput);
982 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
984 device->user_data = user_data;
987 LIBINPUT_EXPORT void *
988 libinput_device_get_user_data(struct libinput_device *device)
990 return device->user_data;
993 LIBINPUT_EXPORT const char *
994 libinput_device_get_sysname(struct libinput_device *device)
996 return evdev_device_get_sysname((struct evdev_device *) device);
999 LIBINPUT_EXPORT const char *
1000 libinput_device_get_output_name(struct libinput_device *device)
1002 return evdev_device_get_output((struct evdev_device *) device);
1005 LIBINPUT_EXPORT struct libinput_seat *
1006 libinput_device_get_seat(struct libinput_device *device)
1008 return device->seat;
1011 LIBINPUT_EXPORT void
1012 libinput_device_led_update(struct libinput_device *device,
1013 enum libinput_led leds)
1015 evdev_device_led_update((struct evdev_device *) device, leds);
1019 libinput_device_get_keys(struct libinput_device *device,
1020 char *keys, size_t size)
1022 return evdev_device_get_keys((struct evdev_device *) device,
1027 LIBINPUT_EXPORT void
1028 libinput_device_calibrate(struct libinput_device *device,
1029 float calibration[6])
1031 evdev_device_calibrate((struct evdev_device *) device, calibration);
1035 libinput_device_has_capability(struct libinput_device *device,
1036 enum libinput_device_capability capability)
1038 return evdev_device_has_capability((struct evdev_device *) device,