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