2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <linux/input.h>
32 #include <mtdev-plumbing.h>
38 #include "libinput-private.h"
40 #define DEFAULT_AXIS_STEP_DISTANCE li_fixed_from_int(10)
43 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
46 enum libinput_led weston;
49 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
50 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
51 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
53 struct input_event ev[ARRAY_LENGTH(map) + 1];
56 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
59 memset(ev, 0, sizeof(ev));
60 for (i = 0; i < ARRAY_LENGTH(map); i++) {
62 ev[i].code = map[i].evdev;
63 ev[i].value = !!(leds & map[i].weston);
66 ev[i].code = SYN_REPORT;
68 i = write(device->fd, ev, sizeof ev);
69 (void)i; /* no, we really don't care about the return value */
73 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
75 if (!device->abs.apply_calibration) {
80 *x = device->abs.x * device->abs.calibration[0] +
81 device->abs.y * device->abs.calibration[1] +
82 device->abs.calibration[2];
84 *y = device->abs.x * device->abs.calibration[3] +
85 device->abs.y * device->abs.calibration[4] +
86 device->abs.calibration[5];
91 evdev_device_transform_x(struct evdev_device *device,
95 return ((uint64_t)x - li_fixed_from_int(device->abs.min_x)) * width /
96 (device->abs.max_x - device->abs.min_x + 1);
100 evdev_device_transform_y(struct evdev_device *device,
104 return ((uint64_t)y - li_fixed_from_int(device->abs.min_y)) * height /
105 (device->abs.max_y - device->abs.min_y + 1);
109 evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
113 struct libinput_device *base = &device->base;
115 slot = device->mt.slot;
117 switch (device->pending_event) {
120 case EVDEV_RELATIVE_MOTION:
121 pointer_notify_motion(base,
128 case EVDEV_ABSOLUTE_MT_DOWN:
129 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
132 touch_notify_touch(base,
135 li_fixed_from_int(device->mt.slots[slot].x),
136 li_fixed_from_int(device->mt.slots[slot].y),
137 LIBINPUT_TOUCH_TYPE_DOWN);
139 case EVDEV_ABSOLUTE_MT_MOTION:
140 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
143 touch_notify_touch(base,
146 li_fixed_from_int(device->mt.slots[slot].x),
147 li_fixed_from_int(device->mt.slots[slot].y),
148 LIBINPUT_TOUCH_TYPE_MOTION);
150 case EVDEV_ABSOLUTE_MT_UP:
151 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
154 touch_notify_touch(base,
158 LIBINPUT_TOUCH_TYPE_UP);
160 case EVDEV_ABSOLUTE_TOUCH_DOWN:
161 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
164 transform_absolute(device, &cx, &cy);
165 touch_notify_touch(base,
168 li_fixed_from_int(cx),
169 li_fixed_from_int(cy),
170 LIBINPUT_TOUCH_TYPE_DOWN);
172 case EVDEV_ABSOLUTE_MOTION:
173 transform_absolute(device, &cx, &cy);
174 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
175 touch_notify_touch(base,
178 li_fixed_from_int(cx),
179 li_fixed_from_int(cy),
180 LIBINPUT_TOUCH_TYPE_DOWN);
181 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
182 pointer_notify_motion_absolute(base,
184 li_fixed_from_int(cx),
185 li_fixed_from_int(cy));
188 case EVDEV_ABSOLUTE_TOUCH_UP:
189 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
192 touch_notify_touch(base,
196 LIBINPUT_TOUCH_TYPE_UP);
199 assert(0 && "Unknown pending event type");
203 device->pending_event = EVDEV_NONE;
207 evdev_process_touch_button(struct evdev_device *device, int time, int value)
209 if (device->pending_event != EVDEV_NONE &&
210 device->pending_event != EVDEV_ABSOLUTE_MOTION)
211 evdev_flush_pending_event(device, time);
213 device->pending_event = (value ?
214 EVDEV_ABSOLUTE_TOUCH_DOWN :
215 EVDEV_ABSOLUTE_TOUCH_UP);
219 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
221 /* ignore kernel key repeat */
225 if (e->code == BTN_TOUCH) {
227 evdev_process_touch_button(device, time, e->value);
231 evdev_flush_pending_event(device, time);
242 pointer_notify_button(
246 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
247 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
255 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
256 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
262 evdev_process_touch(struct evdev_device *device,
263 struct input_event *e,
268 evdev_flush_pending_event(device, time);
269 device->mt.slot = e->value;
271 case ABS_MT_TRACKING_ID:
272 if (device->pending_event != EVDEV_NONE &&
273 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
274 evdev_flush_pending_event(device, time);
276 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
278 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
280 case ABS_MT_POSITION_X:
281 device->mt.slots[device->mt.slot].x = e->value;
282 if (device->pending_event == EVDEV_NONE)
283 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
285 case ABS_MT_POSITION_Y:
286 device->mt.slots[device->mt.slot].y = e->value;
287 if (device->pending_event == EVDEV_NONE)
288 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
294 evdev_process_absolute_motion(struct evdev_device *device,
295 struct input_event *e)
299 device->abs.x = e->value;
300 if (device->pending_event == EVDEV_NONE)
301 device->pending_event = EVDEV_ABSOLUTE_MOTION;
304 device->abs.y = e->value;
305 if (device->pending_event == EVDEV_NONE)
306 device->pending_event = EVDEV_ABSOLUTE_MOTION;
312 evdev_process_relative(struct evdev_device *device,
313 struct input_event *e, uint32_t time)
315 struct libinput_device *base = &device->base;
319 if (device->pending_event != EVDEV_RELATIVE_MOTION)
320 evdev_flush_pending_event(device, time);
321 device->rel.dx += li_fixed_from_int(e->value);
322 device->pending_event = EVDEV_RELATIVE_MOTION;
325 if (device->pending_event != EVDEV_RELATIVE_MOTION)
326 evdev_flush_pending_event(device, time);
327 device->rel.dy += li_fixed_from_int(e->value);
328 device->pending_event = EVDEV_RELATIVE_MOTION;
331 evdev_flush_pending_event(device, time);
335 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
336 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
339 evdev_flush_pending_event(device, time);
348 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
349 e->value * DEFAULT_AXIS_STEP_DISTANCE);
359 evdev_process_absolute(struct evdev_device *device,
360 struct input_event *e,
364 evdev_process_touch(device, e, time);
366 evdev_process_absolute_motion(device, e);
371 evdev_need_touch_frame(struct evdev_device *device)
373 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
376 switch (device->pending_event) {
378 case EVDEV_RELATIVE_MOTION:
380 case EVDEV_ABSOLUTE_MT_DOWN:
381 case EVDEV_ABSOLUTE_MT_MOTION:
382 case EVDEV_ABSOLUTE_MT_UP:
383 case EVDEV_ABSOLUTE_TOUCH_DOWN:
384 case EVDEV_ABSOLUTE_TOUCH_UP:
385 case EVDEV_ABSOLUTE_MOTION:
393 fallback_process(struct evdev_dispatch *dispatch,
394 struct evdev_device *device,
395 struct input_event *event,
400 switch (event->type) {
402 evdev_process_relative(device, event, time);
405 evdev_process_absolute(device, event, time);
408 evdev_process_key(device, event, time);
411 need_frame = evdev_need_touch_frame(device);
412 evdev_flush_pending_event(device, time);
414 touch_notify_frame(&device->base, time);
420 fallback_destroy(struct evdev_dispatch *dispatch)
425 struct evdev_dispatch_interface fallback_interface = {
430 static struct evdev_dispatch *
431 fallback_dispatch_create(void)
433 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
434 if (dispatch == NULL)
437 dispatch->interface = &fallback_interface;
443 evdev_process_event(struct evdev_device *device, struct input_event *e)
445 struct evdev_dispatch *dispatch = device->dispatch;
446 uint32_t time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
448 dispatch->interface->process(dispatch, device, e, time);
452 evdev_device_dispatch_one(struct evdev_device *device,
453 struct input_event *ev)
455 if (!device->mtdev) {
456 evdev_process_event(device, ev);
458 mtdev_put_event(device->mtdev, ev);
459 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
460 while (!mtdev_empty(device->mtdev)) {
461 struct input_event e;
462 mtdev_get_event(device->mtdev, &e);
463 evdev_process_event(device, &e);
470 evdev_sync_device(struct evdev_device *device)
472 struct input_event ev;
476 rc = libevdev_next_event(device->evdev,
477 LIBEVDEV_READ_FLAG_SYNC, &ev);
480 evdev_device_dispatch_one(device, &ev);
481 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
483 return rc == -EAGAIN ? 0 : rc;
487 evdev_device_dispatch(void *data)
489 struct evdev_device *device = data;
490 struct libinput *libinput = device->base.seat->libinput;
491 struct input_event ev;
494 /* If the compositor is repainting, this function is called only once
495 * per frame and we have to process all the events available on the
496 * fd, otherwise there will be input lag. */
498 rc = libevdev_next_event(device->evdev,
499 LIBEVDEV_READ_FLAG_NORMAL, &ev);
500 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
501 /* send one more sync event so we handle all
502 currently pending events before we sync up
503 to the current state */
504 ev.code = SYN_REPORT;
505 evdev_device_dispatch_one(device, &ev);
507 rc = evdev_sync_device(device);
509 rc = LIBEVDEV_READ_STATUS_SUCCESS;
510 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
511 evdev_device_dispatch_one(device, &ev);
513 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
515 if (rc != -EAGAIN && rc != -EINTR) {
516 libinput_remove_source(libinput, device->source);
517 device->source = NULL;
522 evdev_configure_device(struct evdev_device *device)
524 const struct input_absinfo *absinfo;
525 int has_abs, has_rel, has_mt;
526 int has_button, has_keyboard, has_touch;
536 if (libevdev_has_event_type(device->evdev, EV_ABS)) {
538 if ((absinfo = libevdev_get_abs_info(device->evdev, ABS_X))) {
539 device->abs.min_x = absinfo->minimum;
540 device->abs.max_x = absinfo->maximum;
543 if ((absinfo = libevdev_get_abs_info(device->evdev, ABS_Y))) {
544 device->abs.min_y = absinfo->minimum;
545 device->abs.max_y = absinfo->maximum;
548 /* We only handle the slotted Protocol B in weston.
549 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
550 require mtdev for conversion. */
551 if (libevdev_has_event_code(device->evdev, EV_ABS, ABS_MT_POSITION_X) &&
552 libevdev_has_event_code(device->evdev, EV_ABS, ABS_MT_POSITION_Y)) {
553 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_POSITION_X);
554 device->abs.min_x = absinfo->minimum;
555 device->abs.max_x = absinfo->maximum;
556 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_POSITION_Y);
557 device->abs.min_y = absinfo->minimum;
558 device->abs.max_y = absinfo->maximum;
563 if (!libevdev_has_event_code(device->evdev, EV_ABS, ABS_MT_SLOT)) {
564 device->mtdev = mtdev_new_open(device->fd);
567 device->mt.slot = device->mtdev->caps.slot.value;
569 device->mt.slot = libevdev_get_current_slot(device->evdev);
573 if (libevdev_has_event_code(device->evdev, EV_REL, REL_X) ||
574 libevdev_has_event_code(device->evdev, EV_REL, REL_Y))
577 if (libevdev_has_event_type(device->evdev, EV_KEY)) {
578 if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_TOOL_FINGER) &&
579 !libevdev_has_event_code(device->evdev, EV_KEY, BTN_TOOL_PEN) &&
580 (has_abs || has_mt)) {
581 device->dispatch = evdev_touchpad_create(device);
583 for (i = KEY_ESC; i < KEY_MAX; i++) {
584 if (i >= BTN_MISC && i < KEY_OK)
586 if (libevdev_has_event_code(device->evdev, EV_KEY, i)) {
591 if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_TOUCH))
593 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
594 if (libevdev_has_event_code(device->evdev, EV_KEY, i)) {
600 if (libevdev_has_event_type(device->evdev, EV_LED))
603 if ((has_abs || has_rel) && has_button)
604 device->seat_caps |= EVDEV_DEVICE_POINTER;
606 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
607 if (has_touch && !has_button)
608 device->seat_caps |= EVDEV_DEVICE_TOUCH;
613 struct evdev_device *
614 evdev_device_create(struct libinput_seat *seat,
618 struct libinput *libinput = seat->libinput;
619 struct evdev_device *device;
622 int unhandled_device = 0;
624 /* Use non-blocking mode so that we can loop on read on
625 * evdev_device_data() until all events on the fd are
626 * read. mtdev_get() also expects this. */
627 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
629 log_info("opening input device '%s' failed (%s).\n",
630 devnode, strerror(-fd));
634 device = zalloc(sizeof *device);
638 libinput_device_init(&device->base, seat);
640 rc = libevdev_new_from_fd(fd, &device->evdev);
644 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
646 device->seat_caps = 0;
648 device->mtdev = NULL;
649 device->devnode = strdup(devnode);
650 device->sysname = strdup(sysname);
651 device->mt.slot = -1;
654 device->dispatch = NULL;
656 device->pending_event = EVDEV_NONE;
657 device->devname = libevdev_get_name(device->evdev);
659 libinput_seat_ref(seat);
661 if (evdev_configure_device(device) == -1)
664 if (device->seat_caps == 0) {
665 unhandled_device = 1;
669 /* If the dispatch was not set up use the fallback. */
670 if (device->dispatch == NULL)
671 device->dispatch = fallback_dispatch_create();
672 if (device->dispatch == NULL)
676 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
680 list_insert(seat->devices_list.prev, &device->base.link);
681 notify_added_device(&device->base);
687 close_restricted(libinput, fd);
688 evdev_device_destroy(device);
690 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
694 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
696 memset(keys, 0, size);
697 return ioctl(device->fd, EVIOCGKEY(size), keys);
701 evdev_device_get_output(struct evdev_device *device)
703 return device->output_name;
707 evdev_device_get_sysname(struct evdev_device *device)
709 return device->sysname;
713 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
715 device->abs.apply_calibration = 1;
716 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
720 evdev_device_has_capability(struct evdev_device *device,
721 enum libinput_device_capability capability)
723 switch (capability) {
724 case LIBINPUT_DEVICE_CAP_POINTER:
725 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
726 case LIBINPUT_DEVICE_CAP_KEYBOARD:
727 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
728 case LIBINPUT_DEVICE_CAP_TOUCH:
729 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
736 evdev_device_remove(struct evdev_device *device)
739 libinput_remove_source(device->base.seat->libinput,
743 mtdev_close_delete(device->mtdev);
744 close_restricted(device->base.seat->libinput, device->fd);
746 list_remove(&device->base.link);
748 notify_removed_device(&device->base);
749 libinput_device_unref(&device->base);
753 evdev_device_destroy(struct evdev_device *device)
755 struct evdev_dispatch *dispatch;
757 dispatch = device->dispatch;
759 dispatch->interface->destroy(dispatch);
761 libinput_seat_unref(device->base.seat);
762 libevdev_free(device->evdev);
763 free(device->devnode);
764 free(device->sysname);