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