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_motion {
69 struct libinput_event base;
75 struct libinput_event_pointer_motion_absolute {
76 struct libinput_event base;
82 struct libinput_event_pointer_button {
83 struct libinput_event base;
86 enum libinput_pointer_button_state state;
89 struct libinput_event_pointer_axis {
90 struct libinput_event base;
92 enum libinput_pointer_axis axis;
96 struct libinput_event_touch_touch {
97 struct libinput_event base;
102 enum libinput_touch_type touch_type;
106 libinput_post_event(struct libinput *libinput,
107 struct libinput_event *event);
109 LIBINPUT_EXPORT enum libinput_event_type
110 libinput_event_get_type(struct libinput_event *event)
115 LIBINPUT_EXPORT struct libinput*
116 libinput_event_get_context(struct libinput_event *event)
118 return event->device->seat->libinput;
121 LIBINPUT_EXPORT struct libinput_device*
122 libinput_event_get_device(struct libinput_event *event)
124 return event->device;
127 LIBINPUT_EXPORT uint32_t
128 libinput_event_keyboard_get_time(
129 struct libinput_event_keyboard *event)
134 LIBINPUT_EXPORT uint32_t
135 libinput_event_keyboard_get_key(
136 struct libinput_event_keyboard *event)
141 LIBINPUT_EXPORT enum libinput_keyboard_key_state
142 libinput_event_keyboard_get_key_state(
143 struct libinput_event_keyboard *event)
148 LIBINPUT_EXPORT uint32_t
149 libinput_event_pointer_motion_get_time(
150 struct libinput_event_pointer_motion *event)
155 LIBINPUT_EXPORT li_fixed_t
156 libinput_event_pointer_motion_get_dx(
157 struct libinput_event_pointer_motion *event)
162 LIBINPUT_EXPORT li_fixed_t
163 libinput_event_pointer_motion_get_dy(
164 struct libinput_event_pointer_motion *event)
169 LIBINPUT_EXPORT uint32_t
170 libinput_event_pointer_motion_absolute_get_time(
171 struct libinput_event_pointer_motion_absolute *event)
176 LIBINPUT_EXPORT li_fixed_t
177 libinput_event_pointer_motion_absolute_get_x(
178 struct libinput_event_pointer_motion_absolute *event)
183 LIBINPUT_EXPORT li_fixed_t
184 libinput_event_pointer_motion_absolute_get_y(
185 struct libinput_event_pointer_motion_absolute *event)
190 LIBINPUT_EXPORT uint32_t
191 libinput_event_pointer_button_get_time(
192 struct libinput_event_pointer_button *event)
197 LIBINPUT_EXPORT uint32_t
198 libinput_event_pointer_button_get_button(
199 struct libinput_event_pointer_button *event)
201 return event->button;
204 LIBINPUT_EXPORT enum libinput_pointer_button_state
205 libinput_event_pointer_button_get_state(
206 struct libinput_event_pointer_button *event)
211 LIBINPUT_EXPORT uint32_t
212 libinput_event_pointer_axis_get_time(
213 struct libinput_event_pointer_axis *event)
218 LIBINPUT_EXPORT enum libinput_pointer_axis
219 libinput_event_pointer_axis_get_axis(
220 struct libinput_event_pointer_axis *event)
225 LIBINPUT_EXPORT li_fixed_t
226 libinput_event_pointer_axis_get_value(
227 struct libinput_event_pointer_axis *event)
232 LIBINPUT_EXPORT uint32_t
233 libinput_event_touch_touch_get_time(
234 struct libinput_event_touch_touch *event)
239 LIBINPUT_EXPORT uint32_t
240 libinput_event_touch_touch_get_slot(
241 struct libinput_event_touch_touch *event)
246 LIBINPUT_EXPORT li_fixed_t
247 libinput_event_touch_touch_get_x(
248 struct libinput_event_touch_touch *event)
253 LIBINPUT_EXPORT li_fixed_t
254 libinput_event_touch_touch_get_y(
255 struct libinput_event_touch_touch *event)
260 LIBINPUT_EXPORT enum libinput_touch_type
261 libinput_event_touch_touch_get_touch_type(
262 struct libinput_event_touch_touch *event)
264 return event->touch_type;
267 struct libinput_source *
268 libinput_add_fd(struct libinput *libinput,
270 libinput_source_dispatch_t dispatch,
273 struct libinput_source *source;
274 struct epoll_event ep;
276 source = malloc(sizeof *source);
280 source->dispatch = dispatch;
281 source->user_data = user_data;
284 memset(&ep, 0, sizeof ep);
286 ep.data.ptr = source;
288 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
298 libinput_remove_source(struct libinput *libinput,
299 struct libinput_source *source)
301 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
304 list_insert(&libinput->source_destroy_list, &source->link);
308 libinput_init(struct libinput *libinput,
309 const struct libinput_interface *interface,
310 const struct libinput_interface_backend *interface_backend,
313 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
314 if (libinput->epoll_fd < 0)
317 libinput->events_len = 4;
318 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
319 if (!libinput->events) {
320 close(libinput->epoll_fd);
324 libinput->interface = interface;
325 libinput->interface_backend = interface_backend;
326 libinput->user_data = user_data;
327 list_init(&libinput->source_destroy_list);
328 list_init(&libinput->seat_list);
334 libinput_device_destroy(struct libinput_device *device);
337 libinput_seat_destroy(struct libinput_seat *seat);
340 libinput_drop_destroyed_sources(struct libinput *libinput)
342 struct libinput_source *source, *next;
344 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
346 list_init(&libinput->source_destroy_list);
350 libinput_destroy(struct libinput *libinput)
352 struct libinput_event *event;
353 struct libinput_device *device, *next_device;
354 struct libinput_seat *seat, *next_seat;
356 if (libinput == NULL)
359 libinput_suspend(libinput);
361 libinput->interface_backend->destroy(libinput);
363 while ((event = libinput_get_event(libinput)))
364 libinput_event_destroy(event);
366 libinput_drop_destroyed_sources(libinput);
368 free(libinput->events);
370 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
371 list_for_each_safe(device, next_device,
374 libinput_device_destroy(device);
376 libinput_seat_destroy(seat);
379 close(libinput->epoll_fd);
383 static enum libinput_event_class
384 libinput_event_get_class(struct libinput_event *event)
386 switch (event->type) {
387 case LIBINPUT_EVENT_NONE:
388 return LIBINPUT_EVENT_CLASS_NONE;
390 case LIBINPUT_EVENT_DEVICE_ADDED:
391 case LIBINPUT_EVENT_DEVICE_REMOVED:
392 return LIBINPUT_EVENT_CLASS_BASE;
394 case LIBINPUT_EVENT_KEYBOARD_KEY:
395 case LIBINPUT_EVENT_POINTER_MOTION:
396 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
397 case LIBINPUT_EVENT_POINTER_BUTTON:
398 case LIBINPUT_EVENT_POINTER_AXIS:
399 case LIBINPUT_EVENT_TOUCH_TOUCH:
400 return LIBINPUT_EVENT_CLASS_DEVICE;
403 /* We should never end up here. */
408 libinput_event_destroy(struct libinput_event *event)
413 switch (libinput_event_get_class(event)) {
414 case LIBINPUT_EVENT_CLASS_NONE:
415 case LIBINPUT_EVENT_CLASS_BASE:
417 case LIBINPUT_EVENT_CLASS_SEAT:
418 libinput_seat_unref(event->device->seat);
420 case LIBINPUT_EVENT_CLASS_DEVICE:
421 libinput_device_unref(event->device);
429 open_restricted(struct libinput *libinput,
430 const char *path, int flags)
432 return libinput->interface->open_restricted(path,
434 libinput->user_data);
438 close_restricted(struct libinput *libinput, int fd)
440 return libinput->interface->close_restricted(fd, libinput->user_data);
444 libinput_seat_init(struct libinput_seat *seat,
445 struct libinput *libinput,
446 const char *physical_name,
447 const char *logical_name,
448 libinput_seat_destroy_func destroy)
451 seat->libinput = libinput;
452 seat->physical_name = strdup(physical_name);
453 seat->logical_name = strdup(logical_name);
454 seat->destroy = destroy;
455 list_init(&seat->devices_list);
459 libinput_seat_ref(struct libinput_seat *seat)
465 libinput_seat_destroy(struct libinput_seat *seat)
467 list_remove(&seat->link);
468 free(seat->logical_name);
469 free(seat->physical_name);
474 libinput_seat_unref(struct libinput_seat *seat)
476 assert(seat->refcount > 0);
478 if (seat->refcount == 0)
479 libinput_seat_destroy(seat);
483 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
485 seat->user_data = user_data;
488 LIBINPUT_EXPORT void *
489 libinput_seat_get_user_data(struct libinput_seat *seat)
491 return seat->user_data;
494 LIBINPUT_EXPORT const char *
495 libinput_seat_get_physical_name(struct libinput_seat *seat)
497 return seat->physical_name;
500 LIBINPUT_EXPORT const char *
501 libinput_seat_get_logical_name(struct libinput_seat *seat)
503 return seat->logical_name;
507 libinput_device_init(struct libinput_device *device,
508 struct libinput_seat *seat)
511 device->refcount = 1;
515 libinput_device_ref(struct libinput_device *device)
521 libinput_device_destroy(struct libinput_device *device)
523 evdev_device_destroy((struct evdev_device *) device);
527 libinput_device_unref(struct libinput_device *device)
529 assert(device->refcount > 0);
531 if (device->refcount == 0)
532 libinput_device_destroy(device);
536 libinput_get_fd(struct libinput *libinput)
538 return libinput->epoll_fd;
542 libinput_dispatch(struct libinput *libinput)
544 struct libinput_source *source;
545 struct epoll_event ep[32];
548 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
552 for (i = 0; i < count; ++i) {
553 source = ep[i].data.ptr;
554 if (source->fd == -1)
557 source->dispatch(source->user_data);
560 libinput_drop_destroyed_sources(libinput);
566 init_event_base(struct libinput_event *event,
567 struct libinput_device *device,
568 enum libinput_event_type type)
571 event->device = device;
575 post_base_event(struct libinput_device *device,
576 enum libinput_event_type type,
577 struct libinput_event *event)
579 struct libinput *libinput = device->seat->libinput;
580 init_event_base(event, device, type);
581 libinput_post_event(libinput, event);
585 post_device_event(struct libinput_device *device,
586 enum libinput_event_type type,
587 struct libinput_event *event)
589 init_event_base(event, device, type);
590 libinput_post_event(device->seat->libinput, event);
594 notify_added_device(struct libinput_device *device)
596 struct libinput_event_device_notify *added_device_event;
598 added_device_event = malloc(sizeof *added_device_event);
599 if (!added_device_event)
602 *added_device_event = (struct libinput_event_device_notify) {
606 post_base_event(device,
607 LIBINPUT_EVENT_DEVICE_ADDED,
608 &added_device_event->base);
612 notify_removed_device(struct libinput_device *device)
614 struct libinput_event_device_notify *removed_device_event;
616 removed_device_event = malloc(sizeof *removed_device_event);
617 if (!removed_device_event)
620 *removed_device_event = (struct libinput_event_device_notify) {
624 post_base_event(device,
625 LIBINPUT_EVENT_DEVICE_REMOVED,
626 &removed_device_event->base);
630 keyboard_notify_key(struct libinput_device *device,
633 enum libinput_keyboard_key_state state)
635 struct libinput_event_keyboard *key_event;
637 key_event = malloc(sizeof *key_event);
641 *key_event = (struct libinput_event_keyboard) {
647 post_device_event(device,
648 LIBINPUT_EVENT_KEYBOARD_KEY,
653 pointer_notify_motion(struct libinput_device *device,
658 struct libinput_event_pointer_motion *motion_event;
660 motion_event = malloc(sizeof *motion_event);
664 *motion_event = (struct libinput_event_pointer_motion) {
670 post_device_event(device,
671 LIBINPUT_EVENT_POINTER_MOTION,
672 &motion_event->base);
676 pointer_notify_motion_absolute(struct libinput_device *device,
681 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
683 motion_absolute_event = malloc(sizeof *motion_absolute_event);
684 if (!motion_absolute_event)
687 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
693 post_device_event(device,
694 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
695 &motion_absolute_event->base);
699 pointer_notify_button(struct libinput_device *device,
702 enum libinput_pointer_button_state state)
704 struct libinput_event_pointer_button *button_event;
706 button_event = malloc(sizeof *button_event);
710 *button_event = (struct libinput_event_pointer_button) {
716 post_device_event(device,
717 LIBINPUT_EVENT_POINTER_BUTTON,
718 &button_event->base);
722 pointer_notify_axis(struct libinput_device *device,
724 enum libinput_pointer_axis axis,
727 struct libinput_event_pointer_axis *axis_event;
729 axis_event = malloc(sizeof *axis_event);
733 *axis_event = (struct libinput_event_pointer_axis) {
739 post_device_event(device,
740 LIBINPUT_EVENT_POINTER_AXIS,
745 touch_notify_touch(struct libinput_device *device,
750 enum libinput_touch_type touch_type)
752 struct libinput_event_touch_touch *touch_event;
754 touch_event = malloc(sizeof *touch_event);
758 *touch_event = (struct libinput_event_touch_touch) {
763 .touch_type = touch_type,
766 post_device_event(device,
767 LIBINPUT_EVENT_TOUCH_TOUCH,
772 libinput_post_event(struct libinput *libinput,
773 struct libinput_event *event)
775 struct libinput_event **events = libinput->events;
776 size_t events_len = libinput->events_len;
777 size_t events_count = libinput->events_count;
782 if (events_count > events_len) {
784 events = realloc(events, events_len * sizeof *events);
786 fprintf(stderr, "Failed to reallocate event ring "
791 if (libinput->events_count > 0 && libinput->events_in == 0) {
792 libinput->events_in = libinput->events_len;
793 } else if (libinput->events_count > 0 &&
794 libinput->events_out >= libinput->events_in) {
795 move_len = libinput->events_len - libinput->events_out;
796 new_out = events_len - move_len;
797 memmove(events + new_out,
798 events + libinput->events_out,
799 move_len * sizeof *events);
800 libinput->events_out = new_out;
803 libinput->events = events;
804 libinput->events_len = events_len;
807 switch (libinput_event_get_class(event)) {
808 case LIBINPUT_EVENT_CLASS_NONE:
809 case LIBINPUT_EVENT_CLASS_BASE:
811 case LIBINPUT_EVENT_CLASS_SEAT:
812 libinput_seat_ref(event->device->seat);
814 case LIBINPUT_EVENT_CLASS_DEVICE:
815 libinput_device_ref(event->device);
819 libinput->events_count = events_count;
820 events[libinput->events_in] = event;
821 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
824 LIBINPUT_EXPORT struct libinput_event *
825 libinput_get_event(struct libinput *libinput)
827 struct libinput_event *event;
829 if (libinput->events_count == 0)
832 event = libinput->events[libinput->events_out];
833 libinput->events_out =
834 (libinput->events_out + 1) % libinput->events_len;
835 libinput->events_count--;
840 LIBINPUT_EXPORT enum libinput_event_type
841 libinput_next_event_type(struct libinput *libinput)
843 struct libinput_event *event;
845 if (libinput->events_count == 0)
846 return LIBINPUT_EVENT_NONE;
848 event = libinput->events[libinput->events_out];
852 LIBINPUT_EXPORT void *
853 libinput_get_user_data(struct libinput *libinput)
855 return libinput->user_data;
859 libinput_resume(struct libinput *libinput)
861 return libinput->interface_backend->resume(libinput);
865 libinput_suspend(struct libinput *libinput)
867 libinput->interface_backend->suspend(libinput);
871 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
873 device->user_data = user_data;
876 LIBINPUT_EXPORT void *
877 libinput_device_get_user_data(struct libinput_device *device)
879 return device->user_data;
882 LIBINPUT_EXPORT const char *
883 libinput_device_get_sysname(struct libinput_device *device)
885 return evdev_device_get_sysname((struct evdev_device *) device);
888 LIBINPUT_EXPORT const char *
889 libinput_device_get_output_name(struct libinput_device *device)
891 return evdev_device_get_output((struct evdev_device *) device);
894 LIBINPUT_EXPORT struct libinput_seat *
895 libinput_device_get_seat(struct libinput_device *device)
901 libinput_device_led_update(struct libinput_device *device,
902 enum libinput_led leds)
904 evdev_device_led_update((struct evdev_device *) device, leds);
908 libinput_device_get_keys(struct libinput_device *device,
909 char *keys, size_t size)
911 return evdev_device_get_keys((struct evdev_device *) device,
917 libinput_device_calibrate(struct libinput_device *device,
918 float calibration[6])
920 evdev_device_calibrate((struct evdev_device *) device, calibration);
924 libinput_device_has_capability(struct libinput_device *device,
925 enum libinput_device_capability capability)
927 return evdev_device_has_capability((struct evdev_device *) device,