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