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.
25 #include <linux/input.h>
30 #include "compositor.h"
32 #include "evdev-private.h"
33 #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,
90 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
91 WL_KEYBOARD_KEY_STATE_RELEASED);
97 evdev_process_touch(struct evdev_input_device *device,
98 struct input_event *e)
100 const int screen_width = device->output->current->width;
101 const int screen_height = device->output->current->height;
105 device->mt.slot = e->value;
107 case ABS_MT_TRACKING_ID:
109 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
111 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
113 case ABS_MT_POSITION_X:
114 device->mt.x[device->mt.slot] =
115 (e->value - device->abs.min_x) * screen_width /
116 (device->abs.max_x - device->abs.min_x) +
118 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
120 case ABS_MT_POSITION_Y:
121 device->mt.y[device->mt.slot] =
122 (e->value - device->abs.min_y) * screen_height /
123 (device->abs.max_y - device->abs.min_y) +
125 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
131 evdev_process_absolute_motion(struct evdev_input_device *device,
132 struct input_event *e)
134 const int screen_width = device->output->current->width;
135 const int screen_height = device->output->current->height;
140 (e->value - device->abs.min_x) * screen_width /
141 (device->abs.max_x - device->abs.min_x) +
143 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
147 (e->value - device->abs.min_y) * screen_height /
148 (device->abs.max_y - device->abs.min_y) +
150 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
156 evdev_process_relative(struct evdev_input_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;
169 notify_axis(&device->master->base.seat,
171 WL_POINTER_AXIS_VERTICAL_SCROLL,
172 wl_fixed_from_int(e->value));
175 notify_axis(&device->master->base.seat,
177 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
178 wl_fixed_from_int(e->value));
184 evdev_process_absolute(struct evdev_input_device *device,
185 struct input_event *e)
188 evdev_process_touch(device, e);
190 evdev_process_absolute_motion(device, e);
195 is_motion_event(struct input_event *e)
208 case ABS_MT_POSITION_X:
209 case ABS_MT_POSITION_Y:
218 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
220 struct weston_seat *master = &device->master->base;
222 if (!device->pending_events)
225 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
226 notify_motion(&master->seat, time,
227 master->seat.pointer->x + device->rel.dx,
228 master->seat.pointer->y + device->rel.dy);
229 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
233 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
234 notify_touch(&master->seat, time,
236 wl_fixed_from_int(device->mt.x[device->mt.slot]),
237 wl_fixed_from_int(device->mt.y[device->mt.slot]),
239 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
240 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
242 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
243 notify_touch(&master->seat, time,
245 wl_fixed_from_int(device->mt.x[device->mt.slot]),
246 wl_fixed_from_int(device->mt.y[device->mt.slot]),
248 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
249 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
251 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
252 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
254 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
256 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
257 notify_motion(&master->seat, time,
258 wl_fixed_from_int(device->abs.x),
259 wl_fixed_from_int(device->abs.y));
260 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
265 fallback_process(struct evdev_dispatch *dispatch,
266 struct evdev_input_device *device,
267 struct input_event *event,
270 switch (event->type) {
272 evdev_process_relative(device, event, time);
275 evdev_process_absolute(device, event);
278 evdev_process_key(device, event, time);
284 fallback_destroy(struct evdev_dispatch *dispatch)
289 struct evdev_dispatch_interface fallback_interface = {
294 static struct evdev_dispatch *
295 fallback_dispatch_create(void)
297 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
298 if (dispatch == NULL)
301 dispatch->interface = &fallback_interface;
307 evdev_process_events(struct evdev_input_device *device,
308 struct input_event *ev, int count)
310 struct evdev_dispatch *dispatch = device->dispatch;
311 struct input_event *e, *end;
314 device->pending_events = 0;
318 for (e = ev; e < end; e++) {
319 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
321 /* we try to minimize the amount of notifications to be
322 * forwarded to the compositor, so we accumulate motion
323 * events and send as a bunch */
324 if (!is_motion_event(e))
325 evdev_flush_motion(device, time);
327 dispatch->interface->process(dispatch, device, e, time);
330 evdev_flush_motion(device, time);
334 evdev_input_device_data(int fd, uint32_t mask, void *data)
336 struct weston_compositor *ec;
337 struct evdev_input_device *device = data;
338 struct input_event ev[32];
341 ec = device->master->base.compositor;
345 /* If the compositor is repainting, this function is called only once
346 * per frame and we have to process all the events available on the
347 * fd, otherwise there will be input lag. */
350 len = mtdev_get(device->mtdev, fd, ev,
352 sizeof (struct input_event);
354 len = read(fd, &ev, sizeof ev);
356 if (len < 0 || len % sizeof ev[0] != 0) {
357 /* FIXME: call device_removed when errno is ENODEV. */
361 evdev_process_events(device, ev, len / sizeof ev[0]);
369 evdev_configure_device(struct evdev_input_device *device)
371 struct input_absinfo absinfo;
372 unsigned long ev_bits[NBITS(EV_MAX)];
373 unsigned long abs_bits[NBITS(ABS_MAX)];
374 unsigned long rel_bits[NBITS(REL_MAX)];
375 unsigned long key_bits[NBITS(KEY_MAX)];
376 int has_key, has_abs;
383 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
384 if (TEST_BIT(ev_bits, EV_ABS)) {
387 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
389 if (TEST_BIT(abs_bits, ABS_X)) {
390 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
391 device->abs.min_x = absinfo.minimum;
392 device->abs.max_x = absinfo.maximum;
393 device->caps |= EVDEV_MOTION_ABS;
395 if (TEST_BIT(abs_bits, ABS_Y)) {
396 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
397 device->abs.min_y = absinfo.minimum;
398 device->abs.max_y = absinfo.maximum;
399 device->caps |= EVDEV_MOTION_ABS;
401 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
404 device->caps |= EVDEV_TOUCH;
407 if (TEST_BIT(ev_bits, EV_REL)) {
408 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
410 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
411 device->caps |= EVDEV_MOTION_REL;
413 if (TEST_BIT(ev_bits, EV_KEY)) {
415 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
417 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
418 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
420 device->dispatch = evdev_touchpad_create(device);
421 for (i = KEY_ESC; i < KEY_MAX; i++) {
422 if (i >= BTN_MISC && i < KEY_OK)
424 if (TEST_BIT(key_bits, i)) {
425 device->caps |= EVDEV_KEYBOARD;
429 for (i = BTN_MISC; i < KEY_OK; i++) {
430 if (TEST_BIT(key_bits, i)) {
431 device->caps |= EVDEV_BUTTON;
436 if (TEST_BIT(ev_bits, EV_LED)) {
437 device->caps |= EVDEV_KEYBOARD;
440 /* This rule tries to catch accelerometer devices and opt out. We may
441 * want to adjust the protocol later adding a proper event for dealing
442 * with accelerometers and implement here accordingly */
443 if (has_abs && !has_key)
447 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
448 weston_seat_init_pointer(&device->master->base);
449 if ((device->caps & EVDEV_KEYBOARD))
450 weston_seat_init_keyboard(&device->master->base, NULL);
451 if ((device->caps & EVDEV_TOUCH))
452 weston_seat_init_touch(&device->master->base);
457 static struct evdev_input_device *
458 evdev_input_device_create(struct evdev_seat *master,
459 struct wl_display *display, const char *path)
461 struct evdev_input_device *device;
462 struct weston_compositor *ec;
464 device = malloc(sizeof *device);
468 ec = master->base.compositor;
470 container_of(ec->output_list.next, struct weston_output, link);
472 device->master = master;
474 device->mtdev = NULL;
475 device->devnode = strdup(path);
476 device->mt.slot = -1;
479 device->dispatch = NULL;
481 /* Use non-blocking mode so that we can loop on read on
482 * evdev_input_device_data() until all events on the fd are
483 * read. mtdev_get() also expects this. */
484 device->fd = weston_launcher_open(ec, path, O_RDWR | O_NONBLOCK);
488 if (evdev_configure_device(device) == -1)
491 /* If the dispatch was not set up use the fallback. */
492 if (device->dispatch == NULL)
493 device->dispatch = fallback_dispatch_create();
494 if (device->dispatch == NULL)
499 device->mtdev = mtdev_new_open(device->fd);
501 weston_log("mtdev failed to open for %s\n", path);
504 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
506 evdev_input_device_data, device);
507 if (device->source == NULL)
510 wl_list_insert(master->devices_list.prev, &device->link);
515 device->dispatch->interface->destroy(device->dispatch);
519 free(device->devnode);
524 static const char default_seat[] = "seat0";
527 device_added(struct udev_device *udev_device, struct evdev_seat *master)
529 struct weston_compositor *c;
531 const char *device_seat;
533 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
535 device_seat = default_seat;
537 if (strcmp(device_seat, master->seat_id))
540 c = master->base.compositor;
541 devnode = udev_device_get_devnode(udev_device);
542 evdev_input_device_create(master, c->wl_display, devnode);
546 device_removed(struct evdev_input_device *device)
548 struct evdev_dispatch *dispatch;
550 dispatch = device->dispatch;
552 dispatch->interface->destroy(dispatch);
554 wl_event_source_remove(device->source);
555 wl_list_remove(&device->link);
557 mtdev_close_delete(device->mtdev);
559 free(device->devnode);
564 evdev_notify_keyboard_focus(struct evdev_seat *seat)
566 struct evdev_input_device *device;
567 struct wl_array keys;
569 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
573 memset(all_keys, 0, sizeof all_keys);
574 wl_list_for_each(device, &seat->devices_list, link) {
575 memset(evdev_keys, 0, sizeof evdev_keys);
576 ret = ioctl(device->fd,
577 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
579 weston_log("failed to get keys for device %s\n",
583 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
584 all_keys[i] |= evdev_keys[i];
587 wl_array_init(&keys);
588 for (i = 0; i < KEY_CNT; i++) {
589 set = all_keys[i >> 3] & (1 << (i & 7));
591 k = wl_array_add(&keys, sizeof *k);
596 notify_keyboard_focus(&seat->base.seat, &keys);
598 wl_array_release(&keys);
602 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
604 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
605 struct udev_enumerate *e;
606 struct udev_list_entry *entry;
607 struct udev_device *device;
608 const char *path, *sysname;
610 e = udev_enumerate_new(udev);
611 udev_enumerate_add_match_subsystem(e, "input");
612 udev_enumerate_scan_devices(e);
613 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
614 path = udev_list_entry_get_name(entry);
615 device = udev_device_new_from_syspath(udev, path);
617 sysname = udev_device_get_sysname(device);
618 if (strncmp("event", sysname, 5) != 0) {
619 udev_device_unref(device);
623 device_added(device, seat);
625 udev_device_unref(device);
627 udev_enumerate_unref(e);
629 evdev_notify_keyboard_focus(seat);
631 if (wl_list_empty(&seat->devices_list)) {
633 "warning: no input devices on entering Weston. "
635 "\t- no permissions to read /dev/input/event*\n"
636 "\t- seats misconfigured "
637 "(Weston backend option 'seat', "
638 "udev device property ID_SEAT)\n");
643 evdev_udev_handler(int fd, uint32_t mask, void *data)
645 struct evdev_seat *master = data;
646 struct udev_device *udev_device;
647 struct evdev_input_device *device, *next;
651 udev_device = udev_monitor_receive_device(master->udev_monitor);
655 action = udev_device_get_action(udev_device);
657 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
660 if (!strcmp(action, "add")) {
661 device_added(udev_device, master);
663 else if (!strcmp(action, "remove")) {
664 devnode = udev_device_get_devnode(udev_device);
665 wl_list_for_each_safe(device, next,
666 &master->devices_list, link)
667 if (!strcmp(device->devnode, devnode)) {
668 device_removed(device);
673 udev_device_unref(udev_device);
679 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
681 struct evdev_seat *master = (struct evdev_seat *) seat_base;
682 struct wl_event_loop *loop;
683 struct weston_compositor *c = master->base.compositor;
686 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
687 if (!master->udev_monitor) {
688 weston_log("udev: failed to create the udev monitor\n");
692 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
695 if (udev_monitor_enable_receiving(master->udev_monitor)) {
696 weston_log("udev: failed to bind the udev monitor\n");
697 udev_monitor_unref(master->udev_monitor);
701 loop = wl_display_get_event_loop(c->wl_display);
702 fd = udev_monitor_get_fd(master->udev_monitor);
703 master->udev_monitor_source =
704 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
705 evdev_udev_handler, master);
706 if (!master->udev_monitor_source) {
707 udev_monitor_unref(master->udev_monitor);
715 evdev_disable_udev_monitor(struct weston_seat *seat_base)
717 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
719 if (!seat->udev_monitor)
722 udev_monitor_unref(seat->udev_monitor);
723 seat->udev_monitor = NULL;
724 wl_event_source_remove(seat->udev_monitor_source);
725 seat->udev_monitor_source = NULL;
729 evdev_input_create(struct weston_compositor *c, struct udev *udev,
732 struct evdev_seat *seat;
734 seat = malloc(sizeof *seat);
738 memset(seat, 0, sizeof *seat);
739 weston_seat_init(&seat->base, c);
740 seat->base.led_update = evdev_led_update;
742 wl_list_init(&seat->devices_list);
743 seat->seat_id = strdup(seat_id);
744 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
750 evdev_add_devices(udev, &seat->base);
752 c->seat = &seat->base;
756 evdev_remove_devices(struct weston_seat *seat_base)
758 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
759 struct evdev_input_device *device, *next;
761 wl_list_for_each_safe(device, next, &seat->devices_list, link)
762 device_removed(device);
764 notify_keyboard_focus(&seat->base.seat, NULL);
768 evdev_input_destroy(struct weston_seat *seat_base)
770 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
772 evdev_remove_devices(seat_base);
773 evdev_disable_udev_monitor(&seat->base);
775 wl_list_remove(&seat->base.link);