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