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>
37 #include "libinput-private.h"
39 #define DEFAULT_AXIS_STEP_DISTANCE li_fixed_from_int(10)
42 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
45 enum libinput_led weston;
48 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
49 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
50 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
52 struct input_event ev[ARRAY_LENGTH(map) + 1];
55 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
58 memset(ev, 0, sizeof(ev));
59 for (i = 0; i < ARRAY_LENGTH(map); i++) {
61 ev[i].code = map[i].evdev;
62 ev[i].value = !!(leds & map[i].weston);
65 ev[i].code = SYN_REPORT;
67 i = write(device->fd, ev, sizeof ev);
68 (void)i; /* no, we really don't care about the return value */
72 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
74 if (!device->abs.apply_calibration) {
79 *x = device->abs.x * device->abs.calibration[0] +
80 device->abs.y * device->abs.calibration[1] +
81 device->abs.calibration[2];
83 *y = device->abs.x * device->abs.calibration[3] +
84 device->abs.y * device->abs.calibration[4] +
85 device->abs.calibration[5];
90 evdev_device_transform_x(struct evdev_device *device,
94 return (x - li_fixed_from_int(device->abs.min_x)) * width /
95 (device->abs.max_x - device->abs.min_x + 1);
99 evdev_device_transform_y(struct evdev_device *device,
103 return (y - li_fixed_from_int(device->abs.min_y)) * height /
104 (device->abs.max_y - device->abs.min_y + 1);
108 evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
112 struct libinput_device *base = &device->base;
114 slot = device->mt.slot;
116 switch (device->pending_event) {
119 case EVDEV_RELATIVE_MOTION:
120 pointer_notify_motion(base,
127 case EVDEV_ABSOLUTE_MT_DOWN:
128 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
131 touch_notify_touch(base,
134 li_fixed_from_int(device->mt.slots[slot].x),
135 li_fixed_from_int(device->mt.slots[slot].y),
136 LIBINPUT_TOUCH_TYPE_DOWN);
138 case EVDEV_ABSOLUTE_MT_MOTION:
139 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
142 touch_notify_touch(base,
145 li_fixed_from_int(device->mt.slots[slot].x),
146 li_fixed_from_int(device->mt.slots[slot].y),
147 LIBINPUT_TOUCH_TYPE_MOTION);
149 case EVDEV_ABSOLUTE_MT_UP:
150 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
153 touch_notify_touch(base,
157 LIBINPUT_TOUCH_TYPE_UP);
159 case EVDEV_ABSOLUTE_TOUCH_DOWN:
160 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
163 transform_absolute(device, &cx, &cy);
164 touch_notify_touch(base,
167 li_fixed_from_int(cx),
168 li_fixed_from_int(cy),
169 LIBINPUT_TOUCH_TYPE_DOWN);
171 case EVDEV_ABSOLUTE_MOTION:
172 transform_absolute(device, &cx, &cy);
173 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
174 touch_notify_touch(base,
177 li_fixed_from_int(cx),
178 li_fixed_from_int(cy),
179 LIBINPUT_TOUCH_TYPE_DOWN);
180 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
181 pointer_notify_motion_absolute(base,
183 li_fixed_from_int(cx),
184 li_fixed_from_int(cy));
187 case EVDEV_ABSOLUTE_TOUCH_UP:
188 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
191 touch_notify_touch(base,
193 0, 0, 0, LIBINPUT_TOUCH_TYPE_UP);
196 assert(0 && "Unknown pending event type");
200 device->pending_event = EVDEV_NONE;
204 evdev_process_touch_button(struct evdev_device *device, int time, int value)
206 if (device->pending_event != EVDEV_NONE &&
207 device->pending_event != EVDEV_ABSOLUTE_MOTION)
208 evdev_flush_pending_event(device, time);
210 device->pending_event = (value ?
211 EVDEV_ABSOLUTE_TOUCH_DOWN :
212 EVDEV_ABSOLUTE_TOUCH_UP);
216 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
218 /* ignore kernel key repeat */
222 if (e->code == BTN_TOUCH) {
224 evdev_process_touch_button(device, time, e->value);
228 evdev_flush_pending_event(device, time);
239 pointer_notify_button(
243 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
244 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
252 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
253 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
259 evdev_process_touch(struct evdev_device *device,
260 struct input_event *e,
265 evdev_flush_pending_event(device, time);
266 device->mt.slot = e->value;
268 case ABS_MT_TRACKING_ID:
269 if (device->pending_event != EVDEV_NONE &&
270 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
271 evdev_flush_pending_event(device, time);
273 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
275 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
277 case ABS_MT_POSITION_X:
278 device->mt.slots[device->mt.slot].x = e->value;
279 if (device->pending_event == EVDEV_NONE)
280 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
282 case ABS_MT_POSITION_Y:
283 device->mt.slots[device->mt.slot].y = e->value;
284 if (device->pending_event == EVDEV_NONE)
285 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
291 evdev_process_absolute_motion(struct evdev_device *device,
292 struct input_event *e)
296 device->abs.x = e->value;
297 if (device->pending_event == EVDEV_NONE)
298 device->pending_event = EVDEV_ABSOLUTE_MOTION;
301 device->abs.y = e->value;
302 if (device->pending_event == EVDEV_NONE)
303 device->pending_event = EVDEV_ABSOLUTE_MOTION;
309 evdev_process_relative(struct evdev_device *device,
310 struct input_event *e, uint32_t time)
312 struct libinput_device *base = &device->base;
316 if (device->pending_event != EVDEV_RELATIVE_MOTION)
317 evdev_flush_pending_event(device, time);
318 device->rel.dx += li_fixed_from_int(e->value);
319 device->pending_event = EVDEV_RELATIVE_MOTION;
322 if (device->pending_event != EVDEV_RELATIVE_MOTION)
323 evdev_flush_pending_event(device, time);
324 device->rel.dy += li_fixed_from_int(e->value);
325 device->pending_event = EVDEV_RELATIVE_MOTION;
328 evdev_flush_pending_event(device, time);
332 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
333 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
336 evdev_flush_pending_event(device, time);
345 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
346 e->value * DEFAULT_AXIS_STEP_DISTANCE);
356 evdev_process_absolute(struct evdev_device *device,
357 struct input_event *e,
361 evdev_process_touch(device, e, time);
363 evdev_process_absolute_motion(device, e);
368 evdev_need_touch_frame(struct evdev_device *device)
370 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
373 switch (device->pending_event) {
375 case EVDEV_RELATIVE_MOTION:
377 case EVDEV_ABSOLUTE_MT_DOWN:
378 case EVDEV_ABSOLUTE_MT_MOTION:
379 case EVDEV_ABSOLUTE_MT_UP:
380 case EVDEV_ABSOLUTE_TOUCH_DOWN:
381 case EVDEV_ABSOLUTE_TOUCH_UP:
382 case EVDEV_ABSOLUTE_MOTION:
390 fallback_process(struct evdev_dispatch *dispatch,
391 struct evdev_device *device,
392 struct input_event *event,
397 switch (event->type) {
399 evdev_process_relative(device, event, time);
402 evdev_process_absolute(device, event, time);
405 evdev_process_key(device, event, time);
408 need_frame = evdev_need_touch_frame(device);
409 evdev_flush_pending_event(device, time);
411 touch_notify_frame(&device->base, time);
417 fallback_destroy(struct evdev_dispatch *dispatch)
422 struct evdev_dispatch_interface fallback_interface = {
427 static struct evdev_dispatch *
428 fallback_dispatch_create(void)
430 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
431 if (dispatch == NULL)
434 dispatch->interface = &fallback_interface;
440 evdev_process_events(struct evdev_device *device,
441 struct input_event *ev, int count)
443 struct evdev_dispatch *dispatch = device->dispatch;
444 struct input_event *e, *end;
449 for (e = ev; e < end; e++) {
450 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
452 dispatch->interface->process(dispatch, device, e, time);
457 evdev_device_dispatch(void *data)
459 struct evdev_device *device = data;
460 struct libinput *libinput = device->base.seat->libinput;
462 struct input_event ev[32];
465 /* If the compositor is repainting, this function is called only once
466 * per frame and we have to process all the events available on the
467 * fd, otherwise there will be input lag. */
470 len = mtdev_get(device->mtdev, fd, ev,
472 sizeof (struct input_event);
474 len = read(fd, &ev, sizeof ev);
476 if (len < 0 || len % sizeof ev[0] != 0) {
477 if (len < 0 && errno != EAGAIN && errno != EINTR) {
478 libinput_remove_source(libinput,
480 device->source = NULL;
486 evdev_process_events(device, ev, len / sizeof ev[0]);
492 evdev_configure_device(struct evdev_device *device)
494 struct input_absinfo absinfo;
495 unsigned long ev_bits[NBITS(EV_MAX)];
496 unsigned long abs_bits[NBITS(ABS_MAX)];
497 unsigned long rel_bits[NBITS(REL_MAX)];
498 unsigned long key_bits[NBITS(KEY_MAX)];
499 int has_abs, has_rel, has_mt;
500 int has_button, has_keyboard, has_touch;
510 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
511 if (TEST_BIT(ev_bits, EV_ABS)) {
512 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
515 if (TEST_BIT(abs_bits, ABS_X)) {
516 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
517 device->abs.min_x = absinfo.minimum;
518 device->abs.max_x = absinfo.maximum;
521 if (TEST_BIT(abs_bits, ABS_Y)) {
522 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
523 device->abs.min_y = absinfo.minimum;
524 device->abs.max_y = absinfo.maximum;
527 /* We only handle the slotted Protocol B in weston.
528 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
529 require mtdev for conversion. */
530 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
531 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
532 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
534 device->abs.min_x = absinfo.minimum;
535 device->abs.max_x = absinfo.maximum;
536 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
538 device->abs.min_y = absinfo.minimum;
539 device->abs.max_y = absinfo.maximum;
544 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
545 device->mtdev = mtdev_new_open(device->fd);
548 device->mt.slot = device->mtdev->caps.slot.value;
550 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
552 device->mt.slot = absinfo.value;
556 if (TEST_BIT(ev_bits, EV_REL)) {
557 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
559 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
562 if (TEST_BIT(ev_bits, EV_KEY)) {
563 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
565 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
566 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
567 (has_abs || has_mt)) {
568 device->dispatch = evdev_touchpad_create(device);
570 for (i = KEY_ESC; i < KEY_MAX; i++) {
571 if (i >= BTN_MISC && i < KEY_OK)
573 if (TEST_BIT(key_bits, i)) {
578 if (TEST_BIT(key_bits, BTN_TOUCH))
580 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
581 if (TEST_BIT(key_bits, i)) {
587 if (TEST_BIT(ev_bits, EV_LED))
590 if ((has_abs || has_rel) && has_button)
591 device->seat_caps |= EVDEV_DEVICE_POINTER;
593 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
594 if (has_touch && !has_button)
595 device->seat_caps |= EVDEV_DEVICE_TOUCH;
600 struct evdev_device *
601 evdev_device_create(struct libinput_seat *seat,
605 struct libinput *libinput = seat->libinput;
606 struct evdev_device *device;
607 char devname[256] = "unknown";
610 /* Use non-blocking mode so that we can loop on read on
611 * evdev_device_data() until all events on the fd are
612 * read. mtdev_get() also expects this. */
613 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
615 log_info("opening input device '%s' failed (%s).\n",
616 devnode, strerror(-fd));
620 device = zalloc(sizeof *device);
624 libinput_device_init(&device->base, seat);
626 device->seat_caps = 0;
628 device->mtdev = NULL;
629 device->devnode = strdup(devnode);
630 device->sysname = strdup(sysname);
631 device->mt.slot = -1;
634 device->dispatch = NULL;
636 device->pending_event = EVDEV_NONE;
638 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
639 devname[sizeof(devname) - 1] = '\0';
640 device->devname = strdup(devname);
642 libinput_seat_ref(seat);
644 if (evdev_configure_device(device) == -1)
647 if (device->seat_caps == 0) {
651 /* If the dispatch was not set up use the fallback. */
652 if (device->dispatch == NULL)
653 device->dispatch = fallback_dispatch_create();
654 if (device->dispatch == NULL)
658 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
662 list_insert(seat->devices_list.prev, &device->base.link);
663 notify_added_device(&device->base);
669 close_restricted(libinput, fd);
670 evdev_device_destroy(device);
672 if (device->seat_caps == 0)
673 return EVDEV_UNHANDLED_DEVICE;
679 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
681 memset(keys, 0, size);
682 return ioctl(device->fd, EVIOCGKEY(size), keys);
686 evdev_device_get_output(struct evdev_device *device)
688 return device->output_name;
692 evdev_device_get_sysname(struct evdev_device *device)
694 return device->sysname;
698 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
700 device->abs.apply_calibration = 1;
701 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
705 evdev_device_has_capability(struct evdev_device *device,
706 enum libinput_device_capability capability)
708 switch (capability) {
709 case LIBINPUT_DEVICE_CAP_POINTER:
710 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
711 case LIBINPUT_DEVICE_CAP_KEYBOARD:
712 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
713 case LIBINPUT_DEVICE_CAP_TOUCH:
714 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
721 evdev_device_remove(struct evdev_device *device)
724 libinput_remove_source(device->base.seat->libinput,
728 mtdev_close_delete(device->mtdev);
729 close_restricted(device->base.seat->libinput, device->fd);
730 list_remove(&device->base.link);
732 notify_removed_device(&device->base);
733 libinput_device_unref(&device->base);
737 evdev_device_destroy(struct evdev_device *device)
739 struct evdev_dispatch *dispatch;
741 dispatch = device->dispatch;
743 dispatch->interface->destroy(dispatch);
745 libinput_seat_unref(device->base.seat);
747 free(device->devname);
748 free(device->devnode);
749 free(device->sysname);