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 *
84 libinput_create(const struct libinput_interface *interface, void *user_data)
86 struct libinput *libinput;
88 libinput = zalloc(sizeof *libinput);
92 list_init(&libinput->source_destroy_list);
94 libinput->interface = interface;
95 libinput->user_data = user_data;
97 libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
98 if (libinput->epoll_fd < 0)
105 libinput_destroy(struct libinput *libinput)
107 struct libinput_event *event;
109 while ((event = libinput_get_event(libinput)))
111 free(libinput->events);
113 close(libinput->epoll_fd);
118 libinput_get_fd(struct libinput *libinput)
120 return libinput->epoll_fd;
124 libinput_dispatch(struct libinput *libinput)
126 struct libinput_source *source, *next;
127 struct epoll_event ep[32];
130 count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
134 for (i = 0; i < count; ++i) {
135 source = ep[i].data.ptr;
136 if (source->fd == -1)
139 source->dispatch(source->user_data);
142 list_for_each_safe(source, next, &libinput->source_destroy_list, link)
144 list_init(&libinput->source_destroy_list);
150 init_event_base(struct libinput_event *event,
151 enum libinput_event_type type,
152 struct libinput_device *device)
155 event->device = device;
159 post_device_event(struct libinput_device *device,
160 enum libinput_event_type type,
161 struct libinput_event *event)
163 init_event_base(event, type, device);
164 libinput_post_event(device->libinput, event);
168 device_register_capability(struct libinput_device *device,
169 enum libinput_device_capability capability)
171 struct libinput_event_device_register_capability *capability_event;
173 capability_event = malloc(sizeof *capability_event);
175 *capability_event = (struct libinput_event_device_register_capability) {
176 .capability = capability,
179 post_device_event(device,
180 LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
181 &capability_event->base);
185 device_unregister_capability(struct libinput_device *device,
186 enum libinput_device_capability capability)
188 struct libinput_event_device_unregister_capability *capability_event;
190 capability_event = malloc(sizeof *capability_event);
192 *capability_event = (struct libinput_event_device_unregister_capability) {
193 .capability = capability,
196 post_device_event(device,
197 LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
198 &capability_event->base);
202 keyboard_notify_key(struct libinput_device *device,
205 enum libinput_keyboard_key_state state)
207 struct libinput_event_keyboard_key *key_event;
209 key_event = malloc(sizeof *key_event);
213 *key_event = (struct libinput_event_keyboard_key) {
219 post_device_event(device,
220 LIBINPUT_EVENT_KEYBOARD_KEY,
225 pointer_notify_motion(struct libinput_device *device,
230 struct libinput_event_pointer_motion *motion_event;
232 motion_event = malloc(sizeof *motion_event);
236 *motion_event = (struct libinput_event_pointer_motion) {
242 post_device_event(device,
243 LIBINPUT_EVENT_POINTER_MOTION,
244 &motion_event->base);
248 pointer_notify_motion_absolute(struct libinput_device *device,
253 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
255 motion_absolute_event = malloc(sizeof *motion_absolute_event);
256 if (!motion_absolute_event)
259 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
265 post_device_event(device,
266 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
267 &motion_absolute_event->base);
271 pointer_notify_button(struct libinput_device *device,
274 enum libinput_pointer_button_state state)
276 struct libinput_event_pointer_button *button_event;
278 button_event = malloc(sizeof *button_event);
282 *button_event = (struct libinput_event_pointer_button) {
288 post_device_event(device,
289 LIBINPUT_EVENT_POINTER_BUTTON,
290 &button_event->base);
294 pointer_notify_axis(struct libinput_device *device,
296 enum libinput_pointer_axis axis,
299 struct libinput_event_pointer_axis *axis_event;
301 axis_event = malloc(sizeof *axis_event);
305 *axis_event = (struct libinput_event_pointer_axis) {
311 post_device_event(device,
312 LIBINPUT_EVENT_POINTER_AXIS,
317 touch_notify_touch(struct libinput_device *device,
322 enum libinput_touch_type touch_type)
324 struct libinput_event_touch_touch *touch_event;
326 touch_event = malloc(sizeof *touch_event);
330 *touch_event = (struct libinput_event_touch_touch) {
335 .touch_type = touch_type,
338 post_device_event(device,
339 LIBINPUT_EVENT_TOUCH_TOUCH,
344 libinput_post_event(struct libinput *libinput,
345 struct libinput_event *event)
347 struct libinput_event **events = libinput->events;
348 size_t events_len = libinput->events_len;
349 size_t events_count = libinput->events_count;
354 if (events_count > events_len) {
359 events = realloc(events, events_len * sizeof *events);
361 fprintf(stderr, "Failed to reallocate event ring "
366 if (libinput->events_count > 0 && libinput->events_in == 0) {
367 libinput->events_in = libinput->events_len;
368 } else if (libinput->events_count > 0 &&
369 libinput->events_out >= libinput->events_in) {
370 move_len = libinput->events_len - libinput->events_out;
371 new_out = events_len - move_len;
372 memmove(events + new_out,
373 libinput->events + libinput->events_out,
374 move_len * sizeof *events);
375 libinput->events_out = new_out;
378 libinput->events = events;
379 libinput->events_len = events_len;
382 libinput->events_count = events_count;
383 events[libinput->events_in] = event;
384 libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
387 LIBINPUT_EXPORT struct libinput_event *
388 libinput_get_event(struct libinput *libinput)
390 struct libinput_event *event;
392 if (libinput->events_count == 0)
395 event = libinput->events[libinput->events_out];
396 libinput->events_out =
397 (libinput->events_out + 1) % libinput->events_len;
398 libinput->events_count--;
404 libinput_device_terminate(struct libinput_device *device)
406 evdev_device_terminate((struct evdev_device *) device);
407 device->terminated = 1;
411 libinput_device_destroy(struct libinput_device *device)
413 assert(device->terminated);
414 evdev_device_destroy((struct evdev_device *) device);
417 LIBINPUT_EXPORT void *
418 libinput_device_get_user_data(struct libinput_device *device)
420 return device->user_data;
424 libinput_device_led_update(struct libinput_device *device,
425 enum libinput_led leds)
427 evdev_device_led_update((struct evdev_device *) device, leds);
431 libinput_device_get_keys(struct libinput_device *device,
432 char *keys, size_t size)
434 return evdev_device_get_keys((struct evdev_device *) device,
440 libinput_device_calibrate(struct libinput_device *device,
441 float calibration[6])
443 evdev_device_calibrate((struct evdev_device *) device, calibration);