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);
230 return libinput->events_count > 0 ? 0 : -EAGAIN;
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);
253 post_device_event(struct libinput_device *device,
254 enum libinput_event_type type,
255 struct libinput_event *event)
257 init_event_base(event, type,
258 (union libinput_event_target) { .device = device });
259 libinput_post_event(device->seat->libinput, event);
263 notify_added_seat(struct libinput_seat *seat)
265 struct libinput_event_added_seat *added_seat_event;
267 added_seat_event = malloc(sizeof *added_seat_event);
268 if (!added_seat_event)
271 *added_seat_event = (struct libinput_event_added_seat) {
275 post_base_event(seat->libinput,
276 LIBINPUT_EVENT_ADDED_SEAT,
277 &added_seat_event->base);
281 notify_removed_seat(struct libinput_seat *seat)
283 struct libinput_event_removed_seat *removed_seat_event;
285 removed_seat_event = malloc(sizeof *removed_seat_event);
286 if (!removed_seat_event)
289 *removed_seat_event = (struct libinput_event_removed_seat) {
293 post_base_event(seat->libinput,
294 LIBINPUT_EVENT_REMOVED_SEAT,
295 &removed_seat_event->base);
299 notify_added_device(struct libinput_device *device)
301 struct libinput_event_added_device *added_device_event;
303 added_device_event = malloc(sizeof *added_device_event);
304 if (!added_device_event)
307 *added_device_event = (struct libinput_event_added_device) {
311 post_base_event(device->seat->libinput,
312 LIBINPUT_EVENT_ADDED_DEVICE,
313 &added_device_event->base);
317 notify_removed_device(struct libinput_device *device)
319 struct libinput_event_removed_device *removed_device_event;
321 removed_device_event = malloc(sizeof *removed_device_event);
322 if (!removed_device_event)
325 *removed_device_event = (struct libinput_event_removed_device) {
329 post_base_event(device->seat->libinput,
330 LIBINPUT_EVENT_REMOVED_DEVICE,
331 &removed_device_event->base);
335 device_register_capability(struct libinput_device *device,
336 enum libinput_device_capability capability)
338 struct libinput_event_device_register_capability *capability_event;
340 capability_event = malloc(sizeof *capability_event);
342 *capability_event = (struct libinput_event_device_register_capability) {
343 .capability = capability,
346 post_device_event(device,
347 LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
348 &capability_event->base);
352 device_unregister_capability(struct libinput_device *device,
353 enum libinput_device_capability capability)
355 struct libinput_event_device_unregister_capability *capability_event;
357 capability_event = malloc(sizeof *capability_event);
359 *capability_event = (struct libinput_event_device_unregister_capability) {
360 .capability = capability,
363 post_device_event(device,
364 LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
365 &capability_event->base);
369 keyboard_notify_key(struct libinput_device *device,
372 enum libinput_keyboard_key_state state)
374 struct libinput_event_keyboard_key *key_event;
376 key_event = malloc(sizeof *key_event);
380 *key_event = (struct libinput_event_keyboard_key) {
386 post_device_event(device,
387 LIBINPUT_EVENT_KEYBOARD_KEY,
392 pointer_notify_motion(struct libinput_device *device,
397 struct libinput_event_pointer_motion *motion_event;
399 motion_event = malloc(sizeof *motion_event);
403 *motion_event = (struct libinput_event_pointer_motion) {
409 post_device_event(device,
410 LIBINPUT_EVENT_POINTER_MOTION,
411 &motion_event->base);
415 pointer_notify_motion_absolute(struct libinput_device *device,
420 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
422 motion_absolute_event = malloc(sizeof *motion_absolute_event);
423 if (!motion_absolute_event)
426 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
432 post_device_event(device,
433 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
434 &motion_absolute_event->base);
438 pointer_notify_button(struct libinput_device *device,
441 enum libinput_pointer_button_state state)
443 struct libinput_event_pointer_button *button_event;
445 button_event = malloc(sizeof *button_event);
449 *button_event = (struct libinput_event_pointer_button) {
455 post_device_event(device,
456 LIBINPUT_EVENT_POINTER_BUTTON,
457 &button_event->base);
461 pointer_notify_axis(struct libinput_device *device,
463 enum libinput_pointer_axis axis,
466 struct libinput_event_pointer_axis *axis_event;
468 axis_event = malloc(sizeof *axis_event);
472 *axis_event = (struct libinput_event_pointer_axis) {
478 post_device_event(device,
479 LIBINPUT_EVENT_POINTER_AXIS,
484 touch_notify_touch(struct libinput_device *device,
489 enum libinput_touch_type touch_type)
491 struct libinput_event_touch_touch *touch_event;
493 touch_event = malloc(sizeof *touch_event);
497 *touch_event = (struct libinput_event_touch_touch) {
502 .touch_type = touch_type,
505 post_device_event(device,
506 LIBINPUT_EVENT_TOUCH_TOUCH,
511 libinput_post_event(struct libinput *libinput,
512 struct libinput_event *event)
514 struct libinput_event **events = libinput->events;
515 size_t events_len = libinput->events_len;
516 size_t events_count = libinput->events_count;
521 if (events_count > events_len) {
526 events = realloc(events, events_len * sizeof *events);
528 fprintf(stderr, "Failed to reallocate event ring "
533 if (libinput->events_count > 0 && libinput->events_in == 0) {
534 libinput->events_in = libinput->events_len;
535 } else if (libinput->events_count > 0 &&
536 libinput->events_out >= libinput->events_in) {
537 move_len = libinput->events_len - libinput->events_out;
538 new_out = events_len - move_len;
539 memmove(events + new_out,
540 libinput->events + libinput->events_out,
541 move_len * sizeof *events);
542 libinput->events_out = new_out;
545 libinput->events = events;
546 libinput->events_len = events_len;
549 libinput->events_count = events_count;
550 events[libinput->events_in] = event;
551 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
554 LIBINPUT_EXPORT struct libinput_event *
555 libinput_get_event(struct libinput *libinput)
557 struct libinput_event *event;
559 if (libinput->events_count == 0)
562 event = libinput->events[libinput->events_out];
563 libinput->events_out =
564 (libinput->events_out + 1) % libinput->events_len;
565 libinput->events_count--;
570 LIBINPUT_EXPORT void *
571 libinput_get_user_data(struct libinput *libinput)
573 return libinput->user_data;
577 libinput_resume(struct libinput *libinput)
579 return udev_input_enable((struct udev_input *) libinput);
583 libinput_suspend(struct libinput *libinput)
585 udev_input_disable((struct udev_input *) libinput);
589 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
591 device->user_data = user_data;
594 LIBINPUT_EXPORT void *
595 libinput_device_get_user_data(struct libinput_device *device)
597 return device->user_data;
600 LIBINPUT_EXPORT const char *
601 libinput_device_get_output_name(struct libinput_device *device)
603 return evdev_device_get_output((struct evdev_device *) device);
606 LIBINPUT_EXPORT struct libinput_seat *
607 libinput_device_get_seat(struct libinput_device *device)
613 libinput_device_led_update(struct libinput_device *device,
614 enum libinput_led leds)
616 evdev_device_led_update((struct evdev_device *) device, leds);
620 libinput_device_get_keys(struct libinput_device *device,
621 char *keys, size_t size)
623 return evdev_device_get_keys((struct evdev_device *) device,
629 libinput_device_calibrate(struct libinput_device *device,
630 float calibration[6])
632 evdev_device_calibrate((struct evdev_device *) device, calibration);