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 "launcher-util.h"
35 evdev_led_update(struct wl_list *evdev_devices, enum weston_led leds)
38 enum weston_led weston;
41 { LED_NUM_LOCK, LED_NUML },
42 { LED_CAPS_LOCK, LED_CAPSL },
43 { LED_SCROLL_LOCK, LED_SCROLLL },
45 struct evdev_input_device *device;
46 struct input_event ev[ARRAY_LENGTH(map)];
49 memset(ev, 0, sizeof(ev));
50 for (i = 0; i < ARRAY_LENGTH(map); i++) {
52 ev[i].code = map[i].evdev;
53 ev[i].value = !!(leds & map[i].weston);
56 wl_list_for_each(device, evdev_devices, link) {
57 if (device->caps & EVDEV_KEYBOARD)
58 write(device->fd, ev, sizeof ev);
63 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
65 struct drm_seat *seat = (struct drm_seat *) seat_base;
67 evdev_led_update(&seat->devices_list, leds);
71 evdev_process_key(struct evdev_input_device *device,
72 struct input_event *e, int time)
86 notify_button(&device->seat->seat,
88 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
89 WL_POINTER_BUTTON_STATE_RELEASED);
93 notify_key(&device->seat->seat,
95 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
96 WL_KEYBOARD_KEY_STATE_RELEASED,
97 STATE_UPDATE_AUTOMATIC);
103 evdev_process_touch(struct evdev_input_device *device,
104 struct input_event *e)
106 const int screen_width = device->output->current->width;
107 const int screen_height = device->output->current->height;
111 device->mt.slot = e->value;
113 case ABS_MT_TRACKING_ID:
115 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
117 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
119 case ABS_MT_POSITION_X:
120 device->mt.x[device->mt.slot] =
121 (e->value - device->abs.min_x) * screen_width /
122 (device->abs.max_x - device->abs.min_x) +
124 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
126 case ABS_MT_POSITION_Y:
127 device->mt.y[device->mt.slot] =
128 (e->value - device->abs.min_y) * screen_height /
129 (device->abs.max_y - device->abs.min_y) +
131 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
137 evdev_process_absolute_motion(struct evdev_input_device *device,
138 struct input_event *e)
140 const int screen_width = device->output->current->width;
141 const int screen_height = device->output->current->height;
146 (e->value - device->abs.min_x) * screen_width /
147 (device->abs.max_x - device->abs.min_x) +
149 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
153 (e->value - device->abs.min_y) * screen_height /
154 (device->abs.max_y - device->abs.min_y) +
156 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
162 evdev_process_relative(struct evdev_input_device *device,
163 struct input_event *e, uint32_t time)
167 device->rel.dx += wl_fixed_from_int(e->value);
168 device->pending_events |= EVDEV_RELATIVE_MOTION;
171 device->rel.dy += wl_fixed_from_int(e->value);
172 device->pending_events |= EVDEV_RELATIVE_MOTION;
175 notify_axis(&device->seat->seat,
177 WL_POINTER_AXIS_VERTICAL_SCROLL,
178 wl_fixed_from_int(e->value));
181 notify_axis(&device->seat->seat,
183 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
184 wl_fixed_from_int(e->value));
190 evdev_process_absolute(struct evdev_input_device *device,
191 struct input_event *e)
194 evdev_process_touch(device, e);
196 evdev_process_absolute_motion(device, e);
201 is_motion_event(struct input_event *e)
214 case ABS_MT_POSITION_X:
215 case ABS_MT_POSITION_Y:
224 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
226 struct weston_seat *master = device->seat;
228 if (!device->pending_events)
231 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
232 notify_motion(&master->seat, time,
233 master->seat.pointer->x + device->rel.dx,
234 master->seat.pointer->y + device->rel.dy);
235 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
239 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
240 notify_touch(&master->seat, time,
242 wl_fixed_from_int(device->mt.x[device->mt.slot]),
243 wl_fixed_from_int(device->mt.y[device->mt.slot]),
245 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
246 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
248 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
249 notify_touch(&master->seat, time,
251 wl_fixed_from_int(device->mt.x[device->mt.slot]),
252 wl_fixed_from_int(device->mt.y[device->mt.slot]),
254 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
255 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
257 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
258 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
260 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
262 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
263 notify_motion(&master->seat, time,
264 wl_fixed_from_int(device->abs.x),
265 wl_fixed_from_int(device->abs.y));
266 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
271 fallback_process(struct evdev_dispatch *dispatch,
272 struct evdev_input_device *device,
273 struct input_event *event,
276 switch (event->type) {
278 evdev_process_relative(device, event, time);
281 evdev_process_absolute(device, event);
284 evdev_process_key(device, event, time);
290 fallback_destroy(struct evdev_dispatch *dispatch)
295 struct evdev_dispatch_interface fallback_interface = {
300 static struct evdev_dispatch *
301 fallback_dispatch_create(void)
303 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
304 if (dispatch == NULL)
307 dispatch->interface = &fallback_interface;
313 evdev_process_events(struct evdev_input_device *device,
314 struct input_event *ev, int count)
316 struct evdev_dispatch *dispatch = device->dispatch;
317 struct input_event *e, *end;
320 device->pending_events = 0;
324 for (e = ev; e < end; e++) {
325 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
327 /* we try to minimize the amount of notifications to be
328 * forwarded to the compositor, so we accumulate motion
329 * events and send as a bunch */
330 if (!is_motion_event(e))
331 evdev_flush_motion(device, time);
333 dispatch->interface->process(dispatch, device, e, time);
336 evdev_flush_motion(device, time);
340 evdev_input_device_data(int fd, uint32_t mask, void *data)
342 struct weston_compositor *ec;
343 struct evdev_input_device *device = data;
344 struct input_event ev[32];
347 ec = device->seat->compositor;
351 /* If the compositor is repainting, this function is called only once
352 * per frame and we have to process all the events available on the
353 * fd, otherwise there will be input lag. */
356 len = mtdev_get(device->mtdev, fd, ev,
358 sizeof (struct input_event);
360 len = read(fd, &ev, sizeof ev);
362 if (len < 0 || len % sizeof ev[0] != 0) {
363 /* FIXME: call evdev_input_device_destroy when errno is ENODEV. */
367 evdev_process_events(device, ev, len / sizeof ev[0]);
375 evdev_configure_device(struct evdev_input_device *device)
377 struct input_absinfo absinfo;
378 unsigned long ev_bits[NBITS(EV_MAX)];
379 unsigned long abs_bits[NBITS(ABS_MAX)];
380 unsigned long rel_bits[NBITS(REL_MAX)];
381 unsigned long key_bits[NBITS(KEY_MAX)];
382 int has_key, has_abs;
389 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
390 if (TEST_BIT(ev_bits, EV_ABS)) {
393 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
395 if (TEST_BIT(abs_bits, ABS_X)) {
396 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
397 device->abs.min_x = absinfo.minimum;
398 device->abs.max_x = absinfo.maximum;
399 device->caps |= EVDEV_MOTION_ABS;
401 if (TEST_BIT(abs_bits, ABS_Y)) {
402 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
403 device->abs.min_y = absinfo.minimum;
404 device->abs.max_y = absinfo.maximum;
405 device->caps |= EVDEV_MOTION_ABS;
407 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
408 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
410 device->abs.min_x = absinfo.minimum;
411 device->abs.max_x = absinfo.maximum;
412 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
414 device->abs.min_y = absinfo.minimum;
415 device->abs.max_y = absinfo.maximum;
418 device->caps |= EVDEV_TOUCH;
421 if (TEST_BIT(ev_bits, EV_REL)) {
422 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
424 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
425 device->caps |= EVDEV_MOTION_REL;
427 if (TEST_BIT(ev_bits, EV_KEY)) {
429 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
431 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
432 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
434 device->dispatch = evdev_touchpad_create(device);
435 for (i = KEY_ESC; i < KEY_MAX; i++) {
436 if (i >= BTN_MISC && i < KEY_OK)
438 if (TEST_BIT(key_bits, i)) {
439 device->caps |= EVDEV_KEYBOARD;
443 for (i = BTN_MISC; i < KEY_OK; i++) {
444 if (TEST_BIT(key_bits, i)) {
445 device->caps |= EVDEV_BUTTON;
450 if (TEST_BIT(ev_bits, EV_LED)) {
451 device->caps |= EVDEV_KEYBOARD;
454 /* This rule tries to catch accelerometer devices and opt out. We may
455 * want to adjust the protocol later adding a proper event for dealing
456 * with accelerometers and implement here accordingly */
457 if (has_abs && !has_key && !device->is_mt)
461 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
462 weston_seat_init_pointer(device->seat);
463 if ((device->caps & EVDEV_KEYBOARD))
464 weston_seat_init_keyboard(device->seat, NULL);
465 if ((device->caps & EVDEV_TOUCH))
466 weston_seat_init_touch(device->seat);
471 static struct evdev_input_device *
472 evdev_input_device_create(struct weston_seat *seat,
473 const char *path, int device_fd)
475 struct evdev_input_device *device;
476 struct weston_compositor *ec;
478 device = malloc(sizeof *device);
481 memset(device, 0, sizeof *device);
483 ec = seat->compositor;
485 container_of(ec->output_list.next, struct weston_output, link);
489 device->mtdev = NULL;
490 device->devnode = strdup(path);
491 device->mt.slot = -1;
494 device->dispatch = NULL;
495 device->fd = device_fd;
497 if (evdev_configure_device(device) == -1)
500 /* If the dispatch was not set up use the fallback. */
501 if (device->dispatch == NULL)
502 device->dispatch = fallback_dispatch_create();
503 if (device->dispatch == NULL)
508 device->mtdev = mtdev_new_open(device->fd);
510 weston_log("mtdev failed to open for %s\n", path);
513 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
515 evdev_input_device_data, device);
516 if (device->source == NULL)
522 device->dispatch->interface->destroy(device->dispatch);
524 free(device->devnode);
530 evdev_input_device_destroy(struct evdev_input_device *device)
532 struct evdev_dispatch *dispatch;
534 dispatch = device->dispatch;
536 dispatch->interface->destroy(dispatch);
538 wl_event_source_remove(device->source);
539 wl_list_remove(&device->link);
541 mtdev_close_delete(device->mtdev);
543 free(device->devnode);
547 static const char default_seat[] = "seat0";
550 device_added(struct udev_device *udev_device, struct drm_seat *master)
552 struct weston_compositor *c;
553 struct evdev_input_device *device;
555 const char *device_seat;
558 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
560 device_seat = default_seat;
562 if (strcmp(device_seat, master->seat_id))
565 c = master->base.compositor;
566 devnode = udev_device_get_devnode(udev_device);
568 /* Use non-blocking mode so that we can loop on read on
569 * evdev_input_device_data() until all events on the fd are
570 * read. mtdev_get() also expects this. */
571 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
573 weston_log("opening input device '%s' failed.\n", devnode);
577 device = evdev_input_device_create(&master->base, devnode, fd);
580 weston_log("not using input device '%s'.\n", devnode);
584 wl_list_insert(master->devices_list.prev, &device->link);
588 evdev_notify_keyboard_focus(struct weston_seat *seat,
589 struct wl_list *evdev_devices)
591 struct evdev_input_device *device;
592 struct wl_array keys;
594 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
598 memset(all_keys, 0, sizeof all_keys);
599 wl_list_for_each(device, evdev_devices, link) {
600 memset(evdev_keys, 0, sizeof evdev_keys);
601 ret = ioctl(device->fd,
602 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
604 weston_log("failed to get keys for device %s\n",
608 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
609 all_keys[i] |= evdev_keys[i];
612 wl_array_init(&keys);
613 for (i = 0; i < KEY_CNT; i++) {
614 set = all_keys[i >> 3] & (1 << (i & 7));
616 k = wl_array_add(&keys, sizeof *k);
621 notify_keyboard_focus_in(&seat->seat, &keys, STATE_UPDATE_AUTOMATIC);
623 wl_array_release(&keys);
627 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
629 struct drm_seat *seat = (struct drm_seat *) seat_base;
630 struct udev_enumerate *e;
631 struct udev_list_entry *entry;
632 struct udev_device *device;
633 const char *path, *sysname;
635 e = udev_enumerate_new(udev);
636 udev_enumerate_add_match_subsystem(e, "input");
637 udev_enumerate_scan_devices(e);
638 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
639 path = udev_list_entry_get_name(entry);
640 device = udev_device_new_from_syspath(udev, path);
642 sysname = udev_device_get_sysname(device);
643 if (strncmp("event", sysname, 5) != 0) {
644 udev_device_unref(device);
648 device_added(device, seat);
650 udev_device_unref(device);
652 udev_enumerate_unref(e);
654 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
656 if (wl_list_empty(&seat->devices_list)) {
658 "warning: no input devices on entering Weston. "
660 "\t- no permissions to read /dev/input/event*\n"
661 "\t- seats misconfigured "
662 "(Weston backend option 'seat', "
663 "udev device property ID_SEAT)\n");
668 evdev_udev_handler(int fd, uint32_t mask, void *data)
670 struct drm_seat *master = data;
671 struct udev_device *udev_device;
672 struct evdev_input_device *device, *next;
676 udev_device = udev_monitor_receive_device(master->udev_monitor);
680 action = udev_device_get_action(udev_device);
682 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
685 if (!strcmp(action, "add")) {
686 device_added(udev_device, master);
688 else if (!strcmp(action, "remove")) {
689 devnode = udev_device_get_devnode(udev_device);
690 wl_list_for_each_safe(device, next,
691 &master->devices_list, link)
692 if (!strcmp(device->devnode, devnode)) {
693 evdev_input_device_destroy(device);
698 udev_device_unref(udev_device);
704 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
706 struct drm_seat *master = (struct drm_seat *) seat_base;
707 struct wl_event_loop *loop;
708 struct weston_compositor *c = master->base.compositor;
711 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
712 if (!master->udev_monitor) {
713 weston_log("udev: failed to create the udev monitor\n");
717 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
720 if (udev_monitor_enable_receiving(master->udev_monitor)) {
721 weston_log("udev: failed to bind the udev monitor\n");
722 udev_monitor_unref(master->udev_monitor);
726 loop = wl_display_get_event_loop(c->wl_display);
727 fd = udev_monitor_get_fd(master->udev_monitor);
728 master->udev_monitor_source =
729 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
730 evdev_udev_handler, master);
731 if (!master->udev_monitor_source) {
732 udev_monitor_unref(master->udev_monitor);
740 evdev_disable_udev_monitor(struct weston_seat *seat_base)
742 struct drm_seat *seat = (struct drm_seat *) seat_base;
744 if (!seat->udev_monitor)
747 udev_monitor_unref(seat->udev_monitor);
748 seat->udev_monitor = NULL;
749 wl_event_source_remove(seat->udev_monitor_source);
750 seat->udev_monitor_source = NULL;
754 evdev_input_create(struct weston_compositor *c, struct udev *udev,
757 struct drm_seat *seat;
759 seat = malloc(sizeof *seat);
763 memset(seat, 0, sizeof *seat);
764 weston_seat_init(&seat->base, c);
765 seat->base.led_update = drm_led_update;
767 wl_list_init(&seat->devices_list);
768 seat->seat_id = strdup(seat_id);
769 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
775 evdev_add_devices(udev, &seat->base);
777 c->seat = &seat->base;
781 evdev_remove_devices(struct weston_seat *seat_base)
783 struct drm_seat *seat = (struct drm_seat *) seat_base;
784 struct evdev_input_device *device, *next;
786 wl_list_for_each_safe(device, next, &seat->devices_list, link)
787 evdev_input_device_destroy(device);
789 notify_keyboard_focus_out(&seat->base.seat);
793 evdev_input_destroy(struct weston_seat *seat_base)
795 struct drm_seat *seat = (struct drm_seat *) seat_base;
797 evdev_remove_devices(seat_base);
798 evdev_disable_udev_monitor(&seat->base);
800 weston_seat_release(seat_base);