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