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.
26 #include <linux/input.h>
31 #include "compositor.h"
33 #include "evdev-private.h"
34 #include "launcher-util.h"
37 evdev_led_update(struct weston_seat *seat_base, enum weston_led leds)
40 enum weston_led weston;
43 { LED_NUM_LOCK, LED_NUML },
44 { LED_CAPS_LOCK, LED_CAPSL },
45 { LED_SCROLL_LOCK, LED_SCROLLL },
47 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
48 struct evdev_input_device *device;
49 struct input_event ev[ARRAY_LENGTH(map)];
52 memset(ev, 0, sizeof(ev));
53 for (i = 0; i < ARRAY_LENGTH(map); i++) {
55 ev[i].code = map[i].evdev;
56 ev[i].value = !!(leds & map[i].weston);
59 wl_list_for_each(device, &seat->devices_list, link) {
60 if (device->caps & EVDEV_KEYBOARD)
61 write(device->fd, ev, sizeof *ev);
66 evdev_process_key(struct evdev_input_device *device,
67 struct input_event *e, int time)
81 notify_button(&device->master->base.seat,
83 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
84 WL_POINTER_BUTTON_STATE_RELEASED);
88 notify_key(&device->master->base.seat,
89 time, e->code, e->value);
95 evdev_process_touch(struct evdev_input_device *device,
96 struct input_event *e)
98 const int screen_width = device->output->current->width;
99 const int screen_height = device->output->current->height;
103 device->mt.slot = e->value;
105 case ABS_MT_TRACKING_ID:
107 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
109 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
111 case ABS_MT_POSITION_X:
112 device->mt.x[device->mt.slot] =
113 (e->value - device->abs.min_x) * screen_width /
114 (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) +
123 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
129 evdev_process_absolute_motion(struct evdev_input_device *device,
130 struct input_event *e)
132 const int screen_width = device->output->current->width;
133 const int screen_height = device->output->current->height;
138 (e->value - device->abs.min_x) * screen_width /
139 (device->abs.max_x - device->abs.min_x) +
141 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
145 (e->value - device->abs.min_y) * screen_height /
146 (device->abs.max_y - device->abs.min_y) +
148 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
154 evdev_process_relative(struct evdev_input_device *device,
155 struct input_event *e, uint32_t time)
159 device->rel.dx += wl_fixed_from_int(e->value);
160 device->pending_events |= EVDEV_RELATIVE_MOTION;
163 device->rel.dy += wl_fixed_from_int(e->value);
164 device->pending_events |= EVDEV_RELATIVE_MOTION;
167 notify_axis(&device->master->base.seat,
169 WL_POINTER_AXIS_VERTICAL_SCROLL, e->value);
172 notify_axis(&device->master->base.seat,
174 WL_POINTER_AXIS_HORIZONTAL_SCROLL, e->value);
180 evdev_process_absolute(struct evdev_input_device *device,
181 struct input_event *e)
184 evdev_process_touch(device, e);
186 evdev_process_absolute_motion(device, e);
191 is_motion_event(struct input_event *e)
204 case ABS_MT_POSITION_X:
205 case ABS_MT_POSITION_Y:
214 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
216 struct weston_seat *master = &device->master->base;
218 if (!device->pending_events)
221 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
222 notify_motion(&master->seat, time,
223 master->seat.pointer->x + device->rel.dx,
224 master->seat.pointer->y + device->rel.dy);
225 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
229 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
230 notify_touch(&master->seat, time,
232 wl_fixed_from_int(device->mt.x[device->mt.slot]),
233 wl_fixed_from_int(device->mt.y[device->mt.slot]),
235 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
236 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
238 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
239 notify_touch(&master->seat, time,
241 wl_fixed_from_int(device->mt.x[device->mt.slot]),
242 wl_fixed_from_int(device->mt.y[device->mt.slot]),
244 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
245 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
247 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
248 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
250 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
252 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
253 notify_motion(&master->seat, time,
254 wl_fixed_from_int(device->abs.x),
255 wl_fixed_from_int(device->abs.y));
256 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
261 fallback_process(struct evdev_dispatch *dispatch,
262 struct evdev_input_device *device,
263 struct input_event *event,
266 switch (event->type) {
268 evdev_process_relative(device, event, time);
271 evdev_process_absolute(device, event);
274 evdev_process_key(device, event, time);
280 fallback_destroy(struct evdev_dispatch *dispatch)
285 struct evdev_dispatch_interface fallback_interface = {
290 static struct evdev_dispatch *
291 fallback_dispatch_create(void)
293 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
294 if (dispatch == NULL)
297 dispatch->interface = &fallback_interface;
303 evdev_process_events(struct evdev_input_device *device,
304 struct input_event *ev, int count)
306 struct evdev_dispatch *dispatch = device->dispatch;
307 struct input_event *e, *end;
310 device->pending_events = 0;
314 for (e = ev; e < end; e++) {
315 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
317 /* we try to minimize the amount of notifications to be
318 * forwarded to the compositor, so we accumulate motion
319 * events and send as a bunch */
320 if (!is_motion_event(e))
321 evdev_flush_motion(device, time);
323 dispatch->interface->process(dispatch, device, e, time);
326 evdev_flush_motion(device, time);
330 evdev_input_device_data(int fd, uint32_t mask, void *data)
332 struct weston_compositor *ec;
333 struct evdev_input_device *device = data;
334 struct input_event ev[32];
337 ec = device->master->base.compositor;
341 /* If the compositor is repainting, this function is called only once
342 * per frame and we have to process all the events available on the
343 * fd, otherwise there will be input lag. */
346 len = mtdev_get(device->mtdev, fd, ev,
348 sizeof (struct input_event);
350 len = read(fd, &ev, sizeof ev);
352 if (len < 0 || len % sizeof ev[0] != 0) {
353 /* FIXME: call device_removed when errno is ENODEV. */
357 evdev_process_events(device, ev, len / sizeof ev[0]);
365 evdev_configure_device(struct evdev_input_device *device)
367 struct input_absinfo absinfo;
368 unsigned long ev_bits[NBITS(EV_MAX)];
369 unsigned long abs_bits[NBITS(ABS_MAX)];
370 unsigned long rel_bits[NBITS(REL_MAX)];
371 unsigned long key_bits[NBITS(KEY_MAX)];
372 int has_key, has_abs;
379 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
380 if (TEST_BIT(ev_bits, EV_ABS)) {
383 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
385 if (TEST_BIT(abs_bits, ABS_X)) {
386 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
387 device->abs.min_x = absinfo.minimum;
388 device->abs.max_x = absinfo.maximum;
389 device->caps |= EVDEV_MOTION_ABS;
391 if (TEST_BIT(abs_bits, ABS_Y)) {
392 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
393 device->abs.min_y = absinfo.minimum;
394 device->abs.max_y = absinfo.maximum;
395 device->caps |= EVDEV_MOTION_ABS;
397 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
400 device->caps |= EVDEV_TOUCH;
403 if (TEST_BIT(ev_bits, EV_REL)) {
404 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
406 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
407 device->caps |= EVDEV_MOTION_REL;
409 if (TEST_BIT(ev_bits, EV_KEY)) {
411 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
413 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
414 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
416 device->dispatch = evdev_touchpad_create(device);
417 for (i = KEY_ESC; i < KEY_MAX; i++) {
418 if (i >= BTN_MISC && i < KEY_OK)
420 if (TEST_BIT(key_bits, i)) {
421 device->caps |= EVDEV_KEYBOARD;
425 for (i = BTN_MISC; i < KEY_OK; i++) {
426 if (TEST_BIT(key_bits, i)) {
427 device->caps |= EVDEV_BUTTON;
433 /* This rule tries to catch accelerometer devices and opt out. We may
434 * want to adjust the protocol later adding a proper event for dealing
435 * with accelerometers and implement here accordingly */
436 if (has_abs && !has_key)
442 static struct evdev_input_device *
443 evdev_input_device_create(struct evdev_seat *master,
444 struct wl_display *display, const char *path)
446 struct evdev_input_device *device;
447 struct weston_compositor *ec;
449 device = malloc(sizeof *device);
453 ec = master->base.compositor;
455 container_of(ec->output_list.next, struct weston_output, link);
457 device->master = master;
459 device->mtdev = NULL;
460 device->devnode = strdup(path);
461 device->mt.slot = -1;
464 device->dispatch = NULL;
466 /* Use non-blocking mode so that we can loop on read on
467 * evdev_input_device_data() until all events on the fd are
468 * read. mtdev_get() also expects this. */
469 device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
473 if (evdev_configure_device(device) == -1)
476 /* If the dispatch was not set up use the fallback. */
477 if (device->dispatch == NULL)
478 device->dispatch = fallback_dispatch_create();
479 if (device->dispatch == NULL)
484 device->mtdev = mtdev_new_open(device->fd);
486 fprintf(stderr, "mtdev failed to open for %s\n", path);
489 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
491 evdev_input_device_data, device);
492 if (device->source == NULL)
495 wl_list_insert(master->devices_list.prev, &device->link);
500 device->dispatch->interface->destroy(device->dispatch);
504 free(device->devnode);
509 static const char default_seat[] = "seat0";
512 device_added(struct udev_device *udev_device, struct evdev_seat *master)
514 struct weston_compositor *c;
516 const char *device_seat;
518 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
520 device_seat = default_seat;
522 if (strcmp(device_seat, master->seat_id))
525 c = master->base.compositor;
526 devnode = udev_device_get_devnode(udev_device);
527 evdev_input_device_create(master, c->wl_display, devnode);
531 device_removed(struct evdev_input_device *device)
533 struct evdev_dispatch *dispatch;
535 dispatch = device->dispatch;
537 dispatch->interface->destroy(dispatch);
539 wl_event_source_remove(device->source);
540 wl_list_remove(&device->link);
542 mtdev_close_delete(device->mtdev);
544 free(device->devnode);
549 evdev_notify_keyboard_focus(struct evdev_seat *seat)
551 struct evdev_input_device *device;
552 struct wl_array keys;
554 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
558 memset(all_keys, 0, sizeof all_keys);
559 wl_list_for_each(device, &seat->devices_list, link) {
560 memset(evdev_keys, 0, sizeof evdev_keys);
561 ret = ioctl(device->fd,
562 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
564 fprintf(stderr, "failed to get keys for device %s\n",
568 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
569 all_keys[i] |= evdev_keys[i];
572 wl_array_init(&keys);
573 for (i = 0; i < KEY_CNT; i++) {
574 set = all_keys[i >> 3] & (1 << (i & 7));
576 k = wl_array_add(&keys, sizeof *k);
581 notify_keyboard_focus(&seat->base.seat, &keys);
583 wl_array_release(&keys);
587 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
589 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
590 struct udev_enumerate *e;
591 struct udev_list_entry *entry;
592 struct udev_device *device;
593 const char *path, *sysname;
595 e = udev_enumerate_new(udev);
596 udev_enumerate_add_match_subsystem(e, "input");
597 udev_enumerate_scan_devices(e);
598 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
599 path = udev_list_entry_get_name(entry);
600 device = udev_device_new_from_syspath(udev, path);
602 sysname = udev_device_get_sysname(device);
603 if (strncmp("event", sysname, 5) != 0) {
604 udev_device_unref(device);
608 device_added(device, seat);
610 udev_device_unref(device);
612 udev_enumerate_unref(e);
614 evdev_notify_keyboard_focus(seat);
616 if (wl_list_empty(&seat->devices_list)) {
618 "warning: no input devices on entering Weston. "
620 "\t- no permissions to read /dev/input/event*\n"
621 "\t- seats misconfigured "
622 "(Weston backend option 'seat', "
623 "udev device property ID_SEAT)\n");
628 evdev_udev_handler(int fd, uint32_t mask, void *data)
630 struct evdev_seat *master = data;
631 struct udev_device *udev_device;
632 struct evdev_input_device *device, *next;
636 udev_device = udev_monitor_receive_device(master->udev_monitor);
640 action = udev_device_get_action(udev_device);
642 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
645 if (!strcmp(action, "add")) {
646 device_added(udev_device, master);
648 else if (!strcmp(action, "remove")) {
649 devnode = udev_device_get_devnode(udev_device);
650 wl_list_for_each_safe(device, next,
651 &master->devices_list, link)
652 if (!strcmp(device->devnode, devnode)) {
653 device_removed(device);
658 udev_device_unref(udev_device);
664 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
666 struct evdev_seat *master = (struct evdev_seat *) seat_base;
667 struct wl_event_loop *loop;
668 struct weston_compositor *c = master->base.compositor;
671 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
672 if (!master->udev_monitor) {
673 fprintf(stderr, "udev: failed to create the udev monitor\n");
677 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
680 if (udev_monitor_enable_receiving(master->udev_monitor)) {
681 fprintf(stderr, "udev: failed to bind the udev monitor\n");
682 udev_monitor_unref(master->udev_monitor);
686 loop = wl_display_get_event_loop(c->wl_display);
687 fd = udev_monitor_get_fd(master->udev_monitor);
688 master->udev_monitor_source =
689 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
690 evdev_udev_handler, master);
691 if (!master->udev_monitor_source) {
692 udev_monitor_unref(master->udev_monitor);
700 evdev_disable_udev_monitor(struct weston_seat *seat_base)
702 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
704 if (!seat->udev_monitor)
707 udev_monitor_unref(seat->udev_monitor);
708 seat->udev_monitor = NULL;
709 wl_event_source_remove(seat->udev_monitor_source);
710 seat->udev_monitor_source = NULL;
714 evdev_input_create(struct weston_compositor *c, struct udev *udev,
717 struct evdev_seat *seat;
719 seat = malloc(sizeof *seat);
723 memset(seat, 0, sizeof *seat);
724 weston_seat_init(&seat->base, c);
725 seat->base.led_update = evdev_led_update;
727 wl_list_init(&seat->devices_list);
728 seat->seat_id = strdup(seat_id);
729 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
735 evdev_add_devices(udev, &seat->base);
737 c->seat = &seat->base;
741 evdev_remove_devices(struct weston_seat *seat_base)
743 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
744 struct evdev_input_device *device, *next;
746 wl_list_for_each_safe(device, next, &seat->devices_list, link)
747 device_removed(device);
749 notify_keyboard_focus(&seat->base.seat, NULL);
753 evdev_input_destroy(struct weston_seat *seat_base)
755 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
757 evdev_remove_devices(seat_base);
758 evdev_disable_udev_monitor(&seat->base);
760 wl_list_remove(&seat->base.link);