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