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;
51 struct libinput_device *device;
54 struct libinput_event_keyboard {
55 struct libinput_event base;
58 enum libinput_keyboard_key_state state;
61 struct libinput_event_pointer {
62 struct libinput_event base;
67 enum libinput_pointer_button_state state;
68 enum libinput_pointer_axis axis;
72 struct libinput_event_touch {
73 struct libinput_event base;
78 enum libinput_touch_type touch_type;
82 libinput_post_event(struct libinput *libinput,
83 struct libinput_event *event);
85 LIBINPUT_EXPORT enum libinput_event_type
86 libinput_event_get_type(struct libinput_event *event)
91 LIBINPUT_EXPORT struct libinput*
92 libinput_event_get_context(struct libinput_event *event)
94 return event->device->seat->libinput;
97 LIBINPUT_EXPORT struct libinput_device*
98 libinput_event_get_device(struct libinput_event *event)
100 return event->device;
103 LIBINPUT_EXPORT struct libinput_event_pointer*
104 libinput_event_get_pointer_event(struct libinput_event *event)
106 switch (event->type) {
107 case LIBINPUT_EVENT_NONE:
108 abort(); /* not used as actual event type */
109 case LIBINPUT_EVENT_DEVICE_ADDED:
110 case LIBINPUT_EVENT_DEVICE_REMOVED:
111 case LIBINPUT_EVENT_KEYBOARD_KEY:
113 case LIBINPUT_EVENT_POINTER_MOTION:
114 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
115 case LIBINPUT_EVENT_POINTER_BUTTON:
116 case LIBINPUT_EVENT_POINTER_AXIS:
117 return (struct libinput_event_pointer*)event;
118 case LIBINPUT_EVENT_TOUCH_TOUCH:
119 case LIBINPUT_EVENT_TOUCH_FRAME:
126 LIBINPUT_EXPORT struct libinput_event_keyboard*
127 libinput_event_get_keyboard_event(struct libinput_event *event)
129 switch (event->type) {
130 case LIBINPUT_EVENT_NONE:
131 abort(); /* not used as actual event type */
132 case LIBINPUT_EVENT_DEVICE_ADDED:
133 case LIBINPUT_EVENT_DEVICE_REMOVED:
135 case LIBINPUT_EVENT_KEYBOARD_KEY:
136 return (struct libinput_event_keyboard*)event;
137 case LIBINPUT_EVENT_POINTER_MOTION:
138 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
139 case LIBINPUT_EVENT_POINTER_BUTTON:
140 case LIBINPUT_EVENT_POINTER_AXIS:
141 case LIBINPUT_EVENT_TOUCH_TOUCH:
142 case LIBINPUT_EVENT_TOUCH_FRAME:
149 LIBINPUT_EXPORT struct libinput_event_touch*
150 libinput_event_get_touch_event(struct libinput_event *event)
152 switch (event->type) {
153 case LIBINPUT_EVENT_NONE:
154 abort(); /* not used as actual event type */
155 case LIBINPUT_EVENT_DEVICE_ADDED:
156 case LIBINPUT_EVENT_DEVICE_REMOVED:
157 case LIBINPUT_EVENT_KEYBOARD_KEY:
158 case LIBINPUT_EVENT_POINTER_MOTION:
159 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
160 case LIBINPUT_EVENT_POINTER_BUTTON:
161 case LIBINPUT_EVENT_POINTER_AXIS:
163 case LIBINPUT_EVENT_TOUCH_TOUCH:
164 case LIBINPUT_EVENT_TOUCH_FRAME:
165 return (struct libinput_event_touch*)event;
171 LIBINPUT_EXPORT struct libinput_event_device_notify*
172 libinput_event_get_device_notify_event(struct libinput_event *event)
174 switch (event->type) {
175 case LIBINPUT_EVENT_NONE:
176 abort(); /* not used as actual event type */
177 case LIBINPUT_EVENT_DEVICE_ADDED:
178 case LIBINPUT_EVENT_DEVICE_REMOVED:
179 return (struct libinput_event_device_notify*)event;
180 case LIBINPUT_EVENT_KEYBOARD_KEY:
181 case LIBINPUT_EVENT_POINTER_MOTION:
182 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
183 case LIBINPUT_EVENT_POINTER_BUTTON:
184 case LIBINPUT_EVENT_POINTER_AXIS:
185 case LIBINPUT_EVENT_TOUCH_TOUCH:
186 case LIBINPUT_EVENT_TOUCH_FRAME:
193 LIBINPUT_EXPORT uint32_t
194 libinput_event_keyboard_get_time(
195 struct libinput_event_keyboard *event)
200 LIBINPUT_EXPORT uint32_t
201 libinput_event_keyboard_get_key(
202 struct libinput_event_keyboard *event)
207 LIBINPUT_EXPORT enum libinput_keyboard_key_state
208 libinput_event_keyboard_get_key_state(
209 struct libinput_event_keyboard *event)
214 LIBINPUT_EXPORT uint32_t
215 libinput_event_pointer_get_time(
216 struct libinput_event_pointer *event)
221 LIBINPUT_EXPORT li_fixed_t
222 libinput_event_pointer_get_dx(
223 struct libinput_event_pointer *event)
228 LIBINPUT_EXPORT li_fixed_t
229 libinput_event_pointer_get_dy(
230 struct libinput_event_pointer *event)
235 LIBINPUT_EXPORT li_fixed_t
236 libinput_event_pointer_get_absolute_x(
237 struct libinput_event_pointer *event)
242 LIBINPUT_EXPORT li_fixed_t
243 libinput_event_pointer_get_absolute_y(
244 struct libinput_event_pointer *event)
249 LIBINPUT_EXPORT uint32_t
250 libinput_event_pointer_get_button(
251 struct libinput_event_pointer *event)
253 return event->button;
256 LIBINPUT_EXPORT enum libinput_pointer_button_state
257 libinput_event_pointer_get_button_state(
258 struct libinput_event_pointer *event)
263 LIBINPUT_EXPORT enum libinput_pointer_axis
264 libinput_event_pointer_get_axis(
265 struct libinput_event_pointer *event)
270 LIBINPUT_EXPORT li_fixed_t
271 libinput_event_pointer_get_axis_value(
272 struct libinput_event_pointer *event)
277 LIBINPUT_EXPORT uint32_t
278 libinput_event_touch_get_time(
279 struct libinput_event_touch *event)
284 LIBINPUT_EXPORT uint32_t
285 libinput_event_touch_get_slot(
286 struct libinput_event_touch *event)
291 LIBINPUT_EXPORT li_fixed_t
292 libinput_event_touch_get_x(
293 struct libinput_event_touch *event)
298 LIBINPUT_EXPORT li_fixed_t
299 libinput_event_touch_get_y(
300 struct libinput_event_touch *event)
305 LIBINPUT_EXPORT enum libinput_touch_type
306 libinput_event_touch_get_touch_type(
307 struct libinput_event_touch *event)
309 return event->touch_type;
312 struct libinput_source *
313 libinput_add_fd(struct libinput *libinput,
315 libinput_source_dispatch_t dispatch,
318 struct libinput_source *source;
319 struct epoll_event ep;
321 source = malloc(sizeof *source);
325 source->dispatch = dispatch;
326 source->user_data = user_data;
329 memset(&ep, 0, sizeof ep);
331 ep.data.ptr = source;
333 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
343 libinput_remove_source(struct libinput *libinput,
344 struct libinput_source *source)
346 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
349 list_insert(&libinput->source_destroy_list, &source->link);
353 libinput_init(struct libinput *libinput,
354 const struct libinput_interface *interface,
355 const struct libinput_interface_backend *interface_backend,
358 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
359 if (libinput->epoll_fd < 0)
362 libinput->events_len = 4;
363 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
364 if (!libinput->events) {
365 close(libinput->epoll_fd);
369 libinput->interface = interface;
370 libinput->interface_backend = interface_backend;
371 libinput->user_data = user_data;
372 list_init(&libinput->source_destroy_list);
373 list_init(&libinput->seat_list);
379 libinput_device_destroy(struct libinput_device *device);
382 libinput_seat_destroy(struct libinput_seat *seat);
385 libinput_drop_destroyed_sources(struct libinput *libinput)
387 struct libinput_source *source, *next;
389 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
391 list_init(&libinput->source_destroy_list);
395 libinput_destroy(struct libinput *libinput)
397 struct libinput_event *event;
398 struct libinput_device *device, *next_device;
399 struct libinput_seat *seat, *next_seat;
401 if (libinput == NULL)
404 libinput_suspend(libinput);
406 libinput->interface_backend->destroy(libinput);
408 while ((event = libinput_get_event(libinput)))
409 libinput_event_destroy(event);
411 libinput_drop_destroyed_sources(libinput);
413 free(libinput->events);
415 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
416 list_for_each_safe(device, next_device,
419 libinput_device_destroy(device);
421 libinput_seat_destroy(seat);
424 close(libinput->epoll_fd);
429 libinput_event_destroy(struct libinput_event *event)
435 libinput_device_unref(event->device);
441 open_restricted(struct libinput *libinput,
442 const char *path, int flags)
444 return libinput->interface->open_restricted(path,
446 libinput->user_data);
450 close_restricted(struct libinput *libinput, int fd)
452 return libinput->interface->close_restricted(fd, libinput->user_data);
456 libinput_seat_init(struct libinput_seat *seat,
457 struct libinput *libinput,
458 const char *physical_name,
459 const char *logical_name,
460 libinput_seat_destroy_func destroy)
463 seat->libinput = libinput;
464 seat->physical_name = strdup(physical_name);
465 seat->logical_name = strdup(logical_name);
466 seat->destroy = destroy;
467 list_init(&seat->devices_list);
471 libinput_seat_ref(struct libinput_seat *seat)
477 libinput_seat_destroy(struct libinput_seat *seat)
479 list_remove(&seat->link);
480 free(seat->logical_name);
481 free(seat->physical_name);
486 libinput_seat_unref(struct libinput_seat *seat)
488 assert(seat->refcount > 0);
490 if (seat->refcount == 0)
491 libinput_seat_destroy(seat);
495 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
497 seat->user_data = user_data;
500 LIBINPUT_EXPORT void *
501 libinput_seat_get_user_data(struct libinput_seat *seat)
503 return seat->user_data;
506 LIBINPUT_EXPORT const char *
507 libinput_seat_get_physical_name(struct libinput_seat *seat)
509 return seat->physical_name;
512 LIBINPUT_EXPORT const char *
513 libinput_seat_get_logical_name(struct libinput_seat *seat)
515 return seat->logical_name;
519 libinput_device_init(struct libinput_device *device,
520 struct libinput_seat *seat)
523 device->refcount = 1;
527 libinput_device_ref(struct libinput_device *device)
533 libinput_device_destroy(struct libinput_device *device)
535 evdev_device_destroy((struct evdev_device *) device);
539 libinput_device_unref(struct libinput_device *device)
541 assert(device->refcount > 0);
543 if (device->refcount == 0)
544 libinput_device_destroy(device);
548 libinput_get_fd(struct libinput *libinput)
550 return libinput->epoll_fd;
554 libinput_dispatch(struct libinput *libinput)
556 struct libinput_source *source;
557 struct epoll_event ep[32];
560 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
564 for (i = 0; i < count; ++i) {
565 source = ep[i].data.ptr;
566 if (source->fd == -1)
569 source->dispatch(source->user_data);
572 libinput_drop_destroyed_sources(libinput);
578 init_event_base(struct libinput_event *event,
579 struct libinput_device *device,
580 enum libinput_event_type type)
583 event->device = device;
587 post_base_event(struct libinput_device *device,
588 enum libinput_event_type type,
589 struct libinput_event *event)
591 struct libinput *libinput = device->seat->libinput;
592 init_event_base(event, device, type);
593 libinput_post_event(libinput, event);
597 post_device_event(struct libinput_device *device,
598 enum libinput_event_type type,
599 struct libinput_event *event)
601 init_event_base(event, device, type);
602 libinput_post_event(device->seat->libinput, event);
606 notify_added_device(struct libinput_device *device)
608 struct libinput_event_device_notify *added_device_event;
610 added_device_event = zalloc(sizeof *added_device_event);
611 if (!added_device_event)
614 *added_device_event = (struct libinput_event_device_notify) {
618 post_base_event(device,
619 LIBINPUT_EVENT_DEVICE_ADDED,
620 &added_device_event->base);
624 notify_removed_device(struct libinput_device *device)
626 struct libinput_event_device_notify *removed_device_event;
628 removed_device_event = zalloc(sizeof *removed_device_event);
629 if (!removed_device_event)
632 *removed_device_event = (struct libinput_event_device_notify) {
636 post_base_event(device,
637 LIBINPUT_EVENT_DEVICE_REMOVED,
638 &removed_device_event->base);
642 keyboard_notify_key(struct libinput_device *device,
645 enum libinput_keyboard_key_state state)
647 struct libinput_event_keyboard *key_event;
649 key_event = zalloc(sizeof *key_event);
653 *key_event = (struct libinput_event_keyboard) {
659 post_device_event(device,
660 LIBINPUT_EVENT_KEYBOARD_KEY,
665 pointer_notify_motion(struct libinput_device *device,
670 struct libinput_event_pointer *motion_event;
672 motion_event = zalloc(sizeof *motion_event);
676 *motion_event = (struct libinput_event_pointer) {
682 post_device_event(device,
683 LIBINPUT_EVENT_POINTER_MOTION,
684 &motion_event->base);
688 pointer_notify_motion_absolute(struct libinput_device *device,
693 struct libinput_event_pointer *motion_absolute_event;
695 motion_absolute_event = zalloc(sizeof *motion_absolute_event);
696 if (!motion_absolute_event)
699 *motion_absolute_event = (struct libinput_event_pointer) {
705 post_device_event(device,
706 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
707 &motion_absolute_event->base);
711 pointer_notify_button(struct libinput_device *device,
714 enum libinput_pointer_button_state state)
716 struct libinput_event_pointer *button_event;
718 button_event = zalloc(sizeof *button_event);
722 *button_event = (struct libinput_event_pointer) {
728 post_device_event(device,
729 LIBINPUT_EVENT_POINTER_BUTTON,
730 &button_event->base);
734 pointer_notify_axis(struct libinput_device *device,
736 enum libinput_pointer_axis axis,
739 struct libinput_event_pointer *axis_event;
741 axis_event = zalloc(sizeof *axis_event);
745 *axis_event = (struct libinput_event_pointer) {
751 post_device_event(device,
752 LIBINPUT_EVENT_POINTER_AXIS,
757 touch_notify_touch(struct libinput_device *device,
762 enum libinput_touch_type touch_type)
764 struct libinput_event_touch *touch_event;
766 touch_event = zalloc(sizeof *touch_event);
770 *touch_event = (struct libinput_event_touch) {
775 .touch_type = touch_type,
778 post_device_event(device,
779 LIBINPUT_EVENT_TOUCH_TOUCH,
784 touch_notify_frame(struct libinput_device *device,
787 struct libinput_event_touch *touch_event;
789 touch_event = zalloc(sizeof *touch_event);
793 *touch_event = (struct libinput_event_touch) {
797 post_device_event(device,
798 LIBINPUT_EVENT_TOUCH_FRAME,
804 libinput_post_event(struct libinput *libinput,
805 struct libinput_event *event)
807 struct libinput_event **events = libinput->events;
808 size_t events_len = libinput->events_len;
809 size_t events_count = libinput->events_count;
814 if (events_count > events_len) {
816 events = realloc(events, events_len * sizeof *events);
818 fprintf(stderr, "Failed to reallocate event ring "
823 if (libinput->events_count > 0 && libinput->events_in == 0) {
824 libinput->events_in = libinput->events_len;
825 } else if (libinput->events_count > 0 &&
826 libinput->events_out >= libinput->events_in) {
827 move_len = libinput->events_len - libinput->events_out;
828 new_out = events_len - move_len;
829 memmove(events + new_out,
830 events + libinput->events_out,
831 move_len * sizeof *events);
832 libinput->events_out = new_out;
835 libinput->events = events;
836 libinput->events_len = events_len;
840 libinput_device_ref(event->device);
842 libinput->events_count = events_count;
843 events[libinput->events_in] = event;
844 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
847 LIBINPUT_EXPORT struct libinput_event *
848 libinput_get_event(struct libinput *libinput)
850 struct libinput_event *event;
852 if (libinput->events_count == 0)
855 event = libinput->events[libinput->events_out];
856 libinput->events_out =
857 (libinput->events_out + 1) % libinput->events_len;
858 libinput->events_count--;
863 LIBINPUT_EXPORT enum libinput_event_type
864 libinput_next_event_type(struct libinput *libinput)
866 struct libinput_event *event;
868 if (libinput->events_count == 0)
869 return LIBINPUT_EVENT_NONE;
871 event = libinput->events[libinput->events_out];
875 LIBINPUT_EXPORT void *
876 libinput_get_user_data(struct libinput *libinput)
878 return libinput->user_data;
882 libinput_resume(struct libinput *libinput)
884 return libinput->interface_backend->resume(libinput);
888 libinput_suspend(struct libinput *libinput)
890 libinput->interface_backend->suspend(libinput);
894 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
896 device->user_data = user_data;
899 LIBINPUT_EXPORT void *
900 libinput_device_get_user_data(struct libinput_device *device)
902 return device->user_data;
905 LIBINPUT_EXPORT const char *
906 libinput_device_get_sysname(struct libinput_device *device)
908 return evdev_device_get_sysname((struct evdev_device *) device);
911 LIBINPUT_EXPORT const char *
912 libinput_device_get_output_name(struct libinput_device *device)
914 return evdev_device_get_output((struct evdev_device *) device);
917 LIBINPUT_EXPORT struct libinput_seat *
918 libinput_device_get_seat(struct libinput_device *device)
924 libinput_device_led_update(struct libinput_device *device,
925 enum libinput_led leds)
927 evdev_device_led_update((struct evdev_device *) device, leds);
931 libinput_device_get_keys(struct libinput_device *device,
932 char *keys, size_t size)
934 return evdev_device_get_keys((struct evdev_device *) device,
940 libinput_device_calibrate(struct libinput_device *device,
941 float calibration[6])
943 evdev_device_calibrate((struct evdev_device *) device, calibration);
947 libinput_device_has_capability(struct libinput_device *device,
948 enum libinput_device_capability capability)
950 return evdev_device_has_capability((struct evdev_device *) device,