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;
38 struct wl_event_source *udev_monitor_source;
44 struct evdev_input_device {
45 struct evdev_input *master;
47 struct wl_event_source *source;
48 struct weston_output *output;
52 int min_x, max_x, min_y, max_y;
53 int old_x, old_y, reset_x, reset_y;
68 int type; /* event type flags */
70 int is_touchpad, is_mt;
73 /* event type flags */
74 #define EVDEV_ABSOLUTE_MOTION (1 << 0)
75 #define EVDEV_ABSOLUTE_MT_DOWN (1 << 1)
76 #define EVDEV_ABSOLUTE_MT_MOTION (1 << 2)
77 #define EVDEV_ABSOLUTE_MT_UP (1 << 3)
78 #define EVDEV_RELATIVE_MOTION (1 << 4)
81 evdev_process_key(struct evdev_input_device *device,
82 struct input_event *e, int time)
92 case BTN_TOOL_AIRBRUSH:
96 if (device->is_touchpad)
98 device->abs.reset_x = 1;
99 device->abs.reset_y = 1;
103 /* Multitouch touchscreen devices might not send individually
104 * button events each time a new finger is down. So we don't
105 * send notification for such devices and we solve the button
106 * case emulating on compositor side. */
110 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
113 /* Intentional fallthrough! */
122 notify_button(&device->master->base.input_device,
123 time, e->code, e->value);
127 notify_key(&device->master->base.input_device,
128 time, e->code, e->value);
134 evdev_process_touch(struct evdev_input_device *device,
135 struct input_event *e)
137 const int screen_width = device->output->current->width;
138 const int screen_height = device->output->current->height;
142 device->mt.slot = e->value;
144 case ABS_MT_TRACKING_ID:
146 device->type |= EVDEV_ABSOLUTE_MT_DOWN;
148 device->type |= EVDEV_ABSOLUTE_MT_UP;
150 case ABS_MT_POSITION_X:
151 device->mt.x[device->mt.slot] =
152 (e->value - device->abs.min_x) * screen_width /
153 (device->abs.max_x - device->abs.min_x) +
155 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
157 case ABS_MT_POSITION_Y:
158 device->mt.y[device->mt.slot] =
159 (e->value - device->abs.min_y) * screen_height /
160 (device->abs.max_y - device->abs.min_y) +
162 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
168 evdev_process_absolute_motion(struct evdev_input_device *device,
169 struct input_event *e)
171 const int screen_width = device->output->current->width;
172 const int screen_height = device->output->current->height;
177 (e->value - device->abs.min_x) * screen_width /
178 (device->abs.max_x - device->abs.min_x) +
180 device->type |= EVDEV_ABSOLUTE_MOTION;
184 (e->value - device->abs.min_y) * screen_height /
185 (device->abs.max_y - device->abs.min_y) +
187 device->type |= EVDEV_ABSOLUTE_MOTION;
193 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
194 struct input_event *e)
196 /* FIXME: Make this configurable somehow. */
197 const int touchpad_speed = 700;
201 e->value -= device->abs.min_x;
202 if (device->abs.reset_x)
203 device->abs.reset_x = 0;
206 (e->value - device->abs.old_x) *
208 (device->abs.max_x - device->abs.min_x);
210 device->abs.old_x = e->value;
211 device->type |= EVDEV_RELATIVE_MOTION;
214 e->value -= device->abs.min_y;
215 if (device->abs.reset_y)
216 device->abs.reset_y = 0;
219 (e->value - device->abs.old_y) *
221 /* maybe use x size here to have the same scale? */
222 (device->abs.max_y - device->abs.min_y);
224 device->abs.old_y = e->value;
225 device->type |= EVDEV_RELATIVE_MOTION;
231 evdev_process_relative(struct evdev_input_device *device,
232 struct input_event *e, uint32_t time)
236 device->rel.dx += e->value;
237 device->type |= EVDEV_RELATIVE_MOTION;
240 device->rel.dy += e->value;
241 device->type |= EVDEV_RELATIVE_MOTION;
244 notify_axis(&device->master->base.input_device,
246 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, e->value);
249 notify_axis(&device->master->base.input_device,
251 WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, e->value);
257 evdev_process_absolute(struct evdev_input_device *device,
258 struct input_event *e)
260 if (device->is_touchpad) {
261 evdev_process_absolute_motion_touchpad(device, e);
262 } else if (device->is_mt) {
263 evdev_process_touch(device, e);
265 evdev_process_absolute_motion(device, e);
270 is_motion_event(struct input_event *e)
283 case ABS_MT_POSITION_X:
284 case ABS_MT_POSITION_Y:
293 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
295 struct wl_input_device *master = &device->master->base.input_device;
300 if (device->type & EVDEV_RELATIVE_MOTION) {
301 notify_motion(master, time,
302 master->x + device->rel.dx,
303 master->y + device->rel.dy);
304 device->type &= ~EVDEV_RELATIVE_MOTION;
308 if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
309 notify_touch(master, time,
311 device->mt.x[device->mt.slot],
312 device->mt.y[device->mt.slot],
313 WL_INPUT_DEVICE_TOUCH_DOWN);
314 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
315 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
317 if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
318 notify_touch(master, time,
320 device->mt.x[device->mt.slot],
321 device->mt.y[device->mt.slot],
322 WL_INPUT_DEVICE_TOUCH_MOTION);
323 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
324 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
326 if (device->type & EVDEV_ABSOLUTE_MT_UP) {
327 notify_touch(master, time, device->mt.slot, 0, 0,
328 WL_INPUT_DEVICE_TOUCH_UP);
329 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
331 if (device->type & EVDEV_ABSOLUTE_MOTION) {
332 notify_motion(master, time, device->abs.x, device->abs.y);
333 device->type &= ~EVDEV_ABSOLUTE_MOTION;
338 evdev_process_events(struct evdev_input_device *device,
339 struct input_event *ev, int count)
341 struct input_event *e, *end;
348 for (e = ev; e < end; e++) {
349 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
351 /* we try to minimize the amount of notifications to be
352 * forwarded to the compositor, so we accumulate motion
353 * events and send as a bunch */
354 if (!is_motion_event(e))
355 evdev_flush_motion(device, time);
358 evdev_process_relative(device, e, time);
361 evdev_process_absolute(device, e);
364 evdev_process_key(device, e, time);
369 evdev_flush_motion(device, time);
373 evdev_input_device_data(int fd, uint32_t mask, void *data)
375 struct weston_compositor *ec;
376 struct evdev_input_device *device = data;
377 struct input_event ev[32];
380 ec = device->master->base.compositor;
384 /* If the compositor is repainting, this function is called only once
385 * per frame and we have to process all the events available on the
386 * fd, otherwise there will be input lag. */
389 len = mtdev_get(device->mtdev, fd, ev,
391 sizeof (struct input_event);
393 len = read(fd, &ev, sizeof ev);
395 if (len < 0 || len % sizeof ev[0] != 0) {
396 /* FIXME: call device_removed when errno is ENODEV. */
400 evdev_process_events(device, ev, len / sizeof ev[0]);
407 /* copied from udev/extras/input_id/input_id.c */
408 /* we must use this kernel-compatible implementation */
409 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
410 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
411 #define OFF(x) ((x)%BITS_PER_LONG)
412 #define BIT(x) (1UL<<OFF(x))
413 #define LONG(x) ((x)/BITS_PER_LONG)
414 #define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
418 evdev_configure_device(struct evdev_input_device *device)
420 struct input_absinfo absinfo;
421 unsigned long ev_bits[NBITS(EV_MAX)];
422 unsigned long abs_bits[NBITS(ABS_MAX)];
423 unsigned long key_bits[NBITS(KEY_MAX)];
424 int has_key, has_abs;
429 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
430 if (TEST_BIT(ev_bits, EV_ABS)) {
433 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
435 if (TEST_BIT(abs_bits, ABS_X)) {
436 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
437 device->abs.min_x = absinfo.minimum;
438 device->abs.max_x = absinfo.maximum;
440 if (TEST_BIT(abs_bits, ABS_Y)) {
441 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
442 device->abs.min_y = absinfo.minimum;
443 device->abs.max_y = absinfo.maximum;
445 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
450 if (TEST_BIT(ev_bits, EV_KEY)) {
452 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
454 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
455 !TEST_BIT(key_bits, BTN_TOOL_PEN))
456 device->is_touchpad = 1;
459 /* This rule tries to catch accelerometer devices and opt out. We may
460 * want to adjust the protocol later adding a proper event for dealing
461 * with accelerometers and implement here accordingly */
462 if (has_abs && !has_key)
468 static struct evdev_input_device *
469 evdev_input_device_create(struct evdev_input *master,
470 struct wl_display *display, const char *path)
472 struct evdev_input_device *device;
473 struct weston_compositor *ec;
475 device = malloc(sizeof *device);
479 ec = master->base.compositor;
481 container_of(ec->output_list.next, struct weston_output, link);
483 device->master = master;
484 device->is_touchpad = 0;
486 device->mtdev = NULL;
487 device->devnode = strdup(path);
488 device->mt.slot = -1;
492 /* Use non-blocking mode so that we can loop on read on
493 * evdev_input_device_data() until all events on the fd are
494 * read. mtdev_get() also expects this. */
495 device->fd = open(path, O_RDONLY | O_NONBLOCK);
499 if (evdev_configure_device(device) == -1)
503 device->mtdev = mtdev_new_open(device->fd);
505 fprintf(stderr, "mtdev failed to open for %s\n", path);
508 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
510 evdev_input_device_data, device);
511 if (device->source == NULL)
514 wl_list_insert(master->devices_list.prev, &device->link);
521 free(device->devnode);
526 static const char default_seat[] = "seat0";
529 device_added(struct udev_device *udev_device, struct evdev_input *master)
531 struct weston_compositor *c;
533 const char *device_seat;
535 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
537 device_seat = default_seat;
539 if (strcmp(device_seat, master->seat_id))
542 c = master->base.compositor;
543 devnode = udev_device_get_devnode(udev_device);
544 evdev_input_device_create(master, c->wl_display, devnode);
548 device_removed(struct evdev_input_device *device)
550 wl_event_source_remove(device->source);
551 wl_list_remove(&device->link);
553 mtdev_close_delete(device->mtdev);
555 free(device->devnode);
560 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
562 struct evdev_input *input = (struct evdev_input *) input_base;
563 struct udev_enumerate *e;
564 struct udev_list_entry *entry;
565 struct udev_device *device;
566 const char *path, *sysname;
568 e = udev_enumerate_new(udev);
569 udev_enumerate_add_match_subsystem(e, "input");
570 udev_enumerate_scan_devices(e);
571 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
572 path = udev_list_entry_get_name(entry);
573 device = udev_device_new_from_syspath(udev, path);
575 sysname = udev_device_get_sysname(device);
576 if (strncmp("event", sysname, 5) != 0) {
577 udev_device_unref(device);
581 device_added(device, input);
583 udev_device_unref(device);
585 udev_enumerate_unref(e);
587 if (wl_list_empty(&input->devices_list)) {
589 "warning: no input devices on entering Weston. "
591 "\t- no permissions to read /dev/input/event*\n"
592 "\t- seats misconfigured "
593 "(Weston backend option 'seat', "
594 "udev device property ID_SEAT)\n");
599 evdev_udev_handler(int fd, uint32_t mask, void *data)
601 struct evdev_input *master = data;
602 struct udev_device *udev_device;
603 struct evdev_input_device *device, *next;
607 udev_device = udev_monitor_receive_device(master->udev_monitor);
611 action = udev_device_get_action(udev_device);
613 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
616 if (!strcmp(action, "add")) {
617 device_added(udev_device, master);
619 else if (!strcmp(action, "remove")) {
620 devnode = udev_device_get_devnode(udev_device);
621 wl_list_for_each_safe(device, next,
622 &master->devices_list, link)
623 if (!strcmp(device->devnode, devnode)) {
624 device_removed(device);
629 udev_device_unref(udev_device);
635 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
637 struct wl_event_loop *loop;
638 struct weston_compositor *c = master->base.compositor;
641 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
642 if (!master->udev_monitor) {
643 fprintf(stderr, "udev: failed to create the udev monitor\n");
647 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
650 if (udev_monitor_enable_receiving(master->udev_monitor)) {
651 fprintf(stderr, "udev: failed to bind the udev monitor\n");
652 udev_monitor_unref(master->udev_monitor);
656 loop = wl_display_get_event_loop(c->wl_display);
657 fd = udev_monitor_get_fd(master->udev_monitor);
658 master->udev_monitor_source =
659 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
660 evdev_udev_handler, master);
661 if (!master->udev_monitor_source) {
662 udev_monitor_unref(master->udev_monitor);
670 evdev_input_create(struct weston_compositor *c, struct udev *udev,
673 struct evdev_input *input;
675 input = malloc(sizeof *input);
679 memset(input, 0, sizeof *input);
680 weston_input_device_init(&input->base, c);
682 wl_list_init(&input->devices_list);
683 input->seat_id = strdup(seat);
684 if (!evdev_config_udev_monitor(udev, input)) {
685 free(input->seat_id);
690 evdev_add_devices(udev, &input->base);
692 c->input_device = &input->base.input_device;
696 evdev_remove_devices(struct weston_input_device *input_base)
698 struct evdev_input *input = (struct evdev_input *) input_base;
699 struct evdev_input_device *device, *next;
701 wl_list_for_each_safe(device, next, &input->devices_list, link)
702 device_removed(device);
706 evdev_input_destroy(struct weston_input_device *input_base)
708 struct evdev_input *input = (struct evdev_input *) input_base;
710 evdev_remove_devices(input_base);
712 udev_monitor_unref(input->udev_monitor);
713 wl_event_source_remove(input->udev_monitor_source);
715 wl_list_remove(&input->base.link);
716 free(input->seat_id);