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