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