Use an enum to enable/disable tapping configuration
[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                 close(source->fd);
461                 free(source);
462                 return NULL;
463         }
464
465         return source;
466 }
467
468 void
469 libinput_remove_source(struct libinput *libinput,
470                        struct libinput_source *source)
471 {
472         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
473         source->fd = -1;
474         list_insert(&libinput->source_destroy_list, &source->link);
475 }
476
477 int
478 libinput_init(struct libinput *libinput,
479               const struct libinput_interface *interface,
480               const struct libinput_interface_backend *interface_backend,
481               void *user_data)
482 {
483         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
484         if (libinput->epoll_fd < 0)
485                 return -1;
486
487         libinput->events_len = 4;
488         libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
489         if (!libinput->events) {
490                 close(libinput->epoll_fd);
491                 return -1;
492         }
493
494         libinput->log_handler = libinput_default_log_func;
495         libinput->log_priority = LIBINPUT_LOG_PRIORITY_ERROR;
496         libinput->interface = interface;
497         libinput->interface_backend = interface_backend;
498         libinput->user_data = user_data;
499         libinput->refcount = 1;
500         list_init(&libinput->source_destroy_list);
501         list_init(&libinput->seat_list);
502
503         if (libinput_timer_subsys_init(libinput) != 0) {
504                 free(libinput->events);
505                 close(libinput->epoll_fd);
506                 return -1;
507         }
508
509         return 0;
510 }
511
512 static void
513 libinput_device_destroy(struct libinput_device *device);
514
515 static void
516 libinput_seat_destroy(struct libinput_seat *seat);
517
518 static void
519 libinput_drop_destroyed_sources(struct libinput *libinput)
520 {
521         struct libinput_source *source, *next;
522
523         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
524                 free(source);
525         list_init(&libinput->source_destroy_list);
526 }
527
528 LIBINPUT_EXPORT struct libinput *
529 libinput_ref(struct libinput *libinput)
530 {
531         libinput->refcount++;
532         return libinput;
533 }
534
535 LIBINPUT_EXPORT struct libinput *
536 libinput_unref(struct libinput *libinput)
537 {
538         struct libinput_event *event;
539         struct libinput_device *device, *next_device;
540         struct libinput_seat *seat, *next_seat;
541
542         if (libinput == NULL)
543                 return NULL;
544
545         assert(libinput->refcount > 0);
546         libinput->refcount--;
547         if (libinput->refcount > 0)
548                 return libinput;
549
550         libinput_suspend(libinput);
551
552         libinput->interface_backend->destroy(libinput);
553
554         while ((event = libinput_get_event(libinput)))
555                libinput_event_destroy(event);
556
557         free(libinput->events);
558
559         list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
560                 list_for_each_safe(device, next_device,
561                                    &seat->devices_list,
562                                    link)
563                         libinput_device_destroy(device);
564
565                 libinput_seat_destroy(seat);
566         }
567
568         libinput_timer_subsys_destroy(libinput);
569         libinput_drop_destroyed_sources(libinput);
570         close(libinput->epoll_fd);
571         free(libinput);
572
573         return NULL;
574 }
575
576 LIBINPUT_EXPORT void
577 libinput_event_destroy(struct libinput_event *event)
578 {
579         if (event == NULL)
580                 return;
581
582         if (event->device)
583                 libinput_device_unref(event->device);
584
585         free(event);
586 }
587
588 int
589 open_restricted(struct libinput *libinput,
590                 const char *path, int flags)
591 {
592         return libinput->interface->open_restricted(path,
593                                                     flags,
594                                                     libinput->user_data);
595 }
596
597 void
598 close_restricted(struct libinput *libinput, int fd)
599 {
600         return libinput->interface->close_restricted(fd, libinput->user_data);
601 }
602
603 void
604 libinput_seat_init(struct libinput_seat *seat,
605                    struct libinput *libinput,
606                    const char *physical_name,
607                    const char *logical_name,
608                    libinput_seat_destroy_func destroy)
609 {
610         seat->refcount = 1;
611         seat->libinput = libinput;
612         seat->physical_name = strdup(physical_name);
613         seat->logical_name = strdup(logical_name);
614         seat->destroy = destroy;
615         list_init(&seat->devices_list);
616         list_insert(&libinput->seat_list, &seat->link);
617 }
618
619 LIBINPUT_EXPORT struct libinput_seat *
620 libinput_seat_ref(struct libinput_seat *seat)
621 {
622         seat->refcount++;
623         return seat;
624 }
625
626 static void
627 libinput_seat_destroy(struct libinput_seat *seat)
628 {
629         list_remove(&seat->link);
630         free(seat->logical_name);
631         free(seat->physical_name);
632         seat->destroy(seat);
633 }
634
635 LIBINPUT_EXPORT struct libinput_seat *
636 libinput_seat_unref(struct libinput_seat *seat)
637 {
638         assert(seat->refcount > 0);
639         seat->refcount--;
640         if (seat->refcount == 0) {
641                 libinput_seat_destroy(seat);
642                 return NULL;
643         } else {
644                 return seat;
645         }
646 }
647
648 LIBINPUT_EXPORT void
649 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
650 {
651         seat->user_data = user_data;
652 }
653
654 LIBINPUT_EXPORT void *
655 libinput_seat_get_user_data(struct libinput_seat *seat)
656 {
657         return seat->user_data;
658 }
659
660 LIBINPUT_EXPORT const char *
661 libinput_seat_get_physical_name(struct libinput_seat *seat)
662 {
663         return seat->physical_name;
664 }
665
666 LIBINPUT_EXPORT const char *
667 libinput_seat_get_logical_name(struct libinput_seat *seat)
668 {
669         return seat->logical_name;
670 }
671
672 void
673 libinput_device_init(struct libinput_device *device,
674                      struct libinput_seat *seat)
675 {
676         device->seat = seat;
677         device->refcount = 1;
678 }
679
680 LIBINPUT_EXPORT struct libinput_device *
681 libinput_device_ref(struct libinput_device *device)
682 {
683         device->refcount++;
684         return device;
685 }
686
687 static void
688 libinput_device_destroy(struct libinput_device *device)
689 {
690         evdev_device_destroy((struct evdev_device *) device);
691 }
692
693 LIBINPUT_EXPORT struct libinput_device *
694 libinput_device_unref(struct libinput_device *device)
695 {
696         assert(device->refcount > 0);
697         device->refcount--;
698         if (device->refcount == 0) {
699                 libinput_device_destroy(device);
700                 return NULL;
701         } else {
702                 return device;
703         }
704 }
705
706 LIBINPUT_EXPORT int
707 libinput_get_fd(struct libinput *libinput)
708 {
709         return libinput->epoll_fd;
710 }
711
712 LIBINPUT_EXPORT int
713 libinput_dispatch(struct libinput *libinput)
714 {
715         struct libinput_source *source;
716         struct epoll_event ep[32];
717         int i, count;
718
719         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
720         if (count < 0)
721                 return -errno;
722
723         for (i = 0; i < count; ++i) {
724                 source = ep[i].data.ptr;
725                 if (source->fd == -1)
726                         continue;
727
728                 source->dispatch(source->user_data);
729         }
730
731         libinput_drop_destroyed_sources(libinput);
732
733         return 0;
734 }
735
736 static uint32_t
737 update_seat_key_count(struct libinput_seat *seat,
738                       int32_t key,
739                       enum libinput_key_state state)
740 {
741         assert(key >= 0 && key <= KEY_MAX);
742
743         switch (state) {
744         case LIBINPUT_KEY_STATE_PRESSED:
745                 return ++seat->button_count[key];
746         case LIBINPUT_KEY_STATE_RELEASED:
747                 /* We might not have received the first PRESSED event. */
748                 if (seat->button_count[key] == 0)
749                         return 0;
750
751                 return --seat->button_count[key];
752         }
753
754         return 0;
755 }
756
757 static uint32_t
758 update_seat_button_count(struct libinput_seat *seat,
759                          int32_t button,
760                          enum libinput_button_state state)
761 {
762         assert(button >= 0 && button <= KEY_MAX);
763
764         switch (state) {
765         case LIBINPUT_BUTTON_STATE_PRESSED:
766                 return ++seat->button_count[button];
767         case LIBINPUT_BUTTON_STATE_RELEASED:
768                 /* We might not have received the first PRESSED event. */
769                 if (seat->button_count[button] == 0)
770                         return 0;
771
772                 return --seat->button_count[button];
773         }
774
775         return 0;
776 }
777
778 static void
779 init_event_base(struct libinput_event *event,
780                 struct libinput_device *device,
781                 enum libinput_event_type type)
782 {
783         event->type = type;
784         event->device = device;
785 }
786
787 static void
788 post_base_event(struct libinput_device *device,
789                 enum libinput_event_type type,
790                 struct libinput_event *event)
791 {
792         struct libinput *libinput = device->seat->libinput;
793         init_event_base(event, device, type);
794         libinput_post_event(libinput, event);
795 }
796
797 static void
798 post_device_event(struct libinput_device *device,
799                   enum libinput_event_type type,
800                   struct libinput_event *event)
801 {
802         init_event_base(event, device, type);
803         libinput_post_event(device->seat->libinput, event);
804 }
805
806 void
807 notify_added_device(struct libinput_device *device)
808 {
809         struct libinput_event_device_notify *added_device_event;
810
811         added_device_event = zalloc(sizeof *added_device_event);
812         if (!added_device_event)
813                 return;
814
815         post_base_event(device,
816                         LIBINPUT_EVENT_DEVICE_ADDED,
817                         &added_device_event->base);
818 }
819
820 void
821 notify_removed_device(struct libinput_device *device)
822 {
823         struct libinput_event_device_notify *removed_device_event;
824
825         removed_device_event = zalloc(sizeof *removed_device_event);
826         if (!removed_device_event)
827                 return;
828
829         post_base_event(device,
830                         LIBINPUT_EVENT_DEVICE_REMOVED,
831                         &removed_device_event->base);
832 }
833
834 void
835 keyboard_notify_key(struct libinput_device *device,
836                     uint32_t time,
837                     uint32_t key,
838                     enum libinput_key_state state)
839 {
840         struct libinput_event_keyboard *key_event;
841         uint32_t seat_key_count;
842
843         key_event = zalloc(sizeof *key_event);
844         if (!key_event)
845                 return;
846
847         seat_key_count = update_seat_key_count(device->seat, key, state);
848
849         *key_event = (struct libinput_event_keyboard) {
850                 .time = time,
851                 .key = key,
852                 .state = state,
853                 .seat_key_count = seat_key_count,
854         };
855
856         post_device_event(device,
857                           LIBINPUT_EVENT_KEYBOARD_KEY,
858                           &key_event->base);
859 }
860
861 void
862 pointer_notify_motion(struct libinput_device *device,
863                       uint32_t time,
864                       double dx,
865                       double dy)
866 {
867         struct libinput_event_pointer *motion_event;
868
869         motion_event = zalloc(sizeof *motion_event);
870         if (!motion_event)
871                 return;
872
873         *motion_event = (struct libinput_event_pointer) {
874                 .time = time,
875                 .x = dx,
876                 .y = dy,
877         };
878
879         post_device_event(device,
880                           LIBINPUT_EVENT_POINTER_MOTION,
881                           &motion_event->base);
882 }
883
884 void
885 pointer_notify_motion_absolute(struct libinput_device *device,
886                                uint32_t time,
887                                double x,
888                                double y)
889 {
890         struct libinput_event_pointer *motion_absolute_event;
891
892         motion_absolute_event = zalloc(sizeof *motion_absolute_event);
893         if (!motion_absolute_event)
894                 return;
895
896         *motion_absolute_event = (struct libinput_event_pointer) {
897                 .time = time,
898                 .x = x,
899                 .y = y,
900         };
901
902         post_device_event(device,
903                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
904                           &motion_absolute_event->base);
905 }
906
907 void
908 pointer_notify_button(struct libinput_device *device,
909                       uint32_t time,
910                       int32_t button,
911                       enum libinput_button_state state)
912 {
913         struct libinput_event_pointer *button_event;
914         int32_t seat_button_count;
915
916         button_event = zalloc(sizeof *button_event);
917         if (!button_event)
918                 return;
919
920         seat_button_count = update_seat_button_count(device->seat,
921                                                      button,
922                                                      state);
923
924         *button_event = (struct libinput_event_pointer) {
925                 .time = time,
926                 .button = button,
927                 .state = state,
928                 .seat_button_count = seat_button_count,
929         };
930
931         post_device_event(device,
932                           LIBINPUT_EVENT_POINTER_BUTTON,
933                           &button_event->base);
934 }
935
936 void
937 pointer_notify_axis(struct libinput_device *device,
938                     uint32_t time,
939                     enum libinput_pointer_axis axis,
940                     double value)
941 {
942         struct libinput_event_pointer *axis_event;
943
944         axis_event = zalloc(sizeof *axis_event);
945         if (!axis_event)
946                 return;
947
948         *axis_event = (struct libinput_event_pointer) {
949                 .time = time,
950                 .axis = axis,
951                 .value = value,
952         };
953
954         post_device_event(device,
955                           LIBINPUT_EVENT_POINTER_AXIS,
956                           &axis_event->base);
957 }
958
959 void
960 touch_notify_touch_down(struct libinput_device *device,
961                         uint32_t time,
962                         int32_t slot,
963                         int32_t seat_slot,
964                         double x,
965                         double y)
966 {
967         struct libinput_event_touch *touch_event;
968
969         touch_event = zalloc(sizeof *touch_event);
970         if (!touch_event)
971                 return;
972
973         *touch_event = (struct libinput_event_touch) {
974                 .time = time,
975                 .slot = slot,
976                 .seat_slot = seat_slot,
977                 .x = x,
978                 .y = y,
979         };
980
981         post_device_event(device,
982                           LIBINPUT_EVENT_TOUCH_DOWN,
983                           &touch_event->base);
984 }
985
986 void
987 touch_notify_touch_motion(struct libinput_device *device,
988                           uint32_t time,
989                           int32_t slot,
990                           int32_t seat_slot,
991                           double x,
992                           double y)
993 {
994         struct libinput_event_touch *touch_event;
995
996         touch_event = zalloc(sizeof *touch_event);
997         if (!touch_event)
998                 return;
999
1000         *touch_event = (struct libinput_event_touch) {
1001                 .time = time,
1002                 .slot = slot,
1003                 .seat_slot = seat_slot,
1004                 .x = x,
1005                 .y = y,
1006         };
1007
1008         post_device_event(device,
1009                           LIBINPUT_EVENT_TOUCH_MOTION,
1010                           &touch_event->base);
1011 }
1012
1013 void
1014 touch_notify_touch_up(struct libinput_device *device,
1015                       uint32_t time,
1016                       int32_t slot,
1017                       int32_t seat_slot)
1018 {
1019         struct libinput_event_touch *touch_event;
1020
1021         touch_event = zalloc(sizeof *touch_event);
1022         if (!touch_event)
1023                 return;
1024
1025         *touch_event = (struct libinput_event_touch) {
1026                 .time = time,
1027                 .slot = slot,
1028                 .seat_slot = seat_slot,
1029         };
1030
1031         post_device_event(device,
1032                           LIBINPUT_EVENT_TOUCH_UP,
1033                           &touch_event->base);
1034 }
1035
1036 void
1037 touch_notify_frame(struct libinput_device *device,
1038                    uint32_t time)
1039 {
1040         struct libinput_event_touch *touch_event;
1041
1042         touch_event = zalloc(sizeof *touch_event);
1043         if (!touch_event)
1044                 return;
1045
1046         *touch_event = (struct libinput_event_touch) {
1047                 .time = time,
1048         };
1049
1050         post_device_event(device,
1051                           LIBINPUT_EVENT_TOUCH_FRAME,
1052                           &touch_event->base);
1053 }
1054
1055
1056 static void
1057 libinput_post_event(struct libinput *libinput,
1058                     struct libinput_event *event)
1059 {
1060         struct libinput_event **events = libinput->events;
1061         size_t events_len = libinput->events_len;
1062         size_t events_count = libinput->events_count;
1063         size_t move_len;
1064         size_t new_out;
1065
1066         events_count++;
1067         if (events_count > events_len) {
1068                 events_len *= 2;
1069                 events = realloc(events, events_len * sizeof *events);
1070                 if (!events) {
1071                         fprintf(stderr, "Failed to reallocate event ring "
1072                                 "buffer");
1073                         return;
1074                 }
1075
1076                 if (libinput->events_count > 0 && libinput->events_in == 0) {
1077                         libinput->events_in = libinput->events_len;
1078                 } else if (libinput->events_count > 0 &&
1079                            libinput->events_out >= libinput->events_in) {
1080                         move_len = libinput->events_len - libinput->events_out;
1081                         new_out = events_len - move_len;
1082                         memmove(events + new_out,
1083                                 events + libinput->events_out,
1084                                 move_len * sizeof *events);
1085                         libinput->events_out = new_out;
1086                 }
1087
1088                 libinput->events = events;
1089                 libinput->events_len = events_len;
1090         }
1091
1092         if (event->device)
1093                 libinput_device_ref(event->device);
1094
1095         libinput->events_count = events_count;
1096         events[libinput->events_in] = event;
1097         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
1098 }
1099
1100 LIBINPUT_EXPORT struct libinput_event *
1101 libinput_get_event(struct libinput *libinput)
1102 {
1103         struct libinput_event *event;
1104
1105         if (libinput->events_count == 0)
1106                 return NULL;
1107
1108         event = libinput->events[libinput->events_out];
1109         libinput->events_out =
1110                 (libinput->events_out + 1) % libinput->events_len;
1111         libinput->events_count--;
1112
1113         return event;
1114 }
1115
1116 LIBINPUT_EXPORT enum libinput_event_type
1117 libinput_next_event_type(struct libinput *libinput)
1118 {
1119         struct libinput_event *event;
1120
1121         if (libinput->events_count == 0)
1122                 return LIBINPUT_EVENT_NONE;
1123
1124         event = libinput->events[libinput->events_out];
1125         return event->type;
1126 }
1127
1128 LIBINPUT_EXPORT void *
1129 libinput_get_user_data(struct libinput *libinput)
1130 {
1131         return libinput->user_data;
1132 }
1133
1134 LIBINPUT_EXPORT int
1135 libinput_resume(struct libinput *libinput)
1136 {
1137         return libinput->interface_backend->resume(libinput);
1138 }
1139
1140 LIBINPUT_EXPORT void
1141 libinput_suspend(struct libinput *libinput)
1142 {
1143         libinput->interface_backend->suspend(libinput);
1144 }
1145
1146 LIBINPUT_EXPORT void
1147 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
1148 {
1149         device->user_data = user_data;
1150 }
1151
1152 LIBINPUT_EXPORT void *
1153 libinput_device_get_user_data(struct libinput_device *device)
1154 {
1155         return device->user_data;
1156 }
1157
1158 LIBINPUT_EXPORT const char *
1159 libinput_device_get_sysname(struct libinput_device *device)
1160 {
1161         return evdev_device_get_sysname((struct evdev_device *) device);
1162 }
1163
1164 LIBINPUT_EXPORT const char *
1165 libinput_device_get_name(struct libinput_device *device)
1166 {
1167         return evdev_device_get_name((struct evdev_device *) device);
1168 }
1169
1170 LIBINPUT_EXPORT unsigned int
1171 libinput_device_get_id_product(struct libinput_device *device)
1172 {
1173         return evdev_device_get_id_product((struct evdev_device *) device);
1174 }
1175
1176 LIBINPUT_EXPORT unsigned int
1177 libinput_device_get_id_vendor(struct libinput_device *device)
1178 {
1179         return evdev_device_get_id_vendor((struct evdev_device *) device);
1180 }
1181
1182 LIBINPUT_EXPORT const char *
1183 libinput_device_get_output_name(struct libinput_device *device)
1184 {
1185         return evdev_device_get_output((struct evdev_device *) device);
1186 }
1187
1188 LIBINPUT_EXPORT struct libinput_seat *
1189 libinput_device_get_seat(struct libinput_device *device)
1190 {
1191         return device->seat;
1192 }
1193
1194 LIBINPUT_EXPORT void
1195 libinput_device_led_update(struct libinput_device *device,
1196                            enum libinput_led leds)
1197 {
1198         evdev_device_led_update((struct evdev_device *) device, leds);
1199 }
1200
1201 LIBINPUT_EXPORT int
1202 libinput_device_get_keys(struct libinput_device *device,
1203                          char *keys, size_t size)
1204 {
1205         return evdev_device_get_keys((struct evdev_device *) device,
1206                                      keys,
1207                                      size);
1208 }
1209
1210 LIBINPUT_EXPORT void
1211 libinput_device_calibrate(struct libinput_device *device,
1212                           float calibration[6])
1213 {
1214         evdev_device_calibrate((struct evdev_device *) device, calibration);
1215 }
1216
1217 LIBINPUT_EXPORT int
1218 libinput_device_has_capability(struct libinput_device *device,
1219                                enum libinput_device_capability capability)
1220 {
1221         return evdev_device_has_capability((struct evdev_device *) device,
1222                                            capability);
1223 }
1224
1225 LIBINPUT_EXPORT int
1226 libinput_device_get_size(struct libinput_device *device,
1227                          double *width,
1228                          double *height)
1229 {
1230         return evdev_device_get_size((struct evdev_device *)device,
1231                                      width,
1232                                      height);
1233 }
1234
1235 LIBINPUT_EXPORT struct libinput_event *
1236 libinput_event_device_notify_get_base_event(struct libinput_event_device_notify *event)
1237 {
1238         return &event->base;
1239 }
1240
1241 LIBINPUT_EXPORT struct libinput_event *
1242 libinput_event_keyboard_get_base_event(struct libinput_event_keyboard *event)
1243 {
1244         return &event->base;
1245 }
1246
1247 LIBINPUT_EXPORT struct libinput_event *
1248 libinput_event_pointer_get_base_event(struct libinput_event_pointer *event)
1249 {
1250         return &event->base;
1251 }
1252
1253 LIBINPUT_EXPORT struct libinput_event *
1254 libinput_event_touch_get_base_event(struct libinput_event_touch *event)
1255 {
1256         return &event->base;
1257 }
1258
1259 LIBINPUT_EXPORT const char *
1260 libinput_config_status_to_str(enum libinput_config_status status)
1261 {
1262         const char *str = NULL;
1263
1264         switch(status) {
1265         case LIBINPUT_CONFIG_STATUS_SUCCESS:
1266                 str = "Success";
1267                 break;
1268         case LIBINPUT_CONFIG_STATUS_UNSUPPORTED:
1269                 str = "Unsupported configuration option";
1270                 break;
1271         case LIBINPUT_CONFIG_STATUS_INVALID:
1272                 str = "Invalid argument range";
1273                 break;
1274         }
1275
1276         return str;
1277 }
1278
1279 LIBINPUT_EXPORT int
1280 libinput_device_config_tap_get_finger_count(struct libinput_device *device)
1281 {
1282         return device->config.tap ? device->config.tap->count(device) : 0;
1283 }
1284
1285 LIBINPUT_EXPORT enum libinput_config_status
1286 libinput_device_config_tap_set_enabled(struct libinput_device *device,
1287                                        enum libinput_config_tap_state enable)
1288 {
1289         if (enable != LIBINPUT_CONFIG_TAP_ENABLED &&
1290             enable != LIBINPUT_CONFIG_TAP_DISABLED)
1291                 return LIBINPUT_CONFIG_STATUS_INVALID;
1292
1293         if (enable &&
1294             libinput_device_config_tap_get_finger_count(device) == 0)
1295                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1296
1297         return device->config.tap->set_enabled(device, enable);
1298 }
1299
1300 LIBINPUT_EXPORT enum libinput_config_tap_state
1301 libinput_device_config_tap_get_enabled(struct libinput_device *device)
1302 {
1303         if (libinput_device_config_tap_get_finger_count(device) == 0)
1304                 return LIBINPUT_CONFIG_TAP_DISABLED;
1305
1306         return device->config.tap->get_enabled(device);
1307 }
1308
1309 LIBINPUT_EXPORT enum libinput_config_tap_state
1310 libinput_device_config_tap_get_default_enabled(struct libinput_device *device)
1311 {
1312         if (libinput_device_config_tap_get_finger_count(device) == 0)
1313                 return LIBINPUT_CONFIG_TAP_DISABLED;
1314
1315         return device->config.tap->get_default(device);
1316 }