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