Drop seat events
[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 *libinput;
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->libinput;
131 }
132
133 LIBINPUT_EXPORT struct libinput_device *
134 libinput_event_added_device_get_device(
135         struct libinput_event_added_device *event)
136 {
137         return event->device;
138 }
139
140 LIBINPUT_EXPORT struct libinput_device *
141 libinput_event_removed_device_get_device(
142         struct libinput_event_removed_device *event)
143 {
144         return event->device;
145 }
146
147 LIBINPUT_EXPORT uint32_t
148 libinput_event_keyboard_key_get_time(
149         struct libinput_event_keyboard_key *event)
150 {
151         return event->time;
152 }
153
154 LIBINPUT_EXPORT uint32_t
155 libinput_event_keyboard_key_get_key(
156         struct libinput_event_keyboard_key *event)
157 {
158         return event->key;
159 }
160
161 LIBINPUT_EXPORT enum libinput_keyboard_key_state
162 libinput_event_keyboard_key_get_state(
163         struct libinput_event_keyboard_key *event)
164 {
165         return event->state;
166 }
167
168 LIBINPUT_EXPORT uint32_t
169 libinput_event_pointer_motion_get_time(
170         struct libinput_event_pointer_motion *event)
171 {
172         return event->time;
173 }
174
175 LIBINPUT_EXPORT li_fixed_t
176 libinput_event_pointer_motion_get_dx(
177         struct libinput_event_pointer_motion *event)
178 {
179         return event->dx;
180 }
181
182 LIBINPUT_EXPORT li_fixed_t
183 libinput_event_pointer_motion_get_dy(
184         struct libinput_event_pointer_motion *event)
185 {
186         return event->dy;
187 }
188
189 LIBINPUT_EXPORT uint32_t
190 libinput_event_pointer_motion_absolute_get_time(
191         struct libinput_event_pointer_motion_absolute *event)
192 {
193         return event->time;
194 }
195
196 LIBINPUT_EXPORT li_fixed_t
197 libinput_event_pointer_motion_absolute_get_x(
198         struct libinput_event_pointer_motion_absolute *event)
199 {
200         return event->x;
201 }
202
203 LIBINPUT_EXPORT li_fixed_t
204 libinput_event_pointer_motion_absolute_get_y(
205         struct libinput_event_pointer_motion_absolute *event)
206 {
207         return event->y;
208 }
209
210 LIBINPUT_EXPORT uint32_t
211 libinput_event_pointer_button_get_time(
212         struct libinput_event_pointer_button *event)
213 {
214         return event->time;
215 }
216
217 LIBINPUT_EXPORT uint32_t
218 libinput_event_pointer_button_get_button(
219         struct libinput_event_pointer_button *event)
220 {
221         return event->button;
222 }
223
224 LIBINPUT_EXPORT enum libinput_pointer_button_state
225 libinput_event_pointer_button_get_state(
226         struct libinput_event_pointer_button *event)
227 {
228         return event->state;
229 }
230
231 LIBINPUT_EXPORT uint32_t
232 libinput_event_pointer_axis_get_time(
233         struct libinput_event_pointer_axis *event)
234 {
235         return event->time;
236 }
237
238 LIBINPUT_EXPORT enum libinput_pointer_axis
239 libinput_event_pointer_axis_get_axis(
240         struct libinput_event_pointer_axis *event)
241 {
242         return event->axis;
243 }
244
245 LIBINPUT_EXPORT li_fixed_t
246 libinput_event_pointer_axis_get_value(
247         struct libinput_event_pointer_axis *event)
248 {
249         return event->value;
250 }
251
252 LIBINPUT_EXPORT uint32_t
253 libinput_event_touch_touch_get_time(
254         struct libinput_event_touch_touch *event)
255 {
256         return event->time;
257 }
258
259 LIBINPUT_EXPORT uint32_t
260 libinput_event_touch_touch_get_slot(
261         struct libinput_event_touch_touch *event)
262 {
263         return event->slot;
264 }
265
266 LIBINPUT_EXPORT li_fixed_t
267 libinput_event_touch_touch_get_x(
268         struct libinput_event_touch_touch *event)
269 {
270         return event->x;
271 }
272
273 LIBINPUT_EXPORT li_fixed_t
274 libinput_event_touch_touch_get_y(
275         struct libinput_event_touch_touch *event)
276 {
277         return event->y;
278 }
279
280 LIBINPUT_EXPORT enum libinput_touch_type
281 libinput_event_touch_touch_get_touch_type(
282         struct libinput_event_touch_touch *event)
283 {
284         return event->touch_type;
285 }
286
287 struct libinput_source *
288 libinput_add_fd(struct libinput *libinput,
289                 int fd,
290                 libinput_source_dispatch_t dispatch,
291                 void *user_data)
292 {
293         struct libinput_source *source;
294         struct epoll_event ep;
295
296         source = malloc(sizeof *source);
297         if (!source)
298                 return NULL;
299
300         source->dispatch = dispatch;
301         source->user_data = user_data;
302         source->fd = fd;
303
304         memset(&ep, 0, sizeof ep);
305         ep.events = EPOLLIN;
306         ep.data.ptr = source;
307
308         if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
309                 close(source->fd);
310                 free(source);
311                 return NULL;
312         }
313
314         return source;
315 }
316
317 void
318 libinput_remove_source(struct libinput *libinput,
319                        struct libinput_source *source)
320 {
321         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
322         close(source->fd);
323         source->fd = -1;
324         list_insert(&libinput->source_destroy_list, &source->link);
325 }
326
327 int
328 libinput_init(struct libinput *libinput,
329               const struct libinput_interface *interface,
330               const struct libinput_interface_backend *interface_backend,
331               void *user_data)
332 {
333         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
334         if (libinput->epoll_fd < 0)
335                 return -1;
336
337         libinput->events_len = 4;
338         libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
339         if (!libinput->events) {
340                 close(libinput->epoll_fd);
341                 return -1;
342         }
343
344         libinput->interface = interface;
345         libinput->interface_backend = interface_backend;
346         libinput->user_data = user_data;
347         list_init(&libinput->source_destroy_list);
348         list_init(&libinput->seat_list);
349
350         return 0;
351 }
352
353 static void
354 libinput_device_destroy(struct libinput_device *device);
355
356 static void
357 libinput_seat_destroy(struct libinput_seat *seat);
358
359 static void
360 libinput_drop_destroyed_sources(struct libinput *libinput)
361 {
362         struct libinput_source *source, *next;
363
364         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
365                 free(source);
366         list_init(&libinput->source_destroy_list);
367 }
368
369 LIBINPUT_EXPORT void
370 libinput_destroy(struct libinput *libinput)
371 {
372         struct libinput_event *event;
373         struct libinput_device *device, *next_device;
374         struct libinput_seat *seat, *next_seat;
375
376         if (libinput == NULL)
377                 return;
378
379         libinput_suspend(libinput);
380
381         libinput->interface_backend->destroy(libinput);
382
383         while ((event = libinput_get_event(libinput)))
384                libinput_event_destroy(event);
385
386         libinput_drop_destroyed_sources(libinput);
387
388         free(libinput->events);
389
390         list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
391                 list_for_each_safe(device, next_device,
392                                    &seat->devices_list,
393                                    link)
394                         libinput_device_destroy(device);
395
396                 libinput_seat_destroy(seat);
397         }
398
399         close(libinput->epoll_fd);
400         free(libinput);
401 }
402
403 static enum libinput_event_class
404 libinput_event_get_class(struct libinput_event *event)
405 {
406         switch (event->type) {
407         case LIBINPUT_EVENT_NONE:
408                 return LIBINPUT_EVENT_CLASS_NONE;
409
410         case LIBINPUT_EVENT_ADDED_DEVICE:
411         case LIBINPUT_EVENT_REMOVED_DEVICE:
412                 return LIBINPUT_EVENT_CLASS_BASE;
413
414         case LIBINPUT_EVENT_KEYBOARD_KEY:
415         case LIBINPUT_EVENT_POINTER_MOTION:
416         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
417         case LIBINPUT_EVENT_POINTER_BUTTON:
418         case LIBINPUT_EVENT_POINTER_AXIS:
419         case LIBINPUT_EVENT_TOUCH_TOUCH:
420                 return LIBINPUT_EVENT_CLASS_DEVICE;
421         }
422
423         /* We should never end up here. */
424         abort();
425 }
426
427 LIBINPUT_EXPORT void
428 libinput_event_destroy(struct libinput_event *event)
429 {
430         if (event == NULL)
431                 return;
432
433         switch (libinput_event_get_class(event)) {
434         case LIBINPUT_EVENT_CLASS_NONE:
435         case LIBINPUT_EVENT_CLASS_BASE:
436                 break;
437         case LIBINPUT_EVENT_CLASS_SEAT:
438                 libinput_seat_unref(event->target.seat);
439                 break;
440         case LIBINPUT_EVENT_CLASS_DEVICE:
441                 libinput_device_unref(event->target.device);
442                 break;
443         }
444
445         free(event);
446 }
447
448 int
449 open_restricted(struct libinput *libinput,
450                 const char *path, int flags)
451 {
452         return libinput->interface->open_restricted(path,
453                                                     flags,
454                                                     libinput->user_data);
455 }
456
457 void
458 close_restricted(struct libinput *libinput, int fd)
459 {
460         return libinput->interface->close_restricted(fd, libinput->user_data);
461 }
462
463 void
464 libinput_seat_init(struct libinput_seat *seat,
465                    struct libinput *libinput,
466                    const char *name,
467                    libinput_seat_destroy_func destroy)
468 {
469         seat->refcount = 1;
470         seat->libinput = libinput;
471         seat->name = strdup(name);
472         seat->destroy = destroy;
473         list_init(&seat->devices_list);
474 }
475
476 LIBINPUT_EXPORT void
477 libinput_seat_ref(struct libinput_seat *seat)
478 {
479         seat->refcount++;
480 }
481
482 static void
483 libinput_seat_destroy(struct libinput_seat *seat)
484 {
485         list_remove(&seat->link);
486         free(seat->name);
487         seat->destroy(seat);
488 }
489
490 LIBINPUT_EXPORT void
491 libinput_seat_unref(struct libinput_seat *seat)
492 {
493         assert(seat->refcount > 0);
494         seat->refcount--;
495         if (seat->refcount == 0)
496                 libinput_seat_destroy(seat);
497 }
498
499 LIBINPUT_EXPORT void
500 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
501 {
502         seat->user_data = user_data;
503 }
504
505 LIBINPUT_EXPORT void *
506 libinput_seat_get_user_data(struct libinput_seat *seat)
507 {
508         return seat->user_data;
509 }
510
511 LIBINPUT_EXPORT const char *
512 libinput_seat_get_name(struct libinput_seat *seat)
513 {
514         return seat->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 *libinput,
579                 enum libinput_event_type type,
580                 union libinput_event_target target)
581 {
582         event->type = type;
583         event->libinput = libinput;
584         event->target = target;
585 }
586
587 static void
588 post_base_event(struct libinput *libinput,
589                 enum libinput_event_type type,
590                 struct libinput_event *event)
591 {
592         init_event_base(event, libinput, type,
593                         (union libinput_event_target) { .libinput = libinput });
594         libinput_post_event(libinput, event);
595 }
596
597 static void
598 post_device_event(struct libinput_device *device,
599                   enum libinput_event_type type,
600                   struct libinput_event *event)
601 {
602         init_event_base(event, device->seat->libinput, type,
603                         (union libinput_event_target) { .device = device });
604         libinput_post_event(device->seat->libinput, event);
605 }
606
607 void
608 notify_added_device(struct libinput_device *device)
609 {
610         struct libinput_event_added_device *added_device_event;
611
612         added_device_event = malloc(sizeof *added_device_event);
613         if (!added_device_event)
614                 return;
615
616         *added_device_event = (struct libinput_event_added_device) {
617                 .device = device,
618         };
619
620         post_base_event(device->seat->libinput,
621                         LIBINPUT_EVENT_ADDED_DEVICE,
622                         &added_device_event->base);
623 }
624
625 void
626 notify_removed_device(struct libinput_device *device)
627 {
628         struct libinput_event_removed_device *removed_device_event;
629
630         removed_device_event = malloc(sizeof *removed_device_event);
631         if (!removed_device_event)
632                 return;
633
634         *removed_device_event = (struct libinput_event_removed_device) {
635                 .device = device,
636         };
637
638         post_base_event(device->seat->libinput,
639                         LIBINPUT_EVENT_REMOVED_DEVICE,
640                         &removed_device_event->base);
641 }
642
643 void
644 keyboard_notify_key(struct libinput_device *device,
645                     uint32_t time,
646                     uint32_t key,
647                     enum libinput_keyboard_key_state state)
648 {
649         struct libinput_event_keyboard_key *key_event;
650
651         key_event = malloc(sizeof *key_event);
652         if (!key_event)
653                 return;
654
655         *key_event = (struct libinput_event_keyboard_key) {
656                 .time = time,
657                 .key = key,
658                 .state = state,
659         };
660
661         post_device_event(device,
662                           LIBINPUT_EVENT_KEYBOARD_KEY,
663                           &key_event->base);
664 }
665
666 void
667 pointer_notify_motion(struct libinput_device *device,
668                       uint32_t time,
669                       li_fixed_t dx,
670                       li_fixed_t dy)
671 {
672         struct libinput_event_pointer_motion *motion_event;
673
674         motion_event = malloc(sizeof *motion_event);
675         if (!motion_event)
676                 return;
677
678         *motion_event = (struct libinput_event_pointer_motion) {
679                 .time = time,
680                 .dx = dx,
681                 .dy = dy,
682         };
683
684         post_device_event(device,
685                           LIBINPUT_EVENT_POINTER_MOTION,
686                           &motion_event->base);
687 }
688
689 void
690 pointer_notify_motion_absolute(struct libinput_device *device,
691                                uint32_t time,
692                                li_fixed_t x,
693                                li_fixed_t y)
694 {
695         struct libinput_event_pointer_motion_absolute *motion_absolute_event;
696
697         motion_absolute_event = malloc(sizeof *motion_absolute_event);
698         if (!motion_absolute_event)
699                 return;
700
701         *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
702                 .time = time,
703                 .x = x,
704                 .y = y,
705         };
706
707         post_device_event(device,
708                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
709                           &motion_absolute_event->base);
710 }
711
712 void
713 pointer_notify_button(struct libinput_device *device,
714                       uint32_t time,
715                       int32_t button,
716                       enum libinput_pointer_button_state state)
717 {
718         struct libinput_event_pointer_button *button_event;
719
720         button_event = malloc(sizeof *button_event);
721         if (!button_event)
722                 return;
723
724         *button_event = (struct libinput_event_pointer_button) {
725                 .time = time,
726                 .button = button,
727                 .state = state,
728         };
729
730         post_device_event(device,
731                           LIBINPUT_EVENT_POINTER_BUTTON,
732                           &button_event->base);
733 }
734
735 void
736 pointer_notify_axis(struct libinput_device *device,
737                     uint32_t time,
738                     enum libinput_pointer_axis axis,
739                     li_fixed_t value)
740 {
741         struct libinput_event_pointer_axis *axis_event;
742
743         axis_event = malloc(sizeof *axis_event);
744         if (!axis_event)
745                 return;
746
747         *axis_event = (struct libinput_event_pointer_axis) {
748                 .time = time,
749                 .axis = axis,
750                 .value = value,
751         };
752
753         post_device_event(device,
754                           LIBINPUT_EVENT_POINTER_AXIS,
755                           &axis_event->base);
756 }
757
758 void
759 touch_notify_touch(struct libinput_device *device,
760                    uint32_t time,
761                    int32_t slot,
762                    li_fixed_t x,
763                    li_fixed_t y,
764                    enum libinput_touch_type touch_type)
765 {
766         struct libinput_event_touch_touch *touch_event;
767
768         touch_event = malloc(sizeof *touch_event);
769         if (!touch_event)
770                 return;
771
772         *touch_event = (struct libinput_event_touch_touch) {
773                 .time = time,
774                 .slot = slot,
775                 .x = x,
776                 .y = y,
777                 .touch_type = touch_type,
778         };
779
780         post_device_event(device,
781                           LIBINPUT_EVENT_TOUCH_TOUCH,
782                           &touch_event->base);
783 }
784
785 static void
786 libinput_post_event(struct libinput *libinput,
787                     struct libinput_event *event)
788 {
789         struct libinput_event **events = libinput->events;
790         size_t events_len = libinput->events_len;
791         size_t events_count = libinput->events_count;
792         size_t move_len;
793         size_t new_out;
794
795         events_count++;
796         if (events_count > events_len) {
797                 events_len *= 2;
798                 events = realloc(events, events_len * sizeof *events);
799                 if (!events) {
800                         fprintf(stderr, "Failed to reallocate event ring "
801                                 "buffer");
802                         return;
803                 }
804
805                 if (libinput->events_count > 0 && libinput->events_in == 0) {
806                         libinput->events_in = libinput->events_len;
807                 } else if (libinput->events_count > 0 &&
808                            libinput->events_out >= libinput->events_in) {
809                         move_len = libinput->events_len - libinput->events_out;
810                         new_out = events_len - move_len;
811                         memmove(events + new_out,
812                                 events + libinput->events_out,
813                                 move_len * sizeof *events);
814                         libinput->events_out = new_out;
815                 }
816
817                 libinput->events = events;
818                 libinput->events_len = events_len;
819         }
820
821         switch (libinput_event_get_class(event)) {
822         case LIBINPUT_EVENT_CLASS_NONE:
823         case LIBINPUT_EVENT_CLASS_BASE:
824                 break;
825         case LIBINPUT_EVENT_CLASS_SEAT:
826                 libinput_seat_ref(event->target.seat);
827                 break;
828         case LIBINPUT_EVENT_CLASS_DEVICE:
829                 libinput_device_ref(event->target.device);
830                 break;
831         }
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 }