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
49 EVDEV_KEY_TYPE_BUTTON,
53 hw_set_key_down(struct evdev_device *device, int code, int pressed)
55 long_set_bit_state(device->hw_key_mask, code, pressed);
59 hw_is_key_down(struct evdev_device *device, int code)
61 return long_bit_is_set(device->hw_key_mask, code);
65 get_key_down_count(struct evdev_device *device, int code)
67 return device->key_count[code];
71 update_key_down_count(struct evdev_device *device, int code, int pressed)
74 assert(code >= 0 && code < KEY_CNT);
77 key_count = ++device->key_count[code];
79 assert(device->key_count[code] > 0);
80 key_count = --device->key_count[code];
84 log_bug_libinput(device->base.seat->libinput,
85 "Key count for %s reached abnormal values\n",
86 libevdev_event_code_get_name(EV_KEY, code));
93 evdev_keyboard_notify_key(struct evdev_device *device,
96 enum libinput_key_state state)
100 down_count = update_key_down_count(device, key, state);
102 if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
103 (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
104 keyboard_notify_key(&device->base, time, key, state);
108 evdev_pointer_notify_button(struct evdev_device *device,
111 enum libinput_button_state state)
115 down_count = update_key_down_count(device, button, state);
117 if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
118 (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
119 pointer_notify_button(&device->base, time, button, state);
121 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
122 device->buttons.change_to_left_handed)
123 device->buttons.change_to_left_handed(device);
125 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
126 device->scroll.change_scroll_method)
127 device->scroll.change_scroll_method(device);
133 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
135 static const struct {
136 enum libinput_led weston;
139 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
140 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
141 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
143 struct input_event ev[ARRAY_LENGTH(map) + 1];
146 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
149 memset(ev, 0, sizeof(ev));
150 for (i = 0; i < ARRAY_LENGTH(map); i++) {
152 ev[i].code = map[i].evdev;
153 ev[i].value = !!(leds & map[i].weston);
156 ev[i].code = SYN_REPORT;
158 i = write(device->fd, ev, sizeof ev);
159 (void)i; /* no, we really don't care about the return value */
163 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
165 if (!device->abs.apply_calibration)
168 matrix_mult_vec(&device->abs.calibration, x, y);
172 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
174 return (val - absinfo->minimum) * to_range /
175 (absinfo->maximum - absinfo->minimum + 1);
179 evdev_device_transform_x(struct evdev_device *device,
183 return scale_axis(device->abs.absinfo_x, x, width);
187 evdev_device_transform_y(struct evdev_device *device,
191 return scale_axis(device->abs.absinfo_y, y, height);
195 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
197 struct libinput *libinput = device->base.seat->libinput;
198 struct motion_params motion;
199 double dx_unaccel, dy_unaccel;
204 struct libinput_device *base = &device->base;
205 struct libinput_seat *seat = base->seat;
207 slot = device->mt.slot;
209 switch (device->pending_event) {
212 case EVDEV_RELATIVE_MOTION:
213 dx_unaccel = device->rel.dx / ((double) device->dpi /
215 dy_unaccel = device->rel.dy / ((double) device->dpi /
220 /* Use unaccelerated deltas for pointing stick scroll */
221 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
222 hw_is_key_down(device, device->scroll.button)) {
223 if (device->scroll.button_scroll_active)
224 evdev_post_scroll(device, time,
225 dx_unaccel, dy_unaccel);
229 /* Apply pointer acceleration. */
230 motion.dx = dx_unaccel;
231 motion.dy = dy_unaccel;
232 filter_dispatch(device->pointer.filter, &motion, device, time);
234 if (motion.dx == 0.0 && motion.dy == 0.0 &&
235 dx_unaccel == 0.0 && dy_unaccel == 0.0) {
239 pointer_notify_motion(base, time,
240 motion.dx, motion.dy,
241 dx_unaccel, dy_unaccel);
243 case EVDEV_ABSOLUTE_MT_DOWN:
244 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
247 if (device->mt.slots[slot].seat_slot != -1) {
248 log_bug_kernel(libinput,
249 "%s: Driver sent multiple touch down for the "
251 udev_device_get_devnode(device->udev_device));
255 seat_slot = ffs(~seat->slot_map) - 1;
256 device->mt.slots[slot].seat_slot = seat_slot;
261 seat->slot_map |= 1 << seat_slot;
262 x = device->mt.slots[slot].x;
263 y = device->mt.slots[slot].y;
264 transform_absolute(device, &x, &y);
266 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
268 case EVDEV_ABSOLUTE_MT_MOTION:
269 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
272 seat_slot = device->mt.slots[slot].seat_slot;
273 x = device->mt.slots[slot].x;
274 y = device->mt.slots[slot].y;
279 transform_absolute(device, &x, &y);
280 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
282 case EVDEV_ABSOLUTE_MT_UP:
283 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
286 seat_slot = device->mt.slots[slot].seat_slot;
287 device->mt.slots[slot].seat_slot = -1;
292 seat->slot_map &= ~(1 << seat_slot);
294 touch_notify_touch_up(base, time, slot, seat_slot);
296 case EVDEV_ABSOLUTE_TOUCH_DOWN:
297 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
300 if (device->abs.seat_slot != -1) {
301 log_bug_kernel(libinput,
302 "%s: Driver sent multiple touch down for the "
304 udev_device_get_devnode(device->udev_device));
308 seat_slot = ffs(~seat->slot_map) - 1;
309 device->abs.seat_slot = seat_slot;
314 seat->slot_map |= 1 << seat_slot;
318 transform_absolute(device, &cx, &cy);
320 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
322 case EVDEV_ABSOLUTE_MOTION:
325 transform_absolute(device, &cx, &cy);
329 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
330 seat_slot = device->abs.seat_slot;
335 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
336 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
337 pointer_notify_motion_absolute(base, time, x, y);
340 case EVDEV_ABSOLUTE_TOUCH_UP:
341 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
344 seat_slot = device->abs.seat_slot;
345 device->abs.seat_slot = -1;
350 seat->slot_map &= ~(1 << seat_slot);
352 touch_notify_touch_up(base, time, -1, seat_slot);
355 assert(0 && "Unknown pending event type");
359 device->pending_event = EVDEV_NONE;
362 static enum evdev_key_type
363 get_key_type(uint16_t code)
365 if (code == BTN_TOUCH)
366 return EVDEV_KEY_TYPE_NONE;
368 if (code >= KEY_ESC && code <= KEY_MICMUTE)
369 return EVDEV_KEY_TYPE_KEY;
370 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
371 return EVDEV_KEY_TYPE_BUTTON;
372 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
373 return EVDEV_KEY_TYPE_KEY;
374 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
375 return EVDEV_KEY_TYPE_BUTTON;
376 return EVDEV_KEY_TYPE_NONE;
380 evdev_button_scroll_timeout(uint64_t time, void *data)
382 struct evdev_device *device = data;
384 device->scroll.button_scroll_active = true;
388 evdev_button_scroll_button(struct evdev_device *device,
389 uint64_t time, int is_press)
392 libinput_timer_set(&device->scroll.timer,
393 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
395 libinput_timer_cancel(&device->scroll.timer);
396 if (device->scroll.button_scroll_active) {
397 evdev_stop_scroll(device, time);
398 device->scroll.button_scroll_active = false;
400 /* If the button is released quickly enough emit the
401 * button press/release events. */
402 evdev_pointer_notify_button(device, time,
403 device->scroll.button,
404 LIBINPUT_BUTTON_STATE_PRESSED);
405 evdev_pointer_notify_button(device, time,
406 device->scroll.button,
407 LIBINPUT_BUTTON_STATE_RELEASED);
413 evdev_process_touch_button(struct evdev_device *device,
414 uint64_t time, int value)
416 if (device->pending_event != EVDEV_NONE &&
417 device->pending_event != EVDEV_ABSOLUTE_MOTION)
418 evdev_flush_pending_event(device, time);
420 device->pending_event = (value ?
421 EVDEV_ABSOLUTE_TOUCH_DOWN :
422 EVDEV_ABSOLUTE_TOUCH_UP);
426 evdev_process_key(struct evdev_device *device,
427 struct input_event *e, uint64_t time)
429 enum evdev_key_type type;
431 /* ignore kernel key repeat */
435 if (e->code == BTN_TOUCH) {
437 evdev_process_touch_button(device, time, e->value);
441 evdev_flush_pending_event(device, time);
443 type = get_key_type(e->code);
445 /* Ignore key release events from the kernel for keys that libinput
446 * never got a pressed event for. */
449 case EVDEV_KEY_TYPE_NONE:
451 case EVDEV_KEY_TYPE_KEY:
452 case EVDEV_KEY_TYPE_BUTTON:
453 if (!hw_is_key_down(device, e->code))
458 hw_set_key_down(device, e->code, e->value);
461 case EVDEV_KEY_TYPE_NONE:
463 case EVDEV_KEY_TYPE_KEY:
464 evdev_keyboard_notify_key(
468 e->value ? LIBINPUT_KEY_STATE_PRESSED :
469 LIBINPUT_KEY_STATE_RELEASED);
471 case EVDEV_KEY_TYPE_BUTTON:
472 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
473 e->code == device->scroll.button) {
474 evdev_button_scroll_button(device, time, e->value);
477 evdev_pointer_notify_button(
480 evdev_to_left_handed(device, e->code),
481 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
482 LIBINPUT_BUTTON_STATE_RELEASED);
488 evdev_process_touch(struct evdev_device *device,
489 struct input_event *e,
494 evdev_flush_pending_event(device, time);
495 device->mt.slot = e->value;
497 case ABS_MT_TRACKING_ID:
498 if (device->pending_event != EVDEV_NONE &&
499 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
500 evdev_flush_pending_event(device, time);
502 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
504 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
506 case ABS_MT_POSITION_X:
507 device->mt.slots[device->mt.slot].x = e->value;
508 if (device->pending_event == EVDEV_NONE)
509 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
511 case ABS_MT_POSITION_Y:
512 device->mt.slots[device->mt.slot].y = e->value;
513 if (device->pending_event == EVDEV_NONE)
514 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
520 evdev_process_absolute_motion(struct evdev_device *device,
521 struct input_event *e)
525 device->abs.x = e->value;
526 if (device->pending_event == EVDEV_NONE)
527 device->pending_event = EVDEV_ABSOLUTE_MOTION;
530 device->abs.y = e->value;
531 if (device->pending_event == EVDEV_NONE)
532 device->pending_event = EVDEV_ABSOLUTE_MOTION;
538 evdev_notify_axis(struct evdev_device *device,
540 enum libinput_pointer_axis axis,
543 if (device->scroll.natural_scrolling_enabled)
546 pointer_notify_axis(&device->base,
553 evdev_process_relative(struct evdev_device *device,
554 struct input_event *e, uint64_t time)
558 if (device->pending_event != EVDEV_RELATIVE_MOTION)
559 evdev_flush_pending_event(device, time);
560 device->rel.dx += e->value;
561 device->pending_event = EVDEV_RELATIVE_MOTION;
564 if (device->pending_event != EVDEV_RELATIVE_MOTION)
565 evdev_flush_pending_event(device, time);
566 device->rel.dy += e->value;
567 device->pending_event = EVDEV_RELATIVE_MOTION;
570 evdev_flush_pending_event(device, time);
574 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
575 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
578 evdev_flush_pending_event(device, time);
582 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
583 e->value * DEFAULT_AXIS_STEP_DISTANCE);
589 evdev_process_absolute(struct evdev_device *device,
590 struct input_event *e,
594 evdev_process_touch(device, e, time);
596 evdev_process_absolute_motion(device, e);
601 evdev_any_button_down(struct evdev_device *device)
605 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
606 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
607 hw_is_key_down(device, button))
614 evdev_need_touch_frame(struct evdev_device *device)
616 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
619 switch (device->pending_event) {
621 case EVDEV_RELATIVE_MOTION:
623 case EVDEV_ABSOLUTE_MT_DOWN:
624 case EVDEV_ABSOLUTE_MT_MOTION:
625 case EVDEV_ABSOLUTE_MT_UP:
626 case EVDEV_ABSOLUTE_TOUCH_DOWN:
627 case EVDEV_ABSOLUTE_TOUCH_UP:
628 case EVDEV_ABSOLUTE_MOTION:
636 evdev_tag_external_mouse(struct evdev_device *device,
637 struct udev_device *udev_device)
641 bustype = libevdev_get_id_bustype(device->evdev);
642 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
643 if (device->seat_caps & EVDEV_DEVICE_POINTER)
644 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
649 evdev_tag_trackpoint(struct evdev_device *device,
650 struct udev_device *udev_device)
652 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
653 device->tags |= EVDEV_TAG_TRACKPOINT;
657 fallback_process(struct evdev_dispatch *dispatch,
658 struct evdev_device *device,
659 struct input_event *event,
662 bool need_frame = false;
664 switch (event->type) {
666 evdev_process_relative(device, event, time);
669 evdev_process_absolute(device, event, time);
672 evdev_process_key(device, event, time);
675 need_frame = evdev_need_touch_frame(device);
676 evdev_flush_pending_event(device, time);
678 touch_notify_frame(&device->base, time);
684 fallback_destroy(struct evdev_dispatch *dispatch)
690 fallback_tag_device(struct evdev_device *device,
691 struct udev_device *udev_device)
693 evdev_tag_external_mouse(device, udev_device);
694 evdev_tag_trackpoint(device, udev_device);
698 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
700 struct evdev_device *device = (struct evdev_device*)libinput_device;
702 return device->abs.absinfo_x && device->abs.absinfo_y;
705 static enum libinput_config_status
706 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
707 const float matrix[6])
709 struct evdev_device *device = (struct evdev_device*)libinput_device;
711 evdev_device_calibrate(device, matrix);
713 return LIBINPUT_CONFIG_STATUS_SUCCESS;
717 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
720 struct evdev_device *device = (struct evdev_device*)libinput_device;
722 matrix_to_farray6(&device->abs.usermatrix, matrix);
724 return !matrix_is_identity(&device->abs.usermatrix);
728 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
731 struct evdev_device *device = (struct evdev_device*)libinput_device;
733 matrix_to_farray6(&device->abs.default_calibration, matrix);
735 return !matrix_is_identity(&device->abs.default_calibration);
738 struct evdev_dispatch_interface fallback_interface = {
741 NULL, /* device_added */
742 NULL, /* device_removed */
743 NULL, /* device_suspended */
744 NULL, /* device_resumed */
749 evdev_sendevents_get_modes(struct libinput_device *device)
751 return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
754 static enum libinput_config_status
755 evdev_sendevents_set_mode(struct libinput_device *device,
756 enum libinput_config_send_events_mode mode)
758 struct evdev_device *evdev = (struct evdev_device*)device;
759 struct evdev_dispatch *dispatch = evdev->dispatch;
761 if (mode == dispatch->sendevents.current_mode)
762 return LIBINPUT_CONFIG_STATUS_SUCCESS;
765 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
766 evdev_device_resume(evdev);
768 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
769 evdev_device_suspend(evdev);
771 default: /* no support for combined modes yet */
772 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
775 dispatch->sendevents.current_mode = mode;
777 return LIBINPUT_CONFIG_STATUS_SUCCESS;
780 static enum libinput_config_send_events_mode
781 evdev_sendevents_get_mode(struct libinput_device *device)
783 struct evdev_device *evdev = (struct evdev_device*)device;
784 struct evdev_dispatch *dispatch = evdev->dispatch;
786 return dispatch->sendevents.current_mode;
789 static enum libinput_config_send_events_mode
790 evdev_sendevents_get_default_mode(struct libinput_device *device)
792 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
796 evdev_left_handed_has(struct libinput_device *device)
798 /* This is only hooked up when we have left-handed configuration, so we
799 * can hardcode 1 here */
804 evdev_change_to_left_handed(struct evdev_device *device)
806 if (device->buttons.want_left_handed == device->buttons.left_handed)
809 if (evdev_any_button_down(device))
812 device->buttons.left_handed = device->buttons.want_left_handed;
815 static enum libinput_config_status
816 evdev_left_handed_set(struct libinput_device *device, int left_handed)
818 struct evdev_device *evdev_device = (struct evdev_device *)device;
820 evdev_device->buttons.want_left_handed = left_handed ? true : false;
822 evdev_device->buttons.change_to_left_handed(evdev_device);
824 return LIBINPUT_CONFIG_STATUS_SUCCESS;
828 evdev_left_handed_get(struct libinput_device *device)
830 struct evdev_device *evdev_device = (struct evdev_device *)device;
832 /* return the wanted configuration, even if it hasn't taken
834 return evdev_device->buttons.want_left_handed;
838 evdev_left_handed_get_default(struct libinput_device *device)
844 evdev_init_left_handed(struct evdev_device *device,
845 void (*change_to_left_handed)(struct evdev_device *))
847 device->buttons.config_left_handed.has = evdev_left_handed_has;
848 device->buttons.config_left_handed.set = evdev_left_handed_set;
849 device->buttons.config_left_handed.get = evdev_left_handed_get;
850 device->buttons.config_left_handed.get_default = evdev_left_handed_get_default;
851 device->base.config.left_handed = &device->buttons.config_left_handed;
852 device->buttons.left_handed = false;
853 device->buttons.want_left_handed = false;
854 device->buttons.change_to_left_handed = change_to_left_handed;
860 evdev_scroll_get_methods(struct libinput_device *device)
862 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
866 evdev_change_scroll_method(struct evdev_device *device)
868 if (device->scroll.want_method == device->scroll.method &&
869 device->scroll.want_button == device->scroll.button)
872 if (evdev_any_button_down(device))
875 device->scroll.method = device->scroll.want_method;
876 device->scroll.button = device->scroll.want_button;
879 static enum libinput_config_status
880 evdev_scroll_set_method(struct libinput_device *device,
881 enum libinput_config_scroll_method method)
883 struct evdev_device *evdev = (struct evdev_device*)device;
885 evdev->scroll.want_method = method;
886 evdev->scroll.change_scroll_method(evdev);
888 return LIBINPUT_CONFIG_STATUS_SUCCESS;
891 static enum libinput_config_scroll_method
892 evdev_scroll_get_method(struct libinput_device *device)
894 struct evdev_device *evdev = (struct evdev_device *)device;
896 /* return the wanted configuration, even if it hasn't taken
898 return evdev->scroll.want_method;
901 static enum libinput_config_scroll_method
902 evdev_scroll_get_default_method(struct libinput_device *device)
904 struct evdev_device *evdev = (struct evdev_device *)device;
906 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
907 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
909 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
912 static enum libinput_config_status
913 evdev_scroll_set_button(struct libinput_device *device,
916 struct evdev_device *evdev = (struct evdev_device*)device;
918 evdev->scroll.want_button = button;
919 evdev->scroll.change_scroll_method(evdev);
921 return LIBINPUT_CONFIG_STATUS_SUCCESS;
925 evdev_scroll_get_button(struct libinput_device *device)
927 struct evdev_device *evdev = (struct evdev_device *)device;
929 /* return the wanted configuration, even if it hasn't taken
931 return evdev->scroll.want_button;
935 evdev_scroll_get_default_button(struct libinput_device *device)
937 struct evdev_device *evdev = (struct evdev_device *)device;
939 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
946 evdev_init_button_scroll(struct evdev_device *device,
947 void (*change_scroll_method)(struct evdev_device *))
949 libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
950 evdev_button_scroll_timeout, device);
951 device->scroll.config.get_methods = evdev_scroll_get_methods;
952 device->scroll.config.set_method = evdev_scroll_set_method;
953 device->scroll.config.get_method = evdev_scroll_get_method;
954 device->scroll.config.get_default_method = evdev_scroll_get_default_method;
955 device->scroll.config.set_button = evdev_scroll_set_button;
956 device->scroll.config.get_button = evdev_scroll_get_button;
957 device->scroll.config.get_default_button = evdev_scroll_get_default_button;
958 device->base.config.scroll_method = &device->scroll.config;
959 device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
960 device->scroll.want_method = device->scroll.method;
961 device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
962 device->scroll.want_button = device->scroll.button;
963 device->scroll.change_scroll_method = change_scroll_method;
969 evdev_init_calibration(struct evdev_device *device,
970 struct evdev_dispatch *dispatch)
972 device->base.config.calibration = &dispatch->calibration;
974 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
975 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
976 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
977 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
981 evdev_init_sendevents(struct evdev_device *device,
982 struct evdev_dispatch *dispatch)
984 device->base.config.sendevents = &dispatch->sendevents.config;
986 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
987 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
988 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
989 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
990 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
994 evdev_scroll_config_natural_has(struct libinput_device *device)
999 static enum libinput_config_status
1000 evdev_scroll_config_natural_set(struct libinput_device *device,
1003 struct evdev_device *dev = (struct evdev_device *)device;
1005 dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1007 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1011 evdev_scroll_config_natural_get(struct libinput_device *device)
1013 struct evdev_device *dev = (struct evdev_device *)device;
1015 return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1019 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1021 /* could enable this on Apple touchpads. could do that, could
1022 * very well do that... */
1027 evdev_init_natural_scroll(struct evdev_device *device)
1029 device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1030 device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1031 device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1032 device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1033 device->scroll.natural_scrolling_enabled = false;
1034 device->base.config.natural_scroll = &device->scroll.config_natural;
1037 static struct evdev_dispatch *
1038 fallback_dispatch_create(struct libinput_device *device)
1040 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1041 struct evdev_device *evdev_device = (struct evdev_device *)device;
1043 if (dispatch == NULL)
1046 dispatch->interface = &fallback_interface;
1048 if (evdev_device->buttons.want_left_handed &&
1049 evdev_init_left_handed(evdev_device,
1050 evdev_change_to_left_handed) == -1) {
1055 if (evdev_device->scroll.want_button &&
1056 evdev_init_button_scroll(evdev_device,
1057 evdev_change_scroll_method) == -1) {
1062 if (evdev_device->scroll.natural_scrolling_enabled)
1063 evdev_init_natural_scroll(evdev_device);
1065 evdev_init_calibration(evdev_device, dispatch);
1066 evdev_init_sendevents(evdev_device, dispatch);
1072 evdev_process_event(struct evdev_device *device, struct input_event *e)
1074 struct evdev_dispatch *dispatch = device->dispatch;
1075 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1077 dispatch->interface->process(dispatch, device, e, time);
1081 evdev_device_dispatch_one(struct evdev_device *device,
1082 struct input_event *ev)
1084 if (!device->mtdev) {
1085 evdev_process_event(device, ev);
1087 mtdev_put_event(device->mtdev, ev);
1088 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1089 while (!mtdev_empty(device->mtdev)) {
1090 struct input_event e;
1091 mtdev_get_event(device->mtdev, &e);
1092 evdev_process_event(device, &e);
1099 evdev_sync_device(struct evdev_device *device)
1101 struct input_event ev;
1105 rc = libevdev_next_event(device->evdev,
1106 LIBEVDEV_READ_FLAG_SYNC, &ev);
1109 evdev_device_dispatch_one(device, &ev);
1110 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1112 return rc == -EAGAIN ? 0 : rc;
1116 evdev_device_dispatch(void *data)
1118 struct evdev_device *device = data;
1119 struct libinput *libinput = device->base.seat->libinput;
1120 struct input_event ev;
1123 /* If the compositor is repainting, this function is called only once
1124 * per frame and we have to process all the events available on the
1125 * fd, otherwise there will be input lag. */
1127 rc = libevdev_next_event(device->evdev,
1128 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1129 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1130 switch (ratelimit_test(&device->syn_drop_limit)) {
1131 case RATELIMIT_PASS:
1132 log_info(libinput, "SYN_DROPPED event from "
1133 "\"%s\" - some input events have "
1134 "been lost.\n", device->devname);
1136 case RATELIMIT_THRESHOLD:
1137 log_info(libinput, "SYN_DROPPED flood "
1141 case RATELIMIT_EXCEEDED:
1145 /* send one more sync event so we handle all
1146 currently pending events before we sync up
1147 to the current state */
1148 ev.code = SYN_REPORT;
1149 evdev_device_dispatch_one(device, &ev);
1151 rc = evdev_sync_device(device);
1153 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1154 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1155 evdev_device_dispatch_one(device, &ev);
1157 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1159 if (rc != -EAGAIN && rc != -EINTR) {
1160 libinput_remove_source(libinput, device->source);
1161 device->source = NULL;
1166 evdev_accel_config_available(struct libinput_device *device)
1168 /* this function is only called if we set up ptraccel, so we can
1169 reply with a resounding "Yes" */
1173 static enum libinput_config_status
1174 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1176 struct evdev_device *dev = (struct evdev_device *)device;
1178 if (!filter_set_speed(dev->pointer.filter, speed))
1179 return LIBINPUT_CONFIG_STATUS_INVALID;
1181 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1185 evdev_accel_config_get_speed(struct libinput_device *device)
1187 struct evdev_device *dev = (struct evdev_device *)device;
1189 return filter_get_speed(dev->pointer.filter);
1193 evdev_accel_config_get_default_speed(struct libinput_device *device)
1199 evdev_device_init_pointer_acceleration(struct evdev_device *device)
1201 device->pointer.filter =
1202 create_pointer_accelerator_filter(
1203 pointer_accel_profile_linear);
1204 if (!device->pointer.filter)
1207 device->pointer.config.available = evdev_accel_config_available;
1208 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1209 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1210 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1211 device->base.config.accel = &device->pointer.config;
1218 evdev_need_mtdev(struct evdev_device *device)
1220 struct libevdev *evdev = device->evdev;
1222 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1223 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1224 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1228 evdev_tag_device(struct evdev_device *device)
1230 if (device->dispatch->interface->tag_device)
1231 device->dispatch->interface->tag_device(device,
1232 device->udev_device);
1236 evdev_read_dpi_prop(struct evdev_device *device)
1238 struct libinput *libinput = device->base.seat->libinput;
1239 const char *mouse_dpi;
1240 int dpi = DEFAULT_MOUSE_DPI;
1242 mouse_dpi = udev_device_get_property_value(device->udev_device,
1245 dpi = parse_mouse_dpi_property(mouse_dpi);
1247 log_error(libinput, "Mouse DPI property for '%s' is "
1248 "present but invalid, using %d "
1252 dpi = DEFAULT_MOUSE_DPI;
1260 evdev_fix_abs_resolution(struct libevdev *evdev,
1262 const struct input_absinfo *absinfo)
1264 struct input_absinfo fixed;
1266 if (absinfo->resolution == 0) {
1268 fixed.resolution = 1;
1269 /* libevdev_set_abs_info() changes the absinfo we already
1270 have a pointer to, no need to fetch it again */
1271 libevdev_set_abs_info(evdev, code, &fixed);
1279 evdev_configure_device(struct evdev_device *device)
1281 struct libinput *libinput = device->base.seat->libinput;
1282 struct libevdev *evdev = device->evdev;
1283 const struct input_absinfo *absinfo;
1284 int has_abs, has_rel, has_mt;
1285 int has_button, has_keyboard, has_touch;
1286 struct mt_slot *slots;
1291 const char *devnode = udev_device_get_devnode(device->udev_device);
1300 for (i = BTN_JOYSTICK; i < BTN_DIGI; i++) {
1301 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1303 "input device '%s', %s is a joystick, ignoring\n",
1304 device->devname, devnode);
1309 if (libevdev_has_event_type(evdev, EV_ABS)) {
1311 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1312 if (evdev_fix_abs_resolution(evdev,
1315 device->abs.fake_resolution = 1;
1316 device->abs.absinfo_x = absinfo;
1319 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1320 if (evdev_fix_abs_resolution(evdev,
1323 device->abs.fake_resolution = 1;
1324 device->abs.absinfo_y = absinfo;
1328 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1329 the limited ABS_* range - they aren't MT devices, they
1330 just have too many ABS_ axes */
1331 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1332 libevdev_get_num_slots(evdev) == -1) {
1335 } else if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1336 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1337 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1338 if (evdev_fix_abs_resolution(evdev,
1341 device->abs.fake_resolution = 1;
1342 device->abs.absinfo_x = absinfo;
1344 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1345 if (evdev_fix_abs_resolution(evdev,
1348 device->abs.fake_resolution = 1;
1349 device->abs.absinfo_y = absinfo;
1354 /* We only handle the slotted Protocol B in libinput.
1355 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1356 require mtdev for conversion. */
1357 if (evdev_need_mtdev(device)) {
1358 device->mtdev = mtdev_new_open(device->fd);
1362 num_slots = device->mtdev->caps.slot.maximum;
1363 if (device->mtdev->caps.slot.minimum < 0 ||
1366 active_slot = device->mtdev->caps.slot.value;
1368 num_slots = libevdev_get_num_slots(device->evdev);
1369 active_slot = libevdev_get_current_slot(evdev);
1372 slots = calloc(num_slots, sizeof(struct mt_slot));
1376 for (slot = 0; slot < num_slots; ++slot) {
1377 slots[slot].seat_slot = -1;
1381 device->mt.slots = slots;
1382 device->mt.slots_len = num_slots;
1383 device->mt.slot = active_slot;
1387 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1388 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1391 if (libevdev_has_event_type(evdev, EV_KEY)) {
1392 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1393 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1394 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1395 (has_abs || has_mt)) {
1396 device->dispatch = evdev_mt_touchpad_create(device);
1398 "input device '%s', %s is a touchpad\n",
1399 device->devname, devnode);
1400 return device->dispatch == NULL ? -1 : 0;
1403 for (i = 0; i < KEY_MAX; i++) {
1404 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1405 switch (get_key_type(i)) {
1406 case EVDEV_KEY_TYPE_NONE:
1408 case EVDEV_KEY_TYPE_KEY:
1411 case EVDEV_KEY_TYPE_BUTTON:
1418 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1421 if (libevdev_has_event_type(evdev, EV_LED))
1424 if ((has_abs || has_rel) && has_button) {
1425 if (evdev_device_init_pointer_acceleration(device) == -1)
1428 device->seat_caps |= EVDEV_DEVICE_POINTER;
1431 "input device '%s', %s is a pointer caps =%s%s%s\n",
1432 device->devname, devnode,
1433 has_abs ? " absolute-motion" : "",
1434 has_rel ? " relative-motion": "",
1435 has_button ? " button" : "");
1437 /* want left-handed config option */
1438 device->buttons.want_left_handed = true;
1439 /* want natural-scroll config option */
1440 device->scroll.natural_scrolling_enabled = true;
1443 if (has_rel && has_button) {
1444 /* want button scrolling config option */
1445 device->scroll.want_button = 1;
1449 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1451 "input device '%s', %s is a keyboard\n",
1452 device->devname, devnode);
1454 if (has_touch && !has_button) {
1455 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1457 "input device '%s', %s is a touch device\n",
1458 device->devname, devnode);
1465 evdev_notify_added_device(struct evdev_device *device)
1467 struct libinput_device *dev;
1469 list_for_each(dev, &device->base.seat->devices_list, link) {
1470 struct evdev_device *d = (struct evdev_device*)dev;
1471 if (dev == &device->base)
1474 /* Notify existing device d about addition of device device */
1475 if (d->dispatch->interface->device_added)
1476 d->dispatch->interface->device_added(d, device);
1478 /* Notify new device device about existing device d */
1479 if (device->dispatch->interface->device_added)
1480 device->dispatch->interface->device_added(device, d);
1482 /* Notify new device device if existing device d is suspended */
1483 if (d->suspended && device->dispatch->interface->device_suspended)
1484 device->dispatch->interface->device_suspended(device, d);
1487 notify_added_device(&device->base);
1491 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1493 struct udev *udev = udev_device_get_udev(udev_device);
1494 struct udev_device *udev_device_new = NULL;
1498 if (fstat(fd, &st) < 0)
1501 udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1502 if (!udev_device_new)
1505 rc = strcmp(udev_device_get_syspath(udev_device_new),
1506 udev_device_get_syspath(udev_device));
1508 if (udev_device_new)
1509 udev_device_unref(udev_device_new);
1513 struct evdev_device *
1514 evdev_device_create(struct libinput_seat *seat,
1515 struct udev_device *udev_device)
1517 struct libinput *libinput = seat->libinput;
1518 struct evdev_device *device = NULL;
1521 int unhandled_device = 0;
1522 const char *devnode = udev_device_get_devnode(udev_device);
1524 /* Use non-blocking mode so that we can loop on read on
1525 * evdev_device_data() until all events on the fd are
1526 * read. mtdev_get() also expects this. */
1527 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1530 "opening input device '%s' failed (%s).\n",
1531 devnode, strerror(-fd));
1535 if (evdev_device_compare_syspath(udev_device, fd) != 0)
1538 device = zalloc(sizeof *device);
1542 libinput_device_init(&device->base, seat);
1543 libinput_seat_ref(seat);
1545 rc = libevdev_new_from_fd(fd, &device->evdev);
1549 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1551 device->seat_caps = 0;
1553 device->mtdev = NULL;
1554 device->udev_device = udev_device_ref(udev_device);
1557 device->abs.seat_slot = -1;
1558 device->dispatch = NULL;
1560 device->pending_event = EVDEV_NONE;
1561 device->devname = libevdev_get_name(device->evdev);
1562 device->scroll.threshold = 5.0; /* Default may be overridden */
1563 device->scroll.direction = 0;
1564 device->dpi = evdev_read_dpi_prop(device);
1565 /* at most 5 SYN_DROPPED log-messages per 30s */
1566 ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1568 matrix_init_identity(&device->abs.calibration);
1569 matrix_init_identity(&device->abs.usermatrix);
1570 matrix_init_identity(&device->abs.default_calibration);
1572 if (evdev_configure_device(device) == -1)
1575 if (device->seat_caps == 0) {
1576 unhandled_device = 1;
1580 /* If the dispatch was not set up use the fallback. */
1581 if (device->dispatch == NULL)
1582 device->dispatch = fallback_dispatch_create(&device->base);
1583 if (device->dispatch == NULL)
1587 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1588 if (!device->source)
1591 list_insert(seat->devices_list.prev, &device->base.link);
1593 evdev_tag_device(device);
1594 evdev_notify_added_device(device);
1600 close_restricted(libinput, fd);
1602 evdev_device_destroy(device);
1604 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1608 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1610 memset(keys, 0, size);
1615 evdev_device_get_output(struct evdev_device *device)
1617 return device->output_name;
1621 evdev_device_get_sysname(struct evdev_device *device)
1623 return udev_device_get_sysname(device->udev_device);
1627 evdev_device_get_name(struct evdev_device *device)
1629 return device->devname;
1633 evdev_device_get_id_product(struct evdev_device *device)
1635 return libevdev_get_id_product(device->evdev);
1639 evdev_device_get_id_vendor(struct evdev_device *device)
1641 return libevdev_get_id_vendor(device->evdev);
1644 struct udev_device *
1645 evdev_device_get_udev_device(struct evdev_device *device)
1647 return udev_device_ref(device->udev_device);
1651 evdev_device_set_default_calibration(struct evdev_device *device,
1652 const float calibration[6])
1654 matrix_from_farray6(&device->abs.default_calibration, calibration);
1655 evdev_device_calibrate(device, calibration);
1659 evdev_device_calibrate(struct evdev_device *device,
1660 const float calibration[6])
1662 struct matrix scale,
1667 matrix_from_farray6(&transform, calibration);
1668 device->abs.apply_calibration = !matrix_is_identity(&transform);
1670 if (!device->abs.apply_calibration) {
1671 matrix_init_identity(&device->abs.calibration);
1675 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1676 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1678 /* The transformation matrix is in the form:
1682 * Where a, e are the scale components, a, b, d, e are the rotation
1683 * component (combined with scale) and c and f are the translation
1684 * component. The translation component in the input matrix must be
1685 * normalized to multiples of the device width and height,
1686 * respectively. e.g. c == 1 shifts one device-width to the right.
1688 * We pre-calculate a single matrix to apply to event coordinates:
1689 * M = Un-Normalize * Calibration * Normalize
1691 * Normalize: scales the device coordinates to [0,1]
1692 * Calibration: user-supplied matrix
1693 * Un-Normalize: scales back up to device coordinates
1694 * Matrix maths requires the normalize/un-normalize in reverse
1698 /* back up the user matrix so we can return it on request */
1699 matrix_from_farray6(&device->abs.usermatrix, calibration);
1702 matrix_init_translate(&translate,
1703 device->abs.absinfo_x->minimum,
1704 device->abs.absinfo_y->minimum);
1705 matrix_init_scale(&scale, sx, sy);
1706 matrix_mult(&scale, &translate, &scale);
1709 matrix_mult(&transform, &scale, &transform);
1712 matrix_init_translate(&translate,
1713 -device->abs.absinfo_x->minimum/sx,
1714 -device->abs.absinfo_y->minimum/sy);
1715 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1716 matrix_mult(&scale, &translate, &scale);
1718 /* store final matrix in device */
1719 matrix_mult(&device->abs.calibration, &transform, &scale);
1723 evdev_device_has_capability(struct evdev_device *device,
1724 enum libinput_device_capability capability)
1726 switch (capability) {
1727 case LIBINPUT_DEVICE_CAP_POINTER:
1728 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1729 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1730 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1731 case LIBINPUT_DEVICE_CAP_TOUCH:
1732 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1739 evdev_device_get_size(struct evdev_device *device,
1743 const struct input_absinfo *x, *y;
1745 x = libevdev_get_abs_info(device->evdev, ABS_X);
1746 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1748 if (!x || !y || device->abs.fake_resolution ||
1749 !x->resolution || !y->resolution)
1752 *width = evdev_convert_to_mm(x, x->maximum);
1753 *height = evdev_convert_to_mm(y, y->maximum);
1759 evdev_device_has_button(struct evdev_device *device, uint32_t code)
1761 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
1764 return libevdev_has_event_code(device->evdev, EV_KEY, code);
1768 evdev_is_scrolling(const struct evdev_device *device,
1769 enum libinput_pointer_axis axis)
1771 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1772 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1774 return (device->scroll.direction & (1 << axis)) != 0;
1778 evdev_start_scrolling(struct evdev_device *device,
1779 enum libinput_pointer_axis axis)
1781 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1782 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1784 device->scroll.direction |= (1 << axis);
1788 evdev_post_scroll(struct evdev_device *device,
1793 double trigger_horiz, trigger_vert;
1795 if (!evdev_is_scrolling(device,
1796 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1797 device->scroll.buildup_vertical += dy;
1798 if (!evdev_is_scrolling(device,
1799 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1800 device->scroll.buildup_horizontal += dx;
1802 trigger_vert = device->scroll.buildup_vertical;
1803 trigger_horiz = device->scroll.buildup_horizontal;
1805 /* If we're not scrolling yet, use a distance trigger: moving
1806 past a certain distance starts scrolling */
1807 if (!evdev_is_scrolling(device,
1808 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
1809 !evdev_is_scrolling(device,
1810 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1811 if (fabs(trigger_vert) >= device->scroll.threshold)
1812 evdev_start_scrolling(device,
1813 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1814 if (fabs(trigger_horiz) >= device->scroll.threshold)
1815 evdev_start_scrolling(device,
1816 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1817 /* We're already scrolling in one direction. Require some
1818 trigger speed to start scrolling in the other direction */
1819 } else if (!evdev_is_scrolling(device,
1820 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1821 if (fabs(dy) >= device->scroll.threshold)
1822 evdev_start_scrolling(device,
1823 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1824 } else if (!evdev_is_scrolling(device,
1825 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1826 if (fabs(dx) >= device->scroll.threshold)
1827 evdev_start_scrolling(device,
1828 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1831 /* We use the trigger to enable, but the delta from this event for
1832 * the actual scroll movement. Otherwise we get a jump once
1833 * scrolling engages */
1835 evdev_is_scrolling(device,
1836 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1837 evdev_notify_axis(device,
1839 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1844 evdev_is_scrolling(device,
1845 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1846 evdev_notify_axis(device,
1848 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1854 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1856 /* terminate scrolling with a zero scroll event */
1857 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1858 pointer_notify_axis(&device->base,
1860 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1862 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1863 pointer_notify_axis(&device->base,
1865 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1868 device->scroll.buildup_horizontal = 0;
1869 device->scroll.buildup_vertical = 0;
1870 device->scroll.direction = 0;
1874 release_pressed_keys(struct evdev_device *device)
1876 struct libinput *libinput = device->base.seat->libinput;
1880 if ((time = libinput_now(libinput)) == 0)
1883 for (code = 0; code < KEY_CNT; code++) {
1884 int count = get_key_down_count(device, code);
1887 log_bug_libinput(libinput,
1888 "Key %d is down %d times.\n",
1893 while (get_key_down_count(device, code) > 0) {
1894 switch (get_key_type(code)) {
1895 case EVDEV_KEY_TYPE_NONE:
1897 case EVDEV_KEY_TYPE_KEY:
1898 evdev_keyboard_notify_key(
1902 LIBINPUT_KEY_STATE_RELEASED);
1904 case EVDEV_KEY_TYPE_BUTTON:
1905 evdev_pointer_notify_button(
1908 evdev_to_left_handed(device, code),
1909 LIBINPUT_BUTTON_STATE_RELEASED);
1917 evdev_notify_suspended_device(struct evdev_device *device)
1919 struct libinput_device *it;
1921 if (device->suspended)
1924 list_for_each(it, &device->base.seat->devices_list, link) {
1925 struct evdev_device *d = (struct evdev_device*)it;
1926 if (it == &device->base)
1929 if (d->dispatch->interface->device_suspended)
1930 d->dispatch->interface->device_suspended(d, device);
1933 device->suspended = 1;
1937 evdev_notify_resumed_device(struct evdev_device *device)
1939 struct libinput_device *it;
1941 if (!device->suspended)
1944 list_for_each(it, &device->base.seat->devices_list, link) {
1945 struct evdev_device *d = (struct evdev_device*)it;
1946 if (it == &device->base)
1949 if (d->dispatch->interface->device_resumed)
1950 d->dispatch->interface->device_resumed(d, device);
1953 device->suspended = 0;
1957 evdev_device_suspend(struct evdev_device *device)
1959 evdev_notify_suspended_device(device);
1961 if (device->source) {
1962 libinput_remove_source(device->base.seat->libinput,
1964 device->source = NULL;
1967 release_pressed_keys(device);
1969 if (device->mtdev) {
1970 mtdev_close_delete(device->mtdev);
1971 device->mtdev = NULL;
1974 if (device->fd != -1) {
1975 close_restricted(device->base.seat->libinput, device->fd);
1983 evdev_device_resume(struct evdev_device *device)
1985 struct libinput *libinput = device->base.seat->libinput;
1987 const char *devnode;
1989 if (device->fd != -1)
1992 if (device->was_removed)
1995 devnode = udev_device_get_devnode(device->udev_device);
1996 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
2001 if (evdev_device_compare_syspath(device->udev_device, fd)) {
2002 close_restricted(libinput, fd);
2008 if (evdev_need_mtdev(device)) {
2009 device->mtdev = mtdev_new_open(device->fd);
2015 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2016 if (!device->source) {
2017 mtdev_close_delete(device->mtdev);
2021 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2023 evdev_notify_resumed_device(device);
2029 evdev_device_remove(struct evdev_device *device)
2031 struct libinput_device *dev;
2033 list_for_each(dev, &device->base.seat->devices_list, link) {
2034 struct evdev_device *d = (struct evdev_device*)dev;;
2035 if (dev == &device->base)
2038 if (d->dispatch->interface->device_removed)
2039 d->dispatch->interface->device_removed(d, device);
2042 evdev_device_suspend(device);
2044 /* A device may be removed while suspended, mark it to
2045 * skip re-opening a different device with the same node */
2046 device->was_removed = true;
2048 list_remove(&device->base.link);
2050 notify_removed_device(&device->base);
2051 libinput_device_unref(&device->base);
2055 evdev_device_destroy(struct evdev_device *device)
2057 struct evdev_dispatch *dispatch;
2059 dispatch = device->dispatch;
2061 dispatch->interface->destroy(dispatch);
2063 filter_destroy(device->pointer.filter);
2064 libinput_seat_unref(device->base.seat);
2065 libevdev_free(device->evdev);
2066 udev_device_unref(device->udev_device);
2067 free(device->mt.slots);