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.
28 #include <linux/input.h>
34 #include "compositor.h"
37 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
40 evdev_led_update(struct evdev_device *device, enum weston_led leds)
43 enum weston_led weston;
46 { LED_NUM_LOCK, LED_NUML },
47 { LED_CAPS_LOCK, LED_CAPSL },
48 { LED_SCROLL_LOCK, LED_SCROLLL },
50 struct input_event ev[ARRAY_LENGTH(map) + 1];
53 if (!(device->seat_caps & EVDEV_SEAT_KEYBOARD))
56 memset(ev, 0, sizeof(ev));
57 for (i = 0; i < ARRAY_LENGTH(map); i++) {
59 ev[i].code = map[i].evdev;
60 ev[i].value = !!(leds & map[i].weston);
63 ev[i].code = SYN_REPORT;
65 i = write(device->fd, ev, sizeof ev);
66 (void)i; /* no, we really don't care about the return value */
70 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
72 if (!device->abs.apply_calibration) {
77 *x = device->abs.x * device->abs.calibration[0] +
78 device->abs.y * device->abs.calibration[1] +
79 device->abs.calibration[2];
81 *y = device->abs.x * device->abs.calibration[3] +
82 device->abs.y * device->abs.calibration[4] +
83 device->abs.calibration[5];
88 evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
90 struct weston_seat *master = device->seat;
95 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 if (device->output == NULL)
107 weston_output_transform_coordinate(device->output,
108 wl_fixed_from_int(device->mt.slots[slot].x),
109 wl_fixed_from_int(device->mt.slots[slot].y),
111 seat_slot = ffs(~master->slot_map) - 1;
112 device->mt.slots[slot].seat_slot = seat_slot;
113 master->slot_map |= 1 << seat_slot;
115 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_DOWN);
117 case EVDEV_ABSOLUTE_MT_MOTION:
118 if (device->output == NULL)
120 weston_output_transform_coordinate(device->output,
121 wl_fixed_from_int(device->mt.slots[slot].x),
122 wl_fixed_from_int(device->mt.slots[slot].y),
124 seat_slot = device->mt.slots[slot].seat_slot;
125 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_MOTION);
127 case EVDEV_ABSOLUTE_MT_UP:
128 seat_slot = device->mt.slots[slot].seat_slot;
129 master->slot_map &= ~(1 << seat_slot);
130 notify_touch(master, time, seat_slot, 0, 0, WL_TOUCH_UP);
132 case EVDEV_ABSOLUTE_TOUCH_DOWN:
133 if (device->output == NULL)
135 transform_absolute(device, &cx, &cy);
136 weston_output_transform_coordinate(device->output,
137 wl_fixed_from_int(cx),
138 wl_fixed_from_int(cy),
140 seat_slot = ffs(~master->slot_map) - 1;
141 device->abs.seat_slot = seat_slot;
142 master->slot_map |= 1 << seat_slot;
143 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_DOWN);
145 case EVDEV_ABSOLUTE_MOTION:
146 if (device->output == NULL)
148 transform_absolute(device, &cx, &cy);
149 weston_output_transform_coordinate(device->output,
150 wl_fixed_from_int(cx),
151 wl_fixed_from_int(cy),
154 if (device->seat_caps & EVDEV_SEAT_TOUCH)
155 notify_touch(master, time, device->abs.seat_slot,
156 x, y, WL_TOUCH_MOTION);
157 else if (device->seat_caps & EVDEV_SEAT_POINTER)
158 notify_motion_absolute(master, time, x, y);
160 case EVDEV_ABSOLUTE_TOUCH_UP:
161 seat_slot = device->abs.seat_slot;
162 master->slot_map &= ~(1 << seat_slot);
163 notify_touch(master, time, seat_slot, 0, 0, WL_TOUCH_UP);
166 assert(0 && "Unknown pending event type");
169 device->pending_event = EVDEV_NONE;
173 evdev_process_touch_button(struct evdev_device *device, int time, int value)
175 if (device->pending_event != EVDEV_NONE &&
176 device->pending_event != EVDEV_ABSOLUTE_MOTION)
177 evdev_flush_pending_event(device, time);
179 device->pending_event = (value ?
180 EVDEV_ABSOLUTE_TOUCH_DOWN :
181 EVDEV_ABSOLUTE_TOUCH_UP);
185 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
187 /* ignore kernel key repeat */
191 if (e->code == BTN_TOUCH) {
193 evdev_process_touch_button(device, time, e->value);
197 evdev_flush_pending_event(device, time);
208 notify_button(device->seat,
210 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
211 WL_POINTER_BUTTON_STATE_RELEASED);
215 notify_key(device->seat,
217 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
218 WL_KEYBOARD_KEY_STATE_RELEASED,
219 STATE_UPDATE_AUTOMATIC);
225 evdev_process_touch(struct evdev_device *device,
226 struct input_event *e,
229 int screen_width, screen_height;
231 if (device->output == NULL)
234 screen_width = device->output->current_mode->width;
235 screen_height = device->output->current_mode->height;
239 evdev_flush_pending_event(device, time);
240 device->mt.slot = e->value;
242 case ABS_MT_TRACKING_ID:
243 if (device->pending_event != EVDEV_NONE &&
244 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
245 evdev_flush_pending_event(device, time);
247 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
249 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
251 case ABS_MT_POSITION_X:
252 device->mt.slots[device->mt.slot].x =
253 (e->value - device->abs.min_x) * screen_width /
254 (device->abs.max_x - device->abs.min_x);
255 if (device->pending_event == EVDEV_NONE)
256 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
258 case ABS_MT_POSITION_Y:
259 device->mt.slots[device->mt.slot].y =
260 (e->value - device->abs.min_y) * screen_height /
261 (device->abs.max_y - device->abs.min_y);
262 if (device->pending_event == EVDEV_NONE)
263 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
269 evdev_process_absolute_motion(struct evdev_device *device,
270 struct input_event *e)
272 int screen_width, screen_height;
274 if (device->output == NULL)
277 screen_width = device->output->current_mode->width;
278 screen_height = device->output->current_mode->height;
283 (e->value - device->abs.min_x) * screen_width /
284 (device->abs.max_x - device->abs.min_x);
285 if (device->pending_event == EVDEV_NONE)
286 device->pending_event = EVDEV_ABSOLUTE_MOTION;
290 (e->value - device->abs.min_y) * screen_height /
291 (device->abs.max_y - device->abs.min_y);
292 if (device->pending_event == EVDEV_NONE)
293 device->pending_event = EVDEV_ABSOLUTE_MOTION;
299 evdev_process_relative(struct evdev_device *device,
300 struct input_event *e, uint32_t time)
304 if (device->pending_event != EVDEV_RELATIVE_MOTION)
305 evdev_flush_pending_event(device, time);
306 device->rel.dx += wl_fixed_from_int(e->value);
307 device->pending_event = EVDEV_RELATIVE_MOTION;
310 if (device->pending_event != EVDEV_RELATIVE_MOTION)
311 evdev_flush_pending_event(device, time);
312 device->rel.dy += wl_fixed_from_int(e->value);
313 device->pending_event = EVDEV_RELATIVE_MOTION;
316 evdev_flush_pending_event(device, time);
322 notify_axis(device->seat,
324 WL_POINTER_AXIS_VERTICAL_SCROLL,
325 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
332 evdev_flush_pending_event(device, time);
338 notify_axis(device->seat,
340 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
341 e->value * DEFAULT_AXIS_STEP_DISTANCE);
351 evdev_process_absolute(struct evdev_device *device,
352 struct input_event *e,
356 evdev_process_touch(device, e, time);
358 evdev_process_absolute_motion(device, e);
363 fallback_process(struct evdev_dispatch *dispatch,
364 struct evdev_device *device,
365 struct input_event *event,
368 switch (event->type) {
370 evdev_process_relative(device, event, time);
373 evdev_process_absolute(device, event, time);
376 evdev_process_key(device, event, time);
379 evdev_flush_pending_event(device, time);
385 fallback_destroy(struct evdev_dispatch *dispatch)
390 struct evdev_dispatch_interface fallback_interface = {
395 static struct evdev_dispatch *
396 fallback_dispatch_create(void)
398 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
399 if (dispatch == NULL)
402 dispatch->interface = &fallback_interface;
408 evdev_process_events(struct evdev_device *device,
409 struct input_event *ev, int count)
411 struct evdev_dispatch *dispatch = device->dispatch;
412 struct input_event *e, *end;
417 for (e = ev; e < end; e++) {
418 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
420 dispatch->interface->process(dispatch, device, e, time);
425 evdev_device_data(int fd, uint32_t mask, void *data)
427 struct weston_compositor *ec;
428 struct evdev_device *device = data;
429 struct input_event ev[32];
432 ec = device->seat->compositor;
433 if (!ec->session_active)
436 /* If the compositor is repainting, this function is called only once
437 * per frame and we have to process all the events available on the
438 * fd, otherwise there will be input lag. */
441 len = mtdev_get(device->mtdev, fd, ev,
443 sizeof (struct input_event);
445 len = read(fd, &ev, sizeof ev);
447 if (len < 0 || len % sizeof ev[0] != 0) {
448 if (len < 0 && errno != EAGAIN && errno != EINTR) {
449 weston_log("device %s died\n",
451 wl_event_source_remove(device->source);
452 device->source = NULL;
458 evdev_process_events(device, ev, len / sizeof ev[0]);
466 evdev_configure_device(struct evdev_device *device)
468 struct input_absinfo absinfo;
469 unsigned long ev_bits[NBITS(EV_MAX)];
470 unsigned long abs_bits[NBITS(ABS_MAX)];
471 unsigned long rel_bits[NBITS(REL_MAX)];
472 unsigned long key_bits[NBITS(KEY_MAX)];
473 int has_abs, has_rel, has_mt;
474 int has_button, has_keyboard, has_touch;
484 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
485 if (TEST_BIT(ev_bits, EV_ABS)) {
486 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
489 if (TEST_BIT(abs_bits, ABS_X)) {
490 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
491 device->abs.min_x = absinfo.minimum;
492 device->abs.max_x = absinfo.maximum;
495 if (TEST_BIT(abs_bits, ABS_Y)) {
496 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
497 device->abs.min_y = absinfo.minimum;
498 device->abs.max_y = absinfo.maximum;
501 /* We only handle the slotted Protocol B in weston.
502 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
503 require mtdev for conversion. */
504 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
505 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
506 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
508 device->abs.min_x = absinfo.minimum;
509 device->abs.max_x = absinfo.maximum;
510 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
512 device->abs.min_y = absinfo.minimum;
513 device->abs.max_y = absinfo.maximum;
518 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
519 device->mtdev = mtdev_new_open(device->fd);
520 if (!device->mtdev) {
521 weston_log("mtdev required but failed to open for %s\n",
525 device->mt.slot = device->mtdev->caps.slot.value;
527 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
529 device->mt.slot = absinfo.value;
533 if (TEST_BIT(ev_bits, EV_REL)) {
534 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
536 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
539 if (TEST_BIT(ev_bits, EV_KEY)) {
540 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
542 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
543 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
544 (has_abs || has_mt)) {
545 device->dispatch = evdev_touchpad_create(device);
546 weston_log("input device %s, %s is a touchpad\n",
547 device->devname, device->devnode);
549 for (i = KEY_ESC; i < KEY_MAX; i++) {
550 if (i >= BTN_MISC && i < KEY_OK)
552 if (TEST_BIT(key_bits, i)) {
557 if (TEST_BIT(key_bits, BTN_TOUCH))
559 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
560 if (TEST_BIT(key_bits, i)) {
566 if (TEST_BIT(ev_bits, EV_LED))
569 if ((has_abs || has_rel) && has_button) {
570 weston_seat_init_pointer(device->seat);
571 device->seat_caps |= EVDEV_SEAT_POINTER;
572 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
573 device->devname, device->devnode,
574 has_abs ? " absolute-motion" : "",
575 has_rel ? " relative-motion": "",
576 has_button ? " button" : "");
579 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
581 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
582 weston_log("input device %s, %s is a keyboard\n",
583 device->devname, device->devnode);
585 if (has_touch && !has_button) {
586 weston_seat_init_touch(device->seat);
587 device->seat_caps |= EVDEV_SEAT_TOUCH;
588 weston_log("input device %s, %s is a touch device\n",
589 device->devname, device->devnode);
596 notify_output_destroy(struct wl_listener *listener, void *data)
598 struct evdev_device *device =
599 container_of(listener,
600 struct evdev_device, output_destroy_listener);
601 struct weston_compositor *c = device->seat->compositor;
602 struct weston_output *output;
604 if (!device->output_name && !wl_list_empty(&c->output_list)) {
605 output = container_of(c->output_list.next,
606 struct weston_output, link);
607 evdev_device_set_output(device, output);
609 device->output = NULL;
614 evdev_device_set_output(struct evdev_device *device,
615 struct weston_output *output)
617 if (device->output_destroy_listener.notify) {
618 wl_list_remove(&device->output_destroy_listener.link);
619 device->output_destroy_listener.notify = NULL;
622 device->output = output;
623 device->output_destroy_listener.notify = notify_output_destroy;
624 wl_signal_add(&output->destroy_signal,
625 &device->output_destroy_listener);
628 struct evdev_device *
629 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
631 struct evdev_device *device;
632 struct weston_compositor *ec;
633 char devname[256] = "unknown";
635 device = zalloc(sizeof *device);
639 ec = seat->compositor;
641 device->seat_caps = 0;
643 device->mtdev = NULL;
644 device->devnode = strdup(path);
645 device->mt.slot = -1;
648 device->dispatch = NULL;
649 device->fd = device_fd;
650 device->pending_event = EVDEV_NONE;
651 wl_list_init(&device->link);
653 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
654 devname[sizeof(devname) - 1] = '\0';
655 device->devname = strdup(devname);
657 if (evdev_configure_device(device) == -1)
660 if (device->seat_caps == 0) {
661 evdev_device_destroy(device);
662 return EVDEV_UNHANDLED_DEVICE;
665 /* If the dispatch was not set up use the fallback. */
666 if (device->dispatch == NULL)
667 device->dispatch = fallback_dispatch_create();
668 if (device->dispatch == NULL)
671 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
673 evdev_device_data, device);
674 if (device->source == NULL)
680 evdev_device_destroy(device);
685 evdev_device_destroy(struct evdev_device *device)
687 struct evdev_dispatch *dispatch;
689 if (device->seat_caps & EVDEV_SEAT_POINTER)
690 weston_seat_release_pointer(device->seat);
691 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
692 weston_seat_release_keyboard(device->seat);
693 if (device->seat_caps & EVDEV_SEAT_TOUCH)
694 weston_seat_release_touch(device->seat);
696 dispatch = device->dispatch;
698 dispatch->interface->destroy(dispatch);
701 wl_event_source_remove(device->source);
703 wl_list_remove(&device->output_destroy_listener.link);
704 wl_list_remove(&device->link);
706 mtdev_close_delete(device->mtdev);
708 free(device->devname);
709 free(device->devnode);
710 free(device->output_name);
715 evdev_notify_keyboard_focus(struct weston_seat *seat,
716 struct wl_list *evdev_devices)
718 struct evdev_device *device;
719 struct wl_array keys;
721 char evdev_keys[(KEY_CNT + 7) / 8];
722 char all_keys[(KEY_CNT + 7) / 8];
726 if (!seat->keyboard_device_count > 0)
729 memset(all_keys, 0, sizeof all_keys);
730 wl_list_for_each(device, evdev_devices, link) {
731 memset(evdev_keys, 0, sizeof evdev_keys);
732 ret = ioctl(device->fd,
733 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
735 weston_log("failed to get keys for device %s\n",
739 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
740 all_keys[i] |= evdev_keys[i];
743 wl_array_init(&keys);
744 for (i = 0; i < KEY_CNT; i++) {
745 set = all_keys[i >> 3] & (1 << (i & 7));
747 k = wl_array_add(&keys, sizeof *k);
752 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
754 wl_array_release(&keys);