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 device->left_handed.change_to_enabled)
156 device->left_handed.change_to_enabled(device);
158 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
159 device->scroll.change_scroll_method)
160 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 * (double)device->dpi / DEFAULT_MOUSE_DPI;
234 normalized->y = delta->y * (double)device->dpi / DEFAULT_MOUSE_DPI;
238 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
240 struct libinput *libinput = device->base.seat->libinput;
241 struct motion_params motion;
244 struct libinput_device *base = &device->base;
245 struct libinput_seat *seat = base->seat;
246 struct normalized_coords accel, unaccel;
247 struct device_coords point;
249 slot = device->mt.slot;
251 switch (device->pending_event) {
254 case EVDEV_RELATIVE_MOTION:
255 normalize_delta(device, &device->rel, &unaccel);
259 /* Use unaccelerated deltas for pointing stick scroll */
260 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
261 hw_is_key_down(device, device->scroll.button)) {
262 if (device->scroll.button_scroll_active)
263 evdev_post_scroll(device, time,
264 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
269 /* Apply pointer acceleration. */
270 motion.dx = unaccel.x;
271 motion.dy = unaccel.y;
272 filter_dispatch(device->pointer.filter, &motion, device, time);
276 if (accel.x == 0.0 && accel.y == 0.0 &&
277 unaccel.x == 0.0 && unaccel.y == 0.0) {
281 pointer_notify_motion(base, time, &accel, &unaccel);
283 case EVDEV_ABSOLUTE_MT_DOWN:
284 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
287 if (device->mt.slots[slot].seat_slot != -1) {
288 log_bug_kernel(libinput,
289 "%s: Driver sent multiple touch down for the "
291 udev_device_get_devnode(device->udev_device));
295 seat_slot = ffs(~seat->slot_map) - 1;
296 device->mt.slots[slot].seat_slot = seat_slot;
301 seat->slot_map |= 1 << seat_slot;
302 point = device->mt.slots[slot].point;
303 transform_absolute(device, &point);
305 touch_notify_touch_down(base, time, slot, seat_slot,
308 case EVDEV_ABSOLUTE_MT_MOTION:
309 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
312 seat_slot = device->mt.slots[slot].seat_slot;
313 point = device->mt.slots[slot].point;
318 transform_absolute(device, &point);
319 touch_notify_touch_motion(base, time, slot, seat_slot,
322 case EVDEV_ABSOLUTE_MT_UP:
323 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
326 seat_slot = device->mt.slots[slot].seat_slot;
327 device->mt.slots[slot].seat_slot = -1;
332 seat->slot_map &= ~(1 << seat_slot);
334 touch_notify_touch_up(base, time, slot, seat_slot);
336 case EVDEV_ABSOLUTE_TOUCH_DOWN:
337 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
340 if (device->abs.seat_slot != -1) {
341 log_bug_kernel(libinput,
342 "%s: Driver sent multiple touch down for the "
344 udev_device_get_devnode(device->udev_device));
348 seat_slot = ffs(~seat->slot_map) - 1;
349 device->abs.seat_slot = seat_slot;
354 seat->slot_map |= 1 << seat_slot;
356 point = device->abs.point;
357 transform_absolute(device, &point);
359 touch_notify_touch_down(base, time, -1, seat_slot, &point);
361 case EVDEV_ABSOLUTE_MOTION:
362 point = device->abs.point;
363 transform_absolute(device, &point);
365 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
366 seat_slot = device->abs.seat_slot;
371 touch_notify_touch_motion(base, time, -1, seat_slot,
373 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
374 pointer_notify_motion_absolute(base, time, &point);
377 case EVDEV_ABSOLUTE_TOUCH_UP:
378 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
381 seat_slot = device->abs.seat_slot;
382 device->abs.seat_slot = -1;
387 seat->slot_map &= ~(1 << seat_slot);
389 touch_notify_touch_up(base, time, -1, seat_slot);
392 assert(0 && "Unknown pending event type");
396 device->pending_event = EVDEV_NONE;
399 static enum evdev_key_type
400 get_key_type(uint16_t code)
402 if (code == BTN_TOUCH)
403 return EVDEV_KEY_TYPE_NONE;
405 if (code >= KEY_ESC && code <= KEY_MICMUTE)
406 return EVDEV_KEY_TYPE_KEY;
407 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
408 return EVDEV_KEY_TYPE_BUTTON;
409 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
410 return EVDEV_KEY_TYPE_KEY;
411 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
412 return EVDEV_KEY_TYPE_BUTTON;
413 return EVDEV_KEY_TYPE_NONE;
417 evdev_button_scroll_timeout(uint64_t time, void *data)
419 struct evdev_device *device = data;
421 device->scroll.button_scroll_active = true;
425 evdev_button_scroll_button(struct evdev_device *device,
426 uint64_t time, int is_press)
429 libinput_timer_set(&device->scroll.timer,
430 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
432 libinput_timer_cancel(&device->scroll.timer);
433 if (device->scroll.button_scroll_active) {
434 evdev_stop_scroll(device, time,
435 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS);
436 device->scroll.button_scroll_active = false;
438 /* If the button is released quickly enough emit the
439 * button press/release events. */
440 evdev_pointer_notify_button(device, time,
441 device->scroll.button,
442 LIBINPUT_BUTTON_STATE_PRESSED);
443 evdev_pointer_notify_button(device, time,
444 device->scroll.button,
445 LIBINPUT_BUTTON_STATE_RELEASED);
451 evdev_process_touch_button(struct evdev_device *device,
452 uint64_t time, int value)
454 if (device->pending_event != EVDEV_NONE &&
455 device->pending_event != EVDEV_ABSOLUTE_MOTION)
456 evdev_flush_pending_event(device, time);
458 device->pending_event = (value ?
459 EVDEV_ABSOLUTE_TOUCH_DOWN :
460 EVDEV_ABSOLUTE_TOUCH_UP);
464 evdev_process_key(struct evdev_device *device,
465 struct input_event *e, uint64_t time)
467 enum evdev_key_type type;
469 /* ignore kernel key repeat */
473 if (e->code == BTN_TOUCH) {
475 evdev_process_touch_button(device, time, e->value);
479 evdev_flush_pending_event(device, time);
481 type = get_key_type(e->code);
483 /* Ignore key release events from the kernel for keys that libinput
484 * never got a pressed event for. */
487 case EVDEV_KEY_TYPE_NONE:
489 case EVDEV_KEY_TYPE_KEY:
490 case EVDEV_KEY_TYPE_BUTTON:
491 if (!hw_is_key_down(device, e->code))
496 hw_set_key_down(device, e->code, e->value);
499 case EVDEV_KEY_TYPE_NONE:
501 case EVDEV_KEY_TYPE_KEY:
502 evdev_keyboard_notify_key(
506 e->value ? LIBINPUT_KEY_STATE_PRESSED :
507 LIBINPUT_KEY_STATE_RELEASED);
509 case EVDEV_KEY_TYPE_BUTTON:
510 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
511 e->code == device->scroll.button) {
512 evdev_button_scroll_button(device, time, e->value);
515 evdev_pointer_notify_button(
518 evdev_to_left_handed(device, e->code),
519 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
520 LIBINPUT_BUTTON_STATE_RELEASED);
526 evdev_process_touch(struct evdev_device *device,
527 struct input_event *e,
532 if ((size_t)e->value >= device->mt.slots_len) {
533 log_bug_libinput(device->base.seat->libinput,
534 "%s exceeds slots (%d of %d)\n",
537 device->mt.slots_len);
538 e->value = device->mt.slots_len - 1;
540 evdev_flush_pending_event(device, time);
541 device->mt.slot = e->value;
543 case ABS_MT_TRACKING_ID:
544 if (device->pending_event != EVDEV_NONE &&
545 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
546 evdev_flush_pending_event(device, time);
548 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
550 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
552 case ABS_MT_POSITION_X:
553 device->mt.slots[device->mt.slot].point.x = e->value;
554 if (device->pending_event == EVDEV_NONE)
555 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
557 case ABS_MT_POSITION_Y:
558 device->mt.slots[device->mt.slot].point.y = e->value;
559 if (device->pending_event == EVDEV_NONE)
560 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
566 evdev_process_absolute_motion(struct evdev_device *device,
567 struct input_event *e)
571 device->abs.point.x = e->value;
572 if (device->pending_event == EVDEV_NONE)
573 device->pending_event = EVDEV_ABSOLUTE_MOTION;
576 device->abs.point.y = e->value;
577 if (device->pending_event == EVDEV_NONE)
578 device->pending_event = EVDEV_ABSOLUTE_MOTION;
584 evdev_notify_axis(struct evdev_device *device,
587 enum libinput_pointer_axis_source source,
588 const struct normalized_coords *delta_in,
589 const struct discrete_coords *discrete_in)
591 struct normalized_coords delta = *delta_in;
592 struct discrete_coords discrete = *discrete_in;
594 if (device->scroll.natural_scrolling_enabled) {
601 pointer_notify_axis(&device->base,
610 evdev_process_relative(struct evdev_device *device,
611 struct input_event *e, uint64_t time)
613 struct normalized_coords wheel_degrees = { 0.0, 0.0 };
614 struct discrete_coords discrete = { 0.0, 0.0 };
618 if (device->pending_event != EVDEV_RELATIVE_MOTION)
619 evdev_flush_pending_event(device, time);
620 device->rel.x += e->value;
621 device->pending_event = EVDEV_RELATIVE_MOTION;
624 if (device->pending_event != EVDEV_RELATIVE_MOTION)
625 evdev_flush_pending_event(device, time);
626 device->rel.y += e->value;
627 device->pending_event = EVDEV_RELATIVE_MOTION;
630 evdev_flush_pending_event(device, time);
631 wheel_degrees.y = -1 * e->value *
632 device->scroll.wheel_click_angle;
633 discrete.y = -1 * e->value;
637 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
638 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
643 evdev_flush_pending_event(device, time);
644 wheel_degrees.x = e->value * device->scroll.wheel_click_angle;
645 discrete.x = e->value;
649 AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
650 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
658 evdev_process_absolute(struct evdev_device *device,
659 struct input_event *e,
663 evdev_process_touch(device, e, time);
665 evdev_process_absolute_motion(device, e);
670 evdev_any_button_down(struct evdev_device *device)
674 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
675 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
676 hw_is_key_down(device, button))
683 evdev_need_touch_frame(struct evdev_device *device)
685 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
688 switch (device->pending_event) {
690 case EVDEV_RELATIVE_MOTION:
692 case EVDEV_ABSOLUTE_MT_DOWN:
693 case EVDEV_ABSOLUTE_MT_MOTION:
694 case EVDEV_ABSOLUTE_MT_UP:
695 case EVDEV_ABSOLUTE_TOUCH_DOWN:
696 case EVDEV_ABSOLUTE_TOUCH_UP:
697 case EVDEV_ABSOLUTE_MOTION:
705 evdev_tag_external_mouse(struct evdev_device *device,
706 struct udev_device *udev_device)
710 bustype = libevdev_get_id_bustype(device->evdev);
711 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
712 if (device->seat_caps & EVDEV_DEVICE_POINTER)
713 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
718 evdev_tag_trackpoint(struct evdev_device *device,
719 struct udev_device *udev_device)
721 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
722 device->tags |= EVDEV_TAG_TRACKPOINT;
726 fallback_process(struct evdev_dispatch *dispatch,
727 struct evdev_device *device,
728 struct input_event *event,
731 bool need_frame = false;
733 switch (event->type) {
735 evdev_process_relative(device, event, time);
738 evdev_process_absolute(device, event, time);
741 evdev_process_key(device, event, time);
744 need_frame = evdev_need_touch_frame(device);
745 evdev_flush_pending_event(device, time);
747 touch_notify_frame(&device->base, time);
753 fallback_destroy(struct evdev_dispatch *dispatch)
759 fallback_tag_device(struct evdev_device *device,
760 struct udev_device *udev_device)
762 evdev_tag_external_mouse(device, udev_device);
763 evdev_tag_trackpoint(device, udev_device);
767 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
769 struct evdev_device *device = (struct evdev_device*)libinput_device;
771 return device->abs.absinfo_x && device->abs.absinfo_y;
774 static enum libinput_config_status
775 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
776 const float matrix[6])
778 struct evdev_device *device = (struct evdev_device*)libinput_device;
780 evdev_device_calibrate(device, matrix);
782 return LIBINPUT_CONFIG_STATUS_SUCCESS;
786 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
789 struct evdev_device *device = (struct evdev_device*)libinput_device;
791 matrix_to_farray6(&device->abs.usermatrix, matrix);
793 return !matrix_is_identity(&device->abs.usermatrix);
797 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
800 struct evdev_device *device = (struct evdev_device*)libinput_device;
802 matrix_to_farray6(&device->abs.default_calibration, matrix);
804 return !matrix_is_identity(&device->abs.default_calibration);
807 struct evdev_dispatch_interface fallback_interface = {
811 NULL, /* device_added */
812 NULL, /* device_removed */
813 NULL, /* device_suspended */
814 NULL, /* device_resumed */
819 evdev_sendevents_get_modes(struct libinput_device *device)
821 return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
824 static enum libinput_config_status
825 evdev_sendevents_set_mode(struct libinput_device *device,
826 enum libinput_config_send_events_mode mode)
828 struct evdev_device *evdev = (struct evdev_device*)device;
829 struct evdev_dispatch *dispatch = evdev->dispatch;
831 if (mode == dispatch->sendevents.current_mode)
832 return LIBINPUT_CONFIG_STATUS_SUCCESS;
835 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
836 evdev_device_resume(evdev);
838 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
839 evdev_device_suspend(evdev);
841 default: /* no support for combined modes yet */
842 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
845 dispatch->sendevents.current_mode = mode;
847 return LIBINPUT_CONFIG_STATUS_SUCCESS;
850 static enum libinput_config_send_events_mode
851 evdev_sendevents_get_mode(struct libinput_device *device)
853 struct evdev_device *evdev = (struct evdev_device*)device;
854 struct evdev_dispatch *dispatch = evdev->dispatch;
856 return dispatch->sendevents.current_mode;
859 static enum libinput_config_send_events_mode
860 evdev_sendevents_get_default_mode(struct libinput_device *device)
862 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
866 evdev_left_handed_has(struct libinput_device *device)
868 /* This is only hooked up when we have left-handed configuration, so we
869 * can hardcode 1 here */
874 evdev_change_to_left_handed(struct evdev_device *device)
876 if (device->left_handed.want_enabled == device->left_handed.enabled)
879 if (evdev_any_button_down(device))
882 device->left_handed.enabled = device->left_handed.want_enabled;
885 static enum libinput_config_status
886 evdev_left_handed_set(struct libinput_device *device, int left_handed)
888 struct evdev_device *evdev_device = (struct evdev_device *)device;
890 evdev_device->left_handed.want_enabled = left_handed ? true : false;
892 evdev_device->left_handed.change_to_enabled(evdev_device);
894 return LIBINPUT_CONFIG_STATUS_SUCCESS;
898 evdev_left_handed_get(struct libinput_device *device)
900 struct evdev_device *evdev_device = (struct evdev_device *)device;
902 /* return the wanted configuration, even if it hasn't taken
904 return evdev_device->left_handed.want_enabled;
908 evdev_left_handed_get_default(struct libinput_device *device)
914 evdev_init_left_handed(struct evdev_device *device,
915 void (*change_to_left_handed)(struct evdev_device *))
917 device->left_handed.config.has = evdev_left_handed_has;
918 device->left_handed.config.set = evdev_left_handed_set;
919 device->left_handed.config.get = evdev_left_handed_get;
920 device->left_handed.config.get_default = evdev_left_handed_get_default;
921 device->base.config.left_handed = &device->left_handed.config;
922 device->left_handed.enabled = false;
923 device->left_handed.want_enabled = false;
924 device->left_handed.change_to_enabled = change_to_left_handed;
930 evdev_scroll_get_methods(struct libinput_device *device)
932 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
936 evdev_change_scroll_method(struct evdev_device *device)
938 if (device->scroll.want_method == device->scroll.method &&
939 device->scroll.want_button == device->scroll.button)
942 if (evdev_any_button_down(device))
945 device->scroll.method = device->scroll.want_method;
946 device->scroll.button = device->scroll.want_button;
949 static enum libinput_config_status
950 evdev_scroll_set_method(struct libinput_device *device,
951 enum libinput_config_scroll_method method)
953 struct evdev_device *evdev = (struct evdev_device*)device;
955 evdev->scroll.want_method = method;
956 evdev->scroll.change_scroll_method(evdev);
958 return LIBINPUT_CONFIG_STATUS_SUCCESS;
961 static enum libinput_config_scroll_method
962 evdev_scroll_get_method(struct libinput_device *device)
964 struct evdev_device *evdev = (struct evdev_device *)device;
966 /* return the wanted configuration, even if it hasn't taken
968 return evdev->scroll.want_method;
971 static enum libinput_config_scroll_method
972 evdev_scroll_get_default_method(struct libinput_device *device)
974 struct evdev_device *evdev = (struct evdev_device *)device;
976 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
977 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
979 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
982 static enum libinput_config_status
983 evdev_scroll_set_button(struct libinput_device *device,
986 struct evdev_device *evdev = (struct evdev_device*)device;
988 evdev->scroll.want_button = button;
989 evdev->scroll.change_scroll_method(evdev);
991 return LIBINPUT_CONFIG_STATUS_SUCCESS;
995 evdev_scroll_get_button(struct libinput_device *device)
997 struct evdev_device *evdev = (struct evdev_device *)device;
999 /* return the wanted configuration, even if it hasn't taken
1001 return evdev->scroll.want_button;
1005 evdev_scroll_get_default_button(struct libinput_device *device)
1007 struct evdev_device *evdev = (struct evdev_device *)device;
1009 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
1016 evdev_init_button_scroll(struct evdev_device *device,
1017 void (*change_scroll_method)(struct evdev_device *))
1019 libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
1020 evdev_button_scroll_timeout, device);
1021 device->scroll.config.get_methods = evdev_scroll_get_methods;
1022 device->scroll.config.set_method = evdev_scroll_set_method;
1023 device->scroll.config.get_method = evdev_scroll_get_method;
1024 device->scroll.config.get_default_method = evdev_scroll_get_default_method;
1025 device->scroll.config.set_button = evdev_scroll_set_button;
1026 device->scroll.config.get_button = evdev_scroll_get_button;
1027 device->scroll.config.get_default_button = evdev_scroll_get_default_button;
1028 device->base.config.scroll_method = &device->scroll.config;
1029 device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
1030 device->scroll.want_method = device->scroll.method;
1031 device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
1032 device->scroll.want_button = device->scroll.button;
1033 device->scroll.change_scroll_method = change_scroll_method;
1039 evdev_init_calibration(struct evdev_device *device,
1040 struct evdev_dispatch *dispatch)
1042 device->base.config.calibration = &dispatch->calibration;
1044 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
1045 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
1046 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
1047 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
1051 evdev_init_sendevents(struct evdev_device *device,
1052 struct evdev_dispatch *dispatch)
1054 device->base.config.sendevents = &dispatch->sendevents.config;
1056 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1057 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
1058 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
1059 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
1060 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
1064 evdev_scroll_config_natural_has(struct libinput_device *device)
1069 static enum libinput_config_status
1070 evdev_scroll_config_natural_set(struct libinput_device *device,
1073 struct evdev_device *dev = (struct evdev_device *)device;
1075 dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1077 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1081 evdev_scroll_config_natural_get(struct libinput_device *device)
1083 struct evdev_device *dev = (struct evdev_device *)device;
1085 return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1089 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1091 /* could enable this on Apple touchpads. could do that, could
1092 * very well do that... */
1097 evdev_init_natural_scroll(struct evdev_device *device)
1099 device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1100 device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1101 device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1102 device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1103 device->scroll.natural_scrolling_enabled = false;
1104 device->base.config.natural_scroll = &device->scroll.config_natural;
1107 static struct evdev_dispatch *
1108 fallback_dispatch_create(struct libinput_device *device)
1110 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1111 struct evdev_device *evdev_device = (struct evdev_device *)device;
1113 if (dispatch == NULL)
1116 dispatch->interface = &fallback_interface;
1118 if (evdev_device->left_handed.want_enabled &&
1119 evdev_init_left_handed(evdev_device,
1120 evdev_change_to_left_handed) == -1) {
1125 if (evdev_device->scroll.want_button &&
1126 evdev_init_button_scroll(evdev_device,
1127 evdev_change_scroll_method) == -1) {
1132 if (evdev_device->scroll.natural_scrolling_enabled)
1133 evdev_init_natural_scroll(evdev_device);
1135 evdev_init_calibration(evdev_device, dispatch);
1136 evdev_init_sendevents(evdev_device, dispatch);
1142 evdev_process_event(struct evdev_device *device, struct input_event *e)
1144 struct evdev_dispatch *dispatch = device->dispatch;
1145 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1147 dispatch->interface->process(dispatch, device, e, time);
1151 evdev_device_dispatch_one(struct evdev_device *device,
1152 struct input_event *ev)
1154 if (!device->mtdev) {
1155 evdev_process_event(device, ev);
1157 mtdev_put_event(device->mtdev, ev);
1158 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1159 while (!mtdev_empty(device->mtdev)) {
1160 struct input_event e;
1161 mtdev_get_event(device->mtdev, &e);
1162 evdev_process_event(device, &e);
1169 evdev_sync_device(struct evdev_device *device)
1171 struct input_event ev;
1175 rc = libevdev_next_event(device->evdev,
1176 LIBEVDEV_READ_FLAG_SYNC, &ev);
1179 evdev_device_dispatch_one(device, &ev);
1180 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1182 return rc == -EAGAIN ? 0 : rc;
1186 evdev_device_dispatch(void *data)
1188 struct evdev_device *device = data;
1189 struct libinput *libinput = device->base.seat->libinput;
1190 struct input_event ev;
1193 /* If the compositor is repainting, this function is called only once
1194 * per frame and we have to process all the events available on the
1195 * fd, otherwise there will be input lag. */
1197 rc = libevdev_next_event(device->evdev,
1198 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1199 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1200 switch (ratelimit_test(&device->syn_drop_limit)) {
1201 case RATELIMIT_PASS:
1202 log_info(libinput, "SYN_DROPPED event from "
1203 "\"%s\" - some input events have "
1204 "been lost.\n", device->devname);
1206 case RATELIMIT_THRESHOLD:
1207 log_info(libinput, "SYN_DROPPED flood "
1211 case RATELIMIT_EXCEEDED:
1215 /* send one more sync event so we handle all
1216 currently pending events before we sync up
1217 to the current state */
1218 ev.code = SYN_REPORT;
1219 evdev_device_dispatch_one(device, &ev);
1221 rc = evdev_sync_device(device);
1223 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1224 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1225 evdev_device_dispatch_one(device, &ev);
1227 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1229 if (rc != -EAGAIN && rc != -EINTR) {
1230 libinput_remove_source(libinput, device->source);
1231 device->source = NULL;
1236 evdev_accel_config_available(struct libinput_device *device)
1238 /* this function is only called if we set up ptraccel, so we can
1239 reply with a resounding "Yes" */
1243 static enum libinput_config_status
1244 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1246 struct evdev_device *dev = (struct evdev_device *)device;
1248 if (!filter_set_speed(dev->pointer.filter, speed))
1249 return LIBINPUT_CONFIG_STATUS_INVALID;
1251 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1255 evdev_accel_config_get_speed(struct libinput_device *device)
1257 struct evdev_device *dev = (struct evdev_device *)device;
1259 return filter_get_speed(dev->pointer.filter);
1263 evdev_accel_config_get_default_speed(struct libinput_device *device)
1269 evdev_device_init_pointer_acceleration(struct evdev_device *device,
1270 accel_profile_func_t profile)
1272 device->pointer.filter = create_pointer_accelerator_filter(profile);
1273 if (!device->pointer.filter)
1276 device->pointer.config.available = evdev_accel_config_available;
1277 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1278 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1279 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1280 device->base.config.accel = &device->pointer.config;
1282 evdev_accel_config_set_speed(&device->base,
1283 evdev_accel_config_get_default_speed(&device->base));
1289 evdev_need_mtdev(struct evdev_device *device)
1291 struct libevdev *evdev = device->evdev;
1293 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1294 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1295 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1299 evdev_tag_device(struct evdev_device *device)
1301 if (device->dispatch->interface->tag_device)
1302 device->dispatch->interface->tag_device(device,
1303 device->udev_device);
1307 evdev_read_wheel_click_prop(struct evdev_device *device)
1309 struct libinput *libinput = device->base.seat->libinput;
1311 int angle = DEFAULT_WHEEL_CLICK_ANGLE;
1313 prop = udev_device_get_property_value(device->udev_device,
1314 "MOUSE_WHEEL_CLICK_ANGLE");
1316 angle = parse_mouse_wheel_click_angle_property(prop);
1319 "Mouse wheel click angle '%s' is present but invalid,"
1320 "using %d degrees instead\n",
1322 DEFAULT_WHEEL_CLICK_ANGLE);
1323 angle = DEFAULT_WHEEL_CLICK_ANGLE;
1330 evdev_read_dpi_prop(struct evdev_device *device)
1332 struct libinput *libinput = device->base.seat->libinput;
1333 const char *mouse_dpi;
1334 int dpi = DEFAULT_MOUSE_DPI;
1336 mouse_dpi = udev_device_get_property_value(device->udev_device,
1339 dpi = parse_mouse_dpi_property(mouse_dpi);
1341 log_error(libinput, "Mouse DPI property for '%s' is "
1342 "present but invalid, using %d "
1346 dpi = DEFAULT_MOUSE_DPI;
1353 /* Return 1 if the given resolutions have been set, or 0 otherwise */
1355 evdev_fix_abs_resolution(struct evdev_device *device,
1361 struct libinput *libinput = device->base.seat->libinput;
1362 struct libevdev *evdev = device->evdev;
1363 const struct input_absinfo *absx, *absy;
1364 struct input_absinfo fixed;
1367 if (!(xcode == ABS_X && ycode == ABS_Y) &&
1368 !(xcode == ABS_MT_POSITION_X && ycode == ABS_MT_POSITION_Y)) {
1369 log_bug_libinput(libinput,
1370 "Invalid x/y code combination %d/%d\n",
1375 if (xresolution == 0 || yresolution == 0 ||
1376 (xresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution) ||
1377 (yresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution)) {
1378 log_bug_libinput(libinput,
1379 "Invalid x/y resolutions %d/%d\n",
1380 xresolution, yresolution);
1384 absx = libevdev_get_abs_info(evdev, xcode);
1385 absy = libevdev_get_abs_info(evdev, ycode);
1387 if ((absx->resolution == 0 && absy->resolution != 0) ||
1388 (absx->resolution != 0 && absy->resolution == 0)) {
1389 log_bug_kernel(libinput,
1390 "Kernel has only x or y resolution, not both.\n");
1394 if (absx->resolution == 0 || absx->resolution == EVDEV_FAKE_RESOLUTION) {
1396 fixed.resolution = xresolution;
1397 /* libevdev_set_abs_info() changes the absinfo we already
1398 have a pointer to, no need to fetch it again */
1399 libevdev_set_abs_info(evdev, xcode, &fixed);
1403 if (absy->resolution == 0 || absy->resolution == EVDEV_FAKE_RESOLUTION) {
1405 fixed.resolution = yresolution;
1406 /* libevdev_set_abs_info() changes the absinfo we already
1407 have a pointer to, no need to fetch it again */
1408 libevdev_set_abs_info(evdev, ycode, &fixed);
1415 static enum evdev_device_udev_tags
1416 evdev_device_get_udev_tags(struct evdev_device *device,
1417 struct udev_device *udev_device)
1420 enum evdev_device_udev_tags tags = 0;
1421 const struct evdev_udev_tag_match *match;
1424 for (i = 0; i < 2 && udev_device; i++) {
1425 match = evdev_udev_tag_matches;
1426 while (match->name) {
1427 prop = udev_device_get_property_value(
1435 udev_device = udev_device_get_parent(udev_device);
1442 evdev_fix_android_mt(struct evdev_device *device)
1444 struct libevdev *evdev = device->evdev;
1446 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1447 libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1450 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1451 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1454 libevdev_set_abs_info(evdev, ABS_X,
1455 libevdev_get_abs_info(evdev, ABS_MT_POSITION_X));
1456 libevdev_set_abs_info(evdev, ABS_Y,
1457 libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y));
1461 evdev_reject_device(struct evdev_device *device)
1463 struct libevdev *evdev = device->evdev;
1465 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ^
1466 libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1469 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ^
1470 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1477 evdev_configure_mt_device(struct evdev_device *device)
1479 struct libevdev *evdev = device->evdev;
1480 struct mt_slot *slots;
1485 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1486 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1489 if (evdev_fix_abs_resolution(device,
1492 EVDEV_FAKE_RESOLUTION,
1493 EVDEV_FAKE_RESOLUTION))
1494 device->abs.fake_resolution = 1;
1496 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1497 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1500 /* We only handle the slotted Protocol B in libinput.
1501 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1502 require mtdev for conversion. */
1503 if (evdev_need_mtdev(device)) {
1504 device->mtdev = mtdev_new_open(device->fd);
1508 /* pick 10 slots as default for type A
1511 active_slot = device->mtdev->caps.slot.value;
1513 num_slots = libevdev_get_num_slots(device->evdev);
1514 active_slot = libevdev_get_current_slot(evdev);
1517 slots = calloc(num_slots, sizeof(struct mt_slot));
1521 for (slot = 0; slot < num_slots; ++slot) {
1522 slots[slot].seat_slot = -1;
1523 slots[slot].point.x = 0;
1524 slots[slot].point.y = 0;
1526 device->mt.slots = slots;
1527 device->mt.slots_len = num_slots;
1528 device->mt.slot = active_slot;
1534 evdev_configure_device(struct evdev_device *device)
1536 struct libinput *libinput = device->base.seat->libinput;
1537 struct libevdev *evdev = device->evdev;
1538 const char *devnode = udev_device_get_devnode(device->udev_device);
1539 enum evdev_device_udev_tags udev_tags;
1541 udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1543 if ((udev_tags & EVDEV_UDEV_TAG_INPUT) == 0 ||
1544 (udev_tags & ~EVDEV_UDEV_TAG_INPUT) == 0) {
1546 "input device '%s', %s not tagged as input device\n",
1547 device->devname, devnode);
1552 "input device '%s', %s is tagged by udev as:%s%s%s%s%s%s%s%s\n",
1553 device->devname, devnode,
1554 udev_tags & EVDEV_UDEV_TAG_KEYBOARD ? " Keyboard" : "",
1555 udev_tags & EVDEV_UDEV_TAG_MOUSE ? " Mouse" : "",
1556 udev_tags & EVDEV_UDEV_TAG_TOUCHPAD ? " Touchpad" : "",
1557 udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN ? " Touchscreen" : "",
1558 udev_tags & EVDEV_UDEV_TAG_TABLET ? " Tablet" : "",
1559 udev_tags & EVDEV_UDEV_TAG_JOYSTICK ? " Joystick" : "",
1560 udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER ? " Accelerometer" : "",
1561 udev_tags & EVDEV_UDEV_TAG_BUTTONSET ? " Buttonset" : "");
1563 /* libwacom *adds* TABLET, TOUCHPAD but leaves JOYSTICK in place, so
1564 make sure we only ignore real joystick devices */
1565 if ((udev_tags & EVDEV_UDEV_TAG_JOYSTICK) == udev_tags) {
1567 "input device '%s', %s is a joystick, ignoring\n",
1568 device->devname, devnode);
1572 /* libwacom assigns tablet _and_ tablet_pad to the pad devices */
1573 if (udev_tags & EVDEV_UDEV_TAG_BUTTONSET) {
1575 "input device '%s', %s is a buttonset, ignoring\n",
1576 device->devname, devnode);
1580 if (evdev_reject_device(device) == -1) {
1582 "input device '%s', %s was rejected.\n",
1583 device->devname, devnode);
1587 if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1588 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X)) {
1589 evdev_fix_android_mt(device);
1591 if (evdev_fix_abs_resolution(device,
1594 EVDEV_FAKE_RESOLUTION,
1595 EVDEV_FAKE_RESOLUTION))
1596 device->abs.fake_resolution = 1;
1597 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_X);
1598 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_Y);
1600 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1601 the limited ABS_* range - they aren't MT devices, they
1602 just have too many ABS_ axes */
1603 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1604 libevdev_get_num_slots(evdev) == -1) {
1605 udev_tags &= ~EVDEV_UDEV_TAG_TOUCHSCREEN;
1606 } else if (evdev_configure_mt_device(device) == -1) {
1611 if (udev_tags & EVDEV_UDEV_TAG_TOUCHPAD) {
1612 device->dispatch = evdev_mt_touchpad_create(device);
1614 "input device '%s', %s is a touchpad\n",
1615 device->devname, devnode);
1616 return device->dispatch == NULL ? -1 : 0;
1619 if (udev_tags & EVDEV_UDEV_TAG_MOUSE) {
1620 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
1621 !libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
1622 evdev_device_init_pointer_acceleration(
1624 pointer_accel_profile_linear) == -1)
1627 device->seat_caps |= EVDEV_DEVICE_POINTER;
1630 "input device '%s', %s is a pointer caps\n",
1631 device->devname, devnode);
1633 /* want left-handed config option */
1634 device->left_handed.want_enabled = true;
1635 /* want natural-scroll config option */
1636 device->scroll.natural_scrolling_enabled = true;
1637 /* want button scrolling config option */
1638 device->scroll.want_button = 1;
1641 if (udev_tags & EVDEV_UDEV_TAG_KEYBOARD) {
1642 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1644 "input device '%s', %s is a keyboard\n",
1645 device->devname, devnode);
1648 if (udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN) {
1649 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1651 "input device '%s', %s is a touch device\n",
1652 device->devname, devnode);
1659 evdev_notify_added_device(struct evdev_device *device)
1661 struct libinput_device *dev;
1663 list_for_each(dev, &device->base.seat->devices_list, link) {
1664 struct evdev_device *d = (struct evdev_device*)dev;
1665 if (dev == &device->base)
1668 /* Notify existing device d about addition of device device */
1669 if (d->dispatch->interface->device_added)
1670 d->dispatch->interface->device_added(d, device);
1672 /* Notify new device device about existing device d */
1673 if (device->dispatch->interface->device_added)
1674 device->dispatch->interface->device_added(device, d);
1676 /* Notify new device device if existing device d is suspended */
1677 if (d->suspended && device->dispatch->interface->device_suspended)
1678 device->dispatch->interface->device_suspended(device, d);
1681 notify_added_device(&device->base);
1685 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1687 struct udev *udev = udev_device_get_udev(udev_device);
1688 struct udev_device *udev_device_new = NULL;
1692 if (fstat(fd, &st) < 0)
1695 udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1696 if (!udev_device_new)
1699 rc = strcmp(udev_device_get_syspath(udev_device_new),
1700 udev_device_get_syspath(udev_device));
1702 if (udev_device_new)
1703 udev_device_unref(udev_device_new);
1708 evdev_set_device_group(struct evdev_device *device,
1709 struct udev_device *udev_device)
1711 struct libinput_device_group *group = NULL;
1712 const char *udev_group;
1714 udev_group = udev_device_get_property_value(udev_device,
1715 "LIBINPUT_DEVICE_GROUP");
1717 struct libinput_device *d;
1719 list_for_each(d, &device->base.seat->devices_list, link) {
1720 const char *identifier = d->group->identifier;
1723 strcmp(identifier, udev_group) == 0) {
1731 group = libinput_device_group_create(udev_group);
1734 libinput_device_set_device_group(&device->base, group);
1735 libinput_device_group_unref(group);
1737 libinput_device_set_device_group(&device->base, group);
1743 struct evdev_device *
1744 evdev_device_create(struct libinput_seat *seat,
1745 struct udev_device *udev_device)
1747 struct libinput *libinput = seat->libinput;
1748 struct evdev_device *device = NULL;
1751 int unhandled_device = 0;
1752 const char *devnode = udev_device_get_devnode(udev_device);
1754 /* Use non-blocking mode so that we can loop on read on
1755 * evdev_device_data() until all events on the fd are
1756 * read. mtdev_get() also expects this. */
1757 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1760 "opening input device '%s' failed (%s).\n",
1761 devnode, strerror(-fd));
1765 if (evdev_device_compare_syspath(udev_device, fd) != 0)
1768 device = zalloc(sizeof *device);
1772 libinput_device_init(&device->base, seat);
1773 libinput_seat_ref(seat);
1775 rc = libevdev_new_from_fd(fd, &device->evdev);
1779 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1781 device->seat_caps = 0;
1783 device->mtdev = NULL;
1784 device->udev_device = udev_device_ref(udev_device);
1787 device->abs.seat_slot = -1;
1788 device->dispatch = NULL;
1790 device->pending_event = EVDEV_NONE;
1791 device->devname = libevdev_get_name(device->evdev);
1792 device->scroll.threshold = 5.0; /* Default may be overridden */
1793 device->scroll.direction = 0;
1794 device->scroll.wheel_click_angle =
1795 evdev_read_wheel_click_prop(device);
1796 device->dpi = evdev_read_dpi_prop(device);
1797 /* at most 5 SYN_DROPPED log-messages per 30s */
1798 ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1800 matrix_init_identity(&device->abs.calibration);
1801 matrix_init_identity(&device->abs.usermatrix);
1802 matrix_init_identity(&device->abs.default_calibration);
1804 if (evdev_configure_device(device) == -1)
1807 if (device->seat_caps == 0) {
1808 unhandled_device = 1;
1812 /* If the dispatch was not set up use the fallback. */
1813 if (device->dispatch == NULL)
1814 device->dispatch = fallback_dispatch_create(&device->base);
1815 if (device->dispatch == NULL)
1819 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1820 if (!device->source)
1823 if (evdev_set_device_group(device, udev_device))
1826 list_insert(seat->devices_list.prev, &device->base.link);
1828 evdev_tag_device(device);
1829 evdev_notify_added_device(device);
1835 close_restricted(libinput, fd);
1837 evdev_device_destroy(device);
1839 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1843 evdev_device_get_output(struct evdev_device *device)
1845 return device->output_name;
1849 evdev_device_get_sysname(struct evdev_device *device)
1851 return udev_device_get_sysname(device->udev_device);
1855 evdev_device_get_name(struct evdev_device *device)
1857 return device->devname;
1861 evdev_device_get_id_product(struct evdev_device *device)
1863 return libevdev_get_id_product(device->evdev);
1867 evdev_device_get_id_vendor(struct evdev_device *device)
1869 return libevdev_get_id_vendor(device->evdev);
1872 struct udev_device *
1873 evdev_device_get_udev_device(struct evdev_device *device)
1875 return udev_device_ref(device->udev_device);
1879 evdev_device_set_default_calibration(struct evdev_device *device,
1880 const float calibration[6])
1882 matrix_from_farray6(&device->abs.default_calibration, calibration);
1883 evdev_device_calibrate(device, calibration);
1887 evdev_device_calibrate(struct evdev_device *device,
1888 const float calibration[6])
1890 struct matrix scale,
1895 matrix_from_farray6(&transform, calibration);
1896 device->abs.apply_calibration = !matrix_is_identity(&transform);
1898 if (!device->abs.apply_calibration) {
1899 matrix_init_identity(&device->abs.calibration);
1903 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1904 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1906 /* The transformation matrix is in the form:
1910 * Where a, e are the scale components, a, b, d, e are the rotation
1911 * component (combined with scale) and c and f are the translation
1912 * component. The translation component in the input matrix must be
1913 * normalized to multiples of the device width and height,
1914 * respectively. e.g. c == 1 shifts one device-width to the right.
1916 * We pre-calculate a single matrix to apply to event coordinates:
1917 * M = Un-Normalize * Calibration * Normalize
1919 * Normalize: scales the device coordinates to [0,1]
1920 * Calibration: user-supplied matrix
1921 * Un-Normalize: scales back up to device coordinates
1922 * Matrix maths requires the normalize/un-normalize in reverse
1926 /* back up the user matrix so we can return it on request */
1927 matrix_from_farray6(&device->abs.usermatrix, calibration);
1930 matrix_init_translate(&translate,
1931 device->abs.absinfo_x->minimum,
1932 device->abs.absinfo_y->minimum);
1933 matrix_init_scale(&scale, sx, sy);
1934 matrix_mult(&scale, &translate, &scale);
1937 matrix_mult(&transform, &scale, &transform);
1940 matrix_init_translate(&translate,
1941 -device->abs.absinfo_x->minimum/sx,
1942 -device->abs.absinfo_y->minimum/sy);
1943 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1944 matrix_mult(&scale, &translate, &scale);
1946 /* store final matrix in device */
1947 matrix_mult(&device->abs.calibration, &transform, &scale);
1951 evdev_device_has_capability(struct evdev_device *device,
1952 enum libinput_device_capability capability)
1954 switch (capability) {
1955 case LIBINPUT_DEVICE_CAP_POINTER:
1956 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1957 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1958 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1959 case LIBINPUT_DEVICE_CAP_TOUCH:
1960 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1967 evdev_device_get_size(struct evdev_device *device,
1971 const struct input_absinfo *x, *y;
1973 x = libevdev_get_abs_info(device->evdev, ABS_X);
1974 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1976 if (!x || !y || device->abs.fake_resolution ||
1977 !x->resolution || !y->resolution)
1980 *width = evdev_convert_to_mm(x, x->maximum);
1981 *height = evdev_convert_to_mm(y, y->maximum);
1987 evdev_device_has_button(struct evdev_device *device, uint32_t code)
1989 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
1992 return libevdev_has_event_code(device->evdev, EV_KEY, code);
1996 evdev_is_scrolling(const struct evdev_device *device,
1997 enum libinput_pointer_axis axis)
1999 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2000 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2002 return (device->scroll.direction & AS_MASK(axis)) != 0;
2006 evdev_start_scrolling(struct evdev_device *device,
2007 enum libinput_pointer_axis axis)
2009 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2010 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2012 device->scroll.direction |= AS_MASK(axis);
2016 evdev_post_scroll(struct evdev_device *device,
2018 enum libinput_pointer_axis_source source,
2019 const struct normalized_coords *delta)
2021 const struct normalized_coords *trigger;
2022 struct normalized_coords event;
2024 if (!evdev_is_scrolling(device,
2025 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2026 device->scroll.buildup.y += delta->y;
2027 if (!evdev_is_scrolling(device,
2028 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2029 device->scroll.buildup.x += delta->x;
2031 trigger = &device->scroll.buildup;
2033 /* If we're not scrolling yet, use a distance trigger: moving
2034 past a certain distance starts scrolling */
2035 if (!evdev_is_scrolling(device,
2036 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
2037 !evdev_is_scrolling(device,
2038 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2039 if (fabs(trigger->y) >= device->scroll.threshold)
2040 evdev_start_scrolling(device,
2041 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2042 if (fabs(trigger->x) >= device->scroll.threshold)
2043 evdev_start_scrolling(device,
2044 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2045 /* We're already scrolling in one direction. Require some
2046 trigger speed to start scrolling in the other direction */
2047 } else if (!evdev_is_scrolling(device,
2048 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2049 if (fabs(delta->y) >= device->scroll.threshold)
2050 evdev_start_scrolling(device,
2051 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2052 } else if (!evdev_is_scrolling(device,
2053 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
2054 if (fabs(delta->x) >= device->scroll.threshold)
2055 evdev_start_scrolling(device,
2056 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2061 /* We use the trigger to enable, but the delta from this event for
2062 * the actual scroll movement. Otherwise we get a jump once
2063 * scrolling engages */
2064 if (!evdev_is_scrolling(device,
2065 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2068 if (!evdev_is_scrolling(device,
2069 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2072 if (event.x != 0.0 || event.y != 0.0) {
2073 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2074 evdev_notify_axis(device,
2076 device->scroll.direction,
2084 evdev_stop_scroll(struct evdev_device *device,
2086 enum libinput_pointer_axis_source source)
2088 const struct normalized_coords zero = { 0.0, 0.0 };
2089 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2091 /* terminate scrolling with a zero scroll event */
2092 if (device->scroll.direction != 0)
2093 pointer_notify_axis(&device->base,
2095 device->scroll.direction,
2100 device->scroll.buildup.x = 0;
2101 device->scroll.buildup.y = 0;
2102 device->scroll.direction = 0;
2106 release_pressed_keys(struct evdev_device *device)
2108 struct libinput *libinput = device->base.seat->libinput;
2112 if ((time = libinput_now(libinput)) == 0)
2115 for (code = 0; code < KEY_CNT; code++) {
2116 int count = get_key_down_count(device, code);
2119 log_bug_libinput(libinput,
2120 "Key %d is down %d times.\n",
2125 while (get_key_down_count(device, code) > 0) {
2126 switch (get_key_type(code)) {
2127 case EVDEV_KEY_TYPE_NONE:
2129 case EVDEV_KEY_TYPE_KEY:
2130 evdev_keyboard_notify_key(
2134 LIBINPUT_KEY_STATE_RELEASED);
2136 case EVDEV_KEY_TYPE_BUTTON:
2137 evdev_pointer_notify_button(
2140 evdev_to_left_handed(device, code),
2141 LIBINPUT_BUTTON_STATE_RELEASED);
2149 evdev_notify_suspended_device(struct evdev_device *device)
2151 struct libinput_device *it;
2153 if (device->suspended)
2156 list_for_each(it, &device->base.seat->devices_list, link) {
2157 struct evdev_device *d = (struct evdev_device*)it;
2158 if (it == &device->base)
2161 if (d->dispatch->interface->device_suspended)
2162 d->dispatch->interface->device_suspended(d, device);
2165 device->suspended = 1;
2169 evdev_notify_resumed_device(struct evdev_device *device)
2171 struct libinput_device *it;
2173 if (!device->suspended)
2176 list_for_each(it, &device->base.seat->devices_list, link) {
2177 struct evdev_device *d = (struct evdev_device*)it;
2178 if (it == &device->base)
2181 if (d->dispatch->interface->device_resumed)
2182 d->dispatch->interface->device_resumed(d, device);
2185 device->suspended = 0;
2189 evdev_device_suspend(struct evdev_device *device)
2191 evdev_notify_suspended_device(device);
2193 if (device->source) {
2194 libinput_remove_source(device->base.seat->libinput,
2196 device->source = NULL;
2199 release_pressed_keys(device);
2201 if (device->mtdev) {
2202 mtdev_close_delete(device->mtdev);
2203 device->mtdev = NULL;
2206 if (device->fd != -1) {
2207 close_restricted(device->base.seat->libinput, device->fd);
2215 evdev_device_resume(struct evdev_device *device)
2217 struct libinput *libinput = device->base.seat->libinput;
2219 const char *devnode;
2220 struct input_event ev;
2221 enum libevdev_read_status status;
2223 if (device->fd != -1)
2226 if (device->was_removed)
2229 devnode = udev_device_get_devnode(device->udev_device);
2230 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
2235 if (evdev_device_compare_syspath(device->udev_device, fd)) {
2236 close_restricted(libinput, fd);
2242 if (evdev_need_mtdev(device)) {
2243 device->mtdev = mtdev_new_open(device->fd);
2248 libevdev_change_fd(device->evdev, fd);
2249 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
2251 /* re-sync libevdev's view of the device, but discard the actual
2252 events. Our device is in a neutral state already */
2253 libevdev_next_event(device->evdev,
2254 LIBEVDEV_READ_FLAG_FORCE_SYNC,
2257 status = libevdev_next_event(device->evdev,
2258 LIBEVDEV_READ_FLAG_SYNC,
2260 } while (status == LIBEVDEV_READ_STATUS_SYNC);
2263 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2264 if (!device->source) {
2265 mtdev_close_delete(device->mtdev);
2269 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2271 evdev_notify_resumed_device(device);
2277 evdev_device_remove(struct evdev_device *device)
2279 struct libinput_device *dev;
2281 list_for_each(dev, &device->base.seat->devices_list, link) {
2282 struct evdev_device *d = (struct evdev_device*)dev;
2283 if (dev == &device->base)
2286 if (d->dispatch->interface->device_removed)
2287 d->dispatch->interface->device_removed(d, device);
2290 evdev_device_suspend(device);
2292 if (device->dispatch->interface->remove)
2293 device->dispatch->interface->remove(device->dispatch);
2295 /* A device may be removed while suspended, mark it to
2296 * skip re-opening a different device with the same node */
2297 device->was_removed = true;
2299 list_remove(&device->base.link);
2301 notify_removed_device(&device->base);
2302 libinput_device_unref(&device->base);
2306 evdev_device_destroy(struct evdev_device *device)
2308 struct evdev_dispatch *dispatch;
2310 dispatch = device->dispatch;
2312 dispatch->interface->destroy(dispatch);
2314 if (device->base.group)
2315 libinput_device_group_unref(device->base.group);
2317 filter_destroy(device->pointer.filter);
2318 libinput_seat_unref(device->base.seat);
2319 libevdev_free(device->evdev);
2320 udev_device_unref(device->udev_device);
2321 free(device->mt.slots);