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 notify_key(device->seat,
89 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
90 WL_KEYBOARD_KEY_STATE_RELEASED,
91 STATE_UPDATE_AUTOMATIC);
97 evdev_process_touch(struct evdev_device *device, struct input_event *e)
99 const int screen_width = device->output->current->width;
100 const int screen_height = device->output->current->height;
104 device->mt.slot = e->value;
106 case ABS_MT_TRACKING_ID:
108 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
110 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
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;
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;
128 evdev_process_absolute_motion(struct evdev_device *device,
129 struct input_event *e)
131 const int screen_width = device->output->current->width;
132 const int screen_height = device->output->current->height;
137 (e->value - device->abs.min_x) * screen_width /
138 (device->abs.max_x - device->abs.min_x);
139 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
143 (e->value - device->abs.min_y) * screen_height /
144 (device->abs.max_y - device->abs.min_y);
145 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
151 evdev_process_relative(struct evdev_device *device,
152 struct input_event *e, uint32_t time)
156 device->rel.dx += wl_fixed_from_int(e->value);
157 device->pending_events |= EVDEV_RELATIVE_MOTION;
160 device->rel.dy += wl_fixed_from_int(e->value);
161 device->pending_events |= EVDEV_RELATIVE_MOTION;
169 notify_axis(device->seat,
171 WL_POINTER_AXIS_VERTICAL_SCROLL,
172 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
184 notify_axis(device->seat,
186 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
187 e->value * DEFAULT_AXIS_STEP_DISTANCE);
197 evdev_process_absolute(struct evdev_device *device, struct input_event *e)
200 evdev_process_touch(device, e);
202 evdev_process_absolute_motion(device, e);
207 is_motion_event(struct input_event *e)
221 case ABS_MT_POSITION_X:
222 case ABS_MT_POSITION_Y:
231 transform_absolute(struct evdev_device *device)
235 if (!device->abs.apply_calibration)
238 x = device->abs.x * device->abs.calibration[0] +
239 device->abs.y * device->abs.calibration[1] +
240 device->abs.calibration[2];
242 y = device->abs.x * device->abs.calibration[3] +
243 device->abs.y * device->abs.calibration[4] +
244 device->abs.calibration[5];
251 evdev_flush_motion(struct evdev_device *device, uint32_t time)
253 struct weston_seat *master = device->seat;
257 if (!(device->pending_events & EVDEV_SYN))
260 slot = device->mt.slot;
261 device->pending_events &= ~EVDEV_SYN;
262 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
263 notify_motion(master, time, device->rel.dx, device->rel.dy);
264 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
268 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
269 weston_output_transform_coordinate(device->output,
273 notify_touch(master, time,
274 device->mt.slot, x, y, WL_TOUCH_DOWN);
275 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
276 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
278 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
279 weston_output_transform_coordinate(device->output,
283 notify_touch(master, time,
284 device->mt.slot, x, y, WL_TOUCH_MOTION);
285 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
286 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
288 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
289 notify_touch(master, time, device->mt.slot, 0, 0,
291 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
293 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
294 transform_absolute(device);
295 weston_output_transform_coordinate(device->output,
297 device->abs.y, &x, &y);
298 notify_motion_absolute(master, time, x, y);
299 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
304 fallback_process(struct evdev_dispatch *dispatch,
305 struct evdev_device *device,
306 struct input_event *event,
309 switch (event->type) {
311 evdev_process_relative(device, event, time);
314 evdev_process_absolute(device, event);
317 evdev_process_key(device, event, time);
320 device->pending_events |= EVDEV_SYN;
326 fallback_destroy(struct evdev_dispatch *dispatch)
331 struct evdev_dispatch_interface fallback_interface = {
336 static struct evdev_dispatch *
337 fallback_dispatch_create(void)
339 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
340 if (dispatch == NULL)
343 dispatch->interface = &fallback_interface;
349 evdev_process_events(struct evdev_device *device,
350 struct input_event *ev, int count)
352 struct evdev_dispatch *dispatch = device->dispatch;
353 struct input_event *e, *end;
356 device->pending_events = 0;
360 for (e = ev; e < end; e++) {
361 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
363 /* we try to minimize the amount of notifications to be
364 * forwarded to the compositor, so we accumulate motion
365 * events and send as a bunch */
366 if (!is_motion_event(e))
367 evdev_flush_motion(device, time);
369 dispatch->interface->process(dispatch, device, e, time);
372 evdev_flush_motion(device, time);
376 evdev_device_data(int fd, uint32_t mask, void *data)
378 struct weston_compositor *ec;
379 struct evdev_device *device = data;
380 struct input_event ev[32];
383 ec = device->seat->compositor;
387 /* If the compositor is repainting, this function is called only once
388 * per frame and we have to process all the events available on the
389 * fd, otherwise there will be input lag. */
392 len = mtdev_get(device->mtdev, fd, ev,
394 sizeof (struct input_event);
396 len = read(fd, &ev, sizeof ev);
398 if (len < 0 || len % sizeof ev[0] != 0) {
399 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
403 evdev_process_events(device, ev, len / sizeof ev[0]);
411 evdev_handle_device(struct evdev_device *device)
413 struct input_absinfo absinfo;
414 unsigned long ev_bits[NBITS(EV_MAX)];
415 unsigned long abs_bits[NBITS(ABS_MAX)];
416 unsigned long rel_bits[NBITS(REL_MAX)];
417 unsigned long key_bits[NBITS(KEY_MAX)];
418 int has_key, has_abs;
425 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
426 if (TEST_BIT(ev_bits, EV_ABS)) {
429 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
431 if (TEST_BIT(abs_bits, ABS_X)) {
432 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
433 device->abs.min_x = absinfo.minimum;
434 device->abs.max_x = absinfo.maximum;
435 device->caps |= EVDEV_MOTION_ABS;
437 if (TEST_BIT(abs_bits, ABS_Y)) {
438 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
439 device->abs.min_y = absinfo.minimum;
440 device->abs.max_y = absinfo.maximum;
441 device->caps |= EVDEV_MOTION_ABS;
443 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
444 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
446 device->abs.min_x = absinfo.minimum;
447 device->abs.max_x = absinfo.maximum;
448 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
450 device->abs.min_y = absinfo.minimum;
451 device->abs.max_y = absinfo.maximum;
454 device->caps |= EVDEV_TOUCH;
457 if (TEST_BIT(ev_bits, EV_REL)) {
458 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
460 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
461 device->caps |= EVDEV_MOTION_REL;
463 if (TEST_BIT(ev_bits, EV_KEY)) {
465 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
467 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
468 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
470 device->dispatch = evdev_touchpad_create(device);
471 for (i = KEY_ESC; i < KEY_MAX; i++) {
472 if (i >= BTN_MISC && i < KEY_OK)
474 if (TEST_BIT(key_bits, i)) {
475 device->caps |= EVDEV_KEYBOARD;
479 for (i = BTN_MISC; i < KEY_OK; i++) {
480 if (TEST_BIT(key_bits, i)) {
481 device->caps |= EVDEV_BUTTON;
486 if (TEST_BIT(ev_bits, EV_LED)) {
487 device->caps |= EVDEV_KEYBOARD;
490 /* This rule tries to catch accelerometer devices and opt out. We may
491 * want to adjust the protocol later adding a proper event for dealing
492 * with accelerometers and implement here accordingly */
493 if (has_abs && !has_key && !device->is_mt) {
494 weston_log("input device %s, %s "
495 "ignored: unsupported device type\n",
496 device->devname, device->devnode);
504 evdev_configure_device(struct evdev_device *device)
507 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
508 weston_seat_init_pointer(device->seat);
509 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
510 device->devname, device->devnode,
511 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
512 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
513 device->caps & EVDEV_BUTTON ? " button" : "");
515 if ((device->caps & EVDEV_KEYBOARD)) {
516 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
518 weston_log("input device %s, %s is a keyboard\n",
519 device->devname, device->devnode);
521 if ((device->caps & EVDEV_TOUCH)) {
522 weston_seat_init_touch(device->seat);
523 weston_log("input device %s, %s is a touch device\n",
524 device->devname, device->devnode);
530 struct evdev_device *
531 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
533 struct evdev_device *device;
534 struct weston_compositor *ec;
535 char devname[256] = "unknown";
537 device = malloc(sizeof *device);
540 memset(device, 0, sizeof *device);
542 ec = seat->compositor;
544 container_of(ec->output_list.next, struct weston_output, link);
548 device->mtdev = NULL;
549 device->devnode = strdup(path);
550 device->mt.slot = -1;
553 device->dispatch = NULL;
554 device->fd = device_fd;
556 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
557 device->devname = strdup(devname);
559 if (!evdev_handle_device(device)) {
560 free(device->devnode);
561 free(device->devname);
563 return EVDEV_UNHANDLED_DEVICE;
566 if (evdev_configure_device(device) == -1)
569 /* If the dispatch was not set up use the fallback. */
570 if (device->dispatch == NULL)
571 device->dispatch = fallback_dispatch_create();
572 if (device->dispatch == NULL)
577 device->mtdev = mtdev_new_open(device->fd);
579 weston_log("mtdev failed to open for %s\n", path);
582 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
584 evdev_device_data, device);
585 if (device->source == NULL)
591 device->dispatch->interface->destroy(device->dispatch);
593 free(device->devname);
594 free(device->devnode);
600 evdev_device_destroy(struct evdev_device *device)
602 struct evdev_dispatch *dispatch;
604 dispatch = device->dispatch;
606 dispatch->interface->destroy(dispatch);
608 wl_event_source_remove(device->source);
609 wl_list_remove(&device->link);
611 mtdev_close_delete(device->mtdev);
613 free(device->devname);
614 free(device->devnode);
619 evdev_notify_keyboard_focus(struct weston_seat *seat,
620 struct wl_list *evdev_devices)
622 struct evdev_device *device;
623 struct wl_array keys;
625 char evdev_keys[(KEY_CNT + 7) / 8];
626 char all_keys[(KEY_CNT + 7) / 8];
633 memset(all_keys, 0, sizeof all_keys);
634 wl_list_for_each(device, evdev_devices, link) {
635 memset(evdev_keys, 0, sizeof evdev_keys);
636 ret = ioctl(device->fd,
637 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
639 weston_log("failed to get keys for device %s\n",
643 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
644 all_keys[i] |= evdev_keys[i];
647 wl_array_init(&keys);
648 for (i = 0; i < KEY_CNT; i++) {
649 set = all_keys[i >> 3] & (1 << (i & 7));
651 k = wl_array_add(&keys, sizeof *k);
656 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
658 wl_array_release(&keys);