Make even structs private
[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
388         while ((event = libinput_get_event(libinput)))
389                free(event);
390         free(libinput->events);
391
392         close(libinput->epoll_fd);
393         free(libinput);
394 }
395
396 static enum libinput_event_class
397 libinput_event_get_class(struct libinput_event *event)
398 {
399         switch (event->type) {
400         case LIBINPUT_EVENT_ADDED_SEAT:
401         case LIBINPUT_EVENT_REMOVED_SEAT:
402         case LIBINPUT_EVENT_ADDED_DEVICE:
403         case LIBINPUT_EVENT_REMOVED_DEVICE:
404                 return LIBINPUT_EVENT_CLASS_BASE;
405
406         case LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY:
407         case LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY:
408         case LIBINPUT_EVENT_KEYBOARD_KEY:
409         case LIBINPUT_EVENT_POINTER_MOTION:
410         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
411         case LIBINPUT_EVENT_POINTER_BUTTON:
412         case LIBINPUT_EVENT_POINTER_AXIS:
413         case LIBINPUT_EVENT_TOUCH_TOUCH:
414                 return LIBINPUT_EVENT_CLASS_DEVICE;
415         }
416
417         /* We should never end up here. */
418         abort();
419 }
420
421 LIBINPUT_EXPORT void
422 libinput_event_destroy(struct libinput_event *event)
423 {
424         switch (libinput_event_get_class(event)) {
425         case LIBINPUT_EVENT_CLASS_BASE:
426                 break;
427         case LIBINPUT_EVENT_CLASS_SEAT:
428                 libinput_seat_unref(event->target.seat);
429                 break;
430         case LIBINPUT_EVENT_CLASS_DEVICE:
431                 libinput_device_unref(event->target.device);
432                 break;
433         }
434
435         free(event);
436 }
437
438 int
439 open_restricted(struct libinput *libinput,
440                 const char *path, int flags)
441 {
442         return libinput->interface->open_restricted(path,
443                                                     flags,
444                                                     libinput->user_data);
445 }
446
447 void
448 close_restricted(struct libinput *libinput, int fd)
449 {
450         return libinput->interface->close_restricted(fd, libinput->user_data);
451 }
452
453 void
454 libinput_seat_init(struct libinput_seat *seat,
455                    struct libinput *libinput,
456                    const char *name)
457 {
458         seat->refcount = 1;
459         seat->libinput = libinput;
460         seat->name = strdup(name);
461         list_init(&seat->devices_list);
462 }
463
464 LIBINPUT_EXPORT void
465 libinput_seat_ref(struct libinput_seat *seat)
466 {
467         seat->refcount++;
468 }
469
470 LIBINPUT_EXPORT void
471 libinput_seat_unref(struct libinput_seat *seat)
472 {
473         seat->refcount--;
474         if (seat->refcount == 0) {
475                 free(seat->name);
476                 udev_seat_destroy((struct udev_seat *) seat);
477         }
478 }
479
480 LIBINPUT_EXPORT void
481 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
482 {
483         seat->user_data = user_data;
484 }
485
486 LIBINPUT_EXPORT void *
487 libinput_seat_get_user_data(struct libinput_seat *seat)
488 {
489         return seat->user_data;
490 }
491
492 LIBINPUT_EXPORT const char *
493 libinput_seat_get_name(struct libinput_seat *seat)
494 {
495         return seat->name;
496 }
497
498 void
499 libinput_device_init(struct libinput_device *device,
500                      struct libinput_seat *seat)
501 {
502         device->seat = seat;
503         device->refcount = 1;
504 }
505
506 LIBINPUT_EXPORT void
507 libinput_device_ref(struct libinput_device *device)
508 {
509         device->refcount++;
510 }
511
512 LIBINPUT_EXPORT void
513 libinput_device_unref(struct libinput_device *device)
514 {
515         device->refcount--;
516         if (device->refcount == 0)
517                 evdev_device_destroy((struct evdev_device *) device);
518 }
519
520 LIBINPUT_EXPORT int
521 libinput_get_fd(struct libinput *libinput)
522 {
523         return libinput->epoll_fd;
524 }
525
526 LIBINPUT_EXPORT int
527 libinput_dispatch(struct libinput *libinput)
528 {
529         struct libinput_source *source, *next;
530         struct epoll_event ep[32];
531         int i, count;
532
533         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
534         if (count < 0)
535                 return -errno;
536
537         for (i = 0; i < count; ++i) {
538                 source = ep[i].data.ptr;
539                 if (source->fd == -1)
540                         continue;
541
542                 source->dispatch(source->user_data);
543         }
544
545         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
546                 free(source);
547         list_init(&libinput->source_destroy_list);
548
549         return libinput->events_count > 0 ? 0 : -EAGAIN;
550 }
551
552 static void
553 init_event_base(struct libinput_event *event,
554                 enum libinput_event_type type,
555                 union libinput_event_target target)
556 {
557         event->type = type;
558         event->target = target;
559 }
560
561 static void
562 post_base_event(struct libinput *libinput,
563                 enum libinput_event_type type,
564                 struct libinput_event *event)
565 {
566         init_event_base(event, type,
567                         (union libinput_event_target) { .libinput = libinput });
568         libinput_post_event(libinput, event);
569 }
570
571 static void
572 post_device_event(struct libinput_device *device,
573                   enum libinput_event_type type,
574                   struct libinput_event *event)
575 {
576         init_event_base(event, type,
577                         (union libinput_event_target) { .device = device });
578         libinput_post_event(device->seat->libinput, event);
579 }
580
581 void
582 notify_added_seat(struct libinput_seat *seat)
583 {
584         struct libinput_event_added_seat *added_seat_event;
585
586         added_seat_event = malloc(sizeof *added_seat_event);
587         if (!added_seat_event)
588                 return;
589
590         *added_seat_event = (struct libinput_event_added_seat) {
591                 .seat = seat,
592         };
593
594         post_base_event(seat->libinput,
595                         LIBINPUT_EVENT_ADDED_SEAT,
596                         &added_seat_event->base);
597 }
598
599 void
600 notify_removed_seat(struct libinput_seat *seat)
601 {
602         struct libinput_event_removed_seat *removed_seat_event;
603
604         removed_seat_event = malloc(sizeof *removed_seat_event);
605         if (!removed_seat_event)
606                 return;
607
608         *removed_seat_event = (struct libinput_event_removed_seat) {
609                 .seat = seat,
610         };
611
612         post_base_event(seat->libinput,
613                         LIBINPUT_EVENT_REMOVED_SEAT,
614                         &removed_seat_event->base);
615 }
616
617 void
618 notify_added_device(struct libinput_device *device)
619 {
620         struct libinput_event_added_device *added_device_event;
621
622         added_device_event = malloc(sizeof *added_device_event);
623         if (!added_device_event)
624                 return;
625
626         *added_device_event = (struct libinput_event_added_device) {
627                 .device = device,
628         };
629
630         post_base_event(device->seat->libinput,
631                         LIBINPUT_EVENT_ADDED_DEVICE,
632                         &added_device_event->base);
633 }
634
635 void
636 notify_removed_device(struct libinput_device *device)
637 {
638         struct libinput_event_removed_device *removed_device_event;
639
640         removed_device_event = malloc(sizeof *removed_device_event);
641         if (!removed_device_event)
642                 return;
643
644         *removed_device_event = (struct libinput_event_removed_device) {
645                 .device = device,
646         };
647
648         post_base_event(device->seat->libinput,
649                         LIBINPUT_EVENT_REMOVED_DEVICE,
650                         &removed_device_event->base);
651 }
652
653 void
654 device_register_capability(struct libinput_device *device,
655                            enum libinput_device_capability capability)
656 {
657         struct libinput_event_device_register_capability *capability_event;
658
659         capability_event = malloc(sizeof *capability_event);
660
661         *capability_event = (struct libinput_event_device_register_capability) {
662                 .capability = capability,
663         };
664
665         post_device_event(device,
666                           LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
667                           &capability_event->base);
668 }
669
670 void
671 device_unregister_capability(struct libinput_device *device,
672                              enum libinput_device_capability capability)
673 {
674         struct libinput_event_device_unregister_capability *capability_event;
675
676         capability_event = malloc(sizeof *capability_event);
677
678         *capability_event = (struct libinput_event_device_unregister_capability) {
679                 .capability = capability,
680         };
681
682         post_device_event(device,
683                           LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
684                           &capability_event->base);
685 }
686
687 void
688 keyboard_notify_key(struct libinput_device *device,
689                     uint32_t time,
690                     uint32_t key,
691                     enum libinput_keyboard_key_state state)
692 {
693         struct libinput_event_keyboard_key *key_event;
694
695         key_event = malloc(sizeof *key_event);
696         if (!key_event)
697                 return;
698
699         *key_event = (struct libinput_event_keyboard_key) {
700                 .time = time,
701                 .key = key,
702                 .state = state,
703         };
704
705         post_device_event(device,
706                           LIBINPUT_EVENT_KEYBOARD_KEY,
707                           &key_event->base);
708 }
709
710 void
711 pointer_notify_motion(struct libinput_device *device,
712                       uint32_t time,
713                       li_fixed_t dx,
714                       li_fixed_t dy)
715 {
716         struct libinput_event_pointer_motion *motion_event;
717
718         motion_event = malloc(sizeof *motion_event);
719         if (!motion_event)
720                 return;
721
722         *motion_event = (struct libinput_event_pointer_motion) {
723                 .time = time,
724                 .dx = dx,
725                 .dy = dy,
726         };
727
728         post_device_event(device,
729                           LIBINPUT_EVENT_POINTER_MOTION,
730                           &motion_event->base);
731 }
732
733 void
734 pointer_notify_motion_absolute(struct libinput_device *device,
735                                uint32_t time,
736                                li_fixed_t x,
737                                li_fixed_t y)
738 {
739         struct libinput_event_pointer_motion_absolute *motion_absolute_event;
740
741         motion_absolute_event = malloc(sizeof *motion_absolute_event);
742         if (!motion_absolute_event)
743                 return;
744
745         *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
746                 .time = time,
747                 .x = x,
748                 .y = y,
749         };
750
751         post_device_event(device,
752                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
753                           &motion_absolute_event->base);
754 }
755
756 void
757 pointer_notify_button(struct libinput_device *device,
758                       uint32_t time,
759                       int32_t button,
760                       enum libinput_pointer_button_state state)
761 {
762         struct libinput_event_pointer_button *button_event;
763
764         button_event = malloc(sizeof *button_event);
765         if (!button_event)
766                 return;
767
768         *button_event = (struct libinput_event_pointer_button) {
769                 .time = time,
770                 .button = button,
771                 .state = state,
772         };
773
774         post_device_event(device,
775                           LIBINPUT_EVENT_POINTER_BUTTON,
776                           &button_event->base);
777 }
778
779 void
780 pointer_notify_axis(struct libinput_device *device,
781                     uint32_t time,
782                     enum libinput_pointer_axis axis,
783                     li_fixed_t value)
784 {
785         struct libinput_event_pointer_axis *axis_event;
786
787         axis_event = malloc(sizeof *axis_event);
788         if (!axis_event)
789                 return;
790
791         *axis_event = (struct libinput_event_pointer_axis) {
792                 .time = time,
793                 .axis = axis,
794                 .value = value,
795         };
796
797         post_device_event(device,
798                           LIBINPUT_EVENT_POINTER_AXIS,
799                           &axis_event->base);
800 }
801
802 void
803 touch_notify_touch(struct libinput_device *device,
804                    uint32_t time,
805                    int32_t slot,
806                    li_fixed_t x,
807                    li_fixed_t y,
808                    enum libinput_touch_type touch_type)
809 {
810         struct libinput_event_touch_touch *touch_event;
811
812         touch_event = malloc(sizeof *touch_event);
813         if (!touch_event)
814                 return;
815
816         *touch_event = (struct libinput_event_touch_touch) {
817                 .time = time,
818                 .slot = slot,
819                 .x = x,
820                 .y = y,
821                 .touch_type = touch_type,
822         };
823
824         post_device_event(device,
825                           LIBINPUT_EVENT_TOUCH_TOUCH,
826                           &touch_event->base);
827 }
828
829 static void
830 libinput_post_event(struct libinput *libinput,
831                     struct libinput_event *event)
832 {
833         struct libinput_event **events = libinput->events;
834         size_t events_len = libinput->events_len;
835         size_t events_count = libinput->events_count;
836         size_t move_len;
837         size_t new_out;
838
839         events_count++;
840         if (events_count > events_len) {
841                 if (events_len == 0)
842                         events_len = 4;
843                 else
844                         events_len *= 2;
845                 events = realloc(events, events_len * sizeof *events);
846                 if (!events) {
847                         fprintf(stderr, "Failed to reallocate event ring "
848                                 "buffer");
849                         return;
850                 }
851
852                 if (libinput->events_count > 0 && libinput->events_in == 0) {
853                         libinput->events_in = libinput->events_len;
854                 } else if (libinput->events_count > 0 &&
855                            libinput->events_out >= libinput->events_in) {
856                         move_len = libinput->events_len - libinput->events_out;
857                         new_out = events_len - move_len;
858                         memmove(events + new_out,
859                                 libinput->events + libinput->events_out,
860                                 move_len * sizeof *events);
861                         libinput->events_out = new_out;
862                 }
863
864                 libinput->events = events;
865                 libinput->events_len = events_len;
866         }
867
868         switch (libinput_event_get_class(event)) {
869         case LIBINPUT_EVENT_CLASS_BASE:
870                 break;
871         case LIBINPUT_EVENT_CLASS_SEAT:
872                 libinput_seat_ref(event->target.seat);
873                 break;
874         case LIBINPUT_EVENT_CLASS_DEVICE:
875                 libinput_device_ref(event->target.device);
876                 break;
877         }
878
879         libinput->events_count = events_count;
880         events[libinput->events_in] = event;
881         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
882 }
883
884 LIBINPUT_EXPORT struct libinput_event *
885 libinput_get_event(struct libinput *libinput)
886 {
887         struct libinput_event *event;
888
889         if (libinput->events_count == 0)
890                 return NULL;
891
892         event = libinput->events[libinput->events_out];
893         libinput->events_out =
894                 (libinput->events_out + 1) % libinput->events_len;
895         libinput->events_count--;
896
897         return event;
898 }
899
900 LIBINPUT_EXPORT void *
901 libinput_get_user_data(struct libinput *libinput)
902 {
903         return libinput->user_data;
904 }
905
906 LIBINPUT_EXPORT int
907 libinput_resume(struct libinput *libinput)
908 {
909         return udev_input_enable((struct udev_input *) libinput);
910 }
911
912 LIBINPUT_EXPORT void
913 libinput_suspend(struct libinput *libinput)
914 {
915         udev_input_disable((struct udev_input *) libinput);
916 }
917
918 LIBINPUT_EXPORT void
919 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
920 {
921         device->user_data = user_data;
922 }
923
924 LIBINPUT_EXPORT void *
925 libinput_device_get_user_data(struct libinput_device *device)
926 {
927         return device->user_data;
928 }
929
930 LIBINPUT_EXPORT const char *
931 libinput_device_get_output_name(struct libinput_device *device)
932 {
933         return evdev_device_get_output((struct evdev_device *) device);
934 }
935
936 LIBINPUT_EXPORT struct libinput_seat *
937 libinput_device_get_seat(struct libinput_device *device)
938 {
939         return device->seat;
940 }
941
942 LIBINPUT_EXPORT void
943 libinput_device_led_update(struct libinput_device *device,
944                            enum libinput_led leds)
945 {
946         evdev_device_led_update((struct evdev_device *) device, leds);
947 }
948
949 LIBINPUT_EXPORT int
950 libinput_device_get_keys(struct libinput_device *device,
951                          char *keys, size_t size)
952 {
953         return evdev_device_get_keys((struct evdev_device *) device,
954                                      keys,
955                                      size);
956 }
957
958 LIBINPUT_EXPORT void
959 libinput_device_calibrate(struct libinput_device *device,
960                           float calibration[6])
961 {
962         evdev_device_calibrate((struct evdev_device *) device, calibration);
963 }