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.
26 #include <linux/input.h>
31 #include "compositor.h"
33 #include "launcher-util.h"
36 struct weston_input_device base;
37 struct wl_list devices_list;
38 struct udev_monitor *udev_monitor;
39 struct wl_event_source *udev_monitor_source;
45 struct evdev_input_device {
46 struct evdev_input *master;
48 struct wl_event_source *source;
49 struct weston_output *output;
53 int min_x, max_x, min_y, max_y;
54 int old_x, old_y, reset_x, reset_y;
69 int type; /* event type flags */
71 int is_touchpad, is_mt;
74 /* event type flags */
75 #define EVDEV_ABSOLUTE_MOTION (1 << 0)
76 #define EVDEV_ABSOLUTE_MT_DOWN (1 << 1)
77 #define EVDEV_ABSOLUTE_MT_MOTION (1 << 2)
78 #define EVDEV_ABSOLUTE_MT_UP (1 << 3)
79 #define EVDEV_RELATIVE_MOTION (1 << 4)
82 evdev_process_key(struct evdev_input_device *device,
83 struct input_event *e, int time)
93 case BTN_TOOL_AIRBRUSH:
97 if (device->is_touchpad)
99 device->abs.reset_x = 1;
100 device->abs.reset_y = 1;
104 /* Multitouch touchscreen devices might not send individually
105 * button events each time a new finger is down. So we don't
106 * send notification for such devices and we solve the button
107 * case emulating on compositor side. */
111 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
114 /* Intentional fallthrough! */
123 notify_button(&device->master->base.input_device,
124 time, e->code, e->value);
128 notify_key(&device->master->base.input_device,
129 time, e->code, e->value);
135 evdev_process_touch(struct evdev_input_device *device,
136 struct input_event *e)
138 const int screen_width = device->output->current->width;
139 const int screen_height = device->output->current->height;
143 device->mt.slot = e->value;
145 case ABS_MT_TRACKING_ID:
147 device->type |= EVDEV_ABSOLUTE_MT_DOWN;
149 device->type |= EVDEV_ABSOLUTE_MT_UP;
151 case ABS_MT_POSITION_X:
152 device->mt.x[device->mt.slot] =
153 (e->value - device->abs.min_x) * screen_width /
154 (device->abs.max_x - device->abs.min_x) +
156 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
158 case ABS_MT_POSITION_Y:
159 device->mt.y[device->mt.slot] =
160 (e->value - device->abs.min_y) * screen_height /
161 (device->abs.max_y - device->abs.min_y) +
163 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
169 evdev_process_absolute_motion(struct evdev_input_device *device,
170 struct input_event *e)
172 const int screen_width = device->output->current->width;
173 const int screen_height = device->output->current->height;
178 (e->value - device->abs.min_x) * screen_width /
179 (device->abs.max_x - device->abs.min_x) +
181 device->type |= EVDEV_ABSOLUTE_MOTION;
185 (e->value - device->abs.min_y) * screen_height /
186 (device->abs.max_y - device->abs.min_y) +
188 device->type |= EVDEV_ABSOLUTE_MOTION;
194 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
195 struct input_event *e)
197 /* FIXME: Make this configurable somehow. */
198 const int touchpad_speed = 700;
202 e->value -= device->abs.min_x;
203 if (device->abs.reset_x)
204 device->abs.reset_x = 0;
207 (e->value - device->abs.old_x) *
209 (device->abs.max_x - device->abs.min_x);
211 device->abs.old_x = e->value;
212 device->type |= EVDEV_RELATIVE_MOTION;
215 e->value -= device->abs.min_y;
216 if (device->abs.reset_y)
217 device->abs.reset_y = 0;
220 (e->value - device->abs.old_y) *
222 /* maybe use x size here to have the same scale? */
223 (device->abs.max_y - device->abs.min_y);
225 device->abs.old_y = e->value;
226 device->type |= EVDEV_RELATIVE_MOTION;
232 evdev_process_relative(struct evdev_input_device *device,
233 struct input_event *e, uint32_t time)
237 device->rel.dx += e->value;
238 device->type |= EVDEV_RELATIVE_MOTION;
241 device->rel.dy += e->value;
242 device->type |= EVDEV_RELATIVE_MOTION;
245 notify_axis(&device->master->base.input_device,
247 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, e->value);
250 notify_axis(&device->master->base.input_device,
252 WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, e->value);
258 evdev_process_absolute(struct evdev_input_device *device,
259 struct input_event *e)
261 if (device->is_touchpad) {
262 evdev_process_absolute_motion_touchpad(device, e);
263 } else if (device->is_mt) {
264 evdev_process_touch(device, e);
266 evdev_process_absolute_motion(device, e);
271 is_motion_event(struct input_event *e)
284 case ABS_MT_POSITION_X:
285 case ABS_MT_POSITION_Y:
294 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
296 struct wl_input_device *master = &device->master->base.input_device;
301 if (device->type & EVDEV_RELATIVE_MOTION) {
302 notify_motion(master, time,
303 master->x + wl_fixed_from_int(device->rel.dx),
304 master->y + wl_fixed_from_int(device->rel.dy));
305 device->type &= ~EVDEV_RELATIVE_MOTION;
309 if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
310 notify_touch(master, time,
312 wl_fixed_from_int(device->mt.x[device->mt.slot]),
313 wl_fixed_from_int(device->mt.y[device->mt.slot]),
314 WL_INPUT_DEVICE_TOUCH_DOWN);
315 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
316 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
318 if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
319 notify_touch(master, time,
321 wl_fixed_from_int(device->mt.x[device->mt.slot]),
322 wl_fixed_from_int(device->mt.y[device->mt.slot]),
323 WL_INPUT_DEVICE_TOUCH_MOTION);
324 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
325 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
327 if (device->type & EVDEV_ABSOLUTE_MT_UP) {
328 notify_touch(master, time, device->mt.slot, 0, 0,
329 WL_INPUT_DEVICE_TOUCH_UP);
330 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
332 if (device->type & EVDEV_ABSOLUTE_MOTION) {
333 notify_motion(master, time,
334 wl_fixed_from_int(device->abs.x),
335 wl_fixed_from_int(device->abs.y));
336 device->type &= ~EVDEV_ABSOLUTE_MOTION;
341 evdev_process_events(struct evdev_input_device *device,
342 struct input_event *ev, int count)
344 struct input_event *e, *end;
351 for (e = ev; e < end; e++) {
352 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
354 /* we try to minimize the amount of notifications to be
355 * forwarded to the compositor, so we accumulate motion
356 * events and send as a bunch */
357 if (!is_motion_event(e))
358 evdev_flush_motion(device, time);
361 evdev_process_relative(device, e, time);
364 evdev_process_absolute(device, e);
367 evdev_process_key(device, e, time);
372 evdev_flush_motion(device, time);
376 evdev_input_device_data(int fd, uint32_t mask, void *data)
378 struct weston_compositor *ec;
379 struct evdev_input_device *device = data;
380 struct input_event ev[32];
383 ec = device->master->base.compositor;
387 /* If the compositor is repainting, this function is called only once
388 * per frame and we have to process all the events available on the
389 * fd, otherwise there will be input lag. */
392 len = mtdev_get(device->mtdev, fd, ev,
394 sizeof (struct input_event);
396 len = read(fd, &ev, sizeof ev);
398 if (len < 0 || len % sizeof ev[0] != 0) {
399 /* FIXME: call device_removed when errno is ENODEV. */
403 evdev_process_events(device, ev, len / sizeof ev[0]);
410 /* copied from udev/extras/input_id/input_id.c */
411 /* we must use this kernel-compatible implementation */
412 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
413 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
414 #define OFF(x) ((x)%BITS_PER_LONG)
415 #define BIT(x) (1UL<<OFF(x))
416 #define LONG(x) ((x)/BITS_PER_LONG)
417 #define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
421 evdev_configure_device(struct evdev_input_device *device)
423 struct input_absinfo absinfo;
424 unsigned long ev_bits[NBITS(EV_MAX)];
425 unsigned long abs_bits[NBITS(ABS_MAX)];
426 unsigned long key_bits[NBITS(KEY_MAX)];
427 int has_key, has_abs;
432 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
433 if (TEST_BIT(ev_bits, EV_ABS)) {
436 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
438 if (TEST_BIT(abs_bits, ABS_X)) {
439 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
440 device->abs.min_x = absinfo.minimum;
441 device->abs.max_x = absinfo.maximum;
443 if (TEST_BIT(abs_bits, ABS_Y)) {
444 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
445 device->abs.min_y = absinfo.minimum;
446 device->abs.max_y = absinfo.maximum;
448 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
453 if (TEST_BIT(ev_bits, EV_KEY)) {
455 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
457 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
458 !TEST_BIT(key_bits, BTN_TOOL_PEN))
459 device->is_touchpad = 1;
462 /* This rule tries to catch accelerometer devices and opt out. We may
463 * want to adjust the protocol later adding a proper event for dealing
464 * with accelerometers and implement here accordingly */
465 if (has_abs && !has_key)
471 static struct evdev_input_device *
472 evdev_input_device_create(struct evdev_input *master,
473 struct wl_display *display, const char *path)
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->master = master;
487 device->is_touchpad = 0;
489 device->mtdev = NULL;
490 device->devnode = strdup(path);
491 device->mt.slot = -1;
495 /* Use non-blocking mode so that we can loop on read on
496 * evdev_input_device_data() until all events on the fd are
497 * read. mtdev_get() also expects this. */
498 device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
502 if (evdev_configure_device(device) == -1)
506 device->mtdev = mtdev_new_open(device->fd);
508 fprintf(stderr, "mtdev failed to open for %s\n", path);
511 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
513 evdev_input_device_data, device);
514 if (device->source == NULL)
517 wl_list_insert(master->devices_list.prev, &device->link);
524 free(device->devnode);
529 static const char default_seat[] = "seat0";
532 device_added(struct udev_device *udev_device, struct evdev_input *master)
534 struct weston_compositor *c;
536 const char *device_seat;
538 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
540 device_seat = default_seat;
542 if (strcmp(device_seat, master->seat_id))
545 c = master->base.compositor;
546 devnode = udev_device_get_devnode(udev_device);
547 evdev_input_device_create(master, c->wl_display, devnode);
551 device_removed(struct evdev_input_device *device)
553 wl_event_source_remove(device->source);
554 wl_list_remove(&device->link);
556 mtdev_close_delete(device->mtdev);
558 free(device->devnode);
563 evdev_notify_keyboard_focus(struct evdev_input *input)
565 struct evdev_input_device *device;
566 struct wl_array keys;
568 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
572 memset(all_keys, 0, sizeof all_keys);
573 wl_list_for_each(device, &input->devices_list, link) {
574 memset(evdev_keys, 0, sizeof evdev_keys);
575 ret = ioctl(device->fd,
576 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
578 fprintf(stderr, "failed to get keys for device %s\n",
582 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
583 all_keys[i] |= evdev_keys[i];
586 wl_array_init(&keys);
587 for (i = 0; i < KEY_CNT; i++) {
588 set = all_keys[i >> 3] & (1 << (i & 7));
590 k = wl_array_add(&keys, sizeof *k);
595 notify_keyboard_focus(&input->base.input_device, &keys);
597 wl_array_release(&keys);
601 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
603 struct evdev_input *input = (struct evdev_input *) input_base;
604 struct udev_enumerate *e;
605 struct udev_list_entry *entry;
606 struct udev_device *device;
607 const char *path, *sysname;
609 e = udev_enumerate_new(udev);
610 udev_enumerate_add_match_subsystem(e, "input");
611 udev_enumerate_scan_devices(e);
612 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
613 path = udev_list_entry_get_name(entry);
614 device = udev_device_new_from_syspath(udev, path);
616 sysname = udev_device_get_sysname(device);
617 if (strncmp("event", sysname, 5) != 0) {
618 udev_device_unref(device);
622 device_added(device, input);
624 udev_device_unref(device);
626 udev_enumerate_unref(e);
628 evdev_notify_keyboard_focus(input);
630 if (wl_list_empty(&input->devices_list)) {
632 "warning: no input devices on entering Weston. "
634 "\t- no permissions to read /dev/input/event*\n"
635 "\t- seats misconfigured "
636 "(Weston backend option 'seat', "
637 "udev device property ID_SEAT)\n");
642 evdev_udev_handler(int fd, uint32_t mask, void *data)
644 struct evdev_input *master = data;
645 struct udev_device *udev_device;
646 struct evdev_input_device *device, *next;
650 udev_device = udev_monitor_receive_device(master->udev_monitor);
654 action = udev_device_get_action(udev_device);
656 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
659 if (!strcmp(action, "add")) {
660 device_added(udev_device, master);
662 else if (!strcmp(action, "remove")) {
663 devnode = udev_device_get_devnode(udev_device);
664 wl_list_for_each_safe(device, next,
665 &master->devices_list, link)
666 if (!strcmp(device->devnode, devnode)) {
667 device_removed(device);
672 udev_device_unref(udev_device);
678 evdev_enable_udev_monitor(struct udev *udev, struct weston_input_device *input_base)
680 struct evdev_input *master = (struct evdev_input *) input_base;
681 struct wl_event_loop *loop;
682 struct weston_compositor *c = master->base.compositor;
685 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
686 if (!master->udev_monitor) {
687 fprintf(stderr, "udev: failed to create the udev monitor\n");
691 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
694 if (udev_monitor_enable_receiving(master->udev_monitor)) {
695 fprintf(stderr, "udev: failed to bind the udev monitor\n");
696 udev_monitor_unref(master->udev_monitor);
700 loop = wl_display_get_event_loop(c->wl_display);
701 fd = udev_monitor_get_fd(master->udev_monitor);
702 master->udev_monitor_source =
703 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
704 evdev_udev_handler, master);
705 if (!master->udev_monitor_source) {
706 udev_monitor_unref(master->udev_monitor);
714 evdev_disable_udev_monitor(struct weston_input_device *input_base)
716 struct evdev_input *input = (struct evdev_input *) input_base;
718 if (!input->udev_monitor)
721 udev_monitor_unref(input->udev_monitor);
722 input->udev_monitor = NULL;
723 wl_event_source_remove(input->udev_monitor_source);
724 input->udev_monitor_source = NULL;
728 evdev_input_create(struct weston_compositor *c, struct udev *udev,
731 struct evdev_input *input;
733 input = malloc(sizeof *input);
737 memset(input, 0, sizeof *input);
738 weston_input_device_init(&input->base, c);
740 wl_list_init(&input->devices_list);
741 input->seat_id = strdup(seat);
742 if (!evdev_enable_udev_monitor(udev, &input->base)) {
743 free(input->seat_id);
748 evdev_add_devices(udev, &input->base);
750 c->input_device = &input->base.input_device;
754 evdev_remove_devices(struct weston_input_device *input_base)
756 struct evdev_input *input = (struct evdev_input *) input_base;
757 struct evdev_input_device *device, *next;
759 wl_list_for_each_safe(device, next, &input->devices_list, link)
760 device_removed(device);
762 notify_keyboard_focus(&input->base.input_device, NULL);
766 evdev_input_destroy(struct weston_input_device *input_base)
768 struct evdev_input *input = (struct evdev_input *) input_base;
770 evdev_remove_devices(input_base);
771 evdev_disable_udev_monitor(&input->base);
773 wl_list_remove(&input->base.link);
774 free(input->seat_id);