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),
63 struct evdev_udev_tag_match {
65 enum evdev_device_udev_tags tag;
68 static const struct evdev_udev_tag_match evdev_udev_tag_matches[] = {
69 {"ID_INPUT", EVDEV_UDEV_TAG_INPUT},
70 {"ID_INPUT_KEYBOARD", EVDEV_UDEV_TAG_KEYBOARD},
71 {"ID_INPUT_KEY", EVDEV_UDEV_TAG_KEYBOARD},
72 {"ID_INPUT_MOUSE", EVDEV_UDEV_TAG_MOUSE},
73 {"ID_INPUT_TOUCHPAD", EVDEV_UDEV_TAG_TOUCHPAD},
74 {"ID_INPUT_TOUCHSCREEN", EVDEV_UDEV_TAG_TOUCHSCREEN},
75 {"ID_INPUT_TABLET", EVDEV_UDEV_TAG_TABLET},
76 {"ID_INPUT_JOYSTICK", EVDEV_UDEV_TAG_JOYSTICK},
77 {"ID_INPUT_ACCELEROMETER", EVDEV_UDEV_TAG_ACCELEROMETER},
84 hw_set_key_down(struct evdev_device *device, int code, int pressed)
86 long_set_bit_state(device->hw_key_mask, code, pressed);
90 hw_is_key_down(struct evdev_device *device, int code)
92 return long_bit_is_set(device->hw_key_mask, code);
96 get_key_down_count(struct evdev_device *device, int code)
98 return device->key_count[code];
102 update_key_down_count(struct evdev_device *device, int code, int pressed)
105 assert(code >= 0 && code < KEY_CNT);
108 key_count = ++device->key_count[code];
110 assert(device->key_count[code] > 0);
111 key_count = --device->key_count[code];
114 if (key_count > 32) {
115 log_bug_libinput(device->base.seat->libinput,
116 "Key count for %s reached abnormal values\n",
117 libevdev_event_code_get_name(EV_KEY, code));
124 evdev_keyboard_notify_key(struct evdev_device *device,
127 enum libinput_key_state state)
131 down_count = update_key_down_count(device, key, state);
133 if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
134 (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
135 keyboard_notify_key(&device->base, time, key, state);
139 evdev_pointer_notify_button(struct evdev_device *device,
142 enum libinput_button_state state)
146 down_count = update_key_down_count(device, button, state);
148 if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
149 (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
150 pointer_notify_button(&device->base, time, button, state);
152 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
153 device->left_handed.change_to_enabled)
154 device->left_handed.change_to_enabled(device);
156 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
157 device->scroll.change_scroll_method)
158 device->scroll.change_scroll_method(device);
164 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
166 static const struct {
167 enum libinput_led weston;
170 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
171 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
172 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
174 struct input_event ev[ARRAY_LENGTH(map) + 1];
177 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
180 memset(ev, 0, sizeof(ev));
181 for (i = 0; i < ARRAY_LENGTH(map); i++) {
183 ev[i].code = map[i].evdev;
184 ev[i].value = !!(leds & map[i].weston);
187 ev[i].code = SYN_REPORT;
189 i = write(device->fd, ev, sizeof ev);
190 (void)i; /* no, we really don't care about the return value */
194 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
196 if (!device->abs.apply_calibration)
199 matrix_mult_vec(&device->abs.calibration, x, y);
203 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
205 return (val - absinfo->minimum) * to_range /
206 (absinfo->maximum - absinfo->minimum + 1);
210 evdev_device_transform_x(struct evdev_device *device,
214 return scale_axis(device->abs.absinfo_x, x, width);
218 evdev_device_transform_y(struct evdev_device *device,
222 return scale_axis(device->abs.absinfo_y, y, height);
226 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
228 struct libinput *libinput = device->base.seat->libinput;
229 struct motion_params motion;
230 double dx_unaccel, dy_unaccel;
235 struct libinput_device *base = &device->base;
236 struct libinput_seat *seat = base->seat;
238 slot = device->mt.slot;
240 switch (device->pending_event) {
243 case EVDEV_RELATIVE_MOTION:
244 dx_unaccel = device->rel.dx / ((double) device->dpi /
246 dy_unaccel = device->rel.dy / ((double) device->dpi /
251 /* Use unaccelerated deltas for pointing stick scroll */
252 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
253 hw_is_key_down(device, device->scroll.button)) {
254 if (device->scroll.button_scroll_active)
255 evdev_post_scroll(device, time,
256 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
257 dx_unaccel, dy_unaccel);
261 /* Apply pointer acceleration. */
262 motion.dx = dx_unaccel;
263 motion.dy = dy_unaccel;
264 filter_dispatch(device->pointer.filter, &motion, device, time);
266 if (motion.dx == 0.0 && motion.dy == 0.0 &&
267 dx_unaccel == 0.0 && dy_unaccel == 0.0) {
271 pointer_notify_motion(base, time,
272 motion.dx, motion.dy,
273 dx_unaccel, dy_unaccel);
275 case EVDEV_ABSOLUTE_MT_DOWN:
276 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
279 if (device->mt.slots[slot].seat_slot != -1) {
280 log_bug_kernel(libinput,
281 "%s: Driver sent multiple touch down for the "
283 udev_device_get_devnode(device->udev_device));
287 seat_slot = ffs(~seat->slot_map) - 1;
288 device->mt.slots[slot].seat_slot = seat_slot;
293 seat->slot_map |= 1 << seat_slot;
294 x = device->mt.slots[slot].x;
295 y = device->mt.slots[slot].y;
296 transform_absolute(device, &x, &y);
298 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
300 case EVDEV_ABSOLUTE_MT_MOTION:
301 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
304 seat_slot = device->mt.slots[slot].seat_slot;
305 x = device->mt.slots[slot].x;
306 y = device->mt.slots[slot].y;
311 transform_absolute(device, &x, &y);
312 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
314 case EVDEV_ABSOLUTE_MT_UP:
315 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
318 seat_slot = device->mt.slots[slot].seat_slot;
319 device->mt.slots[slot].seat_slot = -1;
324 seat->slot_map &= ~(1 << seat_slot);
326 touch_notify_touch_up(base, time, slot, seat_slot);
328 case EVDEV_ABSOLUTE_TOUCH_DOWN:
329 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
332 if (device->abs.seat_slot != -1) {
333 log_bug_kernel(libinput,
334 "%s: Driver sent multiple touch down for the "
336 udev_device_get_devnode(device->udev_device));
340 seat_slot = ffs(~seat->slot_map) - 1;
341 device->abs.seat_slot = seat_slot;
346 seat->slot_map |= 1 << seat_slot;
350 transform_absolute(device, &cx, &cy);
352 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
354 case EVDEV_ABSOLUTE_MOTION:
357 transform_absolute(device, &cx, &cy);
361 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
362 seat_slot = device->abs.seat_slot;
367 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
368 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
369 pointer_notify_motion_absolute(base, time, x, y);
372 case EVDEV_ABSOLUTE_TOUCH_UP:
373 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
376 seat_slot = device->abs.seat_slot;
377 device->abs.seat_slot = -1;
382 seat->slot_map &= ~(1 << seat_slot);
384 touch_notify_touch_up(base, time, -1, seat_slot);
387 assert(0 && "Unknown pending event type");
391 device->pending_event = EVDEV_NONE;
394 static enum evdev_key_type
395 get_key_type(uint16_t code)
397 if (code == BTN_TOUCH)
398 return EVDEV_KEY_TYPE_NONE;
400 if (code >= KEY_ESC && code <= KEY_MICMUTE)
401 return EVDEV_KEY_TYPE_KEY;
402 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
403 return EVDEV_KEY_TYPE_BUTTON;
404 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
405 return EVDEV_KEY_TYPE_KEY;
406 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
407 return EVDEV_KEY_TYPE_BUTTON;
408 return EVDEV_KEY_TYPE_NONE;
412 evdev_button_scroll_timeout(uint64_t time, void *data)
414 struct evdev_device *device = data;
416 device->scroll.button_scroll_active = true;
420 evdev_button_scroll_button(struct evdev_device *device,
421 uint64_t time, int is_press)
424 libinput_timer_set(&device->scroll.timer,
425 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
427 libinput_timer_cancel(&device->scroll.timer);
428 if (device->scroll.button_scroll_active) {
429 evdev_stop_scroll(device, time,
430 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS);
431 device->scroll.button_scroll_active = false;
433 /* If the button is released quickly enough emit the
434 * button press/release events. */
435 evdev_pointer_notify_button(device, time,
436 device->scroll.button,
437 LIBINPUT_BUTTON_STATE_PRESSED);
438 evdev_pointer_notify_button(device, time,
439 device->scroll.button,
440 LIBINPUT_BUTTON_STATE_RELEASED);
446 evdev_process_touch_button(struct evdev_device *device,
447 uint64_t time, int value)
449 if (device->pending_event != EVDEV_NONE &&
450 device->pending_event != EVDEV_ABSOLUTE_MOTION)
451 evdev_flush_pending_event(device, time);
453 device->pending_event = (value ?
454 EVDEV_ABSOLUTE_TOUCH_DOWN :
455 EVDEV_ABSOLUTE_TOUCH_UP);
459 evdev_process_key(struct evdev_device *device,
460 struct input_event *e, uint64_t time)
462 enum evdev_key_type type;
464 /* ignore kernel key repeat */
468 if (e->code == BTN_TOUCH) {
470 evdev_process_touch_button(device, time, e->value);
474 evdev_flush_pending_event(device, time);
476 type = get_key_type(e->code);
478 /* Ignore key release events from the kernel for keys that libinput
479 * never got a pressed event for. */
482 case EVDEV_KEY_TYPE_NONE:
484 case EVDEV_KEY_TYPE_KEY:
485 case EVDEV_KEY_TYPE_BUTTON:
486 if (!hw_is_key_down(device, e->code))
491 hw_set_key_down(device, e->code, e->value);
494 case EVDEV_KEY_TYPE_NONE:
496 case EVDEV_KEY_TYPE_KEY:
497 evdev_keyboard_notify_key(
501 e->value ? LIBINPUT_KEY_STATE_PRESSED :
502 LIBINPUT_KEY_STATE_RELEASED);
504 case EVDEV_KEY_TYPE_BUTTON:
505 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
506 e->code == device->scroll.button) {
507 evdev_button_scroll_button(device, time, e->value);
510 evdev_pointer_notify_button(
513 evdev_to_left_handed(device, e->code),
514 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
515 LIBINPUT_BUTTON_STATE_RELEASED);
521 evdev_process_touch(struct evdev_device *device,
522 struct input_event *e,
527 evdev_flush_pending_event(device, time);
528 device->mt.slot = e->value;
530 case ABS_MT_TRACKING_ID:
531 if (device->pending_event != EVDEV_NONE &&
532 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
533 evdev_flush_pending_event(device, time);
535 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
537 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
539 case ABS_MT_POSITION_X:
540 device->mt.slots[device->mt.slot].x = e->value;
541 if (device->pending_event == EVDEV_NONE)
542 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
544 case ABS_MT_POSITION_Y:
545 device->mt.slots[device->mt.slot].y = e->value;
546 if (device->pending_event == EVDEV_NONE)
547 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
553 evdev_process_absolute_motion(struct evdev_device *device,
554 struct input_event *e)
558 device->abs.x = e->value;
559 if (device->pending_event == EVDEV_NONE)
560 device->pending_event = EVDEV_ABSOLUTE_MOTION;
563 device->abs.y = e->value;
564 if (device->pending_event == EVDEV_NONE)
565 device->pending_event = EVDEV_ABSOLUTE_MOTION;
571 evdev_notify_axis(struct evdev_device *device,
574 enum libinput_pointer_axis_source source,
576 double x_discrete, double y_discrete)
578 if (device->scroll.natural_scrolling_enabled) {
585 pointer_notify_axis(&device->base,
590 x_discrete, y_discrete);
594 evdev_process_relative(struct evdev_device *device,
595 struct input_event *e, uint64_t time)
599 if (device->pending_event != EVDEV_RELATIVE_MOTION)
600 evdev_flush_pending_event(device, time);
601 device->rel.dx += e->value;
602 device->pending_event = EVDEV_RELATIVE_MOTION;
605 if (device->pending_event != EVDEV_RELATIVE_MOTION)
606 evdev_flush_pending_event(device, time);
607 device->rel.dy += e->value;
608 device->pending_event = EVDEV_RELATIVE_MOTION;
611 evdev_flush_pending_event(device, time);
615 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
616 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
618 -1 * e->value * device->scroll.wheel_click_angle,
623 evdev_flush_pending_event(device, time);
627 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
628 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
629 e->value * device->scroll.wheel_click_angle,
638 evdev_process_absolute(struct evdev_device *device,
639 struct input_event *e,
643 evdev_process_touch(device, e, time);
645 evdev_process_absolute_motion(device, e);
650 evdev_any_button_down(struct evdev_device *device)
654 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
655 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
656 hw_is_key_down(device, button))
663 evdev_need_touch_frame(struct evdev_device *device)
665 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
668 switch (device->pending_event) {
670 case EVDEV_RELATIVE_MOTION:
672 case EVDEV_ABSOLUTE_MT_DOWN:
673 case EVDEV_ABSOLUTE_MT_MOTION:
674 case EVDEV_ABSOLUTE_MT_UP:
675 case EVDEV_ABSOLUTE_TOUCH_DOWN:
676 case EVDEV_ABSOLUTE_TOUCH_UP:
677 case EVDEV_ABSOLUTE_MOTION:
685 evdev_tag_external_mouse(struct evdev_device *device,
686 struct udev_device *udev_device)
690 bustype = libevdev_get_id_bustype(device->evdev);
691 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
692 if (device->seat_caps & EVDEV_DEVICE_POINTER)
693 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
698 evdev_tag_trackpoint(struct evdev_device *device,
699 struct udev_device *udev_device)
701 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
702 device->tags |= EVDEV_TAG_TRACKPOINT;
706 fallback_process(struct evdev_dispatch *dispatch,
707 struct evdev_device *device,
708 struct input_event *event,
711 bool need_frame = false;
713 switch (event->type) {
715 evdev_process_relative(device, event, time);
718 evdev_process_absolute(device, event, time);
721 evdev_process_key(device, event, time);
724 need_frame = evdev_need_touch_frame(device);
725 evdev_flush_pending_event(device, time);
727 touch_notify_frame(&device->base, time);
733 fallback_destroy(struct evdev_dispatch *dispatch)
739 fallback_tag_device(struct evdev_device *device,
740 struct udev_device *udev_device)
742 evdev_tag_external_mouse(device, udev_device);
743 evdev_tag_trackpoint(device, udev_device);
747 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
749 struct evdev_device *device = (struct evdev_device*)libinput_device;
751 return device->abs.absinfo_x && device->abs.absinfo_y;
754 static enum libinput_config_status
755 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
756 const float matrix[6])
758 struct evdev_device *device = (struct evdev_device*)libinput_device;
760 evdev_device_calibrate(device, matrix);
762 return LIBINPUT_CONFIG_STATUS_SUCCESS;
766 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
769 struct evdev_device *device = (struct evdev_device*)libinput_device;
771 matrix_to_farray6(&device->abs.usermatrix, matrix);
773 return !matrix_is_identity(&device->abs.usermatrix);
777 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
780 struct evdev_device *device = (struct evdev_device*)libinput_device;
782 matrix_to_farray6(&device->abs.default_calibration, matrix);
784 return !matrix_is_identity(&device->abs.default_calibration);
787 struct evdev_dispatch_interface fallback_interface = {
791 NULL, /* device_added */
792 NULL, /* device_removed */
793 NULL, /* device_suspended */
794 NULL, /* device_resumed */
799 evdev_sendevents_get_modes(struct libinput_device *device)
801 return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
804 static enum libinput_config_status
805 evdev_sendevents_set_mode(struct libinput_device *device,
806 enum libinput_config_send_events_mode mode)
808 struct evdev_device *evdev = (struct evdev_device*)device;
809 struct evdev_dispatch *dispatch = evdev->dispatch;
811 if (mode == dispatch->sendevents.current_mode)
812 return LIBINPUT_CONFIG_STATUS_SUCCESS;
815 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
816 evdev_device_resume(evdev);
818 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
819 evdev_device_suspend(evdev);
821 default: /* no support for combined modes yet */
822 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
825 dispatch->sendevents.current_mode = mode;
827 return LIBINPUT_CONFIG_STATUS_SUCCESS;
830 static enum libinput_config_send_events_mode
831 evdev_sendevents_get_mode(struct libinput_device *device)
833 struct evdev_device *evdev = (struct evdev_device*)device;
834 struct evdev_dispatch *dispatch = evdev->dispatch;
836 return dispatch->sendevents.current_mode;
839 static enum libinput_config_send_events_mode
840 evdev_sendevents_get_default_mode(struct libinput_device *device)
842 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
846 evdev_left_handed_has(struct libinput_device *device)
848 /* This is only hooked up when we have left-handed configuration, so we
849 * can hardcode 1 here */
854 evdev_change_to_left_handed(struct evdev_device *device)
856 if (device->left_handed.want_enabled == device->left_handed.enabled)
859 if (evdev_any_button_down(device))
862 device->left_handed.enabled = device->left_handed.want_enabled;
865 static enum libinput_config_status
866 evdev_left_handed_set(struct libinput_device *device, int left_handed)
868 struct evdev_device *evdev_device = (struct evdev_device *)device;
870 evdev_device->left_handed.want_enabled = left_handed ? true : false;
872 evdev_device->left_handed.change_to_enabled(evdev_device);
874 return LIBINPUT_CONFIG_STATUS_SUCCESS;
878 evdev_left_handed_get(struct libinput_device *device)
880 struct evdev_device *evdev_device = (struct evdev_device *)device;
882 /* return the wanted configuration, even if it hasn't taken
884 return evdev_device->left_handed.want_enabled;
888 evdev_left_handed_get_default(struct libinput_device *device)
894 evdev_init_left_handed(struct evdev_device *device,
895 void (*change_to_left_handed)(struct evdev_device *))
897 device->left_handed.config.has = evdev_left_handed_has;
898 device->left_handed.config.set = evdev_left_handed_set;
899 device->left_handed.config.get = evdev_left_handed_get;
900 device->left_handed.config.get_default = evdev_left_handed_get_default;
901 device->base.config.left_handed = &device->left_handed.config;
902 device->left_handed.enabled = false;
903 device->left_handed.want_enabled = false;
904 device->left_handed.change_to_enabled = change_to_left_handed;
910 evdev_scroll_get_methods(struct libinput_device *device)
912 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
916 evdev_change_scroll_method(struct evdev_device *device)
918 if (device->scroll.want_method == device->scroll.method &&
919 device->scroll.want_button == device->scroll.button)
922 if (evdev_any_button_down(device))
925 device->scroll.method = device->scroll.want_method;
926 device->scroll.button = device->scroll.want_button;
929 static enum libinput_config_status
930 evdev_scroll_set_method(struct libinput_device *device,
931 enum libinput_config_scroll_method method)
933 struct evdev_device *evdev = (struct evdev_device*)device;
935 evdev->scroll.want_method = method;
936 evdev->scroll.change_scroll_method(evdev);
938 return LIBINPUT_CONFIG_STATUS_SUCCESS;
941 static enum libinput_config_scroll_method
942 evdev_scroll_get_method(struct libinput_device *device)
944 struct evdev_device *evdev = (struct evdev_device *)device;
946 /* return the wanted configuration, even if it hasn't taken
948 return evdev->scroll.want_method;
951 static enum libinput_config_scroll_method
952 evdev_scroll_get_default_method(struct libinput_device *device)
954 struct evdev_device *evdev = (struct evdev_device *)device;
956 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
957 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
959 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
962 static enum libinput_config_status
963 evdev_scroll_set_button(struct libinput_device *device,
966 struct evdev_device *evdev = (struct evdev_device*)device;
968 evdev->scroll.want_button = button;
969 evdev->scroll.change_scroll_method(evdev);
971 return LIBINPUT_CONFIG_STATUS_SUCCESS;
975 evdev_scroll_get_button(struct libinput_device *device)
977 struct evdev_device *evdev = (struct evdev_device *)device;
979 /* return the wanted configuration, even if it hasn't taken
981 return evdev->scroll.want_button;
985 evdev_scroll_get_default_button(struct libinput_device *device)
987 struct evdev_device *evdev = (struct evdev_device *)device;
989 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
996 evdev_init_button_scroll(struct evdev_device *device,
997 void (*change_scroll_method)(struct evdev_device *))
999 libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
1000 evdev_button_scroll_timeout, device);
1001 device->scroll.config.get_methods = evdev_scroll_get_methods;
1002 device->scroll.config.set_method = evdev_scroll_set_method;
1003 device->scroll.config.get_method = evdev_scroll_get_method;
1004 device->scroll.config.get_default_method = evdev_scroll_get_default_method;
1005 device->scroll.config.set_button = evdev_scroll_set_button;
1006 device->scroll.config.get_button = evdev_scroll_get_button;
1007 device->scroll.config.get_default_button = evdev_scroll_get_default_button;
1008 device->base.config.scroll_method = &device->scroll.config;
1009 device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
1010 device->scroll.want_method = device->scroll.method;
1011 device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
1012 device->scroll.want_button = device->scroll.button;
1013 device->scroll.change_scroll_method = change_scroll_method;
1019 evdev_init_calibration(struct evdev_device *device,
1020 struct evdev_dispatch *dispatch)
1022 device->base.config.calibration = &dispatch->calibration;
1024 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
1025 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
1026 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
1027 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
1031 evdev_init_sendevents(struct evdev_device *device,
1032 struct evdev_dispatch *dispatch)
1034 device->base.config.sendevents = &dispatch->sendevents.config;
1036 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1037 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
1038 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
1039 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
1040 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
1044 evdev_scroll_config_natural_has(struct libinput_device *device)
1049 static enum libinput_config_status
1050 evdev_scroll_config_natural_set(struct libinput_device *device,
1053 struct evdev_device *dev = (struct evdev_device *)device;
1055 dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1057 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1061 evdev_scroll_config_natural_get(struct libinput_device *device)
1063 struct evdev_device *dev = (struct evdev_device *)device;
1065 return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1069 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1071 /* could enable this on Apple touchpads. could do that, could
1072 * very well do that... */
1077 evdev_init_natural_scroll(struct evdev_device *device)
1079 device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1080 device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1081 device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1082 device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1083 device->scroll.natural_scrolling_enabled = false;
1084 device->base.config.natural_scroll = &device->scroll.config_natural;
1087 static struct evdev_dispatch *
1088 fallback_dispatch_create(struct libinput_device *device)
1090 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1091 struct evdev_device *evdev_device = (struct evdev_device *)device;
1093 if (dispatch == NULL)
1096 dispatch->interface = &fallback_interface;
1098 if (evdev_device->left_handed.want_enabled &&
1099 evdev_init_left_handed(evdev_device,
1100 evdev_change_to_left_handed) == -1) {
1105 if (evdev_device->scroll.want_button &&
1106 evdev_init_button_scroll(evdev_device,
1107 evdev_change_scroll_method) == -1) {
1112 if (evdev_device->scroll.natural_scrolling_enabled)
1113 evdev_init_natural_scroll(evdev_device);
1115 evdev_init_calibration(evdev_device, dispatch);
1116 evdev_init_sendevents(evdev_device, dispatch);
1122 evdev_process_event(struct evdev_device *device, struct input_event *e)
1124 struct evdev_dispatch *dispatch = device->dispatch;
1125 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1127 dispatch->interface->process(dispatch, device, e, time);
1131 evdev_device_dispatch_one(struct evdev_device *device,
1132 struct input_event *ev)
1134 if (!device->mtdev) {
1135 evdev_process_event(device, ev);
1137 mtdev_put_event(device->mtdev, ev);
1138 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1139 while (!mtdev_empty(device->mtdev)) {
1140 struct input_event e;
1141 mtdev_get_event(device->mtdev, &e);
1142 evdev_process_event(device, &e);
1149 evdev_sync_device(struct evdev_device *device)
1151 struct input_event ev;
1155 rc = libevdev_next_event(device->evdev,
1156 LIBEVDEV_READ_FLAG_SYNC, &ev);
1159 evdev_device_dispatch_one(device, &ev);
1160 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1162 return rc == -EAGAIN ? 0 : rc;
1166 evdev_device_dispatch(void *data)
1168 struct evdev_device *device = data;
1169 struct libinput *libinput = device->base.seat->libinput;
1170 struct input_event ev;
1173 /* If the compositor is repainting, this function is called only once
1174 * per frame and we have to process all the events available on the
1175 * fd, otherwise there will be input lag. */
1177 rc = libevdev_next_event(device->evdev,
1178 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1179 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1180 switch (ratelimit_test(&device->syn_drop_limit)) {
1181 case RATELIMIT_PASS:
1182 log_info(libinput, "SYN_DROPPED event from "
1183 "\"%s\" - some input events have "
1184 "been lost.\n", device->devname);
1186 case RATELIMIT_THRESHOLD:
1187 log_info(libinput, "SYN_DROPPED flood "
1191 case RATELIMIT_EXCEEDED:
1195 /* send one more sync event so we handle all
1196 currently pending events before we sync up
1197 to the current state */
1198 ev.code = SYN_REPORT;
1199 evdev_device_dispatch_one(device, &ev);
1201 rc = evdev_sync_device(device);
1203 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1204 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1205 evdev_device_dispatch_one(device, &ev);
1207 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1209 if (rc != -EAGAIN && rc != -EINTR) {
1210 libinput_remove_source(libinput, device->source);
1211 device->source = NULL;
1216 evdev_accel_config_available(struct libinput_device *device)
1218 /* this function is only called if we set up ptraccel, so we can
1219 reply with a resounding "Yes" */
1223 static enum libinput_config_status
1224 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1226 struct evdev_device *dev = (struct evdev_device *)device;
1228 if (!filter_set_speed(dev->pointer.filter, speed))
1229 return LIBINPUT_CONFIG_STATUS_INVALID;
1231 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1235 evdev_accel_config_get_speed(struct libinput_device *device)
1237 struct evdev_device *dev = (struct evdev_device *)device;
1239 return filter_get_speed(dev->pointer.filter);
1243 evdev_accel_config_get_default_speed(struct libinput_device *device)
1249 evdev_device_init_pointer_acceleration(struct evdev_device *device)
1251 device->pointer.filter =
1252 create_pointer_accelerator_filter(
1253 pointer_accel_profile_linear);
1254 if (!device->pointer.filter)
1257 device->pointer.config.available = evdev_accel_config_available;
1258 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1259 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1260 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1261 device->base.config.accel = &device->pointer.config;
1263 evdev_accel_config_set_speed(&device->base,
1264 evdev_accel_config_get_default_speed(&device->base));
1270 evdev_need_mtdev(struct evdev_device *device)
1272 struct libevdev *evdev = device->evdev;
1274 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1275 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1276 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1280 evdev_tag_device(struct evdev_device *device)
1282 if (device->dispatch->interface->tag_device)
1283 device->dispatch->interface->tag_device(device,
1284 device->udev_device);
1288 evdev_read_wheel_click_prop(struct evdev_device *device)
1290 struct libinput *libinput = device->base.seat->libinput;
1292 int angle = DEFAULT_WHEEL_CLICK_ANGLE;
1294 prop = udev_device_get_property_value(device->udev_device,
1295 "MOUSE_WHEEL_CLICK_ANGLE");
1297 angle = parse_mouse_wheel_click_angle_property(prop);
1300 "Mouse wheel click angle '%s' is present but invalid,"
1301 "using %d degrees instead\n",
1303 DEFAULT_WHEEL_CLICK_ANGLE);
1304 angle = DEFAULT_WHEEL_CLICK_ANGLE;
1311 evdev_read_dpi_prop(struct evdev_device *device)
1313 struct libinput *libinput = device->base.seat->libinput;
1314 const char *mouse_dpi;
1315 int dpi = DEFAULT_MOUSE_DPI;
1317 mouse_dpi = udev_device_get_property_value(device->udev_device,
1320 dpi = parse_mouse_dpi_property(mouse_dpi);
1322 log_error(libinput, "Mouse DPI property for '%s' is "
1323 "present but invalid, using %d "
1327 dpi = DEFAULT_MOUSE_DPI;
1335 evdev_fix_abs_resolution(struct libevdev *evdev,
1337 const struct input_absinfo *absinfo)
1339 struct input_absinfo fixed;
1341 if (absinfo->resolution == 0) {
1343 fixed.resolution = 1;
1344 /* libevdev_set_abs_info() changes the absinfo we already
1345 have a pointer to, no need to fetch it again */
1346 libevdev_set_abs_info(evdev, code, &fixed);
1353 static enum evdev_device_udev_tags
1354 evdev_device_get_udev_tags(struct evdev_device *device,
1355 struct udev_device *udev_device)
1358 enum evdev_device_udev_tags tags = 0;
1359 const struct evdev_udev_tag_match *match = evdev_udev_tag_matches;
1361 while (match->name) {
1362 prop = udev_device_get_property_value(device->udev_device,
1373 evdev_configure_device(struct evdev_device *device)
1375 struct libinput *libinput = device->base.seat->libinput;
1376 struct libevdev *evdev = device->evdev;
1377 const struct input_absinfo *absinfo;
1378 int has_abs, has_rel, has_mt;
1379 int has_button, has_keyboard, has_touch, has_joystick_button;
1380 struct mt_slot *slots;
1385 const char *devnode = udev_device_get_devnode(device->udev_device);
1386 enum evdev_device_udev_tags udev_tags;
1388 udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1394 has_joystick_button = 0;
1400 "input device '%s', %s is tagged by udev as:%s%s%s%s%s%s\n",
1401 device->devname, devnode,
1402 udev_tags & EVDEV_UDEV_TAG_KEYBOARD ? " Keyboard" : "",
1403 udev_tags & EVDEV_UDEV_TAG_MOUSE ? " Mouse" : "",
1404 udev_tags & EVDEV_UDEV_TAG_TOUCHPAD ? " Touchpad" : "",
1405 udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN ? " Touchscreen" : "",
1406 udev_tags & EVDEV_UDEV_TAG_TABLET ? " Tablet" : "",
1407 udev_tags & EVDEV_UDEV_TAG_JOYSTICK ? " Joystick" : "",
1408 udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER ? " Accelerometer" : "");
1410 for (i = BTN_JOYSTICK; i <= BTN_PINKIE; i++)
1411 if (libevdev_has_event_code(evdev, EV_KEY, i))
1412 has_joystick_button = 1;
1414 for (i = BTN_GAMEPAD; i <= BTN_TR2; i++)
1415 if (libevdev_has_event_code(evdev, EV_KEY, i))
1416 has_joystick_button = 1;
1418 if (has_joystick_button) {
1420 "input device '%s', %s is a joystick, ignoring\n",
1421 device->devname, devnode);
1425 if (libevdev_has_event_type(evdev, EV_ABS)) {
1427 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1428 if (evdev_fix_abs_resolution(evdev,
1431 device->abs.fake_resolution = 1;
1432 device->abs.absinfo_x = absinfo;
1435 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1436 if (evdev_fix_abs_resolution(evdev,
1439 device->abs.fake_resolution = 1;
1440 device->abs.absinfo_y = absinfo;
1444 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1445 the limited ABS_* range - they aren't MT devices, they
1446 just have too many ABS_ axes */
1447 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1448 libevdev_get_num_slots(evdev) == -1) {
1451 } else if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1452 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1453 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1454 if (evdev_fix_abs_resolution(evdev,
1457 device->abs.fake_resolution = 1;
1458 device->abs.absinfo_x = absinfo;
1460 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1461 if (evdev_fix_abs_resolution(evdev,
1464 device->abs.fake_resolution = 1;
1465 device->abs.absinfo_y = absinfo;
1470 /* We only handle the slotted Protocol B in libinput.
1471 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1472 require mtdev for conversion. */
1473 if (evdev_need_mtdev(device)) {
1474 device->mtdev = mtdev_new_open(device->fd);
1478 num_slots = device->mtdev->caps.slot.maximum;
1479 if (device->mtdev->caps.slot.minimum < 0 ||
1482 active_slot = device->mtdev->caps.slot.value;
1484 num_slots = libevdev_get_num_slots(device->evdev);
1485 active_slot = libevdev_get_current_slot(evdev);
1488 slots = calloc(num_slots, sizeof(struct mt_slot));
1492 for (slot = 0; slot < num_slots; ++slot) {
1493 slots[slot].seat_slot = -1;
1497 device->mt.slots = slots;
1498 device->mt.slots_len = num_slots;
1499 device->mt.slot = active_slot;
1503 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1504 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1507 if (libevdev_has_event_type(evdev, EV_KEY)) {
1508 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1509 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1510 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1511 (has_abs || has_mt)) {
1512 device->dispatch = evdev_mt_touchpad_create(device);
1514 "input device '%s', %s is a touchpad\n",
1515 device->devname, devnode);
1516 return device->dispatch == NULL ? -1 : 0;
1519 for (i = 0; i < KEY_MAX; i++) {
1520 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1521 switch (get_key_type(i)) {
1522 case EVDEV_KEY_TYPE_NONE:
1524 case EVDEV_KEY_TYPE_KEY:
1527 case EVDEV_KEY_TYPE_BUTTON:
1534 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1537 if (libevdev_has_event_type(evdev, EV_LED))
1540 if ((has_abs || has_rel) && has_button) {
1542 evdev_device_init_pointer_acceleration(device) == -1)
1545 device->seat_caps |= EVDEV_DEVICE_POINTER;
1548 "input device '%s', %s is a pointer caps =%s%s%s\n",
1549 device->devname, devnode,
1550 has_abs ? " absolute-motion" : "",
1551 has_rel ? " relative-motion": "",
1552 has_button ? " button" : "");
1554 /* want left-handed config option */
1555 device->left_handed.want_enabled = true;
1556 /* want natural-scroll config option */
1557 device->scroll.natural_scrolling_enabled = true;
1560 if (has_rel && has_button) {
1561 /* want button scrolling config option */
1562 device->scroll.want_button = 1;
1566 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1568 "input device '%s', %s is a keyboard\n",
1569 device->devname, devnode);
1571 if (has_touch && !has_button) {
1572 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1574 "input device '%s', %s is a touch device\n",
1575 device->devname, devnode);
1582 evdev_notify_added_device(struct evdev_device *device)
1584 struct libinput_device *dev;
1586 list_for_each(dev, &device->base.seat->devices_list, link) {
1587 struct evdev_device *d = (struct evdev_device*)dev;
1588 if (dev == &device->base)
1591 /* Notify existing device d about addition of device device */
1592 if (d->dispatch->interface->device_added)
1593 d->dispatch->interface->device_added(d, device);
1595 /* Notify new device device about existing device d */
1596 if (device->dispatch->interface->device_added)
1597 device->dispatch->interface->device_added(device, d);
1599 /* Notify new device device if existing device d is suspended */
1600 if (d->suspended && device->dispatch->interface->device_suspended)
1601 device->dispatch->interface->device_suspended(device, d);
1604 notify_added_device(&device->base);
1608 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1610 struct udev *udev = udev_device_get_udev(udev_device);
1611 struct udev_device *udev_device_new = NULL;
1615 if (fstat(fd, &st) < 0)
1618 udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1619 if (!udev_device_new)
1622 rc = strcmp(udev_device_get_syspath(udev_device_new),
1623 udev_device_get_syspath(udev_device));
1625 if (udev_device_new)
1626 udev_device_unref(udev_device_new);
1630 struct evdev_device *
1631 evdev_device_create(struct libinput_seat *seat,
1632 struct udev_device *udev_device)
1634 struct libinput *libinput = seat->libinput;
1635 struct evdev_device *device = NULL;
1638 int unhandled_device = 0;
1639 const char *devnode = udev_device_get_devnode(udev_device);
1640 struct libinput_device_group *group;
1642 /* Use non-blocking mode so that we can loop on read on
1643 * evdev_device_data() until all events on the fd are
1644 * read. mtdev_get() also expects this. */
1645 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1648 "opening input device '%s' failed (%s).\n",
1649 devnode, strerror(-fd));
1653 if (evdev_device_compare_syspath(udev_device, fd) != 0)
1656 device = zalloc(sizeof *device);
1660 libinput_device_init(&device->base, seat);
1661 libinput_seat_ref(seat);
1663 rc = libevdev_new_from_fd(fd, &device->evdev);
1667 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1669 device->seat_caps = 0;
1671 device->mtdev = NULL;
1672 device->udev_device = udev_device_ref(udev_device);
1675 device->abs.seat_slot = -1;
1676 device->dispatch = NULL;
1678 device->pending_event = EVDEV_NONE;
1679 device->devname = libevdev_get_name(device->evdev);
1680 device->scroll.threshold = 5.0; /* Default may be overridden */
1681 device->scroll.direction = 0;
1682 device->scroll.wheel_click_angle =
1683 evdev_read_wheel_click_prop(device);
1684 device->dpi = evdev_read_dpi_prop(device);
1685 /* at most 5 SYN_DROPPED log-messages per 30s */
1686 ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1688 matrix_init_identity(&device->abs.calibration);
1689 matrix_init_identity(&device->abs.usermatrix);
1690 matrix_init_identity(&device->abs.default_calibration);
1692 if (evdev_configure_device(device) == -1)
1695 if (device->seat_caps == 0) {
1696 unhandled_device = 1;
1700 /* If the dispatch was not set up use the fallback. */
1701 if (device->dispatch == NULL)
1702 device->dispatch = fallback_dispatch_create(&device->base);
1703 if (device->dispatch == NULL)
1707 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1708 if (!device->source)
1711 group = libinput_device_group_create();
1714 libinput_device_set_device_group(&device->base, group);
1715 libinput_device_group_unref(group);
1717 list_insert(seat->devices_list.prev, &device->base.link);
1719 evdev_tag_device(device);
1720 evdev_notify_added_device(device);
1726 close_restricted(libinput, fd);
1728 evdev_device_destroy(device);
1730 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1734 evdev_device_get_output(struct evdev_device *device)
1736 return device->output_name;
1740 evdev_device_get_sysname(struct evdev_device *device)
1742 return udev_device_get_sysname(device->udev_device);
1746 evdev_device_get_name(struct evdev_device *device)
1748 return device->devname;
1752 evdev_device_get_id_product(struct evdev_device *device)
1754 return libevdev_get_id_product(device->evdev);
1758 evdev_device_get_id_vendor(struct evdev_device *device)
1760 return libevdev_get_id_vendor(device->evdev);
1763 struct udev_device *
1764 evdev_device_get_udev_device(struct evdev_device *device)
1766 return udev_device_ref(device->udev_device);
1770 evdev_device_set_default_calibration(struct evdev_device *device,
1771 const float calibration[6])
1773 matrix_from_farray6(&device->abs.default_calibration, calibration);
1774 evdev_device_calibrate(device, calibration);
1778 evdev_device_calibrate(struct evdev_device *device,
1779 const float calibration[6])
1781 struct matrix scale,
1786 matrix_from_farray6(&transform, calibration);
1787 device->abs.apply_calibration = !matrix_is_identity(&transform);
1789 if (!device->abs.apply_calibration) {
1790 matrix_init_identity(&device->abs.calibration);
1794 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1795 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1797 /* The transformation matrix is in the form:
1801 * Where a, e are the scale components, a, b, d, e are the rotation
1802 * component (combined with scale) and c and f are the translation
1803 * component. The translation component in the input matrix must be
1804 * normalized to multiples of the device width and height,
1805 * respectively. e.g. c == 1 shifts one device-width to the right.
1807 * We pre-calculate a single matrix to apply to event coordinates:
1808 * M = Un-Normalize * Calibration * Normalize
1810 * Normalize: scales the device coordinates to [0,1]
1811 * Calibration: user-supplied matrix
1812 * Un-Normalize: scales back up to device coordinates
1813 * Matrix maths requires the normalize/un-normalize in reverse
1817 /* back up the user matrix so we can return it on request */
1818 matrix_from_farray6(&device->abs.usermatrix, calibration);
1821 matrix_init_translate(&translate,
1822 device->abs.absinfo_x->minimum,
1823 device->abs.absinfo_y->minimum);
1824 matrix_init_scale(&scale, sx, sy);
1825 matrix_mult(&scale, &translate, &scale);
1828 matrix_mult(&transform, &scale, &transform);
1831 matrix_init_translate(&translate,
1832 -device->abs.absinfo_x->minimum/sx,
1833 -device->abs.absinfo_y->minimum/sy);
1834 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1835 matrix_mult(&scale, &translate, &scale);
1837 /* store final matrix in device */
1838 matrix_mult(&device->abs.calibration, &transform, &scale);
1842 evdev_device_has_capability(struct evdev_device *device,
1843 enum libinput_device_capability capability)
1845 switch (capability) {
1846 case LIBINPUT_DEVICE_CAP_POINTER:
1847 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1848 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1849 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1850 case LIBINPUT_DEVICE_CAP_TOUCH:
1851 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1858 evdev_device_get_size(struct evdev_device *device,
1862 const struct input_absinfo *x, *y;
1864 x = libevdev_get_abs_info(device->evdev, ABS_X);
1865 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1867 if (!x || !y || device->abs.fake_resolution ||
1868 !x->resolution || !y->resolution)
1871 *width = evdev_convert_to_mm(x, x->maximum);
1872 *height = evdev_convert_to_mm(y, y->maximum);
1878 evdev_device_has_button(struct evdev_device *device, uint32_t code)
1880 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
1883 return libevdev_has_event_code(device->evdev, EV_KEY, code);
1887 evdev_is_scrolling(const struct evdev_device *device,
1888 enum libinput_pointer_axis axis)
1890 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1891 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1893 return (device->scroll.direction & AS_MASK(axis)) != 0;
1897 evdev_start_scrolling(struct evdev_device *device,
1898 enum libinput_pointer_axis axis)
1900 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1901 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1903 device->scroll.direction |= AS_MASK(axis);
1907 evdev_post_scroll(struct evdev_device *device,
1909 enum libinput_pointer_axis_source source,
1913 double trigger_horiz, trigger_vert;
1915 if (!evdev_is_scrolling(device,
1916 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1917 device->scroll.buildup_vertical += dy;
1918 if (!evdev_is_scrolling(device,
1919 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1920 device->scroll.buildup_horizontal += dx;
1922 trigger_vert = device->scroll.buildup_vertical;
1923 trigger_horiz = device->scroll.buildup_horizontal;
1925 /* If we're not scrolling yet, use a distance trigger: moving
1926 past a certain distance starts scrolling */
1927 if (!evdev_is_scrolling(device,
1928 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
1929 !evdev_is_scrolling(device,
1930 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1931 if (fabs(trigger_vert) >= device->scroll.threshold)
1932 evdev_start_scrolling(device,
1933 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1934 if (fabs(trigger_horiz) >= device->scroll.threshold)
1935 evdev_start_scrolling(device,
1936 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1937 /* We're already scrolling in one direction. Require some
1938 trigger speed to start scrolling in the other direction */
1939 } else if (!evdev_is_scrolling(device,
1940 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1941 if (fabs(dy) >= device->scroll.threshold)
1942 evdev_start_scrolling(device,
1943 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1944 } else if (!evdev_is_scrolling(device,
1945 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1946 if (fabs(dx) >= device->scroll.threshold)
1947 evdev_start_scrolling(device,
1948 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1951 /* We use the trigger to enable, but the delta from this event for
1952 * the actual scroll movement. Otherwise we get a jump once
1953 * scrolling engages */
1954 if (!evdev_is_scrolling(device,
1955 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1957 if (!evdev_is_scrolling(device,
1958 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1961 if (dx != 0.0 || dy != 0.0)
1962 evdev_notify_axis(device,
1964 device->scroll.direction,
1971 evdev_stop_scroll(struct evdev_device *device,
1973 enum libinput_pointer_axis_source source)
1975 /* terminate scrolling with a zero scroll event */
1976 if (device->scroll.direction != 0)
1977 pointer_notify_axis(&device->base,
1979 device->scroll.direction,
1984 device->scroll.buildup_horizontal = 0;
1985 device->scroll.buildup_vertical = 0;
1986 device->scroll.direction = 0;
1990 release_pressed_keys(struct evdev_device *device)
1992 struct libinput *libinput = device->base.seat->libinput;
1996 if ((time = libinput_now(libinput)) == 0)
1999 for (code = 0; code < KEY_CNT; code++) {
2000 int count = get_key_down_count(device, code);
2003 log_bug_libinput(libinput,
2004 "Key %d is down %d times.\n",
2009 while (get_key_down_count(device, code) > 0) {
2010 switch (get_key_type(code)) {
2011 case EVDEV_KEY_TYPE_NONE:
2013 case EVDEV_KEY_TYPE_KEY:
2014 evdev_keyboard_notify_key(
2018 LIBINPUT_KEY_STATE_RELEASED);
2020 case EVDEV_KEY_TYPE_BUTTON:
2021 evdev_pointer_notify_button(
2024 evdev_to_left_handed(device, code),
2025 LIBINPUT_BUTTON_STATE_RELEASED);
2033 evdev_notify_suspended_device(struct evdev_device *device)
2035 struct libinput_device *it;
2037 if (device->suspended)
2040 list_for_each(it, &device->base.seat->devices_list, link) {
2041 struct evdev_device *d = (struct evdev_device*)it;
2042 if (it == &device->base)
2045 if (d->dispatch->interface->device_suspended)
2046 d->dispatch->interface->device_suspended(d, device);
2049 device->suspended = 1;
2053 evdev_notify_resumed_device(struct evdev_device *device)
2055 struct libinput_device *it;
2057 if (!device->suspended)
2060 list_for_each(it, &device->base.seat->devices_list, link) {
2061 struct evdev_device *d = (struct evdev_device*)it;
2062 if (it == &device->base)
2065 if (d->dispatch->interface->device_resumed)
2066 d->dispatch->interface->device_resumed(d, device);
2069 device->suspended = 0;
2073 evdev_device_suspend(struct evdev_device *device)
2075 evdev_notify_suspended_device(device);
2077 if (device->source) {
2078 libinput_remove_source(device->base.seat->libinput,
2080 device->source = NULL;
2083 release_pressed_keys(device);
2085 if (device->mtdev) {
2086 mtdev_close_delete(device->mtdev);
2087 device->mtdev = NULL;
2090 if (device->fd != -1) {
2091 close_restricted(device->base.seat->libinput, device->fd);
2099 evdev_device_resume(struct evdev_device *device)
2101 struct libinput *libinput = device->base.seat->libinput;
2103 const char *devnode;
2104 struct input_event ev;
2105 enum libevdev_read_status status;
2107 if (device->fd != -1)
2110 if (device->was_removed)
2113 devnode = udev_device_get_devnode(device->udev_device);
2114 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
2119 if (evdev_device_compare_syspath(device->udev_device, fd)) {
2120 close_restricted(libinput, fd);
2126 if (evdev_need_mtdev(device)) {
2127 device->mtdev = mtdev_new_open(device->fd);
2132 libevdev_change_fd(device->evdev, fd);
2133 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
2135 /* re-sync libevdev's view of the device, but discard the actual
2136 events. Our device is in a neutral state already */
2137 libevdev_next_event(device->evdev,
2138 LIBEVDEV_READ_FLAG_FORCE_SYNC,
2141 status = libevdev_next_event(device->evdev,
2142 LIBEVDEV_READ_FLAG_SYNC,
2144 } while (status == LIBEVDEV_READ_STATUS_SYNC);
2147 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2148 if (!device->source) {
2149 mtdev_close_delete(device->mtdev);
2153 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2155 evdev_notify_resumed_device(device);
2161 evdev_device_remove(struct evdev_device *device)
2163 struct libinput_device *dev;
2165 list_for_each(dev, &device->base.seat->devices_list, link) {
2166 struct evdev_device *d = (struct evdev_device*)dev;
2167 if (dev == &device->base)
2170 if (d->dispatch->interface->device_removed)
2171 d->dispatch->interface->device_removed(d, device);
2174 evdev_device_suspend(device);
2176 if (device->dispatch->interface->remove)
2177 device->dispatch->interface->remove(device->dispatch);
2179 /* A device may be removed while suspended, mark it to
2180 * skip re-opening a different device with the same node */
2181 device->was_removed = true;
2183 list_remove(&device->base.link);
2185 notify_removed_device(&device->base);
2186 libinput_device_unref(&device->base);
2190 evdev_device_destroy(struct evdev_device *device)
2192 struct evdev_dispatch *dispatch;
2194 dispatch = device->dispatch;
2196 dispatch->interface->destroy(dispatch);
2198 if (device->base.group)
2199 libinput_device_group_unref(device->base.group);
2201 filter_destroy(device->pointer.filter);
2202 libinput_seat_unref(device->base.seat);
2203 libevdev_free(device->evdev);
2204 udev_device_unref(device->udev_device);
2205 free(device->mt.slots);