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_added_device {
57 struct libinput_event base;
58 struct libinput_device *device;
61 struct libinput_event_removed_device {
62 struct libinput_event base;
63 struct libinput_device *device;
66 struct libinput_event_keyboard_key {
67 struct libinput_event base;
70 enum libinput_keyboard_key_state state;
73 struct libinput_event_pointer_motion {
74 struct libinput_event base;
80 struct libinput_event_pointer_motion_absolute {
81 struct libinput_event base;
87 struct libinput_event_pointer_button {
88 struct libinput_event base;
91 enum libinput_pointer_button_state state;
94 struct libinput_event_pointer_axis {
95 struct libinput_event base;
97 enum libinput_pointer_axis axis;
101 struct libinput_event_touch_touch {
102 struct libinput_event base;
107 enum libinput_touch_type touch_type;
111 libinput_post_event(struct libinput *libinput,
112 struct libinput_event *event);
114 LIBINPUT_EXPORT enum libinput_event_type
115 libinput_event_get_type(struct libinput_event *event)
120 LIBINPUT_EXPORT struct libinput*
121 libinput_event_get_context(struct libinput_event *event)
123 return event->device->seat->libinput;
126 LIBINPUT_EXPORT struct libinput_device*
127 libinput_event_get_device(struct libinput_event *event)
129 return event->device;
132 LIBINPUT_EXPORT uint32_t
133 libinput_event_keyboard_key_get_time(
134 struct libinput_event_keyboard_key *event)
139 LIBINPUT_EXPORT uint32_t
140 libinput_event_keyboard_key_get_key(
141 struct libinput_event_keyboard_key *event)
146 LIBINPUT_EXPORT enum libinput_keyboard_key_state
147 libinput_event_keyboard_key_get_state(
148 struct libinput_event_keyboard_key *event)
153 LIBINPUT_EXPORT uint32_t
154 libinput_event_pointer_motion_get_time(
155 struct libinput_event_pointer_motion *event)
160 LIBINPUT_EXPORT li_fixed_t
161 libinput_event_pointer_motion_get_dx(
162 struct libinput_event_pointer_motion *event)
167 LIBINPUT_EXPORT li_fixed_t
168 libinput_event_pointer_motion_get_dy(
169 struct libinput_event_pointer_motion *event)
174 LIBINPUT_EXPORT uint32_t
175 libinput_event_pointer_motion_absolute_get_time(
176 struct libinput_event_pointer_motion_absolute *event)
181 LIBINPUT_EXPORT li_fixed_t
182 libinput_event_pointer_motion_absolute_get_x(
183 struct libinput_event_pointer_motion_absolute *event)
188 LIBINPUT_EXPORT li_fixed_t
189 libinput_event_pointer_motion_absolute_get_y(
190 struct libinput_event_pointer_motion_absolute *event)
195 LIBINPUT_EXPORT uint32_t
196 libinput_event_pointer_button_get_time(
197 struct libinput_event_pointer_button *event)
202 LIBINPUT_EXPORT uint32_t
203 libinput_event_pointer_button_get_button(
204 struct libinput_event_pointer_button *event)
206 return event->button;
209 LIBINPUT_EXPORT enum libinput_pointer_button_state
210 libinput_event_pointer_button_get_state(
211 struct libinput_event_pointer_button *event)
216 LIBINPUT_EXPORT uint32_t
217 libinput_event_pointer_axis_get_time(
218 struct libinput_event_pointer_axis *event)
223 LIBINPUT_EXPORT enum libinput_pointer_axis
224 libinput_event_pointer_axis_get_axis(
225 struct libinput_event_pointer_axis *event)
230 LIBINPUT_EXPORT li_fixed_t
231 libinput_event_pointer_axis_get_value(
232 struct libinput_event_pointer_axis *event)
237 LIBINPUT_EXPORT uint32_t
238 libinput_event_touch_touch_get_time(
239 struct libinput_event_touch_touch *event)
244 LIBINPUT_EXPORT uint32_t
245 libinput_event_touch_touch_get_slot(
246 struct libinput_event_touch_touch *event)
251 LIBINPUT_EXPORT li_fixed_t
252 libinput_event_touch_touch_get_x(
253 struct libinput_event_touch_touch *event)
258 LIBINPUT_EXPORT li_fixed_t
259 libinput_event_touch_touch_get_y(
260 struct libinput_event_touch_touch *event)
265 LIBINPUT_EXPORT enum libinput_touch_type
266 libinput_event_touch_touch_get_touch_type(
267 struct libinput_event_touch_touch *event)
269 return event->touch_type;
272 struct libinput_source *
273 libinput_add_fd(struct libinput *libinput,
275 libinput_source_dispatch_t dispatch,
278 struct libinput_source *source;
279 struct epoll_event ep;
281 source = malloc(sizeof *source);
285 source->dispatch = dispatch;
286 source->user_data = user_data;
289 memset(&ep, 0, sizeof ep);
291 ep.data.ptr = source;
293 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
303 libinput_remove_source(struct libinput *libinput,
304 struct libinput_source *source)
306 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
309 list_insert(&libinput->source_destroy_list, &source->link);
313 libinput_init(struct libinput *libinput,
314 const struct libinput_interface *interface,
315 const struct libinput_interface_backend *interface_backend,
318 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
319 if (libinput->epoll_fd < 0)
322 libinput->events_len = 4;
323 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
324 if (!libinput->events) {
325 close(libinput->epoll_fd);
329 libinput->interface = interface;
330 libinput->interface_backend = interface_backend;
331 libinput->user_data = user_data;
332 list_init(&libinput->source_destroy_list);
333 list_init(&libinput->seat_list);
339 libinput_device_destroy(struct libinput_device *device);
342 libinput_seat_destroy(struct libinput_seat *seat);
345 libinput_drop_destroyed_sources(struct libinput *libinput)
347 struct libinput_source *source, *next;
349 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
351 list_init(&libinput->source_destroy_list);
355 libinput_destroy(struct libinput *libinput)
357 struct libinput_event *event;
358 struct libinput_device *device, *next_device;
359 struct libinput_seat *seat, *next_seat;
361 if (libinput == NULL)
364 libinput_suspend(libinput);
366 libinput->interface_backend->destroy(libinput);
368 while ((event = libinput_get_event(libinput)))
369 libinput_event_destroy(event);
371 libinput_drop_destroyed_sources(libinput);
373 free(libinput->events);
375 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
376 list_for_each_safe(device, next_device,
379 libinput_device_destroy(device);
381 libinput_seat_destroy(seat);
384 close(libinput->epoll_fd);
388 static enum libinput_event_class
389 libinput_event_get_class(struct libinput_event *event)
391 switch (event->type) {
392 case LIBINPUT_EVENT_NONE:
393 return LIBINPUT_EVENT_CLASS_NONE;
395 case LIBINPUT_EVENT_ADDED_DEVICE:
396 case LIBINPUT_EVENT_REMOVED_DEVICE:
397 return LIBINPUT_EVENT_CLASS_BASE;
399 case LIBINPUT_EVENT_KEYBOARD_KEY:
400 case LIBINPUT_EVENT_POINTER_MOTION:
401 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
402 case LIBINPUT_EVENT_POINTER_BUTTON:
403 case LIBINPUT_EVENT_POINTER_AXIS:
404 case LIBINPUT_EVENT_TOUCH_TOUCH:
405 return LIBINPUT_EVENT_CLASS_DEVICE;
408 /* We should never end up here. */
413 libinput_event_destroy(struct libinput_event *event)
418 switch (libinput_event_get_class(event)) {
419 case LIBINPUT_EVENT_CLASS_NONE:
420 case LIBINPUT_EVENT_CLASS_BASE:
422 case LIBINPUT_EVENT_CLASS_SEAT:
423 libinput_seat_unref(event->device->seat);
425 case LIBINPUT_EVENT_CLASS_DEVICE:
426 libinput_device_unref(event->device);
434 open_restricted(struct libinput *libinput,
435 const char *path, int flags)
437 return libinput->interface->open_restricted(path,
439 libinput->user_data);
443 close_restricted(struct libinput *libinput, int fd)
445 return libinput->interface->close_restricted(fd, libinput->user_data);
449 libinput_seat_init(struct libinput_seat *seat,
450 struct libinput *libinput,
451 const char *physical_name,
452 const char *logical_name,
453 libinput_seat_destroy_func destroy)
456 seat->libinput = libinput;
457 seat->physical_name = strdup(physical_name);
458 seat->logical_name = strdup(logical_name);
459 seat->destroy = destroy;
460 list_init(&seat->devices_list);
464 libinput_seat_ref(struct libinput_seat *seat)
470 libinput_seat_destroy(struct libinput_seat *seat)
472 list_remove(&seat->link);
473 free(seat->logical_name);
474 free(seat->physical_name);
479 libinput_seat_unref(struct libinput_seat *seat)
481 assert(seat->refcount > 0);
483 if (seat->refcount == 0)
484 libinput_seat_destroy(seat);
488 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
490 seat->user_data = user_data;
493 LIBINPUT_EXPORT void *
494 libinput_seat_get_user_data(struct libinput_seat *seat)
496 return seat->user_data;
499 LIBINPUT_EXPORT const char *
500 libinput_seat_get_physical_name(struct libinput_seat *seat)
502 return seat->physical_name;
505 LIBINPUT_EXPORT const char *
506 libinput_seat_get_logical_name(struct libinput_seat *seat)
508 return seat->logical_name;
512 libinput_device_init(struct libinput_device *device,
513 struct libinput_seat *seat)
516 device->refcount = 1;
520 libinput_device_ref(struct libinput_device *device)
526 libinput_device_destroy(struct libinput_device *device)
528 evdev_device_destroy((struct evdev_device *) device);
532 libinput_device_unref(struct libinput_device *device)
534 assert(device->refcount > 0);
536 if (device->refcount == 0)
537 libinput_device_destroy(device);
541 libinput_get_fd(struct libinput *libinput)
543 return libinput->epoll_fd;
547 libinput_dispatch(struct libinput *libinput)
549 struct libinput_source *source;
550 struct epoll_event ep[32];
553 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
557 for (i = 0; i < count; ++i) {
558 source = ep[i].data.ptr;
559 if (source->fd == -1)
562 source->dispatch(source->user_data);
565 libinput_drop_destroyed_sources(libinput);
571 init_event_base(struct libinput_event *event,
572 struct libinput_device *device,
573 enum libinput_event_type type)
576 event->device = device;
580 post_base_event(struct libinput_device *device,
581 enum libinput_event_type type,
582 struct libinput_event *event)
584 struct libinput *libinput = device->seat->libinput;
585 init_event_base(event, device, type);
586 libinput_post_event(libinput, event);
590 post_device_event(struct libinput_device *device,
591 enum libinput_event_type type,
592 struct libinput_event *event)
594 init_event_base(event, device, type);
595 libinput_post_event(device->seat->libinput, event);
599 notify_added_device(struct libinput_device *device)
601 struct libinput_event_added_device *added_device_event;
603 added_device_event = malloc(sizeof *added_device_event);
604 if (!added_device_event)
607 *added_device_event = (struct libinput_event_added_device) {
611 post_base_event(device,
612 LIBINPUT_EVENT_ADDED_DEVICE,
613 &added_device_event->base);
617 notify_removed_device(struct libinput_device *device)
619 struct libinput_event_removed_device *removed_device_event;
621 removed_device_event = malloc(sizeof *removed_device_event);
622 if (!removed_device_event)
625 *removed_device_event = (struct libinput_event_removed_device) {
629 post_base_event(device,
630 LIBINPUT_EVENT_REMOVED_DEVICE,
631 &removed_device_event->base);
635 keyboard_notify_key(struct libinput_device *device,
638 enum libinput_keyboard_key_state state)
640 struct libinput_event_keyboard_key *key_event;
642 key_event = malloc(sizeof *key_event);
646 *key_event = (struct libinput_event_keyboard_key) {
652 post_device_event(device,
653 LIBINPUT_EVENT_KEYBOARD_KEY,
658 pointer_notify_motion(struct libinput_device *device,
663 struct libinput_event_pointer_motion *motion_event;
665 motion_event = malloc(sizeof *motion_event);
669 *motion_event = (struct libinput_event_pointer_motion) {
675 post_device_event(device,
676 LIBINPUT_EVENT_POINTER_MOTION,
677 &motion_event->base);
681 pointer_notify_motion_absolute(struct libinput_device *device,
686 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
688 motion_absolute_event = malloc(sizeof *motion_absolute_event);
689 if (!motion_absolute_event)
692 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
698 post_device_event(device,
699 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
700 &motion_absolute_event->base);
704 pointer_notify_button(struct libinput_device *device,
707 enum libinput_pointer_button_state state)
709 struct libinput_event_pointer_button *button_event;
711 button_event = malloc(sizeof *button_event);
715 *button_event = (struct libinput_event_pointer_button) {
721 post_device_event(device,
722 LIBINPUT_EVENT_POINTER_BUTTON,
723 &button_event->base);
727 pointer_notify_axis(struct libinput_device *device,
729 enum libinput_pointer_axis axis,
732 struct libinput_event_pointer_axis *axis_event;
734 axis_event = malloc(sizeof *axis_event);
738 *axis_event = (struct libinput_event_pointer_axis) {
744 post_device_event(device,
745 LIBINPUT_EVENT_POINTER_AXIS,
750 touch_notify_touch(struct libinput_device *device,
755 enum libinput_touch_type touch_type)
757 struct libinput_event_touch_touch *touch_event;
759 touch_event = malloc(sizeof *touch_event);
763 *touch_event = (struct libinput_event_touch_touch) {
768 .touch_type = touch_type,
771 post_device_event(device,
772 LIBINPUT_EVENT_TOUCH_TOUCH,
777 libinput_post_event(struct libinput *libinput,
778 struct libinput_event *event)
780 struct libinput_event **events = libinput->events;
781 size_t events_len = libinput->events_len;
782 size_t events_count = libinput->events_count;
787 if (events_count > events_len) {
789 events = realloc(events, events_len * sizeof *events);
791 fprintf(stderr, "Failed to reallocate event ring "
796 if (libinput->events_count > 0 && libinput->events_in == 0) {
797 libinput->events_in = libinput->events_len;
798 } else if (libinput->events_count > 0 &&
799 libinput->events_out >= libinput->events_in) {
800 move_len = libinput->events_len - libinput->events_out;
801 new_out = events_len - move_len;
802 memmove(events + new_out,
803 events + libinput->events_out,
804 move_len * sizeof *events);
805 libinput->events_out = new_out;
808 libinput->events = events;
809 libinput->events_len = events_len;
812 switch (libinput_event_get_class(event)) {
813 case LIBINPUT_EVENT_CLASS_NONE:
814 case LIBINPUT_EVENT_CLASS_BASE:
816 case LIBINPUT_EVENT_CLASS_SEAT:
817 libinput_seat_ref(event->device->seat);
819 case LIBINPUT_EVENT_CLASS_DEVICE:
820 libinput_device_ref(event->device);
824 libinput->events_count = events_count;
825 events[libinput->events_in] = event;
826 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
829 LIBINPUT_EXPORT struct libinput_event *
830 libinput_get_event(struct libinput *libinput)
832 struct libinput_event *event;
834 if (libinput->events_count == 0)
837 event = libinput->events[libinput->events_out];
838 libinput->events_out =
839 (libinput->events_out + 1) % libinput->events_len;
840 libinput->events_count--;
845 LIBINPUT_EXPORT enum libinput_event_type
846 libinput_next_event_type(struct libinput *libinput)
848 struct libinput_event *event;
850 if (libinput->events_count == 0)
851 return LIBINPUT_EVENT_NONE;
853 event = libinput->events[libinput->events_out];
857 LIBINPUT_EXPORT void *
858 libinput_get_user_data(struct libinput *libinput)
860 return libinput->user_data;
864 libinput_resume(struct libinput *libinput)
866 return libinput->interface_backend->resume(libinput);
870 libinput_suspend(struct libinput *libinput)
872 libinput->interface_backend->suspend(libinput);
876 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
878 device->user_data = user_data;
881 LIBINPUT_EXPORT void *
882 libinput_device_get_user_data(struct libinput_device *device)
884 return device->user_data;
887 LIBINPUT_EXPORT const char *
888 libinput_device_get_sysname(struct libinput_device *device)
890 return evdev_device_get_sysname((struct evdev_device *) device);
893 LIBINPUT_EXPORT const char *
894 libinput_device_get_output_name(struct libinput_device *device)
896 return evdev_device_get_output((struct evdev_device *) device);
899 LIBINPUT_EXPORT struct libinput_seat *
900 libinput_device_get_seat(struct libinput_device *device)
906 libinput_device_led_update(struct libinput_device *device,
907 enum libinput_led leds)
909 evdev_device_led_update((struct evdev_device *) device, leds);
913 libinput_device_get_keys(struct libinput_device *device,
914 char *keys, size_t size)
916 return evdev_device_get_keys((struct evdev_device *) device,
922 libinput_device_calibrate(struct libinput_device *device,
923 float calibration[6])
925 evdev_device_calibrate((struct evdev_device *) device, calibration);
929 libinput_device_has_capability(struct libinput_device *device,
930 enum libinput_device_capability capability)
932 return evdev_device_has_capability((struct evdev_device *) device,