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