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 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
95 return (val - absinfo->minimum) * to_range /
96 (absinfo->maximum - absinfo->minimum + 1);
100 evdev_device_transform_x(struct evdev_device *device,
104 return scale_axis(device->abs.absinfo_x, x, width);
108 evdev_device_transform_y(struct evdev_device *device,
112 return scale_axis(device->abs.absinfo_y, y, height);
116 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
118 struct motion_params motion;
123 struct libinput_device *base = &device->base;
124 struct libinput_seat *seat = base->seat;
126 slot = device->mt.slot;
128 switch (device->pending_event) {
131 case EVDEV_RELATIVE_MOTION:
132 motion.dx = device->rel.dx;
133 motion.dy = device->rel.dy;
137 /* Apply pointer acceleration. */
138 filter_dispatch(device->pointer.filter, &motion, device, time);
140 if (motion.dx == 0.0 && motion.dy == 0.0)
143 pointer_notify_motion(base, time, motion.dx, motion.dy);
145 case EVDEV_ABSOLUTE_MT_DOWN:
146 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
149 if (device->mt.slots[slot].seat_slot != -1) {
150 log_bug_kernel("%s: Driver sent multiple touch down for the "
151 "same slot", device->devnode);
155 seat_slot = ffs(~seat->slot_map) - 1;
156 device->mt.slots[slot].seat_slot = seat_slot;
161 seat->slot_map |= 1 << seat_slot;
162 x = device->mt.slots[slot].x;
163 y = device->mt.slots[slot].y;
165 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
167 case EVDEV_ABSOLUTE_MT_MOTION:
168 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
171 seat_slot = device->mt.slots[slot].seat_slot;
172 x = device->mt.slots[slot].x;
173 y = device->mt.slots[slot].y;
178 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
180 case EVDEV_ABSOLUTE_MT_UP:
181 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
184 seat_slot = device->mt.slots[slot].seat_slot;
185 device->mt.slots[slot].seat_slot = -1;
190 seat->slot_map &= ~(1 << seat_slot);
192 touch_notify_touch_up(base, time, slot, seat_slot);
194 case EVDEV_ABSOLUTE_TOUCH_DOWN:
195 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
198 if (device->abs.seat_slot != -1) {
199 log_bug_kernel("%s: Driver sent multiple touch down for the "
200 "same slot", device->devnode);
204 seat_slot = ffs(~seat->slot_map) - 1;
205 device->abs.seat_slot = seat_slot;
210 seat->slot_map |= 1 << seat_slot;
212 transform_absolute(device, &cx, &cy);
214 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
216 case EVDEV_ABSOLUTE_MOTION:
217 transform_absolute(device, &cx, &cy);
221 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
222 seat_slot = device->abs.seat_slot;
227 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
228 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
229 pointer_notify_motion_absolute(base, time, x, y);
232 case EVDEV_ABSOLUTE_TOUCH_UP:
233 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
236 seat_slot = device->abs.seat_slot;
237 device->abs.seat_slot = -1;
242 seat->slot_map &= ~(1 << seat_slot);
244 touch_notify_touch_up(base, time, -1, seat_slot);
247 assert(0 && "Unknown pending event type");
251 device->pending_event = EVDEV_NONE;
255 evdev_process_touch_button(struct evdev_device *device,
256 uint64_t time, int value)
258 if (device->pending_event != EVDEV_NONE &&
259 device->pending_event != EVDEV_ABSOLUTE_MOTION)
260 evdev_flush_pending_event(device, time);
262 device->pending_event = (value ?
263 EVDEV_ABSOLUTE_TOUCH_DOWN :
264 EVDEV_ABSOLUTE_TOUCH_UP);
268 evdev_process_key(struct evdev_device *device,
269 struct input_event *e, uint64_t time)
271 /* ignore kernel key repeat */
275 if (e->code > KEY_MAX)
278 if (e->code == BTN_TOUCH) {
280 evdev_process_touch_button(device, time, e->value);
284 evdev_flush_pending_event(device, time);
295 pointer_notify_button(
299 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
300 LIBINPUT_BUTTON_STATE_RELEASED);
304 /* Only let KEY_* codes pass through. */
305 if (!(e->code <= KEY_MICMUTE ||
306 (e->code >= KEY_OK && e->code <= KEY_LIGHTS_TOGGLE)))
313 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
314 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
320 evdev_process_touch(struct evdev_device *device,
321 struct input_event *e,
326 evdev_flush_pending_event(device, time);
327 device->mt.slot = e->value;
329 case ABS_MT_TRACKING_ID:
330 if (device->pending_event != EVDEV_NONE &&
331 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
332 evdev_flush_pending_event(device, time);
334 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
336 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
338 case ABS_MT_POSITION_X:
339 device->mt.slots[device->mt.slot].x = e->value;
340 if (device->pending_event == EVDEV_NONE)
341 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
343 case ABS_MT_POSITION_Y:
344 device->mt.slots[device->mt.slot].y = e->value;
345 if (device->pending_event == EVDEV_NONE)
346 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
352 evdev_process_absolute_motion(struct evdev_device *device,
353 struct input_event *e)
357 device->abs.x = e->value;
358 if (device->pending_event == EVDEV_NONE)
359 device->pending_event = EVDEV_ABSOLUTE_MOTION;
362 device->abs.y = e->value;
363 if (device->pending_event == EVDEV_NONE)
364 device->pending_event = EVDEV_ABSOLUTE_MOTION;
370 evdev_process_relative(struct evdev_device *device,
371 struct input_event *e, uint64_t time)
373 struct libinput_device *base = &device->base;
377 if (device->pending_event != EVDEV_RELATIVE_MOTION)
378 evdev_flush_pending_event(device, time);
379 device->rel.dx += e->value;
380 device->pending_event = EVDEV_RELATIVE_MOTION;
383 if (device->pending_event != EVDEV_RELATIVE_MOTION)
384 evdev_flush_pending_event(device, time);
385 device->rel.dy += e->value;
386 device->pending_event = EVDEV_RELATIVE_MOTION;
389 evdev_flush_pending_event(device, time);
393 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
394 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
397 evdev_flush_pending_event(device, time);
406 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
407 e->value * DEFAULT_AXIS_STEP_DISTANCE);
417 evdev_process_absolute(struct evdev_device *device,
418 struct input_event *e,
422 evdev_process_touch(device, e, time);
424 evdev_process_absolute_motion(device, e);
429 evdev_need_touch_frame(struct evdev_device *device)
431 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
434 switch (device->pending_event) {
436 case EVDEV_RELATIVE_MOTION:
438 case EVDEV_ABSOLUTE_MT_DOWN:
439 case EVDEV_ABSOLUTE_MT_MOTION:
440 case EVDEV_ABSOLUTE_MT_UP:
441 case EVDEV_ABSOLUTE_TOUCH_DOWN:
442 case EVDEV_ABSOLUTE_TOUCH_UP:
443 case EVDEV_ABSOLUTE_MOTION:
451 fallback_process(struct evdev_dispatch *dispatch,
452 struct evdev_device *device,
453 struct input_event *event,
458 switch (event->type) {
460 evdev_process_relative(device, event, time);
463 evdev_process_absolute(device, event, time);
466 evdev_process_key(device, event, time);
469 need_frame = evdev_need_touch_frame(device);
470 evdev_flush_pending_event(device, time);
472 touch_notify_frame(&device->base, time);
478 fallback_destroy(struct evdev_dispatch *dispatch)
483 struct evdev_dispatch_interface fallback_interface = {
488 static struct evdev_dispatch *
489 fallback_dispatch_create(void)
491 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
492 if (dispatch == NULL)
495 dispatch->interface = &fallback_interface;
501 evdev_process_event(struct evdev_device *device, struct input_event *e)
503 struct evdev_dispatch *dispatch = device->dispatch;
504 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
506 dispatch->interface->process(dispatch, device, e, time);
510 evdev_device_dispatch_one(struct evdev_device *device,
511 struct input_event *ev)
513 if (!device->mtdev) {
514 evdev_process_event(device, ev);
516 mtdev_put_event(device->mtdev, ev);
517 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
518 while (!mtdev_empty(device->mtdev)) {
519 struct input_event e;
520 mtdev_get_event(device->mtdev, &e);
521 evdev_process_event(device, &e);
528 evdev_sync_device(struct evdev_device *device)
530 struct input_event ev;
534 rc = libevdev_next_event(device->evdev,
535 LIBEVDEV_READ_FLAG_SYNC, &ev);
538 evdev_device_dispatch_one(device, &ev);
539 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
541 return rc == -EAGAIN ? 0 : rc;
545 evdev_device_dispatch(void *data)
547 struct evdev_device *device = data;
548 struct libinput *libinput = device->base.seat->libinput;
549 struct input_event ev;
552 /* If the compositor is repainting, this function is called only once
553 * per frame and we have to process all the events available on the
554 * fd, otherwise there will be input lag. */
556 rc = libevdev_next_event(device->evdev,
557 LIBEVDEV_READ_FLAG_NORMAL, &ev);
558 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
559 /* send one more sync event so we handle all
560 currently pending events before we sync up
561 to the current state */
562 ev.code = SYN_REPORT;
563 evdev_device_dispatch_one(device, &ev);
565 rc = evdev_sync_device(device);
567 rc = LIBEVDEV_READ_STATUS_SUCCESS;
568 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
569 evdev_device_dispatch_one(device, &ev);
571 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
573 if (rc != -EAGAIN && rc != -EINTR) {
574 libinput_remove_source(libinput, device->source);
575 device->source = NULL;
580 configure_pointer_acceleration(struct evdev_device *device)
582 device->pointer.filter =
583 create_pointer_accelator_filter(
584 pointer_accel_profile_smooth_simple);
585 if (!device->pointer.filter)
592 evdev_configure_device(struct evdev_device *device)
594 struct libevdev *evdev = device->evdev;
595 const struct input_absinfo *absinfo;
596 int has_abs, has_rel, has_mt;
597 int has_button, has_keyboard, has_touch;
598 struct mt_slot *slots;
611 if (libevdev_has_event_type(evdev, EV_ABS)) {
613 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
614 device->abs.absinfo_x = absinfo;
617 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
618 device->abs.absinfo_y = absinfo;
621 /* We only handle the slotted Protocol B in weston.
622 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
623 require mtdev for conversion. */
624 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
625 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
626 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
627 device->abs.absinfo_x = absinfo;
628 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
629 device->abs.absinfo_y = absinfo;
634 if (!libevdev_has_event_code(evdev,
635 EV_ABS, ABS_MT_SLOT)) {
636 device->mtdev = mtdev_new_open(device->fd);
640 num_slots = device->mtdev->caps.slot.maximum;
641 if (device->mtdev->caps.slot.minimum < 0 ||
644 active_slot = device->mtdev->caps.slot.value;
646 num_slots = libevdev_get_num_slots(device->evdev);
647 active_slot = libevdev_get_current_slot(evdev);
650 slots = calloc(num_slots, sizeof(struct mt_slot));
654 for (slot = 0; slot < num_slots; ++slot) {
655 slots[slot].seat_slot = -1;
659 device->mt.slots = slots;
660 device->mt.slots_len = num_slots;
661 device->mt.slot = active_slot;
664 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
665 libevdev_has_event_code(evdev, EV_REL, REL_Y))
668 if (libevdev_has_event_type(evdev, EV_KEY)) {
669 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
670 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
671 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
672 (has_abs || has_mt)) {
673 device->dispatch = evdev_mt_touchpad_create(device);
674 log_info("input device '%s', %s is a touchpad\n",
675 device->devname, device->devnode);
677 for (i = KEY_ESC; i < KEY_MAX; i++) {
678 if (i >= BTN_MISC && i < KEY_OK)
680 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
685 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
687 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
688 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
694 if (libevdev_has_event_type(evdev, EV_LED))
697 if ((has_abs || has_rel) && has_button) {
698 if (configure_pointer_acceleration(device) == -1)
701 device->seat_caps |= EVDEV_DEVICE_POINTER;
703 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
704 device->devname, device->devnode,
705 has_abs ? " absolute-motion" : "",
706 has_rel ? " relative-motion": "",
707 has_button ? " button" : "");
710 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
711 log_info("input device '%s', %s is a keyboard\n",
712 device->devname, device->devnode);
714 if (has_touch && !has_button) {
715 device->seat_caps |= EVDEV_DEVICE_TOUCH;
716 log_info("input device '%s', %s is a touch device\n",
717 device->devname, device->devnode);
723 struct evdev_device *
724 evdev_device_create(struct libinput_seat *seat,
728 struct libinput *libinput = seat->libinput;
729 struct evdev_device *device;
732 int unhandled_device = 0;
734 /* Use non-blocking mode so that we can loop on read on
735 * evdev_device_data() until all events on the fd are
736 * read. mtdev_get() also expects this. */
737 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
739 log_info("opening input device '%s' failed (%s).\n",
740 devnode, strerror(-fd));
744 device = zalloc(sizeof *device);
748 libinput_device_init(&device->base, seat);
750 rc = libevdev_new_from_fd(fd, &device->evdev);
754 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
756 device->seat_caps = 0;
758 device->mtdev = NULL;
759 device->devnode = strdup(devnode);
760 device->sysname = strdup(sysname);
763 device->abs.seat_slot = -1;
764 device->dispatch = NULL;
766 device->pending_event = EVDEV_NONE;
767 device->devname = libevdev_get_name(device->evdev);
769 libinput_seat_ref(seat);
771 if (evdev_configure_device(device) == -1)
774 if (device->seat_caps == 0) {
775 unhandled_device = 1;
779 /* If the dispatch was not set up use the fallback. */
780 if (device->dispatch == NULL)
781 device->dispatch = fallback_dispatch_create();
782 if (device->dispatch == NULL)
786 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
790 list_insert(seat->devices_list.prev, &device->base.link);
791 notify_added_device(&device->base);
797 close_restricted(libinput, fd);
798 evdev_device_destroy(device);
800 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
804 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
808 memset(keys, 0, size);
809 len = ioctl(device->fd, EVIOCGKEY(size), keys);
811 return (len == -1) ? -errno : len;
815 evdev_device_get_output(struct evdev_device *device)
817 return device->output_name;
821 evdev_device_get_sysname(struct evdev_device *device)
823 return device->sysname;
827 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
829 device->abs.apply_calibration = 1;
830 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
834 evdev_device_has_capability(struct evdev_device *device,
835 enum libinput_device_capability capability)
837 switch (capability) {
838 case LIBINPUT_DEVICE_CAP_POINTER:
839 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
840 case LIBINPUT_DEVICE_CAP_KEYBOARD:
841 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
842 case LIBINPUT_DEVICE_CAP_TOUCH:
843 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
850 evdev_device_remove(struct evdev_device *device)
853 libinput_remove_source(device->base.seat->libinput,
857 mtdev_close_delete(device->mtdev);
858 close_restricted(device->base.seat->libinput, device->fd);
860 list_remove(&device->base.link);
862 notify_removed_device(&device->base);
863 libinput_device_unref(&device->base);
867 evdev_device_destroy(struct evdev_device *device)
869 struct evdev_dispatch *dispatch;
871 dispatch = device->dispatch;
873 dispatch->interface->destroy(dispatch);
875 motion_filter_destroy(device->pointer.filter);
876 libinput_seat_unref(device->base.seat);
877 libevdev_free(device->evdev);
878 free(device->mt.slots);
879 free(device->devnode);
880 free(device->sysname);