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 weston_seat *seat_base, 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_seat *seat = (struct evdev_seat *) seat_base;
46 struct evdev_input_device *device;
47 struct input_event ev[ARRAY_LENGTH(map)];
50 memset(ev, 0, sizeof(ev));
51 for (i = 0; i < ARRAY_LENGTH(map); i++) {
53 ev[i].code = map[i].evdev;
54 ev[i].value = !!(leds & map[i].weston);
57 wl_list_for_each(device, &seat->devices_list, link) {
58 if (device->caps & EVDEV_KEYBOARD)
59 write(device->fd, ev, sizeof ev);
64 evdev_process_key(struct evdev_input_device *device,
65 struct input_event *e, int time)
79 notify_button(&device->master->base.seat,
81 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
82 WL_POINTER_BUTTON_STATE_RELEASED);
86 notify_key(&device->master->base.seat,
88 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
89 WL_KEYBOARD_KEY_STATE_RELEASED,
90 STATE_UPDATE_AUTOMATIC);
96 evdev_process_touch(struct evdev_input_device *device,
97 struct input_event *e)
99 const int screen_width = device->output->current->width;
100 const int screen_height = device->output->current->height;
104 device->mt.slot = e->value;
106 case ABS_MT_TRACKING_ID:
108 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
110 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
112 case ABS_MT_POSITION_X:
113 device->mt.x[device->mt.slot] =
114 (e->value - device->abs.min_x) * screen_width /
115 (device->abs.max_x - device->abs.min_x) +
117 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
119 case ABS_MT_POSITION_Y:
120 device->mt.y[device->mt.slot] =
121 (e->value - device->abs.min_y) * screen_height /
122 (device->abs.max_y - device->abs.min_y) +
124 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
130 evdev_process_absolute_motion(struct evdev_input_device *device,
131 struct input_event *e)
133 const int screen_width = device->output->current->width;
134 const int screen_height = device->output->current->height;
139 (e->value - device->abs.min_x) * screen_width /
140 (device->abs.max_x - device->abs.min_x) +
142 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
146 (e->value - device->abs.min_y) * screen_height /
147 (device->abs.max_y - device->abs.min_y) +
149 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
155 evdev_process_relative(struct evdev_input_device *device,
156 struct input_event *e, uint32_t time)
160 device->rel.dx += wl_fixed_from_int(e->value);
161 device->pending_events |= EVDEV_RELATIVE_MOTION;
164 device->rel.dy += wl_fixed_from_int(e->value);
165 device->pending_events |= EVDEV_RELATIVE_MOTION;
168 notify_axis(&device->master->base.seat,
170 WL_POINTER_AXIS_VERTICAL_SCROLL,
171 wl_fixed_from_int(e->value));
174 notify_axis(&device->master->base.seat,
176 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
177 wl_fixed_from_int(e->value));
183 evdev_process_absolute(struct evdev_input_device *device,
184 struct input_event *e)
187 evdev_process_touch(device, e);
189 evdev_process_absolute_motion(device, e);
194 is_motion_event(struct input_event *e)
207 case ABS_MT_POSITION_X:
208 case ABS_MT_POSITION_Y:
217 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
219 struct weston_seat *master = &device->master->base;
221 if (!device->pending_events)
224 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
225 notify_motion(&master->seat, time,
226 master->seat.pointer->x + device->rel.dx,
227 master->seat.pointer->y + device->rel.dy);
228 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
232 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
233 notify_touch(&master->seat, time,
235 wl_fixed_from_int(device->mt.x[device->mt.slot]),
236 wl_fixed_from_int(device->mt.y[device->mt.slot]),
238 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
239 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
241 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
242 notify_touch(&master->seat, time,
244 wl_fixed_from_int(device->mt.x[device->mt.slot]),
245 wl_fixed_from_int(device->mt.y[device->mt.slot]),
247 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
248 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
250 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
251 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
253 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
255 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
256 notify_motion(&master->seat, time,
257 wl_fixed_from_int(device->abs.x),
258 wl_fixed_from_int(device->abs.y));
259 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
264 fallback_process(struct evdev_dispatch *dispatch,
265 struct evdev_input_device *device,
266 struct input_event *event,
269 switch (event->type) {
271 evdev_process_relative(device, event, time);
274 evdev_process_absolute(device, event);
277 evdev_process_key(device, event, time);
283 fallback_destroy(struct evdev_dispatch *dispatch)
288 struct evdev_dispatch_interface fallback_interface = {
293 static struct evdev_dispatch *
294 fallback_dispatch_create(void)
296 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
297 if (dispatch == NULL)
300 dispatch->interface = &fallback_interface;
306 evdev_process_events(struct evdev_input_device *device,
307 struct input_event *ev, int count)
309 struct evdev_dispatch *dispatch = device->dispatch;
310 struct input_event *e, *end;
313 device->pending_events = 0;
317 for (e = ev; e < end; e++) {
318 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
320 /* we try to minimize the amount of notifications to be
321 * forwarded to the compositor, so we accumulate motion
322 * events and send as a bunch */
323 if (!is_motion_event(e))
324 evdev_flush_motion(device, time);
326 dispatch->interface->process(dispatch, device, e, time);
329 evdev_flush_motion(device, time);
333 evdev_input_device_data(int fd, uint32_t mask, void *data)
335 struct weston_compositor *ec;
336 struct evdev_input_device *device = data;
337 struct input_event ev[32];
340 ec = device->master->base.compositor;
344 /* If the compositor is repainting, this function is called only once
345 * per frame and we have to process all the events available on the
346 * fd, otherwise there will be input lag. */
349 len = mtdev_get(device->mtdev, fd, ev,
351 sizeof (struct input_event);
353 len = read(fd, &ev, sizeof ev);
355 if (len < 0 || len % sizeof ev[0] != 0) {
356 /* FIXME: call evdev_input_device_destroy when errno is ENODEV. */
360 evdev_process_events(device, ev, len / sizeof ev[0]);
368 evdev_configure_device(struct evdev_input_device *device)
370 struct input_absinfo absinfo;
371 unsigned long ev_bits[NBITS(EV_MAX)];
372 unsigned long abs_bits[NBITS(ABS_MAX)];
373 unsigned long rel_bits[NBITS(REL_MAX)];
374 unsigned long key_bits[NBITS(KEY_MAX)];
375 int has_key, has_abs;
382 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
383 if (TEST_BIT(ev_bits, EV_ABS)) {
386 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
388 if (TEST_BIT(abs_bits, ABS_X)) {
389 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
390 device->abs.min_x = absinfo.minimum;
391 device->abs.max_x = absinfo.maximum;
392 device->caps |= EVDEV_MOTION_ABS;
394 if (TEST_BIT(abs_bits, ABS_Y)) {
395 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
396 device->abs.min_y = absinfo.minimum;
397 device->abs.max_y = absinfo.maximum;
398 device->caps |= EVDEV_MOTION_ABS;
400 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
401 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
403 device->abs.min_x = absinfo.minimum;
404 device->abs.max_x = absinfo.maximum;
405 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
407 device->abs.min_y = absinfo.minimum;
408 device->abs.max_y = absinfo.maximum;
411 device->caps |= EVDEV_TOUCH;
414 if (TEST_BIT(ev_bits, EV_REL)) {
415 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
417 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
418 device->caps |= EVDEV_MOTION_REL;
420 if (TEST_BIT(ev_bits, EV_KEY)) {
422 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
424 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
425 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
427 device->dispatch = evdev_touchpad_create(device);
428 for (i = KEY_ESC; i < KEY_MAX; i++) {
429 if (i >= BTN_MISC && i < KEY_OK)
431 if (TEST_BIT(key_bits, i)) {
432 device->caps |= EVDEV_KEYBOARD;
436 for (i = BTN_MISC; i < KEY_OK; i++) {
437 if (TEST_BIT(key_bits, i)) {
438 device->caps |= EVDEV_BUTTON;
443 if (TEST_BIT(ev_bits, EV_LED)) {
444 device->caps |= EVDEV_KEYBOARD;
447 /* This rule tries to catch accelerometer devices and opt out. We may
448 * want to adjust the protocol later adding a proper event for dealing
449 * with accelerometers and implement here accordingly */
450 if (has_abs && !has_key && !device->is_mt)
454 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
455 weston_seat_init_pointer(&device->master->base);
456 if ((device->caps & EVDEV_KEYBOARD))
457 weston_seat_init_keyboard(&device->master->base, NULL);
458 if ((device->caps & EVDEV_TOUCH))
459 weston_seat_init_touch(&device->master->base);
464 static struct evdev_input_device *
465 evdev_input_device_create(struct evdev_seat *master,
466 const char *path, int device_fd)
468 struct evdev_input_device *device;
469 struct weston_compositor *ec;
471 device = malloc(sizeof *device);
475 ec = master->base.compositor;
477 container_of(ec->output_list.next, struct weston_output, link);
479 device->master = master;
481 device->mtdev = NULL;
482 device->devnode = strdup(path);
483 device->mt.slot = -1;
486 device->dispatch = NULL;
487 device->fd = device_fd;
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);
518 free(device->devnode);
524 evdev_input_device_destroy(struct evdev_input_device *device)
526 struct evdev_dispatch *dispatch;
528 dispatch = device->dispatch;
530 dispatch->interface->destroy(dispatch);
532 wl_event_source_remove(device->source);
533 wl_list_remove(&device->link);
535 mtdev_close_delete(device->mtdev);
537 free(device->devnode);
541 static const char default_seat[] = "seat0";
544 device_added(struct udev_device *udev_device, struct evdev_seat *master)
546 struct weston_compositor *c;
547 struct evdev_input_device *device;
549 const char *device_seat;
552 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
554 device_seat = default_seat;
556 if (strcmp(device_seat, master->seat_id))
559 c = master->base.compositor;
560 devnode = udev_device_get_devnode(udev_device);
562 /* Use non-blocking mode so that we can loop on read on
563 * evdev_input_device_data() until all events on the fd are
564 * read. mtdev_get() also expects this. */
565 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
567 weston_log("opening input device '%s' failed.\n", devnode);
571 device = evdev_input_device_create(master, devnode, fd);
574 weston_log("not using input device '%s'.\n", devnode);
579 evdev_notify_keyboard_focus(struct evdev_seat *seat)
581 struct evdev_input_device *device;
582 struct wl_array keys;
584 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
588 memset(all_keys, 0, sizeof all_keys);
589 wl_list_for_each(device, &seat->devices_list, link) {
590 memset(evdev_keys, 0, sizeof evdev_keys);
591 ret = ioctl(device->fd,
592 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
594 weston_log("failed to get keys for device %s\n",
598 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
599 all_keys[i] |= evdev_keys[i];
602 wl_array_init(&keys);
603 for (i = 0; i < KEY_CNT; i++) {
604 set = all_keys[i >> 3] & (1 << (i & 7));
606 k = wl_array_add(&keys, sizeof *k);
611 notify_keyboard_focus_in(&seat->base.seat, &keys,
612 STATE_UPDATE_AUTOMATIC);
614 wl_array_release(&keys);
618 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
620 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
621 struct udev_enumerate *e;
622 struct udev_list_entry *entry;
623 struct udev_device *device;
624 const char *path, *sysname;
626 e = udev_enumerate_new(udev);
627 udev_enumerate_add_match_subsystem(e, "input");
628 udev_enumerate_scan_devices(e);
629 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
630 path = udev_list_entry_get_name(entry);
631 device = udev_device_new_from_syspath(udev, path);
633 sysname = udev_device_get_sysname(device);
634 if (strncmp("event", sysname, 5) != 0) {
635 udev_device_unref(device);
639 device_added(device, seat);
641 udev_device_unref(device);
643 udev_enumerate_unref(e);
645 evdev_notify_keyboard_focus(seat);
647 if (wl_list_empty(&seat->devices_list)) {
649 "warning: no input devices on entering Weston. "
651 "\t- no permissions to read /dev/input/event*\n"
652 "\t- seats misconfigured "
653 "(Weston backend option 'seat', "
654 "udev device property ID_SEAT)\n");
659 evdev_udev_handler(int fd, uint32_t mask, void *data)
661 struct evdev_seat *master = data;
662 struct udev_device *udev_device;
663 struct evdev_input_device *device, *next;
667 udev_device = udev_monitor_receive_device(master->udev_monitor);
671 action = udev_device_get_action(udev_device);
673 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
676 if (!strcmp(action, "add")) {
677 device_added(udev_device, master);
679 else if (!strcmp(action, "remove")) {
680 devnode = udev_device_get_devnode(udev_device);
681 wl_list_for_each_safe(device, next,
682 &master->devices_list, link)
683 if (!strcmp(device->devnode, devnode)) {
684 evdev_input_device_destroy(device);
689 udev_device_unref(udev_device);
695 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
697 struct evdev_seat *master = (struct evdev_seat *) seat_base;
698 struct wl_event_loop *loop;
699 struct weston_compositor *c = master->base.compositor;
702 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
703 if (!master->udev_monitor) {
704 weston_log("udev: failed to create the udev monitor\n");
708 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
711 if (udev_monitor_enable_receiving(master->udev_monitor)) {
712 weston_log("udev: failed to bind the udev monitor\n");
713 udev_monitor_unref(master->udev_monitor);
717 loop = wl_display_get_event_loop(c->wl_display);
718 fd = udev_monitor_get_fd(master->udev_monitor);
719 master->udev_monitor_source =
720 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
721 evdev_udev_handler, master);
722 if (!master->udev_monitor_source) {
723 udev_monitor_unref(master->udev_monitor);
731 evdev_disable_udev_monitor(struct weston_seat *seat_base)
733 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
735 if (!seat->udev_monitor)
738 udev_monitor_unref(seat->udev_monitor);
739 seat->udev_monitor = NULL;
740 wl_event_source_remove(seat->udev_monitor_source);
741 seat->udev_monitor_source = NULL;
745 evdev_input_create(struct weston_compositor *c, struct udev *udev,
748 struct evdev_seat *seat;
750 seat = malloc(sizeof *seat);
754 memset(seat, 0, sizeof *seat);
755 weston_seat_init(&seat->base, c);
756 seat->base.led_update = evdev_led_update;
758 wl_list_init(&seat->devices_list);
759 seat->seat_id = strdup(seat_id);
760 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
766 evdev_add_devices(udev, &seat->base);
768 c->seat = &seat->base;
772 evdev_remove_devices(struct weston_seat *seat_base)
774 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
775 struct evdev_input_device *device, *next;
777 wl_list_for_each_safe(device, next, &seat->devices_list, link)
778 evdev_input_device_destroy(device);
780 notify_keyboard_focus_out(&seat->base.seat);
784 evdev_input_destroy(struct weston_seat *seat_base)
786 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
788 evdev_remove_devices(seat_base);
789 evdev_disable_udev_monitor(&seat->base);
791 weston_seat_release(seat_base);