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 enum libinput_event_class {
38 LIBINPUT_EVENT_CLASS_NONE,
39 LIBINPUT_EVENT_CLASS_BASE,
40 LIBINPUT_EVENT_CLASS_SEAT,
41 LIBINPUT_EVENT_CLASS_DEVICE,
44 struct libinput_source {
45 libinput_source_dispatch_t dispatch;
51 struct libinput_event {
52 enum libinput_event_type type;
53 struct libinput_device *device;
56 struct libinput_event_device_notify {
57 struct libinput_event base;
58 struct libinput_device *device;
61 struct libinput_event_keyboard {
62 struct libinput_event base;
65 enum libinput_keyboard_key_state state;
68 struct libinput_event_pointer {
69 struct libinput_event base;
74 enum libinput_pointer_button_state state;
75 enum libinput_pointer_axis axis;
79 struct libinput_event_touch {
80 struct libinput_event base;
85 enum libinput_touch_type touch_type;
89 libinput_post_event(struct libinput *libinput,
90 struct libinput_event *event);
92 LIBINPUT_EXPORT enum libinput_event_type
93 libinput_event_get_type(struct libinput_event *event)
98 LIBINPUT_EXPORT struct libinput*
99 libinput_event_get_context(struct libinput_event *event)
101 return event->device->seat->libinput;
104 LIBINPUT_EXPORT struct libinput_device*
105 libinput_event_get_device(struct libinput_event *event)
107 return event->device;
110 LIBINPUT_EXPORT uint32_t
111 libinput_event_keyboard_get_time(
112 struct libinput_event_keyboard *event)
117 LIBINPUT_EXPORT uint32_t
118 libinput_event_keyboard_get_key(
119 struct libinput_event_keyboard *event)
124 LIBINPUT_EXPORT enum libinput_keyboard_key_state
125 libinput_event_keyboard_get_key_state(
126 struct libinput_event_keyboard *event)
131 LIBINPUT_EXPORT uint32_t
132 libinput_event_pointer_get_time(
133 struct libinput_event_pointer *event)
138 LIBINPUT_EXPORT li_fixed_t
139 libinput_event_pointer_get_dx(
140 struct libinput_event_pointer *event)
145 LIBINPUT_EXPORT li_fixed_t
146 libinput_event_pointer_get_dy(
147 struct libinput_event_pointer *event)
152 LIBINPUT_EXPORT li_fixed_t
153 libinput_event_pointer_get_absolute_x(
154 struct libinput_event_pointer *event)
159 LIBINPUT_EXPORT li_fixed_t
160 libinput_event_pointer_get_absolute_y(
161 struct libinput_event_pointer *event)
166 LIBINPUT_EXPORT uint32_t
167 libinput_event_pointer_get_button(
168 struct libinput_event_pointer *event)
170 return event->button;
173 LIBINPUT_EXPORT enum libinput_pointer_button_state
174 libinput_event_pointer_get_button_state(
175 struct libinput_event_pointer *event)
180 LIBINPUT_EXPORT enum libinput_pointer_axis
181 libinput_event_pointer_get_axis(
182 struct libinput_event_pointer *event)
187 LIBINPUT_EXPORT li_fixed_t
188 libinput_event_pointer_get_axis_value(
189 struct libinput_event_pointer *event)
194 LIBINPUT_EXPORT uint32_t
195 libinput_event_touch_get_time(
196 struct libinput_event_touch *event)
201 LIBINPUT_EXPORT uint32_t
202 libinput_event_touch_get_slot(
203 struct libinput_event_touch *event)
208 LIBINPUT_EXPORT li_fixed_t
209 libinput_event_touch_get_x(
210 struct libinput_event_touch *event)
215 LIBINPUT_EXPORT li_fixed_t
216 libinput_event_touch_get_y(
217 struct libinput_event_touch *event)
222 LIBINPUT_EXPORT enum libinput_touch_type
223 libinput_event_touch_get_touch_type(
224 struct libinput_event_touch *event)
226 return event->touch_type;
229 struct libinput_source *
230 libinput_add_fd(struct libinput *libinput,
232 libinput_source_dispatch_t dispatch,
235 struct libinput_source *source;
236 struct epoll_event ep;
238 source = malloc(sizeof *source);
242 source->dispatch = dispatch;
243 source->user_data = user_data;
246 memset(&ep, 0, sizeof ep);
248 ep.data.ptr = source;
250 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
260 libinput_remove_source(struct libinput *libinput,
261 struct libinput_source *source)
263 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
266 list_insert(&libinput->source_destroy_list, &source->link);
270 libinput_init(struct libinput *libinput,
271 const struct libinput_interface *interface,
272 const struct libinput_interface_backend *interface_backend,
275 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
276 if (libinput->epoll_fd < 0)
279 libinput->events_len = 4;
280 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
281 if (!libinput->events) {
282 close(libinput->epoll_fd);
286 libinput->interface = interface;
287 libinput->interface_backend = interface_backend;
288 libinput->user_data = user_data;
289 list_init(&libinput->source_destroy_list);
290 list_init(&libinput->seat_list);
296 libinput_device_destroy(struct libinput_device *device);
299 libinput_seat_destroy(struct libinput_seat *seat);
302 libinput_drop_destroyed_sources(struct libinput *libinput)
304 struct libinput_source *source, *next;
306 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
308 list_init(&libinput->source_destroy_list);
312 libinput_destroy(struct libinput *libinput)
314 struct libinput_event *event;
315 struct libinput_device *device, *next_device;
316 struct libinput_seat *seat, *next_seat;
318 if (libinput == NULL)
321 libinput_suspend(libinput);
323 libinput->interface_backend->destroy(libinput);
325 while ((event = libinput_get_event(libinput)))
326 libinput_event_destroy(event);
328 libinput_drop_destroyed_sources(libinput);
330 free(libinput->events);
332 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
333 list_for_each_safe(device, next_device,
336 libinput_device_destroy(device);
338 libinput_seat_destroy(seat);
341 close(libinput->epoll_fd);
345 static enum libinput_event_class
346 libinput_event_get_class(struct libinput_event *event)
348 switch (event->type) {
349 case LIBINPUT_EVENT_NONE:
350 return LIBINPUT_EVENT_CLASS_NONE;
352 case LIBINPUT_EVENT_DEVICE_ADDED:
353 case LIBINPUT_EVENT_DEVICE_REMOVED:
354 return LIBINPUT_EVENT_CLASS_BASE;
356 case LIBINPUT_EVENT_KEYBOARD_KEY:
357 case LIBINPUT_EVENT_POINTER_MOTION:
358 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
359 case LIBINPUT_EVENT_POINTER_BUTTON:
360 case LIBINPUT_EVENT_POINTER_AXIS:
361 case LIBINPUT_EVENT_TOUCH_TOUCH:
362 return LIBINPUT_EVENT_CLASS_DEVICE;
365 /* We should never end up here. */
370 libinput_event_destroy(struct libinput_event *event)
375 switch (libinput_event_get_class(event)) {
376 case LIBINPUT_EVENT_CLASS_NONE:
377 case LIBINPUT_EVENT_CLASS_BASE:
379 case LIBINPUT_EVENT_CLASS_SEAT:
380 libinput_seat_unref(event->device->seat);
382 case LIBINPUT_EVENT_CLASS_DEVICE:
383 libinput_device_unref(event->device);
391 open_restricted(struct libinput *libinput,
392 const char *path, int flags)
394 return libinput->interface->open_restricted(path,
396 libinput->user_data);
400 close_restricted(struct libinput *libinput, int fd)
402 return libinput->interface->close_restricted(fd, libinput->user_data);
406 libinput_seat_init(struct libinput_seat *seat,
407 struct libinput *libinput,
408 const char *physical_name,
409 const char *logical_name,
410 libinput_seat_destroy_func destroy)
413 seat->libinput = libinput;
414 seat->physical_name = strdup(physical_name);
415 seat->logical_name = strdup(logical_name);
416 seat->destroy = destroy;
417 list_init(&seat->devices_list);
421 libinput_seat_ref(struct libinput_seat *seat)
427 libinput_seat_destroy(struct libinput_seat *seat)
429 list_remove(&seat->link);
430 free(seat->logical_name);
431 free(seat->physical_name);
436 libinput_seat_unref(struct libinput_seat *seat)
438 assert(seat->refcount > 0);
440 if (seat->refcount == 0)
441 libinput_seat_destroy(seat);
445 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
447 seat->user_data = user_data;
450 LIBINPUT_EXPORT void *
451 libinput_seat_get_user_data(struct libinput_seat *seat)
453 return seat->user_data;
456 LIBINPUT_EXPORT const char *
457 libinput_seat_get_physical_name(struct libinput_seat *seat)
459 return seat->physical_name;
462 LIBINPUT_EXPORT const char *
463 libinput_seat_get_logical_name(struct libinput_seat *seat)
465 return seat->logical_name;
469 libinput_device_init(struct libinput_device *device,
470 struct libinput_seat *seat)
473 device->refcount = 1;
477 libinput_device_ref(struct libinput_device *device)
483 libinput_device_destroy(struct libinput_device *device)
485 evdev_device_destroy((struct evdev_device *) device);
489 libinput_device_unref(struct libinput_device *device)
491 assert(device->refcount > 0);
493 if (device->refcount == 0)
494 libinput_device_destroy(device);
498 libinput_get_fd(struct libinput *libinput)
500 return libinput->epoll_fd;
504 libinput_dispatch(struct libinput *libinput)
506 struct libinput_source *source;
507 struct epoll_event ep[32];
510 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
514 for (i = 0; i < count; ++i) {
515 source = ep[i].data.ptr;
516 if (source->fd == -1)
519 source->dispatch(source->user_data);
522 libinput_drop_destroyed_sources(libinput);
528 init_event_base(struct libinput_event *event,
529 struct libinput_device *device,
530 enum libinput_event_type type)
533 event->device = device;
537 post_base_event(struct libinput_device *device,
538 enum libinput_event_type type,
539 struct libinput_event *event)
541 struct libinput *libinput = device->seat->libinput;
542 init_event_base(event, device, type);
543 libinput_post_event(libinput, event);
547 post_device_event(struct libinput_device *device,
548 enum libinput_event_type type,
549 struct libinput_event *event)
551 init_event_base(event, device, type);
552 libinput_post_event(device->seat->libinput, event);
556 notify_added_device(struct libinput_device *device)
558 struct libinput_event_device_notify *added_device_event;
560 added_device_event = malloc(sizeof *added_device_event);
561 if (!added_device_event)
564 *added_device_event = (struct libinput_event_device_notify) {
568 post_base_event(device,
569 LIBINPUT_EVENT_DEVICE_ADDED,
570 &added_device_event->base);
574 notify_removed_device(struct libinput_device *device)
576 struct libinput_event_device_notify *removed_device_event;
578 removed_device_event = malloc(sizeof *removed_device_event);
579 if (!removed_device_event)
582 *removed_device_event = (struct libinput_event_device_notify) {
586 post_base_event(device,
587 LIBINPUT_EVENT_DEVICE_REMOVED,
588 &removed_device_event->base);
592 keyboard_notify_key(struct libinput_device *device,
595 enum libinput_keyboard_key_state state)
597 struct libinput_event_keyboard *key_event;
599 key_event = malloc(sizeof *key_event);
603 *key_event = (struct libinput_event_keyboard) {
609 post_device_event(device,
610 LIBINPUT_EVENT_KEYBOARD_KEY,
615 pointer_notify_motion(struct libinput_device *device,
620 struct libinput_event_pointer *motion_event;
622 motion_event = malloc(sizeof *motion_event);
626 *motion_event = (struct libinput_event_pointer) {
632 post_device_event(device,
633 LIBINPUT_EVENT_POINTER_MOTION,
634 &motion_event->base);
638 pointer_notify_motion_absolute(struct libinput_device *device,
643 struct libinput_event_pointer *motion_absolute_event;
645 motion_absolute_event = malloc(sizeof *motion_absolute_event);
646 if (!motion_absolute_event)
649 *motion_absolute_event = (struct libinput_event_pointer) {
655 post_device_event(device,
656 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
657 &motion_absolute_event->base);
661 pointer_notify_button(struct libinput_device *device,
664 enum libinput_pointer_button_state state)
666 struct libinput_event_pointer *button_event;
668 button_event = malloc(sizeof *button_event);
672 *button_event = (struct libinput_event_pointer) {
678 post_device_event(device,
679 LIBINPUT_EVENT_POINTER_BUTTON,
680 &button_event->base);
684 pointer_notify_axis(struct libinput_device *device,
686 enum libinput_pointer_axis axis,
689 struct libinput_event_pointer *axis_event;
691 axis_event = malloc(sizeof *axis_event);
695 *axis_event = (struct libinput_event_pointer) {
701 post_device_event(device,
702 LIBINPUT_EVENT_POINTER_AXIS,
707 touch_notify_touch(struct libinput_device *device,
712 enum libinput_touch_type touch_type)
714 struct libinput_event_touch *touch_event;
716 touch_event = malloc(sizeof *touch_event);
720 *touch_event = (struct libinput_event_touch) {
725 .touch_type = touch_type,
728 post_device_event(device,
729 LIBINPUT_EVENT_TOUCH_TOUCH,
734 libinput_post_event(struct libinput *libinput,
735 struct libinput_event *event)
737 struct libinput_event **events = libinput->events;
738 size_t events_len = libinput->events_len;
739 size_t events_count = libinput->events_count;
744 if (events_count > events_len) {
746 events = realloc(events, events_len * sizeof *events);
748 fprintf(stderr, "Failed to reallocate event ring "
753 if (libinput->events_count > 0 && libinput->events_in == 0) {
754 libinput->events_in = libinput->events_len;
755 } else if (libinput->events_count > 0 &&
756 libinput->events_out >= libinput->events_in) {
757 move_len = libinput->events_len - libinput->events_out;
758 new_out = events_len - move_len;
759 memmove(events + new_out,
760 events + libinput->events_out,
761 move_len * sizeof *events);
762 libinput->events_out = new_out;
765 libinput->events = events;
766 libinput->events_len = events_len;
769 switch (libinput_event_get_class(event)) {
770 case LIBINPUT_EVENT_CLASS_NONE:
771 case LIBINPUT_EVENT_CLASS_BASE:
773 case LIBINPUT_EVENT_CLASS_SEAT:
774 libinput_seat_ref(event->device->seat);
776 case LIBINPUT_EVENT_CLASS_DEVICE:
777 libinput_device_ref(event->device);
781 libinput->events_count = events_count;
782 events[libinput->events_in] = event;
783 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
786 LIBINPUT_EXPORT struct libinput_event *
787 libinput_get_event(struct libinput *libinput)
789 struct libinput_event *event;
791 if (libinput->events_count == 0)
794 event = libinput->events[libinput->events_out];
795 libinput->events_out =
796 (libinput->events_out + 1) % libinput->events_len;
797 libinput->events_count--;
802 LIBINPUT_EXPORT enum libinput_event_type
803 libinput_next_event_type(struct libinput *libinput)
805 struct libinput_event *event;
807 if (libinput->events_count == 0)
808 return LIBINPUT_EVENT_NONE;
810 event = libinput->events[libinput->events_out];
814 LIBINPUT_EXPORT void *
815 libinput_get_user_data(struct libinput *libinput)
817 return libinput->user_data;
821 libinput_resume(struct libinput *libinput)
823 return libinput->interface_backend->resume(libinput);
827 libinput_suspend(struct libinput *libinput)
829 libinput->interface_backend->suspend(libinput);
833 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
835 device->user_data = user_data;
838 LIBINPUT_EXPORT void *
839 libinput_device_get_user_data(struct libinput_device *device)
841 return device->user_data;
844 LIBINPUT_EXPORT const char *
845 libinput_device_get_sysname(struct libinput_device *device)
847 return evdev_device_get_sysname((struct evdev_device *) device);
850 LIBINPUT_EXPORT const char *
851 libinput_device_get_output_name(struct libinput_device *device)
853 return evdev_device_get_output((struct evdev_device *) device);
856 LIBINPUT_EXPORT struct libinput_seat *
857 libinput_device_get_seat(struct libinput_device *device)
863 libinput_device_led_update(struct libinput_device *device,
864 enum libinput_led leds)
866 evdev_device_led_update((struct evdev_device *) device, leds);
870 libinput_device_get_keys(struct libinput_device *device,
871 char *keys, size_t size)
873 return evdev_device_get_keys((struct evdev_device *) device,
879 libinput_device_calibrate(struct libinput_device *device,
880 float calibration[6])
882 evdev_device_calibrate((struct evdev_device *) device, calibration);
886 libinput_device_has_capability(struct libinput_device *device,
887 enum libinput_device_capability capability)
889 return evdev_device_has_capability((struct evdev_device *) device,