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