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