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_WHEEL_CLICK_ANGLE 15
44 #define DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT 200
49 EVDEV_KEY_TYPE_BUTTON,
52 enum evdev_device_udev_tags {
53 EVDEV_UDEV_TAG_INPUT = (1 << 0),
54 EVDEV_UDEV_TAG_KEYBOARD = (1 << 1),
55 EVDEV_UDEV_TAG_MOUSE = (1 << 2),
56 EVDEV_UDEV_TAG_TOUCHPAD = (1 << 3),
57 EVDEV_UDEV_TAG_TOUCHSCREEN = (1 << 4),
58 EVDEV_UDEV_TAG_TABLET = (1 << 5),
59 EVDEV_UDEV_TAG_JOYSTICK = (1 << 6),
60 EVDEV_UDEV_TAG_ACCELEROMETER = (1 << 7),
61 EVDEV_UDEV_TAG_BUTTONSET = (1 << 8),
64 struct evdev_udev_tag_match {
66 enum evdev_device_udev_tags tag;
69 static const struct evdev_udev_tag_match evdev_udev_tag_matches[] = {
70 {"ID_INPUT", EVDEV_UDEV_TAG_INPUT},
71 {"ID_INPUT_KEYBOARD", EVDEV_UDEV_TAG_KEYBOARD},
72 {"ID_INPUT_KEY", EVDEV_UDEV_TAG_KEYBOARD},
73 {"ID_INPUT_MOUSE", EVDEV_UDEV_TAG_MOUSE},
74 {"ID_INPUT_TOUCHPAD", EVDEV_UDEV_TAG_TOUCHPAD},
75 {"ID_INPUT_TOUCHSCREEN", EVDEV_UDEV_TAG_TOUCHSCREEN},
76 {"ID_INPUT_TABLET", EVDEV_UDEV_TAG_TABLET},
77 {"ID_INPUT_TABLET_PAD", EVDEV_UDEV_TAG_BUTTONSET},
78 {"ID_INPUT_JOYSTICK", EVDEV_UDEV_TAG_JOYSTICK},
79 {"ID_INPUT_ACCELEROMETER", EVDEV_UDEV_TAG_ACCELEROMETER},
86 hw_set_key_down(struct evdev_device *device, int code, int pressed)
88 long_set_bit_state(device->hw_key_mask, code, pressed);
92 hw_is_key_down(struct evdev_device *device, int code)
94 return long_bit_is_set(device->hw_key_mask, code);
98 get_key_down_count(struct evdev_device *device, int code)
100 return device->key_count[code];
104 update_key_down_count(struct evdev_device *device, int code, int pressed)
107 assert(code >= 0 && code < KEY_CNT);
110 key_count = ++device->key_count[code];
112 assert(device->key_count[code] > 0);
113 key_count = --device->key_count[code];
116 if (key_count > 32) {
117 log_bug_libinput(device->base.seat->libinput,
118 "Key count for %s reached abnormal values\n",
119 libevdev_event_code_get_name(EV_KEY, code));
126 evdev_keyboard_notify_key(struct evdev_device *device,
129 enum libinput_key_state state)
133 down_count = update_key_down_count(device, key, state);
135 if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
136 (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
137 keyboard_notify_key(&device->base, time, key, state);
141 evdev_pointer_notify_physical_button(struct evdev_device *device,
144 enum libinput_button_state state)
146 if (evdev_middlebutton_filter_button(device,
152 evdev_pointer_notify_button(device, time, button, state);
156 evdev_pointer_notify_button(struct evdev_device *device,
159 enum libinput_button_state state)
163 down_count = update_key_down_count(device, button, state);
165 if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
166 (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
167 pointer_notify_button(&device->base, time, button, state);
169 if (state == LIBINPUT_BUTTON_STATE_RELEASED) {
170 if (device->left_handed.change_to_enabled)
171 device->left_handed.change_to_enabled(device);
173 if (device->scroll.change_scroll_method)
174 device->scroll.change_scroll_method(device);
181 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
183 static const struct {
184 enum libinput_led weston;
187 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
188 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
189 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
191 struct input_event ev[ARRAY_LENGTH(map) + 1];
194 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
197 memset(ev, 0, sizeof(ev));
198 for (i = 0; i < ARRAY_LENGTH(map); i++) {
200 ev[i].code = map[i].evdev;
201 ev[i].value = !!(leds & map[i].weston);
204 ev[i].code = SYN_REPORT;
206 i = write(device->fd, ev, sizeof ev);
207 (void)i; /* no, we really don't care about the return value */
211 transform_absolute(struct evdev_device *device,
212 struct device_coords *point)
214 if (!device->abs.apply_calibration)
217 matrix_mult_vec(&device->abs.calibration, &point->x, &point->y);
221 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
223 return (val - absinfo->minimum) * to_range /
224 (absinfo->maximum - absinfo->minimum + 1);
228 evdev_device_transform_x(struct evdev_device *device,
232 return scale_axis(device->abs.absinfo_x, x, width);
236 evdev_device_transform_y(struct evdev_device *device,
240 return scale_axis(device->abs.absinfo_y, y, height);
244 normalize_delta(struct evdev_device *device,
245 const struct device_coords *delta,
246 struct normalized_coords *normalized)
248 normalized->x = delta->x * DEFAULT_MOUSE_DPI / (double)device->dpi;
249 normalized->y = delta->y * DEFAULT_MOUSE_DPI / (double)device->dpi;
253 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
255 struct libinput *libinput = device->base.seat->libinput;
258 struct libinput_device *base = &device->base;
259 struct libinput_seat *seat = base->seat;
260 struct normalized_coords accel, unaccel;
261 struct device_coords point;
263 slot = device->mt.slot;
265 switch (device->pending_event) {
268 case EVDEV_RELATIVE_MOTION:
269 normalize_delta(device, &device->rel, &unaccel);
273 /* Use unaccelerated deltas for pointing stick scroll */
274 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
275 hw_is_key_down(device, device->scroll.button)) {
276 if (device->scroll.button_scroll_active)
277 evdev_post_scroll(device, time,
278 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
283 /* Apply pointer acceleration. */
284 accel = filter_dispatch(device->pointer.filter, &unaccel, device, time);
286 if (normalized_is_zero(accel) && normalized_is_zero(unaccel))
289 pointer_notify_motion(base, time, &accel, &unaccel);
291 case EVDEV_ABSOLUTE_MT_DOWN:
292 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
295 if (device->mt.slots[slot].seat_slot != -1) {
296 log_bug_kernel(libinput,
297 "%s: Driver sent multiple touch down for the "
299 udev_device_get_devnode(device->udev_device));
303 seat_slot = ffs(~seat->slot_map) - 1;
304 device->mt.slots[slot].seat_slot = seat_slot;
309 seat->slot_map |= 1 << seat_slot;
310 point = device->mt.slots[slot].point;
311 transform_absolute(device, &point);
313 touch_notify_touch_down(base, time, slot, seat_slot,
316 case EVDEV_ABSOLUTE_MT_MOTION:
317 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
320 seat_slot = device->mt.slots[slot].seat_slot;
321 point = device->mt.slots[slot].point;
326 transform_absolute(device, &point);
327 touch_notify_touch_motion(base, time, slot, seat_slot,
330 case EVDEV_ABSOLUTE_MT_UP:
331 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
334 seat_slot = device->mt.slots[slot].seat_slot;
335 device->mt.slots[slot].seat_slot = -1;
340 seat->slot_map &= ~(1 << seat_slot);
342 touch_notify_touch_up(base, time, slot, seat_slot);
344 case EVDEV_ABSOLUTE_TOUCH_DOWN:
345 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
348 if (device->abs.seat_slot != -1) {
349 log_bug_kernel(libinput,
350 "%s: Driver sent multiple touch down for the "
352 udev_device_get_devnode(device->udev_device));
356 seat_slot = ffs(~seat->slot_map) - 1;
357 device->abs.seat_slot = seat_slot;
362 seat->slot_map |= 1 << seat_slot;
364 point = device->abs.point;
365 transform_absolute(device, &point);
367 touch_notify_touch_down(base, time, -1, seat_slot, &point);
369 case EVDEV_ABSOLUTE_MOTION:
370 point = device->abs.point;
371 transform_absolute(device, &point);
373 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
374 seat_slot = device->abs.seat_slot;
379 touch_notify_touch_motion(base, time, -1, seat_slot,
381 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
382 pointer_notify_motion_absolute(base, time, &point);
385 case EVDEV_ABSOLUTE_TOUCH_UP:
386 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
389 seat_slot = device->abs.seat_slot;
390 device->abs.seat_slot = -1;
395 seat->slot_map &= ~(1 << seat_slot);
397 touch_notify_touch_up(base, time, -1, seat_slot);
400 assert(0 && "Unknown pending event type");
404 device->pending_event = EVDEV_NONE;
407 static enum evdev_key_type
408 get_key_type(uint16_t code)
410 if (code == BTN_TOUCH)
411 return EVDEV_KEY_TYPE_NONE;
413 if (code >= KEY_ESC && code <= KEY_MICMUTE)
414 return EVDEV_KEY_TYPE_KEY;
415 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
416 return EVDEV_KEY_TYPE_BUTTON;
417 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
418 return EVDEV_KEY_TYPE_KEY;
419 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
420 return EVDEV_KEY_TYPE_BUTTON;
421 return EVDEV_KEY_TYPE_NONE;
425 evdev_button_scroll_timeout(uint64_t time, void *data)
427 struct evdev_device *device = data;
429 device->scroll.button_scroll_active = true;
433 evdev_button_scroll_button(struct evdev_device *device,
434 uint64_t time, int is_press)
437 libinput_timer_set(&device->scroll.timer,
438 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
440 libinput_timer_cancel(&device->scroll.timer);
441 if (device->scroll.button_scroll_active) {
442 evdev_stop_scroll(device, time,
443 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS);
444 device->scroll.button_scroll_active = false;
446 /* If the button is released quickly enough emit the
447 * button press/release events. */
448 evdev_pointer_notify_physical_button(device, time,
449 device->scroll.button,
450 LIBINPUT_BUTTON_STATE_PRESSED);
451 evdev_pointer_notify_physical_button(device, time,
452 device->scroll.button,
453 LIBINPUT_BUTTON_STATE_RELEASED);
459 evdev_process_touch_button(struct evdev_device *device,
460 uint64_t time, int value)
462 if (device->pending_event != EVDEV_NONE &&
463 device->pending_event != EVDEV_ABSOLUTE_MOTION)
464 evdev_flush_pending_event(device, time);
466 device->pending_event = (value ?
467 EVDEV_ABSOLUTE_TOUCH_DOWN :
468 EVDEV_ABSOLUTE_TOUCH_UP);
472 evdev_process_key(struct evdev_device *device,
473 struct input_event *e, uint64_t time)
475 enum evdev_key_type type;
477 /* ignore kernel key repeat */
481 if (e->code == BTN_TOUCH) {
483 evdev_process_touch_button(device, time, e->value);
487 evdev_flush_pending_event(device, time);
489 type = get_key_type(e->code);
491 /* Ignore key release events from the kernel for keys that libinput
492 * never got a pressed event for. */
495 case EVDEV_KEY_TYPE_NONE:
497 case EVDEV_KEY_TYPE_KEY:
498 case EVDEV_KEY_TYPE_BUTTON:
499 if (!hw_is_key_down(device, e->code))
504 hw_set_key_down(device, e->code, e->value);
507 case EVDEV_KEY_TYPE_NONE:
509 case EVDEV_KEY_TYPE_KEY:
510 evdev_keyboard_notify_key(
514 e->value ? LIBINPUT_KEY_STATE_PRESSED :
515 LIBINPUT_KEY_STATE_RELEASED);
517 case EVDEV_KEY_TYPE_BUTTON:
518 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
519 e->code == device->scroll.button) {
520 evdev_button_scroll_button(device, time, e->value);
523 evdev_pointer_notify_physical_button(
526 evdev_to_left_handed(device, e->code),
527 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
528 LIBINPUT_BUTTON_STATE_RELEASED);
534 evdev_process_touch(struct evdev_device *device,
535 struct input_event *e,
540 if ((size_t)e->value >= device->mt.slots_len) {
541 log_bug_libinput(device->base.seat->libinput,
542 "%s exceeds slots (%d of %d)\n",
545 device->mt.slots_len);
546 e->value = device->mt.slots_len - 1;
548 evdev_flush_pending_event(device, time);
549 device->mt.slot = e->value;
551 case ABS_MT_TRACKING_ID:
552 if (device->pending_event != EVDEV_NONE &&
553 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
554 evdev_flush_pending_event(device, time);
556 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
558 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
560 case ABS_MT_POSITION_X:
561 device->mt.slots[device->mt.slot].point.x = e->value;
562 if (device->pending_event == EVDEV_NONE)
563 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
565 case ABS_MT_POSITION_Y:
566 device->mt.slots[device->mt.slot].point.y = e->value;
567 if (device->pending_event == EVDEV_NONE)
568 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
574 evdev_process_absolute_motion(struct evdev_device *device,
575 struct input_event *e)
579 device->abs.point.x = e->value;
580 if (device->pending_event == EVDEV_NONE)
581 device->pending_event = EVDEV_ABSOLUTE_MOTION;
584 device->abs.point.y = e->value;
585 if (device->pending_event == EVDEV_NONE)
586 device->pending_event = EVDEV_ABSOLUTE_MOTION;
592 evdev_notify_axis(struct evdev_device *device,
595 enum libinput_pointer_axis_source source,
596 const struct normalized_coords *delta_in,
597 const struct discrete_coords *discrete_in)
599 struct normalized_coords delta = *delta_in;
600 struct discrete_coords discrete = *discrete_in;
602 if (device->scroll.natural_scrolling_enabled) {
609 pointer_notify_axis(&device->base,
618 evdev_process_relative(struct evdev_device *device,
619 struct input_event *e, uint64_t time)
621 struct normalized_coords wheel_degrees = { 0.0, 0.0 };
622 struct discrete_coords discrete = { 0.0, 0.0 };
626 if (device->pending_event != EVDEV_RELATIVE_MOTION)
627 evdev_flush_pending_event(device, time);
628 device->rel.x += e->value;
629 device->pending_event = EVDEV_RELATIVE_MOTION;
632 if (device->pending_event != EVDEV_RELATIVE_MOTION)
633 evdev_flush_pending_event(device, time);
634 device->rel.y += e->value;
635 device->pending_event = EVDEV_RELATIVE_MOTION;
638 evdev_flush_pending_event(device, time);
639 wheel_degrees.y = -1 * e->value *
640 device->scroll.wheel_click_angle;
641 discrete.y = -1 * e->value;
645 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
646 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
651 evdev_flush_pending_event(device, time);
652 wheel_degrees.x = e->value * device->scroll.wheel_click_angle;
653 discrete.x = e->value;
657 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
658 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
666 evdev_process_absolute(struct evdev_device *device,
667 struct input_event *e,
671 evdev_process_touch(device, e, time);
673 evdev_process_absolute_motion(device, e);
678 evdev_any_button_down(struct evdev_device *device)
682 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
683 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
684 hw_is_key_down(device, button))
691 evdev_need_touch_frame(struct evdev_device *device)
693 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
696 switch (device->pending_event) {
698 case EVDEV_RELATIVE_MOTION:
700 case EVDEV_ABSOLUTE_MT_DOWN:
701 case EVDEV_ABSOLUTE_MT_MOTION:
702 case EVDEV_ABSOLUTE_MT_UP:
703 case EVDEV_ABSOLUTE_TOUCH_DOWN:
704 case EVDEV_ABSOLUTE_TOUCH_UP:
705 case EVDEV_ABSOLUTE_MOTION:
713 evdev_tag_external_mouse(struct evdev_device *device,
714 struct udev_device *udev_device)
718 bustype = libevdev_get_id_bustype(device->evdev);
719 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
720 if (device->seat_caps & EVDEV_DEVICE_POINTER)
721 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
726 evdev_tag_trackpoint(struct evdev_device *device,
727 struct udev_device *udev_device)
729 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
730 device->tags |= EVDEV_TAG_TRACKPOINT;
734 fallback_process(struct evdev_dispatch *dispatch,
735 struct evdev_device *device,
736 struct input_event *event,
739 bool need_frame = false;
741 switch (event->type) {
743 evdev_process_relative(device, event, time);
746 evdev_process_absolute(device, event, time);
749 evdev_process_key(device, event, time);
752 need_frame = evdev_need_touch_frame(device);
753 evdev_flush_pending_event(device, time);
755 touch_notify_frame(&device->base, time);
761 fallback_destroy(struct evdev_dispatch *dispatch)
767 fallback_tag_device(struct evdev_device *device,
768 struct udev_device *udev_device)
770 evdev_tag_external_mouse(device, udev_device);
771 evdev_tag_trackpoint(device, udev_device);
775 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
777 struct evdev_device *device = (struct evdev_device*)libinput_device;
779 return device->abs.absinfo_x && device->abs.absinfo_y;
782 static enum libinput_config_status
783 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
784 const float matrix[6])
786 struct evdev_device *device = (struct evdev_device*)libinput_device;
788 evdev_device_calibrate(device, matrix);
790 return LIBINPUT_CONFIG_STATUS_SUCCESS;
794 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
797 struct evdev_device *device = (struct evdev_device*)libinput_device;
799 matrix_to_farray6(&device->abs.usermatrix, matrix);
801 return !matrix_is_identity(&device->abs.usermatrix);
805 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
808 struct evdev_device *device = (struct evdev_device*)libinput_device;
810 matrix_to_farray6(&device->abs.default_calibration, matrix);
812 return !matrix_is_identity(&device->abs.default_calibration);
815 struct evdev_dispatch_interface fallback_interface = {
819 NULL, /* device_added */
820 NULL, /* device_removed */
821 NULL, /* device_suspended */
822 NULL, /* device_resumed */
827 evdev_sendevents_get_modes(struct libinput_device *device)
829 return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
832 static enum libinput_config_status
833 evdev_sendevents_set_mode(struct libinput_device *device,
834 enum libinput_config_send_events_mode mode)
836 struct evdev_device *evdev = (struct evdev_device*)device;
837 struct evdev_dispatch *dispatch = evdev->dispatch;
839 if (mode == dispatch->sendevents.current_mode)
840 return LIBINPUT_CONFIG_STATUS_SUCCESS;
843 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
844 evdev_device_resume(evdev);
846 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
847 evdev_device_suspend(evdev);
849 default: /* no support for combined modes yet */
850 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
853 dispatch->sendevents.current_mode = mode;
855 return LIBINPUT_CONFIG_STATUS_SUCCESS;
858 static enum libinput_config_send_events_mode
859 evdev_sendevents_get_mode(struct libinput_device *device)
861 struct evdev_device *evdev = (struct evdev_device*)device;
862 struct evdev_dispatch *dispatch = evdev->dispatch;
864 return dispatch->sendevents.current_mode;
867 static enum libinput_config_send_events_mode
868 evdev_sendevents_get_default_mode(struct libinput_device *device)
870 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
874 evdev_left_handed_has(struct libinput_device *device)
876 /* This is only hooked up when we have left-handed configuration, so we
877 * can hardcode 1 here */
882 evdev_change_to_left_handed(struct evdev_device *device)
884 if (device->left_handed.want_enabled == device->left_handed.enabled)
887 if (evdev_any_button_down(device))
890 device->left_handed.enabled = device->left_handed.want_enabled;
893 static enum libinput_config_status
894 evdev_left_handed_set(struct libinput_device *device, int left_handed)
896 struct evdev_device *evdev_device = (struct evdev_device *)device;
898 evdev_device->left_handed.want_enabled = left_handed ? true : false;
900 evdev_device->left_handed.change_to_enabled(evdev_device);
902 return LIBINPUT_CONFIG_STATUS_SUCCESS;
906 evdev_left_handed_get(struct libinput_device *device)
908 struct evdev_device *evdev_device = (struct evdev_device *)device;
910 /* return the wanted configuration, even if it hasn't taken
912 return evdev_device->left_handed.want_enabled;
916 evdev_left_handed_get_default(struct libinput_device *device)
922 evdev_init_left_handed(struct evdev_device *device,
923 void (*change_to_left_handed)(struct evdev_device *))
925 device->left_handed.config.has = evdev_left_handed_has;
926 device->left_handed.config.set = evdev_left_handed_set;
927 device->left_handed.config.get = evdev_left_handed_get;
928 device->left_handed.config.get_default = evdev_left_handed_get_default;
929 device->base.config.left_handed = &device->left_handed.config;
930 device->left_handed.enabled = false;
931 device->left_handed.want_enabled = false;
932 device->left_handed.change_to_enabled = change_to_left_handed;
938 evdev_scroll_get_methods(struct libinput_device *device)
940 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
944 evdev_change_scroll_method(struct evdev_device *device)
946 if (device->scroll.want_method == device->scroll.method &&
947 device->scroll.want_button == device->scroll.button)
950 if (evdev_any_button_down(device))
953 device->scroll.method = device->scroll.want_method;
954 device->scroll.button = device->scroll.want_button;
957 static enum libinput_config_status
958 evdev_scroll_set_method(struct libinput_device *device,
959 enum libinput_config_scroll_method method)
961 struct evdev_device *evdev = (struct evdev_device*)device;
963 evdev->scroll.want_method = method;
964 evdev->scroll.change_scroll_method(evdev);
966 return LIBINPUT_CONFIG_STATUS_SUCCESS;
969 static enum libinput_config_scroll_method
970 evdev_scroll_get_method(struct libinput_device *device)
972 struct evdev_device *evdev = (struct evdev_device *)device;
974 /* return the wanted configuration, even if it hasn't taken
976 return evdev->scroll.want_method;
979 static enum libinput_config_scroll_method
980 evdev_scroll_get_default_method(struct libinput_device *device)
982 struct evdev_device *evdev = (struct evdev_device *)device;
984 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
985 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
987 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
990 static enum libinput_config_status
991 evdev_scroll_set_button(struct libinput_device *device,
994 struct evdev_device *evdev = (struct evdev_device*)device;
996 evdev->scroll.want_button = button;
997 evdev->scroll.change_scroll_method(evdev);
999 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1003 evdev_scroll_get_button(struct libinput_device *device)
1005 struct evdev_device *evdev = (struct evdev_device *)device;
1007 /* return the wanted configuration, even if it hasn't taken
1009 return evdev->scroll.want_button;
1013 evdev_scroll_get_default_button(struct libinput_device *device)
1015 struct evdev_device *evdev = (struct evdev_device *)device;
1017 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
1024 evdev_init_button_scroll(struct evdev_device *device,
1025 void (*change_scroll_method)(struct evdev_device *))
1027 libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
1028 evdev_button_scroll_timeout, device);
1029 device->scroll.config.get_methods = evdev_scroll_get_methods;
1030 device->scroll.config.set_method = evdev_scroll_set_method;
1031 device->scroll.config.get_method = evdev_scroll_get_method;
1032 device->scroll.config.get_default_method = evdev_scroll_get_default_method;
1033 device->scroll.config.set_button = evdev_scroll_set_button;
1034 device->scroll.config.get_button = evdev_scroll_get_button;
1035 device->scroll.config.get_default_button = evdev_scroll_get_default_button;
1036 device->base.config.scroll_method = &device->scroll.config;
1037 device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
1038 device->scroll.want_method = device->scroll.method;
1039 device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
1040 device->scroll.want_button = device->scroll.button;
1041 device->scroll.change_scroll_method = change_scroll_method;
1047 evdev_init_calibration(struct evdev_device *device,
1048 struct evdev_dispatch *dispatch)
1050 device->base.config.calibration = &dispatch->calibration;
1052 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
1053 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
1054 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
1055 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
1059 evdev_init_sendevents(struct evdev_device *device,
1060 struct evdev_dispatch *dispatch)
1062 device->base.config.sendevents = &dispatch->sendevents.config;
1064 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1065 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
1066 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
1067 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
1068 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
1072 evdev_scroll_config_natural_has(struct libinput_device *device)
1077 static enum libinput_config_status
1078 evdev_scroll_config_natural_set(struct libinput_device *device,
1081 struct evdev_device *dev = (struct evdev_device *)device;
1083 dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1085 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1089 evdev_scroll_config_natural_get(struct libinput_device *device)
1091 struct evdev_device *dev = (struct evdev_device *)device;
1093 return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1097 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1099 /* could enable this on Apple touchpads. could do that, could
1100 * very well do that... */
1105 evdev_init_natural_scroll(struct evdev_device *device)
1107 device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1108 device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1109 device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1110 device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1111 device->scroll.natural_scrolling_enabled = false;
1112 device->base.config.natural_scroll = &device->scroll.config_natural;
1115 static struct evdev_dispatch *
1116 fallback_dispatch_create(struct libinput_device *device)
1118 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1119 struct evdev_device *evdev_device = (struct evdev_device *)device;
1121 if (dispatch == NULL)
1124 dispatch->interface = &fallback_interface;
1126 if (evdev_device->left_handed.want_enabled &&
1127 evdev_init_left_handed(evdev_device,
1128 evdev_change_to_left_handed) == -1) {
1133 if (evdev_device->scroll.want_button &&
1134 evdev_init_button_scroll(evdev_device,
1135 evdev_change_scroll_method) == -1) {
1140 if (evdev_device->scroll.natural_scrolling_enabled)
1141 evdev_init_natural_scroll(evdev_device);
1143 evdev_init_calibration(evdev_device, dispatch);
1144 evdev_init_sendevents(evdev_device, dispatch);
1146 /* BTN_MIDDLE is set on mice even when it's not present. So
1147 * we can only use the absense of BTN_MIDDLE to mean something, i.e.
1148 * we enable it by default on anything that only has L&R.
1149 * If we have L&R and no middle, we don't expose it as config
1151 if (libevdev_has_event_code(evdev_device->evdev, EV_KEY, BTN_LEFT) &&
1152 libevdev_has_event_code(evdev_device->evdev, EV_KEY, BTN_RIGHT)) {
1153 bool has_middle = libevdev_has_event_code(evdev_device->evdev,
1156 bool want_config = has_middle;
1157 bool enable_by_default = !has_middle;
1159 evdev_init_middlebutton(evdev_device,
1168 evdev_process_event(struct evdev_device *device, struct input_event *e)
1170 struct evdev_dispatch *dispatch = device->dispatch;
1171 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1174 if (libevdev_event_is_code(e, EV_SYN, SYN_REPORT))
1175 log_debug(device->base.seat->libinput,
1176 "-------------- EV_SYN ------------\n");
1178 log_debug(device->base.seat->libinput,
1179 "%-7s %-16s %-20s %4d\n",
1180 evdev_device_get_sysname(device),
1181 libevdev_event_type_get_name(e->type),
1182 libevdev_event_code_get_name(e->type, e->code),
1186 dispatch->interface->process(dispatch, device, e, time);
1190 evdev_device_dispatch_one(struct evdev_device *device,
1191 struct input_event *ev)
1193 if (!device->mtdev) {
1194 evdev_process_event(device, ev);
1196 mtdev_put_event(device->mtdev, ev);
1197 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1198 while (!mtdev_empty(device->mtdev)) {
1199 struct input_event e;
1200 mtdev_get_event(device->mtdev, &e);
1201 evdev_process_event(device, &e);
1208 evdev_sync_device(struct evdev_device *device)
1210 struct input_event ev;
1214 rc = libevdev_next_event(device->evdev,
1215 LIBEVDEV_READ_FLAG_SYNC, &ev);
1218 evdev_device_dispatch_one(device, &ev);
1219 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1221 return rc == -EAGAIN ? 0 : rc;
1225 evdev_device_dispatch(void *data)
1227 struct evdev_device *device = data;
1228 struct libinput *libinput = device->base.seat->libinput;
1229 struct input_event ev;
1232 /* If the compositor is repainting, this function is called only once
1233 * per frame and we have to process all the events available on the
1234 * fd, otherwise there will be input lag. */
1236 rc = libevdev_next_event(device->evdev,
1237 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1238 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1239 switch (ratelimit_test(&device->syn_drop_limit)) {
1240 case RATELIMIT_PASS:
1241 log_info(libinput, "SYN_DROPPED event from "
1242 "\"%s\" - some input events have "
1243 "been lost.\n", device->devname);
1245 case RATELIMIT_THRESHOLD:
1246 log_info(libinput, "SYN_DROPPED flood "
1250 case RATELIMIT_EXCEEDED:
1254 /* send one more sync event so we handle all
1255 currently pending events before we sync up
1256 to the current state */
1257 ev.code = SYN_REPORT;
1258 evdev_device_dispatch_one(device, &ev);
1260 rc = evdev_sync_device(device);
1262 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1263 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1264 evdev_device_dispatch_one(device, &ev);
1266 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1268 if (rc != -EAGAIN && rc != -EINTR) {
1269 libinput_remove_source(libinput, device->source);
1270 device->source = NULL;
1275 evdev_accel_config_available(struct libinput_device *device)
1277 /* this function is only called if we set up ptraccel, so we can
1278 reply with a resounding "Yes" */
1282 static enum libinput_config_status
1283 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1285 struct evdev_device *dev = (struct evdev_device *)device;
1287 if (!filter_set_speed(dev->pointer.filter, speed))
1288 return LIBINPUT_CONFIG_STATUS_INVALID;
1290 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1294 evdev_accel_config_get_speed(struct libinput_device *device)
1296 struct evdev_device *dev = (struct evdev_device *)device;
1298 return filter_get_speed(dev->pointer.filter);
1302 evdev_accel_config_get_default_speed(struct libinput_device *device)
1308 evdev_device_init_pointer_acceleration(struct evdev_device *device,
1309 accel_profile_func_t profile)
1311 device->pointer.filter = create_pointer_accelerator_filter(profile);
1312 if (!device->pointer.filter)
1315 device->pointer.config.available = evdev_accel_config_available;
1316 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1317 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1318 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1319 device->base.config.accel = &device->pointer.config;
1321 evdev_accel_config_set_speed(&device->base,
1322 evdev_accel_config_get_default_speed(&device->base));
1328 evdev_need_mtdev(struct evdev_device *device)
1330 struct libevdev *evdev = device->evdev;
1332 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1333 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1334 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1338 evdev_tag_device(struct evdev_device *device)
1340 if (device->dispatch->interface->tag_device)
1341 device->dispatch->interface->tag_device(device,
1342 device->udev_device);
1346 evdev_read_wheel_click_prop(struct evdev_device *device)
1348 struct libinput *libinput = device->base.seat->libinput;
1350 int angle = DEFAULT_WHEEL_CLICK_ANGLE;
1352 prop = udev_device_get_property_value(device->udev_device,
1353 "MOUSE_WHEEL_CLICK_ANGLE");
1355 angle = parse_mouse_wheel_click_angle_property(prop);
1358 "Mouse wheel click angle '%s' is present but invalid,"
1359 "using %d degrees instead\n",
1361 DEFAULT_WHEEL_CLICK_ANGLE);
1362 angle = DEFAULT_WHEEL_CLICK_ANGLE;
1370 evdev_get_trackpoint_dpi(struct evdev_device *device)
1372 struct libinput *libinput = device->base.seat->libinput;
1373 const char *trackpoint_accel;
1374 double accel = DEFAULT_TRACKPOINT_ACCEL;
1376 trackpoint_accel = udev_device_get_property_value(
1377 device->udev_device, "POINTINGSTICK_CONST_ACCEL");
1378 if (trackpoint_accel) {
1379 accel = parse_trackpoint_accel_property(trackpoint_accel);
1381 log_error(libinput, "Trackpoint accel property for "
1382 "'%s' is present but invalid, "
1383 "using %.2f instead\n",
1385 DEFAULT_TRACKPOINT_ACCEL);
1386 accel = DEFAULT_TRACKPOINT_ACCEL;
1390 return DEFAULT_MOUSE_DPI / accel;
1394 evdev_read_dpi_prop(struct evdev_device *device)
1396 struct libinput *libinput = device->base.seat->libinput;
1397 const char *mouse_dpi;
1398 int dpi = DEFAULT_MOUSE_DPI;
1401 * Trackpoints do not have dpi, instead hwdb may contain a
1402 * POINTINGSTICK_CONST_ACCEL value to compensate for sensitivity
1403 * differences between models, we translate this to a fake dpi.
1405 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
1406 return evdev_get_trackpoint_dpi(device);
1408 mouse_dpi = udev_device_get_property_value(device->udev_device,
1411 dpi = parse_mouse_dpi_property(mouse_dpi);
1413 log_error(libinput, "Mouse DPI property for '%s' is "
1414 "present but invalid, using %d "
1418 dpi = DEFAULT_MOUSE_DPI;
1425 static inline enum evdev_device_model
1426 evdev_read_model(struct evdev_device *device)
1428 const struct model_map {
1429 const char *property;
1430 enum evdev_device_model model;
1432 { NULL, EVDEV_MODEL_DEFAULT },
1434 const struct model_map *m = model_map;
1436 while (m->property) {
1437 if (!!udev_device_get_property_value(device->udev_device,
1446 /* Return 1 if the given resolutions have been set, or 0 otherwise */
1448 evdev_fix_abs_resolution(struct evdev_device *device,
1454 struct libinput *libinput = device->base.seat->libinput;
1455 struct libevdev *evdev = device->evdev;
1456 const struct input_absinfo *absx, *absy;
1457 struct input_absinfo fixed;
1460 if (!(xcode == ABS_X && ycode == ABS_Y) &&
1461 !(xcode == ABS_MT_POSITION_X && ycode == ABS_MT_POSITION_Y)) {
1462 log_bug_libinput(libinput,
1463 "Invalid x/y code combination %d/%d\n",
1468 if (xresolution == 0 || yresolution == 0 ||
1469 (xresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution) ||
1470 (yresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution)) {
1471 log_bug_libinput(libinput,
1472 "Invalid x/y resolutions %d/%d\n",
1473 xresolution, yresolution);
1477 absx = libevdev_get_abs_info(evdev, xcode);
1478 absy = libevdev_get_abs_info(evdev, ycode);
1480 if (absx->resolution == 0 || absx->resolution == EVDEV_FAKE_RESOLUTION) {
1482 fixed.resolution = xresolution;
1483 /* libevdev_set_abs_info() changes the absinfo we already
1484 have a pointer to, no need to fetch it again */
1485 libevdev_set_abs_info(evdev, xcode, &fixed);
1489 if (absy->resolution == 0 || absy->resolution == EVDEV_FAKE_RESOLUTION) {
1491 fixed.resolution = yresolution;
1492 /* libevdev_set_abs_info() changes the absinfo we already
1493 have a pointer to, no need to fetch it again */
1494 libevdev_set_abs_info(evdev, ycode, &fixed);
1501 static enum evdev_device_udev_tags
1502 evdev_device_get_udev_tags(struct evdev_device *device,
1503 struct udev_device *udev_device)
1506 enum evdev_device_udev_tags tags = 0;
1507 const struct evdev_udev_tag_match *match;
1510 for (i = 0; i < 2 && udev_device; i++) {
1511 match = evdev_udev_tag_matches;
1512 while (match->name) {
1513 prop = udev_device_get_property_value(
1521 udev_device = udev_device_get_parent(udev_device);
1527 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1528 the limited ABS_* range - they aren't MT devices, they
1529 just have too many ABS_ axes */
1531 evdev_is_fake_mt_device(struct evdev_device *device)
1533 struct libevdev *evdev = device->evdev;
1535 return libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1536 libevdev_get_num_slots(evdev) == -1;
1540 evdev_fix_android_mt(struct evdev_device *device)
1542 struct libevdev *evdev = device->evdev;
1544 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1545 libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1548 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1549 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) ||
1550 evdev_is_fake_mt_device(device))
1553 libevdev_enable_event_code(evdev, EV_ABS, ABS_X,
1554 libevdev_get_abs_info(evdev, ABS_MT_POSITION_X));
1555 libevdev_enable_event_code(evdev, EV_ABS, ABS_Y,
1556 libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y));
1560 evdev_check_min_max(struct evdev_device *device, unsigned int code)
1562 struct libevdev *evdev = device->evdev;
1563 const struct input_absinfo *absinfo;
1565 if (!libevdev_has_event_code(evdev, EV_ABS, code))
1568 absinfo = libevdev_get_abs_info(evdev, code);
1569 if (absinfo->minimum == absinfo->maximum) {
1570 /* Some devices have a sort-of legitimate min/max of 0 for
1571 * ABS_MISC and above (e.g. Roccat Kone XTD). Don't ignore
1572 * them, simply disable the axes so we won't get events,
1573 * we don't know what to do with them anyway.
1575 if (absinfo->minimum == 0 &&
1576 code >= ABS_MISC && code < ABS_MT_SLOT) {
1577 log_info(device->base.seat->libinput,
1578 "Disabling EV_ABS %#x on device '%s' (min == max == 0)\n",
1581 libevdev_disable_event_code(device->evdev,
1585 log_bug_kernel(device->base.seat->libinput,
1586 "Device '%s' has min == max on %s\n",
1588 libevdev_event_code_get_name(EV_ABS, code));
1597 evdev_reject_device(struct evdev_device *device)
1599 struct libinput *libinput = device->base.seat->libinput;
1600 struct libevdev *evdev = device->evdev;
1602 const struct input_absinfo *absx, *absy;
1604 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ^
1605 libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1608 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ^
1609 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1612 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
1613 absx = libevdev_get_abs_info(evdev, ABS_X);
1614 absy = libevdev_get_abs_info(evdev, ABS_Y);
1615 if ((absx->resolution == 0 && absy->resolution != 0) ||
1616 (absx->resolution != 0 && absy->resolution == 0)) {
1617 log_bug_kernel(libinput,
1618 "Kernel has only x or y resolution, not both.\n");
1623 if (!evdev_is_fake_mt_device(device) &&
1624 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X)) {
1625 absx = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1626 absy = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1627 if ((absx->resolution == 0 && absy->resolution != 0) ||
1628 (absx->resolution != 0 && absy->resolution == 0)) {
1629 log_bug_kernel(libinput,
1630 "Kernel has only x or y MT resolution, not both.\n");
1635 for (code = 0; code < ABS_CNT; code++) {
1639 case ABS_MT_TOOL_TYPE:
1642 if (evdev_check_min_max(device, code) == -1)
1651 evdev_configure_mt_device(struct evdev_device *device)
1653 struct libevdev *evdev = device->evdev;
1654 struct mt_slot *slots;
1659 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1660 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1663 if (evdev_fix_abs_resolution(device,
1666 EVDEV_FAKE_RESOLUTION,
1667 EVDEV_FAKE_RESOLUTION))
1668 device->abs.fake_resolution = 1;
1670 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1671 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1674 /* We only handle the slotted Protocol B in libinput.
1675 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1676 require mtdev for conversion. */
1677 if (evdev_need_mtdev(device)) {
1678 device->mtdev = mtdev_new_open(device->fd);
1682 /* pick 10 slots as default for type A
1685 active_slot = device->mtdev->caps.slot.value;
1687 num_slots = libevdev_get_num_slots(device->evdev);
1688 active_slot = libevdev_get_current_slot(evdev);
1691 slots = calloc(num_slots, sizeof(struct mt_slot));
1695 for (slot = 0; slot < num_slots; ++slot) {
1696 slots[slot].seat_slot = -1;
1697 slots[slot].point.x = 0;
1698 slots[slot].point.y = 0;
1700 device->mt.slots = slots;
1701 device->mt.slots_len = num_slots;
1702 device->mt.slot = active_slot;
1708 evdev_configure_device(struct evdev_device *device)
1710 struct libinput *libinput = device->base.seat->libinput;
1711 struct libevdev *evdev = device->evdev;
1712 const char *devnode = udev_device_get_devnode(device->udev_device);
1713 enum evdev_device_udev_tags udev_tags;
1715 udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1717 if ((udev_tags & EVDEV_UDEV_TAG_INPUT) == 0 ||
1718 (udev_tags & ~EVDEV_UDEV_TAG_INPUT) == 0) {
1720 "input device '%s', %s not tagged as input device\n",
1721 device->devname, devnode);
1726 "input device '%s', %s is tagged by udev as:%s%s%s%s%s%s%s%s\n",
1727 device->devname, devnode,
1728 udev_tags & EVDEV_UDEV_TAG_KEYBOARD ? " Keyboard" : "",
1729 udev_tags & EVDEV_UDEV_TAG_MOUSE ? " Mouse" : "",
1730 udev_tags & EVDEV_UDEV_TAG_TOUCHPAD ? " Touchpad" : "",
1731 udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN ? " Touchscreen" : "",
1732 udev_tags & EVDEV_UDEV_TAG_TABLET ? " Tablet" : "",
1733 udev_tags & EVDEV_UDEV_TAG_JOYSTICK ? " Joystick" : "",
1734 udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER ? " Accelerometer" : "",
1735 udev_tags & EVDEV_UDEV_TAG_BUTTONSET ? " Buttonset" : "");
1737 /* libwacom *adds* TABLET, TOUCHPAD but leaves JOYSTICK in place, so
1738 make sure we only ignore real joystick devices */
1739 if ((udev_tags & EVDEV_UDEV_TAG_JOYSTICK) == udev_tags) {
1741 "input device '%s', %s is a joystick, ignoring\n",
1742 device->devname, devnode);
1746 /* libwacom assigns tablet _and_ tablet_pad to the pad devices */
1747 if (udev_tags & EVDEV_UDEV_TAG_BUTTONSET) {
1749 "input device '%s', %s is a buttonset, ignoring\n",
1750 device->devname, devnode);
1754 if (evdev_reject_device(device) == -1) {
1756 "input device '%s', %s was rejected.\n",
1757 device->devname, devnode);
1761 if (!evdev_is_fake_mt_device(device))
1762 evdev_fix_android_mt(device);
1764 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
1765 if (evdev_fix_abs_resolution(device,
1768 EVDEV_FAKE_RESOLUTION,
1769 EVDEV_FAKE_RESOLUTION))
1770 device->abs.fake_resolution = 1;
1771 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_X);
1772 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_Y);
1774 if (evdev_is_fake_mt_device(device)) {
1775 udev_tags &= ~EVDEV_UDEV_TAG_TOUCHSCREEN;
1776 } else if (evdev_configure_mt_device(device) == -1) {
1781 if (udev_tags & EVDEV_UDEV_TAG_TOUCHPAD) {
1782 device->dispatch = evdev_mt_touchpad_create(device);
1784 "input device '%s', %s is a touchpad\n",
1785 device->devname, devnode);
1786 return device->dispatch == NULL ? -1 : 0;
1789 if (udev_tags & EVDEV_UDEV_TAG_MOUSE) {
1790 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
1791 !libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
1792 evdev_device_init_pointer_acceleration(
1794 pointer_accel_profile_linear) == -1)
1797 device->seat_caps |= EVDEV_DEVICE_POINTER;
1800 "input device '%s', %s is a pointer caps\n",
1801 device->devname, devnode);
1803 /* want left-handed config option */
1804 device->left_handed.want_enabled = true;
1805 /* want natural-scroll config option */
1806 device->scroll.natural_scrolling_enabled = true;
1807 /* want button scrolling config option */
1808 device->scroll.want_button = 1;
1811 if (udev_tags & EVDEV_UDEV_TAG_KEYBOARD) {
1812 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1814 "input device '%s', %s is a keyboard\n",
1815 device->devname, devnode);
1817 /* want natural-scroll config option */
1818 if (libevdev_has_event_code(evdev, EV_REL, REL_WHEEL) ||
1819 libevdev_has_event_code(evdev, EV_REL, REL_HWHEEL)) {
1820 device->scroll.natural_scrolling_enabled = true;
1821 device->seat_caps |= EVDEV_DEVICE_POINTER;
1825 if (udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN) {
1826 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1828 "input device '%s', %s is a touch device\n",
1829 device->devname, devnode);
1836 evdev_notify_added_device(struct evdev_device *device)
1838 struct libinput_device *dev;
1840 list_for_each(dev, &device->base.seat->devices_list, link) {
1841 struct evdev_device *d = (struct evdev_device*)dev;
1842 if (dev == &device->base)
1845 /* Notify existing device d about addition of device device */
1846 if (d->dispatch->interface->device_added)
1847 d->dispatch->interface->device_added(d, device);
1849 /* Notify new device device about existing device d */
1850 if (device->dispatch->interface->device_added)
1851 device->dispatch->interface->device_added(device, d);
1853 /* Notify new device device if existing device d is suspended */
1854 if (d->suspended && device->dispatch->interface->device_suspended)
1855 device->dispatch->interface->device_suspended(device, d);
1858 notify_added_device(&device->base);
1862 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1864 struct udev *udev = udev_device_get_udev(udev_device);
1865 struct udev_device *udev_device_new = NULL;
1869 if (fstat(fd, &st) < 0)
1872 udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1873 if (!udev_device_new)
1876 rc = strcmp(udev_device_get_syspath(udev_device_new),
1877 udev_device_get_syspath(udev_device));
1879 if (udev_device_new)
1880 udev_device_unref(udev_device_new);
1885 evdev_set_device_group(struct evdev_device *device,
1886 struct udev_device *udev_device)
1888 struct libinput_device_group *group = NULL;
1889 const char *udev_group;
1891 udev_group = udev_device_get_property_value(udev_device,
1892 "LIBINPUT_DEVICE_GROUP");
1894 struct libinput_device *d;
1896 list_for_each(d, &device->base.seat->devices_list, link) {
1897 const char *identifier = d->group->identifier;
1900 strcmp(identifier, udev_group) == 0) {
1908 group = libinput_device_group_create(udev_group);
1911 libinput_device_set_device_group(&device->base, group);
1912 libinput_device_group_unref(group);
1914 libinput_device_set_device_group(&device->base, group);
1920 struct evdev_device *
1921 evdev_device_create(struct libinput_seat *seat,
1922 struct udev_device *udev_device)
1924 struct libinput *libinput = seat->libinput;
1925 struct evdev_device *device = NULL;
1928 int unhandled_device = 0;
1929 const char *devnode = udev_device_get_devnode(udev_device);
1931 /* Use non-blocking mode so that we can loop on read on
1932 * evdev_device_data() until all events on the fd are
1933 * read. mtdev_get() also expects this. */
1934 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1937 "opening input device '%s' failed (%s).\n",
1938 devnode, strerror(-fd));
1942 if (evdev_device_compare_syspath(udev_device, fd) != 0)
1945 device = zalloc(sizeof *device);
1949 libinput_device_init(&device->base, seat);
1950 libinput_seat_ref(seat);
1952 rc = libevdev_new_from_fd(fd, &device->evdev);
1956 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1958 device->seat_caps = 0;
1960 device->mtdev = NULL;
1961 device->udev_device = udev_device_ref(udev_device);
1964 device->abs.seat_slot = -1;
1965 device->dispatch = NULL;
1967 device->pending_event = EVDEV_NONE;
1968 device->devname = libevdev_get_name(device->evdev);
1969 device->scroll.threshold = 5.0; /* Default may be overridden */
1970 device->scroll.direction = 0;
1971 device->scroll.wheel_click_angle =
1972 evdev_read_wheel_click_prop(device);
1973 device->dpi = evdev_read_dpi_prop(device);
1974 device->model = evdev_read_model(device);
1975 /* at most 5 SYN_DROPPED log-messages per 30s */
1976 ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1978 matrix_init_identity(&device->abs.calibration);
1979 matrix_init_identity(&device->abs.usermatrix);
1980 matrix_init_identity(&device->abs.default_calibration);
1982 if (evdev_configure_device(device) == -1)
1985 if (device->seat_caps == 0) {
1986 unhandled_device = 1;
1990 /* If the dispatch was not set up use the fallback. */
1991 if (device->dispatch == NULL)
1992 device->dispatch = fallback_dispatch_create(&device->base);
1993 if (device->dispatch == NULL)
1997 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1998 if (!device->source)
2001 if (evdev_set_device_group(device, udev_device))
2004 list_insert(seat->devices_list.prev, &device->base.link);
2006 evdev_tag_device(device);
2007 evdev_notify_added_device(device);
2013 close_restricted(libinput, fd);
2015 evdev_device_destroy(device);
2017 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
2021 evdev_device_get_output(struct evdev_device *device)
2023 return device->output_name;
2027 evdev_device_get_sysname(struct evdev_device *device)
2029 return udev_device_get_sysname(device->udev_device);
2033 evdev_device_get_name(struct evdev_device *device)
2035 return device->devname;
2039 evdev_device_get_id_product(struct evdev_device *device)
2041 return libevdev_get_id_product(device->evdev);
2045 evdev_device_get_id_vendor(struct evdev_device *device)
2047 return libevdev_get_id_vendor(device->evdev);
2050 struct udev_device *
2051 evdev_device_get_udev_device(struct evdev_device *device)
2053 return udev_device_ref(device->udev_device);
2057 evdev_device_set_default_calibration(struct evdev_device *device,
2058 const float calibration[6])
2060 matrix_from_farray6(&device->abs.default_calibration, calibration);
2061 evdev_device_calibrate(device, calibration);
2065 evdev_device_calibrate(struct evdev_device *device,
2066 const float calibration[6])
2068 struct matrix scale,
2073 matrix_from_farray6(&transform, calibration);
2074 device->abs.apply_calibration = !matrix_is_identity(&transform);
2076 if (!device->abs.apply_calibration) {
2077 matrix_init_identity(&device->abs.calibration);
2081 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
2082 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
2084 /* The transformation matrix is in the form:
2088 * Where a, e are the scale components, a, b, d, e are the rotation
2089 * component (combined with scale) and c and f are the translation
2090 * component. The translation component in the input matrix must be
2091 * normalized to multiples of the device width and height,
2092 * respectively. e.g. c == 1 shifts one device-width to the right.
2094 * We pre-calculate a single matrix to apply to event coordinates:
2095 * M = Un-Normalize * Calibration * Normalize
2097 * Normalize: scales the device coordinates to [0,1]
2098 * Calibration: user-supplied matrix
2099 * Un-Normalize: scales back up to device coordinates
2100 * Matrix maths requires the normalize/un-normalize in reverse
2104 /* back up the user matrix so we can return it on request */
2105 matrix_from_farray6(&device->abs.usermatrix, calibration);
2108 matrix_init_translate(&translate,
2109 device->abs.absinfo_x->minimum,
2110 device->abs.absinfo_y->minimum);
2111 matrix_init_scale(&scale, sx, sy);
2112 matrix_mult(&scale, &translate, &scale);
2115 matrix_mult(&transform, &scale, &transform);
2118 matrix_init_translate(&translate,
2119 -device->abs.absinfo_x->minimum/sx,
2120 -device->abs.absinfo_y->minimum/sy);
2121 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
2122 matrix_mult(&scale, &translate, &scale);
2124 /* store final matrix in device */
2125 matrix_mult(&device->abs.calibration, &transform, &scale);
2129 evdev_device_has_capability(struct evdev_device *device,
2130 enum libinput_device_capability capability)
2132 switch (capability) {
2133 case LIBINPUT_DEVICE_CAP_POINTER:
2134 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
2135 case LIBINPUT_DEVICE_CAP_KEYBOARD:
2136 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
2137 case LIBINPUT_DEVICE_CAP_TOUCH:
2138 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
2145 evdev_device_get_size(struct evdev_device *device,
2149 const struct input_absinfo *x, *y;
2151 x = libevdev_get_abs_info(device->evdev, ABS_X);
2152 y = libevdev_get_abs_info(device->evdev, ABS_Y);
2154 if (!x || !y || device->abs.fake_resolution ||
2155 !x->resolution || !y->resolution)
2158 *width = evdev_convert_to_mm(x, x->maximum);
2159 *height = evdev_convert_to_mm(y, y->maximum);
2165 evdev_device_has_button(struct evdev_device *device, uint32_t code)
2167 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
2170 return libevdev_has_event_code(device->evdev, EV_KEY, code);
2174 evdev_device_has_key(struct evdev_device *device, uint32_t code)
2176 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
2179 return libevdev_has_event_code(device->evdev, EV_KEY, code);
2183 evdev_is_scrolling(const struct evdev_device *device,
2184 enum libinput_pointer_axis axis)
2186 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2187 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2189 return (device->scroll.direction & AS_MASK(axis)) != 0;
2193 evdev_start_scrolling(struct evdev_device *device,
2194 enum libinput_pointer_axis axis)
2196 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2197 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2199 device->scroll.direction |= AS_MASK(axis);
2203 evdev_post_scroll(struct evdev_device *device,
2205 enum libinput_pointer_axis_source source,
2206 const struct normalized_coords *delta)
2208 const struct normalized_coords *trigger;
2209 struct normalized_coords event;
2211 if (!evdev_is_scrolling(device,
2212 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2213 device->scroll.buildup.y += delta->y;
2214 if (!evdev_is_scrolling(device,
2215 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2216 device->scroll.buildup.x += delta->x;
2218 trigger = &device->scroll.buildup;
2220 /* If we're not scrolling yet, use a distance trigger: moving
2221 past a certain distance starts scrolling */
2222 if (!evdev_is_scrolling(device,
2223 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
2224 !evdev_is_scrolling(device,
2225 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2226 if (fabs(trigger->y) >= device->scroll.threshold)
2227 evdev_start_scrolling(device,
2228 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2229 if (fabs(trigger->x) >= device->scroll.threshold)
2230 evdev_start_scrolling(device,
2231 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2232 /* We're already scrolling in one direction. Require some
2233 trigger speed to start scrolling in the other direction */
2234 } else if (!evdev_is_scrolling(device,
2235 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2236 if (fabs(delta->y) >= device->scroll.threshold)
2237 evdev_start_scrolling(device,
2238 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2239 } else if (!evdev_is_scrolling(device,
2240 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
2241 if (fabs(delta->x) >= device->scroll.threshold)
2242 evdev_start_scrolling(device,
2243 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2248 /* We use the trigger to enable, but the delta from this event for
2249 * the actual scroll movement. Otherwise we get a jump once
2250 * scrolling engages */
2251 if (!evdev_is_scrolling(device,
2252 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2255 if (!evdev_is_scrolling(device,
2256 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2259 if (!normalized_is_zero(event)) {
2260 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2261 evdev_notify_axis(device,
2263 device->scroll.direction,
2271 evdev_stop_scroll(struct evdev_device *device,
2273 enum libinput_pointer_axis_source source)
2275 const struct normalized_coords zero = { 0.0, 0.0 };
2276 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2278 /* terminate scrolling with a zero scroll event */
2279 if (device->scroll.direction != 0)
2280 pointer_notify_axis(&device->base,
2282 device->scroll.direction,
2287 device->scroll.buildup.x = 0;
2288 device->scroll.buildup.y = 0;
2289 device->scroll.direction = 0;
2293 release_pressed_keys(struct evdev_device *device)
2295 struct libinput *libinput = device->base.seat->libinput;
2299 if ((time = libinput_now(libinput)) == 0)
2302 for (code = 0; code < KEY_CNT; code++) {
2303 int count = get_key_down_count(device, code);
2306 log_bug_libinput(libinput,
2307 "Key %d is down %d times.\n",
2312 while (get_key_down_count(device, code) > 0) {
2313 switch (get_key_type(code)) {
2314 case EVDEV_KEY_TYPE_NONE:
2316 case EVDEV_KEY_TYPE_KEY:
2317 evdev_keyboard_notify_key(
2321 LIBINPUT_KEY_STATE_RELEASED);
2323 case EVDEV_KEY_TYPE_BUTTON:
2324 evdev_pointer_notify_physical_button(
2327 evdev_to_left_handed(device, code),
2328 LIBINPUT_BUTTON_STATE_RELEASED);
2336 evdev_notify_suspended_device(struct evdev_device *device)
2338 struct libinput_device *it;
2340 if (device->suspended)
2343 list_for_each(it, &device->base.seat->devices_list, link) {
2344 struct evdev_device *d = (struct evdev_device*)it;
2345 if (it == &device->base)
2348 if (d->dispatch->interface->device_suspended)
2349 d->dispatch->interface->device_suspended(d, device);
2352 device->suspended = 1;
2356 evdev_notify_resumed_device(struct evdev_device *device)
2358 struct libinput_device *it;
2360 if (!device->suspended)
2363 list_for_each(it, &device->base.seat->devices_list, link) {
2364 struct evdev_device *d = (struct evdev_device*)it;
2365 if (it == &device->base)
2368 if (d->dispatch->interface->device_resumed)
2369 d->dispatch->interface->device_resumed(d, device);
2372 device->suspended = 0;
2376 evdev_device_suspend(struct evdev_device *device)
2378 evdev_notify_suspended_device(device);
2380 if (device->source) {
2381 libinput_remove_source(device->base.seat->libinput,
2383 device->source = NULL;
2386 release_pressed_keys(device);
2388 if (device->mtdev) {
2389 mtdev_close_delete(device->mtdev);
2390 device->mtdev = NULL;
2393 if (device->fd != -1) {
2394 close_restricted(device->base.seat->libinput, device->fd);
2402 evdev_device_resume(struct evdev_device *device)
2404 struct libinput *libinput = device->base.seat->libinput;
2406 const char *devnode;
2407 struct input_event ev;
2408 enum libevdev_read_status status;
2410 if (device->fd != -1)
2413 if (device->was_removed)
2416 devnode = udev_device_get_devnode(device->udev_device);
2417 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
2422 if (evdev_device_compare_syspath(device->udev_device, fd)) {
2423 close_restricted(libinput, fd);
2429 if (evdev_need_mtdev(device)) {
2430 device->mtdev = mtdev_new_open(device->fd);
2435 libevdev_change_fd(device->evdev, fd);
2436 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
2438 /* re-sync libevdev's view of the device, but discard the actual
2439 events. Our device is in a neutral state already */
2440 libevdev_next_event(device->evdev,
2441 LIBEVDEV_READ_FLAG_FORCE_SYNC,
2444 status = libevdev_next_event(device->evdev,
2445 LIBEVDEV_READ_FLAG_SYNC,
2447 } while (status == LIBEVDEV_READ_STATUS_SYNC);
2450 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2451 if (!device->source) {
2452 mtdev_close_delete(device->mtdev);
2456 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2458 evdev_notify_resumed_device(device);
2464 evdev_device_remove(struct evdev_device *device)
2466 struct libinput_device *dev;
2468 list_for_each(dev, &device->base.seat->devices_list, link) {
2469 struct evdev_device *d = (struct evdev_device*)dev;
2470 if (dev == &device->base)
2473 if (d->dispatch->interface->device_removed)
2474 d->dispatch->interface->device_removed(d, device);
2477 evdev_device_suspend(device);
2479 if (device->dispatch->interface->remove)
2480 device->dispatch->interface->remove(device->dispatch);
2482 /* A device may be removed while suspended, mark it to
2483 * skip re-opening a different device with the same node */
2484 device->was_removed = true;
2486 list_remove(&device->base.link);
2488 notify_removed_device(&device->base);
2489 libinput_device_unref(&device->base);
2493 evdev_device_destroy(struct evdev_device *device)
2495 struct evdev_dispatch *dispatch;
2497 dispatch = device->dispatch;
2499 dispatch->interface->destroy(dispatch);
2501 if (device->base.group)
2502 libinput_device_group_unref(device->base.group);
2504 filter_destroy(device->pointer.filter);
2505 libinput_seat_unref(device->base.seat);
2506 libevdev_free(device->evdev);
2507 udev_device_unref(device->udev_device);
2508 free(device->mt.slots);