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"
33 struct wlsc_input_device base;
34 struct wl_list devices_list;
35 struct udev_monitor *udev_monitor;
39 struct evdev_input_device {
40 struct evdev_input *master;
42 struct wl_event_source *source;
43 struct wlsc_output *output;
48 int min_x, max_x, min_y, max_y;
49 int old_x, old_y, reset_x, reset_y;
54 /* event type flags */
55 #define EVDEV_ABSOLUTE_MOTION (1 << 0)
56 #define EVDEV_RELATIVE_MOTION (1 << 1)
58 struct evdev_motion_accumulator {
61 int type; /* event type flags */
65 evdev_process_key(struct evdev_input_device *device,
66 struct input_event *e, int time)
76 case BTN_TOOL_AIRBRUSH:
80 device->tool = e->value ? e->code : 0;
81 if (device->is_touchpad)
83 device->abs.reset_x = 1;
84 device->abs.reset_y = 1;
89 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
92 /* Intentional fallthrough! */
101 notify_button(&device->master->base.input_device,
102 time, e->code, e->value);
106 notify_key(&device->master->base.input_device,
107 time, e->code, e->value);
113 evdev_process_absolute_motion(struct evdev_input_device *device,
114 struct input_event *e, struct evdev_motion_accumulator *accum)
116 const int screen_width = device->output->current->width;
117 const int screen_height = device->output->current->height;
121 accum->x = (e->value - device->abs.min_x) * screen_width /
122 (device->abs.max_x - device->abs.min_x) +
124 accum->type = EVDEV_ABSOLUTE_MOTION;
127 accum->y = (e->value - device->abs.min_y) * screen_height /
128 (device->abs.max_y - device->abs.min_y) +
130 accum->type = EVDEV_ABSOLUTE_MOTION;
136 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
137 struct input_event *e, struct evdev_motion_accumulator *accum)
139 /* FIXME: Make this configurable somehow. */
140 const int touchpad_speed = 700;
144 e->value -= device->abs.min_x;
145 if (device->abs.reset_x)
146 device->abs.reset_x = 0;
148 accum->dx = (e->value - device->abs.old_x) *
150 (device->abs.max_x - device->abs.min_x);
152 device->abs.old_x = e->value;
153 accum->type = EVDEV_RELATIVE_MOTION;
156 e->value -= device->abs.min_y;
157 if (device->abs.reset_y)
158 device->abs.reset_y = 0;
160 accum->dy = (e->value - device->abs.old_y) *
162 /* maybe use x size here to have the same scale? */
163 (device->abs.max_y - device->abs.min_y);
165 device->abs.old_y = e->value;
166 accum->type = EVDEV_RELATIVE_MOTION;
172 evdev_process_relative_motion(struct input_event *e,
173 struct evdev_motion_accumulator *accum)
177 accum->dx += e->value;
178 accum->type = EVDEV_RELATIVE_MOTION;
181 accum->dy += e->value;
182 accum->type = EVDEV_RELATIVE_MOTION;
188 is_motion_event(struct input_event *e)
209 evdev_flush_motion(struct wl_input_device *device, uint32_t time,
210 struct evdev_motion_accumulator accum)
212 if (accum.type == EVDEV_RELATIVE_MOTION)
213 notify_motion(device, time, accum.x + accum.dx,
215 if (accum.type == EVDEV_ABSOLUTE_MOTION)
216 notify_motion(device, time, accum.x, accum.y);
220 evdev_input_device_data(int fd, uint32_t mask, void *data)
222 struct wlsc_compositor *ec;
223 struct evdev_input_device *device = data;
224 struct input_event ev[8], *e, *end;
226 struct evdev_motion_accumulator accumulator;
229 ec = (struct wlsc_compositor *)
230 device->master->base.input_device.compositor;
236 accumulator.type = 0;
237 accumulator.x = device->master->base.input_device.x;
238 accumulator.y = device->master->base.input_device.y;
240 len = read(fd, &ev, sizeof ev);
241 if (len < 0 || len % sizeof e[0] != 0) {
242 /* FIXME: call device_removed when errno is ENODEV. */;
247 end = (void *) ev + len;
248 for (e = ev; e < end; e++) {
249 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
251 /* we try to minimize the amount of notifications to be
252 * forwarded to the compositor, so we accumulate motion
253 * events and send as a bunch */
254 if (!is_motion_event(e)) {
255 evdev_flush_motion(&device->master->base.input_device,
259 accumulator.type = 0;
264 evdev_process_relative_motion(e, &accumulator);
267 if (device->is_touchpad)
268 evdev_process_absolute_motion_touchpad(device,
271 evdev_process_absolute_motion(device, e,
275 evdev_process_key(device, e, time);
280 evdev_flush_motion(&device->master->base.input_device, time,
287 /* copied from udev/extras/input_id/input_id.c */
288 /* we must use this kernel-compatible implementation */
289 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
290 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
291 #define OFF(x) ((x)%BITS_PER_LONG)
292 #define BIT(x) (1UL<<OFF(x))
293 #define LONG(x) ((x)/BITS_PER_LONG)
294 #define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
298 evdev_configure_device(struct evdev_input_device *device)
300 struct input_absinfo absinfo;
301 unsigned long ev_bits[NBITS(EV_MAX)];
302 unsigned long abs_bits[NBITS(ABS_MAX)];
303 unsigned long key_bits[NBITS(KEY_MAX)];
304 int has_key, has_abs;
309 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
310 if (TEST_BIT(ev_bits, EV_ABS)) {
313 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
315 if (TEST_BIT(abs_bits, ABS_X)) {
316 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
317 device->abs.min_x = absinfo.minimum;
318 device->abs.max_x = absinfo.maximum;
320 if (TEST_BIT(abs_bits, ABS_Y)) {
321 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
322 device->abs.min_y = absinfo.minimum;
323 device->abs.max_y = absinfo.maximum;
326 if (TEST_BIT(ev_bits, EV_KEY)) {
328 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
330 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
331 !TEST_BIT(key_bits, BTN_TOOL_PEN))
332 device->is_touchpad = 1;
335 /* This rule tries to catch accelerometer devices and opt out. We may
336 * want to adjust the protocol later adding a proper event for dealing
337 * with accelerometers and implement here accordingly */
338 if (has_abs && !has_key)
344 static struct evdev_input_device *
345 evdev_input_device_create(struct evdev_input *master,
346 struct wl_display *display, const char *path)
348 struct evdev_input_device *device;
349 struct wl_event_loop *loop;
350 struct wlsc_compositor *ec;
352 device = malloc(sizeof *device);
356 ec = (struct wlsc_compositor *) master->base.input_device.compositor;
358 container_of(ec->output_list.next, struct wlsc_output, link);
361 device->master = master;
362 device->is_touchpad = 0;
363 device->devnode = strdup(path);
365 device->fd = open(path, O_RDONLY);
369 if (evdev_configure_device(device) == -1)
372 loop = wl_display_get_event_loop(display);
373 device->source = wl_event_loop_add_fd(loop, device->fd,
375 evdev_input_device_data, device);
376 if (device->source == NULL)
379 wl_list_insert(master->devices_list.prev, &device->link);
386 free(device->devnode);
391 static const char default_seat[] = "seat0";
394 device_added(struct udev_device *udev_device, struct evdev_input *master)
396 struct wlsc_compositor *c;
398 const char *device_seat;
400 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
402 device_seat = default_seat;
404 if (strcmp(device_seat, master->seat_id))
407 c = (struct wlsc_compositor *) master->base.input_device.compositor;
408 devnode = udev_device_get_devnode(udev_device);
409 if (evdev_input_device_create(master, c->wl_display, devnode))
410 fprintf(stderr, "evdev input device: added: %s\n", devnode);
414 device_removed(struct udev_device *udev_device, struct evdev_input *master)
416 const char *devnode = udev_device_get_devnode(udev_device);
417 struct evdev_input_device *device, *next;
419 wl_list_for_each_safe(device, next, &master->devices_list, link) {
420 if (!strcmp(device->devnode, devnode)) {
421 wl_event_source_remove(device->source);
422 wl_list_remove(&device->link);
424 free(device->devnode);
429 fprintf(stderr, "evdev input device: removed: %s\n", devnode);
433 evdev_udev_handler(int fd, uint32_t mask, void *data)
435 struct evdev_input *master = data;
436 struct udev_device *udev_device;
439 udev_device = udev_monitor_receive_device(master->udev_monitor);
443 action = udev_device_get_action(udev_device);
445 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
448 if (!strcmp(action, "add")) {
449 device_added(udev_device, master);
451 else if (!strcmp(action, "remove"))
452 device_removed(udev_device, master);
454 udev_device_unref(udev_device);
460 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
462 struct wl_event_loop *loop;
463 struct wlsc_compositor *c =
464 (struct wlsc_compositor *) master->base.input_device.compositor;
466 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
467 if (!master->udev_monitor)
470 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
473 if (udev_monitor_enable_receiving(master->udev_monitor)) {
474 fprintf(stderr, "udev: failed to bind the udev monitor\n");
478 loop = wl_display_get_event_loop(c->wl_display);
479 wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
480 WL_EVENT_READABLE, evdev_udev_handler, master);
486 evdev_input_add_devices(struct wlsc_compositor *c,
487 struct udev *udev, const char *seat)
489 struct evdev_input *input;
490 struct udev_enumerate *e;
491 struct udev_list_entry *entry;
492 struct udev_device *device;
495 input = malloc(sizeof *input);
499 memset(input, 0, sizeof *input);
500 wlsc_input_device_init(&input->base, c);
502 wl_list_init(&input->devices_list);
503 input->seat_id = strdup(seat);
504 if (!evdev_config_udev_monitor(udev, input)) {
505 free(input->seat_id);
510 e = udev_enumerate_new(udev);
511 udev_enumerate_add_match_subsystem(e, "input");
512 udev_enumerate_scan_devices(e);
513 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
514 path = udev_list_entry_get_name(entry);
515 device = udev_device_new_from_syspath(udev, path);
517 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
520 device_added(device, input);
522 udev_device_unref(device);
524 udev_enumerate_unref(e);
526 c->input_device = &input->base.input_device;