Track server changes.
[profile/ivi/weston.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 wl_event_loop *loop;
441         struct weston_compositor *ec;
442
443         device = malloc(sizeof *device);
444         if (device == NULL)
445                 return NULL;
446
447         ec = master->base.compositor;
448         device->output =
449                 container_of(ec->output_list.next, struct weston_output, link);
450
451         device->master = master;
452         device->is_touchpad = 0;
453         device->is_mt = 0;
454         device->devnode = strdup(path);
455         device->mt.slot = -1;
456         device->rel.dx = 0;
457         device->rel.dy = 0;
458
459         device->fd = open(path, O_RDONLY);
460         if (device->fd < 0)
461                 goto err0;
462
463         if (evdev_configure_device(device) == -1)
464                 goto err1;
465
466         loop = wl_display_get_event_loop(display);
467         device->source = wl_event_loop_add_fd(loop, device->fd,
468                                               WL_EVENT_READABLE,
469                                               evdev_input_device_data, device);
470         if (device->source == NULL)
471                 goto err1;
472
473         wl_list_insert(master->devices_list.prev, &device->link);
474
475         return device;
476
477 err1:
478         close(device->fd);
479 err0:
480         free(device->devnode);
481         free(device);
482         return NULL;
483 }
484
485 static const char default_seat[] = "seat0";
486
487 static void
488 device_added(struct udev_device *udev_device, struct evdev_input *master)
489 {
490         struct weston_compositor *c;
491         const char *devnode;
492         const char *device_seat;
493
494         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
495         if (!device_seat)
496                 device_seat = default_seat;
497
498         if (strcmp(device_seat, master->seat_id))
499                 return;
500
501         c = master->base.compositor;
502         devnode = udev_device_get_devnode(udev_device);
503         evdev_input_device_create(master, c->wl_display, devnode);
504 }
505
506 static void
507 device_removed(struct udev_device *udev_device, struct evdev_input *master)
508 {
509         const char *devnode = udev_device_get_devnode(udev_device);
510         struct evdev_input_device *device, *next;
511
512         wl_list_for_each_safe(device, next, &master->devices_list, link) {
513                 if (!strcmp(device->devnode, devnode)) {
514                         wl_event_source_remove(device->source);
515                         wl_list_remove(&device->link);
516                         close(device->fd);
517                         free(device->devnode);
518                         free(device);
519                         break;
520                 }
521         }
522 }
523
524 void
525 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
526 {
527         struct evdev_input *input = (struct evdev_input *) input_base;
528         struct udev_enumerate *e;
529         struct udev_list_entry *entry;
530         struct udev_device *device;
531         const char *path;
532
533         e = udev_enumerate_new(udev);
534         udev_enumerate_add_match_subsystem(e, "input");
535         udev_enumerate_scan_devices(e);
536         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
537                 path = udev_list_entry_get_name(entry);
538                 device = udev_device_new_from_syspath(udev, path);
539
540                 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
541                         continue;
542
543                 device_added(device, input);
544
545                 udev_device_unref(device);
546         }
547         udev_enumerate_unref(e);
548
549         if (wl_list_empty(&input->devices_list)) {
550                 fprintf(stderr,
551                         "warning: no input devices on entering Weston. "
552                         "Possible causes:\n"
553                         "\t- no permissions to read /dev/input/event*\n"
554                         "\t- seats misconfigured "
555                         "(Weston backend option 'seat', "
556                         "udev device property ID_SEAT)\n");
557         }
558 }
559
560 static int
561 evdev_udev_handler(int fd, uint32_t mask, void *data)
562 {
563         struct evdev_input *master = data;
564         struct udev_device *udev_device;
565         const char *action;
566
567         udev_device = udev_monitor_receive_device(master->udev_monitor);
568         if (!udev_device)
569                 return 1;
570
571         action = udev_device_get_action(udev_device);
572         if (action) {
573                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
574                         return 0;
575
576                 if (!strcmp(action, "add")) {
577                         device_added(udev_device, master);
578                 }
579                 else if (!strcmp(action, "remove"))
580                         device_removed(udev_device, master);
581         }
582         udev_device_unref(udev_device);
583
584         return 0;
585 }
586
587 static int
588 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
589 {
590         struct wl_event_loop *loop;
591         struct weston_compositor *c = master->base.compositor;
592
593         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
594         if (!master->udev_monitor) {
595                 fprintf(stderr, "udev: failed to create the udev monitor\n");
596                 return 0;
597         }
598
599         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
600                         "input", NULL);
601
602         if (udev_monitor_enable_receiving(master->udev_monitor)) {
603                 fprintf(stderr, "udev: failed to bind the udev monitor\n");
604                 return 0;
605         }
606
607         loop = wl_display_get_event_loop(c->wl_display);
608         wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
609                              WL_EVENT_READABLE, evdev_udev_handler, master);
610
611         return 1;
612 }
613
614 void
615 evdev_input_create(struct weston_compositor *c, struct udev *udev,
616                    const char *seat)
617 {
618         struct evdev_input *input;
619
620         input = malloc(sizeof *input);
621         if (input == NULL)
622                 return;
623
624         memset(input, 0, sizeof *input);
625         weston_input_device_init(&input->base, c);
626
627         wl_list_init(&input->devices_list);
628         input->seat_id = strdup(seat);
629         if (!evdev_config_udev_monitor(udev, input)) {
630                 free(input->seat_id);
631                 free(input);
632                 return;
633         }
634
635         evdev_add_devices(udev, &input->base);
636
637         c->input_device = &input->base.input_device;
638 }
639
640 void
641 evdev_remove_devices(struct weston_input_device *input_base)
642 {
643         struct evdev_input *input = (struct evdev_input *) input_base;
644         struct evdev_input_device *device, *next;
645
646         wl_list_for_each_safe(device, next, &input->devices_list, link) {
647                 wl_event_source_remove(device->source);
648                 wl_list_remove(&device->link);
649                 close(device->fd);
650                 free(device->devnode);
651                 free(device);
652         }
653 }
654
655 void
656 evdev_input_destroy(struct weston_input_device *input_base)
657 {
658         struct evdev_input *input = (struct evdev_input *) input_base;
659
660         evdev_remove_devices(input_base);
661         wl_list_remove(&input->base.link);
662         free(input->seat_id);
663         free(input);
664 }