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