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)) {
403 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
405 device->abs.min_x = absinfo.minimum;
406 device->abs.max_x = absinfo.maximum;
407 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
409 device->abs.min_y = absinfo.minimum;
410 device->abs.max_y = absinfo.maximum;
413 device->caps |= EVDEV_TOUCH;
416 if (TEST_BIT(ev_bits, EV_REL)) {
417 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
419 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
420 device->caps |= EVDEV_MOTION_REL;
422 if (TEST_BIT(ev_bits, EV_KEY)) {
424 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
426 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
427 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
429 device->dispatch = evdev_touchpad_create(device);
430 for (i = KEY_ESC; i < KEY_MAX; i++) {
431 if (i >= BTN_MISC && i < KEY_OK)
433 if (TEST_BIT(key_bits, i)) {
434 device->caps |= EVDEV_KEYBOARD;
438 for (i = BTN_MISC; i < KEY_OK; i++) {
439 if (TEST_BIT(key_bits, i)) {
440 device->caps |= EVDEV_BUTTON;
445 if (TEST_BIT(ev_bits, EV_LED)) {
446 device->caps |= EVDEV_KEYBOARD;
449 /* This rule tries to catch accelerometer devices and opt out. We may
450 * want to adjust the protocol later adding a proper event for dealing
451 * with accelerometers and implement here accordingly */
452 if (has_abs && !has_key && !device->is_mt)
456 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
457 weston_seat_init_pointer(&device->master->base);
458 if ((device->caps & EVDEV_KEYBOARD))
459 weston_seat_init_keyboard(&device->master->base, NULL);
460 if ((device->caps & EVDEV_TOUCH))
461 weston_seat_init_touch(&device->master->base);
466 static struct evdev_input_device *
467 evdev_input_device_create(struct evdev_seat *master,
468 struct wl_display *display, const char *path)
470 struct evdev_input_device *device;
471 struct weston_compositor *ec;
473 device = malloc(sizeof *device);
477 ec = master->base.compositor;
479 container_of(ec->output_list.next, struct weston_output, link);
481 device->master = master;
483 device->mtdev = NULL;
484 device->devnode = strdup(path);
485 device->mt.slot = -1;
488 device->dispatch = NULL;
490 /* Use non-blocking mode so that we can loop on read on
491 * evdev_input_device_data() until all events on the fd are
492 * read. mtdev_get() also expects this. */
493 device->fd = weston_launcher_open(ec, path, O_RDWR | O_NONBLOCK);
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)
519 wl_list_insert(master->devices_list.prev, &device->link);
524 device->dispatch->interface->destroy(device->dispatch);
528 free(device->devnode);
533 static const char default_seat[] = "seat0";
536 device_added(struct udev_device *udev_device, struct evdev_seat *master)
538 struct weston_compositor *c;
540 const char *device_seat;
542 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
544 device_seat = default_seat;
546 if (strcmp(device_seat, master->seat_id))
549 c = master->base.compositor;
550 devnode = udev_device_get_devnode(udev_device);
551 evdev_input_device_create(master, c->wl_display, devnode);
555 device_removed(struct evdev_input_device *device)
557 struct evdev_dispatch *dispatch;
559 dispatch = device->dispatch;
561 dispatch->interface->destroy(dispatch);
563 wl_event_source_remove(device->source);
564 wl_list_remove(&device->link);
566 mtdev_close_delete(device->mtdev);
568 free(device->devnode);
573 evdev_notify_keyboard_focus(struct evdev_seat *seat)
575 struct evdev_input_device *device;
576 struct wl_array keys;
578 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
582 memset(all_keys, 0, sizeof all_keys);
583 wl_list_for_each(device, &seat->devices_list, link) {
584 memset(evdev_keys, 0, sizeof evdev_keys);
585 ret = ioctl(device->fd,
586 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
588 weston_log("failed to get keys for device %s\n",
592 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
593 all_keys[i] |= evdev_keys[i];
596 wl_array_init(&keys);
597 for (i = 0; i < KEY_CNT; i++) {
598 set = all_keys[i >> 3] & (1 << (i & 7));
600 k = wl_array_add(&keys, sizeof *k);
605 notify_keyboard_focus_in(&seat->base.seat, &keys,
606 STATE_UPDATE_AUTOMATIC);
608 wl_array_release(&keys);
612 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
614 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
615 struct udev_enumerate *e;
616 struct udev_list_entry *entry;
617 struct udev_device *device;
618 const char *path, *sysname;
620 e = udev_enumerate_new(udev);
621 udev_enumerate_add_match_subsystem(e, "input");
622 udev_enumerate_scan_devices(e);
623 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
624 path = udev_list_entry_get_name(entry);
625 device = udev_device_new_from_syspath(udev, path);
627 sysname = udev_device_get_sysname(device);
628 if (strncmp("event", sysname, 5) != 0) {
629 udev_device_unref(device);
633 device_added(device, seat);
635 udev_device_unref(device);
637 udev_enumerate_unref(e);
639 evdev_notify_keyboard_focus(seat);
641 if (wl_list_empty(&seat->devices_list)) {
643 "warning: no input devices on entering Weston. "
645 "\t- no permissions to read /dev/input/event*\n"
646 "\t- seats misconfigured "
647 "(Weston backend option 'seat', "
648 "udev device property ID_SEAT)\n");
653 evdev_udev_handler(int fd, uint32_t mask, void *data)
655 struct evdev_seat *master = data;
656 struct udev_device *udev_device;
657 struct evdev_input_device *device, *next;
661 udev_device = udev_monitor_receive_device(master->udev_monitor);
665 action = udev_device_get_action(udev_device);
667 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
670 if (!strcmp(action, "add")) {
671 device_added(udev_device, master);
673 else if (!strcmp(action, "remove")) {
674 devnode = udev_device_get_devnode(udev_device);
675 wl_list_for_each_safe(device, next,
676 &master->devices_list, link)
677 if (!strcmp(device->devnode, devnode)) {
678 device_removed(device);
683 udev_device_unref(udev_device);
689 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
691 struct evdev_seat *master = (struct evdev_seat *) seat_base;
692 struct wl_event_loop *loop;
693 struct weston_compositor *c = master->base.compositor;
696 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
697 if (!master->udev_monitor) {
698 weston_log("udev: failed to create the udev monitor\n");
702 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
705 if (udev_monitor_enable_receiving(master->udev_monitor)) {
706 weston_log("udev: failed to bind the udev monitor\n");
707 udev_monitor_unref(master->udev_monitor);
711 loop = wl_display_get_event_loop(c->wl_display);
712 fd = udev_monitor_get_fd(master->udev_monitor);
713 master->udev_monitor_source =
714 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
715 evdev_udev_handler, master);
716 if (!master->udev_monitor_source) {
717 udev_monitor_unref(master->udev_monitor);
725 evdev_disable_udev_monitor(struct weston_seat *seat_base)
727 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
729 if (!seat->udev_monitor)
732 udev_monitor_unref(seat->udev_monitor);
733 seat->udev_monitor = NULL;
734 wl_event_source_remove(seat->udev_monitor_source);
735 seat->udev_monitor_source = NULL;
739 evdev_input_create(struct weston_compositor *c, struct udev *udev,
742 struct evdev_seat *seat;
744 seat = malloc(sizeof *seat);
748 memset(seat, 0, sizeof *seat);
749 weston_seat_init(&seat->base, c);
750 seat->base.led_update = evdev_led_update;
752 wl_list_init(&seat->devices_list);
753 seat->seat_id = strdup(seat_id);
754 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
760 evdev_add_devices(udev, &seat->base);
762 c->seat = &seat->base;
766 evdev_remove_devices(struct weston_seat *seat_base)
768 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
769 struct evdev_input_device *device, *next;
771 wl_list_for_each_safe(device, next, &seat->devices_list, link)
772 device_removed(device);
774 notify_keyboard_focus_out(&seat->base.seat);
778 evdev_input_destroy(struct weston_seat *seat_base)
780 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
782 evdev_remove_devices(seat_base);
783 evdev_disable_udev_monitor(&seat->base);
785 weston_seat_release(seat_base);