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