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 struct libinput_source {
38 libinput_source_dispatch_t dispatch;
44 struct libinput_event {
45 enum libinput_event_type type;
46 struct libinput_device *device;
49 struct libinput_event_device_notify {
50 struct libinput_event base;
51 struct libinput_device *device;
54 struct libinput_event_keyboard {
55 struct libinput_event base;
58 enum libinput_keyboard_key_state state;
61 struct libinput_event_pointer {
62 struct libinput_event base;
67 enum libinput_pointer_button_state state;
68 enum libinput_pointer_axis axis;
72 struct libinput_event_touch {
73 struct libinput_event base;
78 enum libinput_touch_type touch_type;
82 libinput_post_event(struct libinput *libinput,
83 struct libinput_event *event);
85 LIBINPUT_EXPORT enum libinput_event_type
86 libinput_event_get_type(struct libinput_event *event)
91 LIBINPUT_EXPORT struct libinput*
92 libinput_event_get_context(struct libinput_event *event)
94 return event->device->seat->libinput;
97 LIBINPUT_EXPORT struct libinput_device*
98 libinput_event_get_device(struct libinput_event *event)
100 return event->device;
103 LIBINPUT_EXPORT struct libinput_event_pointer*
104 libinput_event_get_pointer_event(struct libinput_event *event)
106 switch (event->type) {
107 case LIBINPUT_EVENT_NONE:
108 abort(); /* not used as actual event type */
109 case LIBINPUT_EVENT_DEVICE_ADDED:
110 case LIBINPUT_EVENT_DEVICE_REMOVED:
111 case LIBINPUT_EVENT_KEYBOARD_KEY:
113 case LIBINPUT_EVENT_POINTER_MOTION:
114 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
115 case LIBINPUT_EVENT_POINTER_BUTTON:
116 case LIBINPUT_EVENT_POINTER_AXIS:
117 return (struct libinput_event_pointer*)event;
118 case LIBINPUT_EVENT_TOUCH_TOUCH:
119 case LIBINPUT_EVENT_TOUCH_FRAME:
126 LIBINPUT_EXPORT struct libinput_event_keyboard*
127 libinput_event_get_keyboard_event(struct libinput_event *event)
129 switch (event->type) {
130 case LIBINPUT_EVENT_NONE:
131 abort(); /* not used as actual event type */
132 case LIBINPUT_EVENT_DEVICE_ADDED:
133 case LIBINPUT_EVENT_DEVICE_REMOVED:
135 case LIBINPUT_EVENT_KEYBOARD_KEY:
136 return (struct libinput_event_keyboard*)event;
137 case LIBINPUT_EVENT_POINTER_MOTION:
138 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
139 case LIBINPUT_EVENT_POINTER_BUTTON:
140 case LIBINPUT_EVENT_POINTER_AXIS:
141 case LIBINPUT_EVENT_TOUCH_TOUCH:
142 case LIBINPUT_EVENT_TOUCH_FRAME:
149 LIBINPUT_EXPORT struct libinput_event_touch*
150 libinput_event_get_touch_event(struct libinput_event *event)
152 switch (event->type) {
153 case LIBINPUT_EVENT_NONE:
154 abort(); /* not used as actual event type */
155 case LIBINPUT_EVENT_DEVICE_ADDED:
156 case LIBINPUT_EVENT_DEVICE_REMOVED:
157 case LIBINPUT_EVENT_KEYBOARD_KEY:
158 case LIBINPUT_EVENT_POINTER_MOTION:
159 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
160 case LIBINPUT_EVENT_POINTER_BUTTON:
161 case LIBINPUT_EVENT_POINTER_AXIS:
163 case LIBINPUT_EVENT_TOUCH_TOUCH:
164 case LIBINPUT_EVENT_TOUCH_FRAME:
165 return (struct libinput_event_touch*)event;
171 LIBINPUT_EXPORT struct libinput_event_device_notify*
172 libinput_event_get_device_notify_event(struct libinput_event *event)
174 switch (event->type) {
175 case LIBINPUT_EVENT_NONE:
176 abort(); /* not used as actual event type */
177 case LIBINPUT_EVENT_DEVICE_ADDED:
178 case LIBINPUT_EVENT_DEVICE_REMOVED:
179 return (struct libinput_event_device_notify*)event;
180 case LIBINPUT_EVENT_KEYBOARD_KEY:
181 case LIBINPUT_EVENT_POINTER_MOTION:
182 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
183 case LIBINPUT_EVENT_POINTER_BUTTON:
184 case LIBINPUT_EVENT_POINTER_AXIS:
185 case LIBINPUT_EVENT_TOUCH_TOUCH:
186 case LIBINPUT_EVENT_TOUCH_FRAME:
193 LIBINPUT_EXPORT uint32_t
194 libinput_event_keyboard_get_time(
195 struct libinput_event_keyboard *event)
200 LIBINPUT_EXPORT uint32_t
201 libinput_event_keyboard_get_key(
202 struct libinput_event_keyboard *event)
207 LIBINPUT_EXPORT enum libinput_keyboard_key_state
208 libinput_event_keyboard_get_key_state(
209 struct libinput_event_keyboard *event)
214 LIBINPUT_EXPORT uint32_t
215 libinput_event_pointer_get_time(
216 struct libinput_event_pointer *event)
221 LIBINPUT_EXPORT li_fixed_t
222 libinput_event_pointer_get_dx(
223 struct libinput_event_pointer *event)
228 LIBINPUT_EXPORT li_fixed_t
229 libinput_event_pointer_get_dy(
230 struct libinput_event_pointer *event)
235 LIBINPUT_EXPORT li_fixed_t
236 libinput_event_pointer_get_absolute_x(
237 struct libinput_event_pointer *event)
242 LIBINPUT_EXPORT li_fixed_t
243 libinput_event_pointer_get_absolute_y(
244 struct libinput_event_pointer *event)
249 LIBINPUT_EXPORT uint32_t
250 libinput_event_pointer_get_button(
251 struct libinput_event_pointer *event)
253 return event->button;
256 LIBINPUT_EXPORT enum libinput_pointer_button_state
257 libinput_event_pointer_get_button_state(
258 struct libinput_event_pointer *event)
263 LIBINPUT_EXPORT enum libinput_pointer_axis
264 libinput_event_pointer_get_axis(
265 struct libinput_event_pointer *event)
270 LIBINPUT_EXPORT li_fixed_t
271 libinput_event_pointer_get_axis_value(
272 struct libinput_event_pointer *event)
277 LIBINPUT_EXPORT uint32_t
278 libinput_event_touch_get_time(
279 struct libinput_event_touch *event)
284 LIBINPUT_EXPORT uint32_t
285 libinput_event_touch_get_slot(
286 struct libinput_event_touch *event)
291 LIBINPUT_EXPORT li_fixed_t
292 libinput_event_touch_get_x(
293 struct libinput_event_touch *event)
298 LIBINPUT_EXPORT li_fixed_t
299 libinput_event_touch_get_y(
300 struct libinput_event_touch *event)
305 LIBINPUT_EXPORT enum libinput_touch_type
306 libinput_event_touch_get_touch_type(
307 struct libinput_event_touch *event)
309 return event->touch_type;
312 struct libinput_source *
313 libinput_add_fd(struct libinput *libinput,
315 libinput_source_dispatch_t dispatch,
318 struct libinput_source *source;
319 struct epoll_event ep;
321 source = malloc(sizeof *source);
325 source->dispatch = dispatch;
326 source->user_data = user_data;
329 memset(&ep, 0, sizeof ep);
331 ep.data.ptr = source;
333 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
343 libinput_remove_source(struct libinput *libinput,
344 struct libinput_source *source)
346 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
349 list_insert(&libinput->source_destroy_list, &source->link);
353 libinput_init(struct libinput *libinput,
354 const struct libinput_interface *interface,
355 const struct libinput_interface_backend *interface_backend,
358 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
359 if (libinput->epoll_fd < 0)
362 libinput->events_len = 4;
363 libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
364 if (!libinput->events) {
365 close(libinput->epoll_fd);
369 libinput->interface = interface;
370 libinput->interface_backend = interface_backend;
371 libinput->user_data = user_data;
372 list_init(&libinput->source_destroy_list);
373 list_init(&libinput->seat_list);
379 libinput_device_destroy(struct libinput_device *device);
382 libinput_seat_destroy(struct libinput_seat *seat);
385 libinput_drop_destroyed_sources(struct libinput *libinput)
387 struct libinput_source *source, *next;
389 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
391 list_init(&libinput->source_destroy_list);
395 libinput_destroy(struct libinput *libinput)
397 struct libinput_event *event;
398 struct libinput_device *device, *next_device;
399 struct libinput_seat *seat, *next_seat;
401 if (libinput == NULL)
404 libinput_suspend(libinput);
406 libinput->interface_backend->destroy(libinput);
408 while ((event = libinput_get_event(libinput)))
409 libinput_event_destroy(event);
411 libinput_drop_destroyed_sources(libinput);
413 free(libinput->events);
415 list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
416 list_for_each_safe(device, next_device,
419 libinput_device_destroy(device);
421 libinput_seat_destroy(seat);
424 close(libinput->epoll_fd);
429 libinput_event_destroy(struct libinput_event *event)
434 switch (libinput_event_get_type(event)) {
435 case LIBINPUT_EVENT_NONE:
436 abort(); /* not used as actual event type */
437 case LIBINPUT_EVENT_DEVICE_ADDED:
438 case LIBINPUT_EVENT_DEVICE_REMOVED:
440 case LIBINPUT_EVENT_KEYBOARD_KEY:
441 case LIBINPUT_EVENT_POINTER_MOTION:
442 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
443 case LIBINPUT_EVENT_POINTER_BUTTON:
444 case LIBINPUT_EVENT_POINTER_AXIS:
445 case LIBINPUT_EVENT_TOUCH_TOUCH:
446 case LIBINPUT_EVENT_TOUCH_FRAME:
447 libinput_device_unref(event->device);
455 open_restricted(struct libinput *libinput,
456 const char *path, int flags)
458 return libinput->interface->open_restricted(path,
460 libinput->user_data);
464 close_restricted(struct libinput *libinput, int fd)
466 return libinput->interface->close_restricted(fd, libinput->user_data);
470 libinput_seat_init(struct libinput_seat *seat,
471 struct libinput *libinput,
472 const char *physical_name,
473 const char *logical_name,
474 libinput_seat_destroy_func destroy)
477 seat->libinput = libinput;
478 seat->physical_name = strdup(physical_name);
479 seat->logical_name = strdup(logical_name);
480 seat->destroy = destroy;
481 list_init(&seat->devices_list);
485 libinput_seat_ref(struct libinput_seat *seat)
491 libinput_seat_destroy(struct libinput_seat *seat)
493 list_remove(&seat->link);
494 free(seat->logical_name);
495 free(seat->physical_name);
500 libinput_seat_unref(struct libinput_seat *seat)
502 assert(seat->refcount > 0);
504 if (seat->refcount == 0)
505 libinput_seat_destroy(seat);
509 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
511 seat->user_data = user_data;
514 LIBINPUT_EXPORT void *
515 libinput_seat_get_user_data(struct libinput_seat *seat)
517 return seat->user_data;
520 LIBINPUT_EXPORT const char *
521 libinput_seat_get_physical_name(struct libinput_seat *seat)
523 return seat->physical_name;
526 LIBINPUT_EXPORT const char *
527 libinput_seat_get_logical_name(struct libinput_seat *seat)
529 return seat->logical_name;
533 libinput_device_init(struct libinput_device *device,
534 struct libinput_seat *seat)
537 device->refcount = 1;
541 libinput_device_ref(struct libinput_device *device)
547 libinput_device_destroy(struct libinput_device *device)
549 evdev_device_destroy((struct evdev_device *) device);
553 libinput_device_unref(struct libinput_device *device)
555 assert(device->refcount > 0);
557 if (device->refcount == 0)
558 libinput_device_destroy(device);
562 libinput_get_fd(struct libinput *libinput)
564 return libinput->epoll_fd;
568 libinput_dispatch(struct libinput *libinput)
570 struct libinput_source *source;
571 struct epoll_event ep[32];
574 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
578 for (i = 0; i < count; ++i) {
579 source = ep[i].data.ptr;
580 if (source->fd == -1)
583 source->dispatch(source->user_data);
586 libinput_drop_destroyed_sources(libinput);
592 init_event_base(struct libinput_event *event,
593 struct libinput_device *device,
594 enum libinput_event_type type)
597 event->device = device;
601 post_base_event(struct libinput_device *device,
602 enum libinput_event_type type,
603 struct libinput_event *event)
605 struct libinput *libinput = device->seat->libinput;
606 init_event_base(event, device, type);
607 libinput_post_event(libinput, event);
611 post_device_event(struct libinput_device *device,
612 enum libinput_event_type type,
613 struct libinput_event *event)
615 init_event_base(event, device, type);
616 libinput_post_event(device->seat->libinput, event);
620 notify_added_device(struct libinput_device *device)
622 struct libinput_event_device_notify *added_device_event;
624 added_device_event = zalloc(sizeof *added_device_event);
625 if (!added_device_event)
628 *added_device_event = (struct libinput_event_device_notify) {
632 post_base_event(device,
633 LIBINPUT_EVENT_DEVICE_ADDED,
634 &added_device_event->base);
638 notify_removed_device(struct libinput_device *device)
640 struct libinput_event_device_notify *removed_device_event;
642 removed_device_event = zalloc(sizeof *removed_device_event);
643 if (!removed_device_event)
646 *removed_device_event = (struct libinput_event_device_notify) {
650 post_base_event(device,
651 LIBINPUT_EVENT_DEVICE_REMOVED,
652 &removed_device_event->base);
656 keyboard_notify_key(struct libinput_device *device,
659 enum libinput_keyboard_key_state state)
661 struct libinput_event_keyboard *key_event;
663 key_event = zalloc(sizeof *key_event);
667 *key_event = (struct libinput_event_keyboard) {
673 post_device_event(device,
674 LIBINPUT_EVENT_KEYBOARD_KEY,
679 pointer_notify_motion(struct libinput_device *device,
684 struct libinput_event_pointer *motion_event;
686 motion_event = zalloc(sizeof *motion_event);
690 *motion_event = (struct libinput_event_pointer) {
696 post_device_event(device,
697 LIBINPUT_EVENT_POINTER_MOTION,
698 &motion_event->base);
702 pointer_notify_motion_absolute(struct libinput_device *device,
707 struct libinput_event_pointer *motion_absolute_event;
709 motion_absolute_event = zalloc(sizeof *motion_absolute_event);
710 if (!motion_absolute_event)
713 *motion_absolute_event = (struct libinput_event_pointer) {
719 post_device_event(device,
720 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
721 &motion_absolute_event->base);
725 pointer_notify_button(struct libinput_device *device,
728 enum libinput_pointer_button_state state)
730 struct libinput_event_pointer *button_event;
732 button_event = zalloc(sizeof *button_event);
736 *button_event = (struct libinput_event_pointer) {
742 post_device_event(device,
743 LIBINPUT_EVENT_POINTER_BUTTON,
744 &button_event->base);
748 pointer_notify_axis(struct libinput_device *device,
750 enum libinput_pointer_axis axis,
753 struct libinput_event_pointer *axis_event;
755 axis_event = zalloc(sizeof *axis_event);
759 *axis_event = (struct libinput_event_pointer) {
765 post_device_event(device,
766 LIBINPUT_EVENT_POINTER_AXIS,
771 touch_notify_touch(struct libinput_device *device,
776 enum libinput_touch_type touch_type)
778 struct libinput_event_touch *touch_event;
780 touch_event = zalloc(sizeof *touch_event);
784 *touch_event = (struct libinput_event_touch) {
789 .touch_type = touch_type,
792 post_device_event(device,
793 LIBINPUT_EVENT_TOUCH_TOUCH,
798 touch_notify_frame(struct libinput_device *device,
801 struct libinput_event_touch *touch_event;
803 touch_event = zalloc(sizeof *touch_event);
807 *touch_event = (struct libinput_event_touch) {
811 post_device_event(device,
812 LIBINPUT_EVENT_TOUCH_FRAME,
818 libinput_post_event(struct libinput *libinput,
819 struct libinput_event *event)
821 struct libinput_event **events = libinput->events;
822 size_t events_len = libinput->events_len;
823 size_t events_count = libinput->events_count;
828 if (events_count > events_len) {
830 events = realloc(events, events_len * sizeof *events);
832 fprintf(stderr, "Failed to reallocate event ring "
837 if (libinput->events_count > 0 && libinput->events_in == 0) {
838 libinput->events_in = libinput->events_len;
839 } else if (libinput->events_count > 0 &&
840 libinput->events_out >= libinput->events_in) {
841 move_len = libinput->events_len - libinput->events_out;
842 new_out = events_len - move_len;
843 memmove(events + new_out,
844 events + libinput->events_out,
845 move_len * sizeof *events);
846 libinput->events_out = new_out;
849 libinput->events = events;
850 libinput->events_len = events_len;
853 switch (libinput_event_get_type(event)) {
854 case LIBINPUT_EVENT_NONE:
855 abort(); /* not used as actual event type */
856 case LIBINPUT_EVENT_DEVICE_ADDED:
857 case LIBINPUT_EVENT_DEVICE_REMOVED:
859 case LIBINPUT_EVENT_KEYBOARD_KEY:
860 case LIBINPUT_EVENT_POINTER_MOTION:
861 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
862 case LIBINPUT_EVENT_POINTER_BUTTON:
863 case LIBINPUT_EVENT_POINTER_AXIS:
864 case LIBINPUT_EVENT_TOUCH_TOUCH:
865 case LIBINPUT_EVENT_TOUCH_FRAME:
866 libinput_device_ref(event->device);
870 libinput->events_count = events_count;
871 events[libinput->events_in] = event;
872 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
875 LIBINPUT_EXPORT struct libinput_event *
876 libinput_get_event(struct libinput *libinput)
878 struct libinput_event *event;
880 if (libinput->events_count == 0)
883 event = libinput->events[libinput->events_out];
884 libinput->events_out =
885 (libinput->events_out + 1) % libinput->events_len;
886 libinput->events_count--;
891 LIBINPUT_EXPORT enum libinput_event_type
892 libinput_next_event_type(struct libinput *libinput)
894 struct libinput_event *event;
896 if (libinput->events_count == 0)
897 return LIBINPUT_EVENT_NONE;
899 event = libinput->events[libinput->events_out];
903 LIBINPUT_EXPORT void *
904 libinput_get_user_data(struct libinput *libinput)
906 return libinput->user_data;
910 libinput_resume(struct libinput *libinput)
912 return libinput->interface_backend->resume(libinput);
916 libinput_suspend(struct libinput *libinput)
918 libinput->interface_backend->suspend(libinput);
922 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
924 device->user_data = user_data;
927 LIBINPUT_EXPORT void *
928 libinput_device_get_user_data(struct libinput_device *device)
930 return device->user_data;
933 LIBINPUT_EXPORT const char *
934 libinput_device_get_sysname(struct libinput_device *device)
936 return evdev_device_get_sysname((struct evdev_device *) device);
939 LIBINPUT_EXPORT const char *
940 libinput_device_get_output_name(struct libinput_device *device)
942 return evdev_device_get_output((struct evdev_device *) device);
945 LIBINPUT_EXPORT struct libinput_seat *
946 libinput_device_get_seat(struct libinput_device *device)
952 libinput_device_led_update(struct libinput_device *device,
953 enum libinput_led leds)
955 evdev_device_led_update((struct evdev_device *) device, leds);
959 libinput_device_get_keys(struct libinput_device *device,
960 char *keys, size_t size)
962 return evdev_device_get_keys((struct evdev_device *) device,
968 libinput_device_calibrate(struct libinput_device *device,
969 float calibration[6])
971 evdev_device_calibrate((struct evdev_device *) device, calibration);
975 libinput_device_has_capability(struct libinput_device *device,
976 enum libinput_device_capability capability)
978 return evdev_device_has_capability((struct evdev_device *) device,