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