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>
30 #include "compositor.h"
34 struct weston_input_device base;
35 struct wl_list devices_list;
36 struct udev_monitor *udev_monitor;
42 struct evdev_input_device {
43 struct evdev_input *master;
45 struct wl_event_source *source;
46 struct weston_output *output;
50 int min_x, max_x, min_y, max_y;
51 int old_x, old_y, reset_x, reset_y;
65 int type; /* event type flags */
67 int is_touchpad, is_mt;
70 /* event type flags */
71 #define EVDEV_ABSOLUTE_MOTION (1 << 0)
72 #define EVDEV_ABSOLUTE_MT_DOWN (1 << 1)
73 #define EVDEV_ABSOLUTE_MT_MOTION (1 << 2)
74 #define EVDEV_ABSOLUTE_MT_UP (1 << 3)
75 #define EVDEV_RELATIVE_MOTION (1 << 4)
78 evdev_process_key(struct evdev_input_device *device,
79 struct input_event *e, int time)
89 case BTN_TOOL_AIRBRUSH:
93 if (device->is_touchpad)
95 device->abs.reset_x = 1;
96 device->abs.reset_y = 1;
100 /* Multitouch touchscreen devices might not send individually
101 * button events each time a new finger is down. So we don't
102 * send notification for such devices and we solve the button
103 * case emulating on compositor side. */
107 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
110 /* Intentional fallthrough! */
119 notify_button(&device->master->base.input_device,
120 time, e->code, e->value);
124 notify_key(&device->master->base.input_device,
125 time, e->code, e->value);
131 evdev_process_touch(struct evdev_input_device *device,
132 struct input_event *e)
134 const int screen_width = device->output->current->width;
135 const int screen_height = device->output->current->height;
139 device->mt.slot = e->value;
141 case ABS_MT_TRACKING_ID:
143 device->type |= EVDEV_ABSOLUTE_MT_DOWN;
145 device->type |= EVDEV_ABSOLUTE_MT_UP;
147 case ABS_MT_POSITION_X:
148 device->mt.x[device->mt.slot] =
149 (e->value - device->abs.min_x) * screen_width /
150 (device->abs.max_x - device->abs.min_x) +
152 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
154 case ABS_MT_POSITION_Y:
155 device->mt.y[device->mt.slot] =
156 (e->value - device->abs.min_y) * screen_height /
157 (device->abs.max_y - device->abs.min_y) +
159 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
165 evdev_process_absolute_motion(struct evdev_input_device *device,
166 struct input_event *e)
168 const int screen_width = device->output->current->width;
169 const int screen_height = device->output->current->height;
174 (e->value - device->abs.min_x) * screen_width /
175 (device->abs.max_x - device->abs.min_x) +
177 device->type |= EVDEV_ABSOLUTE_MOTION;
181 (e->value - device->abs.min_y) * screen_height /
182 (device->abs.max_y - device->abs.min_y) +
184 device->type |= EVDEV_ABSOLUTE_MOTION;
190 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
191 struct input_event *e)
193 /* FIXME: Make this configurable somehow. */
194 const int touchpad_speed = 700;
198 e->value -= device->abs.min_x;
199 if (device->abs.reset_x)
200 device->abs.reset_x = 0;
203 (e->value - device->abs.old_x) *
205 (device->abs.max_x - device->abs.min_x);
207 device->abs.old_x = e->value;
208 device->type |= EVDEV_RELATIVE_MOTION;
211 e->value -= device->abs.min_y;
212 if (device->abs.reset_y)
213 device->abs.reset_y = 0;
216 (e->value - device->abs.old_y) *
218 /* maybe use x size here to have the same scale? */
219 (device->abs.max_y - device->abs.min_y);
221 device->abs.old_y = e->value;
222 device->type |= EVDEV_RELATIVE_MOTION;
228 evdev_process_relative_motion(struct evdev_input_device *device,
229 struct input_event *e)
233 device->rel.dx += e->value;
234 device->type |= EVDEV_RELATIVE_MOTION;
237 device->rel.dy += e->value;
238 device->type |= EVDEV_RELATIVE_MOTION;
244 evdev_process_absolute(struct evdev_input_device *device,
245 struct input_event *e)
247 if (device->is_touchpad) {
248 evdev_process_absolute_motion_touchpad(device, e);
249 } else if (device->is_mt) {
250 evdev_process_touch(device, e);
252 evdev_process_absolute_motion(device, e);
257 is_motion_event(struct input_event *e)
270 case ABS_MT_POSITION_X:
271 case ABS_MT_POSITION_Y:
280 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
282 struct wl_input_device *master = &device->master->base.input_device;
287 if (device->type & EVDEV_RELATIVE_MOTION) {
288 notify_motion(master, time,
289 master->x + device->rel.dx,
290 master->y + device->rel.dy);
291 device->type &= ~EVDEV_RELATIVE_MOTION;
295 if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
296 notify_touch(master, time,
298 device->mt.x[device->mt.slot],
299 device->mt.y[device->mt.slot],
300 WL_INPUT_DEVICE_TOUCH_DOWN);
301 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
302 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
304 if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
305 notify_touch(master, time,
307 device->mt.x[device->mt.slot],
308 device->mt.y[device->mt.slot],
309 WL_INPUT_DEVICE_TOUCH_MOTION);
310 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
311 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
313 if (device->type & EVDEV_ABSOLUTE_MT_UP) {
314 notify_touch(master, time, device->mt.slot, 0, 0,
315 WL_INPUT_DEVICE_TOUCH_UP);
316 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
318 if (device->type & EVDEV_ABSOLUTE_MOTION) {
319 notify_motion(master, time, device->abs.x, device->abs.y);
320 device->type &= ~EVDEV_ABSOLUTE_MOTION;
325 evdev_input_device_data(int fd, uint32_t mask, void *data)
327 struct weston_compositor *ec;
328 struct evdev_input_device *device = data;
329 struct input_event ev[8], *e, *end;
333 ec = device->master->base.compositor;
337 len = read(fd, &ev, sizeof ev);
338 if (len < 0 || len % sizeof e[0] != 0) {
339 /* FIXME: call device_removed when errno is ENODEV. */;
344 end = (void *) ev + len;
345 for (e = ev; e < end; e++) {
346 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
348 /* we try to minimize the amount of notifications to be
349 * forwarded to the compositor, so we accumulate motion
350 * events and send as a bunch */
351 if (!is_motion_event(e))
352 evdev_flush_motion(device, time);
355 evdev_process_relative_motion(device, e);
358 evdev_process_absolute(device, e);
361 evdev_process_key(device, e, time);
366 evdev_flush_motion(device, time);
372 /* copied from udev/extras/input_id/input_id.c */
373 /* we must use this kernel-compatible implementation */
374 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
375 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
376 #define OFF(x) ((x)%BITS_PER_LONG)
377 #define BIT(x) (1UL<<OFF(x))
378 #define LONG(x) ((x)/BITS_PER_LONG)
379 #define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
383 evdev_configure_device(struct evdev_input_device *device)
385 struct input_absinfo absinfo;
386 unsigned long ev_bits[NBITS(EV_MAX)];
387 unsigned long abs_bits[NBITS(ABS_MAX)];
388 unsigned long key_bits[NBITS(KEY_MAX)];
389 int has_key, has_abs;
394 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
395 if (TEST_BIT(ev_bits, EV_ABS)) {
398 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
400 if (TEST_BIT(abs_bits, ABS_X)) {
401 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
402 device->abs.min_x = absinfo.minimum;
403 device->abs.max_x = absinfo.maximum;
405 if (TEST_BIT(abs_bits, ABS_Y)) {
406 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
407 device->abs.min_y = absinfo.minimum;
408 device->abs.max_y = absinfo.maximum;
410 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
415 if (TEST_BIT(ev_bits, EV_KEY)) {
417 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
419 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
420 !TEST_BIT(key_bits, BTN_TOOL_PEN))
421 device->is_touchpad = 1;
424 /* This rule tries to catch accelerometer devices and opt out. We may
425 * want to adjust the protocol later adding a proper event for dealing
426 * with accelerometers and implement here accordingly */
427 if (has_abs && !has_key)
433 static struct evdev_input_device *
434 evdev_input_device_create(struct evdev_input *master,
435 struct wl_display *display, const char *path)
437 struct evdev_input_device *device;
438 struct wl_event_loop *loop;
439 struct weston_compositor *ec;
441 device = malloc(sizeof *device);
445 ec = master->base.compositor;
447 container_of(ec->output_list.next, struct weston_output, link);
449 device->master = master;
450 device->is_touchpad = 0;
452 device->devnode = strdup(path);
453 device->mt.slot = -1;
457 device->fd = open(path, O_RDONLY);
461 if (evdev_configure_device(device) == -1)
464 loop = wl_display_get_event_loop(display);
465 device->source = wl_event_loop_add_fd(loop, device->fd,
467 evdev_input_device_data, device);
468 if (device->source == NULL)
471 wl_list_insert(master->devices_list.prev, &device->link);
478 free(device->devnode);
483 static const char default_seat[] = "seat0";
486 device_added(struct udev_device *udev_device, struct evdev_input *master)
488 struct weston_compositor *c;
490 const char *device_seat;
492 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
494 device_seat = default_seat;
496 if (strcmp(device_seat, master->seat_id))
499 c = master->base.compositor;
500 devnode = udev_device_get_devnode(udev_device);
501 if (evdev_input_device_create(master, c->wl_display, devnode))
502 fprintf(stderr, "evdev input device: added: %s\n", devnode);
506 device_removed(struct udev_device *udev_device, struct evdev_input *master)
508 const char *devnode = udev_device_get_devnode(udev_device);
509 struct evdev_input_device *device, *next;
511 wl_list_for_each_safe(device, next, &master->devices_list, link) {
512 if (!strcmp(device->devnode, devnode)) {
513 wl_event_source_remove(device->source);
514 wl_list_remove(&device->link);
516 free(device->devnode);
521 fprintf(stderr, "evdev input device: removed: %s\n", devnode);
525 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
527 struct evdev_input *input = (struct evdev_input *) input_base;
528 struct udev_enumerate *e;
529 struct udev_list_entry *entry;
530 struct udev_device *device;
533 e = udev_enumerate_new(udev);
534 udev_enumerate_add_match_subsystem(e, "input");
535 udev_enumerate_scan_devices(e);
536 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
537 path = udev_list_entry_get_name(entry);
538 device = udev_device_new_from_syspath(udev, path);
540 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
543 device_added(device, input);
545 udev_device_unref(device);
547 udev_enumerate_unref(e);
551 evdev_udev_handler(int fd, uint32_t mask, void *data)
553 struct evdev_input *master = data;
554 struct udev_device *udev_device;
557 udev_device = udev_monitor_receive_device(master->udev_monitor);
561 action = udev_device_get_action(udev_device);
563 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
566 if (!strcmp(action, "add")) {
567 device_added(udev_device, master);
569 else if (!strcmp(action, "remove"))
570 device_removed(udev_device, master);
572 udev_device_unref(udev_device);
578 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
580 struct wl_event_loop *loop;
581 struct weston_compositor *c = master->base.compositor;
583 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
584 if (!master->udev_monitor)
587 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
590 if (udev_monitor_enable_receiving(master->udev_monitor)) {
591 fprintf(stderr, "udev: failed to bind the udev monitor\n");
595 loop = wl_display_get_event_loop(c->wl_display);
596 wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
597 WL_EVENT_READABLE, evdev_udev_handler, master);
603 evdev_input_create(struct weston_compositor *c, struct udev *udev,
606 struct evdev_input *input;
608 input = malloc(sizeof *input);
612 memset(input, 0, sizeof *input);
613 weston_input_device_init(&input->base, c);
615 wl_list_init(&input->devices_list);
616 input->seat_id = strdup(seat);
617 if (!evdev_config_udev_monitor(udev, input)) {
618 free(input->seat_id);
623 evdev_add_devices(udev, &input->base);
625 c->input_device = &input->base.input_device;
629 evdev_remove_devices(struct weston_input_device *input_base)
631 struct evdev_input *input = (struct evdev_input *) input_base;
632 struct evdev_input_device *device, *next;
634 wl_list_for_each_safe(device, next, &input->devices_list, link) {
635 fprintf(stderr, "evdev input device: removed: %s\n", device->devnode);
636 wl_event_source_remove(device->source);
637 wl_list_remove(&device->link);
639 free(device->devnode);
645 evdev_input_destroy(struct weston_input_device *input_base)
647 struct evdev_input *input = (struct evdev_input *) input_base;
649 evdev_remove_devices(input_base);
650 wl_list_remove(&input->base.link);
651 free(input->seat_id);