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