Properly dispose event sources and event loops
[platform/upstream/libinput.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <linux/input.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <mtdev.h>
30
31 #include "compositor.h"
32 #include "evdev.h"
33
34 struct evdev_input {
35         struct weston_input_device base;
36         struct wl_list devices_list;
37         struct udev_monitor *udev_monitor;
38         struct wl_event_source *udev_monitor_source;
39         char *seat_id;
40 };
41
42 #define MAX_SLOTS 16
43
44 struct evdev_input_device {
45         struct evdev_input *master;
46         struct wl_list link;
47         struct wl_event_source *source;
48         struct weston_output *output;
49         char *devnode;
50         int fd;
51         struct {
52                 int min_x, max_x, min_y, max_y;
53                 int old_x, old_y, reset_x, reset_y;
54                 int32_t x, y;
55         } abs;
56
57         struct {
58                 int slot;
59                 int32_t x[MAX_SLOTS];
60                 int32_t y[MAX_SLOTS];
61         } mt;
62         struct mtdev *mtdev;
63
64         struct {
65                 int dx, dy;
66         } rel;
67
68         int type; /* event type flags */
69
70         int is_touchpad, is_mt;
71 };
72
73 /* event type flags */
74 #define EVDEV_ABSOLUTE_MOTION           (1 << 0)
75 #define EVDEV_ABSOLUTE_MT_DOWN          (1 << 1)
76 #define EVDEV_ABSOLUTE_MT_MOTION        (1 << 2)
77 #define EVDEV_ABSOLUTE_MT_UP            (1 << 3)
78 #define EVDEV_RELATIVE_MOTION           (1 << 4)
79
80 static inline void
81 evdev_process_key(struct evdev_input_device *device,
82                         struct input_event *e, int time)
83 {
84         if (e->value == 2)
85                 return;
86
87         switch (e->code) {
88         case BTN_TOOL_PEN:
89         case BTN_TOOL_RUBBER:
90         case BTN_TOOL_BRUSH:
91         case BTN_TOOL_PENCIL:
92         case BTN_TOOL_AIRBRUSH:
93         case BTN_TOOL_FINGER:
94         case BTN_TOOL_MOUSE:
95         case BTN_TOOL_LENS:
96                 if (device->is_touchpad)
97                 {
98                         device->abs.reset_x = 1;
99                         device->abs.reset_y = 1;
100                 }
101                 break;
102         case BTN_TOUCH:
103                 /* Multitouch touchscreen devices might not send individually
104                  * button events each time a new finger is down. So we don't
105                  * send notification for such devices and we solve the button
106                  * case emulating on compositor side. */
107                 if (device->is_mt)
108                         break;
109
110                 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
111                  * BTN_LEFT */
112                 e->code = BTN_LEFT;
113                 /* Intentional fallthrough! */
114         case BTN_LEFT:
115         case BTN_RIGHT:
116         case BTN_MIDDLE:
117         case BTN_SIDE:
118         case BTN_EXTRA:
119         case BTN_FORWARD:
120         case BTN_BACK:
121         case BTN_TASK:
122                 notify_button(&device->master->base.input_device,
123                               time, e->code, e->value);
124                 break;
125
126         default:
127                 notify_key(&device->master->base.input_device,
128                            time, e->code, e->value);
129                 break;
130         }
131 }
132
133 static void
134 evdev_process_touch(struct evdev_input_device *device,
135                     struct input_event *e)
136 {
137         const int screen_width = device->output->current->width;
138         const int screen_height = device->output->current->height;
139
140         switch (e->code) {
141         case ABS_MT_SLOT:
142                 device->mt.slot = e->value;
143                 break;
144         case ABS_MT_TRACKING_ID:
145                 if (e->value >= 0)
146                         device->type |= EVDEV_ABSOLUTE_MT_DOWN;
147                 else
148                         device->type |= EVDEV_ABSOLUTE_MT_UP;
149                 break;
150         case ABS_MT_POSITION_X:
151                 device->mt.x[device->mt.slot] =
152                         (e->value - device->abs.min_x) * screen_width /
153                         (device->abs.max_x - device->abs.min_x) +
154                         device->output->x;
155                 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
156                 break;
157         case ABS_MT_POSITION_Y:
158                 device->mt.y[device->mt.slot] =
159                         (e->value - device->abs.min_y) * screen_height /
160                         (device->abs.max_y - device->abs.min_y) +
161                         device->output->y;
162                 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
163                 break;
164         }
165 }
166
167 static inline void
168 evdev_process_absolute_motion(struct evdev_input_device *device,
169                               struct input_event *e)
170 {
171         const int screen_width = device->output->current->width;
172         const int screen_height = device->output->current->height;
173
174         switch (e->code) {
175         case ABS_X:
176                 device->abs.x =
177                         (e->value - device->abs.min_x) * screen_width /
178                         (device->abs.max_x - device->abs.min_x) +
179                         device->output->x;
180                 device->type |= EVDEV_ABSOLUTE_MOTION;
181                 break;
182         case ABS_Y:
183                 device->abs.y =
184                         (e->value - device->abs.min_y) * screen_height /
185                         (device->abs.max_y - device->abs.min_y) +
186                         device->output->y;
187                 device->type |= EVDEV_ABSOLUTE_MOTION;
188                 break;
189         }
190 }
191
192 static inline void
193 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
194                                        struct input_event *e)
195 {
196         /* FIXME: Make this configurable somehow. */
197         const int touchpad_speed = 700;
198
199         switch (e->code) {
200         case ABS_X:
201                 e->value -= device->abs.min_x;
202                 if (device->abs.reset_x)
203                         device->abs.reset_x = 0;
204                 else {
205                         device->rel.dx =
206                                 (e->value - device->abs.old_x) *
207                                 touchpad_speed /
208                                 (device->abs.max_x - device->abs.min_x);
209                 }
210                 device->abs.old_x = e->value;
211                 device->type |= EVDEV_RELATIVE_MOTION;
212                 break;
213         case ABS_Y:
214                 e->value -= device->abs.min_y;
215                 if (device->abs.reset_y)
216                         device->abs.reset_y = 0;
217                 else {
218                         device->rel.dy =
219                                 (e->value - device->abs.old_y) *
220                                 touchpad_speed /
221                                 /* maybe use x size here to have the same scale? */
222                                 (device->abs.max_y - device->abs.min_y);
223                 }
224                 device->abs.old_y = e->value;
225                 device->type |= EVDEV_RELATIVE_MOTION;
226                 break;
227         }
228 }
229
230 static inline void
231 evdev_process_relative(struct evdev_input_device *device,
232                               struct input_event *e, uint32_t time)
233 {
234         switch (e->code) {
235         case REL_X:
236                 device->rel.dx += e->value;
237                 device->type |= EVDEV_RELATIVE_MOTION;
238                 break;
239         case REL_Y:
240                 device->rel.dy += e->value;
241                 device->type |= EVDEV_RELATIVE_MOTION;
242                 break;
243         case REL_WHEEL:
244                 notify_axis(&device->master->base.input_device,
245                               time,
246                               WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, e->value);
247                 break;
248         case REL_HWHEEL:
249                 notify_axis(&device->master->base.input_device,
250                               time,
251                               WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, e->value);
252                 break;
253         }
254 }
255
256 static inline void
257 evdev_process_absolute(struct evdev_input_device *device,
258                        struct input_event *e)
259 {
260         if (device->is_touchpad) {
261                 evdev_process_absolute_motion_touchpad(device, e);
262         } else if (device->is_mt) {
263                 evdev_process_touch(device, e);
264         } else {
265                 evdev_process_absolute_motion(device, e);
266         }
267 }
268
269 static int
270 is_motion_event(struct input_event *e)
271 {
272         switch (e->type) {
273         case EV_REL:
274                 switch (e->code) {
275                 case REL_X:
276                 case REL_Y:
277                         return 1;
278                 }
279         case EV_ABS:
280                 switch (e->code) {
281                 case ABS_X:
282                 case ABS_Y:
283                 case ABS_MT_POSITION_X:
284                 case ABS_MT_POSITION_Y:
285                         return 1;
286                 }
287         }
288
289         return 0;
290 }
291
292 static void
293 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
294 {
295         struct wl_input_device *master = &device->master->base.input_device;
296
297         if (!device->type)
298                 return;
299
300         if (device->type & EVDEV_RELATIVE_MOTION) {
301                 notify_motion(master, time,
302                               master->x + device->rel.dx,
303                               master->y + device->rel.dy);
304                 device->type &= ~EVDEV_RELATIVE_MOTION;
305                 device->rel.dx = 0;
306                 device->rel.dy = 0;
307         }
308         if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
309                 notify_touch(master, time,
310                              device->mt.slot,
311                              device->mt.x[device->mt.slot],
312                              device->mt.y[device->mt.slot],
313                              WL_INPUT_DEVICE_TOUCH_DOWN);
314                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
315                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
316         }
317         if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
318                 notify_touch(master, time,
319                              device->mt.slot,
320                              device->mt.x[device->mt.slot],
321                              device->mt.y[device->mt.slot],
322                              WL_INPUT_DEVICE_TOUCH_MOTION);
323                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
324                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
325         }
326         if (device->type & EVDEV_ABSOLUTE_MT_UP) {
327                 notify_touch(master, time, device->mt.slot, 0, 0,
328                              WL_INPUT_DEVICE_TOUCH_UP);
329                 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
330         }
331         if (device->type & EVDEV_ABSOLUTE_MOTION) {
332                 notify_motion(master, time, device->abs.x, device->abs.y);
333                 device->type &= ~EVDEV_ABSOLUTE_MOTION;
334         }
335 }
336
337 static void
338 evdev_process_events(struct evdev_input_device *device,
339                      struct input_event *ev, int count)
340 {
341         struct input_event *e, *end;
342         uint32_t time = 0;
343
344         device->type = 0;
345
346         e = ev;
347         end = e + count;
348         for (e = ev; e < end; e++) {
349                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
350
351                 /* we try to minimize the amount of notifications to be
352                  * forwarded to the compositor, so we accumulate motion
353                  * events and send as a bunch */
354                 if (!is_motion_event(e))
355                         evdev_flush_motion(device, time);
356                 switch (e->type) {
357                 case EV_REL:
358                         evdev_process_relative(device, e, time);
359                         break;
360                 case EV_ABS:
361                         evdev_process_absolute(device, e);
362                         break;
363                 case EV_KEY:
364                         evdev_process_key(device, e, time);
365                         break;
366                 }
367         }
368
369         evdev_flush_motion(device, time);
370 }
371
372 static int
373 evdev_input_device_data(int fd, uint32_t mask, void *data)
374 {
375         struct weston_compositor *ec;
376         struct evdev_input_device *device = data;
377         struct input_event ev[32];
378         int len;
379
380         ec = device->master->base.compositor;
381         if (!ec->focus)
382                 return 1;
383
384         /* If the compositor is repainting, this function is called only once
385          * per frame and we have to process all the events available on the
386          * fd, otherwise there will be input lag. */
387         do {
388                 if (device->mtdev)
389                         len = mtdev_get(device->mtdev, fd, ev,
390                                         ARRAY_LENGTH(ev)) *
391                                 sizeof (struct input_event);
392                 else
393                         len = read(fd, &ev, sizeof ev);
394
395                 if (len < 0 || len % sizeof ev[0] != 0) {
396                         /* FIXME: call device_removed when errno is ENODEV. */
397                         return 1;
398                 }
399
400                 evdev_process_events(device, ev, len / sizeof ev[0]);
401
402         } while (len > 0);
403
404         return 1;
405 }
406
407 /* copied from udev/extras/input_id/input_id.c */
408 /* we must use this kernel-compatible implementation */
409 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
410 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
411 #define OFF(x)  ((x)%BITS_PER_LONG)
412 #define BIT(x)  (1UL<<OFF(x))
413 #define LONG(x) ((x)/BITS_PER_LONG)
414 #define TEST_BIT(array, bit)    ((array[LONG(bit)] >> OFF(bit)) & 1)
415 /* end copied */
416
417 static int
418 evdev_configure_device(struct evdev_input_device *device)
419 {
420         struct input_absinfo absinfo;
421         unsigned long ev_bits[NBITS(EV_MAX)];
422         unsigned long abs_bits[NBITS(ABS_MAX)];
423         unsigned long key_bits[NBITS(KEY_MAX)];
424         int has_key, has_abs;
425
426         has_key = 0;
427         has_abs = 0;
428
429         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
430         if (TEST_BIT(ev_bits, EV_ABS)) {
431                 has_abs = 1;
432
433                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
434                       abs_bits);
435                 if (TEST_BIT(abs_bits, ABS_X)) {
436                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
437                         device->abs.min_x = absinfo.minimum;
438                         device->abs.max_x = absinfo.maximum;
439                 }
440                 if (TEST_BIT(abs_bits, ABS_Y)) {
441                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
442                         device->abs.min_y = absinfo.minimum;
443                         device->abs.max_y = absinfo.maximum;
444                 }
445                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
446                         device->is_mt = 1;
447                         device->mt.slot = 0;
448                 }
449         }
450         if (TEST_BIT(ev_bits, EV_KEY)) {
451                 has_key = 1;
452                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
453                       key_bits);
454                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
455                              !TEST_BIT(key_bits, BTN_TOOL_PEN))
456                         device->is_touchpad = 1;
457         }
458
459         /* This rule tries to catch accelerometer devices and opt out. We may
460          * want to adjust the protocol later adding a proper event for dealing
461          * with accelerometers and implement here accordingly */
462         if (has_abs && !has_key)
463                 return -1;
464
465         return 0;
466 }
467
468 static struct evdev_input_device *
469 evdev_input_device_create(struct evdev_input *master,
470                           struct wl_display *display, const char *path)
471 {
472         struct evdev_input_device *device;
473         struct weston_compositor *ec;
474
475         device = malloc(sizeof *device);
476         if (device == NULL)
477                 return NULL;
478
479         ec = master->base.compositor;
480         device->output =
481                 container_of(ec->output_list.next, struct weston_output, link);
482
483         device->master = master;
484         device->is_touchpad = 0;
485         device->is_mt = 0;
486         device->mtdev = NULL;
487         device->devnode = strdup(path);
488         device->mt.slot = -1;
489         device->rel.dx = 0;
490         device->rel.dy = 0;
491
492         /* Use non-blocking mode so that we can loop on read on
493          * evdev_input_device_data() until all events on the fd are
494          * read.  mtdev_get() also expects this. */
495         device->fd = open(path, O_RDONLY | O_NONBLOCK);
496         if (device->fd < 0)
497                 goto err0;
498
499         if (evdev_configure_device(device) == -1)
500                 goto err1;
501
502         if (device->is_mt) {
503                 device->mtdev = mtdev_new_open(device->fd);
504                 if (!device->mtdev)
505                         fprintf(stderr, "mtdev failed to open for %s\n", path);
506         }
507
508         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
509                                               WL_EVENT_READABLE,
510                                               evdev_input_device_data, device);
511         if (device->source == NULL)
512                 goto err1;
513
514         wl_list_insert(master->devices_list.prev, &device->link);
515
516         return device;
517
518 err1:
519         close(device->fd);
520 err0:
521         free(device->devnode);
522         free(device);
523         return NULL;
524 }
525
526 static const char default_seat[] = "seat0";
527
528 static void
529 device_added(struct udev_device *udev_device, struct evdev_input *master)
530 {
531         struct weston_compositor *c;
532         const char *devnode;
533         const char *device_seat;
534
535         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
536         if (!device_seat)
537                 device_seat = default_seat;
538
539         if (strcmp(device_seat, master->seat_id))
540                 return;
541
542         c = master->base.compositor;
543         devnode = udev_device_get_devnode(udev_device);
544         evdev_input_device_create(master, c->wl_display, devnode);
545 }
546
547 static void
548 device_removed(struct evdev_input_device *device)
549 {
550         wl_event_source_remove(device->source);
551         wl_list_remove(&device->link);
552         if (device->mtdev)
553                 mtdev_close_delete(device->mtdev);
554         close(device->fd);
555         free(device->devnode);
556         free(device);
557 }
558
559 void
560 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
561 {
562         struct evdev_input *input = (struct evdev_input *) input_base;
563         struct udev_enumerate *e;
564         struct udev_list_entry *entry;
565         struct udev_device *device;
566         const char *path, *sysname;
567
568         e = udev_enumerate_new(udev);
569         udev_enumerate_add_match_subsystem(e, "input");
570         udev_enumerate_scan_devices(e);
571         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
572                 path = udev_list_entry_get_name(entry);
573                 device = udev_device_new_from_syspath(udev, path);
574
575                 sysname = udev_device_get_sysname(device);
576                 if (strncmp("event", sysname, 5) != 0) {
577                         udev_device_unref(device);
578                         continue;
579                 }
580
581                 device_added(device, input);
582
583                 udev_device_unref(device);
584         }
585         udev_enumerate_unref(e);
586
587         if (wl_list_empty(&input->devices_list)) {
588                 fprintf(stderr,
589                         "warning: no input devices on entering Weston. "
590                         "Possible causes:\n"
591                         "\t- no permissions to read /dev/input/event*\n"
592                         "\t- seats misconfigured "
593                         "(Weston backend option 'seat', "
594                         "udev device property ID_SEAT)\n");
595         }
596 }
597
598 static int
599 evdev_udev_handler(int fd, uint32_t mask, void *data)
600 {
601         struct evdev_input *master = data;
602         struct udev_device *udev_device;
603         struct evdev_input_device *device, *next;
604         const char *action;
605         const char *devnode;
606
607         udev_device = udev_monitor_receive_device(master->udev_monitor);
608         if (!udev_device)
609                 return 1;
610
611         action = udev_device_get_action(udev_device);
612         if (action) {
613                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
614                         return 0;
615
616                 if (!strcmp(action, "add")) {
617                         device_added(udev_device, master);
618                 }
619                 else if (!strcmp(action, "remove")) {
620                         devnode = udev_device_get_devnode(udev_device);
621                         wl_list_for_each_safe(device, next,
622                                               &master->devices_list, link)
623                                 if (!strcmp(device->devnode, devnode)) {
624                                         device_removed(device);
625                                         break;
626                                 }
627                 }
628         }
629         udev_device_unref(udev_device);
630
631         return 0;
632 }
633
634 static int
635 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
636 {
637         struct wl_event_loop *loop;
638         struct weston_compositor *c = master->base.compositor;
639         int fd;
640
641         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
642         if (!master->udev_monitor) {
643                 fprintf(stderr, "udev: failed to create the udev monitor\n");
644                 return 0;
645         }
646
647         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
648                         "input", NULL);
649
650         if (udev_monitor_enable_receiving(master->udev_monitor)) {
651                 fprintf(stderr, "udev: failed to bind the udev monitor\n");
652                 udev_monitor_unref(master->udev_monitor);
653                 return 0;
654         }
655
656         loop = wl_display_get_event_loop(c->wl_display);
657         fd = udev_monitor_get_fd(master->udev_monitor);
658         master->udev_monitor_source =
659                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
660                                      evdev_udev_handler, master);
661         if (!master->udev_monitor_source) {
662                 udev_monitor_unref(master->udev_monitor);
663                 return 0;
664         }
665
666         return 1;
667 }
668
669 void
670 evdev_input_create(struct weston_compositor *c, struct udev *udev,
671                    const char *seat)
672 {
673         struct evdev_input *input;
674
675         input = malloc(sizeof *input);
676         if (input == NULL)
677                 return;
678
679         memset(input, 0, sizeof *input);
680         weston_input_device_init(&input->base, c);
681
682         wl_list_init(&input->devices_list);
683         input->seat_id = strdup(seat);
684         if (!evdev_config_udev_monitor(udev, input)) {
685                 free(input->seat_id);
686                 free(input);
687                 return;
688         }
689
690         evdev_add_devices(udev, &input->base);
691
692         c->input_device = &input->base.input_device;
693 }
694
695 void
696 evdev_remove_devices(struct weston_input_device *input_base)
697 {
698         struct evdev_input *input = (struct evdev_input *) input_base;
699         struct evdev_input_device *device, *next;
700
701         wl_list_for_each_safe(device, next, &input->devices_list, link)
702                 device_removed(device);
703 }
704
705 void
706 evdev_input_destroy(struct weston_input_device *input_base)
707 {
708         struct evdev_input *input = (struct evdev_input *) input_base;
709
710         evdev_remove_devices(input_base);
711
712         udev_monitor_unref(input->udev_monitor);
713         wl_event_source_remove(input->udev_monitor_source);
714
715         wl_list_remove(&input->base.link);
716         free(input->seat_id);
717         free(input);
718 }