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"
34 evdev_led_update(struct evdev_device *device, enum weston_led leds)
37 enum weston_led weston;
40 { LED_NUM_LOCK, LED_NUML },
41 { LED_CAPS_LOCK, LED_CAPSL },
42 { LED_SCROLL_LOCK, LED_SCROLLL },
44 struct input_event ev[ARRAY_LENGTH(map)];
47 if (!device->caps & EVDEV_KEYBOARD)
50 memset(ev, 0, sizeof(ev));
51 for (i = 0; i < ARRAY_LENGTH(map); i++) {
53 ev[i].code = map[i].evdev;
54 ev[i].value = !!(leds & map[i].weston);
57 i = write(device->fd, ev, sizeof ev);
58 (void)i; /* no, we really don't care about the return value */
62 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
76 notify_button(&device->seat->seat,
78 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
79 WL_POINTER_BUTTON_STATE_RELEASED);
83 notify_key(&device->seat->seat,
85 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
86 WL_KEYBOARD_KEY_STATE_RELEASED,
87 STATE_UPDATE_AUTOMATIC);
93 evdev_process_touch(struct evdev_device *device, struct input_event *e)
95 const int screen_width = device->output->current->width;
96 const int screen_height = device->output->current->height;
100 device->mt.slot = e->value;
102 case ABS_MT_TRACKING_ID:
104 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
106 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
108 case ABS_MT_POSITION_X:
109 device->mt.x[device->mt.slot] =
110 (e->value - device->abs.min_x) * screen_width /
111 (device->abs.max_x - device->abs.min_x) +
113 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
115 case ABS_MT_POSITION_Y:
116 device->mt.y[device->mt.slot] =
117 (e->value - device->abs.min_y) * screen_height /
118 (device->abs.max_y - device->abs.min_y) +
120 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
126 evdev_process_absolute_motion(struct evdev_device *device,
127 struct input_event *e)
129 const int screen_width = device->output->current->width;
130 const int screen_height = device->output->current->height;
135 (e->value - device->abs.min_x) * screen_width /
136 (device->abs.max_x - device->abs.min_x) +
138 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
142 (e->value - device->abs.min_y) * screen_height /
143 (device->abs.max_y - device->abs.min_y) +
145 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
151 evdev_process_relative(struct evdev_device *device,
152 struct input_event *e, uint32_t time)
156 device->rel.dx += wl_fixed_from_int(e->value);
157 device->pending_events |= EVDEV_RELATIVE_MOTION;
160 device->rel.dy += wl_fixed_from_int(e->value);
161 device->pending_events |= EVDEV_RELATIVE_MOTION;
164 notify_axis(&device->seat->seat,
166 WL_POINTER_AXIS_VERTICAL_SCROLL,
167 wl_fixed_from_int(e->value));
170 notify_axis(&device->seat->seat,
172 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
173 wl_fixed_from_int(e->value));
179 evdev_process_absolute(struct evdev_device *device, struct input_event *e)
182 evdev_process_touch(device, e);
184 evdev_process_absolute_motion(device, e);
189 is_motion_event(struct input_event *e)
202 case ABS_MT_POSITION_X:
203 case ABS_MT_POSITION_Y:
212 evdev_flush_motion(struct evdev_device *device, uint32_t time)
214 struct weston_seat *master = device->seat;
216 if (!device->pending_events)
219 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
220 notify_motion(&master->seat, time,
221 master->seat.pointer->x + device->rel.dx,
222 master->seat.pointer->y + device->rel.dy);
223 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
227 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
228 notify_touch(&master->seat, time,
230 wl_fixed_from_int(device->mt.x[device->mt.slot]),
231 wl_fixed_from_int(device->mt.y[device->mt.slot]),
233 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
234 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
236 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
237 notify_touch(&master->seat, time,
239 wl_fixed_from_int(device->mt.x[device->mt.slot]),
240 wl_fixed_from_int(device->mt.y[device->mt.slot]),
242 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
243 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
245 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
246 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
248 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
250 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
251 notify_motion(&master->seat, time,
252 wl_fixed_from_int(device->abs.x),
253 wl_fixed_from_int(device->abs.y));
254 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
259 fallback_process(struct evdev_dispatch *dispatch,
260 struct evdev_device *device,
261 struct input_event *event,
264 switch (event->type) {
266 evdev_process_relative(device, event, time);
269 evdev_process_absolute(device, event);
272 evdev_process_key(device, event, time);
278 fallback_destroy(struct evdev_dispatch *dispatch)
283 struct evdev_dispatch_interface fallback_interface = {
288 static struct evdev_dispatch *
289 fallback_dispatch_create(void)
291 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
292 if (dispatch == NULL)
295 dispatch->interface = &fallback_interface;
301 evdev_process_events(struct evdev_device *device,
302 struct input_event *ev, int count)
304 struct evdev_dispatch *dispatch = device->dispatch;
305 struct input_event *e, *end;
308 device->pending_events = 0;
312 for (e = ev; e < end; e++) {
313 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
315 /* we try to minimize the amount of notifications to be
316 * forwarded to the compositor, so we accumulate motion
317 * events and send as a bunch */
318 if (!is_motion_event(e))
319 evdev_flush_motion(device, time);
321 dispatch->interface->process(dispatch, device, e, time);
324 evdev_flush_motion(device, time);
328 evdev_device_data(int fd, uint32_t mask, void *data)
330 struct weston_compositor *ec;
331 struct evdev_device *device = data;
332 struct input_event ev[32];
335 ec = device->seat->compositor;
339 /* If the compositor is repainting, this function is called only once
340 * per frame and we have to process all the events available on the
341 * fd, otherwise there will be input lag. */
344 len = mtdev_get(device->mtdev, fd, ev,
346 sizeof (struct input_event);
348 len = read(fd, &ev, sizeof ev);
350 if (len < 0 || len % sizeof ev[0] != 0) {
351 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
355 evdev_process_events(device, ev, len / sizeof ev[0]);
363 evdev_configure_device(struct evdev_device *device)
365 struct input_absinfo absinfo;
366 unsigned long ev_bits[NBITS(EV_MAX)];
367 unsigned long abs_bits[NBITS(ABS_MAX)];
368 unsigned long rel_bits[NBITS(REL_MAX)];
369 unsigned long key_bits[NBITS(KEY_MAX)];
370 int has_key, has_abs;
377 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
378 if (TEST_BIT(ev_bits, EV_ABS)) {
381 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
383 if (TEST_BIT(abs_bits, ABS_X)) {
384 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
385 device->abs.min_x = absinfo.minimum;
386 device->abs.max_x = absinfo.maximum;
387 device->caps |= EVDEV_MOTION_ABS;
389 if (TEST_BIT(abs_bits, ABS_Y)) {
390 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
391 device->abs.min_y = absinfo.minimum;
392 device->abs.max_y = absinfo.maximum;
393 device->caps |= EVDEV_MOTION_ABS;
395 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
396 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
398 device->abs.min_x = absinfo.minimum;
399 device->abs.max_x = absinfo.maximum;
400 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
402 device->abs.min_y = absinfo.minimum;
403 device->abs.max_y = absinfo.maximum;
406 device->caps |= EVDEV_TOUCH;
409 if (TEST_BIT(ev_bits, EV_REL)) {
410 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
412 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
413 device->caps |= EVDEV_MOTION_REL;
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) &&
422 device->dispatch = evdev_touchpad_create(device);
423 for (i = KEY_ESC; i < KEY_MAX; i++) {
424 if (i >= BTN_MISC && i < KEY_OK)
426 if (TEST_BIT(key_bits, i)) {
427 device->caps |= EVDEV_KEYBOARD;
431 for (i = BTN_MISC; i < KEY_OK; i++) {
432 if (TEST_BIT(key_bits, i)) {
433 device->caps |= EVDEV_BUTTON;
438 if (TEST_BIT(ev_bits, EV_LED)) {
439 device->caps |= EVDEV_KEYBOARD;
442 /* This rule tries to catch accelerometer devices and opt out. We may
443 * want to adjust the protocol later adding a proper event for dealing
444 * with accelerometers and implement here accordingly */
445 if (has_abs && !has_key && !device->is_mt) {
446 weston_log("input device %s, %s "
447 "ignored: unsupported device type\n",
448 device->devname, device->devnode);
453 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
454 weston_seat_init_pointer(device->seat);
455 weston_log("input device %s, %s is a pointer\n",
456 device->devname, device->devnode);
458 if ((device->caps & EVDEV_KEYBOARD)) {
459 weston_seat_init_keyboard(device->seat, NULL);
460 weston_log("input device %s, %s is a keyboard\n",
461 device->devname, device->devnode);
463 if ((device->caps & EVDEV_TOUCH)) {
464 weston_seat_init_touch(device->seat);
465 weston_log("input device %s, %s is a touch device\n",
466 device->devname, device->devnode);
472 struct evdev_device *
473 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
475 struct evdev_device *device;
476 struct weston_compositor *ec;
477 char devname[256] = "unknown";
479 device = malloc(sizeof *device);
482 memset(device, 0, sizeof *device);
484 ec = seat->compositor;
486 container_of(ec->output_list.next, struct weston_output, link);
490 device->mtdev = NULL;
491 device->devnode = strdup(path);
492 device->mt.slot = -1;
495 device->dispatch = NULL;
496 device->fd = device_fd;
498 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
499 device->devname = strdup(devname);
501 if (evdev_configure_device(device) == -1)
504 /* If the dispatch was not set up use the fallback. */
505 if (device->dispatch == NULL)
506 device->dispatch = fallback_dispatch_create();
507 if (device->dispatch == NULL)
512 device->mtdev = mtdev_new_open(device->fd);
514 weston_log("mtdev failed to open for %s\n", path);
517 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
519 evdev_device_data, device);
520 if (device->source == NULL)
526 device->dispatch->interface->destroy(device->dispatch);
528 free(device->devname);
529 free(device->devnode);
535 evdev_device_destroy(struct evdev_device *device)
537 struct evdev_dispatch *dispatch;
539 dispatch = device->dispatch;
541 dispatch->interface->destroy(dispatch);
543 wl_event_source_remove(device->source);
544 wl_list_remove(&device->link);
546 mtdev_close_delete(device->mtdev);
548 free(device->devname);
549 free(device->devnode);
554 evdev_notify_keyboard_focus(struct weston_seat *seat,
555 struct wl_list *evdev_devices)
557 struct evdev_device *device;
558 struct wl_array keys;
560 char evdev_keys[(KEY_CNT + 7) / 8];
561 char all_keys[(KEY_CNT + 7) / 8];
565 if (!seat->seat.keyboard)
568 memset(all_keys, 0, sizeof all_keys);
569 wl_list_for_each(device, evdev_devices, link) {
570 memset(evdev_keys, 0, sizeof evdev_keys);
571 ret = ioctl(device->fd,
572 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
574 weston_log("failed to get keys for device %s\n",
578 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
579 all_keys[i] |= evdev_keys[i];
582 wl_array_init(&keys);
583 for (i = 0; i < KEY_CNT; i++) {
584 set = all_keys[i >> 3] & (1 << (i & 7));
586 k = wl_array_add(&keys, sizeof *k);
591 notify_keyboard_focus_in(&seat->seat, &keys, STATE_UPDATE_AUTOMATIC);
593 wl_array_release(&keys);