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,
92 STATE_UPDATE_AUTOMATIC);
98 evdev_process_touch(struct evdev_input_device *device,
99 struct input_event *e)
101 const int screen_width = device->output->current->width;
102 const int screen_height = device->output->current->height;
106 device->mt.slot = e->value;
108 case ABS_MT_TRACKING_ID:
110 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
112 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
114 case ABS_MT_POSITION_X:
115 device->mt.x[device->mt.slot] =
116 (e->value - device->abs.min_x) * screen_width /
117 (device->abs.max_x - device->abs.min_x) +
119 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
121 case ABS_MT_POSITION_Y:
122 device->mt.y[device->mt.slot] =
123 (e->value - device->abs.min_y) * screen_height /
124 (device->abs.max_y - device->abs.min_y) +
126 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
132 evdev_process_absolute_motion(struct evdev_input_device *device,
133 struct input_event *e)
135 const int screen_width = device->output->current->width;
136 const int screen_height = device->output->current->height;
141 (e->value - device->abs.min_x) * screen_width /
142 (device->abs.max_x - device->abs.min_x) +
144 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
148 (e->value - device->abs.min_y) * screen_height /
149 (device->abs.max_y - device->abs.min_y) +
151 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
157 evdev_process_relative(struct evdev_input_device *device,
158 struct input_event *e, uint32_t time)
162 device->rel.dx += wl_fixed_from_int(e->value);
163 device->pending_events |= EVDEV_RELATIVE_MOTION;
166 device->rel.dy += wl_fixed_from_int(e->value);
167 device->pending_events |= EVDEV_RELATIVE_MOTION;
170 notify_axis(&device->master->base.seat,
172 WL_POINTER_AXIS_VERTICAL_SCROLL,
173 wl_fixed_from_int(e->value));
176 notify_axis(&device->master->base.seat,
178 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
179 wl_fixed_from_int(e->value));
185 evdev_process_absolute(struct evdev_input_device *device,
186 struct input_event *e)
189 evdev_process_touch(device, e);
191 evdev_process_absolute_motion(device, e);
196 is_motion_event(struct input_event *e)
209 case ABS_MT_POSITION_X:
210 case ABS_MT_POSITION_Y:
219 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
221 struct weston_seat *master = &device->master->base;
223 if (!device->pending_events)
226 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
227 notify_motion(&master->seat, time,
228 master->seat.pointer->x + device->rel.dx,
229 master->seat.pointer->y + device->rel.dy);
230 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
234 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
235 notify_touch(&master->seat, time,
237 wl_fixed_from_int(device->mt.x[device->mt.slot]),
238 wl_fixed_from_int(device->mt.y[device->mt.slot]),
240 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
241 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
243 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
244 notify_touch(&master->seat, time,
246 wl_fixed_from_int(device->mt.x[device->mt.slot]),
247 wl_fixed_from_int(device->mt.y[device->mt.slot]),
249 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
250 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
252 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
253 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
255 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
257 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
258 notify_motion(&master->seat, time,
259 wl_fixed_from_int(device->abs.x),
260 wl_fixed_from_int(device->abs.y));
261 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
266 fallback_process(struct evdev_dispatch *dispatch,
267 struct evdev_input_device *device,
268 struct input_event *event,
271 switch (event->type) {
273 evdev_process_relative(device, event, time);
276 evdev_process_absolute(device, event);
279 evdev_process_key(device, event, time);
285 fallback_destroy(struct evdev_dispatch *dispatch)
290 struct evdev_dispatch_interface fallback_interface = {
295 static struct evdev_dispatch *
296 fallback_dispatch_create(void)
298 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
299 if (dispatch == NULL)
302 dispatch->interface = &fallback_interface;
308 evdev_process_events(struct evdev_input_device *device,
309 struct input_event *ev, int count)
311 struct evdev_dispatch *dispatch = device->dispatch;
312 struct input_event *e, *end;
315 device->pending_events = 0;
319 for (e = ev; e < end; e++) {
320 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
322 /* we try to minimize the amount of notifications to be
323 * forwarded to the compositor, so we accumulate motion
324 * events and send as a bunch */
325 if (!is_motion_event(e))
326 evdev_flush_motion(device, time);
328 dispatch->interface->process(dispatch, device, e, time);
331 evdev_flush_motion(device, time);
335 evdev_input_device_data(int fd, uint32_t mask, void *data)
337 struct weston_compositor *ec;
338 struct evdev_input_device *device = data;
339 struct input_event ev[32];
342 ec = device->master->base.compositor;
346 /* If the compositor is repainting, this function is called only once
347 * per frame and we have to process all the events available on the
348 * fd, otherwise there will be input lag. */
351 len = mtdev_get(device->mtdev, fd, ev,
353 sizeof (struct input_event);
355 len = read(fd, &ev, sizeof ev);
357 if (len < 0 || len % sizeof ev[0] != 0) {
358 /* FIXME: call device_removed when errno is ENODEV. */
362 evdev_process_events(device, ev, len / sizeof ev[0]);
370 evdev_configure_device(struct evdev_input_device *device)
372 struct input_absinfo absinfo;
373 unsigned long ev_bits[NBITS(EV_MAX)];
374 unsigned long abs_bits[NBITS(ABS_MAX)];
375 unsigned long rel_bits[NBITS(REL_MAX)];
376 unsigned long key_bits[NBITS(KEY_MAX)];
377 int has_key, has_abs;
384 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
385 if (TEST_BIT(ev_bits, EV_ABS)) {
388 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
390 if (TEST_BIT(abs_bits, ABS_X)) {
391 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
392 device->abs.min_x = absinfo.minimum;
393 device->abs.max_x = absinfo.maximum;
394 device->caps |= EVDEV_MOTION_ABS;
396 if (TEST_BIT(abs_bits, ABS_Y)) {
397 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
398 device->abs.min_y = absinfo.minimum;
399 device->abs.max_y = absinfo.maximum;
400 device->caps |= EVDEV_MOTION_ABS;
402 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
405 device->caps |= EVDEV_TOUCH;
408 if (TEST_BIT(ev_bits, EV_REL)) {
409 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
411 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
412 device->caps |= EVDEV_MOTION_REL;
414 if (TEST_BIT(ev_bits, EV_KEY)) {
416 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
418 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
419 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
421 device->dispatch = evdev_touchpad_create(device);
422 for (i = KEY_ESC; i < KEY_MAX; i++) {
423 if (i >= BTN_MISC && i < KEY_OK)
425 if (TEST_BIT(key_bits, i)) {
426 device->caps |= EVDEV_KEYBOARD;
430 for (i = BTN_MISC; i < KEY_OK; i++) {
431 if (TEST_BIT(key_bits, i)) {
432 device->caps |= EVDEV_BUTTON;
437 if (TEST_BIT(ev_bits, EV_LED)) {
438 device->caps |= EVDEV_KEYBOARD;
441 /* This rule tries to catch accelerometer devices and opt out. We may
442 * want to adjust the protocol later adding a proper event for dealing
443 * with accelerometers and implement here accordingly */
444 if (has_abs && !has_key)
448 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
449 weston_seat_init_pointer(&device->master->base);
450 if ((device->caps & EVDEV_KEYBOARD))
451 weston_seat_init_keyboard(&device->master->base, NULL);
452 if ((device->caps & EVDEV_TOUCH))
453 weston_seat_init_touch(&device->master->base);
458 static struct evdev_input_device *
459 evdev_input_device_create(struct evdev_seat *master,
460 struct wl_display *display, const char *path)
462 struct evdev_input_device *device;
463 struct weston_compositor *ec;
465 device = malloc(sizeof *device);
469 ec = master->base.compositor;
471 container_of(ec->output_list.next, struct weston_output, link);
473 device->master = master;
475 device->mtdev = NULL;
476 device->devnode = strdup(path);
477 device->mt.slot = -1;
480 device->dispatch = NULL;
482 /* Use non-blocking mode so that we can loop on read on
483 * evdev_input_device_data() until all events on the fd are
484 * read. mtdev_get() also expects this. */
485 device->fd = weston_launcher_open(ec, path, O_RDWR | O_NONBLOCK);
489 if (evdev_configure_device(device) == -1)
492 /* If the dispatch was not set up use the fallback. */
493 if (device->dispatch == NULL)
494 device->dispatch = fallback_dispatch_create();
495 if (device->dispatch == NULL)
500 device->mtdev = mtdev_new_open(device->fd);
502 weston_log("mtdev failed to open for %s\n", path);
505 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
507 evdev_input_device_data, device);
508 if (device->source == NULL)
511 wl_list_insert(master->devices_list.prev, &device->link);
516 device->dispatch->interface->destroy(device->dispatch);
520 free(device->devnode);
525 static const char default_seat[] = "seat0";
528 device_added(struct udev_device *udev_device, struct evdev_seat *master)
530 struct weston_compositor *c;
532 const char *device_seat;
534 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
536 device_seat = default_seat;
538 if (strcmp(device_seat, master->seat_id))
541 c = master->base.compositor;
542 devnode = udev_device_get_devnode(udev_device);
543 evdev_input_device_create(master, c->wl_display, devnode);
547 device_removed(struct evdev_input_device *device)
549 struct evdev_dispatch *dispatch;
551 dispatch = device->dispatch;
553 dispatch->interface->destroy(dispatch);
555 wl_event_source_remove(device->source);
556 wl_list_remove(&device->link);
558 mtdev_close_delete(device->mtdev);
560 free(device->devnode);
565 evdev_notify_keyboard_focus(struct evdev_seat *seat)
567 struct evdev_input_device *device;
568 struct wl_array keys;
570 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
574 memset(all_keys, 0, sizeof all_keys);
575 wl_list_for_each(device, &seat->devices_list, link) {
576 memset(evdev_keys, 0, sizeof evdev_keys);
577 ret = ioctl(device->fd,
578 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
580 weston_log("failed to get keys for device %s\n",
584 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
585 all_keys[i] |= evdev_keys[i];
588 wl_array_init(&keys);
589 for (i = 0; i < KEY_CNT; i++) {
590 set = all_keys[i >> 3] & (1 << (i & 7));
592 k = wl_array_add(&keys, sizeof *k);
597 notify_keyboard_focus_in(&seat->base.seat, &keys,
598 STATE_UPDATE_AUTOMATIC);
600 wl_array_release(&keys);
604 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
606 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
607 struct udev_enumerate *e;
608 struct udev_list_entry *entry;
609 struct udev_device *device;
610 const char *path, *sysname;
612 e = udev_enumerate_new(udev);
613 udev_enumerate_add_match_subsystem(e, "input");
614 udev_enumerate_scan_devices(e);
615 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
616 path = udev_list_entry_get_name(entry);
617 device = udev_device_new_from_syspath(udev, path);
619 sysname = udev_device_get_sysname(device);
620 if (strncmp("event", sysname, 5) != 0) {
621 udev_device_unref(device);
625 device_added(device, seat);
627 udev_device_unref(device);
629 udev_enumerate_unref(e);
631 evdev_notify_keyboard_focus(seat);
633 if (wl_list_empty(&seat->devices_list)) {
635 "warning: no input devices on entering Weston. "
637 "\t- no permissions to read /dev/input/event*\n"
638 "\t- seats misconfigured "
639 "(Weston backend option 'seat', "
640 "udev device property ID_SEAT)\n");
645 evdev_udev_handler(int fd, uint32_t mask, void *data)
647 struct evdev_seat *master = data;
648 struct udev_device *udev_device;
649 struct evdev_input_device *device, *next;
653 udev_device = udev_monitor_receive_device(master->udev_monitor);
657 action = udev_device_get_action(udev_device);
659 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
662 if (!strcmp(action, "add")) {
663 device_added(udev_device, master);
665 else if (!strcmp(action, "remove")) {
666 devnode = udev_device_get_devnode(udev_device);
667 wl_list_for_each_safe(device, next,
668 &master->devices_list, link)
669 if (!strcmp(device->devnode, devnode)) {
670 device_removed(device);
675 udev_device_unref(udev_device);
681 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
683 struct evdev_seat *master = (struct evdev_seat *) seat_base;
684 struct wl_event_loop *loop;
685 struct weston_compositor *c = master->base.compositor;
688 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
689 if (!master->udev_monitor) {
690 weston_log("udev: failed to create the udev monitor\n");
694 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
697 if (udev_monitor_enable_receiving(master->udev_monitor)) {
698 weston_log("udev: failed to bind the udev monitor\n");
699 udev_monitor_unref(master->udev_monitor);
703 loop = wl_display_get_event_loop(c->wl_display);
704 fd = udev_monitor_get_fd(master->udev_monitor);
705 master->udev_monitor_source =
706 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
707 evdev_udev_handler, master);
708 if (!master->udev_monitor_source) {
709 udev_monitor_unref(master->udev_monitor);
717 evdev_disable_udev_monitor(struct weston_seat *seat_base)
719 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
721 if (!seat->udev_monitor)
724 udev_monitor_unref(seat->udev_monitor);
725 seat->udev_monitor = NULL;
726 wl_event_source_remove(seat->udev_monitor_source);
727 seat->udev_monitor_source = NULL;
731 evdev_input_create(struct weston_compositor *c, struct udev *udev,
734 struct evdev_seat *seat;
736 seat = malloc(sizeof *seat);
740 memset(seat, 0, sizeof *seat);
741 weston_seat_init(&seat->base, c);
742 seat->base.led_update = evdev_led_update;
744 wl_list_init(&seat->devices_list);
745 seat->seat_id = strdup(seat_id);
746 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
752 evdev_add_devices(udev, &seat->base);
754 c->seat = &seat->base;
758 evdev_remove_devices(struct weston_seat *seat_base)
760 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
761 struct evdev_input_device *device, *next;
763 wl_list_for_each_safe(device, next, &seat->devices_list, link)
764 device_removed(device);
766 notify_keyboard_focus_out(&seat->base.seat);
770 evdev_input_destroy(struct weston_seat *seat_base)
772 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
774 evdev_remove_devices(seat_base);
775 evdev_disable_udev_monitor(&seat->base);
777 wl_list_remove(&seat->base.link);