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