Split up the touch event into the different touch types
[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         close(source->fd);
446         source->fd = -1;
447         list_insert(&libinput->source_destroy_list, &source->link);
448 }
449
450 int
451 libinput_init(struct libinput *libinput,
452               const struct libinput_interface *interface,
453               const struct libinput_interface_backend *interface_backend,
454               void *user_data)
455 {
456         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
457         if (libinput->epoll_fd < 0)
458                 return -1;
459
460         libinput->events_len = 4;
461         libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
462         if (!libinput->events) {
463                 close(libinput->epoll_fd);
464                 return -1;
465         }
466
467         libinput->interface = interface;
468         libinput->interface_backend = interface_backend;
469         libinput->user_data = user_data;
470         list_init(&libinput->source_destroy_list);
471         list_init(&libinput->seat_list);
472
473         return 0;
474 }
475
476 static void
477 libinput_device_destroy(struct libinput_device *device);
478
479 static void
480 libinput_seat_destroy(struct libinput_seat *seat);
481
482 static void
483 libinput_drop_destroyed_sources(struct libinput *libinput)
484 {
485         struct libinput_source *source, *next;
486
487         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
488                 free(source);
489         list_init(&libinput->source_destroy_list);
490 }
491
492 LIBINPUT_EXPORT void
493 libinput_destroy(struct libinput *libinput)
494 {
495         struct libinput_event *event;
496         struct libinput_device *device, *next_device;
497         struct libinput_seat *seat, *next_seat;
498
499         if (libinput == NULL)
500                 return;
501
502         libinput_suspend(libinput);
503
504         libinput->interface_backend->destroy(libinput);
505
506         while ((event = libinput_get_event(libinput)))
507                libinput_event_destroy(event);
508
509         libinput_drop_destroyed_sources(libinput);
510
511         free(libinput->events);
512
513         list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
514                 list_for_each_safe(device, next_device,
515                                    &seat->devices_list,
516                                    link)
517                         libinput_device_destroy(device);
518
519                 libinput_seat_destroy(seat);
520         }
521
522         close(libinput->epoll_fd);
523         free(libinput);
524 }
525
526 LIBINPUT_EXPORT void
527 libinput_event_destroy(struct libinput_event *event)
528 {
529         if (event == NULL)
530                 return;
531
532         if (event->device)
533                 libinput_device_unref(event->device);
534
535         free(event);
536 }
537
538 int
539 open_restricted(struct libinput *libinput,
540                 const char *path, int flags)
541 {
542         return libinput->interface->open_restricted(path,
543                                                     flags,
544                                                     libinput->user_data);
545 }
546
547 void
548 close_restricted(struct libinput *libinput, int fd)
549 {
550         return libinput->interface->close_restricted(fd, libinput->user_data);
551 }
552
553 void
554 libinput_seat_init(struct libinput_seat *seat,
555                    struct libinput *libinput,
556                    const char *physical_name,
557                    const char *logical_name,
558                    libinput_seat_destroy_func destroy)
559 {
560         seat->refcount = 1;
561         seat->libinput = libinput;
562         seat->physical_name = strdup(physical_name);
563         seat->logical_name = strdup(logical_name);
564         seat->destroy = destroy;
565         list_init(&seat->devices_list);
566         list_insert(&libinput->seat_list, &seat->link);
567 }
568
569 LIBINPUT_EXPORT void
570 libinput_seat_ref(struct libinput_seat *seat)
571 {
572         seat->refcount++;
573 }
574
575 static void
576 libinput_seat_destroy(struct libinput_seat *seat)
577 {
578         list_remove(&seat->link);
579         free(seat->logical_name);
580         free(seat->physical_name);
581         seat->destroy(seat);
582 }
583
584 LIBINPUT_EXPORT void
585 libinput_seat_unref(struct libinput_seat *seat)
586 {
587         assert(seat->refcount > 0);
588         seat->refcount--;
589         if (seat->refcount == 0)
590                 libinput_seat_destroy(seat);
591 }
592
593 LIBINPUT_EXPORT void
594 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
595 {
596         seat->user_data = user_data;
597 }
598
599 LIBINPUT_EXPORT void *
600 libinput_seat_get_user_data(struct libinput_seat *seat)
601 {
602         return seat->user_data;
603 }
604
605 LIBINPUT_EXPORT const char *
606 libinput_seat_get_physical_name(struct libinput_seat *seat)
607 {
608         return seat->physical_name;
609 }
610
611 LIBINPUT_EXPORT const char *
612 libinput_seat_get_logical_name(struct libinput_seat *seat)
613 {
614         return seat->logical_name;
615 }
616
617 void
618 libinput_device_init(struct libinput_device *device,
619                      struct libinput_seat *seat)
620 {
621         device->seat = seat;
622         device->refcount = 1;
623 }
624
625 LIBINPUT_EXPORT void
626 libinput_device_ref(struct libinput_device *device)
627 {
628         device->refcount++;
629 }
630
631 static void
632 libinput_device_destroy(struct libinput_device *device)
633 {
634         evdev_device_destroy((struct evdev_device *) device);
635 }
636
637 LIBINPUT_EXPORT void
638 libinput_device_unref(struct libinput_device *device)
639 {
640         assert(device->refcount > 0);
641         device->refcount--;
642         if (device->refcount == 0)
643                 libinput_device_destroy(device);
644 }
645
646 LIBINPUT_EXPORT int
647 libinput_get_fd(struct libinput *libinput)
648 {
649         return libinput->epoll_fd;
650 }
651
652 LIBINPUT_EXPORT int
653 libinput_dispatch(struct libinput *libinput)
654 {
655         struct libinput_source *source;
656         struct epoll_event ep[32];
657         int i, count;
658
659         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
660         if (count < 0)
661                 return -errno;
662
663         for (i = 0; i < count; ++i) {
664                 source = ep[i].data.ptr;
665                 if (source->fd == -1)
666                         continue;
667
668                 source->dispatch(source->user_data);
669         }
670
671         libinput_drop_destroyed_sources(libinput);
672
673         return 0;
674 }
675
676 static void
677 init_event_base(struct libinput_event *event,
678                 struct libinput_device *device,
679                 enum libinput_event_type type)
680 {
681         event->type = type;
682         event->device = device;
683 }
684
685 static void
686 post_base_event(struct libinput_device *device,
687                 enum libinput_event_type type,
688                 struct libinput_event *event)
689 {
690         struct libinput *libinput = device->seat->libinput;
691         init_event_base(event, device, type);
692         libinput_post_event(libinput, event);
693 }
694
695 static void
696 post_device_event(struct libinput_device *device,
697                   enum libinput_event_type type,
698                   struct libinput_event *event)
699 {
700         init_event_base(event, device, type);
701         libinput_post_event(device->seat->libinput, event);
702 }
703
704 void
705 notify_added_device(struct libinput_device *device)
706 {
707         struct libinput_event_device_notify *added_device_event;
708
709         added_device_event = zalloc(sizeof *added_device_event);
710         if (!added_device_event)
711                 return;
712
713         post_base_event(device,
714                         LIBINPUT_EVENT_DEVICE_ADDED,
715                         &added_device_event->base);
716 }
717
718 void
719 notify_removed_device(struct libinput_device *device)
720 {
721         struct libinput_event_device_notify *removed_device_event;
722
723         removed_device_event = zalloc(sizeof *removed_device_event);
724         if (!removed_device_event)
725                 return;
726
727         post_base_event(device,
728                         LIBINPUT_EVENT_DEVICE_REMOVED,
729                         &removed_device_event->base);
730 }
731
732 void
733 keyboard_notify_key(struct libinput_device *device,
734                     uint32_t time,
735                     uint32_t key,
736                     enum libinput_keyboard_key_state state)
737 {
738         struct libinput_event_keyboard *key_event;
739
740         key_event = zalloc(sizeof *key_event);
741         if (!key_event)
742                 return;
743
744         *key_event = (struct libinput_event_keyboard) {
745                 .time = time,
746                 .key = key,
747                 .state = state,
748         };
749
750         post_device_event(device,
751                           LIBINPUT_EVENT_KEYBOARD_KEY,
752                           &key_event->base);
753 }
754
755 void
756 pointer_notify_motion(struct libinput_device *device,
757                       uint32_t time,
758                       li_fixed_t dx,
759                       li_fixed_t dy)
760 {
761         struct libinput_event_pointer *motion_event;
762
763         motion_event = zalloc(sizeof *motion_event);
764         if (!motion_event)
765                 return;
766
767         *motion_event = (struct libinput_event_pointer) {
768                 .time = time,
769                 .x = dx,
770                 .y = dy,
771         };
772
773         post_device_event(device,
774                           LIBINPUT_EVENT_POINTER_MOTION,
775                           &motion_event->base);
776 }
777
778 void
779 pointer_notify_motion_absolute(struct libinput_device *device,
780                                uint32_t time,
781                                li_fixed_t x,
782                                li_fixed_t y)
783 {
784         struct libinput_event_pointer *motion_absolute_event;
785
786         motion_absolute_event = zalloc(sizeof *motion_absolute_event);
787         if (!motion_absolute_event)
788                 return;
789
790         *motion_absolute_event = (struct libinput_event_pointer) {
791                 .time = time,
792                 .x = x,
793                 .y = y,
794         };
795
796         post_device_event(device,
797                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
798                           &motion_absolute_event->base);
799 }
800
801 void
802 pointer_notify_button(struct libinput_device *device,
803                       uint32_t time,
804                       int32_t button,
805                       enum libinput_pointer_button_state state)
806 {
807         struct libinput_event_pointer *button_event;
808
809         button_event = zalloc(sizeof *button_event);
810         if (!button_event)
811                 return;
812
813         *button_event = (struct libinput_event_pointer) {
814                 .time = time,
815                 .button = button,
816                 .state = state,
817         };
818
819         post_device_event(device,
820                           LIBINPUT_EVENT_POINTER_BUTTON,
821                           &button_event->base);
822 }
823
824 void
825 pointer_notify_axis(struct libinput_device *device,
826                     uint32_t time,
827                     enum libinput_pointer_axis axis,
828                     li_fixed_t value)
829 {
830         struct libinput_event_pointer *axis_event;
831
832         axis_event = zalloc(sizeof *axis_event);
833         if (!axis_event)
834                 return;
835
836         *axis_event = (struct libinput_event_pointer) {
837                 .time = time,
838                 .axis = axis,
839                 .value = value,
840         };
841
842         post_device_event(device,
843                           LIBINPUT_EVENT_POINTER_AXIS,
844                           &axis_event->base);
845 }
846
847 void
848 touch_notify_touch_down(struct libinput_device *device,
849                         uint32_t time,
850                         int32_t slot,
851                         int32_t seat_slot,
852                         li_fixed_t x,
853                         li_fixed_t y)
854 {
855         struct libinput_event_touch *touch_event;
856
857         touch_event = zalloc(sizeof *touch_event);
858         if (!touch_event)
859                 return;
860
861         *touch_event = (struct libinput_event_touch) {
862                 .time = time,
863                 .slot = slot,
864                 .seat_slot = seat_slot,
865                 .x = x,
866                 .y = y,
867         };
868
869         post_device_event(device,
870                           LIBINPUT_EVENT_TOUCH_DOWN,
871                           &touch_event->base);
872 }
873
874 void
875 touch_notify_touch_motion(struct libinput_device *device,
876                           uint32_t time,
877                           int32_t slot,
878                           int32_t seat_slot,
879                           li_fixed_t x,
880                           li_fixed_t y)
881 {
882         struct libinput_event_touch *touch_event;
883
884         touch_event = zalloc(sizeof *touch_event);
885         if (!touch_event)
886                 return;
887
888         *touch_event = (struct libinput_event_touch) {
889                 .time = time,
890                 .slot = slot,
891                 .seat_slot = seat_slot,
892                 .x = x,
893                 .y = y,
894         };
895
896         post_device_event(device,
897                           LIBINPUT_EVENT_TOUCH_MOTION,
898                           &touch_event->base);
899 }
900
901 void
902 touch_notify_touch_up(struct libinput_device *device,
903                       uint32_t time,
904                       int32_t slot,
905                       int32_t seat_slot)
906 {
907         struct libinput_event_touch *touch_event;
908
909         touch_event = zalloc(sizeof *touch_event);
910         if (!touch_event)
911                 return;
912
913         *touch_event = (struct libinput_event_touch) {
914                 .time = time,
915                 .slot = slot,
916                 .seat_slot = seat_slot,
917         };
918
919         post_device_event(device,
920                           LIBINPUT_EVENT_TOUCH_UP,
921                           &touch_event->base);
922 }
923
924 void
925 touch_notify_frame(struct libinput_device *device,
926                    uint32_t time)
927 {
928         struct libinput_event_touch *touch_event;
929
930         touch_event = zalloc(sizeof *touch_event);
931         if (!touch_event)
932                 return;
933
934         *touch_event = (struct libinput_event_touch) {
935                 .time = time,
936         };
937
938         post_device_event(device,
939                           LIBINPUT_EVENT_TOUCH_FRAME,
940                           &touch_event->base);
941 }
942
943
944 static void
945 libinput_post_event(struct libinput *libinput,
946                     struct libinput_event *event)
947 {
948         struct libinput_event **events = libinput->events;
949         size_t events_len = libinput->events_len;
950         size_t events_count = libinput->events_count;
951         size_t move_len;
952         size_t new_out;
953
954         events_count++;
955         if (events_count > events_len) {
956                 events_len *= 2;
957                 events = realloc(events, events_len * sizeof *events);
958                 if (!events) {
959                         fprintf(stderr, "Failed to reallocate event ring "
960                                 "buffer");
961                         return;
962                 }
963
964                 if (libinput->events_count > 0 && libinput->events_in == 0) {
965                         libinput->events_in = libinput->events_len;
966                 } else if (libinput->events_count > 0 &&
967                            libinput->events_out >= libinput->events_in) {
968                         move_len = libinput->events_len - libinput->events_out;
969                         new_out = events_len - move_len;
970                         memmove(events + new_out,
971                                 events + libinput->events_out,
972                                 move_len * sizeof *events);
973                         libinput->events_out = new_out;
974                 }
975
976                 libinput->events = events;
977                 libinput->events_len = events_len;
978         }
979
980         if (event->device)
981                 libinput_device_ref(event->device);
982
983         libinput->events_count = events_count;
984         events[libinput->events_in] = event;
985         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
986 }
987
988 LIBINPUT_EXPORT struct libinput_event *
989 libinput_get_event(struct libinput *libinput)
990 {
991         struct libinput_event *event;
992
993         if (libinput->events_count == 0)
994                 return NULL;
995
996         event = libinput->events[libinput->events_out];
997         libinput->events_out =
998                 (libinput->events_out + 1) % libinput->events_len;
999         libinput->events_count--;
1000
1001         return event;
1002 }
1003
1004 LIBINPUT_EXPORT enum libinput_event_type
1005 libinput_next_event_type(struct libinput *libinput)
1006 {
1007         struct libinput_event *event;
1008
1009         if (libinput->events_count == 0)
1010                 return LIBINPUT_EVENT_NONE;
1011
1012         event = libinput->events[libinput->events_out];
1013         return event->type;
1014 }
1015
1016 LIBINPUT_EXPORT void *
1017 libinput_get_user_data(struct libinput *libinput)
1018 {
1019         return libinput->user_data;
1020 }
1021
1022 LIBINPUT_EXPORT int
1023 libinput_resume(struct libinput *libinput)
1024 {
1025         return libinput->interface_backend->resume(libinput);
1026 }
1027
1028 LIBINPUT_EXPORT void
1029 libinput_suspend(struct libinput *libinput)
1030 {
1031         libinput->interface_backend->suspend(libinput);
1032 }
1033
1034 LIBINPUT_EXPORT void
1035 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
1036 {
1037         device->user_data = user_data;
1038 }
1039
1040 LIBINPUT_EXPORT void *
1041 libinput_device_get_user_data(struct libinput_device *device)
1042 {
1043         return device->user_data;
1044 }
1045
1046 LIBINPUT_EXPORT const char *
1047 libinput_device_get_sysname(struct libinput_device *device)
1048 {
1049         return evdev_device_get_sysname((struct evdev_device *) device);
1050 }
1051
1052 LIBINPUT_EXPORT const char *
1053 libinput_device_get_output_name(struct libinput_device *device)
1054 {
1055         return evdev_device_get_output((struct evdev_device *) device);
1056 }
1057
1058 LIBINPUT_EXPORT struct libinput_seat *
1059 libinput_device_get_seat(struct libinput_device *device)
1060 {
1061         return device->seat;
1062 }
1063
1064 LIBINPUT_EXPORT void
1065 libinput_device_led_update(struct libinput_device *device,
1066                            enum libinput_led leds)
1067 {
1068         evdev_device_led_update((struct evdev_device *) device, leds);
1069 }
1070
1071 LIBINPUT_EXPORT int
1072 libinput_device_get_keys(struct libinput_device *device,
1073                          char *keys, size_t size)
1074 {
1075         return evdev_device_get_keys((struct evdev_device *) device,
1076                                      keys,
1077                                      size);
1078 }
1079
1080 LIBINPUT_EXPORT void
1081 libinput_device_calibrate(struct libinput_device *device,
1082                           float calibration[6])
1083 {
1084         evdev_device_calibrate((struct evdev_device *) device, calibration);
1085 }
1086
1087 LIBINPUT_EXPORT int
1088 libinput_device_has_capability(struct libinput_device *device,
1089                                enum libinput_device_capability capability)
1090 {
1091         return evdev_device_has_capability((struct evdev_device *) device,
1092                                            capability);
1093 }