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 libinput *libinput = device->base.seat->libinput;
119 struct motion_params motion;
124 struct libinput_device *base = &device->base;
125 struct libinput_seat *seat = base->seat;
127 slot = device->mt.slot;
129 switch (device->pending_event) {
132 case EVDEV_RELATIVE_MOTION:
133 motion.dx = device->rel.dx;
134 motion.dy = device->rel.dy;
138 /* Apply pointer acceleration. */
139 filter_dispatch(device->pointer.filter, &motion, device, time);
141 if (motion.dx == 0.0 && motion.dy == 0.0)
144 pointer_notify_motion(base, time, motion.dx, motion.dy);
146 case EVDEV_ABSOLUTE_MT_DOWN:
147 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
150 if (device->mt.slots[slot].seat_slot != -1) {
151 log_bug_kernel(libinput,
152 "%s: Driver sent multiple touch down for the "
153 "same slot", device->devnode);
157 seat_slot = ffs(~seat->slot_map) - 1;
158 device->mt.slots[slot].seat_slot = seat_slot;
163 seat->slot_map |= 1 << seat_slot;
164 x = device->mt.slots[slot].x;
165 y = device->mt.slots[slot].y;
167 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
169 case EVDEV_ABSOLUTE_MT_MOTION:
170 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
173 seat_slot = device->mt.slots[slot].seat_slot;
174 x = device->mt.slots[slot].x;
175 y = device->mt.slots[slot].y;
180 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
182 case EVDEV_ABSOLUTE_MT_UP:
183 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
186 seat_slot = device->mt.slots[slot].seat_slot;
187 device->mt.slots[slot].seat_slot = -1;
192 seat->slot_map &= ~(1 << seat_slot);
194 touch_notify_touch_up(base, time, slot, seat_slot);
196 case EVDEV_ABSOLUTE_TOUCH_DOWN:
197 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
200 if (device->abs.seat_slot != -1) {
201 log_bug_kernel(libinput,
202 "%s: Driver sent multiple touch down for the "
203 "same slot", device->devnode);
207 seat_slot = ffs(~seat->slot_map) - 1;
208 device->abs.seat_slot = seat_slot;
213 seat->slot_map |= 1 << seat_slot;
215 transform_absolute(device, &cx, &cy);
217 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
219 case EVDEV_ABSOLUTE_MOTION:
220 transform_absolute(device, &cx, &cy);
224 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
225 seat_slot = device->abs.seat_slot;
230 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
231 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
232 pointer_notify_motion_absolute(base, time, x, y);
235 case EVDEV_ABSOLUTE_TOUCH_UP:
236 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
239 seat_slot = device->abs.seat_slot;
240 device->abs.seat_slot = -1;
245 seat->slot_map &= ~(1 << seat_slot);
247 touch_notify_touch_up(base, time, -1, seat_slot);
250 assert(0 && "Unknown pending event type");
254 device->pending_event = EVDEV_NONE;
258 evdev_process_touch_button(struct evdev_device *device,
259 uint64_t time, int value)
261 if (device->pending_event != EVDEV_NONE &&
262 device->pending_event != EVDEV_ABSOLUTE_MOTION)
263 evdev_flush_pending_event(device, time);
265 device->pending_event = (value ?
266 EVDEV_ABSOLUTE_TOUCH_DOWN :
267 EVDEV_ABSOLUTE_TOUCH_UP);
271 evdev_process_key(struct evdev_device *device,
272 struct input_event *e, uint64_t time)
274 /* ignore kernel key repeat */
278 if (e->code > KEY_MAX)
281 if (e->code == BTN_TOUCH) {
283 evdev_process_touch_button(device, time, e->value);
287 evdev_flush_pending_event(device, time);
298 pointer_notify_button(
302 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
303 LIBINPUT_BUTTON_STATE_RELEASED);
307 /* Only let KEY_* codes pass through. */
308 if (!(e->code <= KEY_MICMUTE ||
309 (e->code >= KEY_OK && e->code <= KEY_LIGHTS_TOGGLE)))
316 e->value ? LIBINPUT_KEY_STATE_PRESSED :
317 LIBINPUT_KEY_STATE_RELEASED);
323 evdev_process_touch(struct evdev_device *device,
324 struct input_event *e,
329 evdev_flush_pending_event(device, time);
330 device->mt.slot = e->value;
332 case ABS_MT_TRACKING_ID:
333 if (device->pending_event != EVDEV_NONE &&
334 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
335 evdev_flush_pending_event(device, time);
337 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
339 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
341 case ABS_MT_POSITION_X:
342 device->mt.slots[device->mt.slot].x = e->value;
343 if (device->pending_event == EVDEV_NONE)
344 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
346 case ABS_MT_POSITION_Y:
347 device->mt.slots[device->mt.slot].y = e->value;
348 if (device->pending_event == EVDEV_NONE)
349 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
355 evdev_process_absolute_motion(struct evdev_device *device,
356 struct input_event *e)
360 device->abs.x = e->value;
361 if (device->pending_event == EVDEV_NONE)
362 device->pending_event = EVDEV_ABSOLUTE_MOTION;
365 device->abs.y = e->value;
366 if (device->pending_event == EVDEV_NONE)
367 device->pending_event = EVDEV_ABSOLUTE_MOTION;
373 evdev_process_relative(struct evdev_device *device,
374 struct input_event *e, uint64_t time)
376 struct libinput_device *base = &device->base;
380 if (device->pending_event != EVDEV_RELATIVE_MOTION)
381 evdev_flush_pending_event(device, time);
382 device->rel.dx += e->value;
383 device->pending_event = EVDEV_RELATIVE_MOTION;
386 if (device->pending_event != EVDEV_RELATIVE_MOTION)
387 evdev_flush_pending_event(device, time);
388 device->rel.dy += e->value;
389 device->pending_event = EVDEV_RELATIVE_MOTION;
392 evdev_flush_pending_event(device, time);
396 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
397 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
400 evdev_flush_pending_event(device, time);
409 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
410 e->value * DEFAULT_AXIS_STEP_DISTANCE);
420 evdev_process_absolute(struct evdev_device *device,
421 struct input_event *e,
425 evdev_process_touch(device, e, time);
427 evdev_process_absolute_motion(device, e);
432 evdev_need_touch_frame(struct evdev_device *device)
434 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
437 switch (device->pending_event) {
439 case EVDEV_RELATIVE_MOTION:
441 case EVDEV_ABSOLUTE_MT_DOWN:
442 case EVDEV_ABSOLUTE_MT_MOTION:
443 case EVDEV_ABSOLUTE_MT_UP:
444 case EVDEV_ABSOLUTE_TOUCH_DOWN:
445 case EVDEV_ABSOLUTE_TOUCH_UP:
446 case EVDEV_ABSOLUTE_MOTION:
454 fallback_process(struct evdev_dispatch *dispatch,
455 struct evdev_device *device,
456 struct input_event *event,
461 switch (event->type) {
463 evdev_process_relative(device, event, time);
466 evdev_process_absolute(device, event, time);
469 evdev_process_key(device, event, time);
472 need_frame = evdev_need_touch_frame(device);
473 evdev_flush_pending_event(device, time);
475 touch_notify_frame(&device->base, time);
481 fallback_destroy(struct evdev_dispatch *dispatch)
486 struct evdev_dispatch_interface fallback_interface = {
491 static struct evdev_dispatch *
492 fallback_dispatch_create(void)
494 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
495 if (dispatch == NULL)
498 dispatch->interface = &fallback_interface;
504 evdev_process_event(struct evdev_device *device, struct input_event *e)
506 struct evdev_dispatch *dispatch = device->dispatch;
507 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
509 dispatch->interface->process(dispatch, device, e, time);
513 evdev_device_dispatch_one(struct evdev_device *device,
514 struct input_event *ev)
516 if (!device->mtdev) {
517 evdev_process_event(device, ev);
519 mtdev_put_event(device->mtdev, ev);
520 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
521 while (!mtdev_empty(device->mtdev)) {
522 struct input_event e;
523 mtdev_get_event(device->mtdev, &e);
524 evdev_process_event(device, &e);
531 evdev_sync_device(struct evdev_device *device)
533 struct input_event ev;
537 rc = libevdev_next_event(device->evdev,
538 LIBEVDEV_READ_FLAG_SYNC, &ev);
541 evdev_device_dispatch_one(device, &ev);
542 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
544 return rc == -EAGAIN ? 0 : rc;
548 evdev_device_dispatch(void *data)
550 struct evdev_device *device = data;
551 struct libinput *libinput = device->base.seat->libinput;
552 struct input_event ev;
555 /* If the compositor is repainting, this function is called only once
556 * per frame and we have to process all the events available on the
557 * fd, otherwise there will be input lag. */
559 rc = libevdev_next_event(device->evdev,
560 LIBEVDEV_READ_FLAG_NORMAL, &ev);
561 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
562 /* send one more sync event so we handle all
563 currently pending events before we sync up
564 to the current state */
565 ev.code = SYN_REPORT;
566 evdev_device_dispatch_one(device, &ev);
568 rc = evdev_sync_device(device);
570 rc = LIBEVDEV_READ_STATUS_SUCCESS;
571 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
572 evdev_device_dispatch_one(device, &ev);
574 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
576 if (rc != -EAGAIN && rc != -EINTR) {
577 libinput_remove_source(libinput, device->source);
578 device->source = NULL;
583 configure_pointer_acceleration(struct evdev_device *device)
585 device->pointer.filter =
586 create_pointer_accelator_filter(
587 pointer_accel_profile_smooth_simple);
588 if (!device->pointer.filter)
595 evdev_configure_device(struct evdev_device *device)
597 struct libinput *libinput = device->base.seat->libinput;
598 struct libevdev *evdev = device->evdev;
599 const struct input_absinfo *absinfo;
600 struct input_absinfo fixed;
601 int has_abs, has_rel, has_mt;
602 int has_button, has_keyboard, has_touch;
603 struct mt_slot *slots;
616 if (libevdev_has_event_type(evdev, EV_ABS)) {
618 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
619 if (absinfo->resolution == 0) {
621 fixed.resolution = 1;
622 libevdev_set_abs_info(evdev, ABS_X, &fixed);
624 device->abs.absinfo_x = absinfo;
627 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
628 if (absinfo->resolution == 0) {
630 fixed.resolution = 1;
631 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
633 device->abs.absinfo_y = absinfo;
636 /* We only handle the slotted Protocol B in weston.
637 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
638 require mtdev for conversion. */
639 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
640 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
641 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
642 if (absinfo->resolution == 0) {
644 fixed.resolution = 1;
645 libevdev_set_abs_info(evdev,
649 device->abs.absinfo_x = absinfo;
650 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
651 if (absinfo->resolution == 0) {
653 fixed.resolution = 1;
654 libevdev_set_abs_info(evdev,
658 device->abs.absinfo_y = absinfo;
663 if (!libevdev_has_event_code(evdev,
664 EV_ABS, ABS_MT_SLOT)) {
665 device->mtdev = mtdev_new_open(device->fd);
669 num_slots = device->mtdev->caps.slot.maximum;
670 if (device->mtdev->caps.slot.minimum < 0 ||
673 active_slot = device->mtdev->caps.slot.value;
675 num_slots = libevdev_get_num_slots(device->evdev);
676 active_slot = libevdev_get_current_slot(evdev);
679 slots = calloc(num_slots, sizeof(struct mt_slot));
683 for (slot = 0; slot < num_slots; ++slot) {
684 slots[slot].seat_slot = -1;
688 device->mt.slots = slots;
689 device->mt.slots_len = num_slots;
690 device->mt.slot = active_slot;
693 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
694 libevdev_has_event_code(evdev, EV_REL, REL_Y))
697 if (libevdev_has_event_type(evdev, EV_KEY)) {
698 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
699 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
700 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
701 (has_abs || has_mt)) {
702 device->dispatch = evdev_mt_touchpad_create(device);
704 "input device '%s', %s is a touchpad\n",
705 device->devname, device->devnode);
707 for (i = KEY_ESC; i < KEY_MAX; i++) {
708 if (i >= BTN_MISC && i < KEY_OK)
710 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
715 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
717 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
718 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
724 if (libevdev_has_event_type(evdev, EV_LED))
727 if ((has_abs || has_rel) && has_button) {
728 if (configure_pointer_acceleration(device) == -1)
731 device->seat_caps |= EVDEV_DEVICE_POINTER;
734 "input device '%s', %s is a pointer caps =%s%s%s\n",
735 device->devname, device->devnode,
736 has_abs ? " absolute-motion" : "",
737 has_rel ? " relative-motion": "",
738 has_button ? " button" : "");
741 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
743 "input device '%s', %s is a keyboard\n",
744 device->devname, device->devnode);
746 if (has_touch && !has_button) {
747 device->seat_caps |= EVDEV_DEVICE_TOUCH;
749 "input device '%s', %s is a touch device\n",
750 device->devname, device->devnode);
756 struct evdev_device *
757 evdev_device_create(struct libinput_seat *seat,
761 struct libinput *libinput = seat->libinput;
762 struct evdev_device *device;
765 int unhandled_device = 0;
767 /* Use non-blocking mode so that we can loop on read on
768 * evdev_device_data() until all events on the fd are
769 * read. mtdev_get() also expects this. */
770 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
773 "opening input device '%s' failed (%s).\n",
774 devnode, strerror(-fd));
778 device = zalloc(sizeof *device);
782 libinput_device_init(&device->base, seat);
784 rc = libevdev_new_from_fd(fd, &device->evdev);
788 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
790 device->seat_caps = 0;
792 device->mtdev = NULL;
793 device->devnode = strdup(devnode);
794 device->sysname = strdup(sysname);
797 device->abs.seat_slot = -1;
798 device->dispatch = NULL;
800 device->pending_event = EVDEV_NONE;
801 device->devname = libevdev_get_name(device->evdev);
803 libinput_seat_ref(seat);
805 if (evdev_configure_device(device) == -1)
808 if (device->seat_caps == 0) {
809 unhandled_device = 1;
813 /* If the dispatch was not set up use the fallback. */
814 if (device->dispatch == NULL)
815 device->dispatch = fallback_dispatch_create();
816 if (device->dispatch == NULL)
820 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
824 list_insert(seat->devices_list.prev, &device->base.link);
825 notify_added_device(&device->base);
831 close_restricted(libinput, fd);
832 evdev_device_destroy(device);
834 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
838 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
842 memset(keys, 0, size);
843 len = ioctl(device->fd, EVIOCGKEY(size), keys);
845 return (len == -1) ? -errno : len;
849 evdev_device_get_output(struct evdev_device *device)
851 return device->output_name;
855 evdev_device_get_sysname(struct evdev_device *device)
857 return device->sysname;
861 evdev_device_get_name(struct evdev_device *device)
863 return device->devname;
867 evdev_device_get_id_product(struct evdev_device *device)
869 return libevdev_get_id_product(device->evdev);
873 evdev_device_get_id_vendor(struct evdev_device *device)
875 return libevdev_get_id_vendor(device->evdev);
879 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
881 device->abs.apply_calibration = 1;
882 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
886 evdev_device_has_capability(struct evdev_device *device,
887 enum libinput_device_capability capability)
889 switch (capability) {
890 case LIBINPUT_DEVICE_CAP_POINTER:
891 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
892 case LIBINPUT_DEVICE_CAP_KEYBOARD:
893 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
894 case LIBINPUT_DEVICE_CAP_TOUCH:
895 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
902 evdev_device_get_size(struct evdev_device *device,
906 const struct input_absinfo *x, *y;
908 x = libevdev_get_abs_info(device->evdev, ABS_X);
909 y = libevdev_get_abs_info(device->evdev, ABS_Y);
911 if (!x || !y || !x->resolution || !y->resolution)
914 *width = evdev_convert_to_mm(x, x->maximum);
915 *height = evdev_convert_to_mm(y, y->maximum);
921 evdev_device_remove(struct evdev_device *device)
924 libinput_remove_source(device->base.seat->libinput,
928 mtdev_close_delete(device->mtdev);
929 close_restricted(device->base.seat->libinput, device->fd);
931 list_remove(&device->base.link);
933 notify_removed_device(&device->base);
934 libinput_device_unref(&device->base);
938 evdev_device_destroy(struct evdev_device *device)
940 struct evdev_dispatch *dispatch;
942 dispatch = device->dispatch;
944 dispatch->interface->destroy(dispatch);
946 motion_filter_destroy(device->pointer.filter);
947 libinput_seat_unref(device->base.seat);
948 libevdev_free(device->evdev);
949 free(device->mt.slots);
950 free(device->devnode);
951 free(device->sysname);