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