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