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_button(struct evdev_device *device,
144 enum libinput_button_state state)
148 down_count = update_key_down_count(device, button, state);
150 if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
151 (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
152 pointer_notify_button(&device->base, time, button, state);
154 if (state == LIBINPUT_BUTTON_STATE_RELEASED) {
155 if (device->left_handed.change_to_enabled)
156 device->left_handed.change_to_enabled(device);
158 if (device->scroll.change_scroll_method)
159 device->scroll.change_scroll_method(device);
166 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
168 static const struct {
169 enum libinput_led weston;
172 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
173 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
174 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
176 struct input_event ev[ARRAY_LENGTH(map) + 1];
179 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
182 memset(ev, 0, sizeof(ev));
183 for (i = 0; i < ARRAY_LENGTH(map); i++) {
185 ev[i].code = map[i].evdev;
186 ev[i].value = !!(leds & map[i].weston);
189 ev[i].code = SYN_REPORT;
191 i = write(device->fd, ev, sizeof ev);
192 (void)i; /* no, we really don't care about the return value */
196 transform_absolute(struct evdev_device *device,
197 struct device_coords *point)
199 if (!device->abs.apply_calibration)
202 matrix_mult_vec(&device->abs.calibration, &point->x, &point->y);
206 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
208 return (val - absinfo->minimum) * to_range /
209 (absinfo->maximum - absinfo->minimum + 1);
213 evdev_device_transform_x(struct evdev_device *device,
217 return scale_axis(device->abs.absinfo_x, x, width);
221 evdev_device_transform_y(struct evdev_device *device,
225 return scale_axis(device->abs.absinfo_y, y, height);
229 normalize_delta(struct evdev_device *device,
230 const struct device_coords *delta,
231 struct normalized_coords *normalized)
233 normalized->x = delta->x * DEFAULT_MOUSE_DPI / (double)device->dpi;
234 normalized->y = delta->y * DEFAULT_MOUSE_DPI / (double)device->dpi;
238 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
240 struct libinput *libinput = device->base.seat->libinput;
243 struct libinput_device *base = &device->base;
244 struct libinput_seat *seat = base->seat;
245 struct normalized_coords accel, unaccel;
246 struct device_coords point;
248 slot = device->mt.slot;
250 switch (device->pending_event) {
253 case EVDEV_RELATIVE_MOTION:
254 normalize_delta(device, &device->rel, &unaccel);
258 /* Use unaccelerated deltas for pointing stick scroll */
259 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
260 hw_is_key_down(device, device->scroll.button)) {
261 if (device->scroll.button_scroll_active)
262 evdev_post_scroll(device, time,
263 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
268 /* Apply pointer acceleration. */
269 accel = filter_dispatch(device->pointer.filter, &unaccel, device, time);
271 if (normalized_is_zero(accel) && normalized_is_zero(unaccel))
274 pointer_notify_motion(base, time, &accel, &unaccel);
276 case EVDEV_ABSOLUTE_MT_DOWN:
277 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
280 if (device->mt.slots[slot].seat_slot != -1) {
281 log_bug_kernel(libinput,
282 "%s: Driver sent multiple touch down for the "
284 udev_device_get_devnode(device->udev_device));
288 seat_slot = ffs(~seat->slot_map) - 1;
289 device->mt.slots[slot].seat_slot = seat_slot;
294 seat->slot_map |= 1 << seat_slot;
295 point = device->mt.slots[slot].point;
296 transform_absolute(device, &point);
298 touch_notify_touch_down(base, time, slot, seat_slot,
301 case EVDEV_ABSOLUTE_MT_MOTION:
302 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
305 seat_slot = device->mt.slots[slot].seat_slot;
306 point = device->mt.slots[slot].point;
311 transform_absolute(device, &point);
312 touch_notify_touch_motion(base, time, slot, seat_slot,
315 case EVDEV_ABSOLUTE_MT_UP:
316 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
319 seat_slot = device->mt.slots[slot].seat_slot;
320 device->mt.slots[slot].seat_slot = -1;
325 seat->slot_map &= ~(1 << seat_slot);
327 touch_notify_touch_up(base, time, slot, seat_slot);
329 case EVDEV_ABSOLUTE_TOUCH_DOWN:
330 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
333 if (device->abs.seat_slot != -1) {
334 log_bug_kernel(libinput,
335 "%s: Driver sent multiple touch down for the "
337 udev_device_get_devnode(device->udev_device));
341 seat_slot = ffs(~seat->slot_map) - 1;
342 device->abs.seat_slot = seat_slot;
347 seat->slot_map |= 1 << seat_slot;
349 point = device->abs.point;
350 transform_absolute(device, &point);
352 touch_notify_touch_down(base, time, -1, seat_slot, &point);
354 case EVDEV_ABSOLUTE_MOTION:
355 point = device->abs.point;
356 transform_absolute(device, &point);
358 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
359 seat_slot = device->abs.seat_slot;
364 touch_notify_touch_motion(base, time, -1, seat_slot,
366 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
367 pointer_notify_motion_absolute(base, time, &point);
370 case EVDEV_ABSOLUTE_TOUCH_UP:
371 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
374 seat_slot = device->abs.seat_slot;
375 device->abs.seat_slot = -1;
380 seat->slot_map &= ~(1 << seat_slot);
382 touch_notify_touch_up(base, time, -1, seat_slot);
385 assert(0 && "Unknown pending event type");
389 device->pending_event = EVDEV_NONE;
392 static enum evdev_key_type
393 get_key_type(uint16_t code)
395 if (code == BTN_TOUCH)
396 return EVDEV_KEY_TYPE_NONE;
398 if (code >= KEY_ESC && code <= KEY_MICMUTE)
399 return EVDEV_KEY_TYPE_KEY;
400 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
401 return EVDEV_KEY_TYPE_BUTTON;
402 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
403 return EVDEV_KEY_TYPE_KEY;
404 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
405 return EVDEV_KEY_TYPE_BUTTON;
406 return EVDEV_KEY_TYPE_NONE;
410 evdev_button_scroll_timeout(uint64_t time, void *data)
412 struct evdev_device *device = data;
414 device->scroll.button_scroll_active = true;
418 evdev_button_scroll_button(struct evdev_device *device,
419 uint64_t time, int is_press)
422 libinput_timer_set(&device->scroll.timer,
423 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
425 libinput_timer_cancel(&device->scroll.timer);
426 if (device->scroll.button_scroll_active) {
427 evdev_stop_scroll(device, time,
428 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS);
429 device->scroll.button_scroll_active = false;
431 /* If the button is released quickly enough emit the
432 * button press/release events. */
433 evdev_pointer_notify_button(device, time,
434 device->scroll.button,
435 LIBINPUT_BUTTON_STATE_PRESSED);
436 evdev_pointer_notify_button(device, time,
437 device->scroll.button,
438 LIBINPUT_BUTTON_STATE_RELEASED);
444 evdev_process_touch_button(struct evdev_device *device,
445 uint64_t time, int value)
447 if (device->pending_event != EVDEV_NONE &&
448 device->pending_event != EVDEV_ABSOLUTE_MOTION)
449 evdev_flush_pending_event(device, time);
451 device->pending_event = (value ?
452 EVDEV_ABSOLUTE_TOUCH_DOWN :
453 EVDEV_ABSOLUTE_TOUCH_UP);
457 evdev_process_key(struct evdev_device *device,
458 struct input_event *e, uint64_t time)
460 enum evdev_key_type type;
462 /* ignore kernel key repeat */
466 if (e->code == BTN_TOUCH) {
468 evdev_process_touch_button(device, time, e->value);
472 evdev_flush_pending_event(device, time);
474 type = get_key_type(e->code);
476 /* Ignore key release events from the kernel for keys that libinput
477 * never got a pressed event for. */
480 case EVDEV_KEY_TYPE_NONE:
482 case EVDEV_KEY_TYPE_KEY:
483 case EVDEV_KEY_TYPE_BUTTON:
484 if (!hw_is_key_down(device, e->code))
489 hw_set_key_down(device, e->code, e->value);
492 case EVDEV_KEY_TYPE_NONE:
494 case EVDEV_KEY_TYPE_KEY:
495 evdev_keyboard_notify_key(
499 e->value ? LIBINPUT_KEY_STATE_PRESSED :
500 LIBINPUT_KEY_STATE_RELEASED);
502 case EVDEV_KEY_TYPE_BUTTON:
503 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
504 e->code == device->scroll.button) {
505 evdev_button_scroll_button(device, time, e->value);
508 evdev_pointer_notify_button(
511 evdev_to_left_handed(device, e->code),
512 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
513 LIBINPUT_BUTTON_STATE_RELEASED);
519 evdev_process_touch(struct evdev_device *device,
520 struct input_event *e,
525 if ((size_t)e->value >= device->mt.slots_len) {
526 log_bug_libinput(device->base.seat->libinput,
527 "%s exceeds slots (%d of %d)\n",
530 device->mt.slots_len);
531 e->value = device->mt.slots_len - 1;
533 evdev_flush_pending_event(device, time);
534 device->mt.slot = e->value;
536 case ABS_MT_TRACKING_ID:
537 if (device->pending_event != EVDEV_NONE &&
538 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
539 evdev_flush_pending_event(device, time);
541 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
543 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
545 case ABS_MT_POSITION_X:
546 device->mt.slots[device->mt.slot].point.x = e->value;
547 if (device->pending_event == EVDEV_NONE)
548 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
550 case ABS_MT_POSITION_Y:
551 device->mt.slots[device->mt.slot].point.y = e->value;
552 if (device->pending_event == EVDEV_NONE)
553 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
559 evdev_process_absolute_motion(struct evdev_device *device,
560 struct input_event *e)
564 device->abs.point.x = e->value;
565 if (device->pending_event == EVDEV_NONE)
566 device->pending_event = EVDEV_ABSOLUTE_MOTION;
569 device->abs.point.y = e->value;
570 if (device->pending_event == EVDEV_NONE)
571 device->pending_event = EVDEV_ABSOLUTE_MOTION;
577 evdev_notify_axis(struct evdev_device *device,
580 enum libinput_pointer_axis_source source,
581 const struct normalized_coords *delta_in,
582 const struct discrete_coords *discrete_in)
584 struct normalized_coords delta = *delta_in;
585 struct discrete_coords discrete = *discrete_in;
587 if (device->scroll.natural_scrolling_enabled) {
594 pointer_notify_axis(&device->base,
603 evdev_process_relative(struct evdev_device *device,
604 struct input_event *e, uint64_t time)
606 struct normalized_coords wheel_degrees = { 0.0, 0.0 };
607 struct discrete_coords discrete = { 0.0, 0.0 };
611 if (device->pending_event != EVDEV_RELATIVE_MOTION)
612 evdev_flush_pending_event(device, time);
613 device->rel.x += e->value;
614 device->pending_event = EVDEV_RELATIVE_MOTION;
617 if (device->pending_event != EVDEV_RELATIVE_MOTION)
618 evdev_flush_pending_event(device, time);
619 device->rel.y += e->value;
620 device->pending_event = EVDEV_RELATIVE_MOTION;
623 evdev_flush_pending_event(device, time);
624 wheel_degrees.y = -1 * e->value *
625 device->scroll.wheel_click_angle;
626 discrete.y = -1 * e->value;
630 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
631 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
636 evdev_flush_pending_event(device, time);
637 wheel_degrees.x = e->value * device->scroll.wheel_click_angle;
638 discrete.x = e->value;
642 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
643 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
651 evdev_process_absolute(struct evdev_device *device,
652 struct input_event *e,
656 evdev_process_touch(device, e, time);
658 evdev_process_absolute_motion(device, e);
663 evdev_any_button_down(struct evdev_device *device)
667 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
668 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
669 hw_is_key_down(device, button))
676 evdev_need_touch_frame(struct evdev_device *device)
678 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
681 switch (device->pending_event) {
683 case EVDEV_RELATIVE_MOTION:
685 case EVDEV_ABSOLUTE_MT_DOWN:
686 case EVDEV_ABSOLUTE_MT_MOTION:
687 case EVDEV_ABSOLUTE_MT_UP:
688 case EVDEV_ABSOLUTE_TOUCH_DOWN:
689 case EVDEV_ABSOLUTE_TOUCH_UP:
690 case EVDEV_ABSOLUTE_MOTION:
698 evdev_tag_external_mouse(struct evdev_device *device,
699 struct udev_device *udev_device)
703 bustype = libevdev_get_id_bustype(device->evdev);
704 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
705 if (device->seat_caps & EVDEV_DEVICE_POINTER)
706 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
711 evdev_tag_trackpoint(struct evdev_device *device,
712 struct udev_device *udev_device)
714 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
715 device->tags |= EVDEV_TAG_TRACKPOINT;
719 fallback_process(struct evdev_dispatch *dispatch,
720 struct evdev_device *device,
721 struct input_event *event,
724 bool need_frame = false;
726 switch (event->type) {
728 evdev_process_relative(device, event, time);
731 evdev_process_absolute(device, event, time);
734 evdev_process_key(device, event, time);
737 need_frame = evdev_need_touch_frame(device);
738 evdev_flush_pending_event(device, time);
740 touch_notify_frame(&device->base, time);
746 fallback_destroy(struct evdev_dispatch *dispatch)
752 fallback_tag_device(struct evdev_device *device,
753 struct udev_device *udev_device)
755 evdev_tag_external_mouse(device, udev_device);
756 evdev_tag_trackpoint(device, udev_device);
760 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
762 struct evdev_device *device = (struct evdev_device*)libinput_device;
764 return device->abs.absinfo_x && device->abs.absinfo_y;
767 static enum libinput_config_status
768 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
769 const float matrix[6])
771 struct evdev_device *device = (struct evdev_device*)libinput_device;
773 evdev_device_calibrate(device, matrix);
775 return LIBINPUT_CONFIG_STATUS_SUCCESS;
779 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
782 struct evdev_device *device = (struct evdev_device*)libinput_device;
784 matrix_to_farray6(&device->abs.usermatrix, matrix);
786 return !matrix_is_identity(&device->abs.usermatrix);
790 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
793 struct evdev_device *device = (struct evdev_device*)libinput_device;
795 matrix_to_farray6(&device->abs.default_calibration, matrix);
797 return !matrix_is_identity(&device->abs.default_calibration);
800 struct evdev_dispatch_interface fallback_interface = {
804 NULL, /* device_added */
805 NULL, /* device_removed */
806 NULL, /* device_suspended */
807 NULL, /* device_resumed */
812 evdev_sendevents_get_modes(struct libinput_device *device)
814 return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
817 static enum libinput_config_status
818 evdev_sendevents_set_mode(struct libinput_device *device,
819 enum libinput_config_send_events_mode mode)
821 struct evdev_device *evdev = (struct evdev_device*)device;
822 struct evdev_dispatch *dispatch = evdev->dispatch;
824 if (mode == dispatch->sendevents.current_mode)
825 return LIBINPUT_CONFIG_STATUS_SUCCESS;
828 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
829 evdev_device_resume(evdev);
831 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
832 evdev_device_suspend(evdev);
834 default: /* no support for combined modes yet */
835 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
838 dispatch->sendevents.current_mode = mode;
840 return LIBINPUT_CONFIG_STATUS_SUCCESS;
843 static enum libinput_config_send_events_mode
844 evdev_sendevents_get_mode(struct libinput_device *device)
846 struct evdev_device *evdev = (struct evdev_device*)device;
847 struct evdev_dispatch *dispatch = evdev->dispatch;
849 return dispatch->sendevents.current_mode;
852 static enum libinput_config_send_events_mode
853 evdev_sendevents_get_default_mode(struct libinput_device *device)
855 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
859 evdev_left_handed_has(struct libinput_device *device)
861 /* This is only hooked up when we have left-handed configuration, so we
862 * can hardcode 1 here */
867 evdev_change_to_left_handed(struct evdev_device *device)
869 if (device->left_handed.want_enabled == device->left_handed.enabled)
872 if (evdev_any_button_down(device))
875 device->left_handed.enabled = device->left_handed.want_enabled;
878 static enum libinput_config_status
879 evdev_left_handed_set(struct libinput_device *device, int left_handed)
881 struct evdev_device *evdev_device = (struct evdev_device *)device;
883 evdev_device->left_handed.want_enabled = left_handed ? true : false;
885 evdev_device->left_handed.change_to_enabled(evdev_device);
887 return LIBINPUT_CONFIG_STATUS_SUCCESS;
891 evdev_left_handed_get(struct libinput_device *device)
893 struct evdev_device *evdev_device = (struct evdev_device *)device;
895 /* return the wanted configuration, even if it hasn't taken
897 return evdev_device->left_handed.want_enabled;
901 evdev_left_handed_get_default(struct libinput_device *device)
907 evdev_init_left_handed(struct evdev_device *device,
908 void (*change_to_left_handed)(struct evdev_device *))
910 device->left_handed.config.has = evdev_left_handed_has;
911 device->left_handed.config.set = evdev_left_handed_set;
912 device->left_handed.config.get = evdev_left_handed_get;
913 device->left_handed.config.get_default = evdev_left_handed_get_default;
914 device->base.config.left_handed = &device->left_handed.config;
915 device->left_handed.enabled = false;
916 device->left_handed.want_enabled = false;
917 device->left_handed.change_to_enabled = change_to_left_handed;
923 evdev_scroll_get_methods(struct libinput_device *device)
925 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
929 evdev_change_scroll_method(struct evdev_device *device)
931 if (device->scroll.want_method == device->scroll.method &&
932 device->scroll.want_button == device->scroll.button)
935 if (evdev_any_button_down(device))
938 device->scroll.method = device->scroll.want_method;
939 device->scroll.button = device->scroll.want_button;
942 static enum libinput_config_status
943 evdev_scroll_set_method(struct libinput_device *device,
944 enum libinput_config_scroll_method method)
946 struct evdev_device *evdev = (struct evdev_device*)device;
948 evdev->scroll.want_method = method;
949 evdev->scroll.change_scroll_method(evdev);
951 return LIBINPUT_CONFIG_STATUS_SUCCESS;
954 static enum libinput_config_scroll_method
955 evdev_scroll_get_method(struct libinput_device *device)
957 struct evdev_device *evdev = (struct evdev_device *)device;
959 /* return the wanted configuration, even if it hasn't taken
961 return evdev->scroll.want_method;
964 static enum libinput_config_scroll_method
965 evdev_scroll_get_default_method(struct libinput_device *device)
967 struct evdev_device *evdev = (struct evdev_device *)device;
969 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
970 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
972 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
975 static enum libinput_config_status
976 evdev_scroll_set_button(struct libinput_device *device,
979 struct evdev_device *evdev = (struct evdev_device*)device;
981 evdev->scroll.want_button = button;
982 evdev->scroll.change_scroll_method(evdev);
984 return LIBINPUT_CONFIG_STATUS_SUCCESS;
988 evdev_scroll_get_button(struct libinput_device *device)
990 struct evdev_device *evdev = (struct evdev_device *)device;
992 /* return the wanted configuration, even if it hasn't taken
994 return evdev->scroll.want_button;
998 evdev_scroll_get_default_button(struct libinput_device *device)
1000 struct evdev_device *evdev = (struct evdev_device *)device;
1002 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
1009 evdev_init_button_scroll(struct evdev_device *device,
1010 void (*change_scroll_method)(struct evdev_device *))
1012 libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
1013 evdev_button_scroll_timeout, device);
1014 device->scroll.config.get_methods = evdev_scroll_get_methods;
1015 device->scroll.config.set_method = evdev_scroll_set_method;
1016 device->scroll.config.get_method = evdev_scroll_get_method;
1017 device->scroll.config.get_default_method = evdev_scroll_get_default_method;
1018 device->scroll.config.set_button = evdev_scroll_set_button;
1019 device->scroll.config.get_button = evdev_scroll_get_button;
1020 device->scroll.config.get_default_button = evdev_scroll_get_default_button;
1021 device->base.config.scroll_method = &device->scroll.config;
1022 device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
1023 device->scroll.want_method = device->scroll.method;
1024 device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
1025 device->scroll.want_button = device->scroll.button;
1026 device->scroll.change_scroll_method = change_scroll_method;
1032 evdev_init_calibration(struct evdev_device *device,
1033 struct evdev_dispatch *dispatch)
1035 device->base.config.calibration = &dispatch->calibration;
1037 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
1038 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
1039 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
1040 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
1044 evdev_init_sendevents(struct evdev_device *device,
1045 struct evdev_dispatch *dispatch)
1047 device->base.config.sendevents = &dispatch->sendevents.config;
1049 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1050 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
1051 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
1052 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
1053 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
1057 evdev_scroll_config_natural_has(struct libinput_device *device)
1062 static enum libinput_config_status
1063 evdev_scroll_config_natural_set(struct libinput_device *device,
1066 struct evdev_device *dev = (struct evdev_device *)device;
1068 dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1070 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1074 evdev_scroll_config_natural_get(struct libinput_device *device)
1076 struct evdev_device *dev = (struct evdev_device *)device;
1078 return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1082 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1084 /* could enable this on Apple touchpads. could do that, could
1085 * very well do that... */
1090 evdev_init_natural_scroll(struct evdev_device *device)
1092 device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1093 device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1094 device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1095 device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1096 device->scroll.natural_scrolling_enabled = false;
1097 device->base.config.natural_scroll = &device->scroll.config_natural;
1100 static struct evdev_dispatch *
1101 fallback_dispatch_create(struct libinput_device *device)
1103 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1104 struct evdev_device *evdev_device = (struct evdev_device *)device;
1106 if (dispatch == NULL)
1109 dispatch->interface = &fallback_interface;
1111 if (evdev_device->left_handed.want_enabled &&
1112 evdev_init_left_handed(evdev_device,
1113 evdev_change_to_left_handed) == -1) {
1118 if (evdev_device->scroll.want_button &&
1119 evdev_init_button_scroll(evdev_device,
1120 evdev_change_scroll_method) == -1) {
1125 if (evdev_device->scroll.natural_scrolling_enabled)
1126 evdev_init_natural_scroll(evdev_device);
1128 evdev_init_calibration(evdev_device, dispatch);
1129 evdev_init_sendevents(evdev_device, dispatch);
1135 evdev_process_event(struct evdev_device *device, struct input_event *e)
1137 struct evdev_dispatch *dispatch = device->dispatch;
1138 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1141 if (libevdev_event_is_code(e, EV_SYN, SYN_REPORT))
1142 log_debug(device->base.seat->libinput,
1143 "-------------- EV_SYN ------------\n");
1145 log_debug(device->base.seat->libinput,
1146 "%-7s %-16s %-20s %4d\n",
1147 evdev_device_get_sysname(device),
1148 libevdev_event_type_get_name(e->type),
1149 libevdev_event_code_get_name(e->type, e->code),
1153 dispatch->interface->process(dispatch, device, e, time);
1157 evdev_device_dispatch_one(struct evdev_device *device,
1158 struct input_event *ev)
1160 if (!device->mtdev) {
1161 evdev_process_event(device, ev);
1163 mtdev_put_event(device->mtdev, ev);
1164 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1165 while (!mtdev_empty(device->mtdev)) {
1166 struct input_event e;
1167 mtdev_get_event(device->mtdev, &e);
1168 evdev_process_event(device, &e);
1175 evdev_sync_device(struct evdev_device *device)
1177 struct input_event ev;
1181 rc = libevdev_next_event(device->evdev,
1182 LIBEVDEV_READ_FLAG_SYNC, &ev);
1185 evdev_device_dispatch_one(device, &ev);
1186 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1188 return rc == -EAGAIN ? 0 : rc;
1192 evdev_device_dispatch(void *data)
1194 struct evdev_device *device = data;
1195 struct libinput *libinput = device->base.seat->libinput;
1196 struct input_event ev;
1199 /* If the compositor is repainting, this function is called only once
1200 * per frame and we have to process all the events available on the
1201 * fd, otherwise there will be input lag. */
1203 rc = libevdev_next_event(device->evdev,
1204 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1205 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1206 switch (ratelimit_test(&device->syn_drop_limit)) {
1207 case RATELIMIT_PASS:
1208 log_info(libinput, "SYN_DROPPED event from "
1209 "\"%s\" - some input events have "
1210 "been lost.\n", device->devname);
1212 case RATELIMIT_THRESHOLD:
1213 log_info(libinput, "SYN_DROPPED flood "
1217 case RATELIMIT_EXCEEDED:
1221 /* send one more sync event so we handle all
1222 currently pending events before we sync up
1223 to the current state */
1224 ev.code = SYN_REPORT;
1225 evdev_device_dispatch_one(device, &ev);
1227 rc = evdev_sync_device(device);
1229 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1230 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1231 evdev_device_dispatch_one(device, &ev);
1233 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1235 if (rc != -EAGAIN && rc != -EINTR) {
1236 libinput_remove_source(libinput, device->source);
1237 device->source = NULL;
1242 evdev_accel_config_available(struct libinput_device *device)
1244 /* this function is only called if we set up ptraccel, so we can
1245 reply with a resounding "Yes" */
1249 static enum libinput_config_status
1250 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1252 struct evdev_device *dev = (struct evdev_device *)device;
1254 if (!filter_set_speed(dev->pointer.filter, speed))
1255 return LIBINPUT_CONFIG_STATUS_INVALID;
1257 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1261 evdev_accel_config_get_speed(struct libinput_device *device)
1263 struct evdev_device *dev = (struct evdev_device *)device;
1265 return filter_get_speed(dev->pointer.filter);
1269 evdev_accel_config_get_default_speed(struct libinput_device *device)
1275 evdev_device_init_pointer_acceleration(struct evdev_device *device,
1276 accel_profile_func_t profile)
1278 device->pointer.filter = create_pointer_accelerator_filter(profile);
1279 if (!device->pointer.filter)
1282 device->pointer.config.available = evdev_accel_config_available;
1283 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1284 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1285 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1286 device->base.config.accel = &device->pointer.config;
1288 evdev_accel_config_set_speed(&device->base,
1289 evdev_accel_config_get_default_speed(&device->base));
1295 evdev_need_mtdev(struct evdev_device *device)
1297 struct libevdev *evdev = device->evdev;
1299 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1300 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1301 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1305 evdev_tag_device(struct evdev_device *device)
1307 if (device->dispatch->interface->tag_device)
1308 device->dispatch->interface->tag_device(device,
1309 device->udev_device);
1313 evdev_read_wheel_click_prop(struct evdev_device *device)
1315 struct libinput *libinput = device->base.seat->libinput;
1317 int angle = DEFAULT_WHEEL_CLICK_ANGLE;
1319 prop = udev_device_get_property_value(device->udev_device,
1320 "MOUSE_WHEEL_CLICK_ANGLE");
1322 angle = parse_mouse_wheel_click_angle_property(prop);
1325 "Mouse wheel click angle '%s' is present but invalid,"
1326 "using %d degrees instead\n",
1328 DEFAULT_WHEEL_CLICK_ANGLE);
1329 angle = DEFAULT_WHEEL_CLICK_ANGLE;
1336 evdev_read_dpi_prop(struct evdev_device *device)
1338 struct libinput *libinput = device->base.seat->libinput;
1339 const char *mouse_dpi;
1340 int dpi = DEFAULT_MOUSE_DPI;
1342 mouse_dpi = udev_device_get_property_value(device->udev_device,
1345 dpi = parse_mouse_dpi_property(mouse_dpi);
1347 log_error(libinput, "Mouse DPI property for '%s' is "
1348 "present but invalid, using %d "
1352 dpi = DEFAULT_MOUSE_DPI;
1359 /* Return 1 if the given resolutions have been set, or 0 otherwise */
1361 evdev_fix_abs_resolution(struct evdev_device *device,
1367 struct libinput *libinput = device->base.seat->libinput;
1368 struct libevdev *evdev = device->evdev;
1369 const struct input_absinfo *absx, *absy;
1370 struct input_absinfo fixed;
1373 if (!(xcode == ABS_X && ycode == ABS_Y) &&
1374 !(xcode == ABS_MT_POSITION_X && ycode == ABS_MT_POSITION_Y)) {
1375 log_bug_libinput(libinput,
1376 "Invalid x/y code combination %d/%d\n",
1381 if (xresolution == 0 || yresolution == 0 ||
1382 (xresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution) ||
1383 (yresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution)) {
1384 log_bug_libinput(libinput,
1385 "Invalid x/y resolutions %d/%d\n",
1386 xresolution, yresolution);
1390 absx = libevdev_get_abs_info(evdev, xcode);
1391 absy = libevdev_get_abs_info(evdev, ycode);
1393 if (absx->resolution == 0 || absx->resolution == EVDEV_FAKE_RESOLUTION) {
1395 fixed.resolution = xresolution;
1396 /* libevdev_set_abs_info() changes the absinfo we already
1397 have a pointer to, no need to fetch it again */
1398 libevdev_set_abs_info(evdev, xcode, &fixed);
1402 if (absy->resolution == 0 || absy->resolution == EVDEV_FAKE_RESOLUTION) {
1404 fixed.resolution = yresolution;
1405 /* libevdev_set_abs_info() changes the absinfo we already
1406 have a pointer to, no need to fetch it again */
1407 libevdev_set_abs_info(evdev, ycode, &fixed);
1414 static enum evdev_device_udev_tags
1415 evdev_device_get_udev_tags(struct evdev_device *device,
1416 struct udev_device *udev_device)
1419 enum evdev_device_udev_tags tags = 0;
1420 const struct evdev_udev_tag_match *match;
1423 for (i = 0; i < 2 && udev_device; i++) {
1424 match = evdev_udev_tag_matches;
1425 while (match->name) {
1426 prop = udev_device_get_property_value(
1434 udev_device = udev_device_get_parent(udev_device);
1440 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1441 the limited ABS_* range - they aren't MT devices, they
1442 just have too many ABS_ axes */
1444 evdev_is_fake_mt_device(struct evdev_device *device)
1446 struct libevdev *evdev = device->evdev;
1448 return libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1449 libevdev_get_num_slots(evdev) == -1;
1453 evdev_fix_android_mt(struct evdev_device *device)
1455 struct libevdev *evdev = device->evdev;
1457 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1458 libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1461 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1462 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) ||
1463 evdev_is_fake_mt_device(device))
1466 libevdev_enable_event_code(evdev, EV_ABS, ABS_X,
1467 libevdev_get_abs_info(evdev, ABS_MT_POSITION_X));
1468 libevdev_enable_event_code(evdev, EV_ABS, ABS_Y,
1469 libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y));
1473 evdev_check_min_max(struct evdev_device *device, unsigned int code)
1475 struct libevdev *evdev = device->evdev;
1476 const struct input_absinfo *absinfo;
1478 if (!libevdev_has_event_code(evdev, EV_ABS, code))
1481 absinfo = libevdev_get_abs_info(evdev, code);
1482 if (absinfo->minimum == absinfo->maximum) {
1483 log_bug_kernel(device->base.seat->libinput,
1484 "Device '%s' has min == max on %s\n",
1486 libevdev_event_code_get_name(EV_ABS, code));
1494 evdev_reject_device(struct evdev_device *device)
1496 struct libinput *libinput = device->base.seat->libinput;
1497 struct libevdev *evdev = device->evdev;
1499 const struct input_absinfo *absx, *absy;
1501 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ^
1502 libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1505 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ^
1506 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1509 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
1510 absx = libevdev_get_abs_info(evdev, ABS_X);
1511 absy = libevdev_get_abs_info(evdev, ABS_Y);
1512 if ((absx->resolution == 0 && absy->resolution != 0) ||
1513 (absx->resolution != 0 && absy->resolution == 0)) {
1514 log_bug_kernel(libinput,
1515 "Kernel has only x or y resolution, not both.\n");
1520 if (!evdev_is_fake_mt_device(device) &&
1521 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X)) {
1522 absx = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1523 absy = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1524 if ((absx->resolution == 0 && absy->resolution != 0) ||
1525 (absx->resolution != 0 && absy->resolution == 0)) {
1526 log_bug_kernel(libinput,
1527 "Kernel has only x or y MT resolution, not both.\n");
1532 for (code = 0; code < ABS_CNT; code++) {
1536 case ABS_MT_TOOL_TYPE:
1539 if (evdev_check_min_max(device, code) == -1)
1548 evdev_configure_mt_device(struct evdev_device *device)
1550 struct libevdev *evdev = device->evdev;
1551 struct mt_slot *slots;
1556 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1557 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1560 if (evdev_fix_abs_resolution(device,
1563 EVDEV_FAKE_RESOLUTION,
1564 EVDEV_FAKE_RESOLUTION))
1565 device->abs.fake_resolution = 1;
1567 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1568 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1571 /* We only handle the slotted Protocol B in libinput.
1572 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1573 require mtdev for conversion. */
1574 if (evdev_need_mtdev(device)) {
1575 device->mtdev = mtdev_new_open(device->fd);
1579 /* pick 10 slots as default for type A
1582 active_slot = device->mtdev->caps.slot.value;
1584 num_slots = libevdev_get_num_slots(device->evdev);
1585 active_slot = libevdev_get_current_slot(evdev);
1588 slots = calloc(num_slots, sizeof(struct mt_slot));
1592 for (slot = 0; slot < num_slots; ++slot) {
1593 slots[slot].seat_slot = -1;
1594 slots[slot].point.x = 0;
1595 slots[slot].point.y = 0;
1597 device->mt.slots = slots;
1598 device->mt.slots_len = num_slots;
1599 device->mt.slot = active_slot;
1605 evdev_configure_device(struct evdev_device *device)
1607 struct libinput *libinput = device->base.seat->libinput;
1608 struct libevdev *evdev = device->evdev;
1609 const char *devnode = udev_device_get_devnode(device->udev_device);
1610 enum evdev_device_udev_tags udev_tags;
1612 udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1614 if ((udev_tags & EVDEV_UDEV_TAG_INPUT) == 0 ||
1615 (udev_tags & ~EVDEV_UDEV_TAG_INPUT) == 0) {
1617 "input device '%s', %s not tagged as input device\n",
1618 device->devname, devnode);
1623 "input device '%s', %s is tagged by udev as:%s%s%s%s%s%s%s%s\n",
1624 device->devname, devnode,
1625 udev_tags & EVDEV_UDEV_TAG_KEYBOARD ? " Keyboard" : "",
1626 udev_tags & EVDEV_UDEV_TAG_MOUSE ? " Mouse" : "",
1627 udev_tags & EVDEV_UDEV_TAG_TOUCHPAD ? " Touchpad" : "",
1628 udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN ? " Touchscreen" : "",
1629 udev_tags & EVDEV_UDEV_TAG_TABLET ? " Tablet" : "",
1630 udev_tags & EVDEV_UDEV_TAG_JOYSTICK ? " Joystick" : "",
1631 udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER ? " Accelerometer" : "",
1632 udev_tags & EVDEV_UDEV_TAG_BUTTONSET ? " Buttonset" : "");
1634 /* libwacom *adds* TABLET, TOUCHPAD but leaves JOYSTICK in place, so
1635 make sure we only ignore real joystick devices */
1636 if ((udev_tags & EVDEV_UDEV_TAG_JOYSTICK) == udev_tags) {
1638 "input device '%s', %s is a joystick, ignoring\n",
1639 device->devname, devnode);
1643 /* libwacom assigns tablet _and_ tablet_pad to the pad devices */
1644 if (udev_tags & EVDEV_UDEV_TAG_BUTTONSET) {
1646 "input device '%s', %s is a buttonset, ignoring\n",
1647 device->devname, devnode);
1651 if (evdev_reject_device(device) == -1) {
1653 "input device '%s', %s was rejected.\n",
1654 device->devname, devnode);
1658 if (!evdev_is_fake_mt_device(device))
1659 evdev_fix_android_mt(device);
1661 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
1662 if (evdev_fix_abs_resolution(device,
1665 EVDEV_FAKE_RESOLUTION,
1666 EVDEV_FAKE_RESOLUTION))
1667 device->abs.fake_resolution = 1;
1668 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_X);
1669 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_Y);
1671 if (evdev_is_fake_mt_device(device)) {
1672 udev_tags &= ~EVDEV_UDEV_TAG_TOUCHSCREEN;
1673 } else if (evdev_configure_mt_device(device) == -1) {
1678 if (udev_tags & EVDEV_UDEV_TAG_TOUCHPAD) {
1679 device->dispatch = evdev_mt_touchpad_create(device);
1681 "input device '%s', %s is a touchpad\n",
1682 device->devname, devnode);
1683 return device->dispatch == NULL ? -1 : 0;
1686 if (udev_tags & EVDEV_UDEV_TAG_MOUSE) {
1687 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
1688 !libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
1689 evdev_device_init_pointer_acceleration(
1691 pointer_accel_profile_linear) == -1)
1694 device->seat_caps |= EVDEV_DEVICE_POINTER;
1697 "input device '%s', %s is a pointer caps\n",
1698 device->devname, devnode);
1700 /* want left-handed config option */
1701 device->left_handed.want_enabled = true;
1702 /* want natural-scroll config option */
1703 device->scroll.natural_scrolling_enabled = true;
1704 /* want button scrolling config option */
1705 device->scroll.want_button = 1;
1708 if (udev_tags & EVDEV_UDEV_TAG_KEYBOARD) {
1709 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1711 "input device '%s', %s is a keyboard\n",
1712 device->devname, devnode);
1714 /* want natural-scroll config option */
1715 if (libevdev_has_event_code(evdev, EV_REL, REL_WHEEL) ||
1716 libevdev_has_event_code(evdev, EV_REL, REL_HWHEEL)) {
1717 device->scroll.natural_scrolling_enabled = true;
1718 device->seat_caps |= EVDEV_DEVICE_POINTER;
1722 if (udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN) {
1723 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1725 "input device '%s', %s is a touch device\n",
1726 device->devname, devnode);
1733 evdev_notify_added_device(struct evdev_device *device)
1735 struct libinput_device *dev;
1737 list_for_each(dev, &device->base.seat->devices_list, link) {
1738 struct evdev_device *d = (struct evdev_device*)dev;
1739 if (dev == &device->base)
1742 /* Notify existing device d about addition of device device */
1743 if (d->dispatch->interface->device_added)
1744 d->dispatch->interface->device_added(d, device);
1746 /* Notify new device device about existing device d */
1747 if (device->dispatch->interface->device_added)
1748 device->dispatch->interface->device_added(device, d);
1750 /* Notify new device device if existing device d is suspended */
1751 if (d->suspended && device->dispatch->interface->device_suspended)
1752 device->dispatch->interface->device_suspended(device, d);
1755 notify_added_device(&device->base);
1759 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1761 struct udev *udev = udev_device_get_udev(udev_device);
1762 struct udev_device *udev_device_new = NULL;
1766 if (fstat(fd, &st) < 0)
1769 udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1770 if (!udev_device_new)
1773 rc = strcmp(udev_device_get_syspath(udev_device_new),
1774 udev_device_get_syspath(udev_device));
1776 if (udev_device_new)
1777 udev_device_unref(udev_device_new);
1782 evdev_set_device_group(struct evdev_device *device,
1783 struct udev_device *udev_device)
1785 struct libinput_device_group *group = NULL;
1786 const char *udev_group;
1788 udev_group = udev_device_get_property_value(udev_device,
1789 "LIBINPUT_DEVICE_GROUP");
1791 struct libinput_device *d;
1793 list_for_each(d, &device->base.seat->devices_list, link) {
1794 const char *identifier = d->group->identifier;
1797 strcmp(identifier, udev_group) == 0) {
1805 group = libinput_device_group_create(udev_group);
1808 libinput_device_set_device_group(&device->base, group);
1809 libinput_device_group_unref(group);
1811 libinput_device_set_device_group(&device->base, group);
1817 struct evdev_device *
1818 evdev_device_create(struct libinput_seat *seat,
1819 struct udev_device *udev_device)
1821 struct libinput *libinput = seat->libinput;
1822 struct evdev_device *device = NULL;
1825 int unhandled_device = 0;
1826 const char *devnode = udev_device_get_devnode(udev_device);
1828 /* Use non-blocking mode so that we can loop on read on
1829 * evdev_device_data() until all events on the fd are
1830 * read. mtdev_get() also expects this. */
1831 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1834 "opening input device '%s' failed (%s).\n",
1835 devnode, strerror(-fd));
1839 if (evdev_device_compare_syspath(udev_device, fd) != 0)
1842 device = zalloc(sizeof *device);
1846 libinput_device_init(&device->base, seat);
1847 libinput_seat_ref(seat);
1849 rc = libevdev_new_from_fd(fd, &device->evdev);
1853 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1855 device->seat_caps = 0;
1857 device->mtdev = NULL;
1858 device->udev_device = udev_device_ref(udev_device);
1861 device->abs.seat_slot = -1;
1862 device->dispatch = NULL;
1864 device->pending_event = EVDEV_NONE;
1865 device->devname = libevdev_get_name(device->evdev);
1866 device->scroll.threshold = 5.0; /* Default may be overridden */
1867 device->scroll.direction = 0;
1868 device->scroll.wheel_click_angle =
1869 evdev_read_wheel_click_prop(device);
1870 device->dpi = evdev_read_dpi_prop(device);
1871 /* at most 5 SYN_DROPPED log-messages per 30s */
1872 ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1874 matrix_init_identity(&device->abs.calibration);
1875 matrix_init_identity(&device->abs.usermatrix);
1876 matrix_init_identity(&device->abs.default_calibration);
1878 if (evdev_configure_device(device) == -1)
1881 if (device->seat_caps == 0) {
1882 unhandled_device = 1;
1886 /* If the dispatch was not set up use the fallback. */
1887 if (device->dispatch == NULL)
1888 device->dispatch = fallback_dispatch_create(&device->base);
1889 if (device->dispatch == NULL)
1893 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1894 if (!device->source)
1897 if (evdev_set_device_group(device, udev_device))
1900 list_insert(seat->devices_list.prev, &device->base.link);
1902 evdev_tag_device(device);
1903 evdev_notify_added_device(device);
1909 close_restricted(libinput, fd);
1911 evdev_device_destroy(device);
1913 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1917 evdev_device_get_output(struct evdev_device *device)
1919 return device->output_name;
1923 evdev_device_get_sysname(struct evdev_device *device)
1925 return udev_device_get_sysname(device->udev_device);
1929 evdev_device_get_name(struct evdev_device *device)
1931 return device->devname;
1935 evdev_device_get_id_product(struct evdev_device *device)
1937 return libevdev_get_id_product(device->evdev);
1941 evdev_device_get_id_vendor(struct evdev_device *device)
1943 return libevdev_get_id_vendor(device->evdev);
1946 struct udev_device *
1947 evdev_device_get_udev_device(struct evdev_device *device)
1949 return udev_device_ref(device->udev_device);
1953 evdev_device_set_default_calibration(struct evdev_device *device,
1954 const float calibration[6])
1956 matrix_from_farray6(&device->abs.default_calibration, calibration);
1957 evdev_device_calibrate(device, calibration);
1961 evdev_device_calibrate(struct evdev_device *device,
1962 const float calibration[6])
1964 struct matrix scale,
1969 matrix_from_farray6(&transform, calibration);
1970 device->abs.apply_calibration = !matrix_is_identity(&transform);
1972 if (!device->abs.apply_calibration) {
1973 matrix_init_identity(&device->abs.calibration);
1977 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1978 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1980 /* The transformation matrix is in the form:
1984 * Where a, e are the scale components, a, b, d, e are the rotation
1985 * component (combined with scale) and c and f are the translation
1986 * component. The translation component in the input matrix must be
1987 * normalized to multiples of the device width and height,
1988 * respectively. e.g. c == 1 shifts one device-width to the right.
1990 * We pre-calculate a single matrix to apply to event coordinates:
1991 * M = Un-Normalize * Calibration * Normalize
1993 * Normalize: scales the device coordinates to [0,1]
1994 * Calibration: user-supplied matrix
1995 * Un-Normalize: scales back up to device coordinates
1996 * Matrix maths requires the normalize/un-normalize in reverse
2000 /* back up the user matrix so we can return it on request */
2001 matrix_from_farray6(&device->abs.usermatrix, calibration);
2004 matrix_init_translate(&translate,
2005 device->abs.absinfo_x->minimum,
2006 device->abs.absinfo_y->minimum);
2007 matrix_init_scale(&scale, sx, sy);
2008 matrix_mult(&scale, &translate, &scale);
2011 matrix_mult(&transform, &scale, &transform);
2014 matrix_init_translate(&translate,
2015 -device->abs.absinfo_x->minimum/sx,
2016 -device->abs.absinfo_y->minimum/sy);
2017 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
2018 matrix_mult(&scale, &translate, &scale);
2020 /* store final matrix in device */
2021 matrix_mult(&device->abs.calibration, &transform, &scale);
2025 evdev_device_has_capability(struct evdev_device *device,
2026 enum libinput_device_capability capability)
2028 switch (capability) {
2029 case LIBINPUT_DEVICE_CAP_POINTER:
2030 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
2031 case LIBINPUT_DEVICE_CAP_KEYBOARD:
2032 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
2033 case LIBINPUT_DEVICE_CAP_TOUCH:
2034 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
2041 evdev_device_get_size(struct evdev_device *device,
2045 const struct input_absinfo *x, *y;
2047 x = libevdev_get_abs_info(device->evdev, ABS_X);
2048 y = libevdev_get_abs_info(device->evdev, ABS_Y);
2050 if (!x || !y || device->abs.fake_resolution ||
2051 !x->resolution || !y->resolution)
2054 *width = evdev_convert_to_mm(x, x->maximum);
2055 *height = evdev_convert_to_mm(y, y->maximum);
2061 evdev_device_has_button(struct evdev_device *device, uint32_t code)
2063 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
2066 return libevdev_has_event_code(device->evdev, EV_KEY, code);
2070 evdev_is_scrolling(const struct evdev_device *device,
2071 enum libinput_pointer_axis axis)
2073 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2074 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2076 return (device->scroll.direction & AS_MASK(axis)) != 0;
2080 evdev_start_scrolling(struct evdev_device *device,
2081 enum libinput_pointer_axis axis)
2083 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2084 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2086 device->scroll.direction |= AS_MASK(axis);
2090 evdev_post_scroll(struct evdev_device *device,
2092 enum libinput_pointer_axis_source source,
2093 const struct normalized_coords *delta)
2095 const struct normalized_coords *trigger;
2096 struct normalized_coords event;
2098 if (!evdev_is_scrolling(device,
2099 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2100 device->scroll.buildup.y += delta->y;
2101 if (!evdev_is_scrolling(device,
2102 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2103 device->scroll.buildup.x += delta->x;
2105 trigger = &device->scroll.buildup;
2107 /* If we're not scrolling yet, use a distance trigger: moving
2108 past a certain distance starts scrolling */
2109 if (!evdev_is_scrolling(device,
2110 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
2111 !evdev_is_scrolling(device,
2112 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2113 if (fabs(trigger->y) >= device->scroll.threshold)
2114 evdev_start_scrolling(device,
2115 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2116 if (fabs(trigger->x) >= device->scroll.threshold)
2117 evdev_start_scrolling(device,
2118 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2119 /* We're already scrolling in one direction. Require some
2120 trigger speed to start scrolling in the other direction */
2121 } else if (!evdev_is_scrolling(device,
2122 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2123 if (fabs(delta->y) >= device->scroll.threshold)
2124 evdev_start_scrolling(device,
2125 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2126 } else if (!evdev_is_scrolling(device,
2127 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
2128 if (fabs(delta->x) >= device->scroll.threshold)
2129 evdev_start_scrolling(device,
2130 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2135 /* We use the trigger to enable, but the delta from this event for
2136 * the actual scroll movement. Otherwise we get a jump once
2137 * scrolling engages */
2138 if (!evdev_is_scrolling(device,
2139 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2142 if (!evdev_is_scrolling(device,
2143 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2146 if (!normalized_is_zero(event)) {
2147 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2148 evdev_notify_axis(device,
2150 device->scroll.direction,
2158 evdev_stop_scroll(struct evdev_device *device,
2160 enum libinput_pointer_axis_source source)
2162 const struct normalized_coords zero = { 0.0, 0.0 };
2163 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2165 /* terminate scrolling with a zero scroll event */
2166 if (device->scroll.direction != 0)
2167 pointer_notify_axis(&device->base,
2169 device->scroll.direction,
2174 device->scroll.buildup.x = 0;
2175 device->scroll.buildup.y = 0;
2176 device->scroll.direction = 0;
2180 release_pressed_keys(struct evdev_device *device)
2182 struct libinput *libinput = device->base.seat->libinput;
2186 if ((time = libinput_now(libinput)) == 0)
2189 for (code = 0; code < KEY_CNT; code++) {
2190 int count = get_key_down_count(device, code);
2193 log_bug_libinput(libinput,
2194 "Key %d is down %d times.\n",
2199 while (get_key_down_count(device, code) > 0) {
2200 switch (get_key_type(code)) {
2201 case EVDEV_KEY_TYPE_NONE:
2203 case EVDEV_KEY_TYPE_KEY:
2204 evdev_keyboard_notify_key(
2208 LIBINPUT_KEY_STATE_RELEASED);
2210 case EVDEV_KEY_TYPE_BUTTON:
2211 evdev_pointer_notify_button(
2214 evdev_to_left_handed(device, code),
2215 LIBINPUT_BUTTON_STATE_RELEASED);
2223 evdev_notify_suspended_device(struct evdev_device *device)
2225 struct libinput_device *it;
2227 if (device->suspended)
2230 list_for_each(it, &device->base.seat->devices_list, link) {
2231 struct evdev_device *d = (struct evdev_device*)it;
2232 if (it == &device->base)
2235 if (d->dispatch->interface->device_suspended)
2236 d->dispatch->interface->device_suspended(d, device);
2239 device->suspended = 1;
2243 evdev_notify_resumed_device(struct evdev_device *device)
2245 struct libinput_device *it;
2247 if (!device->suspended)
2250 list_for_each(it, &device->base.seat->devices_list, link) {
2251 struct evdev_device *d = (struct evdev_device*)it;
2252 if (it == &device->base)
2255 if (d->dispatch->interface->device_resumed)
2256 d->dispatch->interface->device_resumed(d, device);
2259 device->suspended = 0;
2263 evdev_device_suspend(struct evdev_device *device)
2265 evdev_notify_suspended_device(device);
2267 if (device->source) {
2268 libinput_remove_source(device->base.seat->libinput,
2270 device->source = NULL;
2273 release_pressed_keys(device);
2275 if (device->mtdev) {
2276 mtdev_close_delete(device->mtdev);
2277 device->mtdev = NULL;
2280 if (device->fd != -1) {
2281 close_restricted(device->base.seat->libinput, device->fd);
2289 evdev_device_resume(struct evdev_device *device)
2291 struct libinput *libinput = device->base.seat->libinput;
2293 const char *devnode;
2294 struct input_event ev;
2295 enum libevdev_read_status status;
2297 if (device->fd != -1)
2300 if (device->was_removed)
2303 devnode = udev_device_get_devnode(device->udev_device);
2304 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
2309 if (evdev_device_compare_syspath(device->udev_device, fd)) {
2310 close_restricted(libinput, fd);
2316 if (evdev_need_mtdev(device)) {
2317 device->mtdev = mtdev_new_open(device->fd);
2322 libevdev_change_fd(device->evdev, fd);
2323 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
2325 /* re-sync libevdev's view of the device, but discard the actual
2326 events. Our device is in a neutral state already */
2327 libevdev_next_event(device->evdev,
2328 LIBEVDEV_READ_FLAG_FORCE_SYNC,
2331 status = libevdev_next_event(device->evdev,
2332 LIBEVDEV_READ_FLAG_SYNC,
2334 } while (status == LIBEVDEV_READ_STATUS_SYNC);
2337 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2338 if (!device->source) {
2339 mtdev_close_delete(device->mtdev);
2343 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2345 evdev_notify_resumed_device(device);
2351 evdev_device_remove(struct evdev_device *device)
2353 struct libinput_device *dev;
2355 list_for_each(dev, &device->base.seat->devices_list, link) {
2356 struct evdev_device *d = (struct evdev_device*)dev;
2357 if (dev == &device->base)
2360 if (d->dispatch->interface->device_removed)
2361 d->dispatch->interface->device_removed(d, device);
2364 evdev_device_suspend(device);
2366 if (device->dispatch->interface->remove)
2367 device->dispatch->interface->remove(device->dispatch);
2369 /* A device may be removed while suspended, mark it to
2370 * skip re-opening a different device with the same node */
2371 device->was_removed = true;
2373 list_remove(&device->base.link);
2375 notify_removed_device(&device->base);
2376 libinput_device_unref(&device->base);
2380 evdev_device_destroy(struct evdev_device *device)
2382 struct evdev_dispatch *dispatch;
2384 dispatch = device->dispatch;
2386 dispatch->interface->destroy(dispatch);
2388 if (device->base.group)
2389 libinput_device_group_unref(device->base.group);
2391 filter_destroy(device->pointer.filter);
2392 libinput_seat_unref(device->base.seat);
2393 libevdev_free(device->evdev);
2394 udev_device_unref(device->udev_device);
2395 free(device->mt.slots);