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"
36 #include "udev-seat.h"
38 struct libinput_source {
39 libinput_source_dispatch_t dispatch;
46 libinput_post_event(struct libinput *libinput,
47 struct libinput_event *event);
49 struct libinput_source *
50 libinput_add_fd(struct libinput *libinput,
52 libinput_source_dispatch_t dispatch,
55 struct libinput_source *source;
56 struct epoll_event ep;
58 source = malloc(sizeof *source);
62 source->dispatch = dispatch;
63 source->user_data = user_data;
66 memset(&ep, 0, sizeof ep);
70 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
80 libinput_remove_source(struct libinput *libinput,
81 struct libinput_source *source)
83 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
86 list_insert(&libinput->source_destroy_list, &source->link);
90 libinput_init(struct libinput *libinput,
91 const struct libinput_interface *interface,
94 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
95 if (libinput->epoll_fd < 0)
98 libinput->interface = interface;
99 libinput->user_data = user_data;
100 list_init(&libinput->source_destroy_list);
101 list_init(&libinput->seat_list);
107 libinput_destroy(struct libinput *libinput)
109 struct libinput_event *event;
111 while ((event = libinput_get_event(libinput)))
113 free(libinput->events);
115 close(libinput->epoll_fd);
120 open_restricted(struct libinput *libinput,
121 const char *path, int flags)
123 return libinput->interface->open_restricted(path,
125 libinput->user_data);
129 close_restricted(struct libinput *libinput, int fd)
131 return libinput->interface->close_restricted(fd, libinput->user_data);
135 libinput_seat_init(struct libinput_seat *seat,
136 struct libinput *libinput,
140 seat->libinput = libinput;
141 seat->name = strdup(name);
142 list_init(&seat->devices_list);
146 libinput_seat_ref(struct libinput_seat *seat)
152 libinput_seat_unref(struct libinput_seat *seat)
155 if (seat->refcount == 0) {
157 udev_seat_destroy((struct udev_seat *) seat);
162 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
164 seat->user_data = user_data;
167 LIBINPUT_EXPORT void *
168 libinput_seat_get_user_data(struct libinput_seat *seat)
170 return seat->user_data;
173 LIBINPUT_EXPORT const char *
174 libinput_seat_get_name(struct libinput_seat *seat)
180 libinput_device_init(struct libinput_device *device,
181 struct libinput_seat *seat)
184 device->refcount = 1;
188 libinput_device_ref(struct libinput_device *device)
194 libinput_device_unref(struct libinput_device *device)
197 if (device->refcount == 0)
198 evdev_device_destroy((struct evdev_device *) device);
202 libinput_get_fd(struct libinput *libinput)
204 return libinput->epoll_fd;
208 libinput_dispatch(struct libinput *libinput)
210 struct libinput_source *source, *next;
211 struct epoll_event ep[32];
214 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
218 for (i = 0; i < count; ++i) {
219 source = ep[i].data.ptr;
220 if (source->fd == -1)
223 source->dispatch(source->user_data);
226 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
228 list_init(&libinput->source_destroy_list);
234 init_event_base(struct libinput_event *event,
235 enum libinput_event_type type,
236 union libinput_event_target target)
239 event->target = target;
243 post_base_event(struct libinput *libinput,
244 enum libinput_event_type type,
245 struct libinput_event *event)
247 init_event_base(event, type,
248 (union libinput_event_target) { .libinput = libinput });
249 libinput_post_event(libinput, event);
254 post_seat_event(struct libinput_seat *seat,
255 enum libinput_event_type type,
256 struct libinput_event *event)
258 init_event_base(event, type,
259 (union libinput_event_target) { .seat = seat });
260 libinput_post_event(seat->libinput, event);
265 post_device_event(struct libinput_device *device,
266 enum libinput_event_type type,
267 struct libinput_event *event)
269 init_event_base(event, type,
270 (union libinput_event_target) { .device = device });
271 libinput_post_event(device->seat->libinput, event);
275 notify_added_seat(struct libinput_seat *seat)
277 struct libinput_event_added_seat *added_seat_event;
279 added_seat_event = malloc(sizeof *added_seat_event);
280 if (!added_seat_event)
283 *added_seat_event = (struct libinput_event_added_seat) {
287 post_base_event(seat->libinput,
288 LIBINPUT_EVENT_ADDED_SEAT,
289 &added_seat_event->base);
293 notify_removed_seat(struct libinput_seat *seat)
295 struct libinput_event_removed_seat *removed_seat_event;
297 removed_seat_event = malloc(sizeof *removed_seat_event);
298 if (!removed_seat_event)
301 *removed_seat_event = (struct libinput_event_removed_seat) {
305 post_base_event(seat->libinput,
306 LIBINPUT_EVENT_REMOVED_SEAT,
307 &removed_seat_event->base);
311 notify_added_device(struct libinput_device *device)
313 struct libinput_event_added_device *added_device_event;
315 added_device_event = malloc(sizeof *added_device_event);
316 if (!added_device_event)
319 *added_device_event = (struct libinput_event_added_device) {
323 post_base_event(device->seat->libinput,
324 LIBINPUT_EVENT_ADDED_DEVICE,
325 &added_device_event->base);
329 notify_removed_device(struct libinput_device *device)
331 struct libinput_event_removed_device *removed_device_event;
333 removed_device_event = malloc(sizeof *removed_device_event);
334 if (!removed_device_event)
337 *removed_device_event = (struct libinput_event_removed_device) {
341 post_base_event(device->seat->libinput,
342 LIBINPUT_EVENT_REMOVED_DEVICE,
343 &removed_device_event->base);
347 device_register_capability(struct libinput_device *device,
348 enum libinput_device_capability capability)
350 struct libinput_event_device_register_capability *capability_event;
352 capability_event = malloc(sizeof *capability_event);
354 *capability_event = (struct libinput_event_device_register_capability) {
355 .capability = capability,
358 post_device_event(device,
359 LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
360 &capability_event->base);
364 device_unregister_capability(struct libinput_device *device,
365 enum libinput_device_capability capability)
367 struct libinput_event_device_unregister_capability *capability_event;
369 capability_event = malloc(sizeof *capability_event);
371 *capability_event = (struct libinput_event_device_unregister_capability) {
372 .capability = capability,
375 post_device_event(device,
376 LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
377 &capability_event->base);
381 keyboard_notify_key(struct libinput_device *device,
384 enum libinput_keyboard_key_state state)
386 struct libinput_event_keyboard_key *key_event;
388 key_event = malloc(sizeof *key_event);
392 *key_event = (struct libinput_event_keyboard_key) {
398 post_device_event(device,
399 LIBINPUT_EVENT_KEYBOARD_KEY,
404 pointer_notify_motion(struct libinput_device *device,
409 struct libinput_event_pointer_motion *motion_event;
411 motion_event = malloc(sizeof *motion_event);
415 *motion_event = (struct libinput_event_pointer_motion) {
421 post_device_event(device,
422 LIBINPUT_EVENT_POINTER_MOTION,
423 &motion_event->base);
427 pointer_notify_motion_absolute(struct libinput_device *device,
432 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
434 motion_absolute_event = malloc(sizeof *motion_absolute_event);
435 if (!motion_absolute_event)
438 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
444 post_device_event(device,
445 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
446 &motion_absolute_event->base);
450 pointer_notify_button(struct libinput_device *device,
453 enum libinput_pointer_button_state state)
455 struct libinput_event_pointer_button *button_event;
457 button_event = malloc(sizeof *button_event);
461 *button_event = (struct libinput_event_pointer_button) {
467 post_device_event(device,
468 LIBINPUT_EVENT_POINTER_BUTTON,
469 &button_event->base);
473 pointer_notify_axis(struct libinput_device *device,
475 enum libinput_pointer_axis axis,
478 struct libinput_event_pointer_axis *axis_event;
480 axis_event = malloc(sizeof *axis_event);
484 *axis_event = (struct libinput_event_pointer_axis) {
490 post_device_event(device,
491 LIBINPUT_EVENT_POINTER_AXIS,
496 touch_notify_touch(struct libinput_device *device,
501 enum libinput_touch_type touch_type)
503 struct libinput_event_touch_touch *touch_event;
505 touch_event = malloc(sizeof *touch_event);
509 *touch_event = (struct libinput_event_touch_touch) {
514 .touch_type = touch_type,
517 post_device_event(device,
518 LIBINPUT_EVENT_TOUCH_TOUCH,
523 libinput_post_event(struct libinput *libinput,
524 struct libinput_event *event)
526 struct libinput_event **events = libinput->events;
527 size_t events_len = libinput->events_len;
528 size_t events_count = libinput->events_count;
533 if (events_count > events_len) {
538 events = realloc(events, events_len * sizeof *events);
540 fprintf(stderr, "Failed to reallocate event ring "
545 if (libinput->events_count > 0 && libinput->events_in == 0) {
546 libinput->events_in = libinput->events_len;
547 } else if (libinput->events_count > 0 &&
548 libinput->events_out >= libinput->events_in) {
549 move_len = libinput->events_len - libinput->events_out;
550 new_out = events_len - move_len;
551 memmove(events + new_out,
552 libinput->events + libinput->events_out,
553 move_len * sizeof *events);
554 libinput->events_out = new_out;
557 libinput->events = events;
558 libinput->events_len = events_len;
561 libinput->events_count = events_count;
562 events[libinput->events_in] = event;
563 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
566 LIBINPUT_EXPORT struct libinput_event *
567 libinput_get_event(struct libinput *libinput)
569 struct libinput_event *event;
571 if (libinput->events_count == 0)
574 event = libinput->events[libinput->events_out];
575 libinput->events_out =
576 (libinput->events_out + 1) % libinput->events_len;
577 libinput->events_count--;
582 LIBINPUT_EXPORT void *
583 libinput_get_user_data(struct libinput *libinput)
585 return libinput->user_data;
589 libinput_resume(struct libinput *libinput)
591 return udev_input_enable((struct udev_input *) libinput);
595 libinput_suspend(struct libinput *libinput)
597 udev_input_disable((struct udev_input *) libinput);
601 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
603 device->user_data = user_data;
606 LIBINPUT_EXPORT void *
607 libinput_device_get_user_data(struct libinput_device *device)
609 return device->user_data;
612 LIBINPUT_EXPORT const char *
613 libinput_device_get_output_name(struct libinput_device *device)
615 return evdev_device_get_output((struct evdev_device *) device);
618 LIBINPUT_EXPORT struct libinput_seat *
619 libinput_device_get_seat(struct libinput_device *device)
625 libinput_device_led_update(struct libinput_device *device,
626 enum libinput_led leds)
628 evdev_device_led_update((struct evdev_device *) device, leds);
632 libinput_device_get_keys(struct libinput_device *device,
633 char *keys, size_t size)
635 return evdev_device_get_keys((struct evdev_device *) device,
641 libinput_device_calibrate(struct libinput_device *device,
642 float calibration[6])
644 evdev_device_calibrate((struct evdev_device *) device, calibration);