321992e85f14dcea7e325c7fe49713ed62bac281
[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         switch (e->code) {
135         case ABS_X:
136                 device->abs.x =
137                         (e->value - device->abs.min_x) * screen_width /
138                         (device->abs.max_x - device->abs.min_x) +
139                         device->output->x;
140                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
141                 break;
142         case ABS_Y:
143                 device->abs.y =
144                         (e->value - device->abs.min_y) * screen_height /
145                         (device->abs.max_y - device->abs.min_y) +
146                         device->output->y;
147                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
148                 break;
149         }
150 }
151
152 static inline void
153 evdev_process_relative(struct evdev_device *device,
154                        struct input_event *e, uint32_t time)
155 {
156         switch (e->code) {
157         case REL_X:
158                 device->rel.dx += wl_fixed_from_int(e->value);
159                 device->pending_events |= EVDEV_RELATIVE_MOTION;
160                 break;
161         case REL_Y:
162                 device->rel.dy += wl_fixed_from_int(e->value);
163                 device->pending_events |= EVDEV_RELATIVE_MOTION;
164                 break;
165         case REL_WHEEL:
166                 switch (e->value) {
167                 case -1:
168                         /* Scroll down */
169                 case 1:
170                         /* Scroll up */
171                         notify_axis(device->seat,
172                                     time,
173                                     WL_POINTER_AXIS_VERTICAL_SCROLL,
174                                     -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
175                         break;
176                 default:
177                         break;
178                 }
179                 break;
180         case REL_HWHEEL:
181                 switch (e->value) {
182                 case -1:
183                         /* Scroll left */
184                 case 1:
185                         /* Scroll right */
186                         notify_axis(device->seat,
187                                     time,
188                                     WL_POINTER_AXIS_HORIZONTAL_SCROLL,
189                                     e->value * DEFAULT_AXIS_STEP_DISTANCE);
190                         break;
191                 default:
192                         break;
193
194                 }
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                 break;
219         case EV_ABS:
220                 switch (e->code) {
221                 case ABS_X:
222                 case ABS_Y:
223                 case ABS_MT_POSITION_X:
224                 case ABS_MT_POSITION_Y:
225                         return 1;
226                 }
227         }
228
229         return 0;
230 }
231
232 static void
233 evdev_flush_motion(struct evdev_device *device, uint32_t time)
234 {
235         struct weston_seat *master = device->seat;
236
237         if (!device->pending_events)
238                 return;
239
240         if (device->pending_events & EVDEV_RELATIVE_MOTION) {
241                 notify_motion(master, time,
242                               master->seat.pointer->x + device->rel.dx,
243                               master->seat.pointer->y + device->rel.dy);
244                 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
245                 device->rel.dx = 0;
246                 device->rel.dy = 0;
247         }
248         if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
249                 notify_touch(master, time,
250                              device->mt.slot,
251                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
252                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
253                              WL_TOUCH_DOWN);
254                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
255                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
256         }
257         if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
258                 notify_touch(master, time,
259                              device->mt.slot,
260                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
261                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
262                              WL_TOUCH_MOTION);
263                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
264                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
265         }
266         if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
267                 notify_touch(master, time, device->mt.slot, 0, 0,
268                              WL_TOUCH_UP);
269                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
270         }
271         if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
272                 notify_motion(master, time,
273                               wl_fixed_from_int(device->abs.x),
274                               wl_fixed_from_int(device->abs.y));
275                 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
276         }
277 }
278
279 static void
280 fallback_process(struct evdev_dispatch *dispatch,
281                  struct evdev_device *device,
282                  struct input_event *event,
283                  uint32_t time)
284 {
285         switch (event->type) {
286         case EV_REL:
287                 evdev_process_relative(device, event, time);
288                 break;
289         case EV_ABS:
290                 evdev_process_absolute(device, event);
291                 break;
292         case EV_KEY:
293                 evdev_process_key(device, event, time);
294                 break;
295         }
296 }
297
298 static void
299 fallback_destroy(struct evdev_dispatch *dispatch)
300 {
301         free(dispatch);
302 }
303
304 struct evdev_dispatch_interface fallback_interface = {
305         fallback_process,
306         fallback_destroy
307 };
308
309 static struct evdev_dispatch *
310 fallback_dispatch_create(void)
311 {
312         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
313         if (dispatch == NULL)
314                 return NULL;
315
316         dispatch->interface = &fallback_interface;
317
318         return dispatch;
319 }
320
321 static void
322 evdev_process_events(struct evdev_device *device,
323                      struct input_event *ev, int count)
324 {
325         struct evdev_dispatch *dispatch = device->dispatch;
326         struct input_event *e, *end;
327         uint32_t time = 0;
328
329         device->pending_events = 0;
330
331         e = ev;
332         end = e + count;
333         for (e = ev; e < end; e++) {
334                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
335
336                 /* we try to minimize the amount of notifications to be
337                  * forwarded to the compositor, so we accumulate motion
338                  * events and send as a bunch */
339                 if (!is_motion_event(e))
340                         evdev_flush_motion(device, time);
341
342                 dispatch->interface->process(dispatch, device, e, time);
343         }
344
345         evdev_flush_motion(device, time);
346 }
347
348 static int
349 evdev_device_data(int fd, uint32_t mask, void *data)
350 {
351         struct weston_compositor *ec;
352         struct evdev_device *device = data;
353         struct input_event ev[32];
354         int len;
355
356         ec = device->seat->compositor;
357         if (!ec->focus)
358                 return 1;
359
360         /* If the compositor is repainting, this function is called only once
361          * per frame and we have to process all the events available on the
362          * fd, otherwise there will be input lag. */
363         do {
364                 if (device->mtdev)
365                         len = mtdev_get(device->mtdev, fd, ev,
366                                         ARRAY_LENGTH(ev)) *
367                                 sizeof (struct input_event);
368                 else
369                         len = read(fd, &ev, sizeof ev);
370
371                 if (len < 0 || len % sizeof ev[0] != 0) {
372                         /* FIXME: call evdev_device_destroy when errno is ENODEV. */
373                         return 1;
374                 }
375
376                 evdev_process_events(device, ev, len / sizeof ev[0]);
377
378         } while (len > 0);
379
380         return 1;
381 }
382
383 static int
384 evdev_handle_device(struct evdev_device *device)
385 {
386         struct input_absinfo absinfo;
387         unsigned long ev_bits[NBITS(EV_MAX)];
388         unsigned long abs_bits[NBITS(ABS_MAX)];
389         unsigned long rel_bits[NBITS(REL_MAX)];
390         unsigned long key_bits[NBITS(KEY_MAX)];
391         int has_key, has_abs;
392         unsigned int i;
393
394         has_key = 0;
395         has_abs = 0;
396         device->caps = 0;
397
398         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
399         if (TEST_BIT(ev_bits, EV_ABS)) {
400                 has_abs = 1;
401
402                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
403                       abs_bits);
404                 if (TEST_BIT(abs_bits, ABS_X)) {
405                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
406                         device->abs.min_x = absinfo.minimum;
407                         device->abs.max_x = absinfo.maximum;
408                         device->caps |= EVDEV_MOTION_ABS;
409                 }
410                 if (TEST_BIT(abs_bits, ABS_Y)) {
411                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
412                         device->abs.min_y = absinfo.minimum;
413                         device->abs.max_y = absinfo.maximum;
414                         device->caps |= EVDEV_MOTION_ABS;
415                 }
416                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
417                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
418                               &absinfo);
419                         device->abs.min_x = absinfo.minimum;
420                         device->abs.max_x = absinfo.maximum;
421                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
422                               &absinfo);
423                         device->abs.min_y = absinfo.minimum;
424                         device->abs.max_y = absinfo.maximum;
425                         device->is_mt = 1;
426                         device->mt.slot = 0;
427                         device->caps |= EVDEV_TOUCH;
428                 }
429         }
430         if (TEST_BIT(ev_bits, EV_REL)) {
431                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
432                       rel_bits);
433                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
434                         device->caps |= EVDEV_MOTION_REL;
435         }
436         if (TEST_BIT(ev_bits, EV_KEY)) {
437                 has_key = 1;
438                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
439                       key_bits);
440                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
441                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
442                     has_abs)
443                         device->dispatch = evdev_touchpad_create(device);
444                 for (i = KEY_ESC; i < KEY_MAX; i++) {
445                         if (i >= BTN_MISC && i < KEY_OK)
446                                 continue;
447                         if (TEST_BIT(key_bits, i)) {
448                                 device->caps |= EVDEV_KEYBOARD;
449                                 break;
450                         }
451                 }
452                 for (i = BTN_MISC; i < KEY_OK; i++) {
453                         if (TEST_BIT(key_bits, i)) {
454                                 device->caps |= EVDEV_BUTTON;
455                                 break;
456                         }
457                 }
458         }
459         if (TEST_BIT(ev_bits, EV_LED)) {
460                 device->caps |= EVDEV_KEYBOARD;
461         }
462
463         /* This rule tries to catch accelerometer devices and opt out. We may
464          * want to adjust the protocol later adding a proper event for dealing
465          * with accelerometers and implement here accordingly */
466         if (has_abs && !has_key && !device->is_mt) {
467                 weston_log("input device %s, %s "
468                            "ignored: unsupported device type\n",
469                            device->devname, device->devnode);
470                 return 0;
471         }
472
473         return 1;
474 }
475
476 static int
477 evdev_configure_device(struct evdev_device *device)
478 {
479         if ((device->caps &
480              (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
481                 weston_seat_init_pointer(device->seat);
482                 weston_log("input device %s, %s is a pointer\n",
483                            device->devname, device->devnode);
484         }
485         if ((device->caps & EVDEV_KEYBOARD)) {
486                 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
487                         return -1;
488                 weston_log("input device %s, %s is a keyboard\n",
489                            device->devname, device->devnode);
490         }
491         if ((device->caps & EVDEV_TOUCH)) {
492                 weston_seat_init_touch(device->seat);
493                 weston_log("input device %s, %s is a touch device\n",
494                            device->devname, device->devnode);
495         }
496
497         return 0;
498 }
499
500 struct evdev_device *
501 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
502 {
503         struct evdev_device *device;
504         struct weston_compositor *ec;
505         char devname[256] = "unknown";
506
507         device = malloc(sizeof *device);
508         if (device == NULL)
509                 return NULL;
510         memset(device, 0, sizeof *device);
511
512         ec = seat->compositor;
513         device->output =
514                 container_of(ec->output_list.next, struct weston_output, link);
515
516         device->seat = seat;
517         device->is_mt = 0;
518         device->mtdev = NULL;
519         device->devnode = strdup(path);
520         device->mt.slot = -1;
521         device->rel.dx = 0;
522         device->rel.dy = 0;
523         device->dispatch = NULL;
524         device->fd = device_fd;
525
526         ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
527         device->devname = strdup(devname);
528
529         if (!evdev_handle_device(device)) {
530                 free(device->devnode);
531                 free(device->devname);
532                 free(device);
533                 return EVDEV_UNHANDLED_DEVICE;
534         }
535
536         if (evdev_configure_device(device) == -1)
537                 goto err1;
538
539         /* If the dispatch was not set up use the fallback. */
540         if (device->dispatch == NULL)
541                 device->dispatch = fallback_dispatch_create();
542         if (device->dispatch == NULL)
543                 goto err1;
544
545
546         if (device->is_mt) {
547                 device->mtdev = mtdev_new_open(device->fd);
548                 if (!device->mtdev)
549                         weston_log("mtdev failed to open for %s\n", path);
550         }
551
552         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
553                                               WL_EVENT_READABLE,
554                                               evdev_device_data, device);
555         if (device->source == NULL)
556                 goto err2;
557
558         return device;
559
560 err2:
561         device->dispatch->interface->destroy(device->dispatch);
562 err1:
563         free(device->devname);
564         free(device->devnode);
565         free(device);
566         return NULL;
567 }
568
569 void
570 evdev_device_destroy(struct evdev_device *device)
571 {
572         struct evdev_dispatch *dispatch;
573
574         dispatch = device->dispatch;
575         if (dispatch)
576                 dispatch->interface->destroy(dispatch);
577
578         wl_event_source_remove(device->source);
579         wl_list_remove(&device->link);
580         if (device->mtdev)
581                 mtdev_close_delete(device->mtdev);
582         close(device->fd);
583         free(device->devname);
584         free(device->devnode);
585         free(device);
586 }
587
588 void
589 evdev_notify_keyboard_focus(struct weston_seat *seat,
590                             struct wl_list *evdev_devices)
591 {
592         struct evdev_device *device;
593         struct wl_array keys;
594         unsigned int i, set;
595         char evdev_keys[(KEY_CNT + 7) / 8];
596         char all_keys[(KEY_CNT + 7) / 8];
597         uint32_t *k;
598         int ret;
599
600         if (!seat->seat.keyboard)
601                 return;
602
603         memset(all_keys, 0, sizeof all_keys);
604         wl_list_for_each(device, evdev_devices, link) {
605                 memset(evdev_keys, 0, sizeof evdev_keys);
606                 ret = ioctl(device->fd,
607                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
608                 if (ret < 0) {
609                         weston_log("failed to get keys for device %s\n",
610                                 device->devnode);
611                         continue;
612                 }
613                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
614                         all_keys[i] |= evdev_keys[i];
615         }
616
617         wl_array_init(&keys);
618         for (i = 0; i < KEY_CNT; i++) {
619                 set = all_keys[i >> 3] & (1 << (i & 7));
620                 if (set) {
621                         k = wl_array_add(&keys, sizeof *k);
622                         *k = i;
623                 }
624         }
625
626         notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
627
628         wl_array_release(&keys);
629 }