f384f4396b9bd98933b7948f0fa945e6118299d5
[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_keyboard_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(enum libinput_log_priority priority,
85                           void *data,
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 struct log_data {
102         enum libinput_log_priority priority;
103         libinput_log_handler handler;
104         void *user_data;
105 };
106
107 static struct log_data log_data = {
108         .priority = LIBINPUT_LOG_PRIORITY_ERROR,
109         .handler = libinput_default_log_func,
110         .user_data = NULL,
111 };
112
113 void
114 log_msg_va(enum libinput_log_priority priority,
115            const char *format,
116            va_list args)
117 {
118         if (log_data.handler && log_data.priority <= priority)
119                 log_data.handler(priority, log_data.user_data, format, args);
120 }
121
122 void
123 log_msg(enum libinput_log_priority priority, const char *format, ...)
124 {
125         va_list args;
126
127         va_start(args, format);
128         log_msg_va(priority, format, args);
129         va_end(args);
130 }
131
132 LIBINPUT_EXPORT void
133 libinput_log_set_priority(enum libinput_log_priority priority)
134 {
135         log_data.priority = priority;
136 }
137
138 LIBINPUT_EXPORT enum libinput_log_priority
139 libinput_log_get_priority(void)
140 {
141         return log_data.priority;
142 }
143
144 LIBINPUT_EXPORT void
145 libinput_log_set_handler(libinput_log_handler log_handler,
146                          void *user_data)
147 {
148         log_data.handler = log_handler;
149         log_data.user_data = user_data;
150 }
151
152 static void
153 libinput_post_event(struct libinput *libinput,
154                     struct libinput_event *event);
155
156 LIBINPUT_EXPORT enum libinput_event_type
157 libinput_event_get_type(struct libinput_event *event)
158 {
159         return event->type;
160 }
161
162 LIBINPUT_EXPORT struct libinput *
163 libinput_event_get_context(struct libinput_event *event)
164 {
165         return event->device->seat->libinput;
166 }
167
168 LIBINPUT_EXPORT struct libinput_device *
169 libinput_event_get_device(struct libinput_event *event)
170 {
171         return event->device;
172 }
173
174 LIBINPUT_EXPORT struct libinput_event_pointer *
175 libinput_event_get_pointer_event(struct libinput_event *event)
176 {
177         switch (event->type) {
178         case LIBINPUT_EVENT_NONE:
179                 abort(); /* not used as actual event type */
180         case LIBINPUT_EVENT_DEVICE_ADDED:
181         case LIBINPUT_EVENT_DEVICE_REMOVED:
182         case LIBINPUT_EVENT_KEYBOARD_KEY:
183                 break;
184         case LIBINPUT_EVENT_POINTER_MOTION:
185         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
186         case LIBINPUT_EVENT_POINTER_BUTTON:
187         case LIBINPUT_EVENT_POINTER_AXIS:
188                 return (struct libinput_event_pointer *) event;
189         case LIBINPUT_EVENT_TOUCH_DOWN:
190         case LIBINPUT_EVENT_TOUCH_UP:
191         case LIBINPUT_EVENT_TOUCH_MOTION:
192         case LIBINPUT_EVENT_TOUCH_CANCEL:
193         case LIBINPUT_EVENT_TOUCH_FRAME:
194                 break;
195         }
196
197         return NULL;
198 }
199
200 LIBINPUT_EXPORT struct libinput_event_keyboard *
201 libinput_event_get_keyboard_event(struct libinput_event *event)
202 {
203         switch (event->type) {
204         case LIBINPUT_EVENT_NONE:
205                 abort(); /* not used as actual event type */
206         case LIBINPUT_EVENT_DEVICE_ADDED:
207         case LIBINPUT_EVENT_DEVICE_REMOVED:
208                 break;
209         case LIBINPUT_EVENT_KEYBOARD_KEY:
210                 return (struct libinput_event_keyboard *) event;
211         case LIBINPUT_EVENT_POINTER_MOTION:
212         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
213         case LIBINPUT_EVENT_POINTER_BUTTON:
214         case LIBINPUT_EVENT_POINTER_AXIS:
215         case LIBINPUT_EVENT_TOUCH_DOWN:
216         case LIBINPUT_EVENT_TOUCH_UP:
217         case LIBINPUT_EVENT_TOUCH_MOTION:
218         case LIBINPUT_EVENT_TOUCH_CANCEL:
219         case LIBINPUT_EVENT_TOUCH_FRAME:
220                 break;
221         }
222
223         return NULL;
224 }
225
226 LIBINPUT_EXPORT struct libinput_event_touch *
227 libinput_event_get_touch_event(struct libinput_event *event)
228 {
229         switch (event->type) {
230         case LIBINPUT_EVENT_NONE:
231                 abort(); /* not used as actual event type */
232         case LIBINPUT_EVENT_DEVICE_ADDED:
233         case LIBINPUT_EVENT_DEVICE_REMOVED:
234         case LIBINPUT_EVENT_KEYBOARD_KEY:
235         case LIBINPUT_EVENT_POINTER_MOTION:
236         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
237         case LIBINPUT_EVENT_POINTER_BUTTON:
238         case LIBINPUT_EVENT_POINTER_AXIS:
239                 break;
240         case LIBINPUT_EVENT_TOUCH_DOWN:
241         case LIBINPUT_EVENT_TOUCH_UP:
242         case LIBINPUT_EVENT_TOUCH_MOTION:
243         case LIBINPUT_EVENT_TOUCH_CANCEL:
244         case LIBINPUT_EVENT_TOUCH_FRAME:
245                 return (struct libinput_event_touch *) event;
246         }
247
248         return NULL;
249 }
250
251 LIBINPUT_EXPORT struct libinput_event_device_notify *
252 libinput_event_get_device_notify_event(struct libinput_event *event)
253 {
254         switch (event->type) {
255         case LIBINPUT_EVENT_NONE:
256                 abort(); /* not used as actual event type */
257         case LIBINPUT_EVENT_DEVICE_ADDED:
258         case LIBINPUT_EVENT_DEVICE_REMOVED:
259                 return (struct libinput_event_device_notify *) event;
260         case LIBINPUT_EVENT_KEYBOARD_KEY:
261         case LIBINPUT_EVENT_POINTER_MOTION:
262         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
263         case LIBINPUT_EVENT_POINTER_BUTTON:
264         case LIBINPUT_EVENT_POINTER_AXIS:
265         case LIBINPUT_EVENT_TOUCH_DOWN:
266         case LIBINPUT_EVENT_TOUCH_UP:
267         case LIBINPUT_EVENT_TOUCH_MOTION:
268         case LIBINPUT_EVENT_TOUCH_CANCEL:
269         case LIBINPUT_EVENT_TOUCH_FRAME:
270                 break;
271         }
272
273         return NULL;
274 }
275
276 LIBINPUT_EXPORT uint32_t
277 libinput_event_keyboard_get_time(struct libinput_event_keyboard *event)
278 {
279         return event->time;
280 }
281
282 LIBINPUT_EXPORT uint32_t
283 libinput_event_keyboard_get_key(struct libinput_event_keyboard *event)
284 {
285         return event->key;
286 }
287
288 LIBINPUT_EXPORT enum libinput_keyboard_key_state
289 libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event)
290 {
291         return event->state;
292 }
293
294 LIBINPUT_EXPORT uint32_t
295 libinput_event_keyboard_get_seat_key_count(
296         struct libinput_event_keyboard *event)
297 {
298         return event->seat_key_count;
299 }
300
301 LIBINPUT_EXPORT uint32_t
302 libinput_event_pointer_get_time(struct libinput_event_pointer *event)
303 {
304         return event->time;
305 }
306
307 LIBINPUT_EXPORT double
308 libinput_event_pointer_get_dx(struct libinput_event_pointer *event)
309 {
310         return event->x;
311 }
312
313 LIBINPUT_EXPORT double
314 libinput_event_pointer_get_dy(struct libinput_event_pointer *event)
315 {
316         return event->y;
317 }
318
319 LIBINPUT_EXPORT double
320 libinput_event_pointer_get_absolute_x(struct libinput_event_pointer *event)
321 {
322         struct evdev_device *device =
323                 (struct evdev_device *) event->base.device;
324
325         return evdev_convert_to_mm(device->abs.absinfo_x, event->x);
326 }
327
328 LIBINPUT_EXPORT double
329 libinput_event_pointer_get_absolute_y(struct libinput_event_pointer *event)
330 {
331         struct evdev_device *device =
332                 (struct evdev_device *) event->base.device;
333
334         return evdev_convert_to_mm(device->abs.absinfo_y, event->y);
335 }
336
337 LIBINPUT_EXPORT double
338 libinput_event_pointer_get_absolute_x_transformed(
339         struct libinput_event_pointer *event,
340         uint32_t width)
341 {
342         struct evdev_device *device =
343                 (struct evdev_device *) event->base.device;
344
345         return evdev_device_transform_x(device, event->x, width);
346 }
347
348 LIBINPUT_EXPORT double
349 libinput_event_pointer_get_absolute_y_transformed(
350         struct libinput_event_pointer *event,
351         uint32_t height)
352 {
353         struct evdev_device *device =
354                 (struct evdev_device *) event->base.device;
355
356         return evdev_device_transform_y(device, event->y, height);
357 }
358
359 LIBINPUT_EXPORT uint32_t
360 libinput_event_pointer_get_button(struct libinput_event_pointer *event)
361 {
362         return event->button;
363 }
364
365 LIBINPUT_EXPORT enum libinput_button_state
366 libinput_event_pointer_get_button_state(struct libinput_event_pointer *event)
367 {
368         return event->state;
369 }
370
371 LIBINPUT_EXPORT uint32_t
372 libinput_event_pointer_get_seat_button_count(
373         struct libinput_event_pointer *event)
374 {
375         return event->seat_button_count;
376 }
377
378 LIBINPUT_EXPORT enum libinput_pointer_axis
379 libinput_event_pointer_get_axis(struct libinput_event_pointer *event)
380 {
381         return event->axis;
382 }
383
384 LIBINPUT_EXPORT double
385 libinput_event_pointer_get_axis_value(struct libinput_event_pointer *event)
386 {
387         return event->value;
388 }
389
390 LIBINPUT_EXPORT uint32_t
391 libinput_event_touch_get_time(struct libinput_event_touch *event)
392 {
393         return event->time;
394 }
395
396 LIBINPUT_EXPORT int32_t
397 libinput_event_touch_get_slot(struct libinput_event_touch *event)
398 {
399         return event->slot;
400 }
401
402 LIBINPUT_EXPORT int32_t
403 libinput_event_touch_get_seat_slot(struct libinput_event_touch *event)
404 {
405         return event->seat_slot;
406 }
407
408 LIBINPUT_EXPORT double
409 libinput_event_touch_get_x(struct libinput_event_touch *event)
410 {
411         struct evdev_device *device =
412                 (struct evdev_device *) event->base.device;
413
414         return evdev_convert_to_mm(device->abs.absinfo_x, event->x);
415 }
416
417 LIBINPUT_EXPORT double
418 libinput_event_touch_get_x_transformed(struct libinput_event_touch *event,
419                                        uint32_t width)
420 {
421         struct evdev_device *device =
422                 (struct evdev_device *) event->base.device;
423
424         return evdev_device_transform_x(device, event->x, width);
425 }
426
427 LIBINPUT_EXPORT double
428 libinput_event_touch_get_y_transformed(struct libinput_event_touch *event,
429                                        uint32_t height)
430 {
431         struct evdev_device *device =
432                 (struct evdev_device *) event->base.device;
433
434         return evdev_device_transform_y(device, event->y, height);
435 }
436
437 LIBINPUT_EXPORT double
438 libinput_event_touch_get_y(struct libinput_event_touch *event)
439 {
440         struct evdev_device *device =
441                 (struct evdev_device *) event->base.device;
442
443         return evdev_convert_to_mm(device->abs.absinfo_y, event->y);
444 }
445
446 struct libinput_source *
447 libinput_add_fd(struct libinput *libinput,
448                 int fd,
449                 libinput_source_dispatch_t dispatch,
450                 void *user_data)
451 {
452         struct libinput_source *source;
453         struct epoll_event ep;
454
455         source = malloc(sizeof *source);
456         if (!source)
457                 return NULL;
458
459         source->dispatch = dispatch;
460         source->user_data = user_data;
461         source->fd = fd;
462
463         memset(&ep, 0, sizeof ep);
464         ep.events = EPOLLIN;
465         ep.data.ptr = source;
466
467         if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
468                 close(source->fd);
469                 free(source);
470                 return NULL;
471         }
472
473         return source;
474 }
475
476 void
477 libinput_remove_source(struct libinput *libinput,
478                        struct libinput_source *source)
479 {
480         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
481         source->fd = -1;
482         list_insert(&libinput->source_destroy_list, &source->link);
483 }
484
485 int
486 libinput_init(struct libinput *libinput,
487               const struct libinput_interface *interface,
488               const struct libinput_interface_backend *interface_backend,
489               void *user_data)
490 {
491         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
492         if (libinput->epoll_fd < 0)
493                 return -1;
494
495         libinput->events_len = 4;
496         libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
497         if (!libinput->events) {
498                 close(libinput->epoll_fd);
499                 return -1;
500         }
501
502         libinput->interface = interface;
503         libinput->interface_backend = interface_backend;
504         libinput->user_data = user_data;
505         list_init(&libinput->source_destroy_list);
506         list_init(&libinput->seat_list);
507
508         if (libinput_timer_subsys_init(libinput) != 0) {
509                 free(libinput->events);
510                 close(libinput->epoll_fd);
511                 return -1;
512         }
513
514         return 0;
515 }
516
517 static void
518 libinput_device_destroy(struct libinput_device *device);
519
520 static void
521 libinput_seat_destroy(struct libinput_seat *seat);
522
523 static void
524 libinput_drop_destroyed_sources(struct libinput *libinput)
525 {
526         struct libinput_source *source, *next;
527
528         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
529                 free(source);
530         list_init(&libinput->source_destroy_list);
531 }
532
533 LIBINPUT_EXPORT void
534 libinput_destroy(struct libinput *libinput)
535 {
536         struct libinput_event *event;
537         struct libinput_device *device, *next_device;
538         struct libinput_seat *seat, *next_seat;
539
540         if (libinput == NULL)
541                 return;
542
543         libinput_suspend(libinput);
544
545         libinput->interface_backend->destroy(libinput);
546
547         while ((event = libinput_get_event(libinput)))
548                libinput_event_destroy(event);
549
550         free(libinput->events);
551
552         list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
553                 list_for_each_safe(device, next_device,
554                                    &seat->devices_list,
555                                    link)
556                         libinput_device_destroy(device);
557
558                 libinput_seat_destroy(seat);
559         }
560
561         libinput_timer_subsys_destroy(libinput);
562         libinput_drop_destroyed_sources(libinput);
563         close(libinput->epoll_fd);
564         free(libinput);
565 }
566
567 LIBINPUT_EXPORT void
568 libinput_event_destroy(struct libinput_event *event)
569 {
570         if (event == NULL)
571                 return;
572
573         if (event->device)
574                 libinput_device_unref(event->device);
575
576         free(event);
577 }
578
579 int
580 open_restricted(struct libinput *libinput,
581                 const char *path, int flags)
582 {
583         return libinput->interface->open_restricted(path,
584                                                     flags,
585                                                     libinput->user_data);
586 }
587
588 void
589 close_restricted(struct libinput *libinput, int fd)
590 {
591         return libinput->interface->close_restricted(fd, libinput->user_data);
592 }
593
594 void
595 libinput_seat_init(struct libinput_seat *seat,
596                    struct libinput *libinput,
597                    const char *physical_name,
598                    const char *logical_name,
599                    libinput_seat_destroy_func destroy)
600 {
601         seat->refcount = 1;
602         seat->libinput = libinput;
603         seat->physical_name = strdup(physical_name);
604         seat->logical_name = strdup(logical_name);
605         seat->destroy = destroy;
606         list_init(&seat->devices_list);
607         list_insert(&libinput->seat_list, &seat->link);
608 }
609
610 LIBINPUT_EXPORT void
611 libinput_seat_ref(struct libinput_seat *seat)
612 {
613         seat->refcount++;
614 }
615
616 static void
617 libinput_seat_destroy(struct libinput_seat *seat)
618 {
619         list_remove(&seat->link);
620         free(seat->logical_name);
621         free(seat->physical_name);
622         seat->destroy(seat);
623 }
624
625 LIBINPUT_EXPORT void
626 libinput_seat_unref(struct libinput_seat *seat)
627 {
628         assert(seat->refcount > 0);
629         seat->refcount--;
630         if (seat->refcount == 0)
631                 libinput_seat_destroy(seat);
632 }
633
634 LIBINPUT_EXPORT void
635 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
636 {
637         seat->user_data = user_data;
638 }
639
640 LIBINPUT_EXPORT void *
641 libinput_seat_get_user_data(struct libinput_seat *seat)
642 {
643         return seat->user_data;
644 }
645
646 LIBINPUT_EXPORT const char *
647 libinput_seat_get_physical_name(struct libinput_seat *seat)
648 {
649         return seat->physical_name;
650 }
651
652 LIBINPUT_EXPORT const char *
653 libinput_seat_get_logical_name(struct libinput_seat *seat)
654 {
655         return seat->logical_name;
656 }
657
658 void
659 libinput_device_init(struct libinput_device *device,
660                      struct libinput_seat *seat)
661 {
662         device->seat = seat;
663         device->refcount = 1;
664 }
665
666 LIBINPUT_EXPORT void
667 libinput_device_ref(struct libinput_device *device)
668 {
669         device->refcount++;
670 }
671
672 static void
673 libinput_device_destroy(struct libinput_device *device)
674 {
675         evdev_device_destroy((struct evdev_device *) device);
676 }
677
678 LIBINPUT_EXPORT void
679 libinput_device_unref(struct libinput_device *device)
680 {
681         assert(device->refcount > 0);
682         device->refcount--;
683         if (device->refcount == 0)
684                 libinput_device_destroy(device);
685 }
686
687 LIBINPUT_EXPORT int
688 libinput_get_fd(struct libinput *libinput)
689 {
690         return libinput->epoll_fd;
691 }
692
693 LIBINPUT_EXPORT int
694 libinput_dispatch(struct libinput *libinput)
695 {
696         struct libinput_source *source;
697         struct epoll_event ep[32];
698         int i, count;
699
700         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
701         if (count < 0)
702                 return -errno;
703
704         for (i = 0; i < count; ++i) {
705                 source = ep[i].data.ptr;
706                 if (source->fd == -1)
707                         continue;
708
709                 source->dispatch(source->user_data);
710         }
711
712         libinput_drop_destroyed_sources(libinput);
713
714         return 0;
715 }
716
717 static uint32_t
718 update_seat_key_count(struct libinput_seat *seat,
719                       int32_t key,
720                       enum libinput_keyboard_key_state state)
721 {
722         assert(key >= 0 && key <= KEY_MAX);
723
724         switch (state) {
725         case LIBINPUT_KEYBOARD_KEY_STATE_PRESSED:
726                 return ++seat->button_count[key];
727         case LIBINPUT_KEYBOARD_KEY_STATE_RELEASED:
728                 /* We might not have received the first PRESSED event. */
729                 if (seat->button_count[key] == 0)
730                         return 0;
731
732                 return --seat->button_count[key];
733         }
734
735         return 0;
736 }
737
738 static uint32_t
739 update_seat_button_count(struct libinput_seat *seat,
740                          int32_t button,
741                          enum libinput_button_state state)
742 {
743         assert(button >= 0 && button <= KEY_MAX);
744
745         switch (state) {
746         case LIBINPUT_BUTTON_STATE_PRESSED:
747                 return ++seat->button_count[button];
748         case LIBINPUT_BUTTON_STATE_RELEASED:
749                 /* We might not have received the first PRESSED event. */
750                 if (seat->button_count[button] == 0)
751                         return 0;
752
753                 return --seat->button_count[button];
754         }
755
756         return 0;
757 }
758
759 static void
760 init_event_base(struct libinput_event *event,
761                 struct libinput_device *device,
762                 enum libinput_event_type type)
763 {
764         event->type = type;
765         event->device = device;
766 }
767
768 static void
769 post_base_event(struct libinput_device *device,
770                 enum libinput_event_type type,
771                 struct libinput_event *event)
772 {
773         struct libinput *libinput = device->seat->libinput;
774         init_event_base(event, device, type);
775         libinput_post_event(libinput, event);
776 }
777
778 static void
779 post_device_event(struct libinput_device *device,
780                   enum libinput_event_type type,
781                   struct libinput_event *event)
782 {
783         init_event_base(event, device, type);
784         libinput_post_event(device->seat->libinput, event);
785 }
786
787 void
788 notify_added_device(struct libinput_device *device)
789 {
790         struct libinput_event_device_notify *added_device_event;
791
792         added_device_event = zalloc(sizeof *added_device_event);
793         if (!added_device_event)
794                 return;
795
796         post_base_event(device,
797                         LIBINPUT_EVENT_DEVICE_ADDED,
798                         &added_device_event->base);
799 }
800
801 void
802 notify_removed_device(struct libinput_device *device)
803 {
804         struct libinput_event_device_notify *removed_device_event;
805
806         removed_device_event = zalloc(sizeof *removed_device_event);
807         if (!removed_device_event)
808                 return;
809
810         post_base_event(device,
811                         LIBINPUT_EVENT_DEVICE_REMOVED,
812                         &removed_device_event->base);
813 }
814
815 void
816 keyboard_notify_key(struct libinput_device *device,
817                     uint32_t time,
818                     uint32_t key,
819                     enum libinput_keyboard_key_state state)
820 {
821         struct libinput_event_keyboard *key_event;
822         uint32_t seat_key_count;
823
824         key_event = zalloc(sizeof *key_event);
825         if (!key_event)
826                 return;
827
828         seat_key_count = update_seat_key_count(device->seat, key, state);
829
830         *key_event = (struct libinput_event_keyboard) {
831                 .time = time,
832                 .key = key,
833                 .state = state,
834                 .seat_key_count = seat_key_count,
835         };
836
837         post_device_event(device,
838                           LIBINPUT_EVENT_KEYBOARD_KEY,
839                           &key_event->base);
840 }
841
842 void
843 pointer_notify_motion(struct libinput_device *device,
844                       uint32_t time,
845                       double dx,
846                       double dy)
847 {
848         struct libinput_event_pointer *motion_event;
849
850         motion_event = zalloc(sizeof *motion_event);
851         if (!motion_event)
852                 return;
853
854         *motion_event = (struct libinput_event_pointer) {
855                 .time = time,
856                 .x = dx,
857                 .y = dy,
858         };
859
860         post_device_event(device,
861                           LIBINPUT_EVENT_POINTER_MOTION,
862                           &motion_event->base);
863 }
864
865 void
866 pointer_notify_motion_absolute(struct libinput_device *device,
867                                uint32_t time,
868                                double x,
869                                double y)
870 {
871         struct libinput_event_pointer *motion_absolute_event;
872
873         motion_absolute_event = zalloc(sizeof *motion_absolute_event);
874         if (!motion_absolute_event)
875                 return;
876
877         *motion_absolute_event = (struct libinput_event_pointer) {
878                 .time = time,
879                 .x = x,
880                 .y = y,
881         };
882
883         post_device_event(device,
884                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
885                           &motion_absolute_event->base);
886 }
887
888 void
889 pointer_notify_button(struct libinput_device *device,
890                       uint32_t time,
891                       int32_t button,
892                       enum libinput_button_state state)
893 {
894         struct libinput_event_pointer *button_event;
895         int32_t seat_button_count;
896
897         button_event = zalloc(sizeof *button_event);
898         if (!button_event)
899                 return;
900
901         seat_button_count = update_seat_button_count(device->seat,
902                                                      button,
903                                                      state);
904
905         *button_event = (struct libinput_event_pointer) {
906                 .time = time,
907                 .button = button,
908                 .state = state,
909                 .seat_button_count = seat_button_count,
910         };
911
912         post_device_event(device,
913                           LIBINPUT_EVENT_POINTER_BUTTON,
914                           &button_event->base);
915 }
916
917 void
918 pointer_notify_axis(struct libinput_device *device,
919                     uint32_t time,
920                     enum libinput_pointer_axis axis,
921                     double value)
922 {
923         struct libinput_event_pointer *axis_event;
924
925         axis_event = zalloc(sizeof *axis_event);
926         if (!axis_event)
927                 return;
928
929         *axis_event = (struct libinput_event_pointer) {
930                 .time = time,
931                 .axis = axis,
932                 .value = value,
933         };
934
935         post_device_event(device,
936                           LIBINPUT_EVENT_POINTER_AXIS,
937                           &axis_event->base);
938 }
939
940 void
941 touch_notify_touch_down(struct libinput_device *device,
942                         uint32_t time,
943                         int32_t slot,
944                         int32_t seat_slot,
945                         double x,
946                         double y)
947 {
948         struct libinput_event_touch *touch_event;
949
950         touch_event = zalloc(sizeof *touch_event);
951         if (!touch_event)
952                 return;
953
954         *touch_event = (struct libinput_event_touch) {
955                 .time = time,
956                 .slot = slot,
957                 .seat_slot = seat_slot,
958                 .x = x,
959                 .y = y,
960         };
961
962         post_device_event(device,
963                           LIBINPUT_EVENT_TOUCH_DOWN,
964                           &touch_event->base);
965 }
966
967 void
968 touch_notify_touch_motion(struct libinput_device *device,
969                           uint32_t time,
970                           int32_t slot,
971                           int32_t seat_slot,
972                           double x,
973                           double y)
974 {
975         struct libinput_event_touch *touch_event;
976
977         touch_event = zalloc(sizeof *touch_event);
978         if (!touch_event)
979                 return;
980
981         *touch_event = (struct libinput_event_touch) {
982                 .time = time,
983                 .slot = slot,
984                 .seat_slot = seat_slot,
985                 .x = x,
986                 .y = y,
987         };
988
989         post_device_event(device,
990                           LIBINPUT_EVENT_TOUCH_MOTION,
991                           &touch_event->base);
992 }
993
994 void
995 touch_notify_touch_up(struct libinput_device *device,
996                       uint32_t time,
997                       int32_t slot,
998                       int32_t seat_slot)
999 {
1000         struct libinput_event_touch *touch_event;
1001
1002         touch_event = zalloc(sizeof *touch_event);
1003         if (!touch_event)
1004                 return;
1005
1006         *touch_event = (struct libinput_event_touch) {
1007                 .time = time,
1008                 .slot = slot,
1009                 .seat_slot = seat_slot,
1010         };
1011
1012         post_device_event(device,
1013                           LIBINPUT_EVENT_TOUCH_UP,
1014                           &touch_event->base);
1015 }
1016
1017 void
1018 touch_notify_frame(struct libinput_device *device,
1019                    uint32_t time)
1020 {
1021         struct libinput_event_touch *touch_event;
1022
1023         touch_event = zalloc(sizeof *touch_event);
1024         if (!touch_event)
1025                 return;
1026
1027         *touch_event = (struct libinput_event_touch) {
1028                 .time = time,
1029         };
1030
1031         post_device_event(device,
1032                           LIBINPUT_EVENT_TOUCH_FRAME,
1033                           &touch_event->base);
1034 }
1035
1036
1037 static void
1038 libinput_post_event(struct libinput *libinput,
1039                     struct libinput_event *event)
1040 {
1041         struct libinput_event **events = libinput->events;
1042         size_t events_len = libinput->events_len;
1043         size_t events_count = libinput->events_count;
1044         size_t move_len;
1045         size_t new_out;
1046
1047         events_count++;
1048         if (events_count > events_len) {
1049                 events_len *= 2;
1050                 events = realloc(events, events_len * sizeof *events);
1051                 if (!events) {
1052                         fprintf(stderr, "Failed to reallocate event ring "
1053                                 "buffer");
1054                         return;
1055                 }
1056
1057                 if (libinput->events_count > 0 && libinput->events_in == 0) {
1058                         libinput->events_in = libinput->events_len;
1059                 } else if (libinput->events_count > 0 &&
1060                            libinput->events_out >= libinput->events_in) {
1061                         move_len = libinput->events_len - libinput->events_out;
1062                         new_out = events_len - move_len;
1063                         memmove(events + new_out,
1064                                 events + libinput->events_out,
1065                                 move_len * sizeof *events);
1066                         libinput->events_out = new_out;
1067                 }
1068
1069                 libinput->events = events;
1070                 libinput->events_len = events_len;
1071         }
1072
1073         if (event->device)
1074                 libinput_device_ref(event->device);
1075
1076         libinput->events_count = events_count;
1077         events[libinput->events_in] = event;
1078         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
1079 }
1080
1081 LIBINPUT_EXPORT struct libinput_event *
1082 libinput_get_event(struct libinput *libinput)
1083 {
1084         struct libinput_event *event;
1085
1086         if (libinput->events_count == 0)
1087                 return NULL;
1088
1089         event = libinput->events[libinput->events_out];
1090         libinput->events_out =
1091                 (libinput->events_out + 1) % libinput->events_len;
1092         libinput->events_count--;
1093
1094         return event;
1095 }
1096
1097 LIBINPUT_EXPORT enum libinput_event_type
1098 libinput_next_event_type(struct libinput *libinput)
1099 {
1100         struct libinput_event *event;
1101
1102         if (libinput->events_count == 0)
1103                 return LIBINPUT_EVENT_NONE;
1104
1105         event = libinput->events[libinput->events_out];
1106         return event->type;
1107 }
1108
1109 LIBINPUT_EXPORT void *
1110 libinput_get_user_data(struct libinput *libinput)
1111 {
1112         return libinput->user_data;
1113 }
1114
1115 LIBINPUT_EXPORT int
1116 libinput_resume(struct libinput *libinput)
1117 {
1118         return libinput->interface_backend->resume(libinput);
1119 }
1120
1121 LIBINPUT_EXPORT void
1122 libinput_suspend(struct libinput *libinput)
1123 {
1124         libinput->interface_backend->suspend(libinput);
1125 }
1126
1127 LIBINPUT_EXPORT void
1128 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
1129 {
1130         device->user_data = user_data;
1131 }
1132
1133 LIBINPUT_EXPORT void *
1134 libinput_device_get_user_data(struct libinput_device *device)
1135 {
1136         return device->user_data;
1137 }
1138
1139 LIBINPUT_EXPORT const char *
1140 libinput_device_get_sysname(struct libinput_device *device)
1141 {
1142         return evdev_device_get_sysname((struct evdev_device *) device);
1143 }
1144
1145 LIBINPUT_EXPORT const char *
1146 libinput_device_get_output_name(struct libinput_device *device)
1147 {
1148         return evdev_device_get_output((struct evdev_device *) device);
1149 }
1150
1151 LIBINPUT_EXPORT struct libinput_seat *
1152 libinput_device_get_seat(struct libinput_device *device)
1153 {
1154         return device->seat;
1155 }
1156
1157 LIBINPUT_EXPORT void
1158 libinput_device_led_update(struct libinput_device *device,
1159                            enum libinput_led leds)
1160 {
1161         evdev_device_led_update((struct evdev_device *) device, leds);
1162 }
1163
1164 LIBINPUT_EXPORT int
1165 libinput_device_get_keys(struct libinput_device *device,
1166                          char *keys, size_t size)
1167 {
1168         return evdev_device_get_keys((struct evdev_device *) device,
1169                                      keys,
1170                                      size);
1171 }
1172
1173 LIBINPUT_EXPORT void
1174 libinput_device_calibrate(struct libinput_device *device,
1175                           float calibration[6])
1176 {
1177         evdev_device_calibrate((struct evdev_device *) device, calibration);
1178 }
1179
1180 LIBINPUT_EXPORT int
1181 libinput_device_has_capability(struct libinput_device *device,
1182                                enum libinput_device_capability capability)
1183 {
1184         return evdev_device_has_capability((struct evdev_device *) device,
1185                                            capability);
1186 }
1187
1188 LIBINPUT_EXPORT struct libinput_event *
1189 libinput_event_device_notify_get_base_event(struct libinput_event_device_notify *event)
1190 {
1191         return &event->base;
1192 }
1193
1194 LIBINPUT_EXPORT struct libinput_event *
1195 libinput_event_keyboard_get_base_event(struct libinput_event_keyboard *event)
1196 {
1197         return &event->base;
1198 }
1199
1200 LIBINPUT_EXPORT struct libinput_event *
1201 libinput_event_pointer_get_base_event(struct libinput_event_pointer *event)
1202 {
1203         return &event->base;
1204 }
1205
1206 LIBINPUT_EXPORT struct libinput_event *
1207 libinput_event_touch_get_base_event(struct libinput_event_touch *event)
1208 {
1209         return &event->base;
1210 }