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_mode)
129 device->scroll.change_scroll_mode(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.mode == 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 "
243 "same slot", device->devnode);
247 seat_slot = ffs(~seat->slot_map) - 1;
248 device->mt.slots[slot].seat_slot = seat_slot;
253 seat->slot_map |= 1 << seat_slot;
254 x = device->mt.slots[slot].x;
255 y = device->mt.slots[slot].y;
256 transform_absolute(device, &x, &y);
258 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
260 case EVDEV_ABSOLUTE_MT_MOTION:
261 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
264 seat_slot = device->mt.slots[slot].seat_slot;
265 x = device->mt.slots[slot].x;
266 y = device->mt.slots[slot].y;
271 transform_absolute(device, &x, &y);
272 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
274 case EVDEV_ABSOLUTE_MT_UP:
275 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
278 seat_slot = device->mt.slots[slot].seat_slot;
279 device->mt.slots[slot].seat_slot = -1;
284 seat->slot_map &= ~(1 << seat_slot);
286 touch_notify_touch_up(base, time, slot, seat_slot);
288 case EVDEV_ABSOLUTE_TOUCH_DOWN:
289 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
292 if (device->abs.seat_slot != -1) {
293 log_bug_kernel(libinput,
294 "%s: Driver sent multiple touch down for the "
295 "same slot", device->devnode);
299 seat_slot = ffs(~seat->slot_map) - 1;
300 device->abs.seat_slot = seat_slot;
305 seat->slot_map |= 1 << seat_slot;
309 transform_absolute(device, &cx, &cy);
311 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
313 case EVDEV_ABSOLUTE_MOTION:
316 transform_absolute(device, &cx, &cy);
320 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
321 seat_slot = device->abs.seat_slot;
326 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
327 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
328 pointer_notify_motion_absolute(base, time, x, y);
331 case EVDEV_ABSOLUTE_TOUCH_UP:
332 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
335 seat_slot = device->abs.seat_slot;
336 device->abs.seat_slot = -1;
341 seat->slot_map &= ~(1 << seat_slot);
343 touch_notify_touch_up(base, time, -1, seat_slot);
346 assert(0 && "Unknown pending event type");
350 device->pending_event = EVDEV_NONE;
353 static enum evdev_key_type
354 get_key_type(uint16_t code)
356 if (code == BTN_TOUCH)
357 return EVDEV_KEY_TYPE_NONE;
359 if (code >= KEY_ESC && code <= KEY_MICMUTE)
360 return EVDEV_KEY_TYPE_KEY;
361 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
362 return EVDEV_KEY_TYPE_BUTTON;
363 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
364 return EVDEV_KEY_TYPE_KEY;
365 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
366 return EVDEV_KEY_TYPE_BUTTON;
367 return EVDEV_KEY_TYPE_NONE;
371 evdev_button_scroll_timeout(uint64_t time, void *data)
373 struct evdev_device *device = data;
375 device->scroll.button_scroll_active = true;
379 evdev_button_scroll_button(struct evdev_device *device,
380 uint64_t time, int is_press)
383 libinput_timer_set(&device->scroll.timer,
384 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
386 libinput_timer_cancel(&device->scroll.timer);
387 if (device->scroll.button_scroll_active) {
388 evdev_stop_scroll(device, time);
389 device->scroll.button_scroll_active = false;
391 /* If the button is released quickly enough emit the
392 * button press/release events. */
393 evdev_pointer_notify_button(device, time,
394 device->scroll.button,
395 LIBINPUT_BUTTON_STATE_PRESSED);
396 evdev_pointer_notify_button(device, time,
397 device->scroll.button,
398 LIBINPUT_BUTTON_STATE_RELEASED);
404 evdev_process_touch_button(struct evdev_device *device,
405 uint64_t time, int value)
407 if (device->pending_event != EVDEV_NONE &&
408 device->pending_event != EVDEV_ABSOLUTE_MOTION)
409 evdev_flush_pending_event(device, time);
411 device->pending_event = (value ?
412 EVDEV_ABSOLUTE_TOUCH_DOWN :
413 EVDEV_ABSOLUTE_TOUCH_UP);
417 evdev_process_key(struct evdev_device *device,
418 struct input_event *e, uint64_t time)
420 enum evdev_key_type type;
422 /* ignore kernel key repeat */
426 if (e->code == BTN_TOUCH) {
428 evdev_process_touch_button(device, time, e->value);
432 evdev_flush_pending_event(device, time);
434 type = get_key_type(e->code);
436 /* Ignore key release events from the kernel for keys that libinput
437 * never got a pressed event for. */
440 case EVDEV_KEY_TYPE_NONE:
442 case EVDEV_KEY_TYPE_KEY:
443 case EVDEV_KEY_TYPE_BUTTON:
444 if (!hw_is_key_down(device, e->code))
449 hw_set_key_down(device, e->code, e->value);
452 case EVDEV_KEY_TYPE_NONE:
454 case EVDEV_KEY_TYPE_KEY:
455 evdev_keyboard_notify_key(
459 e->value ? LIBINPUT_KEY_STATE_PRESSED :
460 LIBINPUT_KEY_STATE_RELEASED);
462 case EVDEV_KEY_TYPE_BUTTON:
463 if (device->scroll.mode == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
464 e->code == device->scroll.button) {
465 evdev_button_scroll_button(device, time, e->value);
468 evdev_pointer_notify_button(
471 evdev_to_left_handed(device, e->code),
472 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
473 LIBINPUT_BUTTON_STATE_RELEASED);
479 evdev_process_touch(struct evdev_device *device,
480 struct input_event *e,
485 evdev_flush_pending_event(device, time);
486 device->mt.slot = e->value;
488 case ABS_MT_TRACKING_ID:
489 if (device->pending_event != EVDEV_NONE &&
490 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
491 evdev_flush_pending_event(device, time);
493 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
495 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
497 case ABS_MT_POSITION_X:
498 device->mt.slots[device->mt.slot].x = e->value;
499 if (device->pending_event == EVDEV_NONE)
500 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
502 case ABS_MT_POSITION_Y:
503 device->mt.slots[device->mt.slot].y = e->value;
504 if (device->pending_event == EVDEV_NONE)
505 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
511 evdev_process_absolute_motion(struct evdev_device *device,
512 struct input_event *e)
516 device->abs.x = e->value;
517 if (device->pending_event == EVDEV_NONE)
518 device->pending_event = EVDEV_ABSOLUTE_MOTION;
521 device->abs.y = e->value;
522 if (device->pending_event == EVDEV_NONE)
523 device->pending_event = EVDEV_ABSOLUTE_MOTION;
529 evdev_process_relative(struct evdev_device *device,
530 struct input_event *e, uint64_t time)
532 struct libinput_device *base = &device->base;
536 if (device->pending_event != EVDEV_RELATIVE_MOTION)
537 evdev_flush_pending_event(device, time);
538 device->rel.dx += e->value;
539 device->pending_event = EVDEV_RELATIVE_MOTION;
542 if (device->pending_event != EVDEV_RELATIVE_MOTION)
543 evdev_flush_pending_event(device, time);
544 device->rel.dy += e->value;
545 device->pending_event = EVDEV_RELATIVE_MOTION;
548 evdev_flush_pending_event(device, time);
552 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
553 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
556 evdev_flush_pending_event(device, time);
560 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
561 e->value * DEFAULT_AXIS_STEP_DISTANCE);
567 evdev_process_absolute(struct evdev_device *device,
568 struct input_event *e,
572 evdev_process_touch(device, e, time);
574 evdev_process_absolute_motion(device, e);
579 evdev_any_button_down(struct evdev_device *device)
583 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
584 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
585 hw_is_key_down(device, button))
592 evdev_need_touch_frame(struct evdev_device *device)
594 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
597 switch (device->pending_event) {
599 case EVDEV_RELATIVE_MOTION:
601 case EVDEV_ABSOLUTE_MT_DOWN:
602 case EVDEV_ABSOLUTE_MT_MOTION:
603 case EVDEV_ABSOLUTE_MT_UP:
604 case EVDEV_ABSOLUTE_TOUCH_DOWN:
605 case EVDEV_ABSOLUTE_TOUCH_UP:
606 case EVDEV_ABSOLUTE_MOTION:
614 evdev_tag_external_mouse(struct evdev_device *device,
615 struct udev_device *udev_device)
619 bustype = libevdev_get_id_bustype(device->evdev);
620 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
621 if (device->seat_caps & EVDEV_DEVICE_POINTER)
622 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
627 evdev_tag_trackpoint(struct evdev_device *device,
628 struct udev_device *udev_device)
630 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
631 device->tags |= EVDEV_TAG_TRACKPOINT;
635 fallback_process(struct evdev_dispatch *dispatch,
636 struct evdev_device *device,
637 struct input_event *event,
640 bool need_frame = false;
642 switch (event->type) {
644 evdev_process_relative(device, event, time);
647 evdev_process_absolute(device, event, time);
650 evdev_process_key(device, event, time);
653 need_frame = evdev_need_touch_frame(device);
654 evdev_flush_pending_event(device, time);
656 touch_notify_frame(&device->base, time);
662 fallback_destroy(struct evdev_dispatch *dispatch)
668 fallback_tag_device(struct evdev_device *device,
669 struct udev_device *udev_device)
671 evdev_tag_external_mouse(device, udev_device);
672 evdev_tag_trackpoint(device, udev_device);
676 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
678 struct evdev_device *device = (struct evdev_device*)libinput_device;
680 return device->abs.absinfo_x && device->abs.absinfo_y;
683 static enum libinput_config_status
684 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
685 const float matrix[6])
687 struct evdev_device *device = (struct evdev_device*)libinput_device;
689 evdev_device_calibrate(device, matrix);
691 return LIBINPUT_CONFIG_STATUS_SUCCESS;
695 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
698 struct evdev_device *device = (struct evdev_device*)libinput_device;
700 matrix_to_farray6(&device->abs.usermatrix, matrix);
702 return !matrix_is_identity(&device->abs.usermatrix);
706 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
709 struct evdev_device *device = (struct evdev_device*)libinput_device;
711 matrix_to_farray6(&device->abs.default_calibration, matrix);
713 return !matrix_is_identity(&device->abs.default_calibration);
716 struct evdev_dispatch_interface fallback_interface = {
719 NULL, /* device_added */
720 NULL, /* device_removed */
721 NULL, /* device_suspended */
722 NULL, /* device_resumed */
727 evdev_sendevents_get_modes(struct libinput_device *device)
729 return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
732 static enum libinput_config_status
733 evdev_sendevents_set_mode(struct libinput_device *device,
734 enum libinput_config_send_events_mode mode)
736 struct evdev_device *evdev = (struct evdev_device*)device;
737 struct evdev_dispatch *dispatch = evdev->dispatch;
739 if (mode == dispatch->sendevents.current_mode)
740 return LIBINPUT_CONFIG_STATUS_SUCCESS;
743 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
744 evdev_device_resume(evdev);
746 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
747 evdev_device_suspend(evdev);
749 default: /* no support for combined modes yet */
750 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
753 dispatch->sendevents.current_mode = mode;
755 return LIBINPUT_CONFIG_STATUS_SUCCESS;
758 static enum libinput_config_send_events_mode
759 evdev_sendevents_get_mode(struct libinput_device *device)
761 struct evdev_device *evdev = (struct evdev_device*)device;
762 struct evdev_dispatch *dispatch = evdev->dispatch;
764 return dispatch->sendevents.current_mode;
767 static enum libinput_config_send_events_mode
768 evdev_sendevents_get_default_mode(struct libinput_device *device)
770 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
774 evdev_left_handed_has(struct libinput_device *device)
776 /* This is only hooked up when we have left-handed configuration, so we
777 * can hardcode 1 here */
782 evdev_change_to_left_handed(struct evdev_device *device)
784 if (device->buttons.want_left_handed == device->buttons.left_handed)
787 if (evdev_any_button_down(device))
790 device->buttons.left_handed = device->buttons.want_left_handed;
793 static enum libinput_config_status
794 evdev_left_handed_set(struct libinput_device *device, int left_handed)
796 struct evdev_device *evdev_device = (struct evdev_device *)device;
798 evdev_device->buttons.want_left_handed = left_handed ? true : false;
800 evdev_device->buttons.change_to_left_handed(evdev_device);
802 return LIBINPUT_CONFIG_STATUS_SUCCESS;
806 evdev_left_handed_get(struct libinput_device *device)
808 struct evdev_device *evdev_device = (struct evdev_device *)device;
810 /* return the wanted configuration, even if it hasn't taken
812 return evdev_device->buttons.want_left_handed;
816 evdev_left_handed_get_default(struct libinput_device *device)
822 evdev_init_left_handed(struct evdev_device *device,
823 void (*change_to_left_handed)(struct evdev_device *))
825 device->buttons.config_left_handed.has = evdev_left_handed_has;
826 device->buttons.config_left_handed.set = evdev_left_handed_set;
827 device->buttons.config_left_handed.get = evdev_left_handed_get;
828 device->buttons.config_left_handed.get_default = evdev_left_handed_get_default;
829 device->base.config.left_handed = &device->buttons.config_left_handed;
830 device->buttons.left_handed = false;
831 device->buttons.want_left_handed = false;
832 device->buttons.change_to_left_handed = change_to_left_handed;
838 evdev_scroll_get_modes(struct libinput_device *device)
840 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
844 evdev_change_scroll_mode(struct evdev_device *device)
846 if (device->scroll.want_mode == device->scroll.mode &&
847 device->scroll.want_button == device->scroll.button)
850 if (evdev_any_button_down(device))
853 device->scroll.mode = device->scroll.want_mode;
854 device->scroll.button = device->scroll.want_button;
857 static enum libinput_config_status
858 evdev_scroll_set_mode(struct libinput_device *device,
859 enum libinput_config_scroll_mode mode)
861 struct evdev_device *evdev = (struct evdev_device*)device;
863 evdev->scroll.want_mode = mode;
864 evdev->scroll.change_scroll_mode(evdev);
866 return LIBINPUT_CONFIG_STATUS_SUCCESS;
869 static enum libinput_config_scroll_mode
870 evdev_scroll_get_mode(struct libinput_device *device)
872 struct evdev_device *evdev = (struct evdev_device *)device;
874 /* return the wanted configuration, even if it hasn't taken
876 return evdev->scroll.want_mode;
879 static enum libinput_config_scroll_mode
880 evdev_scroll_get_default_mode(struct libinput_device *device)
882 struct evdev_device *evdev = (struct evdev_device *)device;
884 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
885 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
887 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
890 static enum libinput_config_status
891 evdev_scroll_set_button(struct libinput_device *device,
894 struct evdev_device *evdev = (struct evdev_device*)device;
896 evdev->scroll.want_button = button;
897 evdev->scroll.change_scroll_mode(evdev);
899 return LIBINPUT_CONFIG_STATUS_SUCCESS;
903 evdev_scroll_get_button(struct libinput_device *device)
905 struct evdev_device *evdev = (struct evdev_device *)device;
907 /* return the wanted configuration, even if it hasn't taken
909 return evdev->scroll.want_button;
913 evdev_scroll_get_default_button(struct libinput_device *device)
915 struct evdev_device *evdev = (struct evdev_device *)device;
917 if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
924 evdev_init_button_scroll(struct evdev_device *device,
925 void (*change_scroll_mode)(struct evdev_device *))
927 libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
928 evdev_button_scroll_timeout, device);
929 device->scroll.config.get_modes = evdev_scroll_get_modes;
930 device->scroll.config.set_mode = evdev_scroll_set_mode;
931 device->scroll.config.get_mode = evdev_scroll_get_mode;
932 device->scroll.config.get_default_mode = evdev_scroll_get_default_mode;
933 device->scroll.config.set_button = evdev_scroll_set_button;
934 device->scroll.config.get_button = evdev_scroll_get_button;
935 device->scroll.config.get_default_button = evdev_scroll_get_default_button;
936 device->base.config.scroll_mode = &device->scroll.config;
937 device->scroll.mode = evdev_scroll_get_default_mode((struct libinput_device *)device);
938 device->scroll.want_mode = device->scroll.mode;
939 device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
940 device->scroll.want_button = device->scroll.button;
941 device->scroll.change_scroll_mode = change_scroll_mode;
946 static struct evdev_dispatch *
947 fallback_dispatch_create(struct libinput_device *device)
949 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
950 struct evdev_device *evdev_device = (struct evdev_device *)device;
952 if (dispatch == NULL)
955 dispatch->interface = &fallback_interface;
957 if (evdev_device->buttons.want_left_handed &&
958 evdev_init_left_handed(evdev_device,
959 evdev_change_to_left_handed) == -1) {
964 if (evdev_device->scroll.want_button &&
965 evdev_init_button_scroll(evdev_device,
966 evdev_change_scroll_mode) == -1) {
971 device->config.calibration = &dispatch->calibration;
973 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
974 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
975 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
976 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
978 device->config.sendevents = &dispatch->sendevents.config;
980 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
981 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
982 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
983 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
984 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
990 evdev_process_event(struct evdev_device *device, struct input_event *e)
992 struct evdev_dispatch *dispatch = device->dispatch;
993 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
995 dispatch->interface->process(dispatch, device, e, time);
999 evdev_device_dispatch_one(struct evdev_device *device,
1000 struct input_event *ev)
1002 if (!device->mtdev) {
1003 evdev_process_event(device, ev);
1005 mtdev_put_event(device->mtdev, ev);
1006 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1007 while (!mtdev_empty(device->mtdev)) {
1008 struct input_event e;
1009 mtdev_get_event(device->mtdev, &e);
1010 evdev_process_event(device, &e);
1017 evdev_sync_device(struct evdev_device *device)
1019 struct input_event ev;
1023 rc = libevdev_next_event(device->evdev,
1024 LIBEVDEV_READ_FLAG_SYNC, &ev);
1027 evdev_device_dispatch_one(device, &ev);
1028 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1030 return rc == -EAGAIN ? 0 : rc;
1034 evdev_device_dispatch(void *data)
1036 struct evdev_device *device = data;
1037 struct libinput *libinput = device->base.seat->libinput;
1038 struct input_event ev;
1041 /* If the compositor is repainting, this function is called only once
1042 * per frame and we have to process all the events available on the
1043 * fd, otherwise there will be input lag. */
1045 rc = libevdev_next_event(device->evdev,
1046 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1047 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1048 switch (ratelimit_test(&device->syn_drop_limit)) {
1049 case RATELIMIT_PASS:
1050 log_info(libinput, "SYN_DROPPED event from "
1051 "\"%s\" - some input events have "
1052 "been lost.\n", device->devname);
1054 case RATELIMIT_THRESHOLD:
1055 log_info(libinput, "SYN_DROPPED flood "
1059 case RATELIMIT_EXCEEDED:
1063 /* send one more sync event so we handle all
1064 currently pending events before we sync up
1065 to the current state */
1066 ev.code = SYN_REPORT;
1067 evdev_device_dispatch_one(device, &ev);
1069 rc = evdev_sync_device(device);
1071 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1072 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1073 evdev_device_dispatch_one(device, &ev);
1075 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1077 if (rc != -EAGAIN && rc != -EINTR) {
1078 libinput_remove_source(libinput, device->source);
1079 device->source = NULL;
1084 evdev_accel_config_available(struct libinput_device *device)
1086 /* this function is only called if we set up ptraccel, so we can
1087 reply with a resounding "Yes" */
1091 static enum libinput_config_status
1092 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1094 struct evdev_device *dev = (struct evdev_device *)device;
1096 if (!filter_set_speed(dev->pointer.filter, speed))
1097 return LIBINPUT_CONFIG_STATUS_INVALID;
1099 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1103 evdev_accel_config_get_speed(struct libinput_device *device)
1105 struct evdev_device *dev = (struct evdev_device *)device;
1107 return filter_get_speed(dev->pointer.filter);
1111 evdev_accel_config_get_default_speed(struct libinput_device *device)
1117 evdev_device_init_pointer_acceleration(struct evdev_device *device)
1119 device->pointer.filter =
1120 create_pointer_accelerator_filter(
1121 pointer_accel_profile_linear);
1122 if (!device->pointer.filter)
1125 device->pointer.config.available = evdev_accel_config_available;
1126 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1127 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1128 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1129 device->base.config.accel = &device->pointer.config;
1136 evdev_need_mtdev(struct evdev_device *device)
1138 struct libevdev *evdev = device->evdev;
1140 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1141 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1142 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1146 evdev_tag_device(struct evdev_device *device)
1149 struct udev_device *udev_device = NULL;
1155 udev_device = udev_device_new_from_syspath(udev, device->syspath);
1157 if (device->dispatch->interface->tag_device)
1158 device->dispatch->interface->tag_device(device, udev_device);
1159 udev_device_unref(udev_device);
1165 evdev_fix_abs_resolution(struct libevdev *evdev,
1167 const struct input_absinfo *absinfo)
1169 struct input_absinfo fixed;
1171 if (absinfo->resolution == 0) {
1173 fixed.resolution = 1;
1174 /* libevdev_set_abs_info() changes the absinfo we already
1175 have a pointer to, no need to fetch it again */
1176 libevdev_set_abs_info(evdev, code, &fixed);
1184 evdev_configure_device(struct evdev_device *device)
1186 struct libinput *libinput = device->base.seat->libinput;
1187 struct libevdev *evdev = device->evdev;
1188 const struct input_absinfo *absinfo;
1189 int has_abs, has_rel, has_mt;
1190 int has_button, has_keyboard, has_touch;
1191 struct mt_slot *slots;
1204 if (libevdev_has_event_type(evdev, EV_ABS)) {
1206 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1207 if (evdev_fix_abs_resolution(evdev,
1210 device->abs.fake_resolution = 1;
1211 device->abs.absinfo_x = absinfo;
1214 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1215 if (evdev_fix_abs_resolution(evdev,
1218 device->abs.fake_resolution = 1;
1219 device->abs.absinfo_y = absinfo;
1223 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1224 the limited ABS_* range - they aren't MT devices, they
1225 just have too many ABS_ axes */
1226 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1227 libevdev_get_num_slots(evdev) == -1) {
1230 } else if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1231 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1232 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1233 if (evdev_fix_abs_resolution(evdev,
1236 device->abs.fake_resolution = 1;
1237 device->abs.absinfo_x = absinfo;
1239 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1240 if (evdev_fix_abs_resolution(evdev,
1243 device->abs.fake_resolution = 1;
1244 device->abs.absinfo_y = absinfo;
1249 /* We only handle the slotted Protocol B in libinput.
1250 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1251 require mtdev for conversion. */
1252 if (evdev_need_mtdev(device)) {
1253 device->mtdev = mtdev_new_open(device->fd);
1257 num_slots = device->mtdev->caps.slot.maximum;
1258 if (device->mtdev->caps.slot.minimum < 0 ||
1261 active_slot = device->mtdev->caps.slot.value;
1263 num_slots = libevdev_get_num_slots(device->evdev);
1264 active_slot = libevdev_get_current_slot(evdev);
1267 slots = calloc(num_slots, sizeof(struct mt_slot));
1271 for (slot = 0; slot < num_slots; ++slot) {
1272 slots[slot].seat_slot = -1;
1276 device->mt.slots = slots;
1277 device->mt.slots_len = num_slots;
1278 device->mt.slot = active_slot;
1282 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1283 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1286 if (libevdev_has_event_type(evdev, EV_KEY)) {
1287 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1288 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1289 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1290 (has_abs || has_mt)) {
1291 device->dispatch = evdev_mt_touchpad_create(device);
1293 "input device '%s', %s is a touchpad\n",
1294 device->devname, device->devnode);
1295 return device->dispatch == NULL ? -1 : 0;
1298 for (i = 0; i < KEY_MAX; i++) {
1299 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1300 switch (get_key_type(i)) {
1301 case EVDEV_KEY_TYPE_NONE:
1303 case EVDEV_KEY_TYPE_KEY:
1306 case EVDEV_KEY_TYPE_BUTTON:
1313 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1316 if (libevdev_has_event_type(evdev, EV_LED))
1319 if ((has_abs || has_rel) && has_button) {
1320 if (evdev_device_init_pointer_acceleration(device) == -1)
1323 device->seat_caps |= EVDEV_DEVICE_POINTER;
1326 "input device '%s', %s is a pointer caps =%s%s%s\n",
1327 device->devname, device->devnode,
1328 has_abs ? " absolute-motion" : "",
1329 has_rel ? " relative-motion": "",
1330 has_button ? " button" : "");
1332 /* want left-handed config option */
1333 device->buttons.want_left_handed = true;
1336 if (has_rel && has_button) {
1337 /* want button scrolling config option */
1338 device->scroll.want_button = 1;
1342 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1344 "input device '%s', %s is a keyboard\n",
1345 device->devname, device->devnode);
1347 if (has_touch && !has_button) {
1348 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1350 "input device '%s', %s is a touch device\n",
1351 device->devname, device->devnode);
1358 evdev_notify_added_device(struct evdev_device *device)
1360 struct libinput_device *dev;
1362 list_for_each(dev, &device->base.seat->devices_list, link) {
1363 struct evdev_device *d = (struct evdev_device*)dev;
1364 if (dev == &device->base)
1367 /* Notify existing device d about addition of device device */
1368 if (d->dispatch->interface->device_added)
1369 d->dispatch->interface->device_added(d, device);
1371 /* Notify new device device about existing device d */
1372 if (device->dispatch->interface->device_added)
1373 device->dispatch->interface->device_added(device, d);
1375 /* Notify new device device if existing device d is suspended */
1376 if (d->suspended && device->dispatch->interface->device_suspended)
1377 device->dispatch->interface->device_suspended(device, d);
1380 notify_added_device(&device->base);
1383 struct evdev_device *
1384 evdev_device_create(struct libinput_seat *seat,
1385 const char *devnode,
1386 const char *sysname,
1387 const char *syspath)
1389 struct libinput *libinput = seat->libinput;
1390 struct evdev_device *device;
1393 int unhandled_device = 0;
1395 /* Use non-blocking mode so that we can loop on read on
1396 * evdev_device_data() until all events on the fd are
1397 * read. mtdev_get() also expects this. */
1398 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1401 "opening input device '%s' failed (%s).\n",
1402 devnode, strerror(-fd));
1406 device = zalloc(sizeof *device);
1410 libinput_device_init(&device->base, seat);
1411 libinput_seat_ref(seat);
1413 rc = libevdev_new_from_fd(fd, &device->evdev);
1417 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1419 device->seat_caps = 0;
1421 device->mtdev = NULL;
1422 device->devnode = strdup(devnode);
1423 device->sysname = strdup(sysname);
1424 device->syspath = strdup(syspath);
1427 device->abs.seat_slot = -1;
1428 device->dispatch = NULL;
1430 device->pending_event = EVDEV_NONE;
1431 device->devname = libevdev_get_name(device->evdev);
1432 device->scroll.threshold = 5.0; /* Default may be overridden */
1433 device->scroll.direction = 0;
1434 device->dpi = DEFAULT_MOUSE_DPI;
1435 /* at most 5 SYN_DROPPED log-messages per 30s */
1436 ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1438 matrix_init_identity(&device->abs.calibration);
1439 matrix_init_identity(&device->abs.usermatrix);
1440 matrix_init_identity(&device->abs.default_calibration);
1442 if (evdev_configure_device(device) == -1)
1445 if (device->seat_caps == 0) {
1446 unhandled_device = 1;
1450 /* If the dispatch was not set up use the fallback. */
1451 if (device->dispatch == NULL)
1452 device->dispatch = fallback_dispatch_create(&device->base);
1453 if (device->dispatch == NULL)
1457 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1458 if (!device->source)
1461 list_insert(seat->devices_list.prev, &device->base.link);
1463 evdev_tag_device(device);
1464 evdev_notify_added_device(device);
1470 close_restricted(libinput, fd);
1471 evdev_device_destroy(device);
1473 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1477 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1479 memset(keys, 0, size);
1484 evdev_device_get_output(struct evdev_device *device)
1486 return device->output_name;
1490 evdev_device_get_sysname(struct evdev_device *device)
1492 return device->sysname;
1496 evdev_device_get_name(struct evdev_device *device)
1498 return device->devname;
1502 evdev_device_get_id_product(struct evdev_device *device)
1504 return libevdev_get_id_product(device->evdev);
1508 evdev_device_get_id_vendor(struct evdev_device *device)
1510 return libevdev_get_id_vendor(device->evdev);
1514 evdev_device_set_default_calibration(struct evdev_device *device,
1515 const float calibration[6])
1517 matrix_from_farray6(&device->abs.default_calibration, calibration);
1518 evdev_device_calibrate(device, calibration);
1522 evdev_device_calibrate(struct evdev_device *device,
1523 const float calibration[6])
1525 struct matrix scale,
1530 matrix_from_farray6(&transform, calibration);
1531 device->abs.apply_calibration = !matrix_is_identity(&transform);
1533 if (!device->abs.apply_calibration) {
1534 matrix_init_identity(&device->abs.calibration);
1538 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1539 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1541 /* The transformation matrix is in the form:
1545 * Where a, e are the scale components, a, b, d, e are the rotation
1546 * component (combined with scale) and c and f are the translation
1547 * component. The translation component in the input matrix must be
1548 * normalized to multiples of the device width and height,
1549 * respectively. e.g. c == 1 shifts one device-width to the right.
1551 * We pre-calculate a single matrix to apply to event coordinates:
1552 * M = Un-Normalize * Calibration * Normalize
1554 * Normalize: scales the device coordinates to [0,1]
1555 * Calibration: user-supplied matrix
1556 * Un-Normalize: scales back up to device coordinates
1557 * Matrix maths requires the normalize/un-normalize in reverse
1561 /* back up the user matrix so we can return it on request */
1562 matrix_from_farray6(&device->abs.usermatrix, calibration);
1565 matrix_init_translate(&translate,
1566 device->abs.absinfo_x->minimum,
1567 device->abs.absinfo_y->minimum);
1568 matrix_init_scale(&scale, sx, sy);
1569 matrix_mult(&scale, &translate, &scale);
1572 matrix_mult(&transform, &scale, &transform);
1575 matrix_init_translate(&translate,
1576 -device->abs.absinfo_x->minimum/sx,
1577 -device->abs.absinfo_y->minimum/sy);
1578 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1579 matrix_mult(&scale, &translate, &scale);
1581 /* store final matrix in device */
1582 matrix_mult(&device->abs.calibration, &transform, &scale);
1586 evdev_device_has_capability(struct evdev_device *device,
1587 enum libinput_device_capability capability)
1589 switch (capability) {
1590 case LIBINPUT_DEVICE_CAP_POINTER:
1591 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1592 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1593 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1594 case LIBINPUT_DEVICE_CAP_TOUCH:
1595 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1602 evdev_device_get_size(struct evdev_device *device,
1606 const struct input_absinfo *x, *y;
1608 x = libevdev_get_abs_info(device->evdev, ABS_X);
1609 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1611 if (!x || !y || device->abs.fake_resolution ||
1612 !x->resolution || !y->resolution)
1615 *width = evdev_convert_to_mm(x, x->maximum);
1616 *height = evdev_convert_to_mm(y, y->maximum);
1622 evdev_device_has_button(struct evdev_device *device, uint32_t code)
1624 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
1627 return libevdev_has_event_code(device->evdev, EV_KEY, code);
1631 evdev_is_scrolling(const struct evdev_device *device,
1632 enum libinput_pointer_axis axis)
1634 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1635 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1637 return (device->scroll.direction & (1 << axis)) != 0;
1641 evdev_start_scrolling(struct evdev_device *device,
1642 enum libinput_pointer_axis axis)
1644 assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1645 axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1647 device->scroll.direction |= (1 << axis);
1651 evdev_post_scroll(struct evdev_device *device,
1656 double trigger_horiz, trigger_vert;
1658 if (!evdev_is_scrolling(device,
1659 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1660 device->scroll.buildup_vertical += dy;
1661 if (!evdev_is_scrolling(device,
1662 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1663 device->scroll.buildup_horizontal += dx;
1665 trigger_vert = device->scroll.buildup_vertical;
1666 trigger_horiz = device->scroll.buildup_horizontal;
1668 /* If we're not scrolling yet, use a distance trigger: moving
1669 past a certain distance starts scrolling */
1670 if (!evdev_is_scrolling(device,
1671 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
1672 !evdev_is_scrolling(device,
1673 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1674 if (fabs(trigger_vert) >= device->scroll.threshold)
1675 evdev_start_scrolling(device,
1676 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1677 if (fabs(trigger_horiz) >= device->scroll.threshold)
1678 evdev_start_scrolling(device,
1679 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1680 /* We're already scrolling in one direction. Require some
1681 trigger speed to start scrolling in the other direction */
1682 } else if (!evdev_is_scrolling(device,
1683 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1684 if (fabs(dy) >= device->scroll.threshold)
1685 evdev_start_scrolling(device,
1686 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1687 } else if (!evdev_is_scrolling(device,
1688 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1689 if (fabs(dx) >= device->scroll.threshold)
1690 evdev_start_scrolling(device,
1691 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1694 /* We use the trigger to enable, but the delta from this event for
1695 * the actual scroll movement. Otherwise we get a jump once
1696 * scrolling engages */
1698 evdev_is_scrolling(device,
1699 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1700 pointer_notify_axis(&device->base,
1702 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1707 evdev_is_scrolling(device,
1708 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1709 pointer_notify_axis(&device->base,
1711 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1717 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1719 /* terminate scrolling with a zero scroll event */
1720 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1721 pointer_notify_axis(&device->base,
1723 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1725 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1726 pointer_notify_axis(&device->base,
1728 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1731 device->scroll.buildup_horizontal = 0;
1732 device->scroll.buildup_vertical = 0;
1733 device->scroll.direction = 0;
1737 release_pressed_keys(struct evdev_device *device)
1739 struct libinput *libinput = device->base.seat->libinput;
1743 if ((time = libinput_now(libinput)) == 0)
1746 for (code = 0; code < KEY_CNT; code++) {
1747 int count = get_key_down_count(device, code);
1750 log_bug_libinput(libinput,
1751 "Key %d is down %d times.\n",
1756 while (get_key_down_count(device, code) > 0) {
1757 switch (get_key_type(code)) {
1758 case EVDEV_KEY_TYPE_NONE:
1760 case EVDEV_KEY_TYPE_KEY:
1761 evdev_keyboard_notify_key(
1765 LIBINPUT_KEY_STATE_RELEASED);
1767 case EVDEV_KEY_TYPE_BUTTON:
1768 evdev_pointer_notify_button(
1771 evdev_to_left_handed(device, code),
1772 LIBINPUT_BUTTON_STATE_RELEASED);
1780 evdev_notify_suspended_device(struct evdev_device *device)
1782 struct libinput_device *it;
1784 if (device->suspended)
1787 list_for_each(it, &device->base.seat->devices_list, link) {
1788 struct evdev_device *d = (struct evdev_device*)it;
1789 if (it == &device->base)
1792 if (d->dispatch->interface->device_suspended)
1793 d->dispatch->interface->device_suspended(d, device);
1796 device->suspended = 1;
1800 evdev_notify_resumed_device(struct evdev_device *device)
1802 struct libinput_device *it;
1804 if (!device->suspended)
1807 list_for_each(it, &device->base.seat->devices_list, link) {
1808 struct evdev_device *d = (struct evdev_device*)it;
1809 if (it == &device->base)
1812 if (d->dispatch->interface->device_resumed)
1813 d->dispatch->interface->device_resumed(d, device);
1816 device->suspended = 0;
1820 evdev_device_suspend(struct evdev_device *device)
1822 evdev_notify_suspended_device(device);
1824 if (device->source) {
1825 libinput_remove_source(device->base.seat->libinput,
1827 device->source = NULL;
1830 release_pressed_keys(device);
1832 if (device->mtdev) {
1833 mtdev_close_delete(device->mtdev);
1834 device->mtdev = NULL;
1837 if (device->fd != -1) {
1838 close_restricted(device->base.seat->libinput, device->fd);
1846 evdev_device_compare_syspath(struct evdev_device *device, int fd)
1848 struct udev *udev = NULL;
1849 struct udev_device *udev_device = NULL;
1850 const char *syspath;
1858 if (fstat(fd, &st) < 0)
1861 udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1865 syspath = udev_device_get_syspath(udev_device);
1866 rc = strcmp(syspath, device->syspath);
1869 udev_device_unref(udev_device);
1876 evdev_device_resume(struct evdev_device *device)
1878 struct libinput *libinput = device->base.seat->libinput;
1881 if (device->fd != -1)
1884 if (device->syspath == NULL)
1887 fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1892 if (evdev_device_compare_syspath(device, fd)) {
1893 close_restricted(libinput, fd);
1899 if (evdev_need_mtdev(device)) {
1900 device->mtdev = mtdev_new_open(device->fd);
1906 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1907 if (!device->source) {
1908 mtdev_close_delete(device->mtdev);
1912 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1914 evdev_notify_resumed_device(device);
1920 evdev_device_remove(struct evdev_device *device)
1922 struct libinput_device *dev;
1924 list_for_each(dev, &device->base.seat->devices_list, link) {
1925 struct evdev_device *d = (struct evdev_device*)dev;;
1926 if (dev == &device->base)
1929 if (d->dispatch->interface->device_removed)
1930 d->dispatch->interface->device_removed(d, device);
1933 evdev_device_suspend(device);
1935 /* A device may be removed while suspended. Free the syspath to
1936 * skip re-opening a different device with the same node */
1937 free(device->syspath);
1938 device->syspath = NULL;
1940 list_remove(&device->base.link);
1942 notify_removed_device(&device->base);
1943 libinput_device_unref(&device->base);
1947 evdev_device_destroy(struct evdev_device *device)
1949 struct evdev_dispatch *dispatch;
1951 dispatch = device->dispatch;
1953 dispatch->interface->destroy(dispatch);
1955 filter_destroy(device->pointer.filter);
1956 libinput_seat_unref(device->base.seat);
1957 libevdev_free(device->evdev);
1958 free(device->mt.slots);
1959 free(device->devnode);
1960 free(device->sysname);
1961 free(device->syspath);