evdev: Add support for device quirks and implement axes swapping
[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 <stdlib.h>
24 #include <string.h>
25 #include <linux/input.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <mtdev.h>
29
30 #include "compositor.h"
31 #include "evdev.h"
32
33 void
34 evdev_led_update(struct evdev_device *device, enum weston_led leds)
35 {
36         static const struct {
37                 enum weston_led weston;
38                 int evdev;
39         } map[] = {
40                 { LED_NUM_LOCK, LED_NUML },
41                 { LED_CAPS_LOCK, LED_CAPSL },
42                 { LED_SCROLL_LOCK, LED_SCROLLL },
43         };
44         struct input_event ev[ARRAY_LENGTH(map)];
45         unsigned int i;
46
47         if (!device->caps & EVDEV_KEYBOARD)
48                 return;
49
50         memset(ev, 0, sizeof(ev));
51         for (i = 0; i < ARRAY_LENGTH(map); i++) {
52                 ev[i].type = EV_LED;
53                 ev[i].code = map[i].evdev;
54                 ev[i].value = !!(leds & map[i].weston);
55         }
56
57         i = write(device->fd, ev, sizeof ev);
58         (void)i; /* no, we really don't care about the return value */
59 }
60
61 static inline void
62 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
63 {
64         if (e->value == 2)
65                 return;
66
67         switch (e->code) {
68         case BTN_LEFT:
69         case BTN_RIGHT:
70         case BTN_MIDDLE:
71         case BTN_SIDE:
72         case BTN_EXTRA:
73         case BTN_FORWARD:
74         case BTN_BACK:
75         case BTN_TASK:
76                 notify_button(device->seat,
77                               time, e->code,
78                               e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
79                                          WL_POINTER_BUTTON_STATE_RELEASED);
80                 break;
81
82         default:
83                 notify_key(device->seat,
84                            time, e->code,
85                            e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
86                                       WL_KEYBOARD_KEY_STATE_RELEASED,
87                            STATE_UPDATE_AUTOMATIC);
88                 break;
89         }
90 }
91
92 static void
93 evdev_process_touch(struct evdev_device *device, struct input_event *e)
94 {
95         const int screen_width = device->output->current->width;
96         const int screen_height = device->output->current->height;
97
98         switch (e->code) {
99         case ABS_MT_SLOT:
100                 device->mt.slot = e->value;
101                 break;
102         case ABS_MT_TRACKING_ID:
103                 if (e->value >= 0)
104                         device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
105                 else
106                         device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
107                 break;
108         case ABS_MT_POSITION_X:
109                 device->mt.x[device->mt.slot] =
110                         (e->value - device->abs.min_x) * screen_width /
111                         (device->abs.max_x - device->abs.min_x) +
112                         device->output->x;
113                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
114                 break;
115         case ABS_MT_POSITION_Y:
116                 device->mt.y[device->mt.slot] =
117                         (e->value - device->abs.min_y) * screen_height /
118                         (device->abs.max_y - device->abs.min_y) +
119                         device->output->y;
120                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
121                 break;
122         }
123 }
124
125 static inline void
126 evdev_process_absolute_motion(struct evdev_device *device,
127                               struct input_event *e)
128 {
129         const int screen_width = device->output->current->width;
130         const int screen_height = device->output->current->height;
131
132         if (device->quirks & EVDEV_QUIRK_SWAP_AXES) {
133                 switch (e->code) {
134                 case ABS_X:
135                         device->abs.y =
136                                 (e->value - device->abs.min_y) * screen_height /
137                                 (device->abs.max_y - device->abs.min_y) +
138                                 device->output->y;
139                         device->pending_events |= EVDEV_ABSOLUTE_MOTION;
140                         break;
141                 case ABS_Y:
142                         device->abs.x =
143                                 (e->value - device->abs.min_x) * screen_width /
144                                 (device->abs.max_x - device->abs.min_x) +
145                                 device->output->x;
146                         device->pending_events |= EVDEV_ABSOLUTE_MOTION;
147                         break;
148                 }
149
150         } else {
151                 switch (e->code) {
152                 case ABS_X:
153                         device->abs.x =
154                                 (e->value - device->abs.min_x) * screen_width /
155                                 (device->abs.max_x - device->abs.min_x) +
156                                 device->output->x;
157                         device->pending_events |= EVDEV_ABSOLUTE_MOTION;
158                         break;
159                 case ABS_Y:
160                         device->abs.y =
161                                 (e->value - device->abs.min_y) * screen_height /
162                                 (device->abs.max_y - device->abs.min_y) +
163                                 device->output->y;
164                         device->pending_events |= EVDEV_ABSOLUTE_MOTION;
165                         break;
166                 }
167         }
168 }
169
170 static inline void
171 evdev_process_relative(struct evdev_device *device,
172                        struct input_event *e, uint32_t time)
173 {
174         switch (e->code) {
175         case REL_X:
176                 device->rel.dx += wl_fixed_from_int(e->value);
177                 device->pending_events |= EVDEV_RELATIVE_MOTION;
178                 break;
179         case REL_Y:
180                 device->rel.dy += wl_fixed_from_int(e->value);
181                 device->pending_events |= EVDEV_RELATIVE_MOTION;
182                 break;
183         case REL_WHEEL:
184                 notify_axis(device->seat,
185                               time,
186                               WL_POINTER_AXIS_VERTICAL_SCROLL,
187                               wl_fixed_from_int(e->value));
188                 break;
189         case REL_HWHEEL:
190                 notify_axis(device->seat,
191                               time,
192                               WL_POINTER_AXIS_HORIZONTAL_SCROLL,
193                               wl_fixed_from_int(e->value));
194                 break;
195         }
196 }
197
198 static inline void
199 evdev_process_absolute(struct evdev_device *device, struct input_event *e)
200 {
201         if (device->is_mt) {
202                 evdev_process_touch(device, e);
203         } else {
204                 evdev_process_absolute_motion(device, e);
205         }
206 }
207
208 static int
209 is_motion_event(struct input_event *e)
210 {
211         switch (e->type) {
212         case EV_REL:
213                 switch (e->code) {
214                 case REL_X:
215                 case REL_Y:
216                         return 1;
217                 }
218         case EV_ABS:
219                 switch (e->code) {
220                 case ABS_X:
221                 case ABS_Y:
222                 case ABS_MT_POSITION_X:
223                 case ABS_MT_POSITION_Y:
224                         return 1;
225                 }
226         }
227
228         return 0;
229 }
230
231 static void
232 evdev_flush_motion(struct evdev_device *device, uint32_t time)
233 {
234         struct weston_seat *master = device->seat;
235
236         if (!device->pending_events)
237                 return;
238
239         if (device->pending_events & EVDEV_RELATIVE_MOTION) {
240                 notify_motion(master, time,
241                               master->seat.pointer->x + device->rel.dx,
242                               master->seat.pointer->y + device->rel.dy);
243                 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
244                 device->rel.dx = 0;
245                 device->rel.dy = 0;
246         }
247         if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
248                 notify_touch(master, time,
249                              device->mt.slot,
250                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
251                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
252                              WL_TOUCH_DOWN);
253                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
254                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
255         }
256         if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
257                 notify_touch(master, time,
258                              device->mt.slot,
259                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
260                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
261                              WL_TOUCH_MOTION);
262                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
263                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
264         }
265         if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
266                 notify_touch(master, time, device->mt.slot, 0, 0,
267                              WL_TOUCH_UP);
268                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
269         }
270         if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
271                 notify_motion(master, time,
272                               wl_fixed_from_int(device->abs.x),
273                               wl_fixed_from_int(device->abs.y));
274                 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
275         }
276 }
277
278 static void
279 fallback_process(struct evdev_dispatch *dispatch,
280                  struct evdev_device *device,
281                  struct input_event *event,
282                  uint32_t time)
283 {
284         switch (event->type) {
285         case EV_REL:
286                 evdev_process_relative(device, event, time);
287                 break;
288         case EV_ABS:
289                 evdev_process_absolute(device, event);
290                 break;
291         case EV_KEY:
292                 evdev_process_key(device, event, time);
293                 break;
294         }
295 }
296
297 static void
298 fallback_destroy(struct evdev_dispatch *dispatch)
299 {
300         free(dispatch);
301 }
302
303 struct evdev_dispatch_interface fallback_interface = {
304         fallback_process,
305         fallback_destroy
306 };
307
308 static struct evdev_dispatch *
309 fallback_dispatch_create(void)
310 {
311         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
312         if (dispatch == NULL)
313                 return NULL;
314
315         dispatch->interface = &fallback_interface;
316
317         return dispatch;
318 }
319
320 static void
321 evdev_process_events(struct evdev_device *device,
322                      struct input_event *ev, int count)
323 {
324         struct evdev_dispatch *dispatch = device->dispatch;
325         struct input_event *e, *end;
326         uint32_t time = 0;
327
328         device->pending_events = 0;
329
330         e = ev;
331         end = e + count;
332         for (e = ev; e < end; e++) {
333                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
334
335                 /* we try to minimize the amount of notifications to be
336                  * forwarded to the compositor, so we accumulate motion
337                  * events and send as a bunch */
338                 if (!is_motion_event(e))
339                         evdev_flush_motion(device, time);
340
341                 dispatch->interface->process(dispatch, device, e, time);
342         }
343
344         evdev_flush_motion(device, time);
345 }
346
347 static int
348 evdev_device_data(int fd, uint32_t mask, void *data)
349 {
350         struct weston_compositor *ec;
351         struct evdev_device *device = data;
352         struct input_event ev[32];
353         int len;
354
355         ec = device->seat->compositor;
356         if (!ec->focus)
357                 return 1;
358
359         /* If the compositor is repainting, this function is called only once
360          * per frame and we have to process all the events available on the
361          * fd, otherwise there will be input lag. */
362         do {
363                 if (device->mtdev)
364                         len = mtdev_get(device->mtdev, fd, ev,
365                                         ARRAY_LENGTH(ev)) *
366                                 sizeof (struct input_event);
367                 else
368                         len = read(fd, &ev, sizeof ev);
369
370                 if (len < 0 || len % sizeof ev[0] != 0) {
371                         /* FIXME: call evdev_device_destroy when errno is ENODEV. */
372                         return 1;
373                 }
374
375                 evdev_process_events(device, ev, len / sizeof ev[0]);
376
377         } while (len > 0);
378
379         return 1;
380 }
381
382 static int
383 evdev_configure_device(struct evdev_device *device)
384 {
385         struct input_absinfo absinfo;
386         unsigned long ev_bits[NBITS(EV_MAX)];
387         unsigned long abs_bits[NBITS(ABS_MAX)];
388         unsigned long rel_bits[NBITS(REL_MAX)];
389         unsigned long key_bits[NBITS(KEY_MAX)];
390         int has_key, has_abs;
391         unsigned int i;
392
393         has_key = 0;
394         has_abs = 0;
395         device->caps = 0;
396
397         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
398         if (TEST_BIT(ev_bits, EV_ABS)) {
399                 has_abs = 1;
400
401                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
402                       abs_bits);
403                 if (TEST_BIT(abs_bits, ABS_X)) {
404                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
405                         device->abs.min_x = absinfo.minimum;
406                         device->abs.max_x = absinfo.maximum;
407                         device->caps |= EVDEV_MOTION_ABS;
408                 }
409                 if (TEST_BIT(abs_bits, ABS_Y)) {
410                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
411                         device->abs.min_y = absinfo.minimum;
412                         device->abs.max_y = absinfo.maximum;
413                         device->caps |= EVDEV_MOTION_ABS;
414                 }
415                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
416                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
417                               &absinfo);
418                         if (device->quirks & EVDEV_QUIRK_SWAP_AXES) {
419                                 device->abs.min_y = absinfo.minimum;
420                                 device->abs.max_y = absinfo.maximum;
421                         } else {
422                                 device->abs.min_x = absinfo.minimum;
423                                 device->abs.max_x = absinfo.maximum;
424                         }
425                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
426                               &absinfo);
427                         if (device->quirks & EVDEV_QUIRK_SWAP_AXES) {
428                                 device->abs.min_x = absinfo.minimum;
429                                 device->abs.max_x = absinfo.maximum;
430                         } else {
431                                 device->abs.min_y = absinfo.minimum;
432                                 device->abs.max_y = absinfo.maximum;
433                         }
434                         device->is_mt = 1;
435                         device->mt.slot = 0;
436                         device->caps |= EVDEV_TOUCH;
437                 }
438         }
439         if (TEST_BIT(ev_bits, EV_REL)) {
440                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
441                       rel_bits);
442                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
443                         device->caps |= EVDEV_MOTION_REL;
444         }
445         if (TEST_BIT(ev_bits, EV_KEY)) {
446                 has_key = 1;
447                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
448                       key_bits);
449                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
450                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
451                     has_abs)
452                         device->dispatch = evdev_touchpad_create(device);
453                 for (i = KEY_ESC; i < KEY_MAX; i++) {
454                         if (i >= BTN_MISC && i < KEY_OK)
455                                 continue;
456                         if (TEST_BIT(key_bits, i)) {
457                                 device->caps |= EVDEV_KEYBOARD;
458                                 break;
459                         }
460                 }
461                 for (i = BTN_MISC; i < KEY_OK; i++) {
462                         if (TEST_BIT(key_bits, i)) {
463                                 device->caps |= EVDEV_BUTTON;
464                                 break;
465                         }
466                 }
467         }
468         if (TEST_BIT(ev_bits, EV_LED)) {
469                 device->caps |= EVDEV_KEYBOARD;
470         }
471
472         /* This rule tries to catch accelerometer devices and opt out. We may
473          * want to adjust the protocol later adding a proper event for dealing
474          * with accelerometers and implement here accordingly */
475         if (has_abs && !has_key && !device->is_mt) {
476                 weston_log("input device %s, %s "
477                            "ignored: unsupported device type\n",
478                            device->devname, device->devnode);
479                 return -1;
480         }
481
482         if ((device->caps &
483              (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
484                 weston_seat_init_pointer(device->seat);
485                 weston_log("input device %s, %s is a pointer\n",
486                            device->devname, device->devnode);
487         }
488         if ((device->caps & EVDEV_KEYBOARD)) {
489                 weston_seat_init_keyboard(device->seat, NULL);
490                 weston_log("input device %s, %s is a keyboard\n",
491                            device->devname, device->devnode);
492         }
493         if ((device->caps & EVDEV_TOUCH)) {
494                 weston_seat_init_touch(device->seat);
495                 weston_log("input device %s, %s is a touch device\n",
496                            device->devname, device->devnode);
497         }
498
499         return 0;
500 }
501
502 struct evdev_device *
503 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
504 {
505         struct evdev_device *device;
506         struct weston_compositor *ec;
507         char devname[256] = "unknown";
508
509         device = malloc(sizeof *device);
510         if (device == NULL)
511                 return NULL;
512         memset(device, 0, sizeof *device);
513
514         ec = seat->compositor;
515         device->output =
516                 container_of(ec->output_list.next, struct weston_output, link);
517
518         device->seat = seat;
519         device->is_mt = 0;
520         device->mtdev = NULL;
521         device->devnode = strdup(path);
522         device->mt.slot = -1;
523         device->rel.dx = 0;
524         device->rel.dy = 0;
525         device->dispatch = NULL;
526         device->fd = device_fd;
527
528         ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
529         device->devname = strdup(devname);
530
531         if (evdev_configure_device(device) == -1)
532                 goto err1;
533
534         /* If the dispatch was not set up use the fallback. */
535         if (device->dispatch == NULL)
536                 device->dispatch = fallback_dispatch_create();
537         if (device->dispatch == NULL)
538                 goto err1;
539
540
541         if (device->is_mt) {
542                 device->mtdev = mtdev_new_open(device->fd);
543                 if (!device->mtdev)
544                         weston_log("mtdev failed to open for %s\n", path);
545         }
546
547         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
548                                               WL_EVENT_READABLE,
549                                               evdev_device_data, device);
550         if (device->source == NULL)
551                 goto err2;
552
553         return device;
554
555 err2:
556         device->dispatch->interface->destroy(device->dispatch);
557 err1:
558         free(device->devname);
559         free(device->devnode);
560         free(device);
561         return NULL;
562 }
563
564 void
565 evdev_device_destroy(struct evdev_device *device)
566 {
567         struct evdev_dispatch *dispatch;
568
569         dispatch = device->dispatch;
570         if (dispatch)
571                 dispatch->interface->destroy(dispatch);
572
573         wl_event_source_remove(device->source);
574         wl_list_remove(&device->link);
575         if (device->mtdev)
576                 mtdev_close_delete(device->mtdev);
577         close(device->fd);
578         free(device->devname);
579         free(device->devnode);
580         free(device);
581 }
582
583 void
584 evdev_notify_keyboard_focus(struct weston_seat *seat,
585                             struct wl_list *evdev_devices)
586 {
587         struct evdev_device *device;
588         struct wl_array keys;
589         unsigned int i, set;
590         char evdev_keys[(KEY_CNT + 7) / 8];
591         char all_keys[(KEY_CNT + 7) / 8];
592         uint32_t *k;
593         int ret;
594
595         if (!seat->seat.keyboard)
596                 return;
597
598         memset(all_keys, 0, sizeof all_keys);
599         wl_list_for_each(device, evdev_devices, link) {
600                 memset(evdev_keys, 0, sizeof evdev_keys);
601                 ret = ioctl(device->fd,
602                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
603                 if (ret < 0) {
604                         weston_log("failed to get keys for device %s\n",
605                                 device->devnode);
606                         continue;
607                 }
608                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
609                         all_keys[i] |= evdev_keys[i];
610         }
611
612         wl_array_init(&keys);
613         for (i = 0; i < KEY_CNT; i++) {
614                 set = all_keys[i >> 3] & (1 << (i & 7));
615                 if (set) {
616                         k = wl_array_add(&keys, sizeof *k);
617                         *k = i;
618                 }
619         }
620
621         notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
622
623         wl_array_release(&keys);
624 }