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