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"
38 struct libinput_source {
39 libinput_source_dispatch_t dispatch;
45 struct libinput_event {
46 enum libinput_event_type type;
47 struct libinput_device *device;
50 struct libinput_event_device_notify {
51 struct libinput_event base;
54 struct libinput_event_keyboard {
55 struct libinput_event base;
58 uint32_t seat_key_count;
59 enum libinput_key_state state;
62 struct libinput_event_pointer {
63 struct libinput_event base;
68 uint32_t seat_button_count;
69 enum libinput_button_state state;
70 enum libinput_pointer_axis axis;
74 struct libinput_event_touch {
75 struct libinput_event base;
84 libinput_default_log_func(struct libinput *libinput,
85 enum libinput_log_priority priority,
86 const char *format, va_list args)
91 case LIBINPUT_LOG_PRIORITY_DEBUG: prefix = "debug"; break;
92 case LIBINPUT_LOG_PRIORITY_INFO: prefix = "info"; break;
93 case LIBINPUT_LOG_PRIORITY_ERROR: prefix = "error"; break;
94 default: prefix="<invalid priority>"; break;
97 fprintf(stderr, "libinput %s: ", prefix);
98 vfprintf(stderr, format, args);
102 log_msg_va(struct libinput *libinput,
103 enum libinput_log_priority priority,
107 if (libinput->log_handler &&
108 libinput->log_priority <= priority)
109 libinput->log_handler(libinput, priority, format, args);
113 log_msg(struct libinput *libinput,
114 enum libinput_log_priority priority,
115 const char *format, ...)
119 va_start(args, format);
120 log_msg_va(libinput, priority, format, args);
125 libinput_log_set_priority(struct libinput *libinput,
126 enum libinput_log_priority priority)
128 libinput->log_priority = priority;
131 LIBINPUT_EXPORT enum libinput_log_priority
132 libinput_log_get_priority(const struct libinput *libinput)
134 return libinput->log_priority;
138 libinput_log_set_handler(struct libinput *libinput,
139 libinput_log_handler log_handler)
141 libinput->log_handler = log_handler;
145 libinput_post_event(struct libinput *libinput,
146 struct libinput_event *event);
148 LIBINPUT_EXPORT enum libinput_event_type
149 libinput_event_get_type(struct libinput_event *event)
154 LIBINPUT_EXPORT struct libinput *
155 libinput_event_get_context(struct libinput_event *event)
157 return event->device->seat->libinput;
160 LIBINPUT_EXPORT struct libinput_device *
161 libinput_event_get_device(struct libinput_event *event)
163 return event->device;
166 LIBINPUT_EXPORT struct libinput_event_pointer *
167 libinput_event_get_pointer_event(struct libinput_event *event)
169 switch (event->type) {
170 case LIBINPUT_EVENT_NONE:
171 abort(); /* not used as actual event type */
172 case LIBINPUT_EVENT_DEVICE_ADDED:
173 case LIBINPUT_EVENT_DEVICE_REMOVED:
174 case LIBINPUT_EVENT_KEYBOARD_KEY:
176 case LIBINPUT_EVENT_POINTER_MOTION:
177 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
178 case LIBINPUT_EVENT_POINTER_BUTTON:
179 case LIBINPUT_EVENT_POINTER_AXIS:
180 return (struct libinput_event_pointer *) event;
181 case LIBINPUT_EVENT_TOUCH_DOWN:
182 case LIBINPUT_EVENT_TOUCH_UP:
183 case LIBINPUT_EVENT_TOUCH_MOTION:
184 case LIBINPUT_EVENT_TOUCH_CANCEL:
185 case LIBINPUT_EVENT_TOUCH_FRAME:
192 LIBINPUT_EXPORT struct libinput_event_keyboard *
193 libinput_event_get_keyboard_event(struct libinput_event *event)
195 switch (event->type) {
196 case LIBINPUT_EVENT_NONE:
197 abort(); /* not used as actual event type */
198 case LIBINPUT_EVENT_DEVICE_ADDED:
199 case LIBINPUT_EVENT_DEVICE_REMOVED:
201 case LIBINPUT_EVENT_KEYBOARD_KEY:
202 return (struct libinput_event_keyboard *) event;
203 case LIBINPUT_EVENT_POINTER_MOTION:
204 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
205 case LIBINPUT_EVENT_POINTER_BUTTON:
206 case LIBINPUT_EVENT_POINTER_AXIS:
207 case LIBINPUT_EVENT_TOUCH_DOWN:
208 case LIBINPUT_EVENT_TOUCH_UP:
209 case LIBINPUT_EVENT_TOUCH_MOTION:
210 case LIBINPUT_EVENT_TOUCH_CANCEL:
211 case LIBINPUT_EVENT_TOUCH_FRAME:
218 LIBINPUT_EXPORT struct libinput_event_touch *
219 libinput_event_get_touch_event(struct libinput_event *event)
221 switch (event->type) {
222 case LIBINPUT_EVENT_NONE:
223 abort(); /* not used as actual event type */
224 case LIBINPUT_EVENT_DEVICE_ADDED:
225 case LIBINPUT_EVENT_DEVICE_REMOVED:
226 case LIBINPUT_EVENT_KEYBOARD_KEY:
227 case LIBINPUT_EVENT_POINTER_MOTION:
228 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
229 case LIBINPUT_EVENT_POINTER_BUTTON:
230 case LIBINPUT_EVENT_POINTER_AXIS:
232 case LIBINPUT_EVENT_TOUCH_DOWN:
233 case LIBINPUT_EVENT_TOUCH_UP:
234 case LIBINPUT_EVENT_TOUCH_MOTION:
235 case LIBINPUT_EVENT_TOUCH_CANCEL:
236 case LIBINPUT_EVENT_TOUCH_FRAME:
237 return (struct libinput_event_touch *) event;
243 LIBINPUT_EXPORT struct libinput_event_device_notify *
244 libinput_event_get_device_notify_event(struct libinput_event *event)
246 switch (event->type) {
247 case LIBINPUT_EVENT_NONE:
248 abort(); /* not used as actual event type */
249 case LIBINPUT_EVENT_DEVICE_ADDED:
250 case LIBINPUT_EVENT_DEVICE_REMOVED:
251 return (struct libinput_event_device_notify *) event;
252 case LIBINPUT_EVENT_KEYBOARD_KEY:
253 case LIBINPUT_EVENT_POINTER_MOTION:
254 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
255 case LIBINPUT_EVENT_POINTER_BUTTON:
256 case LIBINPUT_EVENT_POINTER_AXIS:
257 case LIBINPUT_EVENT_TOUCH_DOWN:
258 case LIBINPUT_EVENT_TOUCH_UP:
259 case LIBINPUT_EVENT_TOUCH_MOTION:
260 case LIBINPUT_EVENT_TOUCH_CANCEL:
261 case LIBINPUT_EVENT_TOUCH_FRAME:
268 LIBINPUT_EXPORT uint32_t
269 libinput_event_keyboard_get_time(struct libinput_event_keyboard *event)
274 LIBINPUT_EXPORT uint32_t
275 libinput_event_keyboard_get_key(struct libinput_event_keyboard *event)
280 LIBINPUT_EXPORT enum libinput_key_state
281 libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event)
286 LIBINPUT_EXPORT uint32_t
287 libinput_event_keyboard_get_seat_key_count(
288 struct libinput_event_keyboard *event)
290 return event->seat_key_count;
293 LIBINPUT_EXPORT uint32_t
294 libinput_event_pointer_get_time(struct libinput_event_pointer *event)
299 LIBINPUT_EXPORT double
300 libinput_event_pointer_get_dx(struct libinput_event_pointer *event)
305 LIBINPUT_EXPORT double
306 libinput_event_pointer_get_dy(struct libinput_event_pointer *event)
311 LIBINPUT_EXPORT double
312 libinput_event_pointer_get_absolute_x(struct libinput_event_pointer *event)
314 struct evdev_device *device =
315 (struct evdev_device *) event->base.device;
317 return evdev_convert_to_mm(device->abs.absinfo_x, event->x);
320 LIBINPUT_EXPORT double
321 libinput_event_pointer_get_absolute_y(struct libinput_event_pointer *event)
323 struct evdev_device *device =
324 (struct evdev_device *) event->base.device;
326 return evdev_convert_to_mm(device->abs.absinfo_y, event->y);
329 LIBINPUT_EXPORT double
330 libinput_event_pointer_get_absolute_x_transformed(
331 struct libinput_event_pointer *event,
334 struct evdev_device *device =
335 (struct evdev_device *) event->base.device;
337 return evdev_device_transform_x(device, event->x, width);
340 LIBINPUT_EXPORT double
341 libinput_event_pointer_get_absolute_y_transformed(
342 struct libinput_event_pointer *event,
345 struct evdev_device *device =
346 (struct evdev_device *) event->base.device;
348 return evdev_device_transform_y(device, event->y, height);
351 LIBINPUT_EXPORT uint32_t
352 libinput_event_pointer_get_button(struct libinput_event_pointer *event)
354 return event->button;
357 LIBINPUT_EXPORT enum libinput_button_state
358 libinput_event_pointer_get_button_state(struct libinput_event_pointer *event)
363 LIBINPUT_EXPORT uint32_t
364 libinput_event_pointer_get_seat_button_count(
365 struct libinput_event_pointer *event)
367 return event->seat_button_count;
370 LIBINPUT_EXPORT enum libinput_pointer_axis
371 libinput_event_pointer_get_axis(struct libinput_event_pointer *event)
376 LIBINPUT_EXPORT double
377 libinput_event_pointer_get_axis_value(struct libinput_event_pointer *event)
382 LIBINPUT_EXPORT uint32_t
383 libinput_event_touch_get_time(struct libinput_event_touch *event)
388 LIBINPUT_EXPORT int32_t
389 libinput_event_touch_get_slot(struct libinput_event_touch *event)
394 LIBINPUT_EXPORT int32_t
395 libinput_event_touch_get_seat_slot(struct libinput_event_touch *event)
397 return event->seat_slot;
400 LIBINPUT_EXPORT double
401 libinput_event_touch_get_x(struct libinput_event_touch *event)
403 struct evdev_device *device =
404 (struct evdev_device *) event->base.device;
406 return evdev_convert_to_mm(device->abs.absinfo_x, event->x);
409 LIBINPUT_EXPORT double
410 libinput_event_touch_get_x_transformed(struct libinput_event_touch *event,
413 struct evdev_device *device =
414 (struct evdev_device *) event->base.device;
416 return evdev_device_transform_x(device, event->x, width);
419 LIBINPUT_EXPORT double
420 libinput_event_touch_get_y_transformed(struct libinput_event_touch *event,
423 struct evdev_device *device =
424 (struct evdev_device *) event->base.device;
426 return evdev_device_transform_y(device, event->y, height);
429 LIBINPUT_EXPORT double
430 libinput_event_touch_get_y(struct libinput_event_touch *event)
432 struct evdev_device *device =
433 (struct evdev_device *) event->base.device;
435 return evdev_convert_to_mm(device->abs.absinfo_y, event->y);
438 struct libinput_source *
439 libinput_add_fd(struct libinput *libinput,
441 libinput_source_dispatch_t dispatch,
444 struct libinput_source *source;
445 struct epoll_event ep;
447 source = malloc(sizeof *source);
451 source->dispatch = dispatch;
452 source->user_data = user_data;
455 memset(&ep, 0, sizeof ep);
457 ep.data.ptr = source;
459 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
468 libinput_remove_source(struct libinput *libinput,
469 struct libinput_source *source)
471 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
473 list_insert(&libinput->source_destroy_list, &source->link);
477 libinput_init(struct libinput *libinput,
478 const struct libinput_interface *interface,
479 const struct libinput_interface_backend *interface_backend,
482 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
483 if (libinput->epoll_fd < 0)
486 libinput->events_len = 4;
487 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
488 if (!libinput->events) {
489 close(libinput->epoll_fd);
493 libinput->log_handler = libinput_default_log_func;
494 libinput->log_priority = LIBINPUT_LOG_PRIORITY_ERROR;
495 libinput->interface = interface;
496 libinput->interface_backend = interface_backend;
497 libinput->user_data = user_data;
498 libinput->refcount = 1;
499 list_init(&libinput->source_destroy_list);
500 list_init(&libinput->seat_list);
502 if (libinput_timer_subsys_init(libinput) != 0) {
503 free(libinput->events);
504 close(libinput->epoll_fd);
512 libinput_device_destroy(struct libinput_device *device);
515 libinput_seat_destroy(struct libinput_seat *seat);
518 libinput_drop_destroyed_sources(struct libinput *libinput)
520 struct libinput_source *source, *next;
522 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
524 list_init(&libinput->source_destroy_list);
527 LIBINPUT_EXPORT struct libinput *
528 libinput_ref(struct libinput *libinput)
530 libinput->refcount++;
534 LIBINPUT_EXPORT struct libinput *
535 libinput_unref(struct libinput *libinput)
537 struct libinput_event *event;
538 struct libinput_device *device, *next_device;
539 struct libinput_seat *seat, *next_seat;
541 if (libinput == NULL)
544 assert(libinput->refcount > 0);
545 libinput->refcount--;
546 if (libinput->refcount > 0)
549 libinput_suspend(libinput);
551 libinput->interface_backend->destroy(libinput);
553 while ((event = libinput_get_event(libinput)))
554 libinput_event_destroy(event);
556 free(libinput->events);
558 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
559 list_for_each_safe(device, next_device,
562 libinput_device_destroy(device);
564 libinput_seat_destroy(seat);
567 libinput_timer_subsys_destroy(libinput);
568 libinput_drop_destroyed_sources(libinput);
569 close(libinput->epoll_fd);
576 libinput_event_destroy(struct libinput_event *event)
582 libinput_device_unref(event->device);
588 open_restricted(struct libinput *libinput,
589 const char *path, int flags)
591 return libinput->interface->open_restricted(path,
593 libinput->user_data);
597 close_restricted(struct libinput *libinput, int fd)
599 return libinput->interface->close_restricted(fd, libinput->user_data);
603 libinput_seat_init(struct libinput_seat *seat,
604 struct libinput *libinput,
605 const char *physical_name,
606 const char *logical_name,
607 libinput_seat_destroy_func destroy)
610 seat->libinput = libinput;
611 seat->physical_name = strdup(physical_name);
612 seat->logical_name = strdup(logical_name);
613 seat->destroy = destroy;
614 list_init(&seat->devices_list);
615 list_insert(&libinput->seat_list, &seat->link);
618 LIBINPUT_EXPORT struct libinput_seat *
619 libinput_seat_ref(struct libinput_seat *seat)
626 libinput_seat_destroy(struct libinput_seat *seat)
628 list_remove(&seat->link);
629 free(seat->logical_name);
630 free(seat->physical_name);
634 LIBINPUT_EXPORT struct libinput_seat *
635 libinput_seat_unref(struct libinput_seat *seat)
637 assert(seat->refcount > 0);
639 if (seat->refcount == 0) {
640 libinput_seat_destroy(seat);
648 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
650 seat->user_data = user_data;
653 LIBINPUT_EXPORT void *
654 libinput_seat_get_user_data(struct libinput_seat *seat)
656 return seat->user_data;
659 LIBINPUT_EXPORT const char *
660 libinput_seat_get_physical_name(struct libinput_seat *seat)
662 return seat->physical_name;
665 LIBINPUT_EXPORT const char *
666 libinput_seat_get_logical_name(struct libinput_seat *seat)
668 return seat->logical_name;
672 libinput_device_init(struct libinput_device *device,
673 struct libinput_seat *seat)
676 device->refcount = 1;
679 LIBINPUT_EXPORT struct libinput_device *
680 libinput_device_ref(struct libinput_device *device)
687 libinput_device_destroy(struct libinput_device *device)
689 evdev_device_destroy((struct evdev_device *) device);
692 LIBINPUT_EXPORT struct libinput_device *
693 libinput_device_unref(struct libinput_device *device)
695 assert(device->refcount > 0);
697 if (device->refcount == 0) {
698 libinput_device_destroy(device);
706 libinput_get_fd(struct libinput *libinput)
708 return libinput->epoll_fd;
712 libinput_dispatch(struct libinput *libinput)
714 struct libinput_source *source;
715 struct epoll_event ep[32];
718 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
722 for (i = 0; i < count; ++i) {
723 source = ep[i].data.ptr;
724 if (source->fd == -1)
727 source->dispatch(source->user_data);
730 libinput_drop_destroyed_sources(libinput);
736 update_seat_key_count(struct libinput_seat *seat,
738 enum libinput_key_state state)
740 assert(key >= 0 && key <= KEY_MAX);
743 case LIBINPUT_KEY_STATE_PRESSED:
744 return ++seat->button_count[key];
745 case LIBINPUT_KEY_STATE_RELEASED:
746 /* We might not have received the first PRESSED event. */
747 if (seat->button_count[key] == 0)
750 return --seat->button_count[key];
757 update_seat_button_count(struct libinput_seat *seat,
759 enum libinput_button_state state)
761 assert(button >= 0 && button <= KEY_MAX);
764 case LIBINPUT_BUTTON_STATE_PRESSED:
765 return ++seat->button_count[button];
766 case LIBINPUT_BUTTON_STATE_RELEASED:
767 /* We might not have received the first PRESSED event. */
768 if (seat->button_count[button] == 0)
771 return --seat->button_count[button];
778 init_event_base(struct libinput_event *event,
779 struct libinput_device *device,
780 enum libinput_event_type type)
783 event->device = device;
787 post_base_event(struct libinput_device *device,
788 enum libinput_event_type type,
789 struct libinput_event *event)
791 struct libinput *libinput = device->seat->libinput;
792 init_event_base(event, device, type);
793 libinput_post_event(libinput, event);
797 post_device_event(struct libinput_device *device,
798 enum libinput_event_type type,
799 struct libinput_event *event)
801 init_event_base(event, device, type);
802 libinput_post_event(device->seat->libinput, event);
806 notify_added_device(struct libinput_device *device)
808 struct libinput_event_device_notify *added_device_event;
810 added_device_event = zalloc(sizeof *added_device_event);
811 if (!added_device_event)
814 post_base_event(device,
815 LIBINPUT_EVENT_DEVICE_ADDED,
816 &added_device_event->base);
820 notify_removed_device(struct libinput_device *device)
822 struct libinput_event_device_notify *removed_device_event;
824 removed_device_event = zalloc(sizeof *removed_device_event);
825 if (!removed_device_event)
828 post_base_event(device,
829 LIBINPUT_EVENT_DEVICE_REMOVED,
830 &removed_device_event->base);
834 keyboard_notify_key(struct libinput_device *device,
837 enum libinput_key_state state)
839 struct libinput_event_keyboard *key_event;
840 uint32_t seat_key_count;
842 key_event = zalloc(sizeof *key_event);
846 seat_key_count = update_seat_key_count(device->seat, key, state);
848 *key_event = (struct libinput_event_keyboard) {
852 .seat_key_count = seat_key_count,
855 post_device_event(device,
856 LIBINPUT_EVENT_KEYBOARD_KEY,
861 pointer_notify_motion(struct libinput_device *device,
866 struct libinput_event_pointer *motion_event;
868 motion_event = zalloc(sizeof *motion_event);
872 *motion_event = (struct libinput_event_pointer) {
878 post_device_event(device,
879 LIBINPUT_EVENT_POINTER_MOTION,
880 &motion_event->base);
884 pointer_notify_motion_absolute(struct libinput_device *device,
889 struct libinput_event_pointer *motion_absolute_event;
891 motion_absolute_event = zalloc(sizeof *motion_absolute_event);
892 if (!motion_absolute_event)
895 *motion_absolute_event = (struct libinput_event_pointer) {
901 post_device_event(device,
902 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
903 &motion_absolute_event->base);
907 pointer_notify_button(struct libinput_device *device,
910 enum libinput_button_state state)
912 struct libinput_event_pointer *button_event;
913 int32_t seat_button_count;
915 button_event = zalloc(sizeof *button_event);
919 seat_button_count = update_seat_button_count(device->seat,
923 *button_event = (struct libinput_event_pointer) {
927 .seat_button_count = seat_button_count,
930 post_device_event(device,
931 LIBINPUT_EVENT_POINTER_BUTTON,
932 &button_event->base);
936 pointer_notify_axis(struct libinput_device *device,
938 enum libinput_pointer_axis axis,
941 struct libinput_event_pointer *axis_event;
943 axis_event = zalloc(sizeof *axis_event);
947 *axis_event = (struct libinput_event_pointer) {
953 post_device_event(device,
954 LIBINPUT_EVENT_POINTER_AXIS,
959 touch_notify_touch_down(struct libinput_device *device,
966 struct libinput_event_touch *touch_event;
968 touch_event = zalloc(sizeof *touch_event);
972 *touch_event = (struct libinput_event_touch) {
975 .seat_slot = seat_slot,
980 post_device_event(device,
981 LIBINPUT_EVENT_TOUCH_DOWN,
986 touch_notify_touch_motion(struct libinput_device *device,
993 struct libinput_event_touch *touch_event;
995 touch_event = zalloc(sizeof *touch_event);
999 *touch_event = (struct libinput_event_touch) {
1002 .seat_slot = seat_slot,
1007 post_device_event(device,
1008 LIBINPUT_EVENT_TOUCH_MOTION,
1009 &touch_event->base);
1013 touch_notify_touch_up(struct libinput_device *device,
1018 struct libinput_event_touch *touch_event;
1020 touch_event = zalloc(sizeof *touch_event);
1024 *touch_event = (struct libinput_event_touch) {
1027 .seat_slot = seat_slot,
1030 post_device_event(device,
1031 LIBINPUT_EVENT_TOUCH_UP,
1032 &touch_event->base);
1036 touch_notify_frame(struct libinput_device *device,
1039 struct libinput_event_touch *touch_event;
1041 touch_event = zalloc(sizeof *touch_event);
1045 *touch_event = (struct libinput_event_touch) {
1049 post_device_event(device,
1050 LIBINPUT_EVENT_TOUCH_FRAME,
1051 &touch_event->base);
1055 libinput_post_event(struct libinput *libinput,
1056 struct libinput_event *event)
1058 struct libinput_event **events = libinput->events;
1059 size_t events_len = libinput->events_len;
1060 size_t events_count = libinput->events_count;
1065 if (events_count > events_len) {
1067 events = realloc(events, events_len * sizeof *events);
1069 fprintf(stderr, "Failed to reallocate event ring "
1074 if (libinput->events_count > 0 && libinput->events_in == 0) {
1075 libinput->events_in = libinput->events_len;
1076 } else if (libinput->events_count > 0 &&
1077 libinput->events_out >= libinput->events_in) {
1078 move_len = libinput->events_len - libinput->events_out;
1079 new_out = events_len - move_len;
1080 memmove(events + new_out,
1081 events + libinput->events_out,
1082 move_len * sizeof *events);
1083 libinput->events_out = new_out;
1086 libinput->events = events;
1087 libinput->events_len = events_len;
1091 libinput_device_ref(event->device);
1093 libinput->events_count = events_count;
1094 events[libinput->events_in] = event;
1095 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
1098 LIBINPUT_EXPORT struct libinput_event *
1099 libinput_get_event(struct libinput *libinput)
1101 struct libinput_event *event;
1103 if (libinput->events_count == 0)
1106 event = libinput->events[libinput->events_out];
1107 libinput->events_out =
1108 (libinput->events_out + 1) % libinput->events_len;
1109 libinput->events_count--;
1114 LIBINPUT_EXPORT enum libinput_event_type
1115 libinput_next_event_type(struct libinput *libinput)
1117 struct libinput_event *event;
1119 if (libinput->events_count == 0)
1120 return LIBINPUT_EVENT_NONE;
1122 event = libinput->events[libinput->events_out];
1126 LIBINPUT_EXPORT void *
1127 libinput_get_user_data(struct libinput *libinput)
1129 return libinput->user_data;
1133 libinput_resume(struct libinput *libinput)
1135 return libinput->interface_backend->resume(libinput);
1138 LIBINPUT_EXPORT void
1139 libinput_suspend(struct libinput *libinput)
1141 libinput->interface_backend->suspend(libinput);
1144 LIBINPUT_EXPORT void
1145 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
1147 device->user_data = user_data;
1150 LIBINPUT_EXPORT void *
1151 libinput_device_get_user_data(struct libinput_device *device)
1153 return device->user_data;
1156 LIBINPUT_EXPORT const char *
1157 libinput_device_get_sysname(struct libinput_device *device)
1159 return evdev_device_get_sysname((struct evdev_device *) device);
1162 LIBINPUT_EXPORT const char *
1163 libinput_device_get_name(struct libinput_device *device)
1165 return evdev_device_get_name((struct evdev_device *) device);
1168 LIBINPUT_EXPORT unsigned int
1169 libinput_device_get_id_product(struct libinput_device *device)
1171 return evdev_device_get_id_product((struct evdev_device *) device);
1174 LIBINPUT_EXPORT unsigned int
1175 libinput_device_get_id_vendor(struct libinput_device *device)
1177 return evdev_device_get_id_vendor((struct evdev_device *) device);
1180 LIBINPUT_EXPORT const char *
1181 libinput_device_get_output_name(struct libinput_device *device)
1183 return evdev_device_get_output((struct evdev_device *) device);
1186 LIBINPUT_EXPORT struct libinput_seat *
1187 libinput_device_get_seat(struct libinput_device *device)
1189 return device->seat;
1192 LIBINPUT_EXPORT void
1193 libinput_device_led_update(struct libinput_device *device,
1194 enum libinput_led leds)
1196 evdev_device_led_update((struct evdev_device *) device, leds);
1200 libinput_device_get_keys(struct libinput_device *device,
1201 char *keys, size_t size)
1203 return evdev_device_get_keys((struct evdev_device *) device,
1208 LIBINPUT_EXPORT void
1209 libinput_device_calibrate(struct libinput_device *device,
1210 float calibration[6])
1212 evdev_device_calibrate((struct evdev_device *) device, calibration);
1216 libinput_device_has_capability(struct libinput_device *device,
1217 enum libinput_device_capability capability)
1219 return evdev_device_has_capability((struct evdev_device *) device,
1224 libinput_device_get_size(struct libinput_device *device,
1228 return evdev_device_get_size((struct evdev_device *)device,
1233 LIBINPUT_EXPORT struct libinput_event *
1234 libinput_event_device_notify_get_base_event(struct libinput_event_device_notify *event)
1236 return &event->base;
1239 LIBINPUT_EXPORT struct libinput_event *
1240 libinput_event_keyboard_get_base_event(struct libinput_event_keyboard *event)
1242 return &event->base;
1245 LIBINPUT_EXPORT struct libinput_event *
1246 libinput_event_pointer_get_base_event(struct libinput_event_pointer *event)
1248 return &event->base;
1251 LIBINPUT_EXPORT struct libinput_event *
1252 libinput_event_touch_get_base_event(struct libinput_event_touch *event)
1254 return &event->base;
1257 LIBINPUT_EXPORT const char *
1258 libinput_config_status_to_str(enum libinput_config_status status)
1260 const char *str = NULL;
1263 case LIBINPUT_CONFIG_STATUS_SUCCESS:
1266 case LIBINPUT_CONFIG_STATUS_UNSUPPORTED:
1267 str = "Unsupported configuration option";
1269 case LIBINPUT_CONFIG_STATUS_INVALID:
1270 str = "Invalid argument range";
1278 libinput_device_config_tap_get_finger_count(struct libinput_device *device)
1280 return device->config.tap ? device->config.tap->count(device) : 0;
1283 LIBINPUT_EXPORT enum libinput_config_status
1284 libinput_device_config_tap_set_enabled(struct libinput_device *device,
1285 enum libinput_config_tap_state enable)
1287 if (enable != LIBINPUT_CONFIG_TAP_ENABLED &&
1288 enable != LIBINPUT_CONFIG_TAP_DISABLED)
1289 return LIBINPUT_CONFIG_STATUS_INVALID;
1292 libinput_device_config_tap_get_finger_count(device) == 0)
1293 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1295 return device->config.tap->set_enabled(device, enable);
1298 LIBINPUT_EXPORT enum libinput_config_tap_state
1299 libinput_device_config_tap_get_enabled(struct libinput_device *device)
1301 if (libinput_device_config_tap_get_finger_count(device) == 0)
1302 return LIBINPUT_CONFIG_TAP_DISABLED;
1304 return device->config.tap->get_enabled(device);
1307 LIBINPUT_EXPORT enum libinput_config_tap_state
1308 libinput_device_config_tap_get_default_enabled(struct libinput_device *device)
1310 if (libinput_device_config_tap_get_finger_count(device) == 0)
1311 return LIBINPUT_CONFIG_TAP_DISABLED;
1313 return device->config.tap->get_default(device);
1317 libinput_device_config_calibration_has_matrix(struct libinput_device *device)
1319 return device->config.calibration ?
1320 device->config.calibration->has_matrix(device) : 0;
1323 LIBINPUT_EXPORT enum libinput_config_status
1324 libinput_device_config_calibration_set_matrix(struct libinput_device *device,
1325 const float matrix[6])
1327 if (!libinput_device_config_calibration_has_matrix(device))
1328 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1330 return device->config.calibration->set_matrix(device, matrix);
1334 libinput_device_config_calibration_get_matrix(struct libinput_device *device,
1337 if (!libinput_device_config_calibration_has_matrix(device))
1340 return device->config.calibration->get_matrix(device, matrix);
1344 libinput_device_config_calibration_get_default_matrix(struct libinput_device *device,
1347 if (!libinput_device_config_calibration_has_matrix(device))
1350 return device->config.calibration->get_default_matrix(device, matrix);
1353 LIBINPUT_EXPORT uint32_t
1354 libinput_device_config_send_events_get_modes(struct libinput_device *device)
1356 uint32_t modes = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1358 if (device->config.sendevents)
1359 modes |= device->config.sendevents->get_modes(device);
1364 LIBINPUT_EXPORT enum libinput_config_status
1365 libinput_device_config_send_events_set_mode(struct libinput_device *device,
1366 enum libinput_config_send_events_mode mode)
1368 if ((libinput_device_config_send_events_get_modes(device) & mode) == 0)
1369 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1371 if (device->config.sendevents)
1372 return device->config.sendevents->set_mode(device, mode);
1373 else /* mode must be _ENABLED to get here */
1374 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1377 LIBINPUT_EXPORT enum libinput_config_send_events_mode
1378 libinput_device_config_send_events_get_mode(struct libinput_device *device)
1380 if (device->config.sendevents)
1381 return device->config.sendevents->get_mode(device);
1383 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1386 LIBINPUT_EXPORT enum libinput_config_send_events_mode
1387 libinput_device_config_send_events_get_default_mode(struct libinput_device *device)
1389 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1394 libinput_device_config_accel_is_available(struct libinput_device *device)
1396 return device->config.accel ?
1397 device->config.accel->available(device) : 0;
1400 LIBINPUT_EXPORT enum libinput_config_status
1401 libinput_device_config_accel_set_speed(struct libinput_device *device,
1404 if (!libinput_device_config_accel_is_available(device))
1405 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1407 if (speed < -1.0 || speed > 1.0)
1408 return LIBINPUT_CONFIG_STATUS_INVALID;
1410 return device->config.accel->set_speed(device, speed);
1413 LIBINPUT_EXPORT double
1414 libinput_device_config_accel_get_speed(struct libinput_device *device)
1416 if (!libinput_device_config_accel_is_available(device))
1419 return device->config.accel->get_speed(device);
1422 LIBINPUT_EXPORT double
1423 libinput_device_config_accel_get_default_speed(struct libinput_device *device)
1425 if (!libinput_device_config_accel_is_available(device))
1428 return device->config.accel->get_default_speed(device);
1432 libinput_device_config_scroll_has_natural_scroll(struct libinput_device *device)
1434 if (!device->config.natural_scroll)
1437 return device->config.natural_scroll->has(device);
1440 LIBINPUT_EXPORT enum libinput_config_status
1441 libinput_device_config_scroll_set_natural_scroll_enabled(struct libinput_device *device,
1444 if (!libinput_device_config_scroll_has_natural_scroll(device))
1445 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1447 return device->config.natural_scroll->set_enabled(device, enabled);
1451 libinput_device_config_scroll_get_natural_scroll_enabled(struct libinput_device *device)
1453 if (!device->config.natural_scroll)
1456 return device->config.natural_scroll->get_enabled(device);
1460 libinput_device_config_scroll_get_default_natural_scroll_enabled(struct libinput_device *device)
1462 if (!device->config.natural_scroll)
1465 return device->config.natural_scroll->get_default_enabled(device);
1469 libinput_device_config_buttons_has_left_handed(struct libinput_device *device)
1471 if (!device->config.left_handed)
1474 return device->config.left_handed->has(device);
1477 LIBINPUT_EXPORT enum libinput_config_status
1478 libinput_device_config_buttons_set_left_handed(struct libinput_device *device,
1481 if (!libinput_device_config_buttons_has_left_handed(device))
1482 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1484 return device->config.left_handed->set(device, left_handed);
1488 libinput_device_config_buttons_get_left_handed(struct libinput_device *device)
1490 if (!libinput_device_config_buttons_has_left_handed(device))
1493 return device->config.left_handed->get(device);
1497 libinput_device_config_buttons_get_default_left_handed(struct libinput_device *device)
1499 if (!libinput_device_config_buttons_has_left_handed(device))
1502 return device->config.left_handed->get_default(device);