08aa7276f681d26358ffdf105783f4ffbca6f421
[platform/upstream/libinput.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 "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <linux/input.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <mtdev.h>
31
32 #include "compositor.h"
33 #include "evdev.h"
34
35 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
36
37 void
38 evdev_led_update(struct evdev_device *device, enum weston_led leds)
39 {
40         static const struct {
41                 enum weston_led weston;
42                 int evdev;
43         } map[] = {
44                 { LED_NUM_LOCK, LED_NUML },
45                 { LED_CAPS_LOCK, LED_CAPSL },
46                 { LED_SCROLL_LOCK, LED_SCROLLL },
47         };
48         struct input_event ev[ARRAY_LENGTH(map)];
49         unsigned int i;
50
51         if (!device->caps & EVDEV_KEYBOARD)
52                 return;
53
54         memset(ev, 0, sizeof(ev));
55         for (i = 0; i < ARRAY_LENGTH(map); i++) {
56                 ev[i].type = EV_LED;
57                 ev[i].code = map[i].evdev;
58                 ev[i].value = !!(leds & map[i].weston);
59         }
60
61         i = write(device->fd, ev, sizeof ev);
62         (void)i; /* no, we really don't care about the return value */
63 }
64
65 static inline void
66 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
67 {
68         if (e->value == 2)
69                 return;
70
71         switch (e->code) {
72         case BTN_LEFT:
73         case BTN_RIGHT:
74         case BTN_MIDDLE:
75         case BTN_SIDE:
76         case BTN_EXTRA:
77         case BTN_FORWARD:
78         case BTN_BACK:
79         case BTN_TASK:
80                 notify_button(device->seat,
81                               time, e->code,
82                               e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
83                                          WL_POINTER_BUTTON_STATE_RELEASED);
84                 break;
85
86         default:
87                 notify_key(device->seat,
88                            time, e->code,
89                            e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
90                                       WL_KEYBOARD_KEY_STATE_RELEASED,
91                            STATE_UPDATE_AUTOMATIC);
92                 break;
93         }
94 }
95
96 static void
97 evdev_process_touch(struct evdev_device *device, struct input_event *e)
98 {
99         const int screen_width = device->output->current->width;
100         const int screen_height = device->output->current->height;
101
102         switch (e->code) {
103         case ABS_MT_SLOT:
104                 device->mt.slot = e->value;
105                 break;
106         case ABS_MT_TRACKING_ID:
107                 if (e->value >= 0)
108                         device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
109                 else
110                         device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
111                 break;
112         case ABS_MT_POSITION_X:
113                 device->mt.x[device->mt.slot] =
114                         (e->value - device->abs.min_x) * screen_width /
115                         (device->abs.max_x - device->abs.min_x);
116                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
117                 break;
118         case ABS_MT_POSITION_Y:
119                 device->mt.y[device->mt.slot] =
120                         (e->value - device->abs.min_y) * screen_height /
121                         (device->abs.max_y - device->abs.min_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 transform_absolute(struct evdev_device *device)
234 {
235         if (!device->abs.apply_calibration)
236                 return;
237
238         device->abs.x = device->abs.x * device->abs.calibration[0] +
239                         device->abs.y * device->abs.calibration[1] +
240                         device->abs.calibration[2];
241
242         device->abs.y = device->abs.x * device->abs.calibration[3] +
243                         device->abs.y * device->abs.calibration[4] +
244                         device->abs.calibration[5];
245 }
246
247 static void
248 evdev_flush_motion(struct evdev_device *device, uint32_t time)
249 {
250         struct weston_seat *master = device->seat;
251         wl_fixed_t x, y;
252         int slot;
253
254         if (!(device->pending_events & EVDEV_SYN))
255                 return;
256
257         slot = device->mt.slot;
258         device->pending_events &= ~EVDEV_SYN;
259         if (device->pending_events & EVDEV_RELATIVE_MOTION) {
260                 notify_motion(master, time, device->rel.dx, device->rel.dy);
261                 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
262                 device->rel.dx = 0;
263                 device->rel.dy = 0;
264         }
265         if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
266                 weston_output_transform_coordinate(device->output,
267                                                    device->mt.x[slot],
268                                                    device->mt.y[slot],
269                                                    &x, &y);
270                 notify_touch(master, time,
271                              device->mt.slot, x, y, WL_TOUCH_DOWN);
272                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
273                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
274         }
275         if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
276                 weston_output_transform_coordinate(device->output,
277                                                    device->mt.x[slot],
278                                                    device->mt.y[slot],
279                                                    &x, &y);
280                 notify_touch(master, time,
281                              device->mt.slot, x, y, WL_TOUCH_MOTION);
282                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
283                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
284         }
285         if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
286                 notify_touch(master, time, device->mt.slot, 0, 0,
287                              WL_TOUCH_UP);
288                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
289         }
290         if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
291                 transform_absolute(device);
292                 weston_output_transform_coordinate(device->output,
293                                                    device->abs.x,
294                                                    device->abs.y, &x, &y);
295                 notify_motion_absolute(master, time, x, 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                         device->abs.min_x = absinfo.minimum;
444                         device->abs.max_x = absinfo.maximum;
445                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
446                               &absinfo);
447                         device->abs.min_y = absinfo.minimum;
448                         device->abs.max_y = absinfo.maximum;
449                         device->is_mt = 1;
450                         device->mt.slot = 0;
451                         device->caps |= EVDEV_TOUCH;
452                 }
453         }
454         if (TEST_BIT(ev_bits, EV_REL)) {
455                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
456                       rel_bits);
457                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
458                         device->caps |= EVDEV_MOTION_REL;
459         }
460         if (TEST_BIT(ev_bits, EV_KEY)) {
461                 has_key = 1;
462                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
463                       key_bits);
464                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
465                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
466                     has_abs)
467                         device->dispatch = evdev_touchpad_create(device);
468                 for (i = KEY_ESC; i < KEY_MAX; i++) {
469                         if (i >= BTN_MISC && i < KEY_OK)
470                                 continue;
471                         if (TEST_BIT(key_bits, i)) {
472                                 device->caps |= EVDEV_KEYBOARD;
473                                 break;
474                         }
475                 }
476                 for (i = BTN_MISC; i < KEY_OK; i++) {
477                         if (TEST_BIT(key_bits, i)) {
478                                 device->caps |= EVDEV_BUTTON;
479                                 break;
480                         }
481                 }
482         }
483         if (TEST_BIT(ev_bits, EV_LED)) {
484                 device->caps |= EVDEV_KEYBOARD;
485         }
486
487         /* This rule tries to catch accelerometer devices and opt out. We may
488          * want to adjust the protocol later adding a proper event for dealing
489          * with accelerometers and implement here accordingly */
490         if (has_abs && !has_key && !device->is_mt) {
491                 weston_log("input device %s, %s "
492                            "ignored: unsupported device type\n",
493                            device->devname, device->devnode);
494                 return 0;
495         }
496
497         return 1;
498 }
499
500 static int
501 evdev_configure_device(struct evdev_device *device)
502 {
503         if ((device->caps &
504              (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
505                 weston_seat_init_pointer(device->seat);
506                 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
507                            device->devname, device->devnode,
508                            device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
509                            device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
510                            device->caps & EVDEV_BUTTON ? " button" : "");
511         }
512         if ((device->caps & EVDEV_KEYBOARD)) {
513                 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
514                         return -1;
515                 weston_log("input device %s, %s is a keyboard\n",
516                            device->devname, device->devnode);
517         }
518         if ((device->caps & EVDEV_TOUCH)) {
519                 weston_seat_init_touch(device->seat);
520                 weston_log("input device %s, %s is a touch device\n",
521                            device->devname, device->devnode);
522         }
523
524         return 0;
525 }
526
527 struct evdev_device *
528 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
529 {
530         struct evdev_device *device;
531         struct weston_compositor *ec;
532         char devname[256] = "unknown";
533
534         device = malloc(sizeof *device);
535         if (device == NULL)
536                 return NULL;
537         memset(device, 0, sizeof *device);
538
539         ec = seat->compositor;
540         device->output =
541                 container_of(ec->output_list.next, struct weston_output, link);
542
543         device->seat = seat;
544         device->is_mt = 0;
545         device->mtdev = NULL;
546         device->devnode = strdup(path);
547         device->mt.slot = -1;
548         device->rel.dx = 0;
549         device->rel.dy = 0;
550         device->dispatch = NULL;
551         device->fd = device_fd;
552
553         ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
554         device->devname = strdup(devname);
555
556         if (!evdev_handle_device(device)) {
557                 free(device->devnode);
558                 free(device->devname);
559                 free(device);
560                 return EVDEV_UNHANDLED_DEVICE;
561         }
562
563         if (evdev_configure_device(device) == -1)
564                 goto err1;
565
566         /* If the dispatch was not set up use the fallback. */
567         if (device->dispatch == NULL)
568                 device->dispatch = fallback_dispatch_create();
569         if (device->dispatch == NULL)
570                 goto err1;
571
572
573         if (device->is_mt) {
574                 device->mtdev = mtdev_new_open(device->fd);
575                 if (!device->mtdev)
576                         weston_log("mtdev failed to open for %s\n", path);
577         }
578
579         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
580                                               WL_EVENT_READABLE,
581                                               evdev_device_data, device);
582         if (device->source == NULL)
583                 goto err2;
584
585         return device;
586
587 err2:
588         device->dispatch->interface->destroy(device->dispatch);
589 err1:
590         free(device->devname);
591         free(device->devnode);
592         free(device);
593         return NULL;
594 }
595
596 void
597 evdev_device_destroy(struct evdev_device *device)
598 {
599         struct evdev_dispatch *dispatch;
600
601         dispatch = device->dispatch;
602         if (dispatch)
603                 dispatch->interface->destroy(dispatch);
604
605         wl_event_source_remove(device->source);
606         wl_list_remove(&device->link);
607         if (device->mtdev)
608                 mtdev_close_delete(device->mtdev);
609         close(device->fd);
610         free(device->devname);
611         free(device->devnode);
612         free(device);
613 }
614
615 void
616 evdev_notify_keyboard_focus(struct weston_seat *seat,
617                             struct wl_list *evdev_devices)
618 {
619         struct evdev_device *device;
620         struct wl_array keys;
621         unsigned int i, set;
622         char evdev_keys[(KEY_CNT + 7) / 8];
623         char all_keys[(KEY_CNT + 7) / 8];
624         uint32_t *k;
625         int ret;
626
627         if (!seat->keyboard)
628                 return;
629
630         memset(all_keys, 0, sizeof all_keys);
631         wl_list_for_each(device, evdev_devices, link) {
632                 memset(evdev_keys, 0, sizeof evdev_keys);
633                 ret = ioctl(device->fd,
634                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
635                 if (ret < 0) {
636                         weston_log("failed to get keys for device %s\n",
637                                 device->devnode);
638                         continue;
639                 }
640                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
641                         all_keys[i] |= evdev_keys[i];
642         }
643
644         wl_array_init(&keys);
645         for (i = 0; i < KEY_CNT; i++) {
646                 set = all_keys[i >> 3] & (1 << (i & 7));
647                 if (set) {
648                         k = wl_array_add(&keys, sizeof *k);
649                         *k = i;
650                 }
651         }
652
653         notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
654
655         wl_array_release(&keys);
656 }