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.
28 #include <sys/epoll.h>
34 #include "libinput-private.h"
36 struct libinput_source {
37 libinput_source_dispatch_t dispatch;
43 struct libinput_source *
44 libinput_add_fd(struct libinput *libinput,
46 libinput_source_dispatch_t dispatch,
49 struct libinput_source *source;
50 struct epoll_event ep;
52 source = malloc(sizeof *source);
56 source->dispatch = dispatch;
57 source->user_data = user_data;
60 memset(&ep, 0, sizeof ep);
64 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
74 libinput_remove_source(struct libinput *libinput,
75 struct libinput_source *source)
77 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
80 list_insert(&libinput->source_destroy_list, &source->link);
83 LIBINPUT_EXPORT struct libinput *
86 struct libinput *libinput;
88 libinput = zalloc(sizeof *libinput);
92 list_init(&libinput->source_destroy_list);
94 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
95 if (libinput->epoll_fd < 0)
102 libinput_destroy(struct libinput *libinput)
104 struct libinput_event *event;
106 while ((event = libinput_get_event(libinput)))
108 free(libinput->events);
110 close(libinput->epoll_fd);
115 libinput_get_fd(struct libinput *libinput)
117 return libinput->epoll_fd;
121 libinput_dispatch(struct libinput *libinput)
123 struct libinput_source *source, *next;
124 struct epoll_event ep[32];
127 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
131 for (i = 0; i < count; ++i) {
132 source = ep[i].data.ptr;
133 if (source->fd == -1)
136 source->dispatch(source->user_data);
139 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
141 list_init(&libinput->source_destroy_list);
147 init_event_base(struct libinput_event *event,
148 enum libinput_event_type type,
149 struct libinput_device *device)
152 event->device = device;
156 post_device_event(struct libinput_device *device,
157 enum libinput_event_type type,
158 struct libinput_event *event)
160 init_event_base(event, type, device);
161 libinput_post_event(device->libinput, event);
165 device_register_capability(struct libinput_device *device,
166 enum libinput_device_capability capability)
168 struct libinput_event_device_register_capability *capability_event;
170 capability_event = malloc(sizeof *capability_event);
172 *capability_event = (struct libinput_event_device_register_capability) {
173 .capability = capability,
176 post_device_event(device,
177 LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
178 &capability_event->base);
182 device_unregister_capability(struct libinput_device *device,
183 enum libinput_device_capability capability)
185 struct libinput_event_device_unregister_capability *capability_event;
187 capability_event = malloc(sizeof *capability_event);
189 *capability_event = (struct libinput_event_device_unregister_capability) {
190 .capability = capability,
193 post_device_event(device,
194 LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
195 &capability_event->base);
199 keyboard_notify_key(struct libinput_device *device,
202 enum libinput_keyboard_key_state state)
204 struct libinput_event_keyboard_key *key_event;
206 key_event = malloc(sizeof *key_event);
210 *key_event = (struct libinput_event_keyboard_key) {
216 post_device_event(device,
217 LIBINPUT_EVENT_KEYBOARD_KEY,
222 pointer_notify_motion(struct libinput_device *device,
227 struct libinput_event_pointer_motion *motion_event;
229 motion_event = malloc(sizeof *motion_event);
233 *motion_event = (struct libinput_event_pointer_motion) {
239 post_device_event(device,
240 LIBINPUT_EVENT_POINTER_MOTION,
241 &motion_event->base);
245 pointer_notify_motion_absolute(struct libinput_device *device,
250 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
252 motion_absolute_event = malloc(sizeof *motion_absolute_event);
253 if (!motion_absolute_event)
256 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
262 post_device_event(device,
263 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
264 &motion_absolute_event->base);
268 pointer_notify_button(struct libinput_device *device,
271 enum libinput_pointer_button_state state)
273 struct libinput_event_pointer_button *button_event;
275 button_event = malloc(sizeof *button_event);
279 *button_event = (struct libinput_event_pointer_button) {
285 post_device_event(device,
286 LIBINPUT_EVENT_POINTER_BUTTON,
287 &button_event->base);
291 pointer_notify_axis(struct libinput_device *device,
293 enum libinput_pointer_axis axis,
296 struct libinput_event_pointer_axis *axis_event;
298 axis_event = malloc(sizeof *axis_event);
302 *axis_event = (struct libinput_event_pointer_axis) {
308 post_device_event(device,
309 LIBINPUT_EVENT_POINTER_AXIS,
314 touch_notify_touch(struct libinput_device *device,
319 enum libinput_touch_type touch_type)
321 struct libinput_event_touch_touch *touch_event;
323 touch_event = malloc(sizeof *touch_event);
327 *touch_event = (struct libinput_event_touch_touch) {
332 .touch_type = touch_type,
335 post_device_event(device,
336 LIBINPUT_EVENT_TOUCH_TOUCH,
341 libinput_post_event(struct libinput *libinput,
342 struct libinput_event *event)
344 struct libinput_event **events = libinput->events;
345 size_t events_len = libinput->events_len;
346 size_t events_count = libinput->events_count;
351 if (events_count > events_len) {
356 events = realloc(events, events_len * sizeof *events);
358 fprintf(stderr, "Failed to reallocate event ring "
363 if (libinput->events_count > 0 && libinput->events_in == 0) {
364 libinput->events_in = libinput->events_len;
365 } else if (libinput->events_count > 0 &&
366 libinput->events_out >= libinput->events_in) {
367 move_len = libinput->events_len - libinput->events_out;
368 new_out = events_len - move_len;
369 memmove(events + new_out,
370 libinput->events + libinput->events_out,
371 move_len * sizeof *events);
372 libinput->events_out = new_out;
375 libinput->events = events;
376 libinput->events_len = events_len;
379 libinput->events_count = events_count;
380 events[libinput->events_in] = event;
381 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
384 LIBINPUT_EXPORT struct libinput_event *
385 libinput_get_event(struct libinput *libinput)
387 struct libinput_event *event;
389 if (libinput->events_count == 0)
392 event = libinput->events[libinput->events_out];
393 libinput->events_out =
394 (libinput->events_out + 1) % libinput->events_len;
395 libinput->events_count--;
401 libinput_device_terminate(struct libinput_device *device)
403 evdev_device_terminate((struct evdev_device *) device);
404 device->terminated = 1;
408 libinput_device_destroy(struct libinput_device *device)
410 assert(device->terminated);
411 evdev_device_destroy((struct evdev_device *) device);
414 LIBINPUT_EXPORT void *
415 libinput_device_get_user_data(struct libinput_device *device)
417 return device->device_interface_data;
421 libinput_device_led_update(struct libinput_device *device,
422 enum libinput_led leds)
424 evdev_device_led_update((struct evdev_device *) device, leds);
428 libinput_device_get_keys(struct libinput_device *device,
429 char *keys, size_t size)
431 return evdev_device_get_keys((struct evdev_device *) device,
437 libinput_device_calibrate(struct libinput_device *device,
438 float calibration[6])
440 evdev_device_calibrate((struct evdev_device *) device, calibration);