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>
33 #include "libinput-private.h"
35 struct libinput_source {
36 libinput_source_dispatch_t dispatch;
42 struct libinput_source *
43 libinput_add_fd(struct libinput *libinput,
45 libinput_source_dispatch_t dispatch,
48 struct libinput_source *source;
49 struct epoll_event ep;
51 source = malloc(sizeof *source);
55 source->dispatch = dispatch;
56 source->user_data = user_data;
59 memset(&ep, 0, sizeof ep);
63 if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
73 libinput_remove_source(struct libinput *libinput,
74 struct libinput_source *source)
76 epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
79 list_insert(&libinput->source_destroy_list, &source->link);
82 LIBINPUT_EXPORT struct libinput *
85 struct libinput *libinput;
87 libinput = zalloc(sizeof *libinput);
91 list_init(&libinput->source_destroy_list);
93 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
94 if (libinput->epoll_fd < 0)
101 libinput_destroy(struct libinput *libinput)
103 struct libinput_event *event;
105 while ((event = libinput_get_event(libinput)))
107 free(libinput->events);
109 close(libinput->epoll_fd);
114 libinput_get_fd(struct libinput *libinput)
116 return libinput->epoll_fd;
120 libinput_dispatch(struct libinput *libinput)
122 struct libinput_source *source, *next;
123 struct epoll_event ep[32];
126 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
130 for (i = 0; i < count; ++i) {
131 source = ep[i].data.ptr;
132 if (source->fd == -1)
135 source->dispatch(source->user_data);
138 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
140 list_init(&libinput->source_destroy_list);
146 init_event_base(struct libinput_event *event,
147 enum libinput_event_type type,
148 struct libinput_device *device)
151 event->device = device;
155 post_device_event(struct libinput_device *device,
156 enum libinput_event_type type,
157 struct libinput_event *event)
159 init_event_base(event, type, device);
160 libinput_post_event(device->libinput, event);
164 keyboard_notify_key(struct libinput_device *device,
167 enum libinput_keyboard_key_state state)
169 struct libinput_event_keyboard_key *key_event;
171 key_event = malloc(sizeof *key_event);
175 *key_event = (struct libinput_event_keyboard_key) {
181 post_device_event(device,
182 LIBINPUT_EVENT_KEYBOARD_KEY,
187 pointer_notify_motion(struct libinput_device *device,
192 struct libinput_event_pointer_motion *motion_event;
194 motion_event = malloc(sizeof *motion_event);
198 *motion_event = (struct libinput_event_pointer_motion) {
204 post_device_event(device,
205 LIBINPUT_EVENT_POINTER_MOTION,
206 &motion_event->base);
210 pointer_notify_motion_absolute(struct libinput_device *device,
215 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
217 motion_absolute_event = malloc(sizeof *motion_absolute_event);
218 if (!motion_absolute_event)
221 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
227 post_device_event(device,
228 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
229 &motion_absolute_event->base);
233 pointer_notify_button(struct libinput_device *device,
236 enum libinput_pointer_button_state state)
238 struct libinput_event_pointer_button *button_event;
240 button_event = malloc(sizeof *button_event);
244 *button_event = (struct libinput_event_pointer_button) {
250 post_device_event(device,
251 LIBINPUT_EVENT_POINTER_BUTTON,
252 &button_event->base);
256 pointer_notify_axis(struct libinput_device *device,
258 enum libinput_pointer_axis axis,
261 struct libinput_event_pointer_axis *axis_event;
263 axis_event = malloc(sizeof *axis_event);
267 *axis_event = (struct libinput_event_pointer_axis) {
273 post_device_event(device,
274 LIBINPUT_EVENT_POINTER_AXIS,
279 touch_notify_touch(struct libinput_device *device,
284 enum libinput_touch_type touch_type)
286 struct libinput_event_touch_touch *touch_event;
288 touch_event = malloc(sizeof *touch_event);
292 *touch_event = (struct libinput_event_touch_touch) {
297 .touch_type = touch_type,
300 post_device_event(device,
301 LIBINPUT_EVENT_TOUCH_TOUCH,
306 libinput_post_event(struct libinput *libinput,
307 struct libinput_event *event)
309 struct libinput_event **events = libinput->events;
310 size_t events_len = libinput->events_len;
311 size_t events_count = libinput->events_count;
316 if (events_count > events_len) {
321 events = realloc(events, events_len * sizeof *events);
323 fprintf(stderr, "Failed to reallocate event ring "
328 if (libinput->events_count > 0 && libinput->events_in == 0) {
329 libinput->events_in = libinput->events_len;
330 } else if (libinput->events_count > 0 &&
331 libinput->events_out >= libinput->events_in) {
332 move_len = libinput->events_len - libinput->events_out;
333 new_out = events_len - move_len;
334 memmove(events + new_out,
335 libinput->events + libinput->events_out,
336 move_len * sizeof *events);
337 libinput->events_out = new_out;
340 libinput->events = events;
341 libinput->events_len = events_len;
344 libinput->events_count = events_count;
345 events[libinput->events_in] = event;
346 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
349 LIBINPUT_EXPORT struct libinput_event *
350 libinput_get_event(struct libinput *libinput)
352 struct libinput_event *event;
354 if (libinput->events_count == 0)
357 event = libinput->events[libinput->events_out];
358 libinput->events_out =
359 (libinput->events_out + 1) % libinput->events_len;
360 libinput->events_count--;
366 libinput_device_destroy(struct libinput_device *device)
368 evdev_device_destroy((struct evdev_device *) device);
371 LIBINPUT_EXPORT void *
372 libinput_device_get_user_data(struct libinput_device *device)
374 return device->device_interface_data;
378 libinput_device_led_update(struct libinput_device *device,
379 enum libinput_led leds)
381 evdev_device_led_update((struct evdev_device *) device, leds);
385 libinput_device_get_keys(struct libinput_device *device,
386 char *keys, size_t size)
388 return evdev_device_get_keys((struct evdev_device *) device,
394 libinput_device_calibrate(struct libinput_device *device,
395 float calibration[6])
397 evdev_device_calibrate((struct evdev_device *) device, calibration);