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.
25 #include <linux/input.h>
30 #include "compositor.h"
33 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
36 evdev_led_update(struct evdev_device *device, enum weston_led leds)
39 enum weston_led weston;
42 { LED_NUM_LOCK, LED_NUML },
43 { LED_CAPS_LOCK, LED_CAPSL },
44 { LED_SCROLL_LOCK, LED_SCROLLL },
46 struct input_event ev[ARRAY_LENGTH(map)];
49 if (!device->caps & EVDEV_KEYBOARD)
52 memset(ev, 0, sizeof(ev));
53 for (i = 0; i < ARRAY_LENGTH(map); i++) {
55 ev[i].code = map[i].evdev;
56 ev[i].value = !!(leds & map[i].weston);
59 i = write(device->fd, ev, sizeof ev);
60 (void)i; /* no, we really don't care about the return value */
64 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
78 notify_button(device->seat,
80 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
81 WL_POINTER_BUTTON_STATE_RELEASED);
85 notify_key(device->seat,
87 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
88 WL_KEYBOARD_KEY_STATE_RELEASED,
89 STATE_UPDATE_AUTOMATIC);
95 evdev_process_touch(struct evdev_device *device, struct input_event *e)
97 const int screen_width = device->output->current->width;
98 const int screen_height = device->output->current->height;
102 device->mt.slot = e->value;
104 case ABS_MT_TRACKING_ID:
106 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
108 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
110 case ABS_MT_POSITION_X:
111 device->mt.x[device->mt.slot] =
112 (e->value - device->abs.min_x) * screen_width /
113 (device->abs.max_x - device->abs.min_x) +
115 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
117 case ABS_MT_POSITION_Y:
118 device->mt.y[device->mt.slot] =
119 (e->value - device->abs.min_y) * screen_height /
120 (device->abs.max_y - device->abs.min_y) +
122 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
128 evdev_process_absolute_motion(struct evdev_device *device,
129 struct input_event *e)
131 const int screen_width = device->output->current->width;
132 const int screen_height = device->output->current->height;
137 (e->value - device->abs.min_x) * screen_width /
138 (device->abs.max_x - device->abs.min_x) +
140 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
144 (e->value - device->abs.min_y) * screen_height /
145 (device->abs.max_y - device->abs.min_y) +
147 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
153 evdev_process_relative(struct evdev_device *device,
154 struct input_event *e, uint32_t time)
158 device->rel.dx += wl_fixed_from_int(e->value);
159 device->pending_events |= EVDEV_RELATIVE_MOTION;
162 device->rel.dy += wl_fixed_from_int(e->value);
163 device->pending_events |= EVDEV_RELATIVE_MOTION;
171 notify_axis(device->seat,
173 WL_POINTER_AXIS_VERTICAL_SCROLL,
174 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
186 notify_axis(device->seat,
188 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
189 e->value * DEFAULT_AXIS_STEP_DISTANCE);
199 evdev_process_absolute(struct evdev_device *device, struct input_event *e)
202 evdev_process_touch(device, e);
204 evdev_process_absolute_motion(device, e);
209 is_motion_event(struct input_event *e)
223 case ABS_MT_POSITION_X:
224 case ABS_MT_POSITION_Y:
233 evdev_flush_motion(struct evdev_device *device, uint32_t time)
235 struct weston_seat *master = device->seat;
237 if (!(device->pending_events & EVDEV_SYN))
240 device->pending_events &= ~EVDEV_SYN;
241 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
242 notify_motion(master, time,
243 master->seat.pointer->x + device->rel.dx,
244 master->seat.pointer->y + device->rel.dy);
245 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
249 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
250 notify_touch(master, time,
252 wl_fixed_from_int(device->mt.x[device->mt.slot]),
253 wl_fixed_from_int(device->mt.y[device->mt.slot]),
255 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
256 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
258 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
259 notify_touch(master, time,
261 wl_fixed_from_int(device->mt.x[device->mt.slot]),
262 wl_fixed_from_int(device->mt.y[device->mt.slot]),
264 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
265 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
267 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
268 notify_touch(master, time, device->mt.slot, 0, 0,
270 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
272 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
273 notify_motion(master, time,
274 wl_fixed_from_int(device->abs.x),
275 wl_fixed_from_int(device->abs.y));
276 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
281 fallback_process(struct evdev_dispatch *dispatch,
282 struct evdev_device *device,
283 struct input_event *event,
286 switch (event->type) {
288 evdev_process_relative(device, event, time);
291 evdev_process_absolute(device, event);
294 evdev_process_key(device, event, time);
297 device->pending_events |= EVDEV_SYN;
303 fallback_destroy(struct evdev_dispatch *dispatch)
308 struct evdev_dispatch_interface fallback_interface = {
313 static struct evdev_dispatch *
314 fallback_dispatch_create(void)
316 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
317 if (dispatch == NULL)
320 dispatch->interface = &fallback_interface;
326 evdev_process_events(struct evdev_device *device,
327 struct input_event *ev, int count)
329 struct evdev_dispatch *dispatch = device->dispatch;
330 struct input_event *e, *end;
333 device->pending_events = 0;
337 for (e = ev; e < end; e++) {
338 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
340 /* we try to minimize the amount of notifications to be
341 * forwarded to the compositor, so we accumulate motion
342 * events and send as a bunch */
343 if (!is_motion_event(e))
344 evdev_flush_motion(device, time);
346 dispatch->interface->process(dispatch, device, e, time);
349 evdev_flush_motion(device, time);
353 evdev_device_data(int fd, uint32_t mask, void *data)
355 struct weston_compositor *ec;
356 struct evdev_device *device = data;
357 struct input_event ev[32];
360 ec = device->seat->compositor;
364 /* If the compositor is repainting, this function is called only once
365 * per frame and we have to process all the events available on the
366 * fd, otherwise there will be input lag. */
369 len = mtdev_get(device->mtdev, fd, ev,
371 sizeof (struct input_event);
373 len = read(fd, &ev, sizeof ev);
375 if (len < 0 || len % sizeof ev[0] != 0) {
376 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
380 evdev_process_events(device, ev, len / sizeof ev[0]);
388 evdev_handle_device(struct evdev_device *device)
390 struct input_absinfo absinfo;
391 unsigned long ev_bits[NBITS(EV_MAX)];
392 unsigned long abs_bits[NBITS(ABS_MAX)];
393 unsigned long rel_bits[NBITS(REL_MAX)];
394 unsigned long key_bits[NBITS(KEY_MAX)];
395 int has_key, has_abs;
402 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
403 if (TEST_BIT(ev_bits, EV_ABS)) {
406 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
408 if (TEST_BIT(abs_bits, ABS_X)) {
409 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
410 device->abs.min_x = absinfo.minimum;
411 device->abs.max_x = absinfo.maximum;
412 device->caps |= EVDEV_MOTION_ABS;
414 if (TEST_BIT(abs_bits, ABS_Y)) {
415 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
416 device->abs.min_y = absinfo.minimum;
417 device->abs.max_y = absinfo.maximum;
418 device->caps |= EVDEV_MOTION_ABS;
420 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
421 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
423 device->abs.min_x = absinfo.minimum;
424 device->abs.max_x = absinfo.maximum;
425 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
427 device->abs.min_y = absinfo.minimum;
428 device->abs.max_y = absinfo.maximum;
431 device->caps |= EVDEV_TOUCH;
434 if (TEST_BIT(ev_bits, EV_REL)) {
435 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
437 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
438 device->caps |= EVDEV_MOTION_REL;
440 if (TEST_BIT(ev_bits, EV_KEY)) {
442 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
444 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
445 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
447 device->dispatch = evdev_touchpad_create(device);
448 for (i = KEY_ESC; i < KEY_MAX; i++) {
449 if (i >= BTN_MISC && i < KEY_OK)
451 if (TEST_BIT(key_bits, i)) {
452 device->caps |= EVDEV_KEYBOARD;
456 for (i = BTN_MISC; i < KEY_OK; i++) {
457 if (TEST_BIT(key_bits, i)) {
458 device->caps |= EVDEV_BUTTON;
463 if (TEST_BIT(ev_bits, EV_LED)) {
464 device->caps |= EVDEV_KEYBOARD;
467 /* This rule tries to catch accelerometer devices and opt out. We may
468 * want to adjust the protocol later adding a proper event for dealing
469 * with accelerometers and implement here accordingly */
470 if (has_abs && !has_key && !device->is_mt) {
471 weston_log("input device %s, %s "
472 "ignored: unsupported device type\n",
473 device->devname, device->devnode);
481 evdev_configure_device(struct evdev_device *device)
484 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
485 weston_seat_init_pointer(device->seat);
486 weston_log("input device %s, %s is a pointer\n",
487 device->devname, device->devnode);
489 if ((device->caps & EVDEV_KEYBOARD)) {
490 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
492 weston_log("input device %s, %s is a keyboard\n",
493 device->devname, device->devnode);
495 if ((device->caps & EVDEV_TOUCH)) {
496 weston_seat_init_touch(device->seat);
497 weston_log("input device %s, %s is a touch device\n",
498 device->devname, device->devnode);
504 struct evdev_device *
505 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
507 struct evdev_device *device;
508 struct weston_compositor *ec;
509 char devname[256] = "unknown";
511 device = malloc(sizeof *device);
514 memset(device, 0, sizeof *device);
516 ec = seat->compositor;
518 container_of(ec->output_list.next, struct weston_output, link);
522 device->mtdev = NULL;
523 device->devnode = strdup(path);
524 device->mt.slot = -1;
527 device->dispatch = NULL;
528 device->fd = device_fd;
530 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
531 device->devname = strdup(devname);
533 if (!evdev_handle_device(device)) {
534 free(device->devnode);
535 free(device->devname);
537 return EVDEV_UNHANDLED_DEVICE;
540 if (evdev_configure_device(device) == -1)
543 /* If the dispatch was not set up use the fallback. */
544 if (device->dispatch == NULL)
545 device->dispatch = fallback_dispatch_create();
546 if (device->dispatch == NULL)
551 device->mtdev = mtdev_new_open(device->fd);
553 weston_log("mtdev failed to open for %s\n", path);
556 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
558 evdev_device_data, device);
559 if (device->source == NULL)
565 device->dispatch->interface->destroy(device->dispatch);
567 free(device->devname);
568 free(device->devnode);
574 evdev_device_destroy(struct evdev_device *device)
576 struct evdev_dispatch *dispatch;
578 dispatch = device->dispatch;
580 dispatch->interface->destroy(dispatch);
582 wl_event_source_remove(device->source);
583 wl_list_remove(&device->link);
585 mtdev_close_delete(device->mtdev);
587 free(device->devname);
588 free(device->devnode);
593 evdev_notify_keyboard_focus(struct weston_seat *seat,
594 struct wl_list *evdev_devices)
596 struct evdev_device *device;
597 struct wl_array keys;
599 char evdev_keys[(KEY_CNT + 7) / 8];
600 char all_keys[(KEY_CNT + 7) / 8];
604 if (!seat->seat.keyboard)
607 memset(all_keys, 0, sizeof all_keys);
608 wl_list_for_each(device, evdev_devices, link) {
609 memset(evdev_keys, 0, sizeof evdev_keys);
610 ret = ioctl(device->fd,
611 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
613 weston_log("failed to get keys for device %s\n",
617 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
618 all_keys[i] |= evdev_keys[i];
621 wl_array_init(&keys);
622 for (i = 0; i < KEY_CNT; i++) {
623 set = all_keys[i >> 3] & (1 << (i & 7));
625 k = wl_array_add(&keys, sizeof *k);
630 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
632 wl_array_release(&keys);