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_flush_pending_event(struct evdev_device *device, uint32_t time)
94 struct libinput_device *base = &device->base;
96 slot = device->mt.slot;
98 switch (device->pending_event) {
101 case EVDEV_RELATIVE_MOTION:
102 pointer_notify_motion(base,
109 case EVDEV_ABSOLUTE_MT_DOWN:
110 touch_notify_touch(base,
113 li_fixed_from_int(device->mt.slots[slot].x),
114 li_fixed_from_int(device->mt.slots[slot].y),
115 LIBINPUT_TOUCH_TYPE_DOWN);
117 case EVDEV_ABSOLUTE_MT_MOTION:
118 touch_notify_touch(base,
121 li_fixed_from_int(device->mt.slots[slot].x),
122 li_fixed_from_int(device->mt.slots[slot].y),
123 LIBINPUT_TOUCH_TYPE_MOTION);
125 case EVDEV_ABSOLUTE_MT_UP:
126 touch_notify_touch(base,
130 LIBINPUT_TOUCH_TYPE_UP);
132 case EVDEV_ABSOLUTE_TOUCH_DOWN:
133 transform_absolute(device, &cx, &cy);
134 touch_notify_touch(base,
137 li_fixed_from_int(cx),
138 li_fixed_from_int(cy),
139 LIBINPUT_TOUCH_TYPE_DOWN);
141 case EVDEV_ABSOLUTE_MOTION:
142 transform_absolute(device, &cx, &cy);
143 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
144 touch_notify_touch(base,
147 li_fixed_from_int(cx),
148 li_fixed_from_int(cy),
149 LIBINPUT_TOUCH_TYPE_DOWN);
150 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
151 pointer_notify_motion_absolute(base,
153 li_fixed_from_int(cx),
154 li_fixed_from_int(cy));
157 case EVDEV_ABSOLUTE_TOUCH_UP:
158 touch_notify_touch(base,
160 0, 0, 0, LIBINPUT_TOUCH_TYPE_UP);
164 assert(0 && "Unknown pending event type");
167 device->pending_event = EVDEV_NONE;
171 evdev_process_touch_button(struct evdev_device *device, int time, int value)
173 if (device->pending_event != EVDEV_NONE &&
174 device->pending_event != EVDEV_ABSOLUTE_MOTION)
175 evdev_flush_pending_event(device, time);
177 device->pending_event = (value ?
178 EVDEV_ABSOLUTE_TOUCH_DOWN :
179 EVDEV_ABSOLUTE_TOUCH_UP);
183 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
185 /* ignore kernel key repeat */
189 if (e->code == BTN_TOUCH) {
191 evdev_process_touch_button(device, time, e->value);
195 evdev_flush_pending_event(device, time);
206 pointer_notify_button(
210 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
211 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
219 e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
220 LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
226 evdev_process_touch(struct evdev_device *device,
227 struct input_event *e,
230 struct libinput *libinput = device->base.seat->libinput;
234 libinput->interface->get_current_screen_dimensions(
238 libinput->user_data);
242 evdev_flush_pending_event(device, time);
243 device->mt.slot = e->value;
245 case ABS_MT_TRACKING_ID:
246 if (device->pending_event != EVDEV_NONE &&
247 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
248 evdev_flush_pending_event(device, time);
250 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
252 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
254 case ABS_MT_POSITION_X:
255 device->mt.slots[device->mt.slot].x =
256 (e->value - device->abs.min_x) * screen_width /
257 (device->abs.max_x - device->abs.min_x);
258 if (device->pending_event == EVDEV_NONE)
259 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
261 case ABS_MT_POSITION_Y:
262 device->mt.slots[device->mt.slot].y =
263 (e->value - device->abs.min_y) * screen_height /
264 (device->abs.max_y - device->abs.min_y);
265 if (device->pending_event == EVDEV_NONE)
266 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
272 evdev_process_absolute_motion(struct evdev_device *device,
273 struct input_event *e)
275 struct libinput *libinput = device->base.seat->libinput;
279 libinput->interface->get_current_screen_dimensions(
283 libinput->user_data);
288 (e->value - device->abs.min_x) * screen_width /
289 (device->abs.max_x - device->abs.min_x);
290 if (device->pending_event == EVDEV_NONE)
291 device->pending_event = EVDEV_ABSOLUTE_MOTION;
295 (e->value - device->abs.min_y) * screen_height /
296 (device->abs.max_y - device->abs.min_y);
297 if (device->pending_event == EVDEV_NONE)
298 device->pending_event = EVDEV_ABSOLUTE_MOTION;
304 evdev_process_relative(struct evdev_device *device,
305 struct input_event *e, uint32_t time)
307 struct libinput_device *base = &device->base;
311 if (device->pending_event != EVDEV_RELATIVE_MOTION)
312 evdev_flush_pending_event(device, time);
313 device->rel.dx += li_fixed_from_int(e->value);
314 device->pending_event = EVDEV_RELATIVE_MOTION;
317 if (device->pending_event != EVDEV_RELATIVE_MOTION)
318 evdev_flush_pending_event(device, time);
319 device->rel.dy += li_fixed_from_int(e->value);
320 device->pending_event = EVDEV_RELATIVE_MOTION;
323 evdev_flush_pending_event(device, time);
332 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
333 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
340 evdev_flush_pending_event(device, time);
349 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
350 e->value * DEFAULT_AXIS_STEP_DISTANCE);
360 evdev_process_absolute(struct evdev_device *device,
361 struct input_event *e,
365 evdev_process_touch(device, e, time);
367 evdev_process_absolute_motion(device, e);
372 fallback_process(struct evdev_dispatch *dispatch,
373 struct evdev_device *device,
374 struct input_event *event,
377 switch (event->type) {
379 evdev_process_relative(device, event, time);
382 evdev_process_absolute(device, event, time);
385 evdev_process_key(device, event, time);
388 evdev_flush_pending_event(device, time);
394 fallback_destroy(struct evdev_dispatch *dispatch)
399 struct evdev_dispatch_interface fallback_interface = {
404 static struct evdev_dispatch *
405 fallback_dispatch_create(void)
407 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
408 if (dispatch == NULL)
411 dispatch->interface = &fallback_interface;
417 evdev_process_events(struct evdev_device *device,
418 struct input_event *ev, int count)
420 struct evdev_dispatch *dispatch = device->dispatch;
421 struct input_event *e, *end;
426 for (e = ev; e < end; e++) {
427 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
429 dispatch->interface->process(dispatch, device, e, time);
434 evdev_device_dispatch(void *data)
436 struct evdev_device *device = data;
437 struct libinput *libinput = device->base.seat->libinput;
439 struct input_event ev[32];
442 /* If the compositor is repainting, this function is called only once
443 * per frame and we have to process all the events available on the
444 * fd, otherwise there will be input lag. */
447 len = mtdev_get(device->mtdev, fd, ev,
449 sizeof (struct input_event);
451 len = read(fd, &ev, sizeof ev);
453 if (len < 0 || len % sizeof ev[0] != 0) {
454 if (len < 0 && errno != EAGAIN && errno != EINTR) {
455 libinput_remove_source(libinput,
457 device->source = NULL;
463 evdev_process_events(device, ev, len / sizeof ev[0]);
469 evdev_configure_device(struct evdev_device *device)
471 struct input_absinfo absinfo;
472 unsigned long ev_bits[NBITS(EV_MAX)];
473 unsigned long abs_bits[NBITS(ABS_MAX)];
474 unsigned long rel_bits[NBITS(REL_MAX)];
475 unsigned long key_bits[NBITS(KEY_MAX)];
476 int has_abs, has_rel, has_mt;
477 int has_button, has_keyboard, has_touch;
487 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
488 if (TEST_BIT(ev_bits, EV_ABS)) {
489 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
492 if (TEST_BIT(abs_bits, ABS_X)) {
493 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
494 device->abs.min_x = absinfo.minimum;
495 device->abs.max_x = absinfo.maximum;
498 if (TEST_BIT(abs_bits, ABS_Y)) {
499 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
500 device->abs.min_y = absinfo.minimum;
501 device->abs.max_y = absinfo.maximum;
504 /* We only handle the slotted Protocol B in weston.
505 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
506 require mtdev for conversion. */
507 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
508 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
509 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
511 device->abs.min_x = absinfo.minimum;
512 device->abs.max_x = absinfo.maximum;
513 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
515 device->abs.min_y = absinfo.minimum;
516 device->abs.max_y = absinfo.maximum;
521 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
522 device->mtdev = mtdev_new_open(device->fd);
525 device->mt.slot = device->mtdev->caps.slot.value;
527 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
529 device->mt.slot = absinfo.value;
533 if (TEST_BIT(ev_bits, EV_REL)) {
534 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
536 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
539 if (TEST_BIT(ev_bits, EV_KEY)) {
540 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
542 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
543 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
544 (has_abs || has_mt)) {
545 device->dispatch = evdev_touchpad_create(device);
547 for (i = KEY_ESC; i < KEY_MAX; i++) {
548 if (i >= BTN_MISC && i < KEY_OK)
550 if (TEST_BIT(key_bits, i)) {
555 if (TEST_BIT(key_bits, BTN_TOUCH))
557 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
558 if (TEST_BIT(key_bits, i)) {
564 if (TEST_BIT(ev_bits, EV_LED))
567 if ((has_abs || has_rel) && has_button)
568 device->seat_caps |= EVDEV_DEVICE_POINTER;
570 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
572 device->seat_caps |= EVDEV_DEVICE_TOUCH;
577 struct evdev_device *
578 evdev_device_create(struct libinput_seat *seat,
583 struct libinput *libinput = seat->libinput;
584 struct evdev_device *device;
585 char devname[256] = "unknown";
587 device = zalloc(sizeof *device);
591 libinput_device_init(&device->base, seat);
593 device->seat_caps = 0;
595 device->mtdev = NULL;
596 device->devnode = strdup(devnode);
597 device->sysname = strdup(sysname);
598 device->mt.slot = -1;
601 device->dispatch = NULL;
603 device->pending_event = EVDEV_NONE;
605 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
606 devname[sizeof(devname) - 1] = '\0';
607 device->devname = strdup(devname);
609 if (evdev_configure_device(device) == -1)
612 if (device->seat_caps == 0) {
613 evdev_device_destroy(device);
617 /* If the dispatch was not set up use the fallback. */
618 if (device->dispatch == NULL)
619 device->dispatch = fallback_dispatch_create();
620 if (device->dispatch == NULL)
624 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
628 list_insert(seat->devices_list.prev, &device->base.link);
629 notify_added_device(&device->base);
634 evdev_device_destroy(device);
639 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
641 memset(keys, 0, size);
642 return ioctl(device->fd, EVIOCGKEY(size), keys);
646 evdev_device_get_output(struct evdev_device *device)
648 return device->output_name;
652 evdev_device_get_sysname(struct evdev_device *device)
654 return device->sysname;
658 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
660 device->abs.apply_calibration = 1;
661 memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
665 evdev_device_has_capability(struct evdev_device *device,
666 enum libinput_device_capability capability)
668 switch (capability) {
669 case LIBINPUT_DEVICE_CAP_POINTER:
670 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
671 case LIBINPUT_DEVICE_CAP_KEYBOARD:
672 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
673 case LIBINPUT_DEVICE_CAP_TOUCH:
674 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
681 evdev_device_remove(struct evdev_device *device)
684 libinput_remove_source(device->base.seat->libinput,
688 mtdev_close_delete(device->mtdev);
690 list_remove(&device->base.link);
692 notify_removed_device(&device->base);
693 libinput_device_unref(&device->base);
697 evdev_device_destroy(struct evdev_device *device)
699 struct evdev_dispatch *dispatch;
701 dispatch = device->dispatch;
703 dispatch->interface->destroy(dispatch);
705 free(device->devname);
706 free(device->devnode);
707 free(device->sysname);