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,
467 libinput_seat_destroy_func destroy)
470 seat->libinput = libinput;
471 seat->name = strdup(name);
472 seat->destroy = destroy;
473 list_init(&seat->devices_list);
477 libinput_seat_ref(struct libinput_seat *seat)
483 libinput_seat_destroy(struct libinput_seat *seat)
485 list_remove(&seat->link);
491 libinput_seat_unref(struct libinput_seat *seat)
493 assert(seat->refcount > 0);
495 if (seat->refcount == 0)
496 libinput_seat_destroy(seat);
500 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
502 seat->user_data = user_data;
505 LIBINPUT_EXPORT void *
506 libinput_seat_get_user_data(struct libinput_seat *seat)
508 return seat->user_data;
511 LIBINPUT_EXPORT const char *
512 libinput_seat_get_name(struct libinput_seat *seat)
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 *libinput,
579 enum libinput_event_type type,
580 union libinput_event_target target)
583 event->libinput = libinput;
584 event->target = target;
588 post_base_event(struct libinput *libinput,
589 enum libinput_event_type type,
590 struct libinput_event *event)
592 init_event_base(event, libinput, type,
593 (union libinput_event_target) { .libinput = libinput });
594 libinput_post_event(libinput, event);
598 post_device_event(struct libinput_device *device,
599 enum libinput_event_type type,
600 struct libinput_event *event)
602 init_event_base(event, device->seat->libinput, type,
603 (union libinput_event_target) { .device = device });
604 libinput_post_event(device->seat->libinput, event);
608 notify_added_device(struct libinput_device *device)
610 struct libinput_event_added_device *added_device_event;
612 added_device_event = malloc(sizeof *added_device_event);
613 if (!added_device_event)
616 *added_device_event = (struct libinput_event_added_device) {
620 post_base_event(device->seat->libinput,
621 LIBINPUT_EVENT_ADDED_DEVICE,
622 &added_device_event->base);
626 notify_removed_device(struct libinput_device *device)
628 struct libinput_event_removed_device *removed_device_event;
630 removed_device_event = malloc(sizeof *removed_device_event);
631 if (!removed_device_event)
634 *removed_device_event = (struct libinput_event_removed_device) {
638 post_base_event(device->seat->libinput,
639 LIBINPUT_EVENT_REMOVED_DEVICE,
640 &removed_device_event->base);
644 keyboard_notify_key(struct libinput_device *device,
647 enum libinput_keyboard_key_state state)
649 struct libinput_event_keyboard_key *key_event;
651 key_event = malloc(sizeof *key_event);
655 *key_event = (struct libinput_event_keyboard_key) {
661 post_device_event(device,
662 LIBINPUT_EVENT_KEYBOARD_KEY,
667 pointer_notify_motion(struct libinput_device *device,
672 struct libinput_event_pointer_motion *motion_event;
674 motion_event = malloc(sizeof *motion_event);
678 *motion_event = (struct libinput_event_pointer_motion) {
684 post_device_event(device,
685 LIBINPUT_EVENT_POINTER_MOTION,
686 &motion_event->base);
690 pointer_notify_motion_absolute(struct libinput_device *device,
695 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
697 motion_absolute_event = malloc(sizeof *motion_absolute_event);
698 if (!motion_absolute_event)
701 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
707 post_device_event(device,
708 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
709 &motion_absolute_event->base);
713 pointer_notify_button(struct libinput_device *device,
716 enum libinput_pointer_button_state state)
718 struct libinput_event_pointer_button *button_event;
720 button_event = malloc(sizeof *button_event);
724 *button_event = (struct libinput_event_pointer_button) {
730 post_device_event(device,
731 LIBINPUT_EVENT_POINTER_BUTTON,
732 &button_event->base);
736 pointer_notify_axis(struct libinput_device *device,
738 enum libinput_pointer_axis axis,
741 struct libinput_event_pointer_axis *axis_event;
743 axis_event = malloc(sizeof *axis_event);
747 *axis_event = (struct libinput_event_pointer_axis) {
753 post_device_event(device,
754 LIBINPUT_EVENT_POINTER_AXIS,
759 touch_notify_touch(struct libinput_device *device,
764 enum libinput_touch_type touch_type)
766 struct libinput_event_touch_touch *touch_event;
768 touch_event = malloc(sizeof *touch_event);
772 *touch_event = (struct libinput_event_touch_touch) {
777 .touch_type = touch_type,
780 post_device_event(device,
781 LIBINPUT_EVENT_TOUCH_TOUCH,
786 libinput_post_event(struct libinput *libinput,
787 struct libinput_event *event)
789 struct libinput_event **events = libinput->events;
790 size_t events_len = libinput->events_len;
791 size_t events_count = libinput->events_count;
796 if (events_count > events_len) {
798 events = realloc(events, events_len * sizeof *events);
800 fprintf(stderr, "Failed to reallocate event ring "
805 if (libinput->events_count > 0 && libinput->events_in == 0) {
806 libinput->events_in = libinput->events_len;
807 } else if (libinput->events_count > 0 &&
808 libinput->events_out >= libinput->events_in) {
809 move_len = libinput->events_len - libinput->events_out;
810 new_out = events_len - move_len;
811 memmove(events + new_out,
812 events + libinput->events_out,
813 move_len * sizeof *events);
814 libinput->events_out = new_out;
817 libinput->events = events;
818 libinput->events_len = events_len;
821 switch (libinput_event_get_class(event)) {
822 case LIBINPUT_EVENT_CLASS_NONE:
823 case LIBINPUT_EVENT_CLASS_BASE:
825 case LIBINPUT_EVENT_CLASS_SEAT:
826 libinput_seat_ref(event->target.seat);
828 case LIBINPUT_EVENT_CLASS_DEVICE:
829 libinput_device_ref(event->target.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,