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"
35 struct weston_input_device base;
36 struct wl_list devices_list;
37 struct udev_monitor *udev_monitor;
43 struct evdev_input_device {
44 struct evdev_input *master;
46 struct wl_event_source *source;
47 struct weston_output *output;
51 int min_x, max_x, min_y, max_y;
52 int old_x, old_y, reset_x, reset_y;
67 int type; /* event type flags */
69 int is_touchpad, is_mt;
72 /* event type flags */
73 #define EVDEV_ABSOLUTE_MOTION (1 << 0)
74 #define EVDEV_ABSOLUTE_MT_DOWN (1 << 1)
75 #define EVDEV_ABSOLUTE_MT_MOTION (1 << 2)
76 #define EVDEV_ABSOLUTE_MT_UP (1 << 3)
77 #define EVDEV_RELATIVE_MOTION (1 << 4)
80 evdev_process_key(struct evdev_input_device *device,
81 struct input_event *e, int time)
91 case BTN_TOOL_AIRBRUSH:
95 if (device->is_touchpad)
97 device->abs.reset_x = 1;
98 device->abs.reset_y = 1;
102 /* Multitouch touchscreen devices might not send individually
103 * button events each time a new finger is down. So we don't
104 * send notification for such devices and we solve the button
105 * case emulating on compositor side. */
109 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
112 /* Intentional fallthrough! */
121 notify_button(&device->master->base.input_device,
122 time, e->code, e->value);
126 notify_key(&device->master->base.input_device,
127 time, e->code, e->value);
133 evdev_process_touch(struct evdev_input_device *device,
134 struct input_event *e)
136 const int screen_width = device->output->current->width;
137 const int screen_height = device->output->current->height;
141 device->mt.slot = e->value;
143 case ABS_MT_TRACKING_ID:
145 device->type |= EVDEV_ABSOLUTE_MT_DOWN;
147 device->type |= EVDEV_ABSOLUTE_MT_UP;
149 case ABS_MT_POSITION_X:
150 device->mt.x[device->mt.slot] =
151 (e->value - device->abs.min_x) * screen_width /
152 (device->abs.max_x - device->abs.min_x) +
154 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
156 case ABS_MT_POSITION_Y:
157 device->mt.y[device->mt.slot] =
158 (e->value - device->abs.min_y) * screen_height /
159 (device->abs.max_y - device->abs.min_y) +
161 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
167 evdev_process_absolute_motion(struct evdev_input_device *device,
168 struct input_event *e)
170 const int screen_width = device->output->current->width;
171 const int screen_height = device->output->current->height;
176 (e->value - device->abs.min_x) * screen_width /
177 (device->abs.max_x - device->abs.min_x) +
179 device->type |= EVDEV_ABSOLUTE_MOTION;
183 (e->value - device->abs.min_y) * screen_height /
184 (device->abs.max_y - device->abs.min_y) +
186 device->type |= EVDEV_ABSOLUTE_MOTION;
192 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
193 struct input_event *e)
195 /* FIXME: Make this configurable somehow. */
196 const int touchpad_speed = 700;
200 e->value -= device->abs.min_x;
201 if (device->abs.reset_x)
202 device->abs.reset_x = 0;
205 (e->value - device->abs.old_x) *
207 (device->abs.max_x - device->abs.min_x);
209 device->abs.old_x = e->value;
210 device->type |= EVDEV_RELATIVE_MOTION;
213 e->value -= device->abs.min_y;
214 if (device->abs.reset_y)
215 device->abs.reset_y = 0;
218 (e->value - device->abs.old_y) *
220 /* maybe use x size here to have the same scale? */
221 (device->abs.max_y - device->abs.min_y);
223 device->abs.old_y = e->value;
224 device->type |= EVDEV_RELATIVE_MOTION;
230 evdev_process_relative(struct evdev_input_device *device,
231 struct input_event *e, uint32_t time)
235 device->rel.dx += e->value;
236 device->type |= EVDEV_RELATIVE_MOTION;
239 device->rel.dy += e->value;
240 device->type |= EVDEV_RELATIVE_MOTION;
243 notify_axis(&device->master->base.input_device,
245 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, e->value);
248 notify_axis(&device->master->base.input_device,
250 WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, e->value);
256 evdev_process_absolute(struct evdev_input_device *device,
257 struct input_event *e)
259 if (device->is_touchpad) {
260 evdev_process_absolute_motion_touchpad(device, e);
261 } else if (device->is_mt) {
262 evdev_process_touch(device, e);
264 evdev_process_absolute_motion(device, e);
269 is_motion_event(struct input_event *e)
282 case ABS_MT_POSITION_X:
283 case ABS_MT_POSITION_Y:
292 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
294 struct wl_input_device *master = &device->master->base.input_device;
299 if (device->type & EVDEV_RELATIVE_MOTION) {
300 notify_motion(master, time,
301 master->x + device->rel.dx,
302 master->y + device->rel.dy);
303 device->type &= ~EVDEV_RELATIVE_MOTION;
307 if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
308 notify_touch(master, time,
310 device->mt.x[device->mt.slot],
311 device->mt.y[device->mt.slot],
312 WL_INPUT_DEVICE_TOUCH_DOWN);
313 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
314 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
316 if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
317 notify_touch(master, time,
319 device->mt.x[device->mt.slot],
320 device->mt.y[device->mt.slot],
321 WL_INPUT_DEVICE_TOUCH_MOTION);
322 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
323 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
325 if (device->type & EVDEV_ABSOLUTE_MT_UP) {
326 notify_touch(master, time, device->mt.slot, 0, 0,
327 WL_INPUT_DEVICE_TOUCH_UP);
328 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
330 if (device->type & EVDEV_ABSOLUTE_MOTION) {
331 notify_motion(master, time, device->abs.x, device->abs.y);
332 device->type &= ~EVDEV_ABSOLUTE_MOTION;
337 evdev_process_events(struct evdev_input_device *device,
338 struct input_event *ev, int count)
340 struct input_event *e, *end;
347 for (e = ev; e < end; e++) {
348 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
350 /* we try to minimize the amount of notifications to be
351 * forwarded to the compositor, so we accumulate motion
352 * events and send as a bunch */
353 if (!is_motion_event(e))
354 evdev_flush_motion(device, time);
357 evdev_process_relative(device, e, time);
360 evdev_process_absolute(device, e);
363 evdev_process_key(device, e, time);
368 evdev_flush_motion(device, time);
372 evdev_input_device_data(int fd, uint32_t mask, void *data)
374 struct weston_compositor *ec;
375 struct evdev_input_device *device = data;
376 struct input_event ev[32];
379 ec = device->master->base.compositor;
383 /* If the compositor is repainting, this function is called only once
384 * per frame and we have to process all the events available on the
385 * fd, otherwise there will be input lag. */
388 len = mtdev_get(device->mtdev, fd, ev,
390 sizeof (struct input_event);
392 len = read(fd, &ev, sizeof ev);
394 if (len < 0 || len % sizeof ev[0] != 0) {
395 /* FIXME: call device_removed when errno is ENODEV. */
399 evdev_process_events(device, ev, len / sizeof ev[0]);
406 /* copied from udev/extras/input_id/input_id.c */
407 /* we must use this kernel-compatible implementation */
408 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
409 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
410 #define OFF(x) ((x)%BITS_PER_LONG)
411 #define BIT(x) (1UL<<OFF(x))
412 #define LONG(x) ((x)/BITS_PER_LONG)
413 #define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
417 evdev_configure_device(struct evdev_input_device *device)
419 struct input_absinfo absinfo;
420 unsigned long ev_bits[NBITS(EV_MAX)];
421 unsigned long abs_bits[NBITS(ABS_MAX)];
422 unsigned long key_bits[NBITS(KEY_MAX)];
423 int has_key, has_abs;
428 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
429 if (TEST_BIT(ev_bits, EV_ABS)) {
432 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
434 if (TEST_BIT(abs_bits, ABS_X)) {
435 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
436 device->abs.min_x = absinfo.minimum;
437 device->abs.max_x = absinfo.maximum;
439 if (TEST_BIT(abs_bits, ABS_Y)) {
440 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
441 device->abs.min_y = absinfo.minimum;
442 device->abs.max_y = absinfo.maximum;
444 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
449 if (TEST_BIT(ev_bits, EV_KEY)) {
451 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
453 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
454 !TEST_BIT(key_bits, BTN_TOOL_PEN))
455 device->is_touchpad = 1;
458 /* This rule tries to catch accelerometer devices and opt out. We may
459 * want to adjust the protocol later adding a proper event for dealing
460 * with accelerometers and implement here accordingly */
461 if (has_abs && !has_key)
467 static struct evdev_input_device *
468 evdev_input_device_create(struct evdev_input *master,
469 struct wl_display *display, const char *path)
471 struct evdev_input_device *device;
472 struct weston_compositor *ec;
474 device = malloc(sizeof *device);
478 ec = master->base.compositor;
480 container_of(ec->output_list.next, struct weston_output, link);
482 device->master = master;
483 device->is_touchpad = 0;
485 device->mtdev = NULL;
486 device->devnode = strdup(path);
487 device->mt.slot = -1;
491 /* Use non-blocking mode so that we can loop on read on
492 * evdev_input_device_data() until all events on the fd are
493 * read. mtdev_get() also expects this. */
494 device->fd = open(path, O_RDONLY | O_NONBLOCK);
498 if (evdev_configure_device(device) == -1)
502 device->mtdev = mtdev_new_open(device->fd);
504 fprintf(stderr, "mtdev failed to open for %s\n", path);
507 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
509 evdev_input_device_data, device);
510 if (device->source == NULL)
513 wl_list_insert(master->devices_list.prev, &device->link);
520 free(device->devnode);
525 static const char default_seat[] = "seat0";
528 device_added(struct udev_device *udev_device, struct evdev_input *master)
530 struct weston_compositor *c;
532 const char *device_seat;
534 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
536 device_seat = default_seat;
538 if (strcmp(device_seat, master->seat_id))
541 c = master->base.compositor;
542 devnode = udev_device_get_devnode(udev_device);
543 evdev_input_device_create(master, c->wl_display, devnode);
547 device_removed(struct evdev_input_device *device)
549 wl_event_source_remove(device->source);
550 wl_list_remove(&device->link);
552 mtdev_close_delete(device->mtdev);
554 free(device->devnode);
559 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
561 struct evdev_input *input = (struct evdev_input *) input_base;
562 struct udev_enumerate *e;
563 struct udev_list_entry *entry;
564 struct udev_device *device;
567 e = udev_enumerate_new(udev);
568 udev_enumerate_add_match_subsystem(e, "input");
569 udev_enumerate_scan_devices(e);
570 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
571 path = udev_list_entry_get_name(entry);
572 device = udev_device_new_from_syspath(udev, path);
574 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
577 device_added(device, input);
579 udev_device_unref(device);
581 udev_enumerate_unref(e);
583 if (wl_list_empty(&input->devices_list)) {
585 "warning: no input devices on entering Weston. "
587 "\t- no permissions to read /dev/input/event*\n"
588 "\t- seats misconfigured "
589 "(Weston backend option 'seat', "
590 "udev device property ID_SEAT)\n");
595 evdev_udev_handler(int fd, uint32_t mask, void *data)
597 struct evdev_input *master = data;
598 struct udev_device *udev_device;
599 struct evdev_input_device *device, *next;
603 udev_device = udev_monitor_receive_device(master->udev_monitor);
607 action = udev_device_get_action(udev_device);
609 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
612 if (!strcmp(action, "add")) {
613 device_added(udev_device, master);
615 else if (!strcmp(action, "remove")) {
616 devnode = udev_device_get_devnode(udev_device);
617 wl_list_for_each_safe(device, next,
618 &master->devices_list, link)
619 if (!strcmp(device->devnode, devnode)) {
620 device_removed(device);
625 udev_device_unref(udev_device);
631 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
633 struct wl_event_loop *loop;
634 struct weston_compositor *c = master->base.compositor;
636 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
637 if (!master->udev_monitor) {
638 fprintf(stderr, "udev: failed to create the udev monitor\n");
642 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
645 if (udev_monitor_enable_receiving(master->udev_monitor)) {
646 fprintf(stderr, "udev: failed to bind the udev monitor\n");
650 loop = wl_display_get_event_loop(c->wl_display);
651 wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
652 WL_EVENT_READABLE, evdev_udev_handler, master);
658 evdev_input_create(struct weston_compositor *c, struct udev *udev,
661 struct evdev_input *input;
663 input = malloc(sizeof *input);
667 memset(input, 0, sizeof *input);
668 weston_input_device_init(&input->base, c);
670 wl_list_init(&input->devices_list);
671 input->seat_id = strdup(seat);
672 if (!evdev_config_udev_monitor(udev, input)) {
673 free(input->seat_id);
678 evdev_add_devices(udev, &input->base);
680 c->input_device = &input->base.input_device;
684 evdev_remove_devices(struct weston_input_device *input_base)
686 struct evdev_input *input = (struct evdev_input *) input_base;
687 struct evdev_input_device *device, *next;
689 wl_list_for_each_safe(device, next, &input->devices_list, link)
690 device_removed(device);
694 evdev_input_destroy(struct weston_input_device *input_base)
696 struct evdev_input *input = (struct evdev_input *) input_base;
698 evdev_remove_devices(input_base);
699 wl_list_remove(&input->base.link);
700 free(input->seat_id);