Fix documentation for libinput_log_set_handler
[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 #include "timer.h"
37
38 struct libinput_source {
39         libinput_source_dispatch_t dispatch;
40         void *user_data;
41         int fd;
42         struct list link;
43 };
44
45 struct libinput_event {
46         enum libinput_event_type type;
47         struct libinput_device *device;
48 };
49
50 struct libinput_event_device_notify {
51         struct libinput_event base;
52 };
53
54 struct libinput_event_keyboard {
55         struct libinput_event base;
56         uint32_t time;
57         uint32_t key;
58         uint32_t seat_key_count;
59         enum libinput_key_state state;
60 };
61
62 struct libinput_event_pointer {
63         struct libinput_event base;
64         uint32_t time;
65         double x;
66         double y;
67         uint32_t button;
68         uint32_t seat_button_count;
69         enum libinput_button_state state;
70         enum libinput_pointer_axis axis;
71         double value;
72 };
73
74 struct libinput_event_touch {
75         struct libinput_event base;
76         uint32_t time;
77         int32_t slot;
78         int32_t seat_slot;
79         double x;
80         double y;
81 };
82
83 static void
84 libinput_default_log_func(struct libinput *libinput,
85                           enum libinput_log_priority priority,
86                           const char *format, va_list args)
87 {
88         const char *prefix;
89
90         switch(priority) {
91         case LIBINPUT_LOG_PRIORITY_DEBUG: prefix = "debug"; break;
92         case LIBINPUT_LOG_PRIORITY_INFO: prefix = "info"; break;
93         case LIBINPUT_LOG_PRIORITY_ERROR: prefix = "error"; break;
94         default: prefix="<invalid priority>"; break;
95         }
96
97         fprintf(stderr, "libinput %s: ", prefix);
98         vfprintf(stderr, format, args);
99 }
100
101 void
102 log_msg_va(struct libinput *libinput,
103            enum libinput_log_priority priority,
104            const char *format,
105            va_list args)
106 {
107         if (libinput->log_handler &&
108             libinput->log_priority <= priority)
109                 libinput->log_handler(libinput, priority, format, args);
110 }
111
112 void
113 log_msg(struct libinput *libinput,
114         enum libinput_log_priority priority,
115         const char *format, ...)
116 {
117         va_list args;
118
119         va_start(args, format);
120         log_msg_va(libinput, priority, format, args);
121         va_end(args);
122 }
123
124 LIBINPUT_EXPORT void
125 libinput_log_set_priority(struct libinput *libinput,
126                           enum libinput_log_priority priority)
127 {
128         libinput->log_priority = priority;
129 }
130
131 LIBINPUT_EXPORT enum libinput_log_priority
132 libinput_log_get_priority(const struct libinput *libinput)
133 {
134         return libinput->log_priority;
135 }
136
137 LIBINPUT_EXPORT void
138 libinput_log_set_handler(struct libinput *libinput,
139                          libinput_log_handler log_handler)
140 {
141         libinput->log_handler = log_handler;
142 }
143
144 static void
145 libinput_post_event(struct libinput *libinput,
146                     struct libinput_event *event);
147
148 LIBINPUT_EXPORT enum libinput_event_type
149 libinput_event_get_type(struct libinput_event *event)
150 {
151         return event->type;
152 }
153
154 LIBINPUT_EXPORT struct libinput *
155 libinput_event_get_context(struct libinput_event *event)
156 {
157         return event->device->seat->libinput;
158 }
159
160 LIBINPUT_EXPORT struct libinput_device *
161 libinput_event_get_device(struct libinput_event *event)
162 {
163         return event->device;
164 }
165
166 LIBINPUT_EXPORT struct libinput_event_pointer *
167 libinput_event_get_pointer_event(struct libinput_event *event)
168 {
169         switch (event->type) {
170         case LIBINPUT_EVENT_NONE:
171                 abort(); /* not used as actual event type */
172         case LIBINPUT_EVENT_DEVICE_ADDED:
173         case LIBINPUT_EVENT_DEVICE_REMOVED:
174         case LIBINPUT_EVENT_KEYBOARD_KEY:
175                 break;
176         case LIBINPUT_EVENT_POINTER_MOTION:
177         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
178         case LIBINPUT_EVENT_POINTER_BUTTON:
179         case LIBINPUT_EVENT_POINTER_AXIS:
180                 return (struct libinput_event_pointer *) event;
181         case LIBINPUT_EVENT_TOUCH_DOWN:
182         case LIBINPUT_EVENT_TOUCH_UP:
183         case LIBINPUT_EVENT_TOUCH_MOTION:
184         case LIBINPUT_EVENT_TOUCH_CANCEL:
185         case LIBINPUT_EVENT_TOUCH_FRAME:
186                 break;
187         }
188
189         return NULL;
190 }
191
192 LIBINPUT_EXPORT struct libinput_event_keyboard *
193 libinput_event_get_keyboard_event(struct libinput_event *event)
194 {
195         switch (event->type) {
196         case LIBINPUT_EVENT_NONE:
197                 abort(); /* not used as actual event type */
198         case LIBINPUT_EVENT_DEVICE_ADDED:
199         case LIBINPUT_EVENT_DEVICE_REMOVED:
200                 break;
201         case LIBINPUT_EVENT_KEYBOARD_KEY:
202                 return (struct libinput_event_keyboard *) event;
203         case LIBINPUT_EVENT_POINTER_MOTION:
204         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
205         case LIBINPUT_EVENT_POINTER_BUTTON:
206         case LIBINPUT_EVENT_POINTER_AXIS:
207         case LIBINPUT_EVENT_TOUCH_DOWN:
208         case LIBINPUT_EVENT_TOUCH_UP:
209         case LIBINPUT_EVENT_TOUCH_MOTION:
210         case LIBINPUT_EVENT_TOUCH_CANCEL:
211         case LIBINPUT_EVENT_TOUCH_FRAME:
212                 break;
213         }
214
215         return NULL;
216 }
217
218 LIBINPUT_EXPORT struct libinput_event_touch *
219 libinput_event_get_touch_event(struct libinput_event *event)
220 {
221         switch (event->type) {
222         case LIBINPUT_EVENT_NONE:
223                 abort(); /* not used as actual event type */
224         case LIBINPUT_EVENT_DEVICE_ADDED:
225         case LIBINPUT_EVENT_DEVICE_REMOVED:
226         case LIBINPUT_EVENT_KEYBOARD_KEY:
227         case LIBINPUT_EVENT_POINTER_MOTION:
228         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
229         case LIBINPUT_EVENT_POINTER_BUTTON:
230         case LIBINPUT_EVENT_POINTER_AXIS:
231                 break;
232         case LIBINPUT_EVENT_TOUCH_DOWN:
233         case LIBINPUT_EVENT_TOUCH_UP:
234         case LIBINPUT_EVENT_TOUCH_MOTION:
235         case LIBINPUT_EVENT_TOUCH_CANCEL:
236         case LIBINPUT_EVENT_TOUCH_FRAME:
237                 return (struct libinput_event_touch *) event;
238         }
239
240         return NULL;
241 }
242
243 LIBINPUT_EXPORT struct libinput_event_device_notify *
244 libinput_event_get_device_notify_event(struct libinput_event *event)
245 {
246         switch (event->type) {
247         case LIBINPUT_EVENT_NONE:
248                 abort(); /* not used as actual event type */
249         case LIBINPUT_EVENT_DEVICE_ADDED:
250         case LIBINPUT_EVENT_DEVICE_REMOVED:
251                 return (struct libinput_event_device_notify *) event;
252         case LIBINPUT_EVENT_KEYBOARD_KEY:
253         case LIBINPUT_EVENT_POINTER_MOTION:
254         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
255         case LIBINPUT_EVENT_POINTER_BUTTON:
256         case LIBINPUT_EVENT_POINTER_AXIS:
257         case LIBINPUT_EVENT_TOUCH_DOWN:
258         case LIBINPUT_EVENT_TOUCH_UP:
259         case LIBINPUT_EVENT_TOUCH_MOTION:
260         case LIBINPUT_EVENT_TOUCH_CANCEL:
261         case LIBINPUT_EVENT_TOUCH_FRAME:
262                 break;
263         }
264
265         return NULL;
266 }
267
268 LIBINPUT_EXPORT uint32_t
269 libinput_event_keyboard_get_time(struct libinput_event_keyboard *event)
270 {
271         return event->time;
272 }
273
274 LIBINPUT_EXPORT uint32_t
275 libinput_event_keyboard_get_key(struct libinput_event_keyboard *event)
276 {
277         return event->key;
278 }
279
280 LIBINPUT_EXPORT enum libinput_key_state
281 libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event)
282 {
283         return event->state;
284 }
285
286 LIBINPUT_EXPORT uint32_t
287 libinput_event_keyboard_get_seat_key_count(
288         struct libinput_event_keyboard *event)
289 {
290         return event->seat_key_count;
291 }
292
293 LIBINPUT_EXPORT uint32_t
294 libinput_event_pointer_get_time(struct libinput_event_pointer *event)
295 {
296         return event->time;
297 }
298
299 LIBINPUT_EXPORT double
300 libinput_event_pointer_get_dx(struct libinput_event_pointer *event)
301 {
302         return event->x;
303 }
304
305 LIBINPUT_EXPORT double
306 libinput_event_pointer_get_dy(struct libinput_event_pointer *event)
307 {
308         return event->y;
309 }
310
311 LIBINPUT_EXPORT double
312 libinput_event_pointer_get_absolute_x(struct libinput_event_pointer *event)
313 {
314         struct evdev_device *device =
315                 (struct evdev_device *) event->base.device;
316
317         return evdev_convert_to_mm(device->abs.absinfo_x, event->x);
318 }
319
320 LIBINPUT_EXPORT double
321 libinput_event_pointer_get_absolute_y(struct libinput_event_pointer *event)
322 {
323         struct evdev_device *device =
324                 (struct evdev_device *) event->base.device;
325
326         return evdev_convert_to_mm(device->abs.absinfo_y, event->y);
327 }
328
329 LIBINPUT_EXPORT double
330 libinput_event_pointer_get_absolute_x_transformed(
331         struct libinput_event_pointer *event,
332         uint32_t width)
333 {
334         struct evdev_device *device =
335                 (struct evdev_device *) event->base.device;
336
337         return evdev_device_transform_x(device, event->x, width);
338 }
339
340 LIBINPUT_EXPORT double
341 libinput_event_pointer_get_absolute_y_transformed(
342         struct libinput_event_pointer *event,
343         uint32_t height)
344 {
345         struct evdev_device *device =
346                 (struct evdev_device *) event->base.device;
347
348         return evdev_device_transform_y(device, event->y, height);
349 }
350
351 LIBINPUT_EXPORT uint32_t
352 libinput_event_pointer_get_button(struct libinput_event_pointer *event)
353 {
354         return event->button;
355 }
356
357 LIBINPUT_EXPORT enum libinput_button_state
358 libinput_event_pointer_get_button_state(struct libinput_event_pointer *event)
359 {
360         return event->state;
361 }
362
363 LIBINPUT_EXPORT uint32_t
364 libinput_event_pointer_get_seat_button_count(
365         struct libinput_event_pointer *event)
366 {
367         return event->seat_button_count;
368 }
369
370 LIBINPUT_EXPORT enum libinput_pointer_axis
371 libinput_event_pointer_get_axis(struct libinput_event_pointer *event)
372 {
373         return event->axis;
374 }
375
376 LIBINPUT_EXPORT double
377 libinput_event_pointer_get_axis_value(struct libinput_event_pointer *event)
378 {
379         return event->value;
380 }
381
382 LIBINPUT_EXPORT uint32_t
383 libinput_event_touch_get_time(struct libinput_event_touch *event)
384 {
385         return event->time;
386 }
387
388 LIBINPUT_EXPORT int32_t
389 libinput_event_touch_get_slot(struct libinput_event_touch *event)
390 {
391         return event->slot;
392 }
393
394 LIBINPUT_EXPORT int32_t
395 libinput_event_touch_get_seat_slot(struct libinput_event_touch *event)
396 {
397         return event->seat_slot;
398 }
399
400 LIBINPUT_EXPORT double
401 libinput_event_touch_get_x(struct libinput_event_touch *event)
402 {
403         struct evdev_device *device =
404                 (struct evdev_device *) event->base.device;
405
406         return evdev_convert_to_mm(device->abs.absinfo_x, event->x);
407 }
408
409 LIBINPUT_EXPORT double
410 libinput_event_touch_get_x_transformed(struct libinput_event_touch *event,
411                                        uint32_t width)
412 {
413         struct evdev_device *device =
414                 (struct evdev_device *) event->base.device;
415
416         return evdev_device_transform_x(device, event->x, width);
417 }
418
419 LIBINPUT_EXPORT double
420 libinput_event_touch_get_y_transformed(struct libinput_event_touch *event,
421                                        uint32_t height)
422 {
423         struct evdev_device *device =
424                 (struct evdev_device *) event->base.device;
425
426         return evdev_device_transform_y(device, event->y, height);
427 }
428
429 LIBINPUT_EXPORT double
430 libinput_event_touch_get_y(struct libinput_event_touch *event)
431 {
432         struct evdev_device *device =
433                 (struct evdev_device *) event->base.device;
434
435         return evdev_convert_to_mm(device->abs.absinfo_y, event->y);
436 }
437
438 struct libinput_source *
439 libinput_add_fd(struct libinput *libinput,
440                 int fd,
441                 libinput_source_dispatch_t dispatch,
442                 void *user_data)
443 {
444         struct libinput_source *source;
445         struct epoll_event ep;
446
447         source = malloc(sizeof *source);
448         if (!source)
449                 return NULL;
450
451         source->dispatch = dispatch;
452         source->user_data = user_data;
453         source->fd = fd;
454
455         memset(&ep, 0, sizeof ep);
456         ep.events = EPOLLIN;
457         ep.data.ptr = source;
458
459         if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
460                 free(source);
461                 return NULL;
462         }
463
464         return source;
465 }
466
467 void
468 libinput_remove_source(struct libinput *libinput,
469                        struct libinput_source *source)
470 {
471         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
472         source->fd = -1;
473         list_insert(&libinput->source_destroy_list, &source->link);
474 }
475
476 int
477 libinput_init(struct libinput *libinput,
478               const struct libinput_interface *interface,
479               const struct libinput_interface_backend *interface_backend,
480               void *user_data)
481 {
482         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
483         if (libinput->epoll_fd < 0)
484                 return -1;
485
486         libinput->events_len = 4;
487         libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
488         if (!libinput->events) {
489                 close(libinput->epoll_fd);
490                 return -1;
491         }
492
493         libinput->log_handler = libinput_default_log_func;
494         libinput->log_priority = LIBINPUT_LOG_PRIORITY_ERROR;
495         libinput->interface = interface;
496         libinput->interface_backend = interface_backend;
497         libinput->user_data = user_data;
498         libinput->refcount = 1;
499         list_init(&libinput->source_destroy_list);
500         list_init(&libinput->seat_list);
501
502         if (libinput_timer_subsys_init(libinput) != 0) {
503                 free(libinput->events);
504                 close(libinput->epoll_fd);
505                 return -1;
506         }
507
508         return 0;
509 }
510
511 static void
512 libinput_device_destroy(struct libinput_device *device);
513
514 static void
515 libinput_seat_destroy(struct libinput_seat *seat);
516
517 static void
518 libinput_drop_destroyed_sources(struct libinput *libinput)
519 {
520         struct libinput_source *source, *next;
521
522         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
523                 free(source);
524         list_init(&libinput->source_destroy_list);
525 }
526
527 LIBINPUT_EXPORT struct libinput *
528 libinput_ref(struct libinput *libinput)
529 {
530         libinput->refcount++;
531         return libinput;
532 }
533
534 LIBINPUT_EXPORT struct libinput *
535 libinput_unref(struct libinput *libinput)
536 {
537         struct libinput_event *event;
538         struct libinput_device *device, *next_device;
539         struct libinput_seat *seat, *next_seat;
540
541         if (libinput == NULL)
542                 return NULL;
543
544         assert(libinput->refcount > 0);
545         libinput->refcount--;
546         if (libinput->refcount > 0)
547                 return libinput;
548
549         libinput_suspend(libinput);
550
551         libinput->interface_backend->destroy(libinput);
552
553         while ((event = libinput_get_event(libinput)))
554                libinput_event_destroy(event);
555
556         free(libinput->events);
557
558         list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
559                 list_for_each_safe(device, next_device,
560                                    &seat->devices_list,
561                                    link)
562                         libinput_device_destroy(device);
563
564                 libinput_seat_destroy(seat);
565         }
566
567         libinput_timer_subsys_destroy(libinput);
568         libinput_drop_destroyed_sources(libinput);
569         close(libinput->epoll_fd);
570         free(libinput);
571
572         return NULL;
573 }
574
575 LIBINPUT_EXPORT void
576 libinput_event_destroy(struct libinput_event *event)
577 {
578         if (event == NULL)
579                 return;
580
581         if (event->device)
582                 libinput_device_unref(event->device);
583
584         free(event);
585 }
586
587 int
588 open_restricted(struct libinput *libinput,
589                 const char *path, int flags)
590 {
591         return libinput->interface->open_restricted(path,
592                                                     flags,
593                                                     libinput->user_data);
594 }
595
596 void
597 close_restricted(struct libinput *libinput, int fd)
598 {
599         return libinput->interface->close_restricted(fd, libinput->user_data);
600 }
601
602 void
603 libinput_seat_init(struct libinput_seat *seat,
604                    struct libinput *libinput,
605                    const char *physical_name,
606                    const char *logical_name,
607                    libinput_seat_destroy_func destroy)
608 {
609         seat->refcount = 1;
610         seat->libinput = libinput;
611         seat->physical_name = strdup(physical_name);
612         seat->logical_name = strdup(logical_name);
613         seat->destroy = destroy;
614         list_init(&seat->devices_list);
615         list_insert(&libinput->seat_list, &seat->link);
616 }
617
618 LIBINPUT_EXPORT struct libinput_seat *
619 libinput_seat_ref(struct libinput_seat *seat)
620 {
621         seat->refcount++;
622         return seat;
623 }
624
625 static void
626 libinput_seat_destroy(struct libinput_seat *seat)
627 {
628         list_remove(&seat->link);
629         free(seat->logical_name);
630         free(seat->physical_name);
631         seat->destroy(seat);
632 }
633
634 LIBINPUT_EXPORT struct libinput_seat *
635 libinput_seat_unref(struct libinput_seat *seat)
636 {
637         assert(seat->refcount > 0);
638         seat->refcount--;
639         if (seat->refcount == 0) {
640                 libinput_seat_destroy(seat);
641                 return NULL;
642         } else {
643                 return seat;
644         }
645 }
646
647 LIBINPUT_EXPORT void
648 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
649 {
650         seat->user_data = user_data;
651 }
652
653 LIBINPUT_EXPORT void *
654 libinput_seat_get_user_data(struct libinput_seat *seat)
655 {
656         return seat->user_data;
657 }
658
659 LIBINPUT_EXPORT const char *
660 libinput_seat_get_physical_name(struct libinput_seat *seat)
661 {
662         return seat->physical_name;
663 }
664
665 LIBINPUT_EXPORT const char *
666 libinput_seat_get_logical_name(struct libinput_seat *seat)
667 {
668         return seat->logical_name;
669 }
670
671 void
672 libinput_device_init(struct libinput_device *device,
673                      struct libinput_seat *seat)
674 {
675         device->seat = seat;
676         device->refcount = 1;
677 }
678
679 LIBINPUT_EXPORT struct libinput_device *
680 libinput_device_ref(struct libinput_device *device)
681 {
682         device->refcount++;
683         return device;
684 }
685
686 static void
687 libinput_device_destroy(struct libinput_device *device)
688 {
689         evdev_device_destroy((struct evdev_device *) device);
690 }
691
692 LIBINPUT_EXPORT struct libinput_device *
693 libinput_device_unref(struct libinput_device *device)
694 {
695         assert(device->refcount > 0);
696         device->refcount--;
697         if (device->refcount == 0) {
698                 libinput_device_destroy(device);
699                 return NULL;
700         } else {
701                 return device;
702         }
703 }
704
705 LIBINPUT_EXPORT int
706 libinput_get_fd(struct libinput *libinput)
707 {
708         return libinput->epoll_fd;
709 }
710
711 LIBINPUT_EXPORT int
712 libinput_dispatch(struct libinput *libinput)
713 {
714         struct libinput_source *source;
715         struct epoll_event ep[32];
716         int i, count;
717
718         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
719         if (count < 0)
720                 return -errno;
721
722         for (i = 0; i < count; ++i) {
723                 source = ep[i].data.ptr;
724                 if (source->fd == -1)
725                         continue;
726
727                 source->dispatch(source->user_data);
728         }
729
730         libinput_drop_destroyed_sources(libinput);
731
732         return 0;
733 }
734
735 static uint32_t
736 update_seat_key_count(struct libinput_seat *seat,
737                       int32_t key,
738                       enum libinput_key_state state)
739 {
740         assert(key >= 0 && key <= KEY_MAX);
741
742         switch (state) {
743         case LIBINPUT_KEY_STATE_PRESSED:
744                 return ++seat->button_count[key];
745         case LIBINPUT_KEY_STATE_RELEASED:
746                 /* We might not have received the first PRESSED event. */
747                 if (seat->button_count[key] == 0)
748                         return 0;
749
750                 return --seat->button_count[key];
751         }
752
753         return 0;
754 }
755
756 static uint32_t
757 update_seat_button_count(struct libinput_seat *seat,
758                          int32_t button,
759                          enum libinput_button_state state)
760 {
761         assert(button >= 0 && button <= KEY_MAX);
762
763         switch (state) {
764         case LIBINPUT_BUTTON_STATE_PRESSED:
765                 return ++seat->button_count[button];
766         case LIBINPUT_BUTTON_STATE_RELEASED:
767                 /* We might not have received the first PRESSED event. */
768                 if (seat->button_count[button] == 0)
769                         return 0;
770
771                 return --seat->button_count[button];
772         }
773
774         return 0;
775 }
776
777 static void
778 init_event_base(struct libinput_event *event,
779                 struct libinput_device *device,
780                 enum libinput_event_type type)
781 {
782         event->type = type;
783         event->device = device;
784 }
785
786 static void
787 post_base_event(struct libinput_device *device,
788                 enum libinput_event_type type,
789                 struct libinput_event *event)
790 {
791         struct libinput *libinput = device->seat->libinput;
792         init_event_base(event, device, type);
793         libinput_post_event(libinput, event);
794 }
795
796 static void
797 post_device_event(struct libinput_device *device,
798                   enum libinput_event_type type,
799                   struct libinput_event *event)
800 {
801         init_event_base(event, device, type);
802         libinput_post_event(device->seat->libinput, event);
803 }
804
805 void
806 notify_added_device(struct libinput_device *device)
807 {
808         struct libinput_event_device_notify *added_device_event;
809
810         added_device_event = zalloc(sizeof *added_device_event);
811         if (!added_device_event)
812                 return;
813
814         post_base_event(device,
815                         LIBINPUT_EVENT_DEVICE_ADDED,
816                         &added_device_event->base);
817 }
818
819 void
820 notify_removed_device(struct libinput_device *device)
821 {
822         struct libinput_event_device_notify *removed_device_event;
823
824         removed_device_event = zalloc(sizeof *removed_device_event);
825         if (!removed_device_event)
826                 return;
827
828         post_base_event(device,
829                         LIBINPUT_EVENT_DEVICE_REMOVED,
830                         &removed_device_event->base);
831 }
832
833 void
834 keyboard_notify_key(struct libinput_device *device,
835                     uint32_t time,
836                     uint32_t key,
837                     enum libinput_key_state state)
838 {
839         struct libinput_event_keyboard *key_event;
840         uint32_t seat_key_count;
841
842         key_event = zalloc(sizeof *key_event);
843         if (!key_event)
844                 return;
845
846         seat_key_count = update_seat_key_count(device->seat, key, state);
847
848         *key_event = (struct libinput_event_keyboard) {
849                 .time = time,
850                 .key = key,
851                 .state = state,
852                 .seat_key_count = seat_key_count,
853         };
854
855         post_device_event(device,
856                           LIBINPUT_EVENT_KEYBOARD_KEY,
857                           &key_event->base);
858 }
859
860 void
861 pointer_notify_motion(struct libinput_device *device,
862                       uint32_t time,
863                       double dx,
864                       double dy)
865 {
866         struct libinput_event_pointer *motion_event;
867
868         motion_event = zalloc(sizeof *motion_event);
869         if (!motion_event)
870                 return;
871
872         *motion_event = (struct libinput_event_pointer) {
873                 .time = time,
874                 .x = dx,
875                 .y = dy,
876         };
877
878         post_device_event(device,
879                           LIBINPUT_EVENT_POINTER_MOTION,
880                           &motion_event->base);
881 }
882
883 void
884 pointer_notify_motion_absolute(struct libinput_device *device,
885                                uint32_t time,
886                                double x,
887                                double y)
888 {
889         struct libinput_event_pointer *motion_absolute_event;
890
891         motion_absolute_event = zalloc(sizeof *motion_absolute_event);
892         if (!motion_absolute_event)
893                 return;
894
895         *motion_absolute_event = (struct libinput_event_pointer) {
896                 .time = time,
897                 .x = x,
898                 .y = y,
899         };
900
901         post_device_event(device,
902                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
903                           &motion_absolute_event->base);
904 }
905
906 void
907 pointer_notify_button(struct libinput_device *device,
908                       uint32_t time,
909                       int32_t button,
910                       enum libinput_button_state state)
911 {
912         struct libinput_event_pointer *button_event;
913         int32_t seat_button_count;
914
915         button_event = zalloc(sizeof *button_event);
916         if (!button_event)
917                 return;
918
919         seat_button_count = update_seat_button_count(device->seat,
920                                                      button,
921                                                      state);
922
923         *button_event = (struct libinput_event_pointer) {
924                 .time = time,
925                 .button = button,
926                 .state = state,
927                 .seat_button_count = seat_button_count,
928         };
929
930         post_device_event(device,
931                           LIBINPUT_EVENT_POINTER_BUTTON,
932                           &button_event->base);
933 }
934
935 void
936 pointer_notify_axis(struct libinput_device *device,
937                     uint32_t time,
938                     enum libinput_pointer_axis axis,
939                     double value)
940 {
941         struct libinput_event_pointer *axis_event;
942
943         axis_event = zalloc(sizeof *axis_event);
944         if (!axis_event)
945                 return;
946
947         *axis_event = (struct libinput_event_pointer) {
948                 .time = time,
949                 .axis = axis,
950                 .value = value,
951         };
952
953         post_device_event(device,
954                           LIBINPUT_EVENT_POINTER_AXIS,
955                           &axis_event->base);
956 }
957
958 void
959 touch_notify_touch_down(struct libinput_device *device,
960                         uint32_t time,
961                         int32_t slot,
962                         int32_t seat_slot,
963                         double x,
964                         double y)
965 {
966         struct libinput_event_touch *touch_event;
967
968         touch_event = zalloc(sizeof *touch_event);
969         if (!touch_event)
970                 return;
971
972         *touch_event = (struct libinput_event_touch) {
973                 .time = time,
974                 .slot = slot,
975                 .seat_slot = seat_slot,
976                 .x = x,
977                 .y = y,
978         };
979
980         post_device_event(device,
981                           LIBINPUT_EVENT_TOUCH_DOWN,
982                           &touch_event->base);
983 }
984
985 void
986 touch_notify_touch_motion(struct libinput_device *device,
987                           uint32_t time,
988                           int32_t slot,
989                           int32_t seat_slot,
990                           double x,
991                           double y)
992 {
993         struct libinput_event_touch *touch_event;
994
995         touch_event = zalloc(sizeof *touch_event);
996         if (!touch_event)
997                 return;
998
999         *touch_event = (struct libinput_event_touch) {
1000                 .time = time,
1001                 .slot = slot,
1002                 .seat_slot = seat_slot,
1003                 .x = x,
1004                 .y = y,
1005         };
1006
1007         post_device_event(device,
1008                           LIBINPUT_EVENT_TOUCH_MOTION,
1009                           &touch_event->base);
1010 }
1011
1012 void
1013 touch_notify_touch_up(struct libinput_device *device,
1014                       uint32_t time,
1015                       int32_t slot,
1016                       int32_t seat_slot)
1017 {
1018         struct libinput_event_touch *touch_event;
1019
1020         touch_event = zalloc(sizeof *touch_event);
1021         if (!touch_event)
1022                 return;
1023
1024         *touch_event = (struct libinput_event_touch) {
1025                 .time = time,
1026                 .slot = slot,
1027                 .seat_slot = seat_slot,
1028         };
1029
1030         post_device_event(device,
1031                           LIBINPUT_EVENT_TOUCH_UP,
1032                           &touch_event->base);
1033 }
1034
1035 void
1036 touch_notify_frame(struct libinput_device *device,
1037                    uint32_t time)
1038 {
1039         struct libinput_event_touch *touch_event;
1040
1041         touch_event = zalloc(sizeof *touch_event);
1042         if (!touch_event)
1043                 return;
1044
1045         *touch_event = (struct libinput_event_touch) {
1046                 .time = time,
1047         };
1048
1049         post_device_event(device,
1050                           LIBINPUT_EVENT_TOUCH_FRAME,
1051                           &touch_event->base);
1052 }
1053
1054 static void
1055 libinput_post_event(struct libinput *libinput,
1056                     struct libinput_event *event)
1057 {
1058         struct libinput_event **events = libinput->events;
1059         size_t events_len = libinput->events_len;
1060         size_t events_count = libinput->events_count;
1061         size_t move_len;
1062         size_t new_out;
1063
1064         events_count++;
1065         if (events_count > events_len) {
1066                 events_len *= 2;
1067                 events = realloc(events, events_len * sizeof *events);
1068                 if (!events) {
1069                         fprintf(stderr, "Failed to reallocate event ring "
1070                                 "buffer");
1071                         return;
1072                 }
1073
1074                 if (libinput->events_count > 0 && libinput->events_in == 0) {
1075                         libinput->events_in = libinput->events_len;
1076                 } else if (libinput->events_count > 0 &&
1077                            libinput->events_out >= libinput->events_in) {
1078                         move_len = libinput->events_len - libinput->events_out;
1079                         new_out = events_len - move_len;
1080                         memmove(events + new_out,
1081                                 events + libinput->events_out,
1082                                 move_len * sizeof *events);
1083                         libinput->events_out = new_out;
1084                 }
1085
1086                 libinput->events = events;
1087                 libinput->events_len = events_len;
1088         }
1089
1090         if (event->device)
1091                 libinput_device_ref(event->device);
1092
1093         libinput->events_count = events_count;
1094         events[libinput->events_in] = event;
1095         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
1096 }
1097
1098 LIBINPUT_EXPORT struct libinput_event *
1099 libinput_get_event(struct libinput *libinput)
1100 {
1101         struct libinput_event *event;
1102
1103         if (libinput->events_count == 0)
1104                 return NULL;
1105
1106         event = libinput->events[libinput->events_out];
1107         libinput->events_out =
1108                 (libinput->events_out + 1) % libinput->events_len;
1109         libinput->events_count--;
1110
1111         return event;
1112 }
1113
1114 LIBINPUT_EXPORT enum libinput_event_type
1115 libinput_next_event_type(struct libinput *libinput)
1116 {
1117         struct libinput_event *event;
1118
1119         if (libinput->events_count == 0)
1120                 return LIBINPUT_EVENT_NONE;
1121
1122         event = libinput->events[libinput->events_out];
1123         return event->type;
1124 }
1125
1126 LIBINPUT_EXPORT void *
1127 libinput_get_user_data(struct libinput *libinput)
1128 {
1129         return libinput->user_data;
1130 }
1131
1132 LIBINPUT_EXPORT int
1133 libinput_resume(struct libinput *libinput)
1134 {
1135         return libinput->interface_backend->resume(libinput);
1136 }
1137
1138 LIBINPUT_EXPORT void
1139 libinput_suspend(struct libinput *libinput)
1140 {
1141         libinput->interface_backend->suspend(libinput);
1142 }
1143
1144 LIBINPUT_EXPORT void
1145 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
1146 {
1147         device->user_data = user_data;
1148 }
1149
1150 LIBINPUT_EXPORT void *
1151 libinput_device_get_user_data(struct libinput_device *device)
1152 {
1153         return device->user_data;
1154 }
1155
1156 LIBINPUT_EXPORT const char *
1157 libinput_device_get_sysname(struct libinput_device *device)
1158 {
1159         return evdev_device_get_sysname((struct evdev_device *) device);
1160 }
1161
1162 LIBINPUT_EXPORT const char *
1163 libinput_device_get_name(struct libinput_device *device)
1164 {
1165         return evdev_device_get_name((struct evdev_device *) device);
1166 }
1167
1168 LIBINPUT_EXPORT unsigned int
1169 libinput_device_get_id_product(struct libinput_device *device)
1170 {
1171         return evdev_device_get_id_product((struct evdev_device *) device);
1172 }
1173
1174 LIBINPUT_EXPORT unsigned int
1175 libinput_device_get_id_vendor(struct libinput_device *device)
1176 {
1177         return evdev_device_get_id_vendor((struct evdev_device *) device);
1178 }
1179
1180 LIBINPUT_EXPORT const char *
1181 libinput_device_get_output_name(struct libinput_device *device)
1182 {
1183         return evdev_device_get_output((struct evdev_device *) device);
1184 }
1185
1186 LIBINPUT_EXPORT struct libinput_seat *
1187 libinput_device_get_seat(struct libinput_device *device)
1188 {
1189         return device->seat;
1190 }
1191
1192 LIBINPUT_EXPORT void
1193 libinput_device_led_update(struct libinput_device *device,
1194                            enum libinput_led leds)
1195 {
1196         evdev_device_led_update((struct evdev_device *) device, leds);
1197 }
1198
1199 LIBINPUT_EXPORT int
1200 libinput_device_get_keys(struct libinput_device *device,
1201                          char *keys, size_t size)
1202 {
1203         return evdev_device_get_keys((struct evdev_device *) device,
1204                                      keys,
1205                                      size);
1206 }
1207
1208 LIBINPUT_EXPORT void
1209 libinput_device_calibrate(struct libinput_device *device,
1210                           float calibration[6])
1211 {
1212         evdev_device_calibrate((struct evdev_device *) device, calibration);
1213 }
1214
1215 LIBINPUT_EXPORT int
1216 libinput_device_has_capability(struct libinput_device *device,
1217                                enum libinput_device_capability capability)
1218 {
1219         return evdev_device_has_capability((struct evdev_device *) device,
1220                                            capability);
1221 }
1222
1223 LIBINPUT_EXPORT int
1224 libinput_device_get_size(struct libinput_device *device,
1225                          double *width,
1226                          double *height)
1227 {
1228         return evdev_device_get_size((struct evdev_device *)device,
1229                                      width,
1230                                      height);
1231 }
1232
1233 LIBINPUT_EXPORT struct libinput_event *
1234 libinput_event_device_notify_get_base_event(struct libinput_event_device_notify *event)
1235 {
1236         return &event->base;
1237 }
1238
1239 LIBINPUT_EXPORT struct libinput_event *
1240 libinput_event_keyboard_get_base_event(struct libinput_event_keyboard *event)
1241 {
1242         return &event->base;
1243 }
1244
1245 LIBINPUT_EXPORT struct libinput_event *
1246 libinput_event_pointer_get_base_event(struct libinput_event_pointer *event)
1247 {
1248         return &event->base;
1249 }
1250
1251 LIBINPUT_EXPORT struct libinput_event *
1252 libinput_event_touch_get_base_event(struct libinput_event_touch *event)
1253 {
1254         return &event->base;
1255 }
1256
1257 LIBINPUT_EXPORT const char *
1258 libinput_config_status_to_str(enum libinput_config_status status)
1259 {
1260         const char *str = NULL;
1261
1262         switch(status) {
1263         case LIBINPUT_CONFIG_STATUS_SUCCESS:
1264                 str = "Success";
1265                 break;
1266         case LIBINPUT_CONFIG_STATUS_UNSUPPORTED:
1267                 str = "Unsupported configuration option";
1268                 break;
1269         case LIBINPUT_CONFIG_STATUS_INVALID:
1270                 str = "Invalid argument range";
1271                 break;
1272         }
1273
1274         return str;
1275 }
1276
1277 LIBINPUT_EXPORT int
1278 libinput_device_config_tap_get_finger_count(struct libinput_device *device)
1279 {
1280         return device->config.tap ? device->config.tap->count(device) : 0;
1281 }
1282
1283 LIBINPUT_EXPORT enum libinput_config_status
1284 libinput_device_config_tap_set_enabled(struct libinput_device *device,
1285                                        enum libinput_config_tap_state enable)
1286 {
1287         if (enable != LIBINPUT_CONFIG_TAP_ENABLED &&
1288             enable != LIBINPUT_CONFIG_TAP_DISABLED)
1289                 return LIBINPUT_CONFIG_STATUS_INVALID;
1290
1291         if (enable &&
1292             libinput_device_config_tap_get_finger_count(device) == 0)
1293                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1294
1295         return device->config.tap->set_enabled(device, enable);
1296 }
1297
1298 LIBINPUT_EXPORT enum libinput_config_tap_state
1299 libinput_device_config_tap_get_enabled(struct libinput_device *device)
1300 {
1301         if (libinput_device_config_tap_get_finger_count(device) == 0)
1302                 return LIBINPUT_CONFIG_TAP_DISABLED;
1303
1304         return device->config.tap->get_enabled(device);
1305 }
1306
1307 LIBINPUT_EXPORT enum libinput_config_tap_state
1308 libinput_device_config_tap_get_default_enabled(struct libinput_device *device)
1309 {
1310         if (libinput_device_config_tap_get_finger_count(device) == 0)
1311                 return LIBINPUT_CONFIG_TAP_DISABLED;
1312
1313         return device->config.tap->get_default(device);
1314 }
1315
1316 LIBINPUT_EXPORT int
1317 libinput_device_config_calibration_has_matrix(struct libinput_device *device)
1318 {
1319         return device->config.calibration ?
1320                 device->config.calibration->has_matrix(device) : 0;
1321 }
1322
1323 LIBINPUT_EXPORT enum libinput_config_status
1324 libinput_device_config_calibration_set_matrix(struct libinput_device *device,
1325                                               const float matrix[6])
1326 {
1327         if (!libinput_device_config_calibration_has_matrix(device))
1328                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1329
1330         return device->config.calibration->set_matrix(device, matrix);
1331 }
1332
1333 LIBINPUT_EXPORT int
1334 libinput_device_config_calibration_get_matrix(struct libinput_device *device,
1335                                               float matrix[6])
1336 {
1337         if (!libinput_device_config_calibration_has_matrix(device))
1338                 return 0;
1339
1340         return device->config.calibration->get_matrix(device, matrix);
1341 }
1342
1343 LIBINPUT_EXPORT int
1344 libinput_device_config_calibration_get_default_matrix(struct libinput_device *device,
1345                                                       float matrix[6])
1346 {
1347         if (!libinput_device_config_calibration_has_matrix(device))
1348                 return 0;
1349
1350         return device->config.calibration->get_default_matrix(device, matrix);
1351 }
1352
1353 LIBINPUT_EXPORT uint32_t
1354 libinput_device_config_send_events_get_modes(struct libinput_device *device)
1355 {
1356         uint32_t modes = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1357
1358         if (device->config.sendevents)
1359                 modes |= device->config.sendevents->get_modes(device);
1360
1361         return modes;
1362 }
1363
1364 LIBINPUT_EXPORT enum libinput_config_status
1365 libinput_device_config_send_events_set_mode(struct libinput_device *device,
1366                                             enum libinput_config_send_events_mode mode)
1367 {
1368         if ((libinput_device_config_send_events_get_modes(device) & mode) == 0)
1369                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1370
1371         if (device->config.sendevents)
1372                 return device->config.sendevents->set_mode(device, mode);
1373         else /* mode must be _ENABLED to get here */
1374                 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1375 }
1376
1377 LIBINPUT_EXPORT enum libinput_config_send_events_mode
1378 libinput_device_config_send_events_get_mode(struct libinput_device *device)
1379 {
1380         if (device->config.sendevents)
1381                 return device->config.sendevents->get_mode(device);
1382         else
1383                 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1384 }
1385
1386 LIBINPUT_EXPORT enum libinput_config_send_events_mode
1387 libinput_device_config_send_events_get_default_mode(struct libinput_device *device)
1388 {
1389         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1390 }
1391
1392
1393 LIBINPUT_EXPORT int
1394 libinput_device_config_accel_is_available(struct libinput_device *device)
1395 {
1396         return device->config.accel ?
1397                 device->config.accel->available(device) : 0;
1398 }
1399
1400 LIBINPUT_EXPORT enum libinput_config_status
1401 libinput_device_config_accel_set_speed(struct libinput_device *device,
1402                                        double speed)
1403 {
1404         if (!libinput_device_config_accel_is_available(device))
1405                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1406
1407         if (speed < -1.0 || speed > 1.0)
1408                 return LIBINPUT_CONFIG_STATUS_INVALID;
1409
1410         return device->config.accel->set_speed(device, speed);
1411 }
1412
1413 LIBINPUT_EXPORT double
1414 libinput_device_config_accel_get_speed(struct libinput_device *device)
1415 {
1416         if (!libinput_device_config_accel_is_available(device))
1417                 return 0;
1418
1419         return device->config.accel->get_speed(device);
1420 }
1421
1422 LIBINPUT_EXPORT double
1423 libinput_device_config_accel_get_default_speed(struct libinput_device *device)
1424 {
1425         if (!libinput_device_config_accel_is_available(device))
1426                 return 0;
1427
1428         return device->config.accel->get_default_speed(device);
1429 }
1430
1431 LIBINPUT_EXPORT int
1432 libinput_device_config_scroll_has_natural_scroll(struct libinput_device *device)
1433 {
1434         if (!device->config.natural_scroll)
1435                 return 0;
1436
1437         return device->config.natural_scroll->has(device);
1438 }
1439
1440 LIBINPUT_EXPORT enum libinput_config_status
1441 libinput_device_config_scroll_set_natural_scroll_enabled(struct libinput_device *device,
1442                                                          int enabled)
1443 {
1444         if (!libinput_device_config_scroll_has_natural_scroll(device))
1445                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1446
1447         return device->config.natural_scroll->set_enabled(device, enabled);
1448 }
1449
1450 LIBINPUT_EXPORT int
1451 libinput_device_config_scroll_get_natural_scroll_enabled(struct libinput_device *device)
1452 {
1453         if (!device->config.natural_scroll)
1454                 return 0;
1455
1456         return device->config.natural_scroll->get_enabled(device);
1457 }
1458
1459 LIBINPUT_EXPORT int
1460 libinput_device_config_scroll_get_default_natural_scroll_enabled(struct libinput_device *device)
1461 {
1462         if (!device->config.natural_scroll)
1463                 return 0;
1464
1465         return device->config.natural_scroll->get_default_enabled(device);
1466 }
1467
1468 LIBINPUT_EXPORT int
1469 libinput_device_config_buttons_has_left_handed(struct libinput_device *device)
1470 {
1471         if (!device->config.left_handed)
1472                 return 0;
1473
1474         return device->config.left_handed->has(device);
1475 }
1476
1477 LIBINPUT_EXPORT enum libinput_config_status
1478 libinput_device_config_buttons_set_left_handed(struct libinput_device *device,
1479                                                int left_handed)
1480 {
1481         if (!libinput_device_config_buttons_has_left_handed(device))
1482                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1483
1484         return device->config.left_handed->set(device, left_handed);
1485 }
1486
1487 LIBINPUT_EXPORT int
1488 libinput_device_config_buttons_get_left_handed(struct libinput_device *device)
1489 {
1490         if (!libinput_device_config_buttons_has_left_handed(device))
1491                 return 0;
1492
1493         return device->config.left_handed->get(device);
1494 }
1495
1496 LIBINPUT_EXPORT int
1497 libinput_device_config_buttons_get_default_left_handed(struct libinput_device *device)
1498 {
1499         if (!libinput_device_config_buttons_has_left_handed(device))
1500                 return 0;
1501
1502         return device->config.left_handed->get_default(device);
1503 }