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>
40 #include "libinput-private.h"
42 #define DEFAULT_AXIS_STEP_DISTANCE 10
45 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
48 enum libinput_led weston;
51 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
52 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
53 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
55 struct input_event ev[ARRAY_LENGTH(map) + 1];
58 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
61 memset(ev, 0, sizeof(ev));
62 for (i = 0; i < ARRAY_LENGTH(map); i++) {
64 ev[i].code = map[i].evdev;
65 ev[i].value = !!(leds & map[i].weston);
68 ev[i].code = SYN_REPORT;
70 i = write(device->fd, ev, sizeof ev);
71 (void)i; /* no, we really don't care about the return value */
75 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
77 if (!device->abs.apply_calibration) {
82 *x = device->abs.x * device->abs.calibration[0] +
83 device->abs.y * device->abs.calibration[1] +
84 device->abs.calibration[2];
86 *y = device->abs.x * device->abs.calibration[3] +
87 device->abs.y * device->abs.calibration[4] +
88 device->abs.calibration[5];
93 evdev_device_transform_x(struct evdev_device *device,
97 return (x - device->abs.min_x) * width /
98 (device->abs.max_x - device->abs.min_x + 1);
102 evdev_device_transform_y(struct evdev_device *device,
106 return (y - device->abs.min_y) * height /
107 (device->abs.max_y - device->abs.min_y + 1);
111 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
113 struct motion_params motion;
118 struct libinput_device *base = &device->base;
119 struct libinput_seat *seat = base->seat;
121 slot = device->mt.slot;
123 switch (device->pending_event) {
126 case EVDEV_RELATIVE_MOTION:
127 motion.dx = device->rel.dx;
128 motion.dy = device->rel.dy;
132 /* Apply pointer acceleration. */
133 filter_dispatch(device->pointer.filter, &motion, device, time);
135 if (motion.dx == 0.0 && motion.dy == 0.0)
138 pointer_notify_motion(base, time, motion.dx, motion.dy);
140 case EVDEV_ABSOLUTE_MT_DOWN:
141 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
144 if (device->mt.slots[slot].seat_slot != -1) {
145 log_bug_kernel("%s: Driver sent multiple touch down for the "
146 "same slot", device->devnode);
150 seat_slot = ffs(~seat->slot_map) - 1;
151 device->mt.slots[slot].seat_slot = seat_slot;
156 seat->slot_map |= 1 << seat_slot;
157 x = device->mt.slots[slot].x;
158 y = device->mt.slots[slot].y;
160 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
162 case EVDEV_ABSOLUTE_MT_MOTION:
163 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
166 seat_slot = device->mt.slots[slot].seat_slot;
167 x = device->mt.slots[slot].x;
168 y = device->mt.slots[slot].y;
173 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
175 case EVDEV_ABSOLUTE_MT_UP:
176 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
179 seat_slot = device->mt.slots[slot].seat_slot;
180 device->mt.slots[slot].seat_slot = -1;
185 seat->slot_map &= ~(1 << seat_slot);
187 touch_notify_touch_up(base, time, slot, seat_slot);
189 case EVDEV_ABSOLUTE_TOUCH_DOWN:
190 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
193 if (device->abs.seat_slot != -1) {
194 log_bug_kernel("%s: Driver sent multiple touch down for the "
195 "same slot", device->devnode);
199 seat_slot = ffs(~seat->slot_map) - 1;
200 device->abs.seat_slot = seat_slot;
205 seat->slot_map |= 1 << seat_slot;
207 transform_absolute(device, &cx, &cy);
209 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
211 case EVDEV_ABSOLUTE_MOTION:
212 transform_absolute(device, &cx, &cy);
216 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
217 seat_slot = device->abs.seat_slot;
222 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
223 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
224 pointer_notify_motion_absolute(base, time, x, y);
227 case EVDEV_ABSOLUTE_TOUCH_UP:
228 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
231 seat_slot = device->abs.seat_slot;
232 device->abs.seat_slot = -1;
237 seat->slot_map &= ~(1 << seat_slot);
239 touch_notify_touch_up(base, time, -1, seat_slot);
242 assert(0 && "Unknown pending event type");
246 device->pending_event = EVDEV_NONE;
250 evdev_process_touch_button(struct evdev_device *device, int time, int value)
252 if (device->pending_event != EVDEV_NONE &&
253 device->pending_event != EVDEV_ABSOLUTE_MOTION)
254 evdev_flush_pending_event(device, time);
256 device->pending_event = (value ?
257 EVDEV_ABSOLUTE_TOUCH_DOWN :
258 EVDEV_ABSOLUTE_TOUCH_UP);
262 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
264 /* ignore kernel key repeat */
268 if (e->code > KEY_MAX)
271 if (e->code == BTN_TOUCH) {
273 evdev_process_touch_button(device, time, e->value);
277 evdev_flush_pending_event(device, time);
288 pointer_notify_button(
292 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
293 LIBINPUT_BUTTON_STATE_RELEASED);
297 /* Only let KEY_* codes pass through. */
298 if (!(e->code <= KEY_MICMUTE ||
299 (e->code >= KEY_OK && e->code <= KEY_LIGHTS_TOGGLE)))
306 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
307 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
313 evdev_process_touch(struct evdev_device *device,
314 struct input_event *e,
319 evdev_flush_pending_event(device, time);
320 device->mt.slot = e->value;
322 case ABS_MT_TRACKING_ID:
323 if (device->pending_event != EVDEV_NONE &&
324 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
325 evdev_flush_pending_event(device, time);
327 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
329 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
331 case ABS_MT_POSITION_X:
332 device->mt.slots[device->mt.slot].x = e->value;
333 if (device->pending_event == EVDEV_NONE)
334 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
336 case ABS_MT_POSITION_Y:
337 device->mt.slots[device->mt.slot].y = e->value;
338 if (device->pending_event == EVDEV_NONE)
339 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
345 evdev_process_absolute_motion(struct evdev_device *device,
346 struct input_event *e)
350 device->abs.x = e->value;
351 if (device->pending_event == EVDEV_NONE)
352 device->pending_event = EVDEV_ABSOLUTE_MOTION;
355 device->abs.y = e->value;
356 if (device->pending_event == EVDEV_NONE)
357 device->pending_event = EVDEV_ABSOLUTE_MOTION;
363 evdev_process_relative(struct evdev_device *device,
364 struct input_event *e, uint64_t time)
366 struct libinput_device *base = &device->base;
370 if (device->pending_event != EVDEV_RELATIVE_MOTION)
371 evdev_flush_pending_event(device, time);
372 device->rel.dx += e->value;
373 device->pending_event = EVDEV_RELATIVE_MOTION;
376 if (device->pending_event != EVDEV_RELATIVE_MOTION)
377 evdev_flush_pending_event(device, time);
378 device->rel.dy += e->value;
379 device->pending_event = EVDEV_RELATIVE_MOTION;
382 evdev_flush_pending_event(device, time);
386 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
387 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
390 evdev_flush_pending_event(device, time);
399 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
400 e->value * DEFAULT_AXIS_STEP_DISTANCE);
410 evdev_process_absolute(struct evdev_device *device,
411 struct input_event *e,
415 evdev_process_touch(device, e, time);
417 evdev_process_absolute_motion(device, e);
422 evdev_need_touch_frame(struct evdev_device *device)
424 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
427 switch (device->pending_event) {
429 case EVDEV_RELATIVE_MOTION:
431 case EVDEV_ABSOLUTE_MT_DOWN:
432 case EVDEV_ABSOLUTE_MT_MOTION:
433 case EVDEV_ABSOLUTE_MT_UP:
434 case EVDEV_ABSOLUTE_TOUCH_DOWN:
435 case EVDEV_ABSOLUTE_TOUCH_UP:
436 case EVDEV_ABSOLUTE_MOTION:
444 fallback_process(struct evdev_dispatch *dispatch,
445 struct evdev_device *device,
446 struct input_event *event,
451 switch (event->type) {
453 evdev_process_relative(device, event, time);
456 evdev_process_absolute(device, event, time);
459 evdev_process_key(device, event, time);
462 need_frame = evdev_need_touch_frame(device);
463 evdev_flush_pending_event(device, time);
465 touch_notify_frame(&device->base, time);
471 fallback_destroy(struct evdev_dispatch *dispatch)
476 struct evdev_dispatch_interface fallback_interface = {
481 static struct evdev_dispatch *
482 fallback_dispatch_create(void)
484 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
485 if (dispatch == NULL)
488 dispatch->interface = &fallback_interface;
494 evdev_process_event(struct evdev_device *device, struct input_event *e)
496 struct evdev_dispatch *dispatch = device->dispatch;
497 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
499 dispatch->interface->process(dispatch, device, e, time);
503 evdev_device_dispatch_one(struct evdev_device *device,
504 struct input_event *ev)
506 if (!device->mtdev) {
507 evdev_process_event(device, ev);
509 mtdev_put_event(device->mtdev, ev);
510 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
511 while (!mtdev_empty(device->mtdev)) {
512 struct input_event e;
513 mtdev_get_event(device->mtdev, &e);
514 evdev_process_event(device, &e);
521 evdev_sync_device(struct evdev_device *device)
523 struct input_event ev;
527 rc = libevdev_next_event(device->evdev,
528 LIBEVDEV_READ_FLAG_SYNC, &ev);
531 evdev_device_dispatch_one(device, &ev);
532 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
534 return rc == -EAGAIN ? 0 : rc;
538 evdev_device_dispatch(void *data)
540 struct evdev_device *device = data;
541 struct libinput *libinput = device->base.seat->libinput;
542 struct input_event ev;
545 /* If the compositor is repainting, this function is called only once
546 * per frame and we have to process all the events available on the
547 * fd, otherwise there will be input lag. */
549 rc = libevdev_next_event(device->evdev,
550 LIBEVDEV_READ_FLAG_NORMAL, &ev);
551 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
552 /* send one more sync event so we handle all
553 currently pending events before we sync up
554 to the current state */
555 ev.code = SYN_REPORT;
556 evdev_device_dispatch_one(device, &ev);
558 rc = evdev_sync_device(device);
560 rc = LIBEVDEV_READ_STATUS_SUCCESS;
561 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
562 evdev_device_dispatch_one(device, &ev);
564 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
566 if (rc != -EAGAIN && rc != -EINTR) {
567 libinput_remove_source(libinput, device->source);
568 device->source = NULL;
573 configure_pointer_acceleration(struct evdev_device *device)
575 device->pointer.filter =
576 create_pointer_accelator_filter(
577 pointer_accel_profile_smooth_simple);
578 if (!device->pointer.filter)
585 evdev_configure_device(struct evdev_device *device)
587 struct libevdev *evdev = device->evdev;
588 const struct input_absinfo *absinfo;
589 int has_abs, has_rel, has_mt;
590 int has_button, has_keyboard, has_touch;
591 struct mt_slot *slots;
604 if (libevdev_has_event_type(evdev, EV_ABS)) {
606 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
607 device->abs.min_x = absinfo->minimum;
608 device->abs.max_x = absinfo->maximum;
611 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
612 device->abs.min_y = absinfo->minimum;
613 device->abs.max_y = absinfo->maximum;
616 /* We only handle the slotted Protocol B in weston.
617 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
618 require mtdev for conversion. */
619 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
620 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
621 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
622 device->abs.min_x = absinfo->minimum;
623 device->abs.max_x = absinfo->maximum;
624 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
625 device->abs.min_y = absinfo->minimum;
626 device->abs.max_y = absinfo->maximum;
631 if (!libevdev_has_event_code(evdev,
632 EV_ABS, ABS_MT_SLOT)) {
633 device->mtdev = mtdev_new_open(device->fd);
637 num_slots = device->mtdev->caps.slot.maximum;
638 if (device->mtdev->caps.slot.minimum < 0 ||
641 active_slot = device->mtdev->caps.slot.value;
643 num_slots = libevdev_get_num_slots(device->evdev);
644 active_slot = libevdev_get_current_slot(evdev);
647 slots = calloc(num_slots, sizeof(struct mt_slot));
651 for (slot = 0; slot < num_slots; ++slot) {
652 slots[slot].seat_slot = -1;
656 device->mt.slots = slots;
657 device->mt.slots_len = num_slots;
658 device->mt.slot = active_slot;
661 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
662 libevdev_has_event_code(evdev, EV_REL, REL_Y))
665 if (libevdev_has_event_type(evdev, EV_KEY)) {
666 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
667 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
668 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
669 (has_abs || has_mt)) {
670 device->dispatch = evdev_mt_touchpad_create(device);
671 log_info("input device '%s', %s is a touchpad\n",
672 device->devname, device->devnode);
674 for (i = KEY_ESC; i < KEY_MAX; i++) {
675 if (i >= BTN_MISC && i < KEY_OK)
677 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
682 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
684 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
685 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
691 if (libevdev_has_event_type(evdev, EV_LED))
694 if ((has_abs || has_rel) && has_button) {
695 if (configure_pointer_acceleration(device) == -1)
698 device->seat_caps |= EVDEV_DEVICE_POINTER;
700 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
701 device->devname, device->devnode,
702 has_abs ? " absolute-motion" : "",
703 has_rel ? " relative-motion": "",
704 has_button ? " button" : "");
707 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
708 log_info("input device '%s', %s is a keyboard\n",
709 device->devname, device->devnode);
711 if (has_touch && !has_button) {
712 device->seat_caps |= EVDEV_DEVICE_TOUCH;
713 log_info("input device '%s', %s is a touch device\n",
714 device->devname, device->devnode);
720 struct evdev_device *
721 evdev_device_create(struct libinput_seat *seat,
725 struct libinput *libinput = seat->libinput;
726 struct evdev_device *device;
729 int unhandled_device = 0;
731 /* Use non-blocking mode so that we can loop on read on
732 * evdev_device_data() until all events on the fd are
733 * read. mtdev_get() also expects this. */
734 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
736 log_info("opening input device '%s' failed (%s).\n",
737 devnode, strerror(-fd));
741 device = zalloc(sizeof *device);
745 libinput_device_init(&device->base, seat);
747 rc = libevdev_new_from_fd(fd, &device->evdev);
751 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
753 device->seat_caps = 0;
755 device->mtdev = NULL;
756 device->devnode = strdup(devnode);
757 device->sysname = strdup(sysname);
760 device->abs.seat_slot = -1;
761 device->dispatch = NULL;
763 device->pending_event = EVDEV_NONE;
764 device->devname = libevdev_get_name(device->evdev);
766 libinput_seat_ref(seat);
768 if (evdev_configure_device(device) == -1)
771 if (device->seat_caps == 0) {
772 unhandled_device = 1;
776 /* If the dispatch was not set up use the fallback. */
777 if (device->dispatch == NULL)
778 device->dispatch = fallback_dispatch_create();
779 if (device->dispatch == NULL)
783 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
787 list_insert(seat->devices_list.prev, &device->base.link);
788 notify_added_device(&device->base);
794 close_restricted(libinput, fd);
795 evdev_device_destroy(device);
797 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
801 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
805 memset(keys, 0, size);
806 len = ioctl(device->fd, EVIOCGKEY(size), keys);
808 return (len == -1) ? -errno : len;
812 evdev_device_get_output(struct evdev_device *device)
814 return device->output_name;
818 evdev_device_get_sysname(struct evdev_device *device)
820 return device->sysname;
824 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
826 device->abs.apply_calibration = 1;
827 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
831 evdev_device_has_capability(struct evdev_device *device,
832 enum libinput_device_capability capability)
834 switch (capability) {
835 case LIBINPUT_DEVICE_CAP_POINTER:
836 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
837 case LIBINPUT_DEVICE_CAP_KEYBOARD:
838 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
839 case LIBINPUT_DEVICE_CAP_TOUCH:
840 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
847 evdev_device_remove(struct evdev_device *device)
850 libinput_remove_source(device->base.seat->libinput,
854 mtdev_close_delete(device->mtdev);
855 close_restricted(device->base.seat->libinput, device->fd);
857 list_remove(&device->base.link);
859 notify_removed_device(&device->base);
860 libinput_device_unref(&device->base);
864 evdev_device_destroy(struct evdev_device *device)
866 struct evdev_dispatch *dispatch;
868 dispatch = device->dispatch;
870 dispatch->interface->destroy(dispatch);
872 motion_filter_destroy(device->pointer.filter);
873 libinput_seat_unref(device->base.seat);
874 libevdev_free(device->evdev);
875 free(device->mt.slots);
876 free(device->devnode);
877 free(device->sysname);