2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 #include "linux/input.h"
33 #include <mtdev-plumbing.h>
41 #include "libinput-private.h"
43 #define DEFAULT_AXIS_STEP_DISTANCE 10
44 #define DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT 200
45 /* The HW DPI rate we normalize to before calculating pointer acceleration */
46 #define DEFAULT_MOUSE_DPI 400
51 EVDEV_KEY_TYPE_BUTTON,
55 hw_set_key_down(struct evdev_device *device, int code, int pressed)
57 long_set_bit_state(device->hw_key_mask, code, pressed);
61 hw_is_key_down(struct evdev_device *device, int code)
63 return long_bit_is_set(device->hw_key_mask, code);
67 get_key_down_count(struct evdev_device *device, int code)
69 return device->key_count[code];
73 update_key_down_count(struct evdev_device *device, int code, int pressed)
76 assert(code >= 0 && code < KEY_CNT);
79 key_count = ++device->key_count[code];
81 assert(device->key_count[code] > 0);
82 key_count = --device->key_count[code];
86 log_bug_libinput(device->base.seat->libinput,
87 "Key count for %s reached abnormal values\n",
88 libevdev_event_code_get_name(EV_KEY, code));
95 evdev_keyboard_notify_key(struct evdev_device *device,
98 enum libinput_key_state state)
102 down_count = update_key_down_count(device, key, state);
104 if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
105 (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
106 keyboard_notify_key(&device->base, time, key, state);
110 evdev_pointer_notify_button(struct evdev_device *device,
113 enum libinput_button_state state)
117 down_count = update_key_down_count(device, button, state);
119 if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
120 (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
121 pointer_notify_button(&device->base, time, button, state);
123 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
124 device->buttons.change_to_left_handed)
125 device->buttons.change_to_left_handed(device);
127 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
128 device->scroll.change_scroll_method)
129 device->scroll.change_scroll_method(device);
135 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
137 static const struct {
138 enum libinput_led weston;
141 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
142 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
143 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
145 struct input_event ev[ARRAY_LENGTH(map) + 1];
148 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
151 memset(ev, 0, sizeof(ev));
152 for (i = 0; i < ARRAY_LENGTH(map); i++) {
154 ev[i].code = map[i].evdev;
155 ev[i].value = !!(leds & map[i].weston);
158 ev[i].code = SYN_REPORT;
160 i = write(device->fd, ev, sizeof ev);
161 (void)i; /* no, we really don't care about the return value */
165 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
167 if (!device->abs.apply_calibration)
170 matrix_mult_vec(&device->abs.calibration, x, y);
174 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
176 return (val - absinfo->minimum) * to_range /
177 (absinfo->maximum - absinfo->minimum + 1);
181 evdev_device_transform_x(struct evdev_device *device,
185 return scale_axis(device->abs.absinfo_x, x, width);
189 evdev_device_transform_y(struct evdev_device *device,
193 return scale_axis(device->abs.absinfo_y, y, height);
197 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
199 struct libinput *libinput = device->base.seat->libinput;
200 struct motion_params motion;
205 struct libinput_device *base = &device->base;
206 struct libinput_seat *seat = base->seat;
208 slot = device->mt.slot;
210 switch (device->pending_event) {
213 case EVDEV_RELATIVE_MOTION:
214 motion.dx = device->rel.dx / ((double)device->dpi / DEFAULT_MOUSE_DPI);
215 motion.dy = device->rel.dy / ((double)device->dpi / DEFAULT_MOUSE_DPI);
219 /* Use unaccelerated deltas for pointing stick scroll */
220 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
221 hw_is_key_down(device, device->scroll.button)) {
222 if (device->scroll.button_scroll_active)
223 evdev_post_scroll(device, time,
224 motion.dx, motion.dy);
228 /* Apply pointer acceleration. */
229 filter_dispatch(device->pointer.filter, &motion, device, time);
231 if (motion.dx == 0.0 && motion.dy == 0.0)
234 pointer_notify_motion(base, time, motion.dx, motion.dy);
236 case EVDEV_ABSOLUTE_MT_DOWN:
237 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
240 if (device->mt.slots[slot].seat_slot != -1) {
241 log_bug_kernel(libinput,
242 "%s: Driver sent multiple touch down for the "
244 udev_device_get_devnode(device->udev_device));
248 seat_slot = ffs(~seat->slot_map) - 1;
249 device->mt.slots[slot].seat_slot = seat_slot;
254 seat->slot_map |= 1 << seat_slot;
255 x = device->mt.slots[slot].x;
256 y = device->mt.slots[slot].y;
257 transform_absolute(device, &x, &y);
259 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
261 case EVDEV_ABSOLUTE_MT_MOTION:
262 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
265 seat_slot = device->mt.slots[slot].seat_slot;
266 x = device->mt.slots[slot].x;
267 y = device->mt.slots[slot].y;
272 transform_absolute(device, &x, &y);
273 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
275 case EVDEV_ABSOLUTE_MT_UP:
276 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
279 seat_slot = device->mt.slots[slot].seat_slot;
280 device->mt.slots[slot].seat_slot = -1;
285 seat->slot_map &= ~(1 << seat_slot);
287 touch_notify_touch_up(base, time, slot, seat_slot);
289 case EVDEV_ABSOLUTE_TOUCH_DOWN:
290 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
293 if (device->abs.seat_slot != -1) {
294 log_bug_kernel(libinput,
295 "%s: Driver sent multiple touch down for the "
297 udev_device_get_devnode(device->udev_device));
301 seat_slot = ffs(~seat->slot_map) - 1;
302 device->abs.seat_slot = seat_slot;
307 seat->slot_map |= 1 << seat_slot;
311 transform_absolute(device, &cx, &cy);
313 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
315 case EVDEV_ABSOLUTE_MOTION:
318 transform_absolute(device, &cx, &cy);
322 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
323 seat_slot = device->abs.seat_slot;
328 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
329 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
330 pointer_notify_motion_absolute(base, time, x, y);
333 case EVDEV_ABSOLUTE_TOUCH_UP:
334 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
337 seat_slot = device->abs.seat_slot;
338 device->abs.seat_slot = -1;
343 seat->slot_map &= ~(1 << seat_slot);
345 touch_notify_touch_up(base, time, -1, seat_slot);
348 assert(0 && "Unknown pending event type");
352 device->pending_event = EVDEV_NONE;
355 static enum evdev_key_type
356 get_key_type(uint16_t code)
358 if (code == BTN_TOUCH)
359 return EVDEV_KEY_TYPE_NONE;
361 if (code >= KEY_ESC && code <= KEY_MICMUTE)
362 return EVDEV_KEY_TYPE_KEY;
363 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
364 return EVDEV_KEY_TYPE_BUTTON;
365 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
366 return EVDEV_KEY_TYPE_KEY;
367 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
368 return EVDEV_KEY_TYPE_BUTTON;
369 return EVDEV_KEY_TYPE_NONE;
373 evdev_button_scroll_timeout(uint64_t time, void *data)
375 struct evdev_device *device = data;
377 device->scroll.button_scroll_active = true;
381 evdev_button_scroll_button(struct evdev_device *device,
382 uint64_t time, int is_press)
385 libinput_timer_set(&device->scroll.timer,
386 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
388 libinput_timer_cancel(&device->scroll.timer);
389 if (device->scroll.button_scroll_active) {
390 evdev_stop_scroll(device, time);
391 device->scroll.button_scroll_active = false;
393 /* If the button is released quickly enough emit the
394 * button press/release events. */
395 evdev_pointer_notify_button(device, time,
396 device->scroll.button,
397 LIBINPUT_BUTTON_STATE_PRESSED);
398 evdev_pointer_notify_button(device, time,
399 device->scroll.button,
400 LIBINPUT_BUTTON_STATE_RELEASED);
406 evdev_process_touch_button(struct evdev_device *device,
407 uint64_t time, int value)
409 if (device->pending_event != EVDEV_NONE &&
410 device->pending_event != EVDEV_ABSOLUTE_MOTION)
411 evdev_flush_pending_event(device, time);
413 device->pending_event = (value ?
414 EVDEV_ABSOLUTE_TOUCH_DOWN :
415 EVDEV_ABSOLUTE_TOUCH_UP);
419 evdev_process_key(struct evdev_device *device,
420 struct input_event *e, uint64_t time)
422 enum evdev_key_type type;
424 /* ignore kernel key repeat */
428 if (e->code == BTN_TOUCH) {
430 evdev_process_touch_button(device, time, e->value);
434 evdev_flush_pending_event(device, time);
436 type = get_key_type(e->code);
438 /* Ignore key release events from the kernel for keys that libinput
439 * never got a pressed event for. */
442 case EVDEV_KEY_TYPE_NONE:
444 case EVDEV_KEY_TYPE_KEY:
445 case EVDEV_KEY_TYPE_BUTTON:
446 if (!hw_is_key_down(device, e->code))
451 hw_set_key_down(device, e->code, e->value);
454 case EVDEV_KEY_TYPE_NONE:
456 case EVDEV_KEY_TYPE_KEY:
457 evdev_keyboard_notify_key(
461 e->value ? LIBINPUT_KEY_STATE_PRESSED :
462 LIBINPUT_KEY_STATE_RELEASED);
464 case EVDEV_KEY_TYPE_BUTTON:
465 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
466 e->code == device->scroll.button) {
467 evdev_button_scroll_button(device, time, e->value);
470 evdev_pointer_notify_button(
473 evdev_to_left_handed(device, e->code),
474 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
475 LIBINPUT_BUTTON_STATE_RELEASED);
481 evdev_process_touch(struct evdev_device *device,
482 struct input_event *e,
487 evdev_flush_pending_event(device, time);
488 device->mt.slot = e->value;
490 case ABS_MT_TRACKING_ID:
491 if (device->pending_event != EVDEV_NONE &&
492 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
493 evdev_flush_pending_event(device, time);
495 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
497 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
499 case ABS_MT_POSITION_X:
500 device->mt.slots[device->mt.slot].x = e->value;
501 if (device->pending_event == EVDEV_NONE)
502 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
504 case ABS_MT_POSITION_Y:
505 device->mt.slots[device->mt.slot].y = e->value;
506 if (device->pending_event == EVDEV_NONE)
507 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
513 evdev_process_absolute_motion(struct evdev_device *device,
514 struct input_event *e)
518 device->abs.x = e->value;
519 if (device->pending_event == EVDEV_NONE)
520 device->pending_event = EVDEV_ABSOLUTE_MOTION;
523 device->abs.y = e->value;
524 if (device->pending_event == EVDEV_NONE)
525 device->pending_event = EVDEV_ABSOLUTE_MOTION;
531 evdev_notify_axis(struct evdev_device *device,
533 enum libinput_pointer_axis axis,
536 if (device->scroll.natural_scrolling_enabled)
539 pointer_notify_axis(&device->base,
546 evdev_process_relative(struct evdev_device *device,
547 struct input_event *e, uint64_t time)
551 if (device->pending_event != EVDEV_RELATIVE_MOTION)
552 evdev_flush_pending_event(device, time);
553 device->rel.dx += e->value;
554 device->pending_event = EVDEV_RELATIVE_MOTION;
557 if (device->pending_event != EVDEV_RELATIVE_MOTION)
558 evdev_flush_pending_event(device, time);
559 device->rel.dy += e->value;
560 device->pending_event = EVDEV_RELATIVE_MOTION;
563 evdev_flush_pending_event(device, time);
567 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
568 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
571 evdev_flush_pending_event(device, time);
575 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
576 e->value * DEFAULT_AXIS_STEP_DISTANCE);
582 evdev_process_absolute(struct evdev_device *device,
583 struct input_event *e,
587 evdev_process_touch(device, e, time);
589 evdev_process_absolute_motion(device, e);
594 evdev_any_button_down(struct evdev_device *device)
598 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
599 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
600 hw_is_key_down(device, button))
607 evdev_need_touch_frame(struct evdev_device *device)
609 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
612 switch (device->pending_event) {
614 case EVDEV_RELATIVE_MOTION:
616 case EVDEV_ABSOLUTE_MT_DOWN:
617 case EVDEV_ABSOLUTE_MT_MOTION:
618 case EVDEV_ABSOLUTE_MT_UP:
619 case EVDEV_ABSOLUTE_TOUCH_DOWN:
620 case EVDEV_ABSOLUTE_TOUCH_UP:
621 case EVDEV_ABSOLUTE_MOTION:
629 evdev_tag_external_mouse(struct evdev_device *device,
630 struct udev_device *udev_device)
634 bustype = libevdev_get_id_bustype(device->evdev);
635 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
636 if (device->seat_caps & EVDEV_DEVICE_POINTER)
637 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
642 evdev_tag_trackpoint(struct evdev_device *device,
643 struct udev_device *udev_device)
645 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
646 device->tags |= EVDEV_TAG_TRACKPOINT;
650 fallback_process(struct evdev_dispatch *dispatch,
651 struct evdev_device *device,
652 struct input_event *event,
655 bool need_frame = false;
657 switch (event->type) {
659 evdev_process_relative(device, event, time);
662 evdev_process_absolute(device, event, time);
665 evdev_process_key(device, event, time);
668 need_frame = evdev_need_touch_frame(device);
669 evdev_flush_pending_event(device, time);
671 touch_notify_frame(&device->base, time);
677 fallback_destroy(struct evdev_dispatch *dispatch)
683 fallback_tag_device(struct evdev_device *device,
684 struct udev_device *udev_device)
686 evdev_tag_external_mouse(device, udev_device);
687 evdev_tag_trackpoint(device, udev_device);
691 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
693 struct evdev_device *device = (struct evdev_device*)libinput_device;
695 return device->abs.absinfo_x && device->abs.absinfo_y;
698 static enum libinput_config_status
699 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
700 const float matrix[6])
702 struct evdev_device *device = (struct evdev_device*)libinput_device;
704 evdev_device_calibrate(device, matrix);
706 return LIBINPUT_CONFIG_STATUS_SUCCESS;
710 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
713 struct evdev_device *device = (struct evdev_device*)libinput_device;
715 matrix_to_farray6(&device->abs.usermatrix, matrix);
717 return !matrix_is_identity(&device->abs.usermatrix);
721 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
724 struct evdev_device *device = (struct evdev_device*)libinput_device;
726 matrix_to_farray6(&device->abs.default_calibration, matrix);
728 return !matrix_is_identity(&device->abs.default_calibration);
731 struct evdev_dispatch_interface fallback_interface = {
734 NULL, /* device_added */
735 NULL, /* device_removed */
736 NULL, /* device_suspended */
737 NULL, /* device_resumed */
742 evdev_sendevents_get_modes(struct libinput_device *device)
744 return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
747 static enum libinput_config_status
748 evdev_sendevents_set_mode(struct libinput_device *device,
749 enum libinput_config_send_events_mode mode)
751 struct evdev_device *evdev = (struct evdev_device*)device;
752 struct evdev_dispatch *dispatch = evdev->dispatch;
754 if (mode == dispatch->sendevents.current_mode)
755 return LIBINPUT_CONFIG_STATUS_SUCCESS;
758 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
759 evdev_device_resume(evdev);
761 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
762 evdev_device_suspend(evdev);
764 default: /* no support for combined modes yet */
765 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
768 dispatch->sendevents.current_mode = mode;
770 return LIBINPUT_CONFIG_STATUS_SUCCESS;
773 static enum libinput_config_send_events_mode
774 evdev_sendevents_get_mode(struct libinput_device *device)
776 struct evdev_device *evdev = (struct evdev_device*)device;
777 struct evdev_dispatch *dispatch = evdev->dispatch;
779 return dispatch->sendevents.current_mode;
782 static enum libinput_config_send_events_mode
783 evdev_sendevents_get_default_mode(struct libinput_device *device)
785 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
789 evdev_left_handed_has(struct libinput_device *device)
791 /* This is only hooked up when we have left-handed configuration, so we
792 * can hardcode 1 here */
797 evdev_change_to_left_handed(struct evdev_device *device)
799 if (device->buttons.want_left_handed == device->buttons.left_handed)
802 if (evdev_any_button_down(device))
805 device->buttons.left_handed = device->buttons.want_left_handed;
808 static enum libinput_config_status
809 evdev_left_handed_set(struct libinput_device *device, int left_handed)
811 struct evdev_device *evdev_device = (struct evdev_device *)device;
813 evdev_device->buttons.want_left_handed = left_handed ? true : false;
815 evdev_device->buttons.change_to_left_handed(evdev_device);
817 return LIBINPUT_CONFIG_STATUS_SUCCESS;
821 evdev_left_handed_get(struct libinput_device *device)
823 struct evdev_device *evdev_device = (struct evdev_device *)device;
825 /* return the wanted configuration, even if it hasn't taken
827 return evdev_device->buttons.want_left_handed;
831 evdev_left_handed_get_default(struct libinput_device *device)
837 evdev_init_left_handed(struct evdev_device *device,
838 void (*change_to_left_handed)(struct evdev_device *))
840 device->buttons.config_left_handed.has = evdev_left_handed_has;
841 device->buttons.config_left_handed.set = evdev_left_handed_set;
842 device->buttons.config_left_handed.get = evdev_left_handed_get;
843 device->buttons.config_left_handed.get_default = evdev_left_handed_get_default;
844 device->base.config.left_handed = &device->buttons.config_left_handed;
845 device->buttons.left_handed = false;
846 device->buttons.want_left_handed = false;
847 device->buttons.change_to_left_handed = change_to_left_handed;
853 evdev_scroll_get_methods(struct libinput_device *device)
855 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
859 evdev_change_scroll_method(struct evdev_device *device)
861 if (device->scroll.want_method == device->scroll.method &&
862 device->scroll.want_button == device->scroll.button)
865 if (evdev_any_button_down(device))
868 device->scroll.method = device->scroll.want_method;
869 device->scroll.button = device->scroll.want_button;
872 static enum libinput_config_status
873 evdev_scroll_set_method(struct libinput_device *device,
874 enum libinput_config_scroll_method method)
876 struct evdev_device *evdev = (struct evdev_device*)device;
878 evdev->scroll.want_method = method;
879 evdev->scroll.change_scroll_method(evdev);
881 return LIBINPUT_CONFIG_STATUS_SUCCESS;
884 static enum libinput_config_scroll_method
885 evdev_scroll_get_method(struct libinput_device *device)
887 struct evdev_device *evdev = (struct evdev_device *)device;
889 /* return the wanted configuration, even if it hasn't taken
891 return evdev->scroll.want_method;
894 static enum libinput_config_scroll_method
895 evdev_scroll_get_default_method(struct libinput_device *device)
897 struct evdev_device *evdev = (struct evdev_device *)device;
899 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
900 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
902 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
905 static enum libinput_config_status
906 evdev_scroll_set_button(struct libinput_device *device,
909 struct evdev_device *evdev = (struct evdev_device*)device;
911 evdev->scroll.want_button = button;
912 evdev->scroll.change_scroll_method(evdev);
914 return LIBINPUT_CONFIG_STATUS_SUCCESS;
918 evdev_scroll_get_button(struct libinput_device *device)
920 struct evdev_device *evdev = (struct evdev_device *)device;
922 /* return the wanted configuration, even if it hasn't taken
924 return evdev->scroll.want_button;
928 evdev_scroll_get_default_button(struct libinput_device *device)
930 struct evdev_device *evdev = (struct evdev_device *)device;
932 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
939 evdev_init_button_scroll(struct evdev_device *device,
940 void (*change_scroll_method)(struct evdev_device *))
942 libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
943 evdev_button_scroll_timeout, device);
944 device->scroll.config.get_methods = evdev_scroll_get_methods;
945 device->scroll.config.set_method = evdev_scroll_set_method;
946 device->scroll.config.get_method = evdev_scroll_get_method;
947 device->scroll.config.get_default_method = evdev_scroll_get_default_method;
948 device->scroll.config.set_button = evdev_scroll_set_button;
949 device->scroll.config.get_button = evdev_scroll_get_button;
950 device->scroll.config.get_default_button = evdev_scroll_get_default_button;
951 device->base.config.scroll_method = &device->scroll.config;
952 device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
953 device->scroll.want_method = device->scroll.method;
954 device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
955 device->scroll.want_button = device->scroll.button;
956 device->scroll.change_scroll_method = change_scroll_method;
962 evdev_init_calibration(struct evdev_device *device,
963 struct evdev_dispatch *dispatch)
965 device->base.config.calibration = &dispatch->calibration;
967 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
968 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
969 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
970 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
974 evdev_init_sendevents(struct evdev_device *device,
975 struct evdev_dispatch *dispatch)
977 device->base.config.sendevents = &dispatch->sendevents.config;
979 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
980 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
981 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
982 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
983 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
987 evdev_scroll_config_natural_has(struct libinput_device *device)
992 static enum libinput_config_status
993 evdev_scroll_config_natural_set(struct libinput_device *device,
996 struct evdev_device *dev = (struct evdev_device *)device;
998 dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1000 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1004 evdev_scroll_config_natural_get(struct libinput_device *device)
1006 struct evdev_device *dev = (struct evdev_device *)device;
1008 return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1012 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1014 /* could enable this on Apple touchpads. could do that, could
1015 * very well do that... */
1020 evdev_init_natural_scroll(struct evdev_device *device)
1022 device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1023 device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1024 device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1025 device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1026 device->scroll.natural_scrolling_enabled = false;
1027 device->base.config.natural_scroll = &device->scroll.config_natural;
1030 static struct evdev_dispatch *
1031 fallback_dispatch_create(struct libinput_device *device)
1033 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1034 struct evdev_device *evdev_device = (struct evdev_device *)device;
1036 if (dispatch == NULL)
1039 dispatch->interface = &fallback_interface;
1041 if (evdev_device->buttons.want_left_handed &&
1042 evdev_init_left_handed(evdev_device,
1043 evdev_change_to_left_handed) == -1) {
1048 if (evdev_device->scroll.want_button &&
1049 evdev_init_button_scroll(evdev_device,
1050 evdev_change_scroll_method) == -1) {
1055 if (evdev_device->scroll.natural_scrolling_enabled)
1056 evdev_init_natural_scroll(evdev_device);
1058 evdev_init_calibration(evdev_device, dispatch);
1059 evdev_init_sendevents(evdev_device, dispatch);
1065 evdev_process_event(struct evdev_device *device, struct input_event *e)
1067 struct evdev_dispatch *dispatch = device->dispatch;
1068 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1070 dispatch->interface->process(dispatch, device, e, time);
1074 evdev_device_dispatch_one(struct evdev_device *device,
1075 struct input_event *ev)
1077 if (!device->mtdev) {
1078 evdev_process_event(device, ev);
1080 mtdev_put_event(device->mtdev, ev);
1081 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1082 while (!mtdev_empty(device->mtdev)) {
1083 struct input_event e;
1084 mtdev_get_event(device->mtdev, &e);
1085 evdev_process_event(device, &e);
1092 evdev_sync_device(struct evdev_device *device)
1094 struct input_event ev;
1098 rc = libevdev_next_event(device->evdev,
1099 LIBEVDEV_READ_FLAG_SYNC, &ev);
1102 evdev_device_dispatch_one(device, &ev);
1103 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1105 return rc == -EAGAIN ? 0 : rc;
1109 evdev_device_dispatch(void *data)
1111 struct evdev_device *device = data;
1112 struct libinput *libinput = device->base.seat->libinput;
1113 struct input_event ev;
1116 /* If the compositor is repainting, this function is called only once
1117 * per frame and we have to process all the events available on the
1118 * fd, otherwise there will be input lag. */
1120 rc = libevdev_next_event(device->evdev,
1121 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1122 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1123 switch (ratelimit_test(&device->syn_drop_limit)) {
1124 case RATELIMIT_PASS:
1125 log_info(libinput, "SYN_DROPPED event from "
1126 "\"%s\" - some input events have "
1127 "been lost.\n", device->devname);
1129 case RATELIMIT_THRESHOLD:
1130 log_info(libinput, "SYN_DROPPED flood "
1134 case RATELIMIT_EXCEEDED:
1138 /* send one more sync event so we handle all
1139 currently pending events before we sync up
1140 to the current state */
1141 ev.code = SYN_REPORT;
1142 evdev_device_dispatch_one(device, &ev);
1144 rc = evdev_sync_device(device);
1146 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1147 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1148 evdev_device_dispatch_one(device, &ev);
1150 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1152 if (rc != -EAGAIN && rc != -EINTR) {
1153 libinput_remove_source(libinput, device->source);
1154 device->source = NULL;
1159 evdev_accel_config_available(struct libinput_device *device)
1161 /* this function is only called if we set up ptraccel, so we can
1162 reply with a resounding "Yes" */
1166 static enum libinput_config_status
1167 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1169 struct evdev_device *dev = (struct evdev_device *)device;
1171 if (!filter_set_speed(dev->pointer.filter, speed))
1172 return LIBINPUT_CONFIG_STATUS_INVALID;
1174 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1178 evdev_accel_config_get_speed(struct libinput_device *device)
1180 struct evdev_device *dev = (struct evdev_device *)device;
1182 return filter_get_speed(dev->pointer.filter);
1186 evdev_accel_config_get_default_speed(struct libinput_device *device)
1192 evdev_device_init_pointer_acceleration(struct evdev_device *device)
1194 device->pointer.filter =
1195 create_pointer_accelerator_filter(
1196 pointer_accel_profile_linear);
1197 if (!device->pointer.filter)
1200 device->pointer.config.available = evdev_accel_config_available;
1201 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1202 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1203 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1204 device->base.config.accel = &device->pointer.config;
1211 evdev_need_mtdev(struct evdev_device *device)
1213 struct libevdev *evdev = device->evdev;
1215 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1216 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1217 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1221 evdev_tag_device(struct evdev_device *device)
1223 if (device->dispatch->interface->tag_device)
1224 device->dispatch->interface->tag_device(device,
1225 device->udev_device);
1229 evdev_fix_abs_resolution(struct libevdev *evdev,
1231 const struct input_absinfo *absinfo)
1233 struct input_absinfo fixed;
1235 if (absinfo->resolution == 0) {
1237 fixed.resolution = 1;
1238 /* libevdev_set_abs_info() changes the absinfo we already
1239 have a pointer to, no need to fetch it again */
1240 libevdev_set_abs_info(evdev, code, &fixed);
1248 evdev_configure_device(struct evdev_device *device)
1250 struct libinput *libinput = device->base.seat->libinput;
1251 struct libevdev *evdev = device->evdev;
1252 const struct input_absinfo *absinfo;
1253 int has_abs, has_rel, has_mt;
1254 int has_button, has_keyboard, has_touch;
1255 struct mt_slot *slots;
1260 const char *devnode = udev_device_get_devnode(device->udev_device);
1269 if (libevdev_has_event_type(evdev, EV_ABS)) {
1271 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1272 if (evdev_fix_abs_resolution(evdev,
1275 device->abs.fake_resolution = 1;
1276 device->abs.absinfo_x = absinfo;
1279 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1280 if (evdev_fix_abs_resolution(evdev,
1283 device->abs.fake_resolution = 1;
1284 device->abs.absinfo_y = absinfo;
1288 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1289 the limited ABS_* range - they aren't MT devices, they
1290 just have too many ABS_ axes */
1291 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1292 libevdev_get_num_slots(evdev) == -1) {
1295 } else if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1296 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1297 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1298 if (evdev_fix_abs_resolution(evdev,
1301 device->abs.fake_resolution = 1;
1302 device->abs.absinfo_x = absinfo;
1304 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1305 if (evdev_fix_abs_resolution(evdev,
1308 device->abs.fake_resolution = 1;
1309 device->abs.absinfo_y = absinfo;
1314 /* We only handle the slotted Protocol B in libinput.
1315 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1316 require mtdev for conversion. */
1317 if (evdev_need_mtdev(device)) {
1318 device->mtdev = mtdev_new_open(device->fd);
1322 num_slots = device->mtdev->caps.slot.maximum;
1323 if (device->mtdev->caps.slot.minimum < 0 ||
1326 active_slot = device->mtdev->caps.slot.value;
1328 num_slots = libevdev_get_num_slots(device->evdev);
1329 active_slot = libevdev_get_current_slot(evdev);
1332 slots = calloc(num_slots, sizeof(struct mt_slot));
1336 for (slot = 0; slot < num_slots; ++slot) {
1337 slots[slot].seat_slot = -1;
1341 device->mt.slots = slots;
1342 device->mt.slots_len = num_slots;
1343 device->mt.slot = active_slot;
1347 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1348 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1351 if (libevdev_has_event_type(evdev, EV_KEY)) {
1352 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1353 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1354 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1355 (has_abs || has_mt)) {
1356 device->dispatch = evdev_mt_touchpad_create(device);
1358 "input device '%s', %s is a touchpad\n",
1359 device->devname, devnode);
1360 return device->dispatch == NULL ? -1 : 0;
1363 for (i = 0; i < KEY_MAX; i++) {
1364 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1365 switch (get_key_type(i)) {
1366 case EVDEV_KEY_TYPE_NONE:
1368 case EVDEV_KEY_TYPE_KEY:
1371 case EVDEV_KEY_TYPE_BUTTON:
1378 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1381 if (libevdev_has_event_type(evdev, EV_LED))
1384 if ((has_abs || has_rel) && has_button) {
1385 if (evdev_device_init_pointer_acceleration(device) == -1)
1388 device->seat_caps |= EVDEV_DEVICE_POINTER;
1391 "input device '%s', %s is a pointer caps =%s%s%s\n",
1392 device->devname, devnode,
1393 has_abs ? " absolute-motion" : "",
1394 has_rel ? " relative-motion": "",
1395 has_button ? " button" : "");
1397 /* want left-handed config option */
1398 device->buttons.want_left_handed = true;
1399 /* want natural-scroll config option */
1400 device->scroll.natural_scrolling_enabled = true;
1403 if (has_rel && has_button) {
1404 /* want button scrolling config option */
1405 device->scroll.want_button = 1;
1409 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1411 "input device '%s', %s is a keyboard\n",
1412 device->devname, devnode);
1414 if (has_touch && !has_button) {
1415 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1417 "input device '%s', %s is a touch device\n",
1418 device->devname, devnode);
1425 evdev_notify_added_device(struct evdev_device *device)
1427 struct libinput_device *dev;
1429 list_for_each(dev, &device->base.seat->devices_list, link) {
1430 struct evdev_device *d = (struct evdev_device*)dev;
1431 if (dev == &device->base)
1434 /* Notify existing device d about addition of device device */
1435 if (d->dispatch->interface->device_added)
1436 d->dispatch->interface->device_added(d, device);
1438 /* Notify new device device about existing device d */
1439 if (device->dispatch->interface->device_added)
1440 device->dispatch->interface->device_added(device, d);
1442 /* Notify new device device if existing device d is suspended */
1443 if (d->suspended && device->dispatch->interface->device_suspended)
1444 device->dispatch->interface->device_suspended(device, d);
1447 notify_added_device(&device->base);
1451 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1453 struct udev *udev = udev_device_get_udev(udev_device);
1454 struct udev_device *udev_device_new;
1458 if (fstat(fd, &st) < 0)
1461 udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1462 if (!udev_device_new)
1465 rc = strcmp(udev_device_get_syspath(udev_device_new),
1466 udev_device_get_syspath(udev_device));
1468 if (udev_device_new)
1469 udev_device_unref(udev_device_new);
1473 struct evdev_device *
1474 evdev_device_create(struct libinput_seat *seat,
1475 struct udev_device *udev_device)
1477 struct libinput *libinput = seat->libinput;
1478 struct evdev_device *device = NULL;
1481 int unhandled_device = 0;
1482 const char *devnode = udev_device_get_devnode(udev_device);
1484 /* Use non-blocking mode so that we can loop on read on
1485 * evdev_device_data() until all events on the fd are
1486 * read. mtdev_get() also expects this. */
1487 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1490 "opening input device '%s' failed (%s).\n",
1491 devnode, strerror(-fd));
1495 if (evdev_device_compare_syspath(udev_device, fd) != 0)
1498 device = zalloc(sizeof *device);
1502 libinput_device_init(&device->base, seat);
1503 libinput_seat_ref(seat);
1505 rc = libevdev_new_from_fd(fd, &device->evdev);
1509 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1511 device->seat_caps = 0;
1513 device->mtdev = NULL;
1514 device->udev_device = udev_device_ref(udev_device);
1517 device->abs.seat_slot = -1;
1518 device->dispatch = NULL;
1520 device->pending_event = EVDEV_NONE;
1521 device->devname = libevdev_get_name(device->evdev);
1522 device->scroll.threshold = 5.0; /* Default may be overridden */
1523 device->scroll.direction = 0;
1524 device->dpi = DEFAULT_MOUSE_DPI;
1525 /* at most 5 SYN_DROPPED log-messages per 30s */
1526 ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1528 matrix_init_identity(&device->abs.calibration);
1529 matrix_init_identity(&device->abs.usermatrix);
1530 matrix_init_identity(&device->abs.default_calibration);
1532 if (evdev_configure_device(device) == -1)
1535 if (device->seat_caps == 0) {
1536 unhandled_device = 1;
1540 /* If the dispatch was not set up use the fallback. */
1541 if (device->dispatch == NULL)
1542 device->dispatch = fallback_dispatch_create(&device->base);
1543 if (device->dispatch == NULL)
1547 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1548 if (!device->source)
1551 list_insert(seat->devices_list.prev, &device->base.link);
1553 evdev_tag_device(device);
1554 evdev_notify_added_device(device);
1560 close_restricted(libinput, fd);
1562 evdev_device_destroy(device);
1564 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1568 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1570 memset(keys, 0, size);
1575 evdev_device_get_output(struct evdev_device *device)
1577 return device->output_name;
1581 evdev_device_get_sysname(struct evdev_device *device)
1583 return udev_device_get_sysname(device->udev_device);
1587 evdev_device_get_name(struct evdev_device *device)
1589 return device->devname;
1593 evdev_device_get_id_product(struct evdev_device *device)
1595 return libevdev_get_id_product(device->evdev);
1599 evdev_device_get_id_vendor(struct evdev_device *device)
1601 return libevdev_get_id_vendor(device->evdev);
1605 evdev_device_set_default_calibration(struct evdev_device *device,
1606 const float calibration[6])
1608 matrix_from_farray6(&device->abs.default_calibration, calibration);
1609 evdev_device_calibrate(device, calibration);
1613 evdev_device_calibrate(struct evdev_device *device,
1614 const float calibration[6])
1616 struct matrix scale,
1621 matrix_from_farray6(&transform, calibration);
1622 device->abs.apply_calibration = !matrix_is_identity(&transform);
1624 if (!device->abs.apply_calibration) {
1625 matrix_init_identity(&device->abs.calibration);
1629 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1630 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1632 /* The transformation matrix is in the form:
1636 * Where a, e are the scale components, a, b, d, e are the rotation
1637 * component (combined with scale) and c and f are the translation
1638 * component. The translation component in the input matrix must be
1639 * normalized to multiples of the device width and height,
1640 * respectively. e.g. c == 1 shifts one device-width to the right.
1642 * We pre-calculate a single matrix to apply to event coordinates:
1643 * M = Un-Normalize * Calibration * Normalize
1645 * Normalize: scales the device coordinates to [0,1]
1646 * Calibration: user-supplied matrix
1647 * Un-Normalize: scales back up to device coordinates
1648 * Matrix maths requires the normalize/un-normalize in reverse
1652 /* back up the user matrix so we can return it on request */
1653 matrix_from_farray6(&device->abs.usermatrix, calibration);
1656 matrix_init_translate(&translate,
1657 device->abs.absinfo_x->minimum,
1658 device->abs.absinfo_y->minimum);
1659 matrix_init_scale(&scale, sx, sy);
1660 matrix_mult(&scale, &translate, &scale);
1663 matrix_mult(&transform, &scale, &transform);
1666 matrix_init_translate(&translate,
1667 -device->abs.absinfo_x->minimum/sx,
1668 -device->abs.absinfo_y->minimum/sy);
1669 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1670 matrix_mult(&scale, &translate, &scale);
1672 /* store final matrix in device */
1673 matrix_mult(&device->abs.calibration, &transform, &scale);
1677 evdev_device_has_capability(struct evdev_device *device,
1678 enum libinput_device_capability capability)
1680 switch (capability) {
1681 case LIBINPUT_DEVICE_CAP_POINTER:
1682 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1683 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1684 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1685 case LIBINPUT_DEVICE_CAP_TOUCH:
1686 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1693 evdev_device_get_size(struct evdev_device *device,
1697 const struct input_absinfo *x, *y;
1699 x = libevdev_get_abs_info(device->evdev, ABS_X);
1700 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1702 if (!x || !y || device->abs.fake_resolution ||
1703 !x->resolution || !y->resolution)
1706 *width = evdev_convert_to_mm(x, x->maximum);
1707 *height = evdev_convert_to_mm(y, y->maximum);
1713 evdev_device_has_button(struct evdev_device *device, uint32_t code)
1715 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
1718 return libevdev_has_event_code(device->evdev, EV_KEY, code);
1722 evdev_is_scrolling(const struct evdev_device *device,
1723 enum libinput_pointer_axis axis)
1725 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1726 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1728 return (device->scroll.direction & (1 << axis)) != 0;
1732 evdev_start_scrolling(struct evdev_device *device,
1733 enum libinput_pointer_axis axis)
1735 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1736 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1738 device->scroll.direction |= (1 << axis);
1742 evdev_post_scroll(struct evdev_device *device,
1747 double trigger_horiz, trigger_vert;
1749 if (!evdev_is_scrolling(device,
1750 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1751 device->scroll.buildup_vertical += dy;
1752 if (!evdev_is_scrolling(device,
1753 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1754 device->scroll.buildup_horizontal += dx;
1756 trigger_vert = device->scroll.buildup_vertical;
1757 trigger_horiz = device->scroll.buildup_horizontal;
1759 /* If we're not scrolling yet, use a distance trigger: moving
1760 past a certain distance starts scrolling */
1761 if (!evdev_is_scrolling(device,
1762 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
1763 !evdev_is_scrolling(device,
1764 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1765 if (fabs(trigger_vert) >= device->scroll.threshold)
1766 evdev_start_scrolling(device,
1767 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1768 if (fabs(trigger_horiz) >= device->scroll.threshold)
1769 evdev_start_scrolling(device,
1770 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1771 /* We're already scrolling in one direction. Require some
1772 trigger speed to start scrolling in the other direction */
1773 } else if (!evdev_is_scrolling(device,
1774 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1775 if (fabs(dy) >= device->scroll.threshold)
1776 evdev_start_scrolling(device,
1777 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1778 } else if (!evdev_is_scrolling(device,
1779 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1780 if (fabs(dx) >= device->scroll.threshold)
1781 evdev_start_scrolling(device,
1782 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1785 /* We use the trigger to enable, but the delta from this event for
1786 * the actual scroll movement. Otherwise we get a jump once
1787 * scrolling engages */
1789 evdev_is_scrolling(device,
1790 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1791 evdev_notify_axis(device,
1793 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1798 evdev_is_scrolling(device,
1799 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1800 evdev_notify_axis(device,
1802 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1808 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1810 /* terminate scrolling with a zero scroll event */
1811 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1812 pointer_notify_axis(&device->base,
1814 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1816 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1817 pointer_notify_axis(&device->base,
1819 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1822 device->scroll.buildup_horizontal = 0;
1823 device->scroll.buildup_vertical = 0;
1824 device->scroll.direction = 0;
1828 release_pressed_keys(struct evdev_device *device)
1830 struct libinput *libinput = device->base.seat->libinput;
1834 if ((time = libinput_now(libinput)) == 0)
1837 for (code = 0; code < KEY_CNT; code++) {
1838 int count = get_key_down_count(device, code);
1841 log_bug_libinput(libinput,
1842 "Key %d is down %d times.\n",
1847 while (get_key_down_count(device, code) > 0) {
1848 switch (get_key_type(code)) {
1849 case EVDEV_KEY_TYPE_NONE:
1851 case EVDEV_KEY_TYPE_KEY:
1852 evdev_keyboard_notify_key(
1856 LIBINPUT_KEY_STATE_RELEASED);
1858 case EVDEV_KEY_TYPE_BUTTON:
1859 evdev_pointer_notify_button(
1862 evdev_to_left_handed(device, code),
1863 LIBINPUT_BUTTON_STATE_RELEASED);
1871 evdev_notify_suspended_device(struct evdev_device *device)
1873 struct libinput_device *it;
1875 if (device->suspended)
1878 list_for_each(it, &device->base.seat->devices_list, link) {
1879 struct evdev_device *d = (struct evdev_device*)it;
1880 if (it == &device->base)
1883 if (d->dispatch->interface->device_suspended)
1884 d->dispatch->interface->device_suspended(d, device);
1887 device->suspended = 1;
1891 evdev_notify_resumed_device(struct evdev_device *device)
1893 struct libinput_device *it;
1895 if (!device->suspended)
1898 list_for_each(it, &device->base.seat->devices_list, link) {
1899 struct evdev_device *d = (struct evdev_device*)it;
1900 if (it == &device->base)
1903 if (d->dispatch->interface->device_resumed)
1904 d->dispatch->interface->device_resumed(d, device);
1907 device->suspended = 0;
1911 evdev_device_suspend(struct evdev_device *device)
1913 evdev_notify_suspended_device(device);
1915 if (device->source) {
1916 libinput_remove_source(device->base.seat->libinput,
1918 device->source = NULL;
1921 release_pressed_keys(device);
1923 if (device->mtdev) {
1924 mtdev_close_delete(device->mtdev);
1925 device->mtdev = NULL;
1928 if (device->fd != -1) {
1929 close_restricted(device->base.seat->libinput, device->fd);
1937 evdev_device_resume(struct evdev_device *device)
1939 struct libinput *libinput = device->base.seat->libinput;
1941 const char *devnode;
1943 if (device->fd != -1)
1946 if (device->was_removed)
1949 devnode = udev_device_get_devnode(device->udev_device);
1950 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1955 if (evdev_device_compare_syspath(device->udev_device, fd)) {
1956 close_restricted(libinput, fd);
1962 if (evdev_need_mtdev(device)) {
1963 device->mtdev = mtdev_new_open(device->fd);
1969 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1970 if (!device->source) {
1971 mtdev_close_delete(device->mtdev);
1975 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1977 evdev_notify_resumed_device(device);
1983 evdev_device_remove(struct evdev_device *device)
1985 struct libinput_device *dev;
1987 list_for_each(dev, &device->base.seat->devices_list, link) {
1988 struct evdev_device *d = (struct evdev_device*)dev;;
1989 if (dev == &device->base)
1992 if (d->dispatch->interface->device_removed)
1993 d->dispatch->interface->device_removed(d, device);
1996 evdev_device_suspend(device);
1998 /* A device may be removed while suspended, mark it to
1999 * skip re-opening a different device with the same node */
2000 device->was_removed = true;
2002 list_remove(&device->base.link);
2004 notify_removed_device(&device->base);
2005 libinput_device_unref(&device->base);
2009 evdev_device_destroy(struct evdev_device *device)
2011 struct evdev_dispatch *dispatch;
2013 dispatch = device->dispatch;
2015 dispatch->interface->destroy(dispatch);
2017 filter_destroy(device->pointer.filter);
2018 libinput_seat_unref(device->base.seat);
2019 libevdev_free(device->evdev);
2020 udev_device_unref(device->udev_device);
2021 free(device->mt.slots);