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_post_event(struct libinput *libinput,
82 struct libinput_event *event);
84 LIBINPUT_EXPORT enum libinput_event_type
85 libinput_event_get_type(struct libinput_event *event)
90 LIBINPUT_EXPORT struct libinput *
91 libinput_event_get_context(struct libinput_event *event)
93 return event->device->seat->libinput;
96 LIBINPUT_EXPORT struct libinput_device *
97 libinput_event_get_device(struct libinput_event *event)
102 LIBINPUT_EXPORT struct libinput_event_pointer *
103 libinput_event_get_pointer_event(struct libinput_event *event)
105 switch (event->type) {
106 case LIBINPUT_EVENT_NONE:
107 abort(); /* not used as actual event type */
108 case LIBINPUT_EVENT_DEVICE_ADDED:
109 case LIBINPUT_EVENT_DEVICE_REMOVED:
110 case LIBINPUT_EVENT_KEYBOARD_KEY:
112 case LIBINPUT_EVENT_POINTER_MOTION:
113 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
114 case LIBINPUT_EVENT_POINTER_BUTTON:
115 case LIBINPUT_EVENT_POINTER_AXIS:
116 return (struct libinput_event_pointer *) event;
117 case LIBINPUT_EVENT_TOUCH_TOUCH:
118 case LIBINPUT_EVENT_TOUCH_FRAME:
125 LIBINPUT_EXPORT struct libinput_event_keyboard *
126 libinput_event_get_keyboard_event(struct libinput_event *event)
128 switch (event->type) {
129 case LIBINPUT_EVENT_NONE:
130 abort(); /* not used as actual event type */
131 case LIBINPUT_EVENT_DEVICE_ADDED:
132 case LIBINPUT_EVENT_DEVICE_REMOVED:
134 case LIBINPUT_EVENT_KEYBOARD_KEY:
135 return (struct libinput_event_keyboard *) event;
136 case LIBINPUT_EVENT_POINTER_MOTION:
137 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
138 case LIBINPUT_EVENT_POINTER_BUTTON:
139 case LIBINPUT_EVENT_POINTER_AXIS:
140 case LIBINPUT_EVENT_TOUCH_TOUCH:
141 case LIBINPUT_EVENT_TOUCH_FRAME:
148 LIBINPUT_EXPORT struct libinput_event_touch *
149 libinput_event_get_touch_event(struct libinput_event *event)
151 switch (event->type) {
152 case LIBINPUT_EVENT_NONE:
153 abort(); /* not used as actual event type */
154 case LIBINPUT_EVENT_DEVICE_ADDED:
155 case LIBINPUT_EVENT_DEVICE_REMOVED:
156 case LIBINPUT_EVENT_KEYBOARD_KEY:
157 case LIBINPUT_EVENT_POINTER_MOTION:
158 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
159 case LIBINPUT_EVENT_POINTER_BUTTON:
160 case LIBINPUT_EVENT_POINTER_AXIS:
162 case LIBINPUT_EVENT_TOUCH_TOUCH:
163 case LIBINPUT_EVENT_TOUCH_FRAME:
164 return (struct libinput_event_touch *) event;
170 LIBINPUT_EXPORT struct libinput_event_device_notify *
171 libinput_event_get_device_notify_event(struct libinput_event *event)
173 switch (event->type) {
174 case LIBINPUT_EVENT_NONE:
175 abort(); /* not used as actual event type */
176 case LIBINPUT_EVENT_DEVICE_ADDED:
177 case LIBINPUT_EVENT_DEVICE_REMOVED:
178 return (struct libinput_event_device_notify *) event;
179 case LIBINPUT_EVENT_KEYBOARD_KEY:
180 case LIBINPUT_EVENT_POINTER_MOTION:
181 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
182 case LIBINPUT_EVENT_POINTER_BUTTON:
183 case LIBINPUT_EVENT_POINTER_AXIS:
184 case LIBINPUT_EVENT_TOUCH_TOUCH:
185 case LIBINPUT_EVENT_TOUCH_FRAME:
192 LIBINPUT_EXPORT uint32_t
193 libinput_event_keyboard_get_time(
194 struct libinput_event_keyboard *event)
199 LIBINPUT_EXPORT uint32_t
200 libinput_event_keyboard_get_key(
201 struct libinput_event_keyboard *event)
206 LIBINPUT_EXPORT enum libinput_keyboard_key_state
207 libinput_event_keyboard_get_key_state(
208 struct libinput_event_keyboard *event)
213 LIBINPUT_EXPORT uint32_t
214 libinput_event_pointer_get_time(
215 struct libinput_event_pointer *event)
220 LIBINPUT_EXPORT li_fixed_t
221 libinput_event_pointer_get_dx(
222 struct libinput_event_pointer *event)
227 LIBINPUT_EXPORT li_fixed_t
228 libinput_event_pointer_get_dy(
229 struct libinput_event_pointer *event)
234 LIBINPUT_EXPORT li_fixed_t
235 libinput_event_pointer_get_absolute_x(
236 struct libinput_event_pointer *event)
241 LIBINPUT_EXPORT li_fixed_t
242 libinput_event_pointer_get_absolute_y(
243 struct libinput_event_pointer *event)
248 LIBINPUT_EXPORT uint32_t
249 libinput_event_pointer_get_button(
250 struct libinput_event_pointer *event)
252 return event->button;
255 LIBINPUT_EXPORT enum libinput_pointer_button_state
256 libinput_event_pointer_get_button_state(
257 struct libinput_event_pointer *event)
262 LIBINPUT_EXPORT enum libinput_pointer_axis
263 libinput_event_pointer_get_axis(
264 struct libinput_event_pointer *event)
269 LIBINPUT_EXPORT li_fixed_t
270 libinput_event_pointer_get_axis_value(
271 struct libinput_event_pointer *event)
276 LIBINPUT_EXPORT uint32_t
277 libinput_event_touch_get_time(
278 struct libinput_event_touch *event)
283 LIBINPUT_EXPORT uint32_t
284 libinput_event_touch_get_slot(
285 struct libinput_event_touch *event)
290 LIBINPUT_EXPORT li_fixed_t
291 libinput_event_touch_get_x(
292 struct libinput_event_touch *event)
297 LIBINPUT_EXPORT li_fixed_t
298 libinput_event_touch_get_y(
299 struct libinput_event_touch *event)
304 LIBINPUT_EXPORT enum libinput_touch_type
305 libinput_event_touch_get_touch_type(
306 struct libinput_event_touch *event)
308 return event->touch_type;
311 struct libinput_source *
312 libinput_add_fd(struct libinput *libinput,
314 libinput_source_dispatch_t dispatch,
317 struct libinput_source *source;
318 struct epoll_event ep;
320 source = malloc(sizeof *source);
324 source->dispatch = dispatch;
325 source->user_data = user_data;
328 memset(&ep, 0, sizeof ep);
330 ep.data.ptr = source;
332 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
342 libinput_remove_source(struct libinput *libinput,
343 struct libinput_source *source)
345 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
348 list_insert(&libinput->source_destroy_list, &source->link);
352 libinput_init(struct libinput *libinput,
353 const struct libinput_interface *interface,
354 const struct libinput_interface_backend *interface_backend,
357 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
358 if (libinput->epoll_fd < 0)
361 libinput->events_len = 4;
362 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
363 if (!libinput->events) {
364 close(libinput->epoll_fd);
368 libinput->interface = interface;
369 libinput->interface_backend = interface_backend;
370 libinput->user_data = user_data;
371 list_init(&libinput->source_destroy_list);
372 list_init(&libinput->seat_list);
378 libinput_device_destroy(struct libinput_device *device);
381 libinput_seat_destroy(struct libinput_seat *seat);
384 libinput_drop_destroyed_sources(struct libinput *libinput)
386 struct libinput_source *source, *next;
388 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
390 list_init(&libinput->source_destroy_list);
394 libinput_destroy(struct libinput *libinput)
396 struct libinput_event *event;
397 struct libinput_device *device, *next_device;
398 struct libinput_seat *seat, *next_seat;
400 if (libinput == NULL)
403 libinput_suspend(libinput);
405 libinput->interface_backend->destroy(libinput);
407 while ((event = libinput_get_event(libinput)))
408 libinput_event_destroy(event);
410 libinput_drop_destroyed_sources(libinput);
412 free(libinput->events);
414 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
415 list_for_each_safe(device, next_device,
418 libinput_device_destroy(device);
420 libinput_seat_destroy(seat);
423 close(libinput->epoll_fd);
428 libinput_event_destroy(struct libinput_event *event)
434 libinput_device_unref(event->device);
440 open_restricted(struct libinput *libinput,
441 const char *path, int flags)
443 return libinput->interface->open_restricted(path,
445 libinput->user_data);
449 close_restricted(struct libinput *libinput, int fd)
451 return libinput->interface->close_restricted(fd, libinput->user_data);
455 libinput_seat_init(struct libinput_seat *seat,
456 struct libinput *libinput,
457 const char *physical_name,
458 const char *logical_name,
459 libinput_seat_destroy_func destroy)
462 seat->libinput = libinput;
463 seat->physical_name = strdup(physical_name);
464 seat->logical_name = strdup(logical_name);
465 seat->destroy = destroy;
466 list_init(&seat->devices_list);
470 libinput_seat_ref(struct libinput_seat *seat)
476 libinput_seat_destroy(struct libinput_seat *seat)
478 list_remove(&seat->link);
479 free(seat->logical_name);
480 free(seat->physical_name);
485 libinput_seat_unref(struct libinput_seat *seat)
487 assert(seat->refcount > 0);
489 if (seat->refcount == 0)
490 libinput_seat_destroy(seat);
494 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
496 seat->user_data = user_data;
499 LIBINPUT_EXPORT void *
500 libinput_seat_get_user_data(struct libinput_seat *seat)
502 return seat->user_data;
505 LIBINPUT_EXPORT const char *
506 libinput_seat_get_physical_name(struct libinput_seat *seat)
508 return seat->physical_name;
511 LIBINPUT_EXPORT const char *
512 libinput_seat_get_logical_name(struct libinput_seat *seat)
514 return seat->logical_name;
518 libinput_device_init(struct libinput_device *device,
519 struct libinput_seat *seat)
522 device->refcount = 1;
526 libinput_device_ref(struct libinput_device *device)
532 libinput_device_destroy(struct libinput_device *device)
534 evdev_device_destroy((struct evdev_device *) device);
538 libinput_device_unref(struct libinput_device *device)
540 assert(device->refcount > 0);
542 if (device->refcount == 0)
543 libinput_device_destroy(device);
547 libinput_get_fd(struct libinput *libinput)
549 return libinput->epoll_fd;
553 libinput_dispatch(struct libinput *libinput)
555 struct libinput_source *source;
556 struct epoll_event ep[32];
559 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
563 for (i = 0; i < count; ++i) {
564 source = ep[i].data.ptr;
565 if (source->fd == -1)
568 source->dispatch(source->user_data);
571 libinput_drop_destroyed_sources(libinput);
577 init_event_base(struct libinput_event *event,
578 struct libinput_device *device,
579 enum libinput_event_type type)
582 event->device = device;
586 post_base_event(struct libinput_device *device,
587 enum libinput_event_type type,
588 struct libinput_event *event)
590 struct libinput *libinput = device->seat->libinput;
591 init_event_base(event, device, type);
592 libinput_post_event(libinput, event);
596 post_device_event(struct libinput_device *device,
597 enum libinput_event_type type,
598 struct libinput_event *event)
600 init_event_base(event, device, type);
601 libinput_post_event(device->seat->libinput, event);
605 notify_added_device(struct libinput_device *device)
607 struct libinput_event_device_notify *added_device_event;
609 added_device_event = zalloc(sizeof *added_device_event);
610 if (!added_device_event)
613 post_base_event(device,
614 LIBINPUT_EVENT_DEVICE_ADDED,
615 &added_device_event->base);
619 notify_removed_device(struct libinput_device *device)
621 struct libinput_event_device_notify *removed_device_event;
623 removed_device_event = zalloc(sizeof *removed_device_event);
624 if (!removed_device_event)
627 post_base_event(device,
628 LIBINPUT_EVENT_DEVICE_REMOVED,
629 &removed_device_event->base);
633 keyboard_notify_key(struct libinput_device *device,
636 enum libinput_keyboard_key_state state)
638 struct libinput_event_keyboard *key_event;
640 key_event = zalloc(sizeof *key_event);
644 *key_event = (struct libinput_event_keyboard) {
650 post_device_event(device,
651 LIBINPUT_EVENT_KEYBOARD_KEY,
656 pointer_notify_motion(struct libinput_device *device,
661 struct libinput_event_pointer *motion_event;
663 motion_event = zalloc(sizeof *motion_event);
667 *motion_event = (struct libinput_event_pointer) {
673 post_device_event(device,
674 LIBINPUT_EVENT_POINTER_MOTION,
675 &motion_event->base);
679 pointer_notify_motion_absolute(struct libinput_device *device,
684 struct libinput_event_pointer *motion_absolute_event;
686 motion_absolute_event = zalloc(sizeof *motion_absolute_event);
687 if (!motion_absolute_event)
690 *motion_absolute_event = (struct libinput_event_pointer) {
696 post_device_event(device,
697 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
698 &motion_absolute_event->base);
702 pointer_notify_button(struct libinput_device *device,
705 enum libinput_pointer_button_state state)
707 struct libinput_event_pointer *button_event;
709 button_event = zalloc(sizeof *button_event);
713 *button_event = (struct libinput_event_pointer) {
719 post_device_event(device,
720 LIBINPUT_EVENT_POINTER_BUTTON,
721 &button_event->base);
725 pointer_notify_axis(struct libinput_device *device,
727 enum libinput_pointer_axis axis,
730 struct libinput_event_pointer *axis_event;
732 axis_event = zalloc(sizeof *axis_event);
736 *axis_event = (struct libinput_event_pointer) {
742 post_device_event(device,
743 LIBINPUT_EVENT_POINTER_AXIS,
748 touch_notify_touch(struct libinput_device *device,
753 enum libinput_touch_type touch_type)
755 struct libinput_event_touch *touch_event;
757 touch_event = zalloc(sizeof *touch_event);
761 *touch_event = (struct libinput_event_touch) {
766 .touch_type = touch_type,
769 post_device_event(device,
770 LIBINPUT_EVENT_TOUCH_TOUCH,
775 touch_notify_frame(struct libinput_device *device,
778 struct libinput_event_touch *touch_event;
780 touch_event = zalloc(sizeof *touch_event);
784 *touch_event = (struct libinput_event_touch) {
788 post_device_event(device,
789 LIBINPUT_EVENT_TOUCH_FRAME,
795 libinput_post_event(struct libinput *libinput,
796 struct libinput_event *event)
798 struct libinput_event **events = libinput->events;
799 size_t events_len = libinput->events_len;
800 size_t events_count = libinput->events_count;
805 if (events_count > events_len) {
807 events = realloc(events, events_len * sizeof *events);
809 fprintf(stderr, "Failed to reallocate event ring "
814 if (libinput->events_count > 0 && libinput->events_in == 0) {
815 libinput->events_in = libinput->events_len;
816 } else if (libinput->events_count > 0 &&
817 libinput->events_out >= libinput->events_in) {
818 move_len = libinput->events_len - libinput->events_out;
819 new_out = events_len - move_len;
820 memmove(events + new_out,
821 events + libinput->events_out,
822 move_len * sizeof *events);
823 libinput->events_out = new_out;
826 libinput->events = events;
827 libinput->events_len = events_len;
831 libinput_device_ref(event->device);
833 libinput->events_count = events_count;
834 events[libinput->events_in] = event;
835 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
838 LIBINPUT_EXPORT struct libinput_event *
839 libinput_get_event(struct libinput *libinput)
841 struct libinput_event *event;
843 if (libinput->events_count == 0)
846 event = libinput->events[libinput->events_out];
847 libinput->events_out =
848 (libinput->events_out + 1) % libinput->events_len;
849 libinput->events_count--;
854 LIBINPUT_EXPORT enum libinput_event_type
855 libinput_next_event_type(struct libinput *libinput)
857 struct libinput_event *event;
859 if (libinput->events_count == 0)
860 return LIBINPUT_EVENT_NONE;
862 event = libinput->events[libinput->events_out];
866 LIBINPUT_EXPORT void *
867 libinput_get_user_data(struct libinput *libinput)
869 return libinput->user_data;
873 libinput_resume(struct libinput *libinput)
875 return libinput->interface_backend->resume(libinput);
879 libinput_suspend(struct libinput *libinput)
881 libinput->interface_backend->suspend(libinput);
885 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
887 device->user_data = user_data;
890 LIBINPUT_EXPORT void *
891 libinput_device_get_user_data(struct libinput_device *device)
893 return device->user_data;
896 LIBINPUT_EXPORT const char *
897 libinput_device_get_sysname(struct libinput_device *device)
899 return evdev_device_get_sysname((struct evdev_device *) device);
902 LIBINPUT_EXPORT const char *
903 libinput_device_get_output_name(struct libinput_device *device)
905 return evdev_device_get_output((struct evdev_device *) device);
908 LIBINPUT_EXPORT struct libinput_seat *
909 libinput_device_get_seat(struct libinput_device *device)
915 libinput_device_led_update(struct libinput_device *device,
916 enum libinput_led leds)
918 evdev_device_led_update((struct evdev_device *) device, leds);
922 libinput_device_get_keys(struct libinput_device *device,
923 char *keys, size_t size)
925 return evdev_device_get_keys((struct evdev_device *) device,
931 libinput_device_calibrate(struct libinput_device *device,
932 float calibration[6])
934 evdev_device_calibrate((struct evdev_device *) device, calibration);
938 libinput_device_has_capability(struct libinput_device *device,
939 enum libinput_device_capability capability)
941 return evdev_device_has_capability((struct evdev_device *) device,