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.
30 #include "linux/input.h"
33 #include <mtdev-plumbing.h>
41 #include "libinput-private.h"
43 #define DEFAULT_AXIS_STEP_DISTANCE 10
44 #define DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT 200
49 EVDEV_KEY_TYPE_BUTTON,
53 hw_set_key_down(struct evdev_device *device, int code, int pressed)
55 long_set_bit_state(device->hw_key_mask, code, pressed);
59 hw_is_key_down(struct evdev_device *device, int code)
61 return long_bit_is_set(device->hw_key_mask, code);
65 get_key_down_count(struct evdev_device *device, int code)
67 return device->key_count[code];
71 update_key_down_count(struct evdev_device *device, int code, int pressed)
74 assert(code >= 0 && code < KEY_CNT);
77 key_count = ++device->key_count[code];
79 assert(device->key_count[code] > 0);
80 key_count = --device->key_count[code];
84 log_bug_libinput(device->base.seat->libinput,
85 "Key count for %s reached abnormal values\n",
86 libevdev_event_code_get_name(EV_KEY, code));
93 evdev_keyboard_notify_key(struct evdev_device *device,
96 enum libinput_key_state state)
100 down_count = update_key_down_count(device, key, state);
102 if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
103 (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
104 keyboard_notify_key(&device->base, time, key, state);
108 evdev_pointer_notify_button(struct evdev_device *device,
111 enum libinput_button_state state)
115 down_count = update_key_down_count(device, button, state);
117 if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
118 (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0))
119 pointer_notify_button(&device->base, time, button, state);
123 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
125 static const struct {
126 enum libinput_led weston;
129 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
130 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
131 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
133 struct input_event ev[ARRAY_LENGTH(map) + 1];
136 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
139 memset(ev, 0, sizeof(ev));
140 for (i = 0; i < ARRAY_LENGTH(map); i++) {
142 ev[i].code = map[i].evdev;
143 ev[i].value = !!(leds & map[i].weston);
146 ev[i].code = SYN_REPORT;
148 i = write(device->fd, ev, sizeof ev);
149 (void)i; /* no, we really don't care about the return value */
153 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
155 if (!device->abs.apply_calibration)
158 matrix_mult_vec(&device->abs.calibration, x, y);
162 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
164 return (val - absinfo->minimum) * to_range /
165 (absinfo->maximum - absinfo->minimum + 1);
169 evdev_device_transform_x(struct evdev_device *device,
173 return scale_axis(device->abs.absinfo_x, x, width);
177 evdev_device_transform_y(struct evdev_device *device,
181 return scale_axis(device->abs.absinfo_y, y, height);
185 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
187 struct libinput *libinput = device->base.seat->libinput;
188 struct motion_params motion;
193 struct libinput_device *base = &device->base;
194 struct libinput_seat *seat = base->seat;
196 slot = device->mt.slot;
198 switch (device->pending_event) {
201 case EVDEV_RELATIVE_MOTION:
202 motion.dx = device->rel.dx;
203 motion.dy = device->rel.dy;
207 /* Use unaccelerated deltas for pointing stick scroll */
208 if (device->scroll.has_middle_button_scroll &&
209 hw_is_key_down(device, BTN_MIDDLE)) {
210 if (device->scroll.middle_button_scroll_active)
211 evdev_post_scroll(device, time,
212 motion.dx, motion.dy);
216 /* Apply pointer acceleration. */
217 filter_dispatch(device->pointer.filter, &motion, device, time);
219 if (motion.dx == 0.0 && motion.dy == 0.0)
222 pointer_notify_motion(base, time, motion.dx, motion.dy);
224 case EVDEV_ABSOLUTE_MT_DOWN:
225 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
228 if (device->mt.slots[slot].seat_slot != -1) {
229 log_bug_kernel(libinput,
230 "%s: Driver sent multiple touch down for the "
231 "same slot", device->devnode);
235 seat_slot = ffs(~seat->slot_map) - 1;
236 device->mt.slots[slot].seat_slot = seat_slot;
241 seat->slot_map |= 1 << seat_slot;
242 x = device->mt.slots[slot].x;
243 y = device->mt.slots[slot].y;
244 transform_absolute(device, &x, &y);
246 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
248 case EVDEV_ABSOLUTE_MT_MOTION:
249 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
252 seat_slot = device->mt.slots[slot].seat_slot;
253 x = device->mt.slots[slot].x;
254 y = device->mt.slots[slot].y;
259 transform_absolute(device, &x, &y);
260 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
262 case EVDEV_ABSOLUTE_MT_UP:
263 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
266 seat_slot = device->mt.slots[slot].seat_slot;
267 device->mt.slots[slot].seat_slot = -1;
272 seat->slot_map &= ~(1 << seat_slot);
274 touch_notify_touch_up(base, time, slot, seat_slot);
276 case EVDEV_ABSOLUTE_TOUCH_DOWN:
277 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
280 if (device->abs.seat_slot != -1) {
281 log_bug_kernel(libinput,
282 "%s: Driver sent multiple touch down for the "
283 "same slot", device->devnode);
287 seat_slot = ffs(~seat->slot_map) - 1;
288 device->abs.seat_slot = seat_slot;
293 seat->slot_map |= 1 << seat_slot;
297 transform_absolute(device, &cx, &cy);
299 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
301 case EVDEV_ABSOLUTE_MOTION:
304 transform_absolute(device, &cx, &cy);
308 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
309 seat_slot = device->abs.seat_slot;
314 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
315 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
316 pointer_notify_motion_absolute(base, time, x, y);
319 case EVDEV_ABSOLUTE_TOUCH_UP:
320 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
323 seat_slot = device->abs.seat_slot;
324 device->abs.seat_slot = -1;
329 seat->slot_map &= ~(1 << seat_slot);
331 touch_notify_touch_up(base, time, -1, seat_slot);
334 assert(0 && "Unknown pending event type");
338 device->pending_event = EVDEV_NONE;
341 static enum evdev_key_type
342 get_key_type(uint16_t code)
344 if (code == BTN_TOUCH)
345 return EVDEV_KEY_TYPE_NONE;
347 if (code >= KEY_ESC && code <= KEY_MICMUTE)
348 return EVDEV_KEY_TYPE_KEY;
349 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
350 return EVDEV_KEY_TYPE_BUTTON;
351 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
352 return EVDEV_KEY_TYPE_KEY;
353 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
354 return EVDEV_KEY_TYPE_BUTTON;
355 return EVDEV_KEY_TYPE_NONE;
359 evdev_middle_button_scroll_timeout(uint64_t time, void *data)
361 struct evdev_device *device = data;
363 device->scroll.middle_button_scroll_active = true;
367 evdev_middle_button_scroll_button(struct evdev_device *device,
368 uint64_t time, int is_press)
371 libinput_timer_set(&device->scroll.timer,
372 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
374 libinput_timer_cancel(&device->scroll.timer);
375 if (device->scroll.middle_button_scroll_active) {
376 evdev_stop_scroll(device, time);
377 device->scroll.middle_button_scroll_active = false;
379 /* If the button is released quickly enough emit the
380 * button press/release events. */
381 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
382 LIBINPUT_BUTTON_STATE_PRESSED);
383 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
384 LIBINPUT_BUTTON_STATE_RELEASED);
390 evdev_process_touch_button(struct evdev_device *device,
391 uint64_t time, int value)
393 if (device->pending_event != EVDEV_NONE &&
394 device->pending_event != EVDEV_ABSOLUTE_MOTION)
395 evdev_flush_pending_event(device, time);
397 device->pending_event = (value ?
398 EVDEV_ABSOLUTE_TOUCH_DOWN :
399 EVDEV_ABSOLUTE_TOUCH_UP);
403 evdev_process_key(struct evdev_device *device,
404 struct input_event *e, uint64_t time)
406 enum evdev_key_type type;
408 /* ignore kernel key repeat */
412 if (e->code == BTN_TOUCH) {
414 evdev_process_touch_button(device, time, e->value);
418 evdev_flush_pending_event(device, time);
420 type = get_key_type(e->code);
422 /* Ignore key release events from the kernel for keys that libinput
423 * never got a pressed event for. */
426 case EVDEV_KEY_TYPE_NONE:
428 case EVDEV_KEY_TYPE_KEY:
429 case EVDEV_KEY_TYPE_BUTTON:
430 if (!hw_is_key_down(device, e->code))
435 hw_set_key_down(device, e->code, e->value);
438 case EVDEV_KEY_TYPE_NONE:
440 case EVDEV_KEY_TYPE_KEY:
441 evdev_keyboard_notify_key(
445 e->value ? LIBINPUT_KEY_STATE_PRESSED :
446 LIBINPUT_KEY_STATE_RELEASED);
448 case EVDEV_KEY_TYPE_BUTTON:
449 if (device->scroll.has_middle_button_scroll &&
450 e->code == BTN_MIDDLE) {
451 evdev_middle_button_scroll_button(device, time,
455 evdev_pointer_notify_button(
459 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
460 LIBINPUT_BUTTON_STATE_RELEASED);
466 evdev_process_touch(struct evdev_device *device,
467 struct input_event *e,
472 evdev_flush_pending_event(device, time);
473 device->mt.slot = e->value;
475 case ABS_MT_TRACKING_ID:
476 if (device->pending_event != EVDEV_NONE &&
477 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
478 evdev_flush_pending_event(device, time);
480 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
482 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
484 case ABS_MT_POSITION_X:
485 device->mt.slots[device->mt.slot].x = e->value;
486 if (device->pending_event == EVDEV_NONE)
487 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
489 case ABS_MT_POSITION_Y:
490 device->mt.slots[device->mt.slot].y = e->value;
491 if (device->pending_event == EVDEV_NONE)
492 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
498 evdev_process_absolute_motion(struct evdev_device *device,
499 struct input_event *e)
503 device->abs.x = e->value;
504 if (device->pending_event == EVDEV_NONE)
505 device->pending_event = EVDEV_ABSOLUTE_MOTION;
508 device->abs.y = e->value;
509 if (device->pending_event == EVDEV_NONE)
510 device->pending_event = EVDEV_ABSOLUTE_MOTION;
516 evdev_process_relative(struct evdev_device *device,
517 struct input_event *e, uint64_t time)
519 struct libinput_device *base = &device->base;
523 if (device->pending_event != EVDEV_RELATIVE_MOTION)
524 evdev_flush_pending_event(device, time);
525 device->rel.dx += e->value;
526 device->pending_event = EVDEV_RELATIVE_MOTION;
529 if (device->pending_event != EVDEV_RELATIVE_MOTION)
530 evdev_flush_pending_event(device, time);
531 device->rel.dy += e->value;
532 device->pending_event = EVDEV_RELATIVE_MOTION;
535 evdev_flush_pending_event(device, time);
539 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
540 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
543 evdev_flush_pending_event(device, time);
552 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
553 e->value * DEFAULT_AXIS_STEP_DISTANCE);
563 evdev_process_absolute(struct evdev_device *device,
564 struct input_event *e,
568 evdev_process_touch(device, e, time);
570 evdev_process_absolute_motion(device, e);
575 evdev_need_touch_frame(struct evdev_device *device)
577 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
580 switch (device->pending_event) {
582 case EVDEV_RELATIVE_MOTION:
584 case EVDEV_ABSOLUTE_MT_DOWN:
585 case EVDEV_ABSOLUTE_MT_MOTION:
586 case EVDEV_ABSOLUTE_MT_UP:
587 case EVDEV_ABSOLUTE_TOUCH_DOWN:
588 case EVDEV_ABSOLUTE_TOUCH_UP:
589 case EVDEV_ABSOLUTE_MOTION:
597 evdev_tag_external_mouse(struct evdev_device *device,
598 struct udev_device *udev_device)
602 bustype = libevdev_get_id_bustype(device->evdev);
603 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
604 if (device->seat_caps & EVDEV_DEVICE_POINTER)
605 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
610 evdev_tag_trackpoint(struct evdev_device *device,
611 struct udev_device *udev_device)
613 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
614 device->tags |= EVDEV_TAG_TRACKPOINT;
618 fallback_process(struct evdev_dispatch *dispatch,
619 struct evdev_device *device,
620 struct input_event *event,
625 switch (event->type) {
627 evdev_process_relative(device, event, time);
630 evdev_process_absolute(device, event, time);
633 evdev_process_key(device, event, time);
636 need_frame = evdev_need_touch_frame(device);
637 evdev_flush_pending_event(device, time);
639 touch_notify_frame(&device->base, time);
645 fallback_destroy(struct evdev_dispatch *dispatch)
651 fallback_tag_device(struct evdev_device *device,
652 struct udev_device *udev_device)
654 evdev_tag_external_mouse(device, udev_device);
655 evdev_tag_trackpoint(device, udev_device);
659 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
661 struct evdev_device *device = (struct evdev_device*)libinput_device;
663 return device->abs.absinfo_x && device->abs.absinfo_y;
666 static enum libinput_config_status
667 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
668 const float matrix[6])
670 struct evdev_device *device = (struct evdev_device*)libinput_device;
672 evdev_device_calibrate(device, matrix);
674 return LIBINPUT_CONFIG_STATUS_SUCCESS;
678 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
681 struct evdev_device *device = (struct evdev_device*)libinput_device;
683 matrix_to_farray6(&device->abs.usermatrix, matrix);
685 return !matrix_is_identity(&device->abs.usermatrix);
689 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
692 struct evdev_device *device = (struct evdev_device*)libinput_device;
694 matrix_to_farray6(&device->abs.default_calibration, matrix);
696 return !matrix_is_identity(&device->abs.default_calibration);
699 struct evdev_dispatch_interface fallback_interface = {
702 NULL, /* device_added */
703 NULL, /* device_removed */
704 NULL, /* device_suspended */
705 NULL, /* device_resumed */
710 evdev_sendevents_get_modes(struct libinput_device *device)
712 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED |
713 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
716 static enum libinput_config_status
717 evdev_sendevents_set_mode(struct libinput_device *device,
718 enum libinput_config_send_events_mode mode)
720 struct evdev_device *evdev = (struct evdev_device*)device;
721 struct evdev_dispatch *dispatch = evdev->dispatch;
723 if (mode == dispatch->sendevents.current_mode)
724 return LIBINPUT_CONFIG_STATUS_SUCCESS;
727 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
728 evdev_device_resume(evdev);
730 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
731 evdev_device_suspend(evdev);
734 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
737 dispatch->sendevents.current_mode = mode;
739 return LIBINPUT_CONFIG_STATUS_SUCCESS;
742 static enum libinput_config_send_events_mode
743 evdev_sendevents_get_mode(struct libinput_device *device)
745 struct evdev_device *evdev = (struct evdev_device*)device;
746 struct evdev_dispatch *dispatch = evdev->dispatch;
748 return dispatch->sendevents.current_mode;
751 static enum libinput_config_send_events_mode
752 evdev_sendevents_get_default_mode(struct libinput_device *device)
754 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
757 static struct evdev_dispatch *
758 fallback_dispatch_create(struct libinput_device *device)
760 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
761 if (dispatch == NULL)
764 dispatch->interface = &fallback_interface;
766 device->config.calibration = &dispatch->calibration;
768 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
769 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
770 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
771 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
773 device->config.sendevents = &dispatch->sendevents.config;
775 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
776 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
777 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
778 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
779 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
785 evdev_process_event(struct evdev_device *device, struct input_event *e)
787 struct evdev_dispatch *dispatch = device->dispatch;
788 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
790 dispatch->interface->process(dispatch, device, e, time);
794 evdev_device_dispatch_one(struct evdev_device *device,
795 struct input_event *ev)
797 if (!device->mtdev) {
798 evdev_process_event(device, ev);
800 mtdev_put_event(device->mtdev, ev);
801 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
802 while (!mtdev_empty(device->mtdev)) {
803 struct input_event e;
804 mtdev_get_event(device->mtdev, &e);
805 evdev_process_event(device, &e);
812 evdev_sync_device(struct evdev_device *device)
814 struct input_event ev;
818 rc = libevdev_next_event(device->evdev,
819 LIBEVDEV_READ_FLAG_SYNC, &ev);
822 evdev_device_dispatch_one(device, &ev);
823 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
825 return rc == -EAGAIN ? 0 : rc;
829 evdev_device_dispatch(void *data)
831 struct evdev_device *device = data;
832 struct libinput *libinput = device->base.seat->libinput;
833 struct input_event ev;
836 /* If the compositor is repainting, this function is called only once
837 * per frame and we have to process all the events available on the
838 * fd, otherwise there will be input lag. */
840 rc = libevdev_next_event(device->evdev,
841 LIBEVDEV_READ_FLAG_NORMAL, &ev);
842 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
843 /* send one more sync event so we handle all
844 currently pending events before we sync up
845 to the current state */
846 ev.code = SYN_REPORT;
847 evdev_device_dispatch_one(device, &ev);
849 rc = evdev_sync_device(device);
851 rc = LIBEVDEV_READ_STATUS_SUCCESS;
852 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
853 evdev_device_dispatch_one(device, &ev);
855 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
857 if (rc != -EAGAIN && rc != -EINTR) {
858 libinput_remove_source(libinput, device->source);
859 device->source = NULL;
864 configure_pointer_acceleration(struct evdev_device *device)
866 device->pointer.filter =
867 create_pointer_accelator_filter(
868 pointer_accel_profile_linear);
869 if (!device->pointer.filter)
877 evdev_need_mtdev(struct evdev_device *device)
879 struct libevdev *evdev = device->evdev;
881 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
882 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
883 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
887 evdev_tag_device(struct evdev_device *device)
890 struct udev_device *udev_device = NULL;
896 udev_device = udev_device_new_from_syspath(udev, device->syspath);
898 if (device->dispatch->interface->tag_device)
899 device->dispatch->interface->tag_device(device, udev_device);
900 udev_device_unref(udev_device);
906 evdev_configure_device(struct evdev_device *device)
908 struct libinput *libinput = device->base.seat->libinput;
909 struct libevdev *evdev = device->evdev;
910 const struct input_absinfo *absinfo;
911 struct input_absinfo fixed;
912 int has_abs, has_rel, has_mt;
913 int has_button, has_keyboard, has_touch;
914 struct mt_slot *slots;
927 if (libevdev_has_event_type(evdev, EV_ABS)) {
929 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
930 if (absinfo->resolution == 0) {
932 fixed.resolution = 1;
933 libevdev_set_abs_info(evdev, ABS_X, &fixed);
934 device->abs.fake_resolution = 1;
936 device->abs.absinfo_x = absinfo;
939 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
940 if (absinfo->resolution == 0) {
942 fixed.resolution = 1;
943 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
944 device->abs.fake_resolution = 1;
946 device->abs.absinfo_y = absinfo;
949 /* We only handle the slotted Protocol B in weston.
950 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
951 require mtdev for conversion. */
952 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
953 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
954 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
955 if (absinfo->resolution == 0) {
957 fixed.resolution = 1;
958 libevdev_set_abs_info(evdev,
961 device->abs.fake_resolution = 1;
963 device->abs.absinfo_x = absinfo;
964 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
965 if (absinfo->resolution == 0) {
967 fixed.resolution = 1;
968 libevdev_set_abs_info(evdev,
971 device->abs.fake_resolution = 1;
973 device->abs.absinfo_y = absinfo;
978 if (evdev_need_mtdev(device)) {
979 device->mtdev = mtdev_new_open(device->fd);
983 num_slots = device->mtdev->caps.slot.maximum;
984 if (device->mtdev->caps.slot.minimum < 0 ||
987 active_slot = device->mtdev->caps.slot.value;
989 num_slots = libevdev_get_num_slots(device->evdev);
990 active_slot = libevdev_get_current_slot(evdev);
993 slots = calloc(num_slots, sizeof(struct mt_slot));
997 for (slot = 0; slot < num_slots; ++slot) {
998 slots[slot].seat_slot = -1;
1002 device->mt.slots = slots;
1003 device->mt.slots_len = num_slots;
1004 device->mt.slot = active_slot;
1008 if (libevdev_has_property(evdev, INPUT_PROP_POINTING_STICK)) {
1009 libinput_timer_init(&device->scroll.timer,
1010 device->base.seat->libinput,
1011 evdev_middle_button_scroll_timeout,
1013 device->scroll.has_middle_button_scroll = true;
1016 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1017 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1020 if (libevdev_has_event_type(evdev, EV_KEY)) {
1021 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1022 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1023 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1024 (has_abs || has_mt)) {
1025 device->dispatch = evdev_mt_touchpad_create(device);
1027 "input device '%s', %s is a touchpad\n",
1028 device->devname, device->devnode);
1029 return device->dispatch == NULL ? -1 : 0;
1032 for (i = 0; i < KEY_MAX; i++) {
1033 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1034 switch (get_key_type(i)) {
1035 case EVDEV_KEY_TYPE_NONE:
1037 case EVDEV_KEY_TYPE_KEY:
1040 case EVDEV_KEY_TYPE_BUTTON:
1047 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1050 if (libevdev_has_event_type(evdev, EV_LED))
1053 if ((has_abs || has_rel) && has_button) {
1054 if (configure_pointer_acceleration(device) == -1)
1057 device->seat_caps |= EVDEV_DEVICE_POINTER;
1060 "input device '%s', %s is a pointer caps =%s%s%s\n",
1061 device->devname, device->devnode,
1062 has_abs ? " absolute-motion" : "",
1063 has_rel ? " relative-motion": "",
1064 has_button ? " button" : "");
1067 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1069 "input device '%s', %s is a keyboard\n",
1070 device->devname, device->devnode);
1072 if (has_touch && !has_button) {
1073 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1075 "input device '%s', %s is a touch device\n",
1076 device->devname, device->devnode);
1083 evdev_notify_added_device(struct evdev_device *device)
1085 struct libinput_device *dev;
1087 list_for_each(dev, &device->base.seat->devices_list, link) {
1088 struct evdev_device *d = (struct evdev_device*)dev;
1089 if (dev == &device->base)
1092 /* Notify existing device d about addition of device device */
1093 if (d->dispatch->interface->device_added)
1094 d->dispatch->interface->device_added(d, device);
1096 /* Notify new device device about existing device d */
1097 if (device->dispatch->interface->device_added)
1098 device->dispatch->interface->device_added(device, d);
1100 /* Notify new device device if existing device d is suspended */
1101 if (d->suspended && device->dispatch->interface->device_suspended)
1102 device->dispatch->interface->device_suspended(device, d);
1105 notify_added_device(&device->base);
1108 struct evdev_device *
1109 evdev_device_create(struct libinput_seat *seat,
1110 const char *devnode,
1111 const char *sysname,
1112 const char *syspath)
1114 struct libinput *libinput = seat->libinput;
1115 struct evdev_device *device;
1118 int unhandled_device = 0;
1120 /* Use non-blocking mode so that we can loop on read on
1121 * evdev_device_data() until all events on the fd are
1122 * read. mtdev_get() also expects this. */
1123 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1126 "opening input device '%s' failed (%s).\n",
1127 devnode, strerror(-fd));
1131 device = zalloc(sizeof *device);
1135 libinput_device_init(&device->base, seat);
1136 libinput_seat_ref(seat);
1138 rc = libevdev_new_from_fd(fd, &device->evdev);
1142 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1144 device->seat_caps = 0;
1146 device->mtdev = NULL;
1147 device->devnode = strdup(devnode);
1148 device->sysname = strdup(sysname);
1149 device->syspath = strdup(syspath);
1152 device->abs.seat_slot = -1;
1153 device->dispatch = NULL;
1155 device->pending_event = EVDEV_NONE;
1156 device->devname = libevdev_get_name(device->evdev);
1157 device->scroll.threshold = 5.0; /* Default may be overridden */
1158 device->scroll.direction = 0;
1160 matrix_init_identity(&device->abs.calibration);
1161 matrix_init_identity(&device->abs.usermatrix);
1162 matrix_init_identity(&device->abs.default_calibration);
1164 if (evdev_configure_device(device) == -1)
1167 if (device->seat_caps == 0) {
1168 unhandled_device = 1;
1172 /* If the dispatch was not set up use the fallback. */
1173 if (device->dispatch == NULL)
1174 device->dispatch = fallback_dispatch_create(&device->base);
1175 if (device->dispatch == NULL)
1179 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1180 if (!device->source)
1183 list_insert(seat->devices_list.prev, &device->base.link);
1185 evdev_tag_device(device);
1186 evdev_notify_added_device(device);
1192 close_restricted(libinput, fd);
1193 evdev_device_destroy(device);
1195 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1199 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1201 memset(keys, 0, size);
1206 evdev_device_get_output(struct evdev_device *device)
1208 return device->output_name;
1212 evdev_device_get_sysname(struct evdev_device *device)
1214 return device->sysname;
1218 evdev_device_get_name(struct evdev_device *device)
1220 return device->devname;
1224 evdev_device_get_id_product(struct evdev_device *device)
1226 return libevdev_get_id_product(device->evdev);
1230 evdev_device_get_id_vendor(struct evdev_device *device)
1232 return libevdev_get_id_vendor(device->evdev);
1236 evdev_device_set_default_calibration(struct evdev_device *device,
1237 const float calibration[6])
1239 matrix_from_farray6(&device->abs.default_calibration, calibration);
1240 evdev_device_calibrate(device, calibration);
1244 evdev_device_calibrate(struct evdev_device *device,
1245 const float calibration[6])
1247 struct matrix scale,
1252 matrix_from_farray6(&transform, calibration);
1253 device->abs.apply_calibration = !matrix_is_identity(&transform);
1255 if (!device->abs.apply_calibration) {
1256 matrix_init_identity(&device->abs.calibration);
1260 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1261 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1263 /* The transformation matrix is in the form:
1267 * Where a, e are the scale components, a, b, d, e are the rotation
1268 * component (combined with scale) and c and f are the translation
1269 * component. The translation component in the input matrix must be
1270 * normalized to multiples of the device width and height,
1271 * respectively. e.g. c == 1 shifts one device-width to the right.
1273 * We pre-calculate a single matrix to apply to event coordinates:
1274 * M = Un-Normalize * Calibration * Normalize
1276 * Normalize: scales the device coordinates to [0,1]
1277 * Calibration: user-supplied matrix
1278 * Un-Normalize: scales back up to device coordinates
1279 * Matrix maths requires the normalize/un-normalize in reverse
1283 /* back up the user matrix so we can return it on request */
1284 matrix_from_farray6(&device->abs.usermatrix, calibration);
1287 matrix_init_translate(&translate,
1288 device->abs.absinfo_x->minimum,
1289 device->abs.absinfo_y->minimum);
1290 matrix_init_scale(&scale, sx, sy);
1291 matrix_mult(&scale, &translate, &scale);
1294 matrix_mult(&transform, &scale, &transform);
1297 matrix_init_translate(&translate,
1298 -device->abs.absinfo_x->minimum/sx,
1299 -device->abs.absinfo_y->minimum/sy);
1300 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1301 matrix_mult(&scale, &translate, &scale);
1303 /* store final matrix in device */
1304 matrix_mult(&device->abs.calibration, &transform, &scale);
1308 evdev_device_has_capability(struct evdev_device *device,
1309 enum libinput_device_capability capability)
1311 switch (capability) {
1312 case LIBINPUT_DEVICE_CAP_POINTER:
1313 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1314 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1315 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1316 case LIBINPUT_DEVICE_CAP_TOUCH:
1317 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1324 evdev_device_get_size(struct evdev_device *device,
1328 const struct input_absinfo *x, *y;
1330 x = libevdev_get_abs_info(device->evdev, ABS_X);
1331 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1333 if (!x || !y || device->abs.fake_resolution ||
1334 !x->resolution || !y->resolution)
1337 *width = evdev_convert_to_mm(x, x->maximum);
1338 *height = evdev_convert_to_mm(y, y->maximum);
1344 evdev_post_scroll(struct evdev_device *device,
1349 if (dy <= -device->scroll.threshold || dy >= device->scroll.threshold)
1350 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1352 if (dx <= -device->scroll.threshold || dx >= device->scroll.threshold)
1353 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1356 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
1357 pointer_notify_axis(&device->base,
1359 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1364 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
1365 pointer_notify_axis(&device->base,
1367 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1373 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1375 /* terminate scrolling with a zero scroll event */
1376 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1377 pointer_notify_axis(&device->base,
1379 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1381 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1382 pointer_notify_axis(&device->base,
1384 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1387 device->scroll.direction = 0;
1391 release_pressed_keys(struct evdev_device *device)
1393 struct libinput *libinput = device->base.seat->libinput;
1397 if ((time = libinput_now(libinput)) == 0)
1400 for (code = 0; code < KEY_CNT; code++) {
1401 int count = get_key_down_count(device, code);
1404 log_bug_libinput(libinput,
1405 "Key %d is down %d times.\n",
1410 while (get_key_down_count(device, code) > 0) {
1411 switch (get_key_type(code)) {
1412 case EVDEV_KEY_TYPE_NONE:
1414 case EVDEV_KEY_TYPE_KEY:
1415 evdev_keyboard_notify_key(
1419 LIBINPUT_KEY_STATE_RELEASED);
1421 case EVDEV_KEY_TYPE_BUTTON:
1422 evdev_pointer_notify_button(
1426 LIBINPUT_BUTTON_STATE_RELEASED);
1434 evdev_notify_suspended_device(struct evdev_device *device)
1436 struct libinput_device *it;
1438 if (device->suspended)
1441 list_for_each(it, &device->base.seat->devices_list, link) {
1442 struct evdev_device *d = (struct evdev_device*)it;
1443 if (it == &device->base)
1446 if (d->dispatch->interface->device_suspended)
1447 d->dispatch->interface->device_suspended(d, device);
1450 device->suspended = 1;
1454 evdev_notify_resumed_device(struct evdev_device *device)
1456 struct libinput_device *it;
1458 if (!device->suspended)
1461 list_for_each(it, &device->base.seat->devices_list, link) {
1462 struct evdev_device *d = (struct evdev_device*)it;
1463 if (it == &device->base)
1466 if (d->dispatch->interface->device_resumed)
1467 d->dispatch->interface->device_resumed(d, device);
1470 device->suspended = 0;
1474 evdev_device_suspend(struct evdev_device *device)
1476 evdev_notify_suspended_device(device);
1478 if (device->source) {
1479 libinput_remove_source(device->base.seat->libinput,
1481 device->source = NULL;
1484 release_pressed_keys(device);
1486 if (device->mtdev) {
1487 mtdev_close_delete(device->mtdev);
1488 device->mtdev = NULL;
1491 if (device->fd != -1) {
1492 close_restricted(device->base.seat->libinput, device->fd);
1500 evdev_device_compare_syspath(struct evdev_device *device, int fd)
1502 struct udev *udev = NULL;
1503 struct udev_device *udev_device = NULL;
1504 const char *syspath;
1512 if (fstat(fd, &st) < 0)
1515 udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1519 syspath = udev_device_get_syspath(udev_device);
1520 rc = strcmp(syspath, device->syspath);
1523 udev_device_unref(udev_device);
1530 evdev_device_resume(struct evdev_device *device)
1532 struct libinput *libinput = device->base.seat->libinput;
1535 if (device->fd != -1)
1538 if (device->syspath == NULL)
1541 fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1546 if (evdev_device_compare_syspath(device, fd)) {
1547 close_restricted(libinput, fd);
1553 if (evdev_need_mtdev(device)) {
1554 device->mtdev = mtdev_new_open(device->fd);
1560 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1561 if (!device->source) {
1562 mtdev_close_delete(device->mtdev);
1566 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1568 evdev_notify_resumed_device(device);
1574 evdev_device_remove(struct evdev_device *device)
1576 struct libinput_device *dev;
1578 list_for_each(dev, &device->base.seat->devices_list, link) {
1579 struct evdev_device *d = (struct evdev_device*)dev;;
1580 if (dev == &device->base)
1583 if (d->dispatch->interface->device_removed)
1584 d->dispatch->interface->device_removed(d, device);
1587 evdev_device_suspend(device);
1589 /* A device may be removed while suspended. Free the syspath to
1590 * skip re-opening a different device with the same node */
1591 free(device->syspath);
1592 device->syspath = NULL;
1594 list_remove(&device->base.link);
1596 notify_removed_device(&device->base);
1597 libinput_device_unref(&device->base);
1601 evdev_device_destroy(struct evdev_device *device)
1603 struct evdev_dispatch *dispatch;
1605 dispatch = device->dispatch;
1607 dispatch->interface->destroy(dispatch);
1609 filter_destroy(device->pointer.filter);
1610 libinput_seat_unref(device->base.seat);
1611 libevdev_free(device->evdev);
1612 free(device->mt.slots);
1613 free(device->devnode);
1614 free(device->sysname);
1615 free(device->syspath);