Reference count event target struct when applicable
[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 "udev-seat.h"
37
38 enum libinput_event_class {
39         LIBINPUT_EVENT_CLASS_BASE,
40         LIBINPUT_EVENT_CLASS_SEAT,
41         LIBINPUT_EVENT_CLASS_DEVICE,
42 };
43
44 struct libinput_source {
45         libinput_source_dispatch_t dispatch;
46         void *user_data;
47         int fd;
48         struct list link;
49 };
50
51 static void
52 libinput_post_event(struct libinput *libinput,
53                     struct libinput_event *event);
54
55 struct libinput_source *
56 libinput_add_fd(struct libinput *libinput,
57                 int fd,
58                 libinput_source_dispatch_t dispatch,
59                 void *user_data)
60 {
61         struct libinput_source *source;
62         struct epoll_event ep;
63
64         source = malloc(sizeof *source);
65         if (!source)
66                 return NULL;
67
68         source->dispatch = dispatch;
69         source->user_data = user_data;
70         source->fd = fd;
71
72         memset(&ep, 0, sizeof ep);
73         ep.events = EPOLLIN;
74         ep.data.ptr = source;
75
76         if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
77                 close(source->fd);
78                 free(source);
79                 return NULL;
80         }
81
82         return source;
83 }
84
85 void
86 libinput_remove_source(struct libinput *libinput,
87                        struct libinput_source *source)
88 {
89         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
90         close(source->fd);
91         source->fd = -1;
92         list_insert(&libinput->source_destroy_list, &source->link);
93 }
94
95 int
96 libinput_init(struct libinput *libinput,
97               const struct libinput_interface *interface,
98               void *user_data)
99 {
100         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
101         if (libinput->epoll_fd < 0)
102                 return -1;
103
104         libinput->interface = interface;
105         libinput->user_data = user_data;
106         list_init(&libinput->source_destroy_list);
107         list_init(&libinput->seat_list);
108
109         return 0;
110 }
111
112 LIBINPUT_EXPORT void
113 libinput_destroy(struct libinput *libinput)
114 {
115         struct libinput_event *event;
116
117         while ((event = libinput_get_event(libinput)))
118                free(event);
119         free(libinput->events);
120
121         close(libinput->epoll_fd);
122         free(libinput);
123 }
124
125 static enum libinput_event_class
126 libinput_event_get_class(struct libinput_event *event)
127 {
128         switch (event->type) {
129         case LIBINPUT_EVENT_ADDED_SEAT:
130         case LIBINPUT_EVENT_REMOVED_SEAT:
131         case LIBINPUT_EVENT_ADDED_DEVICE:
132         case LIBINPUT_EVENT_REMOVED_DEVICE:
133                 return LIBINPUT_EVENT_CLASS_BASE;
134
135         case LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY:
136         case LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY:
137         case LIBINPUT_EVENT_KEYBOARD_KEY:
138         case LIBINPUT_EVENT_POINTER_MOTION:
139         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
140         case LIBINPUT_EVENT_POINTER_BUTTON:
141         case LIBINPUT_EVENT_POINTER_AXIS:
142         case LIBINPUT_EVENT_TOUCH_TOUCH:
143                 return LIBINPUT_EVENT_CLASS_DEVICE;
144         }
145
146         /* We should never end up here. */
147         abort();
148 }
149
150 LIBINPUT_EXPORT void
151 libinput_event_destroy(struct libinput_event *event)
152 {
153         switch (libinput_event_get_class(event)) {
154         case LIBINPUT_EVENT_CLASS_BASE:
155                 break;
156         case LIBINPUT_EVENT_CLASS_SEAT:
157                 libinput_seat_unref(event->target.seat);
158                 break;
159         case LIBINPUT_EVENT_CLASS_DEVICE:
160                 libinput_device_unref(event->target.device);
161                 break;
162         }
163
164         free(event);
165 }
166
167 int
168 open_restricted(struct libinput *libinput,
169                 const char *path, int flags)
170 {
171         return libinput->interface->open_restricted(path,
172                                                     flags,
173                                                     libinput->user_data);
174 }
175
176 void
177 close_restricted(struct libinput *libinput, int fd)
178 {
179         return libinput->interface->close_restricted(fd, libinput->user_data);
180 }
181
182 void
183 libinput_seat_init(struct libinput_seat *seat,
184                    struct libinput *libinput,
185                    const char *name)
186 {
187         seat->refcount = 1;
188         seat->libinput = libinput;
189         seat->name = strdup(name);
190         list_init(&seat->devices_list);
191 }
192
193 LIBINPUT_EXPORT void
194 libinput_seat_ref(struct libinput_seat *seat)
195 {
196         seat->refcount++;
197 }
198
199 LIBINPUT_EXPORT void
200 libinput_seat_unref(struct libinput_seat *seat)
201 {
202         seat->refcount--;
203         if (seat->refcount == 0) {
204                 free(seat->name);
205                 udev_seat_destroy((struct udev_seat *) seat);
206         }
207 }
208
209 LIBINPUT_EXPORT void
210 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
211 {
212         seat->user_data = user_data;
213 }
214
215 LIBINPUT_EXPORT void *
216 libinput_seat_get_user_data(struct libinput_seat *seat)
217 {
218         return seat->user_data;
219 }
220
221 LIBINPUT_EXPORT const char *
222 libinput_seat_get_name(struct libinput_seat *seat)
223 {
224         return seat->name;
225 }
226
227 void
228 libinput_device_init(struct libinput_device *device,
229                      struct libinput_seat *seat)
230 {
231         device->seat = seat;
232         device->refcount = 1;
233 }
234
235 LIBINPUT_EXPORT void
236 libinput_device_ref(struct libinput_device *device)
237 {
238         device->refcount++;
239 }
240
241 LIBINPUT_EXPORT void
242 libinput_device_unref(struct libinput_device *device)
243 {
244         device->refcount--;
245         if (device->refcount == 0)
246                 evdev_device_destroy((struct evdev_device *) device);
247 }
248
249 LIBINPUT_EXPORT int
250 libinput_get_fd(struct libinput *libinput)
251 {
252         return libinput->epoll_fd;
253 }
254
255 LIBINPUT_EXPORT int
256 libinput_dispatch(struct libinput *libinput)
257 {
258         struct libinput_source *source, *next;
259         struct epoll_event ep[32];
260         int i, count;
261
262         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
263         if (count < 0)
264                 return -errno;
265
266         for (i = 0; i < count; ++i) {
267                 source = ep[i].data.ptr;
268                 if (source->fd == -1)
269                         continue;
270
271                 source->dispatch(source->user_data);
272         }
273
274         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
275                 free(source);
276         list_init(&libinput->source_destroy_list);
277
278         return libinput->events_count > 0 ? 0 : -EAGAIN;
279 }
280
281 static void
282 init_event_base(struct libinput_event *event,
283                 enum libinput_event_type type,
284                 union libinput_event_target target)
285 {
286         event->type = type;
287         event->target = target;
288 }
289
290 static void
291 post_base_event(struct libinput *libinput,
292                 enum libinput_event_type type,
293                 struct libinput_event *event)
294 {
295         init_event_base(event, type,
296                         (union libinput_event_target) { .libinput = libinput });
297         libinput_post_event(libinput, event);
298 }
299
300 static void
301 post_device_event(struct libinput_device *device,
302                   enum libinput_event_type type,
303                   struct libinput_event *event)
304 {
305         init_event_base(event, type,
306                         (union libinput_event_target) { .device = device });
307         libinput_post_event(device->seat->libinput, event);
308 }
309
310 void
311 notify_added_seat(struct libinput_seat *seat)
312 {
313         struct libinput_event_added_seat *added_seat_event;
314
315         added_seat_event = malloc(sizeof *added_seat_event);
316         if (!added_seat_event)
317                 return;
318
319         *added_seat_event = (struct libinput_event_added_seat) {
320                 .seat = seat,
321         };
322
323         post_base_event(seat->libinput,
324                         LIBINPUT_EVENT_ADDED_SEAT,
325                         &added_seat_event->base);
326 }
327
328 void
329 notify_removed_seat(struct libinput_seat *seat)
330 {
331         struct libinput_event_removed_seat *removed_seat_event;
332
333         removed_seat_event = malloc(sizeof *removed_seat_event);
334         if (!removed_seat_event)
335                 return;
336
337         *removed_seat_event = (struct libinput_event_removed_seat) {
338                 .seat = seat,
339         };
340
341         post_base_event(seat->libinput,
342                         LIBINPUT_EVENT_REMOVED_SEAT,
343                         &removed_seat_event->base);
344 }
345
346 void
347 notify_added_device(struct libinput_device *device)
348 {
349         struct libinput_event_added_device *added_device_event;
350
351         added_device_event = malloc(sizeof *added_device_event);
352         if (!added_device_event)
353                 return;
354
355         *added_device_event = (struct libinput_event_added_device) {
356                 .device = device,
357         };
358
359         post_base_event(device->seat->libinput,
360                         LIBINPUT_EVENT_ADDED_DEVICE,
361                         &added_device_event->base);
362 }
363
364 void
365 notify_removed_device(struct libinput_device *device)
366 {
367         struct libinput_event_removed_device *removed_device_event;
368
369         removed_device_event = malloc(sizeof *removed_device_event);
370         if (!removed_device_event)
371                 return;
372
373         *removed_device_event = (struct libinput_event_removed_device) {
374                 .device = device,
375         };
376
377         post_base_event(device->seat->libinput,
378                         LIBINPUT_EVENT_REMOVED_DEVICE,
379                         &removed_device_event->base);
380 }
381
382 void
383 device_register_capability(struct libinput_device *device,
384                            enum libinput_device_capability capability)
385 {
386         struct libinput_event_device_register_capability *capability_event;
387
388         capability_event = malloc(sizeof *capability_event);
389
390         *capability_event = (struct libinput_event_device_register_capability) {
391                 .capability = capability,
392         };
393
394         post_device_event(device,
395                           LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
396                           &capability_event->base);
397 }
398
399 void
400 device_unregister_capability(struct libinput_device *device,
401                              enum libinput_device_capability capability)
402 {
403         struct libinput_event_device_unregister_capability *capability_event;
404
405         capability_event = malloc(sizeof *capability_event);
406
407         *capability_event = (struct libinput_event_device_unregister_capability) {
408                 .capability = capability,
409         };
410
411         post_device_event(device,
412                           LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
413                           &capability_event->base);
414 }
415
416 void
417 keyboard_notify_key(struct libinput_device *device,
418                     uint32_t time,
419                     uint32_t key,
420                     enum libinput_keyboard_key_state state)
421 {
422         struct libinput_event_keyboard_key *key_event;
423
424         key_event = malloc(sizeof *key_event);
425         if (!key_event)
426                 return;
427
428         *key_event = (struct libinput_event_keyboard_key) {
429                 .time = time,
430                 .key = key,
431                 .state = state,
432         };
433
434         post_device_event(device,
435                           LIBINPUT_EVENT_KEYBOARD_KEY,
436                           &key_event->base);
437 }
438
439 void
440 pointer_notify_motion(struct libinput_device *device,
441                       uint32_t time,
442                       li_fixed_t dx,
443                       li_fixed_t dy)
444 {
445         struct libinput_event_pointer_motion *motion_event;
446
447         motion_event = malloc(sizeof *motion_event);
448         if (!motion_event)
449                 return;
450
451         *motion_event = (struct libinput_event_pointer_motion) {
452                 .time = time,
453                 .dx = dx,
454                 .dy = dy,
455         };
456
457         post_device_event(device,
458                           LIBINPUT_EVENT_POINTER_MOTION,
459                           &motion_event->base);
460 }
461
462 void
463 pointer_notify_motion_absolute(struct libinput_device *device,
464                                uint32_t time,
465                                li_fixed_t x,
466                                li_fixed_t y)
467 {
468         struct libinput_event_pointer_motion_absolute *motion_absolute_event;
469
470         motion_absolute_event = malloc(sizeof *motion_absolute_event);
471         if (!motion_absolute_event)
472                 return;
473
474         *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
475                 .time = time,
476                 .x = x,
477                 .y = y,
478         };
479
480         post_device_event(device,
481                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
482                           &motion_absolute_event->base);
483 }
484
485 void
486 pointer_notify_button(struct libinput_device *device,
487                       uint32_t time,
488                       int32_t button,
489                       enum libinput_pointer_button_state state)
490 {
491         struct libinput_event_pointer_button *button_event;
492
493         button_event = malloc(sizeof *button_event);
494         if (!button_event)
495                 return;
496
497         *button_event = (struct libinput_event_pointer_button) {
498                 .time = time,
499                 .button = button,
500                 .state = state,
501         };
502
503         post_device_event(device,
504                           LIBINPUT_EVENT_POINTER_BUTTON,
505                           &button_event->base);
506 }
507
508 void
509 pointer_notify_axis(struct libinput_device *device,
510                     uint32_t time,
511                     enum libinput_pointer_axis axis,
512                     li_fixed_t value)
513 {
514         struct libinput_event_pointer_axis *axis_event;
515
516         axis_event = malloc(sizeof *axis_event);
517         if (!axis_event)
518                 return;
519
520         *axis_event = (struct libinput_event_pointer_axis) {
521                 .time = time,
522                 .axis = axis,
523                 .value = value,
524         };
525
526         post_device_event(device,
527                           LIBINPUT_EVENT_POINTER_AXIS,
528                           &axis_event->base);
529 }
530
531 void
532 touch_notify_touch(struct libinput_device *device,
533                    uint32_t time,
534                    int32_t slot,
535                    li_fixed_t x,
536                    li_fixed_t y,
537                    enum libinput_touch_type touch_type)
538 {
539         struct libinput_event_touch_touch *touch_event;
540
541         touch_event = malloc(sizeof *touch_event);
542         if (!touch_event)
543                 return;
544
545         *touch_event = (struct libinput_event_touch_touch) {
546                 .time = time,
547                 .slot = slot,
548                 .x = x,
549                 .y = y,
550                 .touch_type = touch_type,
551         };
552
553         post_device_event(device,
554                           LIBINPUT_EVENT_TOUCH_TOUCH,
555                           &touch_event->base);
556 }
557
558 static void
559 libinput_post_event(struct libinput *libinput,
560                     struct libinput_event *event)
561 {
562         struct libinput_event **events = libinput->events;
563         size_t events_len = libinput->events_len;
564         size_t events_count = libinput->events_count;
565         size_t move_len;
566         size_t new_out;
567
568         events_count++;
569         if (events_count > events_len) {
570                 if (events_len == 0)
571                         events_len = 4;
572                 else
573                         events_len *= 2;
574                 events = realloc(events, events_len * sizeof *events);
575                 if (!events) {
576                         fprintf(stderr, "Failed to reallocate event ring "
577                                 "buffer");
578                         return;
579                 }
580
581                 if (libinput->events_count > 0 && libinput->events_in == 0) {
582                         libinput->events_in = libinput->events_len;
583                 } else if (libinput->events_count > 0 &&
584                            libinput->events_out >= libinput->events_in) {
585                         move_len = libinput->events_len - libinput->events_out;
586                         new_out = events_len - move_len;
587                         memmove(events + new_out,
588                                 libinput->events + libinput->events_out,
589                                 move_len * sizeof *events);
590                         libinput->events_out = new_out;
591                 }
592
593                 libinput->events = events;
594                 libinput->events_len = events_len;
595         }
596
597         switch (libinput_event_get_class(event)) {
598         case LIBINPUT_EVENT_CLASS_BASE:
599                 break;
600         case LIBINPUT_EVENT_CLASS_SEAT:
601                 libinput_seat_ref(event->target.seat);
602                 break;
603         case LIBINPUT_EVENT_CLASS_DEVICE:
604                 libinput_device_ref(event->target.device);
605                 break;
606         }
607
608         libinput->events_count = events_count;
609         events[libinput->events_in] = event;
610         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
611 }
612
613 LIBINPUT_EXPORT struct libinput_event *
614 libinput_get_event(struct libinput *libinput)
615 {
616         struct libinput_event *event;
617
618         if (libinput->events_count == 0)
619                 return NULL;
620
621         event = libinput->events[libinput->events_out];
622         libinput->events_out =
623                 (libinput->events_out + 1) % libinput->events_len;
624         libinput->events_count--;
625
626         return event;
627 }
628
629 LIBINPUT_EXPORT void *
630 libinput_get_user_data(struct libinput *libinput)
631 {
632         return libinput->user_data;
633 }
634
635 LIBINPUT_EXPORT int
636 libinput_resume(struct libinput *libinput)
637 {
638         return udev_input_enable((struct udev_input *) libinput);
639 }
640
641 LIBINPUT_EXPORT void
642 libinput_suspend(struct libinput *libinput)
643 {
644         udev_input_disable((struct udev_input *) libinput);
645 }
646
647 LIBINPUT_EXPORT void
648 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
649 {
650         device->user_data = user_data;
651 }
652
653 LIBINPUT_EXPORT void *
654 libinput_device_get_user_data(struct libinput_device *device)
655 {
656         return device->user_data;
657 }
658
659 LIBINPUT_EXPORT const char *
660 libinput_device_get_output_name(struct libinput_device *device)
661 {
662         return evdev_device_get_output((struct evdev_device *) device);
663 }
664
665 LIBINPUT_EXPORT struct libinput_seat *
666 libinput_device_get_seat(struct libinput_device *device)
667 {
668         return device->seat;
669 }
670
671 LIBINPUT_EXPORT void
672 libinput_device_led_update(struct libinput_device *device,
673                            enum libinput_led leds)
674 {
675         evdev_device_led_update((struct evdev_device *) device, leds);
676 }
677
678 LIBINPUT_EXPORT int
679 libinput_device_get_keys(struct libinput_device *device,
680                          char *keys, size_t size)
681 {
682         return evdev_device_get_keys((struct evdev_device *) device,
683                                      keys,
684                                      size);
685 }
686
687 LIBINPUT_EXPORT void
688 libinput_device_calibrate(struct libinput_device *device,
689                           float calibration[6])
690 {
691         evdev_device_calibrate((struct evdev_device *) device, calibration);
692 }