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 "evdev-private.h"
34 #include "launcher-util.h"
37 evdev_process_key(struct evdev_input_device *device,
38 struct input_event *e, int time)
52 notify_button(&device->master->base.seat,
53 time, e->code, e->value);
57 notify_key(&device->master->base.seat,
58 time, e->code, e->value);
64 evdev_process_touch(struct evdev_input_device *device,
65 struct input_event *e)
67 const int screen_width = device->output->current->width;
68 const int screen_height = device->output->current->height;
72 device->mt.slot = e->value;
74 case ABS_MT_TRACKING_ID:
76 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
78 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
80 case ABS_MT_POSITION_X:
81 device->mt.x[device->mt.slot] =
82 (e->value - device->abs.min_x) * screen_width /
83 (device->abs.max_x - device->abs.min_x) +
85 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
87 case ABS_MT_POSITION_Y:
88 device->mt.y[device->mt.slot] =
89 (e->value - device->abs.min_y) * screen_height /
90 (device->abs.max_y - device->abs.min_y) +
92 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
98 evdev_process_absolute_motion(struct evdev_input_device *device,
99 struct input_event *e)
101 const int screen_width = device->output->current->width;
102 const int screen_height = device->output->current->height;
107 (e->value - device->abs.min_x) * screen_width /
108 (device->abs.max_x - device->abs.min_x) +
110 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
114 (e->value - device->abs.min_y) * screen_height /
115 (device->abs.max_y - device->abs.min_y) +
117 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
123 evdev_process_relative(struct evdev_input_device *device,
124 struct input_event *e, uint32_t time)
128 device->rel.dx += wl_fixed_from_int(e->value);
129 device->pending_events |= EVDEV_RELATIVE_MOTION;
132 device->rel.dy += wl_fixed_from_int(e->value);
133 device->pending_events |= EVDEV_RELATIVE_MOTION;
136 notify_axis(&device->master->base.seat,
138 WL_POINTER_AXIS_VERTICAL_SCROLL, e->value);
141 notify_axis(&device->master->base.seat,
143 WL_POINTER_AXIS_HORIZONTAL_SCROLL, e->value);
149 evdev_process_absolute(struct evdev_input_device *device,
150 struct input_event *e)
153 evdev_process_touch(device, e);
155 evdev_process_absolute_motion(device, e);
160 is_motion_event(struct input_event *e)
173 case ABS_MT_POSITION_X:
174 case ABS_MT_POSITION_Y:
183 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
185 struct weston_seat *master = &device->master->base;
187 if (!device->pending_events)
190 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
191 notify_motion(&master->seat, time,
192 master->seat.pointer->x + device->rel.dx,
193 master->seat.pointer->y + device->rel.dy);
194 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
198 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
199 notify_touch(&master->seat, time,
201 wl_fixed_from_int(device->mt.x[device->mt.slot]),
202 wl_fixed_from_int(device->mt.y[device->mt.slot]),
204 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
205 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
207 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
208 notify_touch(&master->seat, time,
210 wl_fixed_from_int(device->mt.x[device->mt.slot]),
211 wl_fixed_from_int(device->mt.y[device->mt.slot]),
213 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
214 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
216 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
217 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
219 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
221 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
222 notify_motion(&master->seat, time,
223 wl_fixed_from_int(device->abs.x),
224 wl_fixed_from_int(device->abs.y));
225 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
230 fallback_process(struct evdev_dispatch *dispatch,
231 struct evdev_input_device *device,
232 struct input_event *event,
235 switch (event->type) {
237 evdev_process_relative(device, event, time);
240 evdev_process_absolute(device, event);
243 evdev_process_key(device, event, time);
249 fallback_destroy(struct evdev_dispatch *dispatch)
254 struct evdev_dispatch_interface fallback_interface = {
259 static struct evdev_dispatch *
260 fallback_dispatch_create(void)
262 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
263 if (dispatch == NULL)
266 dispatch->interface = &fallback_interface;
272 evdev_process_events(struct evdev_input_device *device,
273 struct input_event *ev, int count)
275 struct evdev_dispatch *dispatch = device->dispatch;
276 struct input_event *e, *end;
279 device->pending_events = 0;
283 for (e = ev; e < end; e++) {
284 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
286 /* we try to minimize the amount of notifications to be
287 * forwarded to the compositor, so we accumulate motion
288 * events and send as a bunch */
289 if (!is_motion_event(e))
290 evdev_flush_motion(device, time);
292 dispatch->interface->process(dispatch, device, e, time);
295 evdev_flush_motion(device, time);
299 evdev_input_device_data(int fd, uint32_t mask, void *data)
301 struct weston_compositor *ec;
302 struct evdev_input_device *device = data;
303 struct input_event ev[32];
306 ec = device->master->base.compositor;
310 /* If the compositor is repainting, this function is called only once
311 * per frame and we have to process all the events available on the
312 * fd, otherwise there will be input lag. */
315 len = mtdev_get(device->mtdev, fd, ev,
317 sizeof (struct input_event);
319 len = read(fd, &ev, sizeof ev);
321 if (len < 0 || len % sizeof ev[0] != 0) {
322 /* FIXME: call device_removed when errno is ENODEV. */
326 evdev_process_events(device, ev, len / sizeof ev[0]);
334 evdev_configure_device(struct evdev_input_device *device)
336 struct input_absinfo absinfo;
337 unsigned long ev_bits[NBITS(EV_MAX)];
338 unsigned long abs_bits[NBITS(ABS_MAX)];
339 unsigned long key_bits[NBITS(KEY_MAX)];
340 int has_key, has_abs;
345 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
346 if (TEST_BIT(ev_bits, EV_ABS)) {
349 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
351 if (TEST_BIT(abs_bits, ABS_X)) {
352 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
353 device->abs.min_x = absinfo.minimum;
354 device->abs.max_x = absinfo.maximum;
356 if (TEST_BIT(abs_bits, ABS_Y)) {
357 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
358 device->abs.min_y = absinfo.minimum;
359 device->abs.max_y = absinfo.maximum;
361 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
366 if (TEST_BIT(ev_bits, EV_KEY)) {
368 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
370 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
371 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
373 device->dispatch = evdev_touchpad_create(device);
376 /* This rule tries to catch accelerometer devices and opt out. We may
377 * want to adjust the protocol later adding a proper event for dealing
378 * with accelerometers and implement here accordingly */
379 if (has_abs && !has_key)
385 static struct evdev_input_device *
386 evdev_input_device_create(struct evdev_seat *master,
387 struct wl_display *display, const char *path)
389 struct evdev_input_device *device;
390 struct weston_compositor *ec;
392 device = malloc(sizeof *device);
396 ec = master->base.compositor;
398 container_of(ec->output_list.next, struct weston_output, link);
400 device->master = master;
402 device->mtdev = NULL;
403 device->devnode = strdup(path);
404 device->mt.slot = -1;
407 device->dispatch = NULL;
409 /* Use non-blocking mode so that we can loop on read on
410 * evdev_input_device_data() until all events on the fd are
411 * read. mtdev_get() also expects this. */
412 device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
416 if (evdev_configure_device(device) == -1)
419 /* If the dispatch was not set up use the fallback. */
420 if (device->dispatch == NULL)
421 device->dispatch = fallback_dispatch_create();
422 if (device->dispatch == NULL)
427 device->mtdev = mtdev_new_open(device->fd);
429 fprintf(stderr, "mtdev failed to open for %s\n", path);
432 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
434 evdev_input_device_data, device);
435 if (device->source == NULL)
438 wl_list_insert(master->devices_list.prev, &device->link);
443 device->dispatch->interface->destroy(device->dispatch);
447 free(device->devnode);
452 static const char default_seat[] = "seat0";
455 device_added(struct udev_device *udev_device, struct evdev_seat *master)
457 struct weston_compositor *c;
459 const char *device_seat;
461 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
463 device_seat = default_seat;
465 if (strcmp(device_seat, master->seat_id))
468 c = master->base.compositor;
469 devnode = udev_device_get_devnode(udev_device);
470 evdev_input_device_create(master, c->wl_display, devnode);
474 device_removed(struct evdev_input_device *device)
476 struct evdev_dispatch *dispatch;
478 dispatch = device->dispatch;
480 dispatch->interface->destroy(dispatch);
482 wl_event_source_remove(device->source);
483 wl_list_remove(&device->link);
485 mtdev_close_delete(device->mtdev);
487 free(device->devnode);
492 evdev_notify_keyboard_focus(struct evdev_seat *seat)
494 struct evdev_input_device *device;
495 struct wl_array keys;
497 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
501 memset(all_keys, 0, sizeof all_keys);
502 wl_list_for_each(device, &seat->devices_list, link) {
503 memset(evdev_keys, 0, sizeof evdev_keys);
504 ret = ioctl(device->fd,
505 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
507 fprintf(stderr, "failed to get keys for device %s\n",
511 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
512 all_keys[i] |= evdev_keys[i];
515 wl_array_init(&keys);
516 for (i = 0; i < KEY_CNT; i++) {
517 set = all_keys[i >> 3] & (1 << (i & 7));
519 k = wl_array_add(&keys, sizeof *k);
524 notify_keyboard_focus(&seat->base.seat, &keys);
526 wl_array_release(&keys);
530 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
532 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
533 struct udev_enumerate *e;
534 struct udev_list_entry *entry;
535 struct udev_device *device;
536 const char *path, *sysname;
538 e = udev_enumerate_new(udev);
539 udev_enumerate_add_match_subsystem(e, "input");
540 udev_enumerate_scan_devices(e);
541 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
542 path = udev_list_entry_get_name(entry);
543 device = udev_device_new_from_syspath(udev, path);
545 sysname = udev_device_get_sysname(device);
546 if (strncmp("event", sysname, 5) != 0) {
547 udev_device_unref(device);
551 device_added(device, seat);
553 udev_device_unref(device);
555 udev_enumerate_unref(e);
557 evdev_notify_keyboard_focus(seat);
559 if (wl_list_empty(&seat->devices_list)) {
561 "warning: no input devices on entering Weston. "
563 "\t- no permissions to read /dev/input/event*\n"
564 "\t- seats misconfigured "
565 "(Weston backend option 'seat', "
566 "udev device property ID_SEAT)\n");
571 evdev_udev_handler(int fd, uint32_t mask, void *data)
573 struct evdev_seat *master = data;
574 struct udev_device *udev_device;
575 struct evdev_input_device *device, *next;
579 udev_device = udev_monitor_receive_device(master->udev_monitor);
583 action = udev_device_get_action(udev_device);
585 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
588 if (!strcmp(action, "add")) {
589 device_added(udev_device, master);
591 else if (!strcmp(action, "remove")) {
592 devnode = udev_device_get_devnode(udev_device);
593 wl_list_for_each_safe(device, next,
594 &master->devices_list, link)
595 if (!strcmp(device->devnode, devnode)) {
596 device_removed(device);
601 udev_device_unref(udev_device);
607 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
609 struct evdev_seat *master = (struct evdev_seat *) seat_base;
610 struct wl_event_loop *loop;
611 struct weston_compositor *c = master->base.compositor;
614 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
615 if (!master->udev_monitor) {
616 fprintf(stderr, "udev: failed to create the udev monitor\n");
620 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
623 if (udev_monitor_enable_receiving(master->udev_monitor)) {
624 fprintf(stderr, "udev: failed to bind the udev monitor\n");
625 udev_monitor_unref(master->udev_monitor);
629 loop = wl_display_get_event_loop(c->wl_display);
630 fd = udev_monitor_get_fd(master->udev_monitor);
631 master->udev_monitor_source =
632 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
633 evdev_udev_handler, master);
634 if (!master->udev_monitor_source) {
635 udev_monitor_unref(master->udev_monitor);
643 evdev_disable_udev_monitor(struct weston_seat *seat_base)
645 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
647 if (!seat->udev_monitor)
650 udev_monitor_unref(seat->udev_monitor);
651 seat->udev_monitor = NULL;
652 wl_event_source_remove(seat->udev_monitor_source);
653 seat->udev_monitor_source = NULL;
657 evdev_input_create(struct weston_compositor *c, struct udev *udev,
660 struct evdev_seat *seat;
662 seat = malloc(sizeof *seat);
666 memset(seat, 0, sizeof *seat);
667 weston_seat_init(&seat->base, c);
669 wl_list_init(&seat->devices_list);
670 seat->seat_id = strdup(seat_id);
671 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
677 evdev_add_devices(udev, &seat->base);
679 c->seat = &seat->base;
683 evdev_remove_devices(struct weston_seat *seat_base)
685 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
686 struct evdev_input_device *device, *next;
688 wl_list_for_each_safe(device, next, &seat->devices_list, link)
689 device_removed(device);
691 notify_keyboard_focus(&seat->base.seat, NULL);
695 evdev_input_destroy(struct weston_seat *seat_base)
697 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
699 evdev_remove_devices(seat_base);
700 evdev_disable_udev_monitor(&seat->base);
702 wl_list_remove(&seat->base.link);