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>
33 #include "compositor.h"
36 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
39 evdev_led_update(struct evdev_device *device, enum weston_led leds)
42 enum weston_led weston;
45 { LED_NUM_LOCK, LED_NUML },
46 { LED_CAPS_LOCK, LED_CAPSL },
47 { LED_SCROLL_LOCK, LED_SCROLLL },
49 struct input_event ev[ARRAY_LENGTH(map) + 1];
52 if (!device->caps & EVDEV_KEYBOARD)
55 memset(ev, 0, sizeof(ev));
56 for (i = 0; i < ARRAY_LENGTH(map); i++) {
58 ev[i].code = map[i].evdev;
59 ev[i].value = !!(leds & map[i].weston);
62 ev[i].code = SYN_REPORT;
64 i = write(device->fd, ev, sizeof ev);
65 (void)i; /* no, we really don't care about the return value */
69 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
71 if (!device->abs.apply_calibration) {
76 *x = device->abs.x * device->abs.calibration[0] +
77 device->abs.y * device->abs.calibration[1] +
78 device->abs.calibration[2];
80 *y = device->abs.x * device->abs.calibration[3] +
81 device->abs.y * device->abs.calibration[4] +
82 device->abs.calibration[5];
87 evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
89 struct weston_seat *master = device->seat;
94 slot = device->mt.slot;
96 switch (device->pending_event) {
99 case EVDEV_RELATIVE_MOTION:
100 notify_motion(master, time, device->rel.dx, device->rel.dy);
104 case EVDEV_ABSOLUTE_MT_DOWN:
105 weston_output_transform_coordinate(device->output,
106 device->mt.slots[slot].x,
107 device->mt.slots[slot].y,
109 notify_touch(master, time,
110 slot, x, y, WL_TOUCH_DOWN);
112 case EVDEV_ABSOLUTE_MT_MOTION:
113 weston_output_transform_coordinate(device->output,
114 device->mt.slots[slot].x,
115 device->mt.slots[slot].y,
117 notify_touch(master, time,
118 slot, x, y, WL_TOUCH_MOTION);
120 case EVDEV_ABSOLUTE_MT_UP:
121 notify_touch(master, time, slot, 0, 0,
124 case EVDEV_ABSOLUTE_TOUCH_DOWN:
125 transform_absolute(device, &cx, &cy);
126 weston_output_transform_coordinate(device->output,
128 notify_touch(master, time, 0, x, y, WL_TOUCH_DOWN);
130 case EVDEV_ABSOLUTE_MOTION:
131 transform_absolute(device, &cx, &cy);
132 weston_output_transform_coordinate(device->output,
135 if (device->caps & EVDEV_TOUCH)
136 notify_touch(master, time, 0, x, y, WL_TOUCH_MOTION);
138 notify_motion_absolute(master, time, x, y);
140 case EVDEV_ABSOLUTE_TOUCH_UP:
141 notify_touch(master, time, 0, 0, 0, WL_TOUCH_UP);
145 assert(0 && "Unknown pending event type");
148 device->pending_event = EVDEV_NONE;
152 evdev_process_touch_button(struct evdev_device *device, int time, int value)
154 if (device->pending_event != EVDEV_NONE &&
155 device->pending_event != EVDEV_ABSOLUTE_MOTION)
156 evdev_flush_pending_event(device, time);
158 device->pending_event = (value ?
159 EVDEV_ABSOLUTE_TOUCH_DOWN :
160 EVDEV_ABSOLUTE_TOUCH_UP);
164 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
166 /* ignore kernel key repeat */
170 if (e->code == BTN_TOUCH) {
172 evdev_process_touch_button(device, time, e->value);
176 evdev_flush_pending_event(device, time);
187 notify_button(device->seat,
189 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
190 WL_POINTER_BUTTON_STATE_RELEASED);
194 notify_key(device->seat,
196 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
197 WL_KEYBOARD_KEY_STATE_RELEASED,
198 STATE_UPDATE_AUTOMATIC);
204 evdev_process_touch(struct evdev_device *device,
205 struct input_event *e,
208 const int screen_width = device->output->current_mode->width;
209 const int screen_height = device->output->current_mode->height;
213 evdev_flush_pending_event(device, time);
214 device->mt.slot = e->value;
216 case ABS_MT_TRACKING_ID:
217 if (device->pending_event != EVDEV_NONE &&
218 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
219 evdev_flush_pending_event(device, time);
221 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
223 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
225 case ABS_MT_POSITION_X:
226 device->mt.slots[device->mt.slot].x =
227 (e->value - device->abs.min_x) * screen_width /
228 (device->abs.max_x - device->abs.min_x);
229 if (device->pending_event == EVDEV_NONE)
230 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
232 case ABS_MT_POSITION_Y:
233 device->mt.slots[device->mt.slot].y =
234 (e->value - device->abs.min_y) * screen_height /
235 (device->abs.max_y - device->abs.min_y);
236 if (device->pending_event == EVDEV_NONE)
237 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
243 evdev_process_absolute_motion(struct evdev_device *device,
244 struct input_event *e)
246 const int screen_width = device->output->current_mode->width;
247 const int screen_height = device->output->current_mode->height;
252 (e->value - device->abs.min_x) * screen_width /
253 (device->abs.max_x - device->abs.min_x);
254 if (device->pending_event == EVDEV_NONE)
255 device->pending_event = EVDEV_ABSOLUTE_MOTION;
259 (e->value - device->abs.min_y) * screen_height /
260 (device->abs.max_y - device->abs.min_y);
261 if (device->pending_event == EVDEV_NONE)
262 device->pending_event = EVDEV_ABSOLUTE_MOTION;
268 evdev_process_relative(struct evdev_device *device,
269 struct input_event *e, uint32_t time)
273 if (device->pending_event != EVDEV_RELATIVE_MOTION)
274 evdev_flush_pending_event(device, time);
275 device->rel.dx += wl_fixed_from_int(e->value);
276 device->pending_event = EVDEV_RELATIVE_MOTION;
279 if (device->pending_event != EVDEV_RELATIVE_MOTION)
280 evdev_flush_pending_event(device, time);
281 device->rel.dy += wl_fixed_from_int(e->value);
282 device->pending_event = EVDEV_RELATIVE_MOTION;
285 evdev_flush_pending_event(device, time);
291 notify_axis(device->seat,
293 WL_POINTER_AXIS_VERTICAL_SCROLL,
294 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
301 evdev_flush_pending_event(device, time);
307 notify_axis(device->seat,
309 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
310 e->value * DEFAULT_AXIS_STEP_DISTANCE);
320 evdev_process_absolute(struct evdev_device *device,
321 struct input_event *e,
325 evdev_process_touch(device, e, time);
327 evdev_process_absolute_motion(device, e);
332 fallback_process(struct evdev_dispatch *dispatch,
333 struct evdev_device *device,
334 struct input_event *event,
337 switch (event->type) {
339 evdev_process_relative(device, event, time);
342 evdev_process_absolute(device, event, time);
345 evdev_process_key(device, event, time);
348 evdev_flush_pending_event(device, time);
354 fallback_destroy(struct evdev_dispatch *dispatch)
359 struct evdev_dispatch_interface fallback_interface = {
364 static struct evdev_dispatch *
365 fallback_dispatch_create(void)
367 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
368 if (dispatch == NULL)
371 dispatch->interface = &fallback_interface;
377 evdev_process_events(struct evdev_device *device,
378 struct input_event *ev, int count)
380 struct evdev_dispatch *dispatch = device->dispatch;
381 struct input_event *e, *end;
386 for (e = ev; e < end; e++) {
387 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
389 dispatch->interface->process(dispatch, device, e, time);
394 evdev_device_data(int fd, uint32_t mask, void *data)
396 struct weston_compositor *ec;
397 struct evdev_device *device = data;
398 struct input_event ev[32];
401 ec = device->seat->compositor;
405 /* If the compositor is repainting, this function is called only once
406 * per frame and we have to process all the events available on the
407 * fd, otherwise there will be input lag. */
410 len = mtdev_get(device->mtdev, fd, ev,
412 sizeof (struct input_event);
414 len = read(fd, &ev, sizeof ev);
416 if (len < 0 || len % sizeof ev[0] != 0) {
417 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
421 evdev_process_events(device, ev, len / sizeof ev[0]);
429 evdev_handle_device(struct evdev_device *device)
431 struct input_absinfo absinfo;
432 unsigned long ev_bits[NBITS(EV_MAX)];
433 unsigned long abs_bits[NBITS(ABS_MAX)];
434 unsigned long rel_bits[NBITS(REL_MAX)];
435 unsigned long key_bits[NBITS(KEY_MAX)];
436 int has_key, has_abs;
443 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
444 if (TEST_BIT(ev_bits, EV_ABS)) {
447 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
450 if (TEST_BIT(abs_bits, ABS_WHEEL) ||
451 TEST_BIT(abs_bits, ABS_GAS) ||
452 TEST_BIT(abs_bits, ABS_BRAKE) ||
453 TEST_BIT(abs_bits, ABS_HAT0X)) {
454 weston_log("device %s is a joystick, ignoring\n",
459 if (TEST_BIT(abs_bits, ABS_X)) {
460 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
461 device->abs.min_x = absinfo.minimum;
462 device->abs.max_x = absinfo.maximum;
463 device->caps |= EVDEV_MOTION_ABS;
465 if (TEST_BIT(abs_bits, ABS_Y)) {
466 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
467 device->abs.min_y = absinfo.minimum;
468 device->abs.max_y = absinfo.maximum;
469 device->caps |= EVDEV_MOTION_ABS;
471 /* We only handle the slotted Protocol B in weston.
472 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
473 require mtdev for conversion. */
474 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
475 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
476 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
478 device->abs.min_x = absinfo.minimum;
479 device->abs.max_x = absinfo.maximum;
480 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
482 device->abs.min_y = absinfo.minimum;
483 device->abs.max_y = absinfo.maximum;
485 device->caps |= EVDEV_TOUCH;
487 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
488 device->mtdev = mtdev_new_open(device->fd);
489 if (!device->mtdev) {
490 weston_log("mtdev required but failed to open for %s\n",
494 device->mt.slot = device->mtdev->caps.slot.value;
496 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
498 device->mt.slot = absinfo.value;
502 if (TEST_BIT(ev_bits, EV_REL)) {
503 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
505 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
506 device->caps |= EVDEV_MOTION_REL;
508 if (TEST_BIT(ev_bits, EV_KEY)) {
510 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
512 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
513 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
515 device->dispatch = evdev_touchpad_create(device);
516 weston_log("input device %s, %s is a touchpad\n",
517 device->devname, device->devnode);
519 for (i = KEY_ESC; i < KEY_MAX; i++) {
520 if (i >= BTN_MISC && i < KEY_OK)
522 if (TEST_BIT(key_bits, i)) {
523 device->caps |= EVDEV_KEYBOARD;
527 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
528 if (TEST_BIT(key_bits, i)) {
529 device->caps |= EVDEV_BUTTON;
533 if (TEST_BIT(key_bits, BTN_TOUCH)) {
534 device->caps |= EVDEV_TOUCH;
538 if (TEST_BIT(ev_bits, EV_LED)) {
539 device->caps |= EVDEV_KEYBOARD;
542 /* This rule tries to catch accelerometer devices and opt out. We may
543 * want to adjust the protocol later adding a proper event for dealing
544 * with accelerometers and implement here accordingly */
545 if (has_abs && !has_key && !device->is_mt) {
546 weston_log("input device %s, %s "
547 "ignored: unsupported device type\n",
548 device->devname, device->devnode);
556 evdev_configure_device(struct evdev_device *device)
559 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
560 weston_seat_init_pointer(device->seat);
561 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
562 device->devname, device->devnode,
563 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
564 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
565 device->caps & EVDEV_BUTTON ? " button" : "");
567 if ((device->caps & EVDEV_KEYBOARD)) {
568 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
570 weston_log("input device %s, %s is a keyboard\n",
571 device->devname, device->devnode);
573 if ((device->caps & EVDEV_TOUCH)) {
574 weston_seat_init_touch(device->seat);
575 weston_log("input device %s, %s is a touch device\n",
576 device->devname, device->devnode);
582 struct evdev_device *
583 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
585 struct evdev_device *device;
586 struct weston_compositor *ec;
587 char devname[256] = "unknown";
589 device = zalloc(sizeof *device);
593 ec = seat->compositor;
595 container_of(ec->output_list.next, struct weston_output, link);
599 device->mtdev = NULL;
600 device->devnode = strdup(path);
601 device->mt.slot = -1;
604 device->dispatch = NULL;
605 device->fd = device_fd;
606 device->pending_event = EVDEV_NONE;
607 wl_list_init(&device->link);
609 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
610 devname[sizeof(devname) - 1] = '\0';
611 device->devname = strdup(devname);
613 if (!evdev_handle_device(device)) {
614 evdev_device_destroy(device);
615 return EVDEV_UNHANDLED_DEVICE;
618 if (evdev_configure_device(device) == -1)
621 /* If the dispatch was not set up use the fallback. */
622 if (device->dispatch == NULL)
623 device->dispatch = fallback_dispatch_create();
624 if (device->dispatch == NULL)
627 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
629 evdev_device_data, device);
630 if (device->source == NULL)
636 evdev_device_destroy(device);
641 evdev_device_destroy(struct evdev_device *device)
643 struct evdev_dispatch *dispatch;
645 dispatch = device->dispatch;
647 dispatch->interface->destroy(dispatch);
650 wl_event_source_remove(device->source);
651 wl_list_remove(&device->link);
653 mtdev_close_delete(device->mtdev);
655 free(device->devname);
656 free(device->devnode);
661 evdev_notify_keyboard_focus(struct weston_seat *seat,
662 struct wl_list *evdev_devices)
664 struct evdev_device *device;
665 struct wl_array keys;
667 char evdev_keys[(KEY_CNT + 7) / 8];
668 char all_keys[(KEY_CNT + 7) / 8];
675 memset(all_keys, 0, sizeof all_keys);
676 wl_list_for_each(device, evdev_devices, link) {
677 memset(evdev_keys, 0, sizeof evdev_keys);
678 ret = ioctl(device->fd,
679 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
681 weston_log("failed to get keys for device %s\n",
685 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
686 all_keys[i] |= evdev_keys[i];
689 wl_array_init(&keys);
690 for (i = 0; i < KEY_CNT; i++) {
691 set = all_keys[i >> 3] & (1 << (i & 7));
693 k = wl_array_add(&keys, sizeof *k);
698 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
700 wl_array_release(&keys);