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 seat_slot = ffs(~seat->slot_map) - 1;
136 device->mt.slots[slot].seat_slot = seat_slot;
141 seat->slot_map |= 1 << seat_slot;
142 x = li_fixed_from_int(device->mt.slots[slot].x);
143 y = li_fixed_from_int(device->mt.slots[slot].y);
145 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
147 case EVDEV_ABSOLUTE_MT_MOTION:
148 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
151 seat_slot = device->mt.slots[slot].seat_slot;
152 x = li_fixed_from_int(device->mt.slots[slot].x);
153 y = li_fixed_from_int(device->mt.slots[slot].y);
158 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
160 case EVDEV_ABSOLUTE_MT_UP:
161 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
164 seat_slot = device->mt.slots[slot].seat_slot;
169 seat->slot_map &= ~(1 << seat_slot);
171 touch_notify_touch_up(base, time, slot, seat_slot);
173 case EVDEV_ABSOLUTE_TOUCH_DOWN:
174 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
177 seat_slot = ffs(~seat->slot_map) - 1;
178 device->abs.seat_slot = seat_slot;
183 seat->slot_map |= 1 << seat_slot;
185 transform_absolute(device, &cx, &cy);
186 x = li_fixed_from_int(cx);
187 y = li_fixed_from_int(cy);
189 touch_notify_touch_down(base, time, -1, seat_slot, x, y);
191 case EVDEV_ABSOLUTE_MOTION:
192 transform_absolute(device, &cx, &cy);
193 x = li_fixed_from_int(cx);
194 y = li_fixed_from_int(cy);
196 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
197 seat_slot = device->abs.seat_slot;
202 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
203 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
204 pointer_notify_motion_absolute(base, time, x, y);
207 case EVDEV_ABSOLUTE_TOUCH_UP:
208 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
211 seat_slot = device->abs.seat_slot;
216 seat->slot_map &= ~(1 << seat_slot);
218 touch_notify_touch_up(base, time, -1, seat_slot);
221 assert(0 && "Unknown pending event type");
225 device->pending_event = EVDEV_NONE;
229 evdev_process_touch_button(struct evdev_device *device, int time, int value)
231 if (device->pending_event != EVDEV_NONE &&
232 device->pending_event != EVDEV_ABSOLUTE_MOTION)
233 evdev_flush_pending_event(device, time);
235 device->pending_event = (value ?
236 EVDEV_ABSOLUTE_TOUCH_DOWN :
237 EVDEV_ABSOLUTE_TOUCH_UP);
241 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
243 /* ignore kernel key repeat */
247 if (e->code == BTN_TOUCH) {
249 evdev_process_touch_button(device, time, e->value);
253 evdev_flush_pending_event(device, time);
264 pointer_notify_button(
268 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
269 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
277 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
278 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
284 evdev_process_touch(struct evdev_device *device,
285 struct input_event *e,
290 evdev_flush_pending_event(device, time);
291 device->mt.slot = e->value;
293 case ABS_MT_TRACKING_ID:
294 if (device->pending_event != EVDEV_NONE &&
295 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
296 evdev_flush_pending_event(device, time);
298 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
300 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
302 case ABS_MT_POSITION_X:
303 device->mt.slots[device->mt.slot].x = e->value;
304 if (device->pending_event == EVDEV_NONE)
305 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
307 case ABS_MT_POSITION_Y:
308 device->mt.slots[device->mt.slot].y = e->value;
309 if (device->pending_event == EVDEV_NONE)
310 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
316 evdev_process_absolute_motion(struct evdev_device *device,
317 struct input_event *e)
321 device->abs.x = e->value;
322 if (device->pending_event == EVDEV_NONE)
323 device->pending_event = EVDEV_ABSOLUTE_MOTION;
326 device->abs.y = e->value;
327 if (device->pending_event == EVDEV_NONE)
328 device->pending_event = EVDEV_ABSOLUTE_MOTION;
334 evdev_process_relative(struct evdev_device *device,
335 struct input_event *e, uint32_t time)
337 struct libinput_device *base = &device->base;
341 if (device->pending_event != EVDEV_RELATIVE_MOTION)
342 evdev_flush_pending_event(device, time);
343 device->rel.dx += li_fixed_from_int(e->value);
344 device->pending_event = EVDEV_RELATIVE_MOTION;
347 if (device->pending_event != EVDEV_RELATIVE_MOTION)
348 evdev_flush_pending_event(device, time);
349 device->rel.dy += li_fixed_from_int(e->value);
350 device->pending_event = EVDEV_RELATIVE_MOTION;
353 evdev_flush_pending_event(device, time);
357 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
358 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
361 evdev_flush_pending_event(device, time);
370 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
371 e->value * DEFAULT_AXIS_STEP_DISTANCE);
381 evdev_process_absolute(struct evdev_device *device,
382 struct input_event *e,
386 evdev_process_touch(device, e, time);
388 evdev_process_absolute_motion(device, e);
393 evdev_need_touch_frame(struct evdev_device *device)
395 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
398 switch (device->pending_event) {
400 case EVDEV_RELATIVE_MOTION:
402 case EVDEV_ABSOLUTE_MT_DOWN:
403 case EVDEV_ABSOLUTE_MT_MOTION:
404 case EVDEV_ABSOLUTE_MT_UP:
405 case EVDEV_ABSOLUTE_TOUCH_DOWN:
406 case EVDEV_ABSOLUTE_TOUCH_UP:
407 case EVDEV_ABSOLUTE_MOTION:
415 fallback_process(struct evdev_dispatch *dispatch,
416 struct evdev_device *device,
417 struct input_event *event,
422 switch (event->type) {
424 evdev_process_relative(device, event, time);
427 evdev_process_absolute(device, event, time);
430 evdev_process_key(device, event, time);
433 need_frame = evdev_need_touch_frame(device);
434 evdev_flush_pending_event(device, time);
436 touch_notify_frame(&device->base, time);
442 fallback_destroy(struct evdev_dispatch *dispatch)
447 struct evdev_dispatch_interface fallback_interface = {
452 static struct evdev_dispatch *
453 fallback_dispatch_create(void)
455 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
456 if (dispatch == NULL)
459 dispatch->interface = &fallback_interface;
465 evdev_process_event(struct evdev_device *device, struct input_event *e)
467 struct evdev_dispatch *dispatch = device->dispatch;
468 uint32_t time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
470 dispatch->interface->process(dispatch, device, e, time);
474 evdev_device_dispatch_one(struct evdev_device *device,
475 struct input_event *ev)
477 if (!device->mtdev) {
478 evdev_process_event(device, ev);
480 mtdev_put_event(device->mtdev, ev);
481 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
482 while (!mtdev_empty(device->mtdev)) {
483 struct input_event e;
484 mtdev_get_event(device->mtdev, &e);
485 evdev_process_event(device, &e);
492 evdev_sync_device(struct evdev_device *device)
494 struct input_event ev;
498 rc = libevdev_next_event(device->evdev,
499 LIBEVDEV_READ_FLAG_SYNC, &ev);
502 evdev_device_dispatch_one(device, &ev);
503 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
505 return rc == -EAGAIN ? 0 : rc;
509 evdev_device_dispatch(void *data)
511 struct evdev_device *device = data;
512 struct libinput *libinput = device->base.seat->libinput;
513 struct input_event ev;
516 /* If the compositor is repainting, this function is called only once
517 * per frame and we have to process all the events available on the
518 * fd, otherwise there will be input lag. */
520 rc = libevdev_next_event(device->evdev,
521 LIBEVDEV_READ_FLAG_NORMAL, &ev);
522 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
523 /* send one more sync event so we handle all
524 currently pending events before we sync up
525 to the current state */
526 ev.code = SYN_REPORT;
527 evdev_device_dispatch_one(device, &ev);
529 rc = evdev_sync_device(device);
531 rc = LIBEVDEV_READ_STATUS_SUCCESS;
532 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
533 evdev_device_dispatch_one(device, &ev);
535 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
537 if (rc != -EAGAIN && rc != -EINTR) {
538 libinput_remove_source(libinput, device->source);
539 device->source = NULL;
544 evdev_configure_device(struct evdev_device *device)
546 struct libevdev *evdev = device->evdev;
547 const struct input_absinfo *absinfo;
548 int has_abs, has_rel, has_mt;
549 int has_button, has_keyboard, has_touch;
550 struct mt_slot *slots;
563 if (libevdev_has_event_type(evdev, EV_ABS)) {
565 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
566 device->abs.min_x = absinfo->minimum;
567 device->abs.max_x = absinfo->maximum;
570 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
571 device->abs.min_y = absinfo->minimum;
572 device->abs.max_y = absinfo->maximum;
575 /* We only handle the slotted Protocol B in weston.
576 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
577 require mtdev for conversion. */
578 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
579 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
580 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
581 device->abs.min_x = absinfo->minimum;
582 device->abs.max_x = absinfo->maximum;
583 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
584 device->abs.min_y = absinfo->minimum;
585 device->abs.max_y = absinfo->maximum;
590 if (!libevdev_has_event_code(evdev,
591 EV_ABS, ABS_MT_SLOT)) {
592 device->mtdev = mtdev_new_open(device->fd);
596 num_slots = device->mtdev->caps.slot.maximum;
597 if (device->mtdev->caps.slot.minimum < 0 ||
600 active_slot = device->mtdev->caps.slot.value;
602 num_slots = libevdev_get_num_slots(device->evdev);
603 active_slot = libevdev_get_current_slot(evdev);
606 slots = calloc(num_slots, sizeof(struct mt_slot));
610 for (slot = 0; slot < num_slots; ++slot) {
611 slots[slot].seat_slot = -1;
615 device->mt.slots = slots;
616 device->mt.slots_len = num_slots;
617 device->mt.slot = active_slot;
620 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
621 libevdev_has_event_code(evdev, EV_REL, REL_Y))
624 if (libevdev_has_event_type(evdev, EV_KEY)) {
625 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
626 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
627 (has_abs || has_mt)) {
628 device->dispatch = evdev_mt_touchpad_create(device);
629 log_info("input device '%s', %s is a touchpad\n",
630 device->devname, device->devnode);
632 for (i = KEY_ESC; i < KEY_MAX; i++) {
633 if (i >= BTN_MISC && i < KEY_OK)
635 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
640 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
642 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
643 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
649 if (libevdev_has_event_type(evdev, EV_LED))
652 if ((has_abs || has_rel) && has_button) {
653 device->seat_caps |= EVDEV_DEVICE_POINTER;
654 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
655 device->devname, device->devnode,
656 has_abs ? " absolute-motion" : "",
657 has_rel ? " relative-motion": "",
658 has_button ? " button" : "");
661 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
662 log_info("input device '%s', %s is a keyboard\n",
663 device->devname, device->devnode);
665 if (has_touch && !has_button) {
666 device->seat_caps |= EVDEV_DEVICE_TOUCH;
667 log_info("input device '%s', %s is a touch device\n",
668 device->devname, device->devnode);
674 struct evdev_device *
675 evdev_device_create(struct libinput_seat *seat,
679 struct libinput *libinput = seat->libinput;
680 struct evdev_device *device;
683 int unhandled_device = 0;
685 /* Use non-blocking mode so that we can loop on read on
686 * evdev_device_data() until all events on the fd are
687 * read. mtdev_get() also expects this. */
688 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
690 log_info("opening input device '%s' failed (%s).\n",
691 devnode, strerror(-fd));
695 device = zalloc(sizeof *device);
699 libinput_device_init(&device->base, seat);
701 rc = libevdev_new_from_fd(fd, &device->evdev);
705 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
707 device->seat_caps = 0;
709 device->mtdev = NULL;
710 device->devnode = strdup(devnode);
711 device->sysname = strdup(sysname);
714 device->dispatch = NULL;
716 device->pending_event = EVDEV_NONE;
717 device->devname = libevdev_get_name(device->evdev);
719 libinput_seat_ref(seat);
721 if (evdev_configure_device(device) == -1)
724 if (device->seat_caps == 0) {
725 unhandled_device = 1;
729 /* If the dispatch was not set up use the fallback. */
730 if (device->dispatch == NULL)
731 device->dispatch = fallback_dispatch_create();
732 if (device->dispatch == NULL)
736 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
740 list_insert(seat->devices_list.prev, &device->base.link);
741 notify_added_device(&device->base);
747 close_restricted(libinput, fd);
748 evdev_device_destroy(device);
750 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
754 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
758 memset(keys, 0, size);
759 len = ioctl(device->fd, EVIOCGKEY(size), keys);
761 return (len == -1) ? -errno : len;
765 evdev_device_get_output(struct evdev_device *device)
767 return device->output_name;
771 evdev_device_get_sysname(struct evdev_device *device)
773 return device->sysname;
777 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
779 device->abs.apply_calibration = 1;
780 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
784 evdev_device_has_capability(struct evdev_device *device,
785 enum libinput_device_capability capability)
787 switch (capability) {
788 case LIBINPUT_DEVICE_CAP_POINTER:
789 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
790 case LIBINPUT_DEVICE_CAP_KEYBOARD:
791 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
792 case LIBINPUT_DEVICE_CAP_TOUCH:
793 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
800 evdev_device_remove(struct evdev_device *device)
803 libinput_remove_source(device->base.seat->libinput,
807 mtdev_close_delete(device->mtdev);
808 close_restricted(device->base.seat->libinput, device->fd);
810 list_remove(&device->base.link);
812 notify_removed_device(&device->base);
813 libinput_device_unref(&device->base);
817 evdev_device_destroy(struct evdev_device *device)
819 struct evdev_dispatch *dispatch;
821 dispatch = device->dispatch;
823 dispatch->interface->destroy(dispatch);
825 libinput_seat_unref(device->base.seat);
826 libevdev_free(device->evdev);
827 free(device->mt.slots);
828 free(device->devnode);
829 free(device->sysname);