Fix some coding style inconsistencies
[platform/upstream/libinput.git] / src / libinput.c
1 /*
2  * Copyright © 2013 Jonas Ådahl
3  *
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.
13  *
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.
21  */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/epoll.h>
30 #include <unistd.h>
31 #include <assert.h>
32
33 #include "libinput.h"
34 #include "libinput-private.h"
35 #include "evdev.h"
36
37 struct libinput_source {
38         libinput_source_dispatch_t dispatch;
39         void *user_data;
40         int fd;
41         struct list link;
42 };
43
44 struct libinput_event {
45         enum libinput_event_type type;
46         struct libinput_device *device;
47 };
48
49 struct libinput_event_device_notify {
50         struct libinput_event base;
51 };
52
53 struct libinput_event_keyboard {
54         struct libinput_event base;
55         uint32_t time;
56         uint32_t key;
57         enum libinput_keyboard_key_state state;
58 };
59
60 struct libinput_event_pointer {
61         struct libinput_event base;
62         uint32_t time;
63         li_fixed_t x;
64         li_fixed_t y;
65         uint32_t button;
66         enum libinput_pointer_button_state state;
67         enum libinput_pointer_axis axis;
68         li_fixed_t value;
69 };
70
71 struct libinput_event_touch {
72         struct libinput_event base;
73         uint32_t time;
74         uint32_t slot;
75         li_fixed_t x;
76         li_fixed_t y;
77         enum libinput_touch_type touch_type;
78 };
79
80 static void
81 libinput_post_event(struct libinput *libinput,
82                     struct libinput_event *event);
83
84 LIBINPUT_EXPORT enum libinput_event_type
85 libinput_event_get_type(struct libinput_event *event)
86 {
87         return event->type;
88 }
89
90 LIBINPUT_EXPORT struct libinput *
91 libinput_event_get_context(struct libinput_event *event)
92 {
93         return event->device->seat->libinput;
94 }
95
96 LIBINPUT_EXPORT struct libinput_device *
97 libinput_event_get_device(struct libinput_event *event)
98 {
99         return event->device;
100 }
101
102 LIBINPUT_EXPORT struct libinput_event_pointer *
103 libinput_event_get_pointer_event(struct libinput_event *event)
104 {
105         switch (event->type) {
106         case LIBINPUT_EVENT_NONE:
107                 abort(); /* not used as actual event type */
108         case LIBINPUT_EVENT_DEVICE_ADDED:
109         case LIBINPUT_EVENT_DEVICE_REMOVED:
110         case LIBINPUT_EVENT_KEYBOARD_KEY:
111                 break;
112         case LIBINPUT_EVENT_POINTER_MOTION:
113         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
114         case LIBINPUT_EVENT_POINTER_BUTTON:
115         case LIBINPUT_EVENT_POINTER_AXIS:
116                 return (struct libinput_event_pointer *) event;
117         case LIBINPUT_EVENT_TOUCH_TOUCH:
118         case LIBINPUT_EVENT_TOUCH_FRAME:
119                 break;
120         }
121
122         return NULL;
123 }
124
125 LIBINPUT_EXPORT struct libinput_event_keyboard *
126 libinput_event_get_keyboard_event(struct libinput_event *event)
127 {
128         switch (event->type) {
129         case LIBINPUT_EVENT_NONE:
130                 abort(); /* not used as actual event type */
131         case LIBINPUT_EVENT_DEVICE_ADDED:
132         case LIBINPUT_EVENT_DEVICE_REMOVED:
133                 break;
134         case LIBINPUT_EVENT_KEYBOARD_KEY:
135                 return (struct libinput_event_keyboard *) event;
136         case LIBINPUT_EVENT_POINTER_MOTION:
137         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
138         case LIBINPUT_EVENT_POINTER_BUTTON:
139         case LIBINPUT_EVENT_POINTER_AXIS:
140         case LIBINPUT_EVENT_TOUCH_TOUCH:
141         case LIBINPUT_EVENT_TOUCH_FRAME:
142                 break;
143         }
144
145         return NULL;
146 }
147
148 LIBINPUT_EXPORT struct libinput_event_touch *
149 libinput_event_get_touch_event(struct libinput_event *event)
150 {
151         switch (event->type) {
152         case LIBINPUT_EVENT_NONE:
153                 abort(); /* not used as actual event type */
154         case LIBINPUT_EVENT_DEVICE_ADDED:
155         case LIBINPUT_EVENT_DEVICE_REMOVED:
156         case LIBINPUT_EVENT_KEYBOARD_KEY:
157         case LIBINPUT_EVENT_POINTER_MOTION:
158         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
159         case LIBINPUT_EVENT_POINTER_BUTTON:
160         case LIBINPUT_EVENT_POINTER_AXIS:
161                 break;
162         case LIBINPUT_EVENT_TOUCH_TOUCH:
163         case LIBINPUT_EVENT_TOUCH_FRAME:
164                 return (struct libinput_event_touch *) event;
165         }
166
167         return NULL;
168 }
169
170 LIBINPUT_EXPORT struct libinput_event_device_notify *
171 libinput_event_get_device_notify_event(struct libinput_event *event)
172 {
173         switch (event->type) {
174         case LIBINPUT_EVENT_NONE:
175                 abort(); /* not used as actual event type */
176         case LIBINPUT_EVENT_DEVICE_ADDED:
177         case LIBINPUT_EVENT_DEVICE_REMOVED:
178                 return (struct libinput_event_device_notify *) event;
179         case LIBINPUT_EVENT_KEYBOARD_KEY:
180         case LIBINPUT_EVENT_POINTER_MOTION:
181         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
182         case LIBINPUT_EVENT_POINTER_BUTTON:
183         case LIBINPUT_EVENT_POINTER_AXIS:
184         case LIBINPUT_EVENT_TOUCH_TOUCH:
185         case LIBINPUT_EVENT_TOUCH_FRAME:
186                 break;
187         }
188
189         return NULL;
190 }
191
192 LIBINPUT_EXPORT uint32_t
193 libinput_event_keyboard_get_time(
194         struct libinput_event_keyboard *event)
195 {
196         return event->time;
197 }
198
199 LIBINPUT_EXPORT uint32_t
200 libinput_event_keyboard_get_key(
201         struct libinput_event_keyboard *event)
202 {
203         return event->key;
204 }
205
206 LIBINPUT_EXPORT enum libinput_keyboard_key_state
207 libinput_event_keyboard_get_key_state(
208         struct libinput_event_keyboard *event)
209 {
210         return event->state;
211 }
212
213 LIBINPUT_EXPORT uint32_t
214 libinput_event_pointer_get_time(
215         struct libinput_event_pointer *event)
216 {
217         return event->time;
218 }
219
220 LIBINPUT_EXPORT li_fixed_t
221 libinput_event_pointer_get_dx(
222         struct libinput_event_pointer *event)
223 {
224         return event->x;
225 }
226
227 LIBINPUT_EXPORT li_fixed_t
228 libinput_event_pointer_get_dy(
229         struct libinput_event_pointer *event)
230 {
231         return event->y;
232 }
233
234 LIBINPUT_EXPORT li_fixed_t
235 libinput_event_pointer_get_absolute_x(
236         struct libinput_event_pointer *event)
237 {
238         return event->x;
239 }
240
241 LIBINPUT_EXPORT li_fixed_t
242 libinput_event_pointer_get_absolute_y(
243         struct libinput_event_pointer *event)
244 {
245         return event->y;
246 }
247
248 LIBINPUT_EXPORT uint32_t
249 libinput_event_pointer_get_button(
250         struct libinput_event_pointer *event)
251 {
252         return event->button;
253 }
254
255 LIBINPUT_EXPORT enum libinput_pointer_button_state
256 libinput_event_pointer_get_button_state(
257         struct libinput_event_pointer *event)
258 {
259         return event->state;
260 }
261
262 LIBINPUT_EXPORT enum libinput_pointer_axis
263 libinput_event_pointer_get_axis(
264         struct libinput_event_pointer *event)
265 {
266         return event->axis;
267 }
268
269 LIBINPUT_EXPORT li_fixed_t
270 libinput_event_pointer_get_axis_value(
271         struct libinput_event_pointer *event)
272 {
273         return event->value;
274 }
275
276 LIBINPUT_EXPORT uint32_t
277 libinput_event_touch_get_time(
278         struct libinput_event_touch *event)
279 {
280         return event->time;
281 }
282
283 LIBINPUT_EXPORT uint32_t
284 libinput_event_touch_get_slot(
285         struct libinput_event_touch *event)
286 {
287         return event->slot;
288 }
289
290 LIBINPUT_EXPORT li_fixed_t
291 libinput_event_touch_get_x(
292         struct libinput_event_touch *event)
293 {
294         return event->x;
295 }
296
297 LIBINPUT_EXPORT li_fixed_t
298 libinput_event_touch_get_y(
299         struct libinput_event_touch *event)
300 {
301         return event->y;
302 }
303
304 LIBINPUT_EXPORT enum libinput_touch_type
305 libinput_event_touch_get_touch_type(
306         struct libinput_event_touch *event)
307 {
308         return event->touch_type;
309 }
310
311 struct libinput_source *
312 libinput_add_fd(struct libinput *libinput,
313                 int fd,
314                 libinput_source_dispatch_t dispatch,
315                 void *user_data)
316 {
317         struct libinput_source *source;
318         struct epoll_event ep;
319
320         source = malloc(sizeof *source);
321         if (!source)
322                 return NULL;
323
324         source->dispatch = dispatch;
325         source->user_data = user_data;
326         source->fd = fd;
327
328         memset(&ep, 0, sizeof ep);
329         ep.events = EPOLLIN;
330         ep.data.ptr = source;
331
332         if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
333                 close(source->fd);
334                 free(source);
335                 return NULL;
336         }
337
338         return source;
339 }
340
341 void
342 libinput_remove_source(struct libinput *libinput,
343                        struct libinput_source *source)
344 {
345         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
346         close(source->fd);
347         source->fd = -1;
348         list_insert(&libinput->source_destroy_list, &source->link);
349 }
350
351 int
352 libinput_init(struct libinput *libinput,
353               const struct libinput_interface *interface,
354               const struct libinput_interface_backend *interface_backend,
355               void *user_data)
356 {
357         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
358         if (libinput->epoll_fd < 0)
359                 return -1;
360
361         libinput->events_len = 4;
362         libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
363         if (!libinput->events) {
364                 close(libinput->epoll_fd);
365                 return -1;
366         }
367
368         libinput->interface = interface;
369         libinput->interface_backend = interface_backend;
370         libinput->user_data = user_data;
371         list_init(&libinput->source_destroy_list);
372         list_init(&libinput->seat_list);
373
374         return 0;
375 }
376
377 static void
378 libinput_device_destroy(struct libinput_device *device);
379
380 static void
381 libinput_seat_destroy(struct libinput_seat *seat);
382
383 static void
384 libinput_drop_destroyed_sources(struct libinput *libinput)
385 {
386         struct libinput_source *source, *next;
387
388         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
389                 free(source);
390         list_init(&libinput->source_destroy_list);
391 }
392
393 LIBINPUT_EXPORT void
394 libinput_destroy(struct libinput *libinput)
395 {
396         struct libinput_event *event;
397         struct libinput_device *device, *next_device;
398         struct libinput_seat *seat, *next_seat;
399
400         if (libinput == NULL)
401                 return;
402
403         libinput_suspend(libinput);
404
405         libinput->interface_backend->destroy(libinput);
406
407         while ((event = libinput_get_event(libinput)))
408                libinput_event_destroy(event);
409
410         libinput_drop_destroyed_sources(libinput);
411
412         free(libinput->events);
413
414         list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
415                 list_for_each_safe(device, next_device,
416                                    &seat->devices_list,
417                                    link)
418                         libinput_device_destroy(device);
419
420                 libinput_seat_destroy(seat);
421         }
422
423         close(libinput->epoll_fd);
424         free(libinput);
425 }
426
427 LIBINPUT_EXPORT void
428 libinput_event_destroy(struct libinput_event *event)
429 {
430         if (event == NULL)
431                 return;
432
433         if (event->device)
434                 libinput_device_unref(event->device);
435
436         free(event);
437 }
438
439 int
440 open_restricted(struct libinput *libinput,
441                 const char *path, int flags)
442 {
443         return libinput->interface->open_restricted(path,
444                                                     flags,
445                                                     libinput->user_data);
446 }
447
448 void
449 close_restricted(struct libinput *libinput, int fd)
450 {
451         return libinput->interface->close_restricted(fd, libinput->user_data);
452 }
453
454 void
455 libinput_seat_init(struct libinput_seat *seat,
456                    struct libinput *libinput,
457                    const char *physical_name,
458                    const char *logical_name,
459                    libinput_seat_destroy_func destroy)
460 {
461         seat->refcount = 1;
462         seat->libinput = libinput;
463         seat->physical_name = strdup(physical_name);
464         seat->logical_name = strdup(logical_name);
465         seat->destroy = destroy;
466         list_init(&seat->devices_list);
467 }
468
469 LIBINPUT_EXPORT void
470 libinput_seat_ref(struct libinput_seat *seat)
471 {
472         seat->refcount++;
473 }
474
475 static void
476 libinput_seat_destroy(struct libinput_seat *seat)
477 {
478         list_remove(&seat->link);
479         free(seat->logical_name);
480         free(seat->physical_name);
481         seat->destroy(seat);
482 }
483
484 LIBINPUT_EXPORT void
485 libinput_seat_unref(struct libinput_seat *seat)
486 {
487         assert(seat->refcount > 0);
488         seat->refcount--;
489         if (seat->refcount == 0)
490                 libinput_seat_destroy(seat);
491 }
492
493 LIBINPUT_EXPORT void
494 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
495 {
496         seat->user_data = user_data;
497 }
498
499 LIBINPUT_EXPORT void *
500 libinput_seat_get_user_data(struct libinput_seat *seat)
501 {
502         return seat->user_data;
503 }
504
505 LIBINPUT_EXPORT const char *
506 libinput_seat_get_physical_name(struct libinput_seat *seat)
507 {
508         return seat->physical_name;
509 }
510
511 LIBINPUT_EXPORT const char *
512 libinput_seat_get_logical_name(struct libinput_seat *seat)
513 {
514         return seat->logical_name;
515 }
516
517 void
518 libinput_device_init(struct libinput_device *device,
519                      struct libinput_seat *seat)
520 {
521         device->seat = seat;
522         device->refcount = 1;
523 }
524
525 LIBINPUT_EXPORT void
526 libinput_device_ref(struct libinput_device *device)
527 {
528         device->refcount++;
529 }
530
531 static void
532 libinput_device_destroy(struct libinput_device *device)
533 {
534         evdev_device_destroy((struct evdev_device *) device);
535 }
536
537 LIBINPUT_EXPORT void
538 libinput_device_unref(struct libinput_device *device)
539 {
540         assert(device->refcount > 0);
541         device->refcount--;
542         if (device->refcount == 0)
543                 libinput_device_destroy(device);
544 }
545
546 LIBINPUT_EXPORT int
547 libinput_get_fd(struct libinput *libinput)
548 {
549         return libinput->epoll_fd;
550 }
551
552 LIBINPUT_EXPORT int
553 libinput_dispatch(struct libinput *libinput)
554 {
555         struct libinput_source *source;
556         struct epoll_event ep[32];
557         int i, count;
558
559         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
560         if (count < 0)
561                 return -errno;
562
563         for (i = 0; i < count; ++i) {
564                 source = ep[i].data.ptr;
565                 if (source->fd == -1)
566                         continue;
567
568                 source->dispatch(source->user_data);
569         }
570
571         libinput_drop_destroyed_sources(libinput);
572
573         return 0;
574 }
575
576 static void
577 init_event_base(struct libinput_event *event,
578                 struct libinput_device *device,
579                 enum libinput_event_type type)
580 {
581         event->type = type;
582         event->device = device;
583 }
584
585 static void
586 post_base_event(struct libinput_device *device,
587                 enum libinput_event_type type,
588                 struct libinput_event *event)
589 {
590         struct libinput *libinput = device->seat->libinput;
591         init_event_base(event, device, type);
592         libinput_post_event(libinput, event);
593 }
594
595 static void
596 post_device_event(struct libinput_device *device,
597                   enum libinput_event_type type,
598                   struct libinput_event *event)
599 {
600         init_event_base(event, device, type);
601         libinput_post_event(device->seat->libinput, event);
602 }
603
604 void
605 notify_added_device(struct libinput_device *device)
606 {
607         struct libinput_event_device_notify *added_device_event;
608
609         added_device_event = zalloc(sizeof *added_device_event);
610         if (!added_device_event)
611                 return;
612
613         post_base_event(device,
614                         LIBINPUT_EVENT_DEVICE_ADDED,
615                         &added_device_event->base);
616 }
617
618 void
619 notify_removed_device(struct libinput_device *device)
620 {
621         struct libinput_event_device_notify *removed_device_event;
622
623         removed_device_event = zalloc(sizeof *removed_device_event);
624         if (!removed_device_event)
625                 return;
626
627         post_base_event(device,
628                         LIBINPUT_EVENT_DEVICE_REMOVED,
629                         &removed_device_event->base);
630 }
631
632 void
633 keyboard_notify_key(struct libinput_device *device,
634                     uint32_t time,
635                     uint32_t key,
636                     enum libinput_keyboard_key_state state)
637 {
638         struct libinput_event_keyboard *key_event;
639
640         key_event = zalloc(sizeof *key_event);
641         if (!key_event)
642                 return;
643
644         *key_event = (struct libinput_event_keyboard) {
645                 .time = time,
646                 .key = key,
647                 .state = state,
648         };
649
650         post_device_event(device,
651                           LIBINPUT_EVENT_KEYBOARD_KEY,
652                           &key_event->base);
653 }
654
655 void
656 pointer_notify_motion(struct libinput_device *device,
657                       uint32_t time,
658                       li_fixed_t dx,
659                       li_fixed_t dy)
660 {
661         struct libinput_event_pointer *motion_event;
662
663         motion_event = zalloc(sizeof *motion_event);
664         if (!motion_event)
665                 return;
666
667         *motion_event = (struct libinput_event_pointer) {
668                 .time = time,
669                 .x = dx,
670                 .y = dy,
671         };
672
673         post_device_event(device,
674                           LIBINPUT_EVENT_POINTER_MOTION,
675                           &motion_event->base);
676 }
677
678 void
679 pointer_notify_motion_absolute(struct libinput_device *device,
680                                uint32_t time,
681                                li_fixed_t x,
682                                li_fixed_t y)
683 {
684         struct libinput_event_pointer *motion_absolute_event;
685
686         motion_absolute_event = zalloc(sizeof *motion_absolute_event);
687         if (!motion_absolute_event)
688                 return;
689
690         *motion_absolute_event = (struct libinput_event_pointer) {
691                 .time = time,
692                 .x = x,
693                 .y = y,
694         };
695
696         post_device_event(device,
697                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
698                           &motion_absolute_event->base);
699 }
700
701 void
702 pointer_notify_button(struct libinput_device *device,
703                       uint32_t time,
704                       int32_t button,
705                       enum libinput_pointer_button_state state)
706 {
707         struct libinput_event_pointer *button_event;
708
709         button_event = zalloc(sizeof *button_event);
710         if (!button_event)
711                 return;
712
713         *button_event = (struct libinput_event_pointer) {
714                 .time = time,
715                 .button = button,
716                 .state = state,
717         };
718
719         post_device_event(device,
720                           LIBINPUT_EVENT_POINTER_BUTTON,
721                           &button_event->base);
722 }
723
724 void
725 pointer_notify_axis(struct libinput_device *device,
726                     uint32_t time,
727                     enum libinput_pointer_axis axis,
728                     li_fixed_t value)
729 {
730         struct libinput_event_pointer *axis_event;
731
732         axis_event = zalloc(sizeof *axis_event);
733         if (!axis_event)
734                 return;
735
736         *axis_event = (struct libinput_event_pointer) {
737                 .time = time,
738                 .axis = axis,
739                 .value = value,
740         };
741
742         post_device_event(device,
743                           LIBINPUT_EVENT_POINTER_AXIS,
744                           &axis_event->base);
745 }
746
747 void
748 touch_notify_touch(struct libinput_device *device,
749                    uint32_t time,
750                    int32_t slot,
751                    li_fixed_t x,
752                    li_fixed_t y,
753                    enum libinput_touch_type touch_type)
754 {
755         struct libinput_event_touch *touch_event;
756
757         touch_event = zalloc(sizeof *touch_event);
758         if (!touch_event)
759                 return;
760
761         *touch_event = (struct libinput_event_touch) {
762                 .time = time,
763                 .slot = slot,
764                 .x = x,
765                 .y = y,
766                 .touch_type = touch_type,
767         };
768
769         post_device_event(device,
770                           LIBINPUT_EVENT_TOUCH_TOUCH,
771                           &touch_event->base);
772 }
773
774 void
775 touch_notify_frame(struct libinput_device *device,
776                    uint32_t time)
777 {
778         struct libinput_event_touch *touch_event;
779
780         touch_event = zalloc(sizeof *touch_event);
781         if (!touch_event)
782                 return;
783
784         *touch_event = (struct libinput_event_touch) {
785                 .time = time,
786         };
787
788         post_device_event(device,
789                           LIBINPUT_EVENT_TOUCH_FRAME,
790                           &touch_event->base);
791 }
792
793
794 static void
795 libinput_post_event(struct libinput *libinput,
796                     struct libinput_event *event)
797 {
798         struct libinput_event **events = libinput->events;
799         size_t events_len = libinput->events_len;
800         size_t events_count = libinput->events_count;
801         size_t move_len;
802         size_t new_out;
803
804         events_count++;
805         if (events_count > events_len) {
806                 events_len *= 2;
807                 events = realloc(events, events_len * sizeof *events);
808                 if (!events) {
809                         fprintf(stderr, "Failed to reallocate event ring "
810                                 "buffer");
811                         return;
812                 }
813
814                 if (libinput->events_count > 0 && libinput->events_in == 0) {
815                         libinput->events_in = libinput->events_len;
816                 } else if (libinput->events_count > 0 &&
817                            libinput->events_out >= libinput->events_in) {
818                         move_len = libinput->events_len - libinput->events_out;
819                         new_out = events_len - move_len;
820                         memmove(events + new_out,
821                                 events + libinput->events_out,
822                                 move_len * sizeof *events);
823                         libinput->events_out = new_out;
824                 }
825
826                 libinput->events = events;
827                 libinput->events_len = events_len;
828         }
829
830         if (event->device)
831                 libinput_device_ref(event->device);
832
833         libinput->events_count = events_count;
834         events[libinput->events_in] = event;
835         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
836 }
837
838 LIBINPUT_EXPORT struct libinput_event *
839 libinput_get_event(struct libinput *libinput)
840 {
841         struct libinput_event *event;
842
843         if (libinput->events_count == 0)
844                 return NULL;
845
846         event = libinput->events[libinput->events_out];
847         libinput->events_out =
848                 (libinput->events_out + 1) % libinput->events_len;
849         libinput->events_count--;
850
851         return event;
852 }
853
854 LIBINPUT_EXPORT enum libinput_event_type
855 libinput_next_event_type(struct libinput *libinput)
856 {
857         struct libinput_event *event;
858
859         if (libinput->events_count == 0)
860                 return LIBINPUT_EVENT_NONE;
861
862         event = libinput->events[libinput->events_out];
863         return event->type;
864 }
865
866 LIBINPUT_EXPORT void *
867 libinput_get_user_data(struct libinput *libinput)
868 {
869         return libinput->user_data;
870 }
871
872 LIBINPUT_EXPORT int
873 libinput_resume(struct libinput *libinput)
874 {
875         return libinput->interface_backend->resume(libinput);
876 }
877
878 LIBINPUT_EXPORT void
879 libinput_suspend(struct libinput *libinput)
880 {
881         libinput->interface_backend->suspend(libinput);
882 }
883
884 LIBINPUT_EXPORT void
885 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
886 {
887         device->user_data = user_data;
888 }
889
890 LIBINPUT_EXPORT void *
891 libinput_device_get_user_data(struct libinput_device *device)
892 {
893         return device->user_data;
894 }
895
896 LIBINPUT_EXPORT const char *
897 libinput_device_get_sysname(struct libinput_device *device)
898 {
899         return evdev_device_get_sysname((struct evdev_device *) device);
900 }
901
902 LIBINPUT_EXPORT const char *
903 libinput_device_get_output_name(struct libinput_device *device)
904 {
905         return evdev_device_get_output((struct evdev_device *) device);
906 }
907
908 LIBINPUT_EXPORT struct libinput_seat *
909 libinput_device_get_seat(struct libinput_device *device)
910 {
911         return device->seat;
912 }
913
914 LIBINPUT_EXPORT void
915 libinput_device_led_update(struct libinput_device *device,
916                            enum libinput_led leds)
917 {
918         evdev_device_led_update((struct evdev_device *) device, leds);
919 }
920
921 LIBINPUT_EXPORT int
922 libinput_device_get_keys(struct libinput_device *device,
923                          char *keys, size_t size)
924 {
925         return evdev_device_get_keys((struct evdev_device *) device,
926                                      keys,
927                                      size);
928 }
929
930 LIBINPUT_EXPORT void
931 libinput_device_calibrate(struct libinput_device *device,
932                           float calibration[6])
933 {
934         evdev_device_calibrate((struct evdev_device *) device, calibration);
935 }
936
937 LIBINPUT_EXPORT int
938 libinput_device_has_capability(struct libinput_device *device,
939                                enum libinput_device_capability capability)
940 {
941         return evdev_device_has_capability((struct evdev_device *) device,
942                                            capability);
943 }