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 li_fixed_t
249 libinput_event_pointer_get_absolute_x_transformed(
250 struct libinput_event_pointer *event,
253 struct evdev_device *device =
254 (struct evdev_device *) event->base.device;
256 return evdev_device_transform_x(device, event->x, width);
259 LIBINPUT_EXPORT li_fixed_t
260 libinput_event_pointer_get_absolute_y_transformed(
261 struct libinput_event_pointer *event,
264 struct evdev_device *device =
265 (struct evdev_device *) event->base.device;
267 return evdev_device_transform_y(device, event->y, height);
270 LIBINPUT_EXPORT uint32_t
271 libinput_event_pointer_get_button(
272 struct libinput_event_pointer *event)
274 return event->button;
277 LIBINPUT_EXPORT enum libinput_pointer_button_state
278 libinput_event_pointer_get_button_state(
279 struct libinput_event_pointer *event)
284 LIBINPUT_EXPORT enum libinput_pointer_axis
285 libinput_event_pointer_get_axis(
286 struct libinput_event_pointer *event)
291 LIBINPUT_EXPORT li_fixed_t
292 libinput_event_pointer_get_axis_value(
293 struct libinput_event_pointer *event)
298 LIBINPUT_EXPORT uint32_t
299 libinput_event_touch_get_time(
300 struct libinput_event_touch *event)
305 LIBINPUT_EXPORT uint32_t
306 libinput_event_touch_get_slot(
307 struct libinput_event_touch *event)
312 LIBINPUT_EXPORT li_fixed_t
313 libinput_event_touch_get_x(
314 struct libinput_event_touch *event)
319 LIBINPUT_EXPORT li_fixed_t
320 libinput_event_touch_get_x_transformed(
321 struct libinput_event_touch *event,
324 struct evdev_device *device =
325 (struct evdev_device *) event->base.device;
327 return evdev_device_transform_x(device, event->x, width);
330 LIBINPUT_EXPORT li_fixed_t
331 libinput_event_touch_get_y_transformed(
332 struct libinput_event_touch *event,
335 struct evdev_device *device =
336 (struct evdev_device *) event->base.device;
338 return evdev_device_transform_y(device, event->y, height);
341 LIBINPUT_EXPORT li_fixed_t
342 libinput_event_touch_get_y(
343 struct libinput_event_touch *event)
348 LIBINPUT_EXPORT enum libinput_touch_type
349 libinput_event_touch_get_touch_type(
350 struct libinput_event_touch *event)
352 return event->touch_type;
355 struct libinput_source *
356 libinput_add_fd(struct libinput *libinput,
358 libinput_source_dispatch_t dispatch,
361 struct libinput_source *source;
362 struct epoll_event ep;
364 source = malloc(sizeof *source);
368 source->dispatch = dispatch;
369 source->user_data = user_data;
372 memset(&ep, 0, sizeof ep);
374 ep.data.ptr = source;
376 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
386 libinput_remove_source(struct libinput *libinput,
387 struct libinput_source *source)
389 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
392 list_insert(&libinput->source_destroy_list, &source->link);
396 libinput_init(struct libinput *libinput,
397 const struct libinput_interface *interface,
398 const struct libinput_interface_backend *interface_backend,
401 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
402 if (libinput->epoll_fd < 0)
405 libinput->events_len = 4;
406 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
407 if (!libinput->events) {
408 close(libinput->epoll_fd);
412 libinput->interface = interface;
413 libinput->interface_backend = interface_backend;
414 libinput->user_data = user_data;
415 list_init(&libinput->source_destroy_list);
416 list_init(&libinput->seat_list);
422 libinput_device_destroy(struct libinput_device *device);
425 libinput_seat_destroy(struct libinput_seat *seat);
428 libinput_drop_destroyed_sources(struct libinput *libinput)
430 struct libinput_source *source, *next;
432 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
434 list_init(&libinput->source_destroy_list);
438 libinput_destroy(struct libinput *libinput)
440 struct libinput_event *event;
441 struct libinput_device *device, *next_device;
442 struct libinput_seat *seat, *next_seat;
444 if (libinput == NULL)
447 libinput_suspend(libinput);
449 libinput->interface_backend->destroy(libinput);
451 while ((event = libinput_get_event(libinput)))
452 libinput_event_destroy(event);
454 libinput_drop_destroyed_sources(libinput);
456 free(libinput->events);
458 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
459 list_for_each_safe(device, next_device,
462 libinput_device_destroy(device);
464 libinput_seat_destroy(seat);
467 close(libinput->epoll_fd);
472 libinput_event_destroy(struct libinput_event *event)
478 libinput_device_unref(event->device);
484 open_restricted(struct libinput *libinput,
485 const char *path, int flags)
487 return libinput->interface->open_restricted(path,
489 libinput->user_data);
493 close_restricted(struct libinput *libinput, int fd)
495 return libinput->interface->close_restricted(fd, libinput->user_data);
499 libinput_seat_init(struct libinput_seat *seat,
500 struct libinput *libinput,
501 const char *physical_name,
502 const char *logical_name,
503 libinput_seat_destroy_func destroy)
506 seat->libinput = libinput;
507 seat->physical_name = strdup(physical_name);
508 seat->logical_name = strdup(logical_name);
509 seat->destroy = destroy;
510 list_init(&seat->devices_list);
514 libinput_seat_ref(struct libinput_seat *seat)
520 libinput_seat_destroy(struct libinput_seat *seat)
522 list_remove(&seat->link);
523 free(seat->logical_name);
524 free(seat->physical_name);
529 libinput_seat_unref(struct libinput_seat *seat)
531 assert(seat->refcount > 0);
533 if (seat->refcount == 0)
534 libinput_seat_destroy(seat);
538 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
540 seat->user_data = user_data;
543 LIBINPUT_EXPORT void *
544 libinput_seat_get_user_data(struct libinput_seat *seat)
546 return seat->user_data;
549 LIBINPUT_EXPORT const char *
550 libinput_seat_get_physical_name(struct libinput_seat *seat)
552 return seat->physical_name;
555 LIBINPUT_EXPORT const char *
556 libinput_seat_get_logical_name(struct libinput_seat *seat)
558 return seat->logical_name;
562 libinput_device_init(struct libinput_device *device,
563 struct libinput_seat *seat)
566 device->refcount = 1;
570 libinput_device_ref(struct libinput_device *device)
576 libinput_device_destroy(struct libinput_device *device)
578 evdev_device_destroy((struct evdev_device *) device);
582 libinput_device_unref(struct libinput_device *device)
584 assert(device->refcount > 0);
586 if (device->refcount == 0)
587 libinput_device_destroy(device);
591 libinput_get_fd(struct libinput *libinput)
593 return libinput->epoll_fd;
597 libinput_dispatch(struct libinput *libinput)
599 struct libinput_source *source;
600 struct epoll_event ep[32];
603 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
607 for (i = 0; i < count; ++i) {
608 source = ep[i].data.ptr;
609 if (source->fd == -1)
612 source->dispatch(source->user_data);
615 libinput_drop_destroyed_sources(libinput);
621 init_event_base(struct libinput_event *event,
622 struct libinput_device *device,
623 enum libinput_event_type type)
626 event->device = device;
630 post_base_event(struct libinput_device *device,
631 enum libinput_event_type type,
632 struct libinput_event *event)
634 struct libinput *libinput = device->seat->libinput;
635 init_event_base(event, device, type);
636 libinput_post_event(libinput, event);
640 post_device_event(struct libinput_device *device,
641 enum libinput_event_type type,
642 struct libinput_event *event)
644 init_event_base(event, device, type);
645 libinput_post_event(device->seat->libinput, event);
649 notify_added_device(struct libinput_device *device)
651 struct libinput_event_device_notify *added_device_event;
653 added_device_event = zalloc(sizeof *added_device_event);
654 if (!added_device_event)
657 post_base_event(device,
658 LIBINPUT_EVENT_DEVICE_ADDED,
659 &added_device_event->base);
663 notify_removed_device(struct libinput_device *device)
665 struct libinput_event_device_notify *removed_device_event;
667 removed_device_event = zalloc(sizeof *removed_device_event);
668 if (!removed_device_event)
671 post_base_event(device,
672 LIBINPUT_EVENT_DEVICE_REMOVED,
673 &removed_device_event->base);
677 keyboard_notify_key(struct libinput_device *device,
680 enum libinput_keyboard_key_state state)
682 struct libinput_event_keyboard *key_event;
684 key_event = zalloc(sizeof *key_event);
688 *key_event = (struct libinput_event_keyboard) {
694 post_device_event(device,
695 LIBINPUT_EVENT_KEYBOARD_KEY,
700 pointer_notify_motion(struct libinput_device *device,
705 struct libinput_event_pointer *motion_event;
707 motion_event = zalloc(sizeof *motion_event);
711 *motion_event = (struct libinput_event_pointer) {
717 post_device_event(device,
718 LIBINPUT_EVENT_POINTER_MOTION,
719 &motion_event->base);
723 pointer_notify_motion_absolute(struct libinput_device *device,
728 struct libinput_event_pointer *motion_absolute_event;
730 motion_absolute_event = zalloc(sizeof *motion_absolute_event);
731 if (!motion_absolute_event)
734 *motion_absolute_event = (struct libinput_event_pointer) {
740 post_device_event(device,
741 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
742 &motion_absolute_event->base);
746 pointer_notify_button(struct libinput_device *device,
749 enum libinput_pointer_button_state state)
751 struct libinput_event_pointer *button_event;
753 button_event = zalloc(sizeof *button_event);
757 *button_event = (struct libinput_event_pointer) {
763 post_device_event(device,
764 LIBINPUT_EVENT_POINTER_BUTTON,
765 &button_event->base);
769 pointer_notify_axis(struct libinput_device *device,
771 enum libinput_pointer_axis axis,
774 struct libinput_event_pointer *axis_event;
776 axis_event = zalloc(sizeof *axis_event);
780 *axis_event = (struct libinput_event_pointer) {
786 post_device_event(device,
787 LIBINPUT_EVENT_POINTER_AXIS,
792 touch_notify_touch(struct libinput_device *device,
797 enum libinput_touch_type touch_type)
799 struct libinput_event_touch *touch_event;
801 touch_event = zalloc(sizeof *touch_event);
805 *touch_event = (struct libinput_event_touch) {
810 .touch_type = touch_type,
813 post_device_event(device,
814 LIBINPUT_EVENT_TOUCH_TOUCH,
819 touch_notify_frame(struct libinput_device *device,
822 struct libinput_event_touch *touch_event;
824 touch_event = zalloc(sizeof *touch_event);
828 *touch_event = (struct libinput_event_touch) {
832 post_device_event(device,
833 LIBINPUT_EVENT_TOUCH_FRAME,
839 libinput_post_event(struct libinput *libinput,
840 struct libinput_event *event)
842 struct libinput_event **events = libinput->events;
843 size_t events_len = libinput->events_len;
844 size_t events_count = libinput->events_count;
849 if (events_count > events_len) {
851 events = realloc(events, events_len * sizeof *events);
853 fprintf(stderr, "Failed to reallocate event ring "
858 if (libinput->events_count > 0 && libinput->events_in == 0) {
859 libinput->events_in = libinput->events_len;
860 } else if (libinput->events_count > 0 &&
861 libinput->events_out >= libinput->events_in) {
862 move_len = libinput->events_len - libinput->events_out;
863 new_out = events_len - move_len;
864 memmove(events + new_out,
865 events + libinput->events_out,
866 move_len * sizeof *events);
867 libinput->events_out = new_out;
870 libinput->events = events;
871 libinput->events_len = events_len;
875 libinput_device_ref(event->device);
877 libinput->events_count = events_count;
878 events[libinput->events_in] = event;
879 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
882 LIBINPUT_EXPORT struct libinput_event *
883 libinput_get_event(struct libinput *libinput)
885 struct libinput_event *event;
887 if (libinput->events_count == 0)
890 event = libinput->events[libinput->events_out];
891 libinput->events_out =
892 (libinput->events_out + 1) % libinput->events_len;
893 libinput->events_count--;
898 LIBINPUT_EXPORT enum libinput_event_type
899 libinput_next_event_type(struct libinput *libinput)
901 struct libinput_event *event;
903 if (libinput->events_count == 0)
904 return LIBINPUT_EVENT_NONE;
906 event = libinput->events[libinput->events_out];
910 LIBINPUT_EXPORT void *
911 libinput_get_user_data(struct libinput *libinput)
913 return libinput->user_data;
917 libinput_resume(struct libinput *libinput)
919 return libinput->interface_backend->resume(libinput);
923 libinput_suspend(struct libinput *libinput)
925 libinput->interface_backend->suspend(libinput);
929 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
931 device->user_data = user_data;
934 LIBINPUT_EXPORT void *
935 libinput_device_get_user_data(struct libinput_device *device)
937 return device->user_data;
940 LIBINPUT_EXPORT const char *
941 libinput_device_get_sysname(struct libinput_device *device)
943 return evdev_device_get_sysname((struct evdev_device *) device);
946 LIBINPUT_EXPORT const char *
947 libinput_device_get_output_name(struct libinput_device *device)
949 return evdev_device_get_output((struct evdev_device *) device);
952 LIBINPUT_EXPORT struct libinput_seat *
953 libinput_device_get_seat(struct libinput_device *device)
959 libinput_device_led_update(struct libinput_device *device,
960 enum libinput_led leds)
962 evdev_device_led_update((struct evdev_device *) device, leds);
966 libinput_device_get_keys(struct libinput_device *device,
967 char *keys, size_t size)
969 return evdev_device_get_keys((struct evdev_device *) device,
975 libinput_device_calibrate(struct libinput_device *device,
976 float calibration[6])
978 evdev_device_calibrate((struct evdev_device *) device, calibration);
982 libinput_device_has_capability(struct libinput_device *device,
983 enum libinput_device_capability capability)
985 return evdev_device_has_capability((struct evdev_device *) device,