Remove unused disabled 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 #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 static void
253 post_device_event(struct libinput_device *device,
254                   enum libinput_event_type type,
255                   struct libinput_event *event)
256 {
257         init_event_base(event, type,
258                         (union libinput_event_target) { .device = device });
259         libinput_post_event(device->seat->libinput, event);
260 }
261
262 void
263 notify_added_seat(struct libinput_seat *seat)
264 {
265         struct libinput_event_added_seat *added_seat_event;
266
267         added_seat_event = malloc(sizeof *added_seat_event);
268         if (!added_seat_event)
269                 return;
270
271         *added_seat_event = (struct libinput_event_added_seat) {
272                 .seat = seat,
273         };
274
275         post_base_event(seat->libinput,
276                         LIBINPUT_EVENT_ADDED_SEAT,
277                         &added_seat_event->base);
278 }
279
280 void
281 notify_removed_seat(struct libinput_seat *seat)
282 {
283         struct libinput_event_removed_seat *removed_seat_event;
284
285         removed_seat_event = malloc(sizeof *removed_seat_event);
286         if (!removed_seat_event)
287                 return;
288
289         *removed_seat_event = (struct libinput_event_removed_seat) {
290                 .seat = seat,
291         };
292
293         post_base_event(seat->libinput,
294                         LIBINPUT_EVENT_REMOVED_SEAT,
295                         &removed_seat_event->base);
296 }
297
298 void
299 notify_added_device(struct libinput_device *device)
300 {
301         struct libinput_event_added_device *added_device_event;
302
303         added_device_event = malloc(sizeof *added_device_event);
304         if (!added_device_event)
305                 return;
306
307         *added_device_event = (struct libinput_event_added_device) {
308                 .device = device,
309         };
310
311         post_base_event(device->seat->libinput,
312                         LIBINPUT_EVENT_ADDED_DEVICE,
313                         &added_device_event->base);
314 }
315
316 void
317 notify_removed_device(struct libinput_device *device)
318 {
319         struct libinput_event_removed_device *removed_device_event;
320
321         removed_device_event = malloc(sizeof *removed_device_event);
322         if (!removed_device_event)
323                 return;
324
325         *removed_device_event = (struct libinput_event_removed_device) {
326                 .device = device,
327         };
328
329         post_base_event(device->seat->libinput,
330                         LIBINPUT_EVENT_REMOVED_DEVICE,
331                         &removed_device_event->base);
332 }
333
334 void
335 device_register_capability(struct libinput_device *device,
336                            enum libinput_device_capability capability)
337 {
338         struct libinput_event_device_register_capability *capability_event;
339
340         capability_event = malloc(sizeof *capability_event);
341
342         *capability_event = (struct libinput_event_device_register_capability) {
343                 .capability = capability,
344         };
345
346         post_device_event(device,
347                           LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
348                           &capability_event->base);
349 }
350
351 void
352 device_unregister_capability(struct libinput_device *device,
353                              enum libinput_device_capability capability)
354 {
355         struct libinput_event_device_unregister_capability *capability_event;
356
357         capability_event = malloc(sizeof *capability_event);
358
359         *capability_event = (struct libinput_event_device_unregister_capability) {
360                 .capability = capability,
361         };
362
363         post_device_event(device,
364                           LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
365                           &capability_event->base);
366 }
367
368 void
369 keyboard_notify_key(struct libinput_device *device,
370                     uint32_t time,
371                     uint32_t key,
372                     enum libinput_keyboard_key_state state)
373 {
374         struct libinput_event_keyboard_key *key_event;
375
376         key_event = malloc(sizeof *key_event);
377         if (!key_event)
378                 return;
379
380         *key_event = (struct libinput_event_keyboard_key) {
381                 .time = time,
382                 .key = key,
383                 .state = state,
384         };
385
386         post_device_event(device,
387                           LIBINPUT_EVENT_KEYBOARD_KEY,
388                           &key_event->base);
389 }
390
391 void
392 pointer_notify_motion(struct libinput_device *device,
393                       uint32_t time,
394                       li_fixed_t dx,
395                       li_fixed_t dy)
396 {
397         struct libinput_event_pointer_motion *motion_event;
398
399         motion_event = malloc(sizeof *motion_event);
400         if (!motion_event)
401                 return;
402
403         *motion_event = (struct libinput_event_pointer_motion) {
404                 .time = time,
405                 .dx = dx,
406                 .dy = dy,
407         };
408
409         post_device_event(device,
410                           LIBINPUT_EVENT_POINTER_MOTION,
411                           &motion_event->base);
412 }
413
414 void
415 pointer_notify_motion_absolute(struct libinput_device *device,
416                                uint32_t time,
417                                li_fixed_t x,
418                                li_fixed_t y)
419 {
420         struct libinput_event_pointer_motion_absolute *motion_absolute_event;
421
422         motion_absolute_event = malloc(sizeof *motion_absolute_event);
423         if (!motion_absolute_event)
424                 return;
425
426         *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
427                 .time = time,
428                 .x = x,
429                 .y = y,
430         };
431
432         post_device_event(device,
433                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
434                           &motion_absolute_event->base);
435 }
436
437 void
438 pointer_notify_button(struct libinput_device *device,
439                       uint32_t time,
440                       int32_t button,
441                       enum libinput_pointer_button_state state)
442 {
443         struct libinput_event_pointer_button *button_event;
444
445         button_event = malloc(sizeof *button_event);
446         if (!button_event)
447                 return;
448
449         *button_event = (struct libinput_event_pointer_button) {
450                 .time = time,
451                 .button = button,
452                 .state = state,
453         };
454
455         post_device_event(device,
456                           LIBINPUT_EVENT_POINTER_BUTTON,
457                           &button_event->base);
458 }
459
460 void
461 pointer_notify_axis(struct libinput_device *device,
462                     uint32_t time,
463                     enum libinput_pointer_axis axis,
464                     li_fixed_t value)
465 {
466         struct libinput_event_pointer_axis *axis_event;
467
468         axis_event = malloc(sizeof *axis_event);
469         if (!axis_event)
470                 return;
471
472         *axis_event = (struct libinput_event_pointer_axis) {
473                 .time = time,
474                 .axis = axis,
475                 .value = value,
476         };
477
478         post_device_event(device,
479                           LIBINPUT_EVENT_POINTER_AXIS,
480                           &axis_event->base);
481 }
482
483 void
484 touch_notify_touch(struct libinput_device *device,
485                    uint32_t time,
486                    int32_t slot,
487                    li_fixed_t x,
488                    li_fixed_t y,
489                    enum libinput_touch_type touch_type)
490 {
491         struct libinput_event_touch_touch *touch_event;
492
493         touch_event = malloc(sizeof *touch_event);
494         if (!touch_event)
495                 return;
496
497         *touch_event = (struct libinput_event_touch_touch) {
498                 .time = time,
499                 .slot = slot,
500                 .x = x,
501                 .y = y,
502                 .touch_type = touch_type,
503         };
504
505         post_device_event(device,
506                           LIBINPUT_EVENT_TOUCH_TOUCH,
507                           &touch_event->base);
508 }
509
510 static void
511 libinput_post_event(struct libinput *libinput,
512                     struct libinput_event *event)
513 {
514         struct libinput_event **events = libinput->events;
515         size_t events_len = libinput->events_len;
516         size_t events_count = libinput->events_count;
517         size_t move_len;
518         size_t new_out;
519
520         events_count++;
521         if (events_count > events_len) {
522                 if (events_len == 0)
523                         events_len = 4;
524                 else
525                         events_len *= 2;
526                 events = realloc(events, events_len * sizeof *events);
527                 if (!events) {
528                         fprintf(stderr, "Failed to reallocate event ring "
529                                 "buffer");
530                         return;
531                 }
532
533                 if (libinput->events_count > 0 && libinput->events_in == 0) {
534                         libinput->events_in = libinput->events_len;
535                 } else if (libinput->events_count > 0 &&
536                            libinput->events_out >= libinput->events_in) {
537                         move_len = libinput->events_len - libinput->events_out;
538                         new_out = events_len - move_len;
539                         memmove(events + new_out,
540                                 libinput->events + libinput->events_out,
541                                 move_len * sizeof *events);
542                         libinput->events_out = new_out;
543                 }
544
545                 libinput->events = events;
546                 libinput->events_len = events_len;
547         }
548
549         libinput->events_count = events_count;
550         events[libinput->events_in] = event;
551         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
552 }
553
554 LIBINPUT_EXPORT struct libinput_event *
555 libinput_get_event(struct libinput *libinput)
556 {
557         struct libinput_event *event;
558
559         if (libinput->events_count == 0)
560                 return NULL;
561
562         event = libinput->events[libinput->events_out];
563         libinput->events_out =
564                 (libinput->events_out + 1) % libinput->events_len;
565         libinput->events_count--;
566
567         return event;
568 }
569
570 LIBINPUT_EXPORT void *
571 libinput_get_user_data(struct libinput *libinput)
572 {
573         return libinput->user_data;
574 }
575
576 LIBINPUT_EXPORT int
577 libinput_resume(struct libinput *libinput)
578 {
579         return udev_input_enable((struct udev_input *) libinput);
580 }
581
582 LIBINPUT_EXPORT void
583 libinput_suspend(struct libinput *libinput)
584 {
585         udev_input_disable((struct udev_input *) libinput);
586 }
587
588 LIBINPUT_EXPORT void
589 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
590 {
591         device->user_data = user_data;
592 }
593
594 LIBINPUT_EXPORT void *
595 libinput_device_get_user_data(struct libinput_device *device)
596 {
597         return device->user_data;
598 }
599
600 LIBINPUT_EXPORT const char *
601 libinput_device_get_output_name(struct libinput_device *device)
602 {
603         return evdev_device_get_output((struct evdev_device *) device);
604 }
605
606 LIBINPUT_EXPORT struct libinput_seat *
607 libinput_device_get_seat(struct libinput_device *device)
608 {
609         return device->seat;
610 }
611
612 LIBINPUT_EXPORT void
613 libinput_device_led_update(struct libinput_device *device,
614                            enum libinput_led leds)
615 {
616         evdev_device_led_update((struct evdev_device *) device, leds);
617 }
618
619 LIBINPUT_EXPORT int
620 libinput_device_get_keys(struct libinput_device *device,
621                          char *keys, size_t size)
622 {
623         return evdev_device_get_keys((struct evdev_device *) device,
624                                      keys,
625                                      size);
626 }
627
628 LIBINPUT_EXPORT void
629 libinput_device_calibrate(struct libinput_device *device,
630                           float calibration[6])
631 {
632         evdev_device_calibrate((struct evdev_device *) device, calibration);
633 }