2 * Copyright © 2010 Intel Corporation
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.
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.
27 #include <linux/input.h>
32 #include "compositor.h"
35 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
38 evdev_led_update(struct evdev_device *device, enum weston_led leds)
41 enum weston_led weston;
44 { LED_NUM_LOCK, LED_NUML },
45 { LED_CAPS_LOCK, LED_CAPSL },
46 { LED_SCROLL_LOCK, LED_SCROLLL },
48 struct input_event ev[ARRAY_LENGTH(map)];
51 if (!device->caps & EVDEV_KEYBOARD)
54 memset(ev, 0, sizeof(ev));
55 for (i = 0; i < ARRAY_LENGTH(map); i++) {
57 ev[i].code = map[i].evdev;
58 ev[i].value = !!(leds & map[i].weston);
61 i = write(device->fd, ev, sizeof ev);
62 (void)i; /* no, we really don't care about the return value */
66 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
80 notify_button(device->seat,
82 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
83 WL_POINTER_BUTTON_STATE_RELEASED);
87 if (e->value == 0 && !device->is_mt)
88 notify_touch(device->seat, time, device->mt.slot, 0, 0,
92 notify_key(device->seat,
94 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
95 WL_KEYBOARD_KEY_STATE_RELEASED,
96 STATE_UPDATE_AUTOMATIC);
102 evdev_process_touch(struct evdev_device *device, struct input_event *e)
104 const int screen_width = device->output->current->width;
105 const int screen_height = device->output->current->height;
109 device->mt.slot = e->value;
111 case ABS_MT_TRACKING_ID:
113 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
115 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
117 case ABS_MT_POSITION_X:
118 device->mt.x[device->mt.slot] =
119 (e->value - device->abs.min_x) * screen_width /
120 (device->abs.max_x - device->abs.min_x);
121 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
123 case ABS_MT_POSITION_Y:
124 device->mt.y[device->mt.slot] =
125 (e->value - device->abs.min_y) * screen_height /
126 (device->abs.max_y - device->abs.min_y);
127 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
133 evdev_process_absolute_motion(struct evdev_device *device,
134 struct input_event *e)
136 const int screen_width = device->output->current->width;
137 const int screen_height = device->output->current->height;
142 (e->value - device->abs.min_x) * screen_width /
143 (device->abs.max_x - device->abs.min_x);
144 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
148 (e->value - device->abs.min_y) * screen_height /
149 (device->abs.max_y - device->abs.min_y);
150 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
156 evdev_process_relative(struct evdev_device *device,
157 struct input_event *e, uint32_t time)
161 device->rel.dx += wl_fixed_from_int(e->value);
162 device->pending_events |= EVDEV_RELATIVE_MOTION;
165 device->rel.dy += wl_fixed_from_int(e->value);
166 device->pending_events |= EVDEV_RELATIVE_MOTION;
174 notify_axis(device->seat,
176 WL_POINTER_AXIS_VERTICAL_SCROLL,
177 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
189 notify_axis(device->seat,
191 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
192 e->value * DEFAULT_AXIS_STEP_DISTANCE);
202 evdev_process_absolute(struct evdev_device *device, struct input_event *e)
205 evdev_process_touch(device, e);
207 evdev_process_absolute_motion(device, e);
212 is_motion_event(struct input_event *e)
226 case ABS_MT_POSITION_X:
227 case ABS_MT_POSITION_Y:
236 transform_absolute(struct evdev_device *device)
240 if (!device->abs.apply_calibration)
243 x = device->abs.x * device->abs.calibration[0] +
244 device->abs.y * device->abs.calibration[1] +
245 device->abs.calibration[2];
247 y = device->abs.x * device->abs.calibration[3] +
248 device->abs.y * device->abs.calibration[4] +
249 device->abs.calibration[5];
256 evdev_flush_motion(struct evdev_device *device, uint32_t time)
258 struct weston_seat *master = device->seat;
262 if (!(device->pending_events & EVDEV_SYN))
265 slot = device->mt.slot;
266 device->pending_events &= ~EVDEV_SYN;
267 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
268 notify_motion(master, time, device->rel.dx, device->rel.dy);
269 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
273 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
274 weston_output_transform_coordinate(device->output,
278 notify_touch(master, time,
279 device->mt.slot, x, y, WL_TOUCH_DOWN);
280 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
281 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
283 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
284 weston_output_transform_coordinate(device->output,
288 notify_touch(master, time,
289 device->mt.slot, x, y, WL_TOUCH_MOTION);
290 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
291 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
293 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
294 notify_touch(master, time, device->mt.slot, 0, 0,
296 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
298 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
299 transform_absolute(device);
300 weston_output_transform_coordinate(device->output,
302 device->abs.y, &x, &y);
304 if (device->caps & EVDEV_TOUCH) {
305 if (master->num_tp == 0)
306 notify_touch(master, time, 0,
307 x, y, WL_TOUCH_DOWN);
309 notify_touch(master, time, 0,
310 x, y, WL_TOUCH_MOTION);
312 notify_motion_absolute(master, time, x, y);
313 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
318 fallback_process(struct evdev_dispatch *dispatch,
319 struct evdev_device *device,
320 struct input_event *event,
323 switch (event->type) {
325 evdev_process_relative(device, event, time);
328 evdev_process_absolute(device, event);
331 evdev_process_key(device, event, time);
334 device->pending_events |= EVDEV_SYN;
340 fallback_destroy(struct evdev_dispatch *dispatch)
345 struct evdev_dispatch_interface fallback_interface = {
350 static struct evdev_dispatch *
351 fallback_dispatch_create(void)
353 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
354 if (dispatch == NULL)
357 dispatch->interface = &fallback_interface;
363 evdev_process_events(struct evdev_device *device,
364 struct input_event *ev, int count)
366 struct evdev_dispatch *dispatch = device->dispatch;
367 struct input_event *e, *end;
370 device->pending_events = 0;
374 for (e = ev; e < end; e++) {
375 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
377 /* we try to minimize the amount of notifications to be
378 * forwarded to the compositor, so we accumulate motion
379 * events and send as a bunch */
380 if (!is_motion_event(e))
381 evdev_flush_motion(device, time);
383 dispatch->interface->process(dispatch, device, e, time);
386 evdev_flush_motion(device, time);
390 evdev_device_data(int fd, uint32_t mask, void *data)
392 struct weston_compositor *ec;
393 struct evdev_device *device = data;
394 struct input_event ev[32];
397 ec = device->seat->compositor;
401 /* If the compositor is repainting, this function is called only once
402 * per frame and we have to process all the events available on the
403 * fd, otherwise there will be input lag. */
406 len = mtdev_get(device->mtdev, fd, ev,
408 sizeof (struct input_event);
410 len = read(fd, &ev, sizeof ev);
412 if (len < 0 || len % sizeof ev[0] != 0) {
413 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
417 evdev_process_events(device, ev, len / sizeof ev[0]);
425 evdev_handle_device(struct evdev_device *device)
427 struct input_absinfo absinfo;
428 unsigned long ev_bits[NBITS(EV_MAX)];
429 unsigned long abs_bits[NBITS(ABS_MAX)];
430 unsigned long rel_bits[NBITS(REL_MAX)];
431 unsigned long key_bits[NBITS(KEY_MAX)];
432 int has_key, has_abs;
439 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
440 if (TEST_BIT(ev_bits, EV_ABS)) {
443 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
445 if (TEST_BIT(abs_bits, ABS_X)) {
446 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
447 device->abs.min_x = absinfo.minimum;
448 device->abs.max_x = absinfo.maximum;
449 device->caps |= EVDEV_MOTION_ABS;
451 if (TEST_BIT(abs_bits, ABS_Y)) {
452 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
453 device->abs.min_y = absinfo.minimum;
454 device->abs.max_y = absinfo.maximum;
455 device->caps |= EVDEV_MOTION_ABS;
457 /* We only handle the slotted Protocol B in weston.
458 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
459 require mtdev for conversion. */
460 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
461 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
462 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
464 device->abs.min_x = absinfo.minimum;
465 device->abs.max_x = absinfo.maximum;
466 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
468 device->abs.min_y = absinfo.minimum;
469 device->abs.max_y = absinfo.maximum;
472 device->caps |= EVDEV_TOUCH;
475 if (TEST_BIT(ev_bits, EV_REL)) {
476 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
478 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
479 device->caps |= EVDEV_MOTION_REL;
481 if (TEST_BIT(ev_bits, EV_KEY)) {
483 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
485 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
486 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
488 device->dispatch = evdev_touchpad_create(device);
489 for (i = KEY_ESC; i < KEY_MAX; i++) {
490 if (i >= BTN_MISC && i < KEY_OK)
492 if (TEST_BIT(key_bits, i)) {
493 device->caps |= EVDEV_KEYBOARD;
497 for (i = BTN_MISC; i < KEY_OK; i++) {
498 if (TEST_BIT(key_bits, i)) {
499 device->caps |= EVDEV_BUTTON;
503 if (TEST_BIT(key_bits, BTN_TOUCH)) {
504 device->caps |= EVDEV_TOUCH;
508 if (TEST_BIT(ev_bits, EV_LED)) {
509 device->caps |= EVDEV_KEYBOARD;
512 /* This rule tries to catch accelerometer devices and opt out. We may
513 * want to adjust the protocol later adding a proper event for dealing
514 * with accelerometers and implement here accordingly */
515 if (has_abs && !has_key && !device->is_mt) {
516 weston_log("input device %s, %s "
517 "ignored: unsupported device type\n",
518 device->devname, device->devnode);
526 evdev_configure_device(struct evdev_device *device)
529 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
530 weston_seat_init_pointer(device->seat);
531 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
532 device->devname, device->devnode,
533 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
534 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
535 device->caps & EVDEV_BUTTON ? " button" : "");
537 if ((device->caps & EVDEV_KEYBOARD)) {
538 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
540 weston_log("input device %s, %s is a keyboard\n",
541 device->devname, device->devnode);
543 if ((device->caps & EVDEV_TOUCH)) {
544 weston_seat_init_touch(device->seat);
545 weston_log("input device %s, %s is a touch device\n",
546 device->devname, device->devnode);
552 struct evdev_device *
553 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
555 struct evdev_device *device;
556 struct weston_compositor *ec;
557 char devname[256] = "unknown";
559 device = zalloc(sizeof *device);
563 ec = seat->compositor;
565 container_of(ec->output_list.next, struct weston_output, link);
569 device->mtdev = NULL;
570 device->devnode = strdup(path);
571 device->mt.slot = -1;
574 device->dispatch = NULL;
575 device->fd = device_fd;
577 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
578 devname[sizeof(devname) - 1] = '\0';
579 device->devname = strdup(devname);
581 if (!evdev_handle_device(device)) {
582 free(device->devnode);
583 free(device->devname);
585 return EVDEV_UNHANDLED_DEVICE;
588 if (evdev_configure_device(device) == -1)
591 /* If the dispatch was not set up use the fallback. */
592 if (device->dispatch == NULL)
593 device->dispatch = fallback_dispatch_create();
594 if (device->dispatch == NULL)
599 device->mtdev = mtdev_new_open(device->fd);
601 weston_log("mtdev failed to open for %s\n", path);
604 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
606 evdev_device_data, device);
607 if (device->source == NULL)
613 device->dispatch->interface->destroy(device->dispatch);
615 free(device->devname);
616 free(device->devnode);
622 evdev_device_destroy(struct evdev_device *device)
624 struct evdev_dispatch *dispatch;
626 dispatch = device->dispatch;
628 dispatch->interface->destroy(dispatch);
630 wl_event_source_remove(device->source);
631 wl_list_remove(&device->link);
633 mtdev_close_delete(device->mtdev);
635 free(device->devname);
636 free(device->devnode);
641 evdev_notify_keyboard_focus(struct weston_seat *seat,
642 struct wl_list *evdev_devices)
644 struct evdev_device *device;
645 struct wl_array keys;
647 char evdev_keys[(KEY_CNT + 7) / 8];
648 char all_keys[(KEY_CNT + 7) / 8];
655 memset(all_keys, 0, sizeof all_keys);
656 wl_list_for_each(device, evdev_devices, link) {
657 memset(evdev_keys, 0, sizeof evdev_keys);
658 ret = ioctl(device->fd,
659 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
661 weston_log("failed to get keys for device %s\n",
665 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
666 all_keys[i] |= evdev_keys[i];
669 wl_array_init(&keys);
670 for (i = 0; i < KEY_CNT; i++) {
671 set = all_keys[i >> 3] & (1 << (i & 7));
673 k = wl_array_add(&keys, sizeof *k);
678 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
680 wl_array_release(&keys);