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 li_fixed_from_int(10)
44 #ifndef KEY_LIGHTS_TOGGLE
45 #define KEY_LIGHTS_TOGGLE 0x21e
49 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
52 enum libinput_led weston;
55 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
56 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
57 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
59 struct input_event ev[ARRAY_LENGTH(map) + 1];
62 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
65 memset(ev, 0, sizeof(ev));
66 for (i = 0; i < ARRAY_LENGTH(map); i++) {
68 ev[i].code = map[i].evdev;
69 ev[i].value = !!(leds & map[i].weston);
72 ev[i].code = SYN_REPORT;
74 i = write(device->fd, ev, sizeof ev);
75 (void)i; /* no, we really don't care about the return value */
79 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
81 if (!device->abs.apply_calibration) {
86 *x = device->abs.x * device->abs.calibration[0] +
87 device->abs.y * device->abs.calibration[1] +
88 device->abs.calibration[2];
90 *y = device->abs.x * device->abs.calibration[3] +
91 device->abs.y * device->abs.calibration[4] +
92 device->abs.calibration[5];
97 evdev_device_transform_x(struct evdev_device *device,
101 return ((uint64_t)x - li_fixed_from_int(device->abs.min_x)) * width /
102 (device->abs.max_x - device->abs.min_x + 1);
106 evdev_device_transform_y(struct evdev_device *device,
110 return ((uint64_t)y - li_fixed_from_int(device->abs.min_y)) * height /
111 (device->abs.max_y - device->abs.min_y + 1);
115 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
117 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 = li_fixed_to_double(device->rel.dx);
133 motion.dy = li_fixed_to_double(device->rel.dy);
137 /* Apply pointer acceleration. */
138 filter_dispatch(device->pointer.filter, &motion, device, time);
140 dx = li_fixed_from_double(motion.dx);
141 dy = li_fixed_from_double(motion.dy);
142 if (dx == 0 && dy == 0)
145 pointer_notify_motion(base, time, dx, dy);
147 case EVDEV_ABSOLUTE_MT_DOWN:
148 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
151 if (device->mt.slots[slot].seat_slot != -1) {
152 log_bug_kernel("%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 = li_fixed_from_int(device->mt.slots[slot].x);
165 y = li_fixed_from_int(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 = li_fixed_from_int(device->mt.slots[slot].x);
175 y = li_fixed_from_int(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("%s: Driver sent multiple touch down for the "
202 "same slot", device->devnode);
206 seat_slot = ffs(~seat->slot_map) - 1;
207 device->abs.seat_slot = seat_slot;
212 seat->slot_map |= 1 << seat_slot;
214 transform_absolute(device, &cx, &cy);
215 x = li_fixed_from_int(cx);
216 y = li_fixed_from_int(cy);
218 touch_notify_touch_down(base, time, -1, seat_slot, x, y);
220 case EVDEV_ABSOLUTE_MOTION:
221 transform_absolute(device, &cx, &cy);
222 x = li_fixed_from_int(cx);
223 y = li_fixed_from_int(cy);
225 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
226 seat_slot = device->abs.seat_slot;
231 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
232 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
233 pointer_notify_motion_absolute(base, time, x, y);
236 case EVDEV_ABSOLUTE_TOUCH_UP:
237 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
240 seat_slot = device->abs.seat_slot;
241 device->abs.seat_slot = -1;
246 seat->slot_map &= ~(1 << seat_slot);
248 touch_notify_touch_up(base, time, -1, seat_slot);
251 assert(0 && "Unknown pending event type");
255 device->pending_event = EVDEV_NONE;
259 evdev_process_touch_button(struct evdev_device *device, int 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, struct input_event *e, int time)
273 /* ignore kernel key repeat */
277 if (e->code > KEY_MAX)
280 if (e->code == BTN_TOUCH) {
282 evdev_process_touch_button(device, time, e->value);
286 evdev_flush_pending_event(device, time);
297 pointer_notify_button(
301 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
302 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
306 /* Only let KEY_* codes pass through. */
307 if (!(e->code <= KEY_MICMUTE ||
308 (e->code >= KEY_OK && e->code <= KEY_LIGHTS_TOGGLE)))
315 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
316 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
322 evdev_process_touch(struct evdev_device *device,
323 struct input_event *e,
328 evdev_flush_pending_event(device, time);
329 device->mt.slot = e->value;
331 case ABS_MT_TRACKING_ID:
332 if (device->pending_event != EVDEV_NONE &&
333 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
334 evdev_flush_pending_event(device, time);
336 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
338 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
340 case ABS_MT_POSITION_X:
341 device->mt.slots[device->mt.slot].x = e->value;
342 if (device->pending_event == EVDEV_NONE)
343 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
345 case ABS_MT_POSITION_Y:
346 device->mt.slots[device->mt.slot].y = e->value;
347 if (device->pending_event == EVDEV_NONE)
348 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
354 evdev_process_absolute_motion(struct evdev_device *device,
355 struct input_event *e)
359 device->abs.x = e->value;
360 if (device->pending_event == EVDEV_NONE)
361 device->pending_event = EVDEV_ABSOLUTE_MOTION;
364 device->abs.y = e->value;
365 if (device->pending_event == EVDEV_NONE)
366 device->pending_event = EVDEV_ABSOLUTE_MOTION;
372 evdev_process_relative(struct evdev_device *device,
373 struct input_event *e, uint64_t time)
375 struct libinput_device *base = &device->base;
379 if (device->pending_event != EVDEV_RELATIVE_MOTION)
380 evdev_flush_pending_event(device, time);
381 device->rel.dx += li_fixed_from_int(e->value);
382 device->pending_event = EVDEV_RELATIVE_MOTION;
385 if (device->pending_event != EVDEV_RELATIVE_MOTION)
386 evdev_flush_pending_event(device, time);
387 device->rel.dy += li_fixed_from_int(e->value);
388 device->pending_event = EVDEV_RELATIVE_MOTION;
391 evdev_flush_pending_event(device, time);
395 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
396 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
399 evdev_flush_pending_event(device, time);
408 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
409 e->value * DEFAULT_AXIS_STEP_DISTANCE);
419 evdev_process_absolute(struct evdev_device *device,
420 struct input_event *e,
424 evdev_process_touch(device, e, time);
426 evdev_process_absolute_motion(device, e);
431 evdev_need_touch_frame(struct evdev_device *device)
433 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
436 switch (device->pending_event) {
438 case EVDEV_RELATIVE_MOTION:
440 case EVDEV_ABSOLUTE_MT_DOWN:
441 case EVDEV_ABSOLUTE_MT_MOTION:
442 case EVDEV_ABSOLUTE_MT_UP:
443 case EVDEV_ABSOLUTE_TOUCH_DOWN:
444 case EVDEV_ABSOLUTE_TOUCH_UP:
445 case EVDEV_ABSOLUTE_MOTION:
453 fallback_process(struct evdev_dispatch *dispatch,
454 struct evdev_device *device,
455 struct input_event *event,
460 switch (event->type) {
462 evdev_process_relative(device, event, time);
465 evdev_process_absolute(device, event, time);
468 evdev_process_key(device, event, time);
471 need_frame = evdev_need_touch_frame(device);
472 evdev_flush_pending_event(device, time);
474 touch_notify_frame(&device->base, time);
480 fallback_destroy(struct evdev_dispatch *dispatch)
485 struct evdev_dispatch_interface fallback_interface = {
490 static struct evdev_dispatch *
491 fallback_dispatch_create(void)
493 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
494 if (dispatch == NULL)
497 dispatch->interface = &fallback_interface;
503 evdev_process_event(struct evdev_device *device, struct input_event *e)
505 struct evdev_dispatch *dispatch = device->dispatch;
506 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
508 dispatch->interface->process(dispatch, device, e, time);
512 evdev_device_dispatch_one(struct evdev_device *device,
513 struct input_event *ev)
515 if (!device->mtdev) {
516 evdev_process_event(device, ev);
518 mtdev_put_event(device->mtdev, ev);
519 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
520 while (!mtdev_empty(device->mtdev)) {
521 struct input_event e;
522 mtdev_get_event(device->mtdev, &e);
523 evdev_process_event(device, &e);
530 evdev_sync_device(struct evdev_device *device)
532 struct input_event ev;
536 rc = libevdev_next_event(device->evdev,
537 LIBEVDEV_READ_FLAG_SYNC, &ev);
540 evdev_device_dispatch_one(device, &ev);
541 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
543 return rc == -EAGAIN ? 0 : rc;
547 evdev_device_dispatch(void *data)
549 struct evdev_device *device = data;
550 struct libinput *libinput = device->base.seat->libinput;
551 struct input_event ev;
554 /* If the compositor is repainting, this function is called only once
555 * per frame and we have to process all the events available on the
556 * fd, otherwise there will be input lag. */
558 rc = libevdev_next_event(device->evdev,
559 LIBEVDEV_READ_FLAG_NORMAL, &ev);
560 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
561 /* send one more sync event so we handle all
562 currently pending events before we sync up
563 to the current state */
564 ev.code = SYN_REPORT;
565 evdev_device_dispatch_one(device, &ev);
567 rc = evdev_sync_device(device);
569 rc = LIBEVDEV_READ_STATUS_SUCCESS;
570 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
571 evdev_device_dispatch_one(device, &ev);
573 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
575 if (rc != -EAGAIN && rc != -EINTR) {
576 libinput_remove_source(libinput, device->source);
577 device->source = NULL;
582 configure_pointer_acceleration(struct evdev_device *device)
584 device->pointer.filter =
585 create_pointer_accelator_filter(
586 pointer_accel_profile_smooth_simple);
587 if (!device->pointer.filter)
594 evdev_configure_device(struct evdev_device *device)
596 struct libevdev *evdev = device->evdev;
597 const struct input_absinfo *absinfo;
598 int has_abs, has_rel, has_mt;
599 int has_button, has_keyboard, has_touch;
600 struct mt_slot *slots;
613 if (libevdev_has_event_type(evdev, EV_ABS)) {
615 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
616 device->abs.min_x = absinfo->minimum;
617 device->abs.max_x = absinfo->maximum;
620 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
621 device->abs.min_y = absinfo->minimum;
622 device->abs.max_y = absinfo->maximum;
625 /* We only handle the slotted Protocol B in weston.
626 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
627 require mtdev for conversion. */
628 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
629 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
630 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
631 device->abs.min_x = absinfo->minimum;
632 device->abs.max_x = absinfo->maximum;
633 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
634 device->abs.min_y = absinfo->minimum;
635 device->abs.max_y = absinfo->maximum;
640 if (!libevdev_has_event_code(evdev,
641 EV_ABS, ABS_MT_SLOT)) {
642 device->mtdev = mtdev_new_open(device->fd);
646 num_slots = device->mtdev->caps.slot.maximum;
647 if (device->mtdev->caps.slot.minimum < 0 ||
650 active_slot = device->mtdev->caps.slot.value;
652 num_slots = libevdev_get_num_slots(device->evdev);
653 active_slot = libevdev_get_current_slot(evdev);
656 slots = calloc(num_slots, sizeof(struct mt_slot));
660 for (slot = 0; slot < num_slots; ++slot) {
661 slots[slot].seat_slot = -1;
665 device->mt.slots = slots;
666 device->mt.slots_len = num_slots;
667 device->mt.slot = active_slot;
670 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
671 libevdev_has_event_code(evdev, EV_REL, REL_Y))
674 if (libevdev_has_event_type(evdev, EV_KEY)) {
675 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
676 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
677 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
678 (has_abs || has_mt)) {
679 device->dispatch = evdev_mt_touchpad_create(device);
680 log_info("input device '%s', %s is a touchpad\n",
681 device->devname, device->devnode);
683 for (i = KEY_ESC; i < KEY_MAX; i++) {
684 if (i >= BTN_MISC && i < KEY_OK)
686 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
691 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
693 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
694 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
700 if (libevdev_has_event_type(evdev, EV_LED))
703 if ((has_abs || has_rel) && has_button) {
704 if (configure_pointer_acceleration(device) == -1)
707 device->seat_caps |= EVDEV_DEVICE_POINTER;
709 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
710 device->devname, device->devnode,
711 has_abs ? " absolute-motion" : "",
712 has_rel ? " relative-motion": "",
713 has_button ? " button" : "");
716 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
717 log_info("input device '%s', %s is a keyboard\n",
718 device->devname, device->devnode);
720 if (has_touch && !has_button) {
721 device->seat_caps |= EVDEV_DEVICE_TOUCH;
722 log_info("input device '%s', %s is a touch device\n",
723 device->devname, device->devnode);
729 struct evdev_device *
730 evdev_device_create(struct libinput_seat *seat,
734 struct libinput *libinput = seat->libinput;
735 struct evdev_device *device;
738 int unhandled_device = 0;
740 /* Use non-blocking mode so that we can loop on read on
741 * evdev_device_data() until all events on the fd are
742 * read. mtdev_get() also expects this. */
743 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
745 log_info("opening input device '%s' failed (%s).\n",
746 devnode, strerror(-fd));
750 device = zalloc(sizeof *device);
754 libinput_device_init(&device->base, seat);
756 rc = libevdev_new_from_fd(fd, &device->evdev);
760 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
762 device->seat_caps = 0;
764 device->mtdev = NULL;
765 device->devnode = strdup(devnode);
766 device->sysname = strdup(sysname);
769 device->abs.seat_slot = -1;
770 device->dispatch = NULL;
772 device->pending_event = EVDEV_NONE;
773 device->devname = libevdev_get_name(device->evdev);
775 libinput_seat_ref(seat);
777 if (evdev_configure_device(device) == -1)
780 if (device->seat_caps == 0) {
781 unhandled_device = 1;
785 /* If the dispatch was not set up use the fallback. */
786 if (device->dispatch == NULL)
787 device->dispatch = fallback_dispatch_create();
788 if (device->dispatch == NULL)
792 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
796 list_insert(seat->devices_list.prev, &device->base.link);
797 notify_added_device(&device->base);
803 close_restricted(libinput, fd);
804 evdev_device_destroy(device);
806 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
810 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
814 memset(keys, 0, size);
815 len = ioctl(device->fd, EVIOCGKEY(size), keys);
817 return (len == -1) ? -errno : len;
821 evdev_device_get_output(struct evdev_device *device)
823 return device->output_name;
827 evdev_device_get_sysname(struct evdev_device *device)
829 return device->sysname;
833 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
835 device->abs.apply_calibration = 1;
836 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
840 evdev_device_has_capability(struct evdev_device *device,
841 enum libinput_device_capability capability)
843 switch (capability) {
844 case LIBINPUT_DEVICE_CAP_POINTER:
845 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
846 case LIBINPUT_DEVICE_CAP_KEYBOARD:
847 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
848 case LIBINPUT_DEVICE_CAP_TOUCH:
849 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
856 evdev_device_remove(struct evdev_device *device)
859 libinput_remove_source(device->base.seat->libinput,
863 mtdev_close_delete(device->mtdev);
864 close_restricted(device->base.seat->libinput, device->fd);
866 list_remove(&device->base.link);
868 notify_removed_device(&device->base);
869 libinput_device_unref(&device->base);
873 evdev_device_destroy(struct evdev_device *device)
875 struct evdev_dispatch *dispatch;
877 dispatch = device->dispatch;
879 dispatch->interface->destroy(dispatch);
881 motion_filter_destroy(device->pointer.filter);
882 libinput_seat_unref(device->base.seat);
883 libevdev_free(device->evdev);
884 free(device->mt.slots);
885 free(device->devnode);
886 free(device->sysname);