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