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