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 evdev_seat *seat = (struct evdev_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 evdev_seat *master,
473 const char *path, int device_fd)
475 struct evdev_input_device *device;
476 struct weston_compositor *ec;
478 device = malloc(sizeof *device);
482 ec = master->base.compositor;
484 container_of(ec->output_list.next, struct weston_output, link);
486 device->seat = &master->base;
488 device->mtdev = NULL;
489 device->devnode = strdup(path);
490 device->mt.slot = -1;
493 device->dispatch = NULL;
494 device->fd = device_fd;
496 if (evdev_configure_device(device) == -1)
499 /* If the dispatch was not set up use the fallback. */
500 if (device->dispatch == NULL)
501 device->dispatch = fallback_dispatch_create();
502 if (device->dispatch == NULL)
507 device->mtdev = mtdev_new_open(device->fd);
509 weston_log("mtdev failed to open for %s\n", path);
512 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
514 evdev_input_device_data, device);
515 if (device->source == NULL)
518 wl_list_insert(master->devices_list.prev, &device->link);
523 device->dispatch->interface->destroy(device->dispatch);
525 free(device->devnode);
531 evdev_input_device_destroy(struct evdev_input_device *device)
533 struct evdev_dispatch *dispatch;
535 dispatch = device->dispatch;
537 dispatch->interface->destroy(dispatch);
539 wl_event_source_remove(device->source);
540 wl_list_remove(&device->link);
542 mtdev_close_delete(device->mtdev);
544 free(device->devnode);
548 static const char default_seat[] = "seat0";
551 device_added(struct udev_device *udev_device, struct evdev_seat *master)
553 struct weston_compositor *c;
554 struct evdev_input_device *device;
556 const char *device_seat;
559 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
561 device_seat = default_seat;
563 if (strcmp(device_seat, master->seat_id))
566 c = master->base.compositor;
567 devnode = udev_device_get_devnode(udev_device);
569 /* Use non-blocking mode so that we can loop on read on
570 * evdev_input_device_data() until all events on the fd are
571 * read. mtdev_get() also expects this. */
572 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
574 weston_log("opening input device '%s' failed.\n", devnode);
578 device = evdev_input_device_create(master, devnode, fd);
581 weston_log("not using input device '%s'.\n", devnode);
586 evdev_notify_keyboard_focus(struct weston_seat *seat,
587 struct wl_list *evdev_devices)
589 struct evdev_input_device *device;
590 struct wl_array keys;
592 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
596 memset(all_keys, 0, sizeof all_keys);
597 wl_list_for_each(device, evdev_devices, link) {
598 memset(evdev_keys, 0, sizeof evdev_keys);
599 ret = ioctl(device->fd,
600 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
602 weston_log("failed to get keys for device %s\n",
606 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
607 all_keys[i] |= evdev_keys[i];
610 wl_array_init(&keys);
611 for (i = 0; i < KEY_CNT; i++) {
612 set = all_keys[i >> 3] & (1 << (i & 7));
614 k = wl_array_add(&keys, sizeof *k);
619 notify_keyboard_focus_in(&seat->seat, &keys, STATE_UPDATE_AUTOMATIC);
621 wl_array_release(&keys);
625 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
627 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
628 struct udev_enumerate *e;
629 struct udev_list_entry *entry;
630 struct udev_device *device;
631 const char *path, *sysname;
633 e = udev_enumerate_new(udev);
634 udev_enumerate_add_match_subsystem(e, "input");
635 udev_enumerate_scan_devices(e);
636 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
637 path = udev_list_entry_get_name(entry);
638 device = udev_device_new_from_syspath(udev, path);
640 sysname = udev_device_get_sysname(device);
641 if (strncmp("event", sysname, 5) != 0) {
642 udev_device_unref(device);
646 device_added(device, seat);
648 udev_device_unref(device);
650 udev_enumerate_unref(e);
652 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
654 if (wl_list_empty(&seat->devices_list)) {
656 "warning: no input devices on entering Weston. "
658 "\t- no permissions to read /dev/input/event*\n"
659 "\t- seats misconfigured "
660 "(Weston backend option 'seat', "
661 "udev device property ID_SEAT)\n");
666 evdev_udev_handler(int fd, uint32_t mask, void *data)
668 struct evdev_seat *master = data;
669 struct udev_device *udev_device;
670 struct evdev_input_device *device, *next;
674 udev_device = udev_monitor_receive_device(master->udev_monitor);
678 action = udev_device_get_action(udev_device);
680 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
683 if (!strcmp(action, "add")) {
684 device_added(udev_device, master);
686 else if (!strcmp(action, "remove")) {
687 devnode = udev_device_get_devnode(udev_device);
688 wl_list_for_each_safe(device, next,
689 &master->devices_list, link)
690 if (!strcmp(device->devnode, devnode)) {
691 evdev_input_device_destroy(device);
696 udev_device_unref(udev_device);
702 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
704 struct evdev_seat *master = (struct evdev_seat *) seat_base;
705 struct wl_event_loop *loop;
706 struct weston_compositor *c = master->base.compositor;
709 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
710 if (!master->udev_monitor) {
711 weston_log("udev: failed to create the udev monitor\n");
715 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
718 if (udev_monitor_enable_receiving(master->udev_monitor)) {
719 weston_log("udev: failed to bind the udev monitor\n");
720 udev_monitor_unref(master->udev_monitor);
724 loop = wl_display_get_event_loop(c->wl_display);
725 fd = udev_monitor_get_fd(master->udev_monitor);
726 master->udev_monitor_source =
727 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
728 evdev_udev_handler, master);
729 if (!master->udev_monitor_source) {
730 udev_monitor_unref(master->udev_monitor);
738 evdev_disable_udev_monitor(struct weston_seat *seat_base)
740 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
742 if (!seat->udev_monitor)
745 udev_monitor_unref(seat->udev_monitor);
746 seat->udev_monitor = NULL;
747 wl_event_source_remove(seat->udev_monitor_source);
748 seat->udev_monitor_source = NULL;
752 evdev_input_create(struct weston_compositor *c, struct udev *udev,
755 struct evdev_seat *seat;
757 seat = malloc(sizeof *seat);
761 memset(seat, 0, sizeof *seat);
762 weston_seat_init(&seat->base, c);
763 seat->base.led_update = drm_led_update;
765 wl_list_init(&seat->devices_list);
766 seat->seat_id = strdup(seat_id);
767 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
773 evdev_add_devices(udev, &seat->base);
775 c->seat = &seat->base;
779 evdev_remove_devices(struct weston_seat *seat_base)
781 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
782 struct evdev_input_device *device, *next;
784 wl_list_for_each_safe(device, next, &seat->devices_list, link)
785 evdev_input_device_destroy(device);
787 notify_keyboard_focus_out(&seat->base.seat);
791 evdev_input_destroy(struct weston_seat *seat_base)
793 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
795 evdev_remove_devices(seat_base);
796 evdev_disable_udev_monitor(&seat->base);
798 weston_seat_release(seat_base);