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