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)
114 struct libinput_device *base = &device->base;
115 struct libinput_seat *seat = base->seat;
117 slot = device->mt.slot;
119 switch (device->pending_event) {
122 case EVDEV_RELATIVE_MOTION:
123 pointer_notify_motion(base,
130 case EVDEV_ABSOLUTE_MT_DOWN:
131 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
134 seat_slot = ffs(~seat->slot_map) - 1;
135 device->mt.slots[slot].seat_slot = seat_slot;
140 seat->slot_map |= 1 << seat_slot;
142 touch_notify_touch(base,
146 li_fixed_from_int(device->mt.slots[slot].x),
147 li_fixed_from_int(device->mt.slots[slot].y),
148 LIBINPUT_TOUCH_TYPE_DOWN);
150 case EVDEV_ABSOLUTE_MT_MOTION:
151 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
154 seat_slot = device->mt.slots[slot].seat_slot;
159 touch_notify_touch(base,
163 li_fixed_from_int(device->mt.slots[slot].x),
164 li_fixed_from_int(device->mt.slots[slot].y),
165 LIBINPUT_TOUCH_TYPE_MOTION);
167 case EVDEV_ABSOLUTE_MT_UP:
168 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
171 seat_slot = device->mt.slots[slot].seat_slot;
176 seat->slot_map &= ~(1 << seat_slot);
178 touch_notify_touch(base,
183 LIBINPUT_TOUCH_TYPE_UP);
185 case EVDEV_ABSOLUTE_TOUCH_DOWN:
186 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
189 seat_slot = ffs(~seat->slot_map) - 1;
190 device->abs.seat_slot = seat_slot;
195 seat->slot_map |= 1 << seat_slot;
197 transform_absolute(device, &cx, &cy);
198 touch_notify_touch(base,
202 li_fixed_from_int(cx),
203 li_fixed_from_int(cy),
204 LIBINPUT_TOUCH_TYPE_DOWN);
206 case EVDEV_ABSOLUTE_MOTION:
207 transform_absolute(device, &cx, &cy);
208 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
209 seat_slot = device->abs.seat_slot;
214 touch_notify_touch(base,
218 li_fixed_from_int(cx),
219 li_fixed_from_int(cy),
220 LIBINPUT_TOUCH_TYPE_DOWN);
221 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
222 pointer_notify_motion_absolute(base,
224 li_fixed_from_int(cx),
225 li_fixed_from_int(cy));
228 case EVDEV_ABSOLUTE_TOUCH_UP:
229 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
232 seat_slot = device->abs.seat_slot;
237 seat->slot_map &= ~(1 << seat_slot);
239 touch_notify_touch(base,
244 LIBINPUT_TOUCH_TYPE_UP);
247 assert(0 && "Unknown pending event type");
251 device->pending_event = EVDEV_NONE;
255 evdev_process_touch_button(struct evdev_device *device, int time, int value)
257 if (device->pending_event != EVDEV_NONE &&
258 device->pending_event != EVDEV_ABSOLUTE_MOTION)
259 evdev_flush_pending_event(device, time);
261 device->pending_event = (value ?
262 EVDEV_ABSOLUTE_TOUCH_DOWN :
263 EVDEV_ABSOLUTE_TOUCH_UP);
267 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
269 /* ignore kernel key repeat */
273 if (e->code == BTN_TOUCH) {
275 evdev_process_touch_button(device, time, e->value);
279 evdev_flush_pending_event(device, time);
290 pointer_notify_button(
294 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
295 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
303 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
304 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
310 evdev_process_touch(struct evdev_device *device,
311 struct input_event *e,
316 evdev_flush_pending_event(device, time);
317 device->mt.slot = e->value;
319 case ABS_MT_TRACKING_ID:
320 if (device->pending_event != EVDEV_NONE &&
321 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
322 evdev_flush_pending_event(device, time);
324 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
326 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
328 case ABS_MT_POSITION_X:
329 device->mt.slots[device->mt.slot].x = e->value;
330 if (device->pending_event == EVDEV_NONE)
331 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
333 case ABS_MT_POSITION_Y:
334 device->mt.slots[device->mt.slot].y = e->value;
335 if (device->pending_event == EVDEV_NONE)
336 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
342 evdev_process_absolute_motion(struct evdev_device *device,
343 struct input_event *e)
347 device->abs.x = e->value;
348 if (device->pending_event == EVDEV_NONE)
349 device->pending_event = EVDEV_ABSOLUTE_MOTION;
352 device->abs.y = e->value;
353 if (device->pending_event == EVDEV_NONE)
354 device->pending_event = EVDEV_ABSOLUTE_MOTION;
360 evdev_process_relative(struct evdev_device *device,
361 struct input_event *e, uint32_t time)
363 struct libinput_device *base = &device->base;
367 if (device->pending_event != EVDEV_RELATIVE_MOTION)
368 evdev_flush_pending_event(device, time);
369 device->rel.dx += li_fixed_from_int(e->value);
370 device->pending_event = EVDEV_RELATIVE_MOTION;
373 if (device->pending_event != EVDEV_RELATIVE_MOTION)
374 evdev_flush_pending_event(device, time);
375 device->rel.dy += li_fixed_from_int(e->value);
376 device->pending_event = EVDEV_RELATIVE_MOTION;
379 evdev_flush_pending_event(device, time);
383 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
384 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
387 evdev_flush_pending_event(device, time);
396 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
397 e->value * DEFAULT_AXIS_STEP_DISTANCE);
407 evdev_process_absolute(struct evdev_device *device,
408 struct input_event *e,
412 evdev_process_touch(device, e, time);
414 evdev_process_absolute_motion(device, e);
419 evdev_need_touch_frame(struct evdev_device *device)
421 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
424 switch (device->pending_event) {
426 case EVDEV_RELATIVE_MOTION:
428 case EVDEV_ABSOLUTE_MT_DOWN:
429 case EVDEV_ABSOLUTE_MT_MOTION:
430 case EVDEV_ABSOLUTE_MT_UP:
431 case EVDEV_ABSOLUTE_TOUCH_DOWN:
432 case EVDEV_ABSOLUTE_TOUCH_UP:
433 case EVDEV_ABSOLUTE_MOTION:
441 fallback_process(struct evdev_dispatch *dispatch,
442 struct evdev_device *device,
443 struct input_event *event,
448 switch (event->type) {
450 evdev_process_relative(device, event, time);
453 evdev_process_absolute(device, event, time);
456 evdev_process_key(device, event, time);
459 need_frame = evdev_need_touch_frame(device);
460 evdev_flush_pending_event(device, time);
462 touch_notify_frame(&device->base, time);
468 fallback_destroy(struct evdev_dispatch *dispatch)
473 struct evdev_dispatch_interface fallback_interface = {
478 static struct evdev_dispatch *
479 fallback_dispatch_create(void)
481 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
482 if (dispatch == NULL)
485 dispatch->interface = &fallback_interface;
491 evdev_process_event(struct evdev_device *device, struct input_event *e)
493 struct evdev_dispatch *dispatch = device->dispatch;
494 uint32_t time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
496 dispatch->interface->process(dispatch, device, e, time);
500 evdev_device_dispatch_one(struct evdev_device *device,
501 struct input_event *ev)
503 if (!device->mtdev) {
504 evdev_process_event(device, ev);
506 mtdev_put_event(device->mtdev, ev);
507 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
508 while (!mtdev_empty(device->mtdev)) {
509 struct input_event e;
510 mtdev_get_event(device->mtdev, &e);
511 evdev_process_event(device, &e);
518 evdev_sync_device(struct evdev_device *device)
520 struct input_event ev;
524 rc = libevdev_next_event(device->evdev,
525 LIBEVDEV_READ_FLAG_SYNC, &ev);
528 evdev_device_dispatch_one(device, &ev);
529 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
531 return rc == -EAGAIN ? 0 : rc;
535 evdev_device_dispatch(void *data)
537 struct evdev_device *device = data;
538 struct libinput *libinput = device->base.seat->libinput;
539 struct input_event ev;
542 /* If the compositor is repainting, this function is called only once
543 * per frame and we have to process all the events available on the
544 * fd, otherwise there will be input lag. */
546 rc = libevdev_next_event(device->evdev,
547 LIBEVDEV_READ_FLAG_NORMAL, &ev);
548 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
549 /* send one more sync event so we handle all
550 currently pending events before we sync up
551 to the current state */
552 ev.code = SYN_REPORT;
553 evdev_device_dispatch_one(device, &ev);
555 rc = evdev_sync_device(device);
557 rc = LIBEVDEV_READ_STATUS_SUCCESS;
558 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
559 evdev_device_dispatch_one(device, &ev);
561 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
563 if (rc != -EAGAIN && rc != -EINTR) {
564 libinput_remove_source(libinput, device->source);
565 device->source = NULL;
570 evdev_configure_device(struct evdev_device *device)
572 const struct input_absinfo *absinfo;
573 int has_abs, has_rel, has_mt;
574 int has_button, has_keyboard, has_touch;
584 if (libevdev_has_event_type(device->evdev, EV_ABS)) {
586 if ((absinfo = libevdev_get_abs_info(device->evdev, ABS_X))) {
587 device->abs.min_x = absinfo->minimum;
588 device->abs.max_x = absinfo->maximum;
591 if ((absinfo = libevdev_get_abs_info(device->evdev, ABS_Y))) {
592 device->abs.min_y = absinfo->minimum;
593 device->abs.max_y = absinfo->maximum;
596 /* We only handle the slotted Protocol B in weston.
597 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
598 require mtdev for conversion. */
599 if (libevdev_has_event_code(device->evdev, EV_ABS, ABS_MT_POSITION_X) &&
600 libevdev_has_event_code(device->evdev, EV_ABS, ABS_MT_POSITION_Y)) {
601 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_POSITION_X);
602 device->abs.min_x = absinfo->minimum;
603 device->abs.max_x = absinfo->maximum;
604 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_POSITION_Y);
605 device->abs.min_y = absinfo->minimum;
606 device->abs.max_y = absinfo->maximum;
611 if (!libevdev_has_event_code(device->evdev, EV_ABS, ABS_MT_SLOT)) {
612 device->mtdev = mtdev_new_open(device->fd);
615 device->mt.slot = device->mtdev->caps.slot.value;
617 device->mt.slot = libevdev_get_current_slot(device->evdev);
621 if (libevdev_has_event_code(device->evdev, EV_REL, REL_X) ||
622 libevdev_has_event_code(device->evdev, EV_REL, REL_Y))
625 if (libevdev_has_event_type(device->evdev, EV_KEY)) {
626 if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_TOOL_FINGER) &&
627 !libevdev_has_event_code(device->evdev, EV_KEY, BTN_TOOL_PEN) &&
628 (has_abs || has_mt)) {
629 device->dispatch = evdev_touchpad_create(device);
631 for (i = KEY_ESC; i < KEY_MAX; i++) {
632 if (i >= BTN_MISC && i < KEY_OK)
634 if (libevdev_has_event_code(device->evdev, EV_KEY, i)) {
639 if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_TOUCH))
641 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
642 if (libevdev_has_event_code(device->evdev, EV_KEY, i)) {
648 if (libevdev_has_event_type(device->evdev, EV_LED))
651 if ((has_abs || has_rel) && has_button)
652 device->seat_caps |= EVDEV_DEVICE_POINTER;
654 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
655 if (has_touch && !has_button)
656 device->seat_caps |= EVDEV_DEVICE_TOUCH;
661 struct evdev_device *
662 evdev_device_create(struct libinput_seat *seat,
666 struct libinput *libinput = seat->libinput;
667 struct evdev_device *device;
670 int unhandled_device = 0;
672 /* Use non-blocking mode so that we can loop on read on
673 * evdev_device_data() until all events on the fd are
674 * read. mtdev_get() also expects this. */
675 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
677 log_info("opening input device '%s' failed (%s).\n",
678 devnode, strerror(-fd));
682 device = zalloc(sizeof *device);
686 libinput_device_init(&device->base, seat);
688 rc = libevdev_new_from_fd(fd, &device->evdev);
692 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
694 device->seat_caps = 0;
696 device->mtdev = NULL;
697 device->devnode = strdup(devnode);
698 device->sysname = strdup(sysname);
699 device->mt.slot = -1;
702 device->dispatch = NULL;
704 device->pending_event = EVDEV_NONE;
705 device->devname = libevdev_get_name(device->evdev);
707 libinput_seat_ref(seat);
709 if (evdev_configure_device(device) == -1)
712 if (device->seat_caps == 0) {
713 unhandled_device = 1;
717 /* If the dispatch was not set up use the fallback. */
718 if (device->dispatch == NULL)
719 device->dispatch = fallback_dispatch_create();
720 if (device->dispatch == NULL)
724 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
728 list_insert(seat->devices_list.prev, &device->base.link);
729 notify_added_device(&device->base);
735 close_restricted(libinput, fd);
736 evdev_device_destroy(device);
738 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
742 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
744 memset(keys, 0, size);
745 return ioctl(device->fd, EVIOCGKEY(size), keys);
749 evdev_device_get_output(struct evdev_device *device)
751 return device->output_name;
755 evdev_device_get_sysname(struct evdev_device *device)
757 return device->sysname;
761 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
763 device->abs.apply_calibration = 1;
764 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
768 evdev_device_has_capability(struct evdev_device *device,
769 enum libinput_device_capability capability)
771 switch (capability) {
772 case LIBINPUT_DEVICE_CAP_POINTER:
773 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
774 case LIBINPUT_DEVICE_CAP_KEYBOARD:
775 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
776 case LIBINPUT_DEVICE_CAP_TOUCH:
777 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
784 evdev_device_remove(struct evdev_device *device)
787 libinput_remove_source(device->base.seat->libinput,
791 mtdev_close_delete(device->mtdev);
792 close_restricted(device->base.seat->libinput, device->fd);
794 list_remove(&device->base.link);
796 notify_removed_device(&device->base);
797 libinput_device_unref(&device->base);
801 evdev_device_destroy(struct evdev_device *device)
803 struct evdev_dispatch *dispatch;
805 dispatch = device->dispatch;
807 dispatch->interface->destroy(dispatch);
809 libinput_seat_unref(device->base.seat);
810 libevdev_free(device->evdev);
811 free(device->devnode);
812 free(device->sysname);