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