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 *libinput;
54 union libinput_event_target target;
57 struct libinput_event_added_device {
58 struct libinput_event base;
59 struct libinput_device *device;
62 struct libinput_event_removed_device {
63 struct libinput_event base;
64 struct libinput_device *device;
67 struct libinput_event_keyboard_key {
68 struct libinput_event base;
71 enum libinput_keyboard_key_state state;
74 struct libinput_event_pointer_motion {
75 struct libinput_event base;
81 struct libinput_event_pointer_motion_absolute {
82 struct libinput_event base;
88 struct libinput_event_pointer_button {
89 struct libinput_event base;
92 enum libinput_pointer_button_state state;
95 struct libinput_event_pointer_axis {
96 struct libinput_event base;
98 enum libinput_pointer_axis axis;
102 struct libinput_event_touch_touch {
103 struct libinput_event base;
108 enum libinput_touch_type touch_type;
112 libinput_post_event(struct libinput *libinput,
113 struct libinput_event *event);
115 LIBINPUT_EXPORT enum libinput_event_type
116 libinput_event_get_type(struct libinput_event *event)
121 LIBINPUT_EXPORT union libinput_event_target
122 libinput_event_get_target(struct libinput_event *event)
124 return event->target;
127 LIBINPUT_EXPORT struct libinput*
128 libinput_event_get_context(struct libinput_event *event)
130 return event->libinput;
133 LIBINPUT_EXPORT struct libinput_device *
134 libinput_event_added_device_get_device(
135 struct libinput_event_added_device *event)
137 return event->device;
140 LIBINPUT_EXPORT struct libinput_device *
141 libinput_event_removed_device_get_device(
142 struct libinput_event_removed_device *event)
144 return event->device;
147 LIBINPUT_EXPORT uint32_t
148 libinput_event_keyboard_key_get_time(
149 struct libinput_event_keyboard_key *event)
154 LIBINPUT_EXPORT uint32_t
155 libinput_event_keyboard_key_get_key(
156 struct libinput_event_keyboard_key *event)
161 LIBINPUT_EXPORT enum libinput_keyboard_key_state
162 libinput_event_keyboard_key_get_state(
163 struct libinput_event_keyboard_key *event)
168 LIBINPUT_EXPORT uint32_t
169 libinput_event_pointer_motion_get_time(
170 struct libinput_event_pointer_motion *event)
175 LIBINPUT_EXPORT li_fixed_t
176 libinput_event_pointer_motion_get_dx(
177 struct libinput_event_pointer_motion *event)
182 LIBINPUT_EXPORT li_fixed_t
183 libinput_event_pointer_motion_get_dy(
184 struct libinput_event_pointer_motion *event)
189 LIBINPUT_EXPORT uint32_t
190 libinput_event_pointer_motion_absolute_get_time(
191 struct libinput_event_pointer_motion_absolute *event)
196 LIBINPUT_EXPORT li_fixed_t
197 libinput_event_pointer_motion_absolute_get_x(
198 struct libinput_event_pointer_motion_absolute *event)
203 LIBINPUT_EXPORT li_fixed_t
204 libinput_event_pointer_motion_absolute_get_y(
205 struct libinput_event_pointer_motion_absolute *event)
210 LIBINPUT_EXPORT uint32_t
211 libinput_event_pointer_button_get_time(
212 struct libinput_event_pointer_button *event)
217 LIBINPUT_EXPORT uint32_t
218 libinput_event_pointer_button_get_button(
219 struct libinput_event_pointer_button *event)
221 return event->button;
224 LIBINPUT_EXPORT enum libinput_pointer_button_state
225 libinput_event_pointer_button_get_state(
226 struct libinput_event_pointer_button *event)
231 LIBINPUT_EXPORT uint32_t
232 libinput_event_pointer_axis_get_time(
233 struct libinput_event_pointer_axis *event)
238 LIBINPUT_EXPORT enum libinput_pointer_axis
239 libinput_event_pointer_axis_get_axis(
240 struct libinput_event_pointer_axis *event)
245 LIBINPUT_EXPORT li_fixed_t
246 libinput_event_pointer_axis_get_value(
247 struct libinput_event_pointer_axis *event)
252 LIBINPUT_EXPORT uint32_t
253 libinput_event_touch_touch_get_time(
254 struct libinput_event_touch_touch *event)
259 LIBINPUT_EXPORT uint32_t
260 libinput_event_touch_touch_get_slot(
261 struct libinput_event_touch_touch *event)
266 LIBINPUT_EXPORT li_fixed_t
267 libinput_event_touch_touch_get_x(
268 struct libinput_event_touch_touch *event)
273 LIBINPUT_EXPORT li_fixed_t
274 libinput_event_touch_touch_get_y(
275 struct libinput_event_touch_touch *event)
280 LIBINPUT_EXPORT enum libinput_touch_type
281 libinput_event_touch_touch_get_touch_type(
282 struct libinput_event_touch_touch *event)
284 return event->touch_type;
287 struct libinput_source *
288 libinput_add_fd(struct libinput *libinput,
290 libinput_source_dispatch_t dispatch,
293 struct libinput_source *source;
294 struct epoll_event ep;
296 source = malloc(sizeof *source);
300 source->dispatch = dispatch;
301 source->user_data = user_data;
304 memset(&ep, 0, sizeof ep);
306 ep.data.ptr = source;
308 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
318 libinput_remove_source(struct libinput *libinput,
319 struct libinput_source *source)
321 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
324 list_insert(&libinput->source_destroy_list, &source->link);
328 libinput_init(struct libinput *libinput,
329 const struct libinput_interface *interface,
330 const struct libinput_interface_backend *interface_backend,
333 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
334 if (libinput->epoll_fd < 0)
337 libinput->events_len = 4;
338 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
339 if (!libinput->events) {
340 close(libinput->epoll_fd);
344 libinput->interface = interface;
345 libinput->interface_backend = interface_backend;
346 libinput->user_data = user_data;
347 list_init(&libinput->source_destroy_list);
348 list_init(&libinput->seat_list);
354 libinput_device_destroy(struct libinput_device *device);
357 libinput_seat_destroy(struct libinput_seat *seat);
360 libinput_drop_destroyed_sources(struct libinput *libinput)
362 struct libinput_source *source, *next;
364 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
366 list_init(&libinput->source_destroy_list);
370 libinput_destroy(struct libinput *libinput)
372 struct libinput_event *event;
373 struct libinput_device *device, *next_device;
374 struct libinput_seat *seat, *next_seat;
376 if (libinput == NULL)
379 libinput_suspend(libinput);
381 libinput->interface_backend->destroy(libinput);
383 while ((event = libinput_get_event(libinput)))
384 libinput_event_destroy(event);
386 libinput_drop_destroyed_sources(libinput);
388 free(libinput->events);
390 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
391 list_for_each_safe(device, next_device,
394 libinput_device_destroy(device);
396 libinput_seat_destroy(seat);
399 close(libinput->epoll_fd);
403 static enum libinput_event_class
404 libinput_event_get_class(struct libinput_event *event)
406 switch (event->type) {
407 case LIBINPUT_EVENT_NONE:
408 return LIBINPUT_EVENT_CLASS_NONE;
410 case LIBINPUT_EVENT_ADDED_DEVICE:
411 case LIBINPUT_EVENT_REMOVED_DEVICE:
412 return LIBINPUT_EVENT_CLASS_BASE;
414 case LIBINPUT_EVENT_KEYBOARD_KEY:
415 case LIBINPUT_EVENT_POINTER_MOTION:
416 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
417 case LIBINPUT_EVENT_POINTER_BUTTON:
418 case LIBINPUT_EVENT_POINTER_AXIS:
419 case LIBINPUT_EVENT_TOUCH_TOUCH:
420 return LIBINPUT_EVENT_CLASS_DEVICE;
423 /* We should never end up here. */
428 libinput_event_destroy(struct libinput_event *event)
433 switch (libinput_event_get_class(event)) {
434 case LIBINPUT_EVENT_CLASS_NONE:
435 case LIBINPUT_EVENT_CLASS_BASE:
437 case LIBINPUT_EVENT_CLASS_SEAT:
438 libinput_seat_unref(event->target.seat);
440 case LIBINPUT_EVENT_CLASS_DEVICE:
441 libinput_device_unref(event->target.device);
449 open_restricted(struct libinput *libinput,
450 const char *path, int flags)
452 return libinput->interface->open_restricted(path,
454 libinput->user_data);
458 close_restricted(struct libinput *libinput, int fd)
460 return libinput->interface->close_restricted(fd, libinput->user_data);
464 libinput_seat_init(struct libinput_seat *seat,
465 struct libinput *libinput,
466 const char *physical_name,
467 const char *logical_name,
468 libinput_seat_destroy_func destroy)
471 seat->libinput = libinput;
472 seat->physical_name = strdup(physical_name);
473 seat->logical_name = strdup(logical_name);
474 seat->destroy = destroy;
475 list_init(&seat->devices_list);
479 libinput_seat_ref(struct libinput_seat *seat)
485 libinput_seat_destroy(struct libinput_seat *seat)
487 list_remove(&seat->link);
488 free(seat->logical_name);
489 free(seat->physical_name);
494 libinput_seat_unref(struct libinput_seat *seat)
496 assert(seat->refcount > 0);
498 if (seat->refcount == 0)
499 libinput_seat_destroy(seat);
503 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
505 seat->user_data = user_data;
508 LIBINPUT_EXPORT void *
509 libinput_seat_get_user_data(struct libinput_seat *seat)
511 return seat->user_data;
514 LIBINPUT_EXPORT const char *
515 libinput_seat_get_physical_name(struct libinput_seat *seat)
517 return seat->physical_name;
520 LIBINPUT_EXPORT const char *
521 libinput_seat_get_logical_name(struct libinput_seat *seat)
523 return seat->logical_name;
527 libinput_device_init(struct libinput_device *device,
528 struct libinput_seat *seat)
531 device->refcount = 1;
535 libinput_device_ref(struct libinput_device *device)
541 libinput_device_destroy(struct libinput_device *device)
543 evdev_device_destroy((struct evdev_device *) device);
547 libinput_device_unref(struct libinput_device *device)
549 assert(device->refcount > 0);
551 if (device->refcount == 0)
552 libinput_device_destroy(device);
556 libinput_get_fd(struct libinput *libinput)
558 return libinput->epoll_fd;
562 libinput_dispatch(struct libinput *libinput)
564 struct libinput_source *source;
565 struct epoll_event ep[32];
568 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
572 for (i = 0; i < count; ++i) {
573 source = ep[i].data.ptr;
574 if (source->fd == -1)
577 source->dispatch(source->user_data);
580 libinput_drop_destroyed_sources(libinput);
586 init_event_base(struct libinput_event *event,
587 struct libinput *libinput,
588 enum libinput_event_type type,
589 union libinput_event_target target)
592 event->libinput = libinput;
593 event->target = target;
597 post_base_event(struct libinput *libinput,
598 enum libinput_event_type type,
599 struct libinput_event *event)
601 init_event_base(event, libinput, type,
602 (union libinput_event_target) { .libinput = libinput });
603 libinput_post_event(libinput, event);
607 post_device_event(struct libinput_device *device,
608 enum libinput_event_type type,
609 struct libinput_event *event)
611 init_event_base(event, device->seat->libinput, type,
612 (union libinput_event_target) { .device = device });
613 libinput_post_event(device->seat->libinput, event);
617 notify_added_device(struct libinput_device *device)
619 struct libinput_event_added_device *added_device_event;
621 added_device_event = malloc(sizeof *added_device_event);
622 if (!added_device_event)
625 *added_device_event = (struct libinput_event_added_device) {
629 post_base_event(device->seat->libinput,
630 LIBINPUT_EVENT_ADDED_DEVICE,
631 &added_device_event->base);
635 notify_removed_device(struct libinput_device *device)
637 struct libinput_event_removed_device *removed_device_event;
639 removed_device_event = malloc(sizeof *removed_device_event);
640 if (!removed_device_event)
643 *removed_device_event = (struct libinput_event_removed_device) {
647 post_base_event(device->seat->libinput,
648 LIBINPUT_EVENT_REMOVED_DEVICE,
649 &removed_device_event->base);
653 keyboard_notify_key(struct libinput_device *device,
656 enum libinput_keyboard_key_state state)
658 struct libinput_event_keyboard_key *key_event;
660 key_event = malloc(sizeof *key_event);
664 *key_event = (struct libinput_event_keyboard_key) {
670 post_device_event(device,
671 LIBINPUT_EVENT_KEYBOARD_KEY,
676 pointer_notify_motion(struct libinput_device *device,
681 struct libinput_event_pointer_motion *motion_event;
683 motion_event = malloc(sizeof *motion_event);
687 *motion_event = (struct libinput_event_pointer_motion) {
693 post_device_event(device,
694 LIBINPUT_EVENT_POINTER_MOTION,
695 &motion_event->base);
699 pointer_notify_motion_absolute(struct libinput_device *device,
704 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
706 motion_absolute_event = malloc(sizeof *motion_absolute_event);
707 if (!motion_absolute_event)
710 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
716 post_device_event(device,
717 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
718 &motion_absolute_event->base);
722 pointer_notify_button(struct libinput_device *device,
725 enum libinput_pointer_button_state state)
727 struct libinput_event_pointer_button *button_event;
729 button_event = malloc(sizeof *button_event);
733 *button_event = (struct libinput_event_pointer_button) {
739 post_device_event(device,
740 LIBINPUT_EVENT_POINTER_BUTTON,
741 &button_event->base);
745 pointer_notify_axis(struct libinput_device *device,
747 enum libinput_pointer_axis axis,
750 struct libinput_event_pointer_axis *axis_event;
752 axis_event = malloc(sizeof *axis_event);
756 *axis_event = (struct libinput_event_pointer_axis) {
762 post_device_event(device,
763 LIBINPUT_EVENT_POINTER_AXIS,
768 touch_notify_touch(struct libinput_device *device,
773 enum libinput_touch_type touch_type)
775 struct libinput_event_touch_touch *touch_event;
777 touch_event = malloc(sizeof *touch_event);
781 *touch_event = (struct libinput_event_touch_touch) {
786 .touch_type = touch_type,
789 post_device_event(device,
790 LIBINPUT_EVENT_TOUCH_TOUCH,
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;
830 switch (libinput_event_get_class(event)) {
831 case LIBINPUT_EVENT_CLASS_NONE:
832 case LIBINPUT_EVENT_CLASS_BASE:
834 case LIBINPUT_EVENT_CLASS_SEAT:
835 libinput_seat_ref(event->target.seat);
837 case LIBINPUT_EVENT_CLASS_DEVICE:
838 libinput_device_ref(event->target.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,