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