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)
115 struct libinput_device *base = &device->base;
116 struct libinput_seat *seat = base->seat;
118 slot = device->mt.slot;
120 switch (device->pending_event) {
123 case EVDEV_RELATIVE_MOTION:
124 pointer_notify_motion(base,
131 case EVDEV_ABSOLUTE_MT_DOWN:
132 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
135 if (device->mt.slots[slot].seat_slot != -1) {
136 log_bug("%s: Driver sent multiple touch down for the "
137 "same slot", device->devnode);
141 seat_slot = ffs(~seat->slot_map) - 1;
142 device->mt.slots[slot].seat_slot = seat_slot;
147 seat->slot_map |= 1 << seat_slot;
148 x = li_fixed_from_int(device->mt.slots[slot].x);
149 y = li_fixed_from_int(device->mt.slots[slot].y);
151 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
153 case EVDEV_ABSOLUTE_MT_MOTION:
154 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
157 seat_slot = device->mt.slots[slot].seat_slot;
158 x = li_fixed_from_int(device->mt.slots[slot].x);
159 y = li_fixed_from_int(device->mt.slots[slot].y);
164 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
166 case EVDEV_ABSOLUTE_MT_UP:
167 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
170 seat_slot = device->mt.slots[slot].seat_slot;
171 device->mt.slots[slot].seat_slot = -1;
176 seat->slot_map &= ~(1 << seat_slot);
178 touch_notify_touch_up(base, time, slot, seat_slot);
180 case EVDEV_ABSOLUTE_TOUCH_DOWN:
181 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
184 if (device->abs.seat_slot != -1) {
185 log_bug("%s: Driver sent multiple touch down for the "
186 "same slot", device->devnode);
190 seat_slot = ffs(~seat->slot_map) - 1;
191 device->abs.seat_slot = seat_slot;
196 seat->slot_map |= 1 << seat_slot;
198 transform_absolute(device, &cx, &cy);
199 x = li_fixed_from_int(cx);
200 y = li_fixed_from_int(cy);
202 touch_notify_touch_down(base, time, -1, seat_slot, x, y);
204 case EVDEV_ABSOLUTE_MOTION:
205 transform_absolute(device, &cx, &cy);
206 x = li_fixed_from_int(cx);
207 y = li_fixed_from_int(cy);
209 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
210 seat_slot = device->abs.seat_slot;
215 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
216 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
217 pointer_notify_motion_absolute(base, time, x, y);
220 case EVDEV_ABSOLUTE_TOUCH_UP:
221 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
224 seat_slot = device->abs.seat_slot;
225 device->abs.seat_slot = -1;
230 seat->slot_map &= ~(1 << seat_slot);
232 touch_notify_touch_up(base, time, -1, seat_slot);
235 assert(0 && "Unknown pending event type");
239 device->pending_event = EVDEV_NONE;
243 evdev_process_touch_button(struct evdev_device *device, int time, int value)
245 if (device->pending_event != EVDEV_NONE &&
246 device->pending_event != EVDEV_ABSOLUTE_MOTION)
247 evdev_flush_pending_event(device, time);
249 device->pending_event = (value ?
250 EVDEV_ABSOLUTE_TOUCH_DOWN :
251 EVDEV_ABSOLUTE_TOUCH_UP);
255 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
257 /* ignore kernel key repeat */
261 if (e->code == BTN_TOUCH) {
263 evdev_process_touch_button(device, time, e->value);
267 evdev_flush_pending_event(device, time);
278 pointer_notify_button(
282 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
283 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
291 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
292 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
298 evdev_process_touch(struct evdev_device *device,
299 struct input_event *e,
304 evdev_flush_pending_event(device, time);
305 device->mt.slot = e->value;
307 case ABS_MT_TRACKING_ID:
308 if (device->pending_event != EVDEV_NONE &&
309 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
310 evdev_flush_pending_event(device, time);
312 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
314 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
316 case ABS_MT_POSITION_X:
317 device->mt.slots[device->mt.slot].x = e->value;
318 if (device->pending_event == EVDEV_NONE)
319 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
321 case ABS_MT_POSITION_Y:
322 device->mt.slots[device->mt.slot].y = e->value;
323 if (device->pending_event == EVDEV_NONE)
324 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
330 evdev_process_absolute_motion(struct evdev_device *device,
331 struct input_event *e)
335 device->abs.x = e->value;
336 if (device->pending_event == EVDEV_NONE)
337 device->pending_event = EVDEV_ABSOLUTE_MOTION;
340 device->abs.y = e->value;
341 if (device->pending_event == EVDEV_NONE)
342 device->pending_event = EVDEV_ABSOLUTE_MOTION;
348 evdev_process_relative(struct evdev_device *device,
349 struct input_event *e, uint32_t time)
351 struct libinput_device *base = &device->base;
355 if (device->pending_event != EVDEV_RELATIVE_MOTION)
356 evdev_flush_pending_event(device, time);
357 device->rel.dx += li_fixed_from_int(e->value);
358 device->pending_event = EVDEV_RELATIVE_MOTION;
361 if (device->pending_event != EVDEV_RELATIVE_MOTION)
362 evdev_flush_pending_event(device, time);
363 device->rel.dy += li_fixed_from_int(e->value);
364 device->pending_event = EVDEV_RELATIVE_MOTION;
367 evdev_flush_pending_event(device, time);
371 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
372 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
375 evdev_flush_pending_event(device, time);
384 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
385 e->value * DEFAULT_AXIS_STEP_DISTANCE);
395 evdev_process_absolute(struct evdev_device *device,
396 struct input_event *e,
400 evdev_process_touch(device, e, time);
402 evdev_process_absolute_motion(device, e);
407 evdev_need_touch_frame(struct evdev_device *device)
409 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
412 switch (device->pending_event) {
414 case EVDEV_RELATIVE_MOTION:
416 case EVDEV_ABSOLUTE_MT_DOWN:
417 case EVDEV_ABSOLUTE_MT_MOTION:
418 case EVDEV_ABSOLUTE_MT_UP:
419 case EVDEV_ABSOLUTE_TOUCH_DOWN:
420 case EVDEV_ABSOLUTE_TOUCH_UP:
421 case EVDEV_ABSOLUTE_MOTION:
429 fallback_process(struct evdev_dispatch *dispatch,
430 struct evdev_device *device,
431 struct input_event *event,
436 switch (event->type) {
438 evdev_process_relative(device, event, time);
441 evdev_process_absolute(device, event, time);
444 evdev_process_key(device, event, time);
447 need_frame = evdev_need_touch_frame(device);
448 evdev_flush_pending_event(device, time);
450 touch_notify_frame(&device->base, time);
456 fallback_destroy(struct evdev_dispatch *dispatch)
461 struct evdev_dispatch_interface fallback_interface = {
466 static struct evdev_dispatch *
467 fallback_dispatch_create(void)
469 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
470 if (dispatch == NULL)
473 dispatch->interface = &fallback_interface;
479 evdev_process_event(struct evdev_device *device, struct input_event *e)
481 struct evdev_dispatch *dispatch = device->dispatch;
482 uint32_t time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
484 dispatch->interface->process(dispatch, device, e, time);
488 evdev_device_dispatch_one(struct evdev_device *device,
489 struct input_event *ev)
491 if (!device->mtdev) {
492 evdev_process_event(device, ev);
494 mtdev_put_event(device->mtdev, ev);
495 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
496 while (!mtdev_empty(device->mtdev)) {
497 struct input_event e;
498 mtdev_get_event(device->mtdev, &e);
499 evdev_process_event(device, &e);
506 evdev_sync_device(struct evdev_device *device)
508 struct input_event ev;
512 rc = libevdev_next_event(device->evdev,
513 LIBEVDEV_READ_FLAG_SYNC, &ev);
516 evdev_device_dispatch_one(device, &ev);
517 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
519 return rc == -EAGAIN ? 0 : rc;
523 evdev_device_dispatch(void *data)
525 struct evdev_device *device = data;
526 struct libinput *libinput = device->base.seat->libinput;
527 struct input_event ev;
530 /* If the compositor is repainting, this function is called only once
531 * per frame and we have to process all the events available on the
532 * fd, otherwise there will be input lag. */
534 rc = libevdev_next_event(device->evdev,
535 LIBEVDEV_READ_FLAG_NORMAL, &ev);
536 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
537 /* send one more sync event so we handle all
538 currently pending events before we sync up
539 to the current state */
540 ev.code = SYN_REPORT;
541 evdev_device_dispatch_one(device, &ev);
543 rc = evdev_sync_device(device);
545 rc = LIBEVDEV_READ_STATUS_SUCCESS;
546 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
547 evdev_device_dispatch_one(device, &ev);
549 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
551 if (rc != -EAGAIN && rc != -EINTR) {
552 libinput_remove_source(libinput, device->source);
553 device->source = NULL;
558 evdev_configure_device(struct evdev_device *device)
560 struct libevdev *evdev = device->evdev;
561 const struct input_absinfo *absinfo;
562 int has_abs, has_rel, has_mt;
563 int has_button, has_keyboard, has_touch;
564 struct mt_slot *slots;
577 if (libevdev_has_event_type(evdev, EV_ABS)) {
579 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
580 device->abs.min_x = absinfo->minimum;
581 device->abs.max_x = absinfo->maximum;
584 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
585 device->abs.min_y = absinfo->minimum;
586 device->abs.max_y = absinfo->maximum;
589 /* We only handle the slotted Protocol B in weston.
590 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
591 require mtdev for conversion. */
592 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
593 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
594 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
595 device->abs.min_x = absinfo->minimum;
596 device->abs.max_x = absinfo->maximum;
597 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
598 device->abs.min_y = absinfo->minimum;
599 device->abs.max_y = absinfo->maximum;
604 if (!libevdev_has_event_code(evdev,
605 EV_ABS, ABS_MT_SLOT)) {
606 device->mtdev = mtdev_new_open(device->fd);
610 num_slots = device->mtdev->caps.slot.maximum;
611 if (device->mtdev->caps.slot.minimum < 0 ||
614 active_slot = device->mtdev->caps.slot.value;
616 num_slots = libevdev_get_num_slots(device->evdev);
617 active_slot = libevdev_get_current_slot(evdev);
620 slots = calloc(num_slots, sizeof(struct mt_slot));
624 for (slot = 0; slot < num_slots; ++slot) {
625 slots[slot].seat_slot = -1;
629 device->mt.slots = slots;
630 device->mt.slots_len = num_slots;
631 device->mt.slot = active_slot;
634 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
635 libevdev_has_event_code(evdev, EV_REL, REL_Y))
638 if (libevdev_has_event_type(evdev, EV_KEY)) {
639 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
640 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
641 (has_abs || has_mt)) {
642 device->dispatch = evdev_mt_touchpad_create(device);
643 log_info("input device '%s', %s is a touchpad\n",
644 device->devname, device->devnode);
646 for (i = KEY_ESC; i < KEY_MAX; i++) {
647 if (i >= BTN_MISC && i < KEY_OK)
649 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
654 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
656 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
657 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
663 if (libevdev_has_event_type(evdev, EV_LED))
666 if ((has_abs || has_rel) && has_button) {
667 device->seat_caps |= EVDEV_DEVICE_POINTER;
668 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
669 device->devname, device->devnode,
670 has_abs ? " absolute-motion" : "",
671 has_rel ? " relative-motion": "",
672 has_button ? " button" : "");
675 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
676 log_info("input device '%s', %s is a keyboard\n",
677 device->devname, device->devnode);
679 if (has_touch && !has_button) {
680 device->seat_caps |= EVDEV_DEVICE_TOUCH;
681 log_info("input device '%s', %s is a touch device\n",
682 device->devname, device->devnode);
688 struct evdev_device *
689 evdev_device_create(struct libinput_seat *seat,
693 struct libinput *libinput = seat->libinput;
694 struct evdev_device *device;
697 int unhandled_device = 0;
699 /* Use non-blocking mode so that we can loop on read on
700 * evdev_device_data() until all events on the fd are
701 * read. mtdev_get() also expects this. */
702 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
704 log_info("opening input device '%s' failed (%s).\n",
705 devnode, strerror(-fd));
709 device = zalloc(sizeof *device);
713 libinput_device_init(&device->base, seat);
715 rc = libevdev_new_from_fd(fd, &device->evdev);
719 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
721 device->seat_caps = 0;
723 device->mtdev = NULL;
724 device->devnode = strdup(devnode);
725 device->sysname = strdup(sysname);
728 device->abs.seat_slot = -1;
729 device->dispatch = NULL;
731 device->pending_event = EVDEV_NONE;
732 device->devname = libevdev_get_name(device->evdev);
734 libinput_seat_ref(seat);
736 if (evdev_configure_device(device) == -1)
739 if (device->seat_caps == 0) {
740 unhandled_device = 1;
744 /* If the dispatch was not set up use the fallback. */
745 if (device->dispatch == NULL)
746 device->dispatch = fallback_dispatch_create();
747 if (device->dispatch == NULL)
751 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
755 list_insert(seat->devices_list.prev, &device->base.link);
756 notify_added_device(&device->base);
762 close_restricted(libinput, fd);
763 evdev_device_destroy(device);
765 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
769 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
773 memset(keys, 0, size);
774 len = ioctl(device->fd, EVIOCGKEY(size), keys);
776 return (len == -1) ? -errno : len;
780 evdev_device_get_output(struct evdev_device *device)
782 return device->output_name;
786 evdev_device_get_sysname(struct evdev_device *device)
788 return device->sysname;
792 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
794 device->abs.apply_calibration = 1;
795 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
799 evdev_device_has_capability(struct evdev_device *device,
800 enum libinput_device_capability capability)
802 switch (capability) {
803 case LIBINPUT_DEVICE_CAP_POINTER:
804 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
805 case LIBINPUT_DEVICE_CAP_KEYBOARD:
806 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
807 case LIBINPUT_DEVICE_CAP_TOUCH:
808 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
815 evdev_device_remove(struct evdev_device *device)
818 libinput_remove_source(device->base.seat->libinput,
822 mtdev_close_delete(device->mtdev);
823 close_restricted(device->base.seat->libinput, device->fd);
825 list_remove(&device->base.link);
827 notify_removed_device(&device->base);
828 libinput_device_unref(&device->base);
832 evdev_device_destroy(struct evdev_device *device)
834 struct evdev_dispatch *dispatch;
836 dispatch = device->dispatch;
838 dispatch->interface->destroy(dispatch);
840 libinput_seat_unref(device->base.seat);
841 libevdev_free(device->evdev);
842 free(device->mt.slots);
843 free(device->devnode);
844 free(device->sysname);