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