Simplify device reference counting of 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 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         struct libinput_device *device;
52 };
53
54 struct libinput_event_keyboard {
55         struct libinput_event base;
56         uint32_t time;
57         uint32_t key;
58         enum libinput_keyboard_key_state state;
59 };
60
61 struct libinput_event_pointer {
62         struct libinput_event base;
63         uint32_t time;
64         li_fixed_t x;
65         li_fixed_t y;
66         uint32_t button;
67         enum libinput_pointer_button_state state;
68         enum libinput_pointer_axis axis;
69         li_fixed_t value;
70 };
71
72 struct libinput_event_touch {
73         struct libinput_event base;
74         uint32_t time;
75         uint32_t slot;
76         li_fixed_t x;
77         li_fixed_t y;
78         enum libinput_touch_type touch_type;
79 };
80
81 static void
82 libinput_post_event(struct libinput *libinput,
83                     struct libinput_event *event);
84
85 LIBINPUT_EXPORT enum libinput_event_type
86 libinput_event_get_type(struct libinput_event *event)
87 {
88         return event->type;
89 }
90
91 LIBINPUT_EXPORT struct libinput*
92 libinput_event_get_context(struct libinput_event *event)
93 {
94         return event->device->seat->libinput;
95 }
96
97 LIBINPUT_EXPORT struct libinput_device*
98 libinput_event_get_device(struct libinput_event *event)
99 {
100         return event->device;
101 }
102
103 LIBINPUT_EXPORT struct libinput_event_pointer*
104 libinput_event_get_pointer_event(struct libinput_event *event)
105 {
106         switch (event->type) {
107         case LIBINPUT_EVENT_NONE:
108                 abort(); /* not used as actual event type */
109         case LIBINPUT_EVENT_DEVICE_ADDED:
110         case LIBINPUT_EVENT_DEVICE_REMOVED:
111         case LIBINPUT_EVENT_KEYBOARD_KEY:
112                 break;
113         case LIBINPUT_EVENT_POINTER_MOTION:
114         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
115         case LIBINPUT_EVENT_POINTER_BUTTON:
116         case LIBINPUT_EVENT_POINTER_AXIS:
117                 return (struct libinput_event_pointer*)event;
118         case LIBINPUT_EVENT_TOUCH_TOUCH:
119         case LIBINPUT_EVENT_TOUCH_FRAME:
120                 break;
121         }
122
123         return NULL;
124 }
125
126 LIBINPUT_EXPORT struct libinput_event_keyboard*
127 libinput_event_get_keyboard_event(struct libinput_event *event)
128 {
129         switch (event->type) {
130         case LIBINPUT_EVENT_NONE:
131                 abort(); /* not used as actual event type */
132         case LIBINPUT_EVENT_DEVICE_ADDED:
133         case LIBINPUT_EVENT_DEVICE_REMOVED:
134                 break;
135         case LIBINPUT_EVENT_KEYBOARD_KEY:
136                 return (struct libinput_event_keyboard*)event;
137         case LIBINPUT_EVENT_POINTER_MOTION:
138         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
139         case LIBINPUT_EVENT_POINTER_BUTTON:
140         case LIBINPUT_EVENT_POINTER_AXIS:
141         case LIBINPUT_EVENT_TOUCH_TOUCH:
142         case LIBINPUT_EVENT_TOUCH_FRAME:
143                 break;
144         }
145
146         return NULL;
147 }
148
149 LIBINPUT_EXPORT struct libinput_event_touch*
150 libinput_event_get_touch_event(struct libinput_event *event)
151 {
152         switch (event->type) {
153         case LIBINPUT_EVENT_NONE:
154                 abort(); /* not used as actual event type */
155         case LIBINPUT_EVENT_DEVICE_ADDED:
156         case LIBINPUT_EVENT_DEVICE_REMOVED:
157         case LIBINPUT_EVENT_KEYBOARD_KEY:
158         case LIBINPUT_EVENT_POINTER_MOTION:
159         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
160         case LIBINPUT_EVENT_POINTER_BUTTON:
161         case LIBINPUT_EVENT_POINTER_AXIS:
162                 break;
163         case LIBINPUT_EVENT_TOUCH_TOUCH:
164         case LIBINPUT_EVENT_TOUCH_FRAME:
165                 return (struct libinput_event_touch*)event;
166         }
167
168         return NULL;
169 }
170
171 LIBINPUT_EXPORT struct libinput_event_device_notify*
172 libinput_event_get_device_notify_event(struct libinput_event *event)
173 {
174         switch (event->type) {
175         case LIBINPUT_EVENT_NONE:
176                 abort(); /* not used as actual event type */
177         case LIBINPUT_EVENT_DEVICE_ADDED:
178         case LIBINPUT_EVENT_DEVICE_REMOVED:
179                 return (struct libinput_event_device_notify*)event;
180         case LIBINPUT_EVENT_KEYBOARD_KEY:
181         case LIBINPUT_EVENT_POINTER_MOTION:
182         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
183         case LIBINPUT_EVENT_POINTER_BUTTON:
184         case LIBINPUT_EVENT_POINTER_AXIS:
185         case LIBINPUT_EVENT_TOUCH_TOUCH:
186         case LIBINPUT_EVENT_TOUCH_FRAME:
187                 break;
188         }
189
190         return NULL;
191 }
192
193 LIBINPUT_EXPORT uint32_t
194 libinput_event_keyboard_get_time(
195         struct libinput_event_keyboard *event)
196 {
197         return event->time;
198 }
199
200 LIBINPUT_EXPORT uint32_t
201 libinput_event_keyboard_get_key(
202         struct libinput_event_keyboard *event)
203 {
204         return event->key;
205 }
206
207 LIBINPUT_EXPORT enum libinput_keyboard_key_state
208 libinput_event_keyboard_get_key_state(
209         struct libinput_event_keyboard *event)
210 {
211         return event->state;
212 }
213
214 LIBINPUT_EXPORT uint32_t
215 libinput_event_pointer_get_time(
216         struct libinput_event_pointer *event)
217 {
218         return event->time;
219 }
220
221 LIBINPUT_EXPORT li_fixed_t
222 libinput_event_pointer_get_dx(
223         struct libinput_event_pointer *event)
224 {
225         return event->x;
226 }
227
228 LIBINPUT_EXPORT li_fixed_t
229 libinput_event_pointer_get_dy(
230         struct libinput_event_pointer *event)
231 {
232         return event->y;
233 }
234
235 LIBINPUT_EXPORT li_fixed_t
236 libinput_event_pointer_get_absolute_x(
237         struct libinput_event_pointer *event)
238 {
239         return event->x;
240 }
241
242 LIBINPUT_EXPORT li_fixed_t
243 libinput_event_pointer_get_absolute_y(
244         struct libinput_event_pointer *event)
245 {
246         return event->y;
247 }
248
249 LIBINPUT_EXPORT uint32_t
250 libinput_event_pointer_get_button(
251         struct libinput_event_pointer *event)
252 {
253         return event->button;
254 }
255
256 LIBINPUT_EXPORT enum libinput_pointer_button_state
257 libinput_event_pointer_get_button_state(
258         struct libinput_event_pointer *event)
259 {
260         return event->state;
261 }
262
263 LIBINPUT_EXPORT enum libinput_pointer_axis
264 libinput_event_pointer_get_axis(
265         struct libinput_event_pointer *event)
266 {
267         return event->axis;
268 }
269
270 LIBINPUT_EXPORT li_fixed_t
271 libinput_event_pointer_get_axis_value(
272         struct libinput_event_pointer *event)
273 {
274         return event->value;
275 }
276
277 LIBINPUT_EXPORT uint32_t
278 libinput_event_touch_get_time(
279         struct libinput_event_touch *event)
280 {
281         return event->time;
282 }
283
284 LIBINPUT_EXPORT uint32_t
285 libinput_event_touch_get_slot(
286         struct libinput_event_touch *event)
287 {
288         return event->slot;
289 }
290
291 LIBINPUT_EXPORT li_fixed_t
292 libinput_event_touch_get_x(
293         struct libinput_event_touch *event)
294 {
295         return event->x;
296 }
297
298 LIBINPUT_EXPORT li_fixed_t
299 libinput_event_touch_get_y(
300         struct libinput_event_touch *event)
301 {
302         return event->y;
303 }
304
305 LIBINPUT_EXPORT enum libinput_touch_type
306 libinput_event_touch_get_touch_type(
307         struct libinput_event_touch *event)
308 {
309         return event->touch_type;
310 }
311
312 struct libinput_source *
313 libinput_add_fd(struct libinput *libinput,
314                 int fd,
315                 libinput_source_dispatch_t dispatch,
316                 void *user_data)
317 {
318         struct libinput_source *source;
319         struct epoll_event ep;
320
321         source = malloc(sizeof *source);
322         if (!source)
323                 return NULL;
324
325         source->dispatch = dispatch;
326         source->user_data = user_data;
327         source->fd = fd;
328
329         memset(&ep, 0, sizeof ep);
330         ep.events = EPOLLIN;
331         ep.data.ptr = source;
332
333         if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
334                 close(source->fd);
335                 free(source);
336                 return NULL;
337         }
338
339         return source;
340 }
341
342 void
343 libinput_remove_source(struct libinput *libinput,
344                        struct libinput_source *source)
345 {
346         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
347         close(source->fd);
348         source->fd = -1;
349         list_insert(&libinput->source_destroy_list, &source->link);
350 }
351
352 int
353 libinput_init(struct libinput *libinput,
354               const struct libinput_interface *interface,
355               const struct libinput_interface_backend *interface_backend,
356               void *user_data)
357 {
358         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
359         if (libinput->epoll_fd < 0)
360                 return -1;
361
362         libinput->events_len = 4;
363         libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
364         if (!libinput->events) {
365                 close(libinput->epoll_fd);
366                 return -1;
367         }
368
369         libinput->interface = interface;
370         libinput->interface_backend = interface_backend;
371         libinput->user_data = user_data;
372         list_init(&libinput->source_destroy_list);
373         list_init(&libinput->seat_list);
374
375         return 0;
376 }
377
378 static void
379 libinput_device_destroy(struct libinput_device *device);
380
381 static void
382 libinput_seat_destroy(struct libinput_seat *seat);
383
384 static void
385 libinput_drop_destroyed_sources(struct libinput *libinput)
386 {
387         struct libinput_source *source, *next;
388
389         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
390                 free(source);
391         list_init(&libinput->source_destroy_list);
392 }
393
394 LIBINPUT_EXPORT void
395 libinput_destroy(struct libinput *libinput)
396 {
397         struct libinput_event *event;
398         struct libinput_device *device, *next_device;
399         struct libinput_seat *seat, *next_seat;
400
401         if (libinput == NULL)
402                 return;
403
404         libinput_suspend(libinput);
405
406         libinput->interface_backend->destroy(libinput);
407
408         while ((event = libinput_get_event(libinput)))
409                libinput_event_destroy(event);
410
411         libinput_drop_destroyed_sources(libinput);
412
413         free(libinput->events);
414
415         list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
416                 list_for_each_safe(device, next_device,
417                                    &seat->devices_list,
418                                    link)
419                         libinput_device_destroy(device);
420
421                 libinput_seat_destroy(seat);
422         }
423
424         close(libinput->epoll_fd);
425         free(libinput);
426 }
427
428 LIBINPUT_EXPORT void
429 libinput_event_destroy(struct libinput_event *event)
430 {
431         if (event == NULL)
432                 return;
433
434         if (event->device)
435                 libinput_device_unref(event->device);
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 {
582         event->type = type;
583         event->device = device;
584 }
585
586 static void
587 post_base_event(struct libinput_device *device,
588                 enum libinput_event_type type,
589                 struct libinput_event *event)
590 {
591         struct libinput *libinput = device->seat->libinput;
592         init_event_base(event, device, type);
593         libinput_post_event(libinput, event);
594 }
595
596 static void
597 post_device_event(struct libinput_device *device,
598                   enum libinput_event_type type,
599                   struct libinput_event *event)
600 {
601         init_event_base(event, device, type);
602         libinput_post_event(device->seat->libinput, event);
603 }
604
605 void
606 notify_added_device(struct libinput_device *device)
607 {
608         struct libinput_event_device_notify *added_device_event;
609
610         added_device_event = zalloc(sizeof *added_device_event);
611         if (!added_device_event)
612                 return;
613
614         *added_device_event = (struct libinput_event_device_notify) {
615                 .device = device,
616         };
617
618         post_base_event(device,
619                         LIBINPUT_EVENT_DEVICE_ADDED,
620                         &added_device_event->base);
621 }
622
623 void
624 notify_removed_device(struct libinput_device *device)
625 {
626         struct libinput_event_device_notify *removed_device_event;
627
628         removed_device_event = zalloc(sizeof *removed_device_event);
629         if (!removed_device_event)
630                 return;
631
632         *removed_device_event = (struct libinput_event_device_notify) {
633                 .device = device,
634         };
635
636         post_base_event(device,
637                         LIBINPUT_EVENT_DEVICE_REMOVED,
638                         &removed_device_event->base);
639 }
640
641 void
642 keyboard_notify_key(struct libinput_device *device,
643                     uint32_t time,
644                     uint32_t key,
645                     enum libinput_keyboard_key_state state)
646 {
647         struct libinput_event_keyboard *key_event;
648
649         key_event = zalloc(sizeof *key_event);
650         if (!key_event)
651                 return;
652
653         *key_event = (struct libinput_event_keyboard) {
654                 .time = time,
655                 .key = key,
656                 .state = state,
657         };
658
659         post_device_event(device,
660                           LIBINPUT_EVENT_KEYBOARD_KEY,
661                           &key_event->base);
662 }
663
664 void
665 pointer_notify_motion(struct libinput_device *device,
666                       uint32_t time,
667                       li_fixed_t dx,
668                       li_fixed_t dy)
669 {
670         struct libinput_event_pointer *motion_event;
671
672         motion_event = zalloc(sizeof *motion_event);
673         if (!motion_event)
674                 return;
675
676         *motion_event = (struct libinput_event_pointer) {
677                 .time = time,
678                 .x = dx,
679                 .y = dy,
680         };
681
682         post_device_event(device,
683                           LIBINPUT_EVENT_POINTER_MOTION,
684                           &motion_event->base);
685 }
686
687 void
688 pointer_notify_motion_absolute(struct libinput_device *device,
689                                uint32_t time,
690                                li_fixed_t x,
691                                li_fixed_t y)
692 {
693         struct libinput_event_pointer *motion_absolute_event;
694
695         motion_absolute_event = zalloc(sizeof *motion_absolute_event);
696         if (!motion_absolute_event)
697                 return;
698
699         *motion_absolute_event = (struct libinput_event_pointer) {
700                 .time = time,
701                 .x = x,
702                 .y = y,
703         };
704
705         post_device_event(device,
706                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
707                           &motion_absolute_event->base);
708 }
709
710 void
711 pointer_notify_button(struct libinput_device *device,
712                       uint32_t time,
713                       int32_t button,
714                       enum libinput_pointer_button_state state)
715 {
716         struct libinput_event_pointer *button_event;
717
718         button_event = zalloc(sizeof *button_event);
719         if (!button_event)
720                 return;
721
722         *button_event = (struct libinput_event_pointer) {
723                 .time = time,
724                 .button = button,
725                 .state = state,
726         };
727
728         post_device_event(device,
729                           LIBINPUT_EVENT_POINTER_BUTTON,
730                           &button_event->base);
731 }
732
733 void
734 pointer_notify_axis(struct libinput_device *device,
735                     uint32_t time,
736                     enum libinput_pointer_axis axis,
737                     li_fixed_t value)
738 {
739         struct libinput_event_pointer *axis_event;
740
741         axis_event = zalloc(sizeof *axis_event);
742         if (!axis_event)
743                 return;
744
745         *axis_event = (struct libinput_event_pointer) {
746                 .time = time,
747                 .axis = axis,
748                 .value = value,
749         };
750
751         post_device_event(device,
752                           LIBINPUT_EVENT_POINTER_AXIS,
753                           &axis_event->base);
754 }
755
756 void
757 touch_notify_touch(struct libinput_device *device,
758                    uint32_t time,
759                    int32_t slot,
760                    li_fixed_t x,
761                    li_fixed_t y,
762                    enum libinput_touch_type touch_type)
763 {
764         struct libinput_event_touch *touch_event;
765
766         touch_event = zalloc(sizeof *touch_event);
767         if (!touch_event)
768                 return;
769
770         *touch_event = (struct libinput_event_touch) {
771                 .time = time,
772                 .slot = slot,
773                 .x = x,
774                 .y = y,
775                 .touch_type = touch_type,
776         };
777
778         post_device_event(device,
779                           LIBINPUT_EVENT_TOUCH_TOUCH,
780                           &touch_event->base);
781 }
782
783 void
784 touch_notify_frame(struct libinput_device *device,
785                    uint32_t time)
786 {
787         struct libinput_event_touch *touch_event;
788
789         touch_event = zalloc(sizeof *touch_event);
790         if (!touch_event)
791                 return;
792
793         *touch_event = (struct libinput_event_touch) {
794                 .time = time,
795         };
796
797         post_device_event(device,
798                           LIBINPUT_EVENT_TOUCH_FRAME,
799                           &touch_event->base);
800 }
801
802
803 static void
804 libinput_post_event(struct libinput *libinput,
805                     struct libinput_event *event)
806 {
807         struct libinput_event **events = libinput->events;
808         size_t events_len = libinput->events_len;
809         size_t events_count = libinput->events_count;
810         size_t move_len;
811         size_t new_out;
812
813         events_count++;
814         if (events_count > events_len) {
815                 events_len *= 2;
816                 events = realloc(events, events_len * sizeof *events);
817                 if (!events) {
818                         fprintf(stderr, "Failed to reallocate event ring "
819                                 "buffer");
820                         return;
821                 }
822
823                 if (libinput->events_count > 0 && libinput->events_in == 0) {
824                         libinput->events_in = libinput->events_len;
825                 } else if (libinput->events_count > 0 &&
826                            libinput->events_out >= libinput->events_in) {
827                         move_len = libinput->events_len - libinput->events_out;
828                         new_out = events_len - move_len;
829                         memmove(events + new_out,
830                                 events + libinput->events_out,
831                                 move_len * sizeof *events);
832                         libinput->events_out = new_out;
833                 }
834
835                 libinput->events = events;
836                 libinput->events_len = events_len;
837         }
838
839         if (event->device)
840                 libinput_device_ref(event->device);
841
842         libinput->events_count = events_count;
843         events[libinput->events_in] = event;
844         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
845 }
846
847 LIBINPUT_EXPORT struct libinput_event *
848 libinput_get_event(struct libinput *libinput)
849 {
850         struct libinput_event *event;
851
852         if (libinput->events_count == 0)
853                 return NULL;
854
855         event = libinput->events[libinput->events_out];
856         libinput->events_out =
857                 (libinput->events_out + 1) % libinput->events_len;
858         libinput->events_count--;
859
860         return event;
861 }
862
863 LIBINPUT_EXPORT enum libinput_event_type
864 libinput_next_event_type(struct libinput *libinput)
865 {
866         struct libinput_event *event;
867
868         if (libinput->events_count == 0)
869                 return LIBINPUT_EVENT_NONE;
870
871         event = libinput->events[libinput->events_out];
872         return event->type;
873 }
874
875 LIBINPUT_EXPORT void *
876 libinput_get_user_data(struct libinput *libinput)
877 {
878         return libinput->user_data;
879 }
880
881 LIBINPUT_EXPORT int
882 libinput_resume(struct libinput *libinput)
883 {
884         return libinput->interface_backend->resume(libinput);
885 }
886
887 LIBINPUT_EXPORT void
888 libinput_suspend(struct libinput *libinput)
889 {
890         libinput->interface_backend->suspend(libinput);
891 }
892
893 LIBINPUT_EXPORT void
894 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
895 {
896         device->user_data = user_data;
897 }
898
899 LIBINPUT_EXPORT void *
900 libinput_device_get_user_data(struct libinput_device *device)
901 {
902         return device->user_data;
903 }
904
905 LIBINPUT_EXPORT const char *
906 libinput_device_get_sysname(struct libinput_device *device)
907 {
908         return evdev_device_get_sysname((struct evdev_device *) device);
909 }
910
911 LIBINPUT_EXPORT const char *
912 libinput_device_get_output_name(struct libinput_device *device)
913 {
914         return evdev_device_get_output((struct evdev_device *) device);
915 }
916
917 LIBINPUT_EXPORT struct libinput_seat *
918 libinput_device_get_seat(struct libinput_device *device)
919 {
920         return device->seat;
921 }
922
923 LIBINPUT_EXPORT void
924 libinput_device_led_update(struct libinput_device *device,
925                            enum libinput_led leds)
926 {
927         evdev_device_led_update((struct evdev_device *) device, leds);
928 }
929
930 LIBINPUT_EXPORT int
931 libinput_device_get_keys(struct libinput_device *device,
932                          char *keys, size_t size)
933 {
934         return evdev_device_get_keys((struct evdev_device *) device,
935                                      keys,
936                                      size);
937 }
938
939 LIBINPUT_EXPORT void
940 libinput_device_calibrate(struct libinput_device *device,
941                           float calibration[6])
942 {
943         evdev_device_calibrate((struct evdev_device *) device, calibration);
944 }
945
946 LIBINPUT_EXPORT int
947 libinput_device_has_capability(struct libinput_device *device,
948                                enum libinput_device_capability capability)
949 {
950         return evdev_device_has_capability((struct evdev_device *) device,
951                                            capability);
952 }