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);
129 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
131 static const struct {
132 enum libinput_led weston;
135 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
136 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
137 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
139 struct input_event ev[ARRAY_LENGTH(map) + 1];
142 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
145 memset(ev, 0, sizeof(ev));
146 for (i = 0; i < ARRAY_LENGTH(map); i++) {
148 ev[i].code = map[i].evdev;
149 ev[i].value = !!(leds & map[i].weston);
152 ev[i].code = SYN_REPORT;
154 i = write(device->fd, ev, sizeof ev);
155 (void)i; /* no, we really don't care about the return value */
159 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
161 if (!device->abs.apply_calibration)
164 matrix_mult_vec(&device->abs.calibration, x, y);
168 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
170 return (val - absinfo->minimum) * to_range /
171 (absinfo->maximum - absinfo->minimum + 1);
175 evdev_device_transform_x(struct evdev_device *device,
179 return scale_axis(device->abs.absinfo_x, x, width);
183 evdev_device_transform_y(struct evdev_device *device,
187 return scale_axis(device->abs.absinfo_y, y, height);
191 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
193 struct libinput *libinput = device->base.seat->libinput;
194 struct motion_params motion;
199 struct libinput_device *base = &device->base;
200 struct libinput_seat *seat = base->seat;
202 slot = device->mt.slot;
204 switch (device->pending_event) {
207 case EVDEV_RELATIVE_MOTION:
208 motion.dx = device->rel.dx;
209 motion.dy = device->rel.dy;
213 /* Use unaccelerated deltas for pointing stick scroll */
214 if (device->scroll.has_middle_button_scroll &&
215 hw_is_key_down(device, BTN_MIDDLE)) {
216 if (device->scroll.middle_button_scroll_active)
217 evdev_post_scroll(device, time,
218 motion.dx, motion.dy);
222 /* Apply pointer acceleration. */
223 filter_dispatch(device->pointer.filter, &motion, device, time);
225 if (motion.dx == 0.0 && motion.dy == 0.0)
228 pointer_notify_motion(base, time, motion.dx, motion.dy);
230 case EVDEV_ABSOLUTE_MT_DOWN:
231 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
234 if (device->mt.slots[slot].seat_slot != -1) {
235 log_bug_kernel(libinput,
236 "%s: Driver sent multiple touch down for the "
237 "same slot", device->devnode);
241 seat_slot = ffs(~seat->slot_map) - 1;
242 device->mt.slots[slot].seat_slot = seat_slot;
247 seat->slot_map |= 1 << seat_slot;
248 x = device->mt.slots[slot].x;
249 y = device->mt.slots[slot].y;
250 transform_absolute(device, &x, &y);
252 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
254 case EVDEV_ABSOLUTE_MT_MOTION:
255 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
258 seat_slot = device->mt.slots[slot].seat_slot;
259 x = device->mt.slots[slot].x;
260 y = device->mt.slots[slot].y;
265 transform_absolute(device, &x, &y);
266 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
268 case EVDEV_ABSOLUTE_MT_UP:
269 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
272 seat_slot = device->mt.slots[slot].seat_slot;
273 device->mt.slots[slot].seat_slot = -1;
278 seat->slot_map &= ~(1 << seat_slot);
280 touch_notify_touch_up(base, time, slot, seat_slot);
282 case EVDEV_ABSOLUTE_TOUCH_DOWN:
283 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
286 if (device->abs.seat_slot != -1) {
287 log_bug_kernel(libinput,
288 "%s: Driver sent multiple touch down for the "
289 "same slot", device->devnode);
293 seat_slot = ffs(~seat->slot_map) - 1;
294 device->abs.seat_slot = seat_slot;
299 seat->slot_map |= 1 << seat_slot;
303 transform_absolute(device, &cx, &cy);
305 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
307 case EVDEV_ABSOLUTE_MOTION:
310 transform_absolute(device, &cx, &cy);
314 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
315 seat_slot = device->abs.seat_slot;
320 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
321 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
322 pointer_notify_motion_absolute(base, time, x, y);
325 case EVDEV_ABSOLUTE_TOUCH_UP:
326 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
329 seat_slot = device->abs.seat_slot;
330 device->abs.seat_slot = -1;
335 seat->slot_map &= ~(1 << seat_slot);
337 touch_notify_touch_up(base, time, -1, seat_slot);
340 assert(0 && "Unknown pending event type");
344 device->pending_event = EVDEV_NONE;
347 static enum evdev_key_type
348 get_key_type(uint16_t code)
350 if (code == BTN_TOUCH)
351 return EVDEV_KEY_TYPE_NONE;
353 if (code >= KEY_ESC && code <= KEY_MICMUTE)
354 return EVDEV_KEY_TYPE_KEY;
355 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
356 return EVDEV_KEY_TYPE_BUTTON;
357 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
358 return EVDEV_KEY_TYPE_KEY;
359 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
360 return EVDEV_KEY_TYPE_BUTTON;
361 return EVDEV_KEY_TYPE_NONE;
365 evdev_middle_button_scroll_timeout(uint64_t time, void *data)
367 struct evdev_device *device = data;
369 device->scroll.middle_button_scroll_active = true;
373 evdev_middle_button_scroll_button(struct evdev_device *device,
374 uint64_t time, int is_press)
377 libinput_timer_set(&device->scroll.timer,
378 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
380 libinput_timer_cancel(&device->scroll.timer);
381 if (device->scroll.middle_button_scroll_active) {
382 evdev_stop_scroll(device, time);
383 device->scroll.middle_button_scroll_active = false;
385 /* If the button is released quickly enough emit the
386 * button press/release events. */
387 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
388 LIBINPUT_BUTTON_STATE_PRESSED);
389 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
390 LIBINPUT_BUTTON_STATE_RELEASED);
396 evdev_process_touch_button(struct evdev_device *device,
397 uint64_t time, int value)
399 if (device->pending_event != EVDEV_NONE &&
400 device->pending_event != EVDEV_ABSOLUTE_MOTION)
401 evdev_flush_pending_event(device, time);
403 device->pending_event = (value ?
404 EVDEV_ABSOLUTE_TOUCH_DOWN :
405 EVDEV_ABSOLUTE_TOUCH_UP);
409 evdev_process_key(struct evdev_device *device,
410 struct input_event *e, uint64_t time)
412 enum evdev_key_type type;
414 /* ignore kernel key repeat */
418 if (e->code == BTN_TOUCH) {
420 evdev_process_touch_button(device, time, e->value);
424 evdev_flush_pending_event(device, time);
426 type = get_key_type(e->code);
428 /* Ignore key release events from the kernel for keys that libinput
429 * never got a pressed event for. */
432 case EVDEV_KEY_TYPE_NONE:
434 case EVDEV_KEY_TYPE_KEY:
435 case EVDEV_KEY_TYPE_BUTTON:
436 if (!hw_is_key_down(device, e->code))
441 hw_set_key_down(device, e->code, e->value);
444 case EVDEV_KEY_TYPE_NONE:
446 case EVDEV_KEY_TYPE_KEY:
447 evdev_keyboard_notify_key(
451 e->value ? LIBINPUT_KEY_STATE_PRESSED :
452 LIBINPUT_KEY_STATE_RELEASED);
454 case EVDEV_KEY_TYPE_BUTTON:
455 if (device->scroll.has_middle_button_scroll &&
456 e->code == BTN_MIDDLE) {
457 evdev_middle_button_scroll_button(device, time,
461 evdev_pointer_notify_button(
464 evdev_to_left_handed(device, e->code),
465 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
466 LIBINPUT_BUTTON_STATE_RELEASED);
472 evdev_process_touch(struct evdev_device *device,
473 struct input_event *e,
478 evdev_flush_pending_event(device, time);
479 device->mt.slot = e->value;
481 case ABS_MT_TRACKING_ID:
482 if (device->pending_event != EVDEV_NONE &&
483 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
484 evdev_flush_pending_event(device, time);
486 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
488 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
490 case ABS_MT_POSITION_X:
491 device->mt.slots[device->mt.slot].x = e->value;
492 if (device->pending_event == EVDEV_NONE)
493 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
495 case ABS_MT_POSITION_Y:
496 device->mt.slots[device->mt.slot].y = e->value;
497 if (device->pending_event == EVDEV_NONE)
498 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
504 evdev_process_absolute_motion(struct evdev_device *device,
505 struct input_event *e)
509 device->abs.x = e->value;
510 if (device->pending_event == EVDEV_NONE)
511 device->pending_event = EVDEV_ABSOLUTE_MOTION;
514 device->abs.y = e->value;
515 if (device->pending_event == EVDEV_NONE)
516 device->pending_event = EVDEV_ABSOLUTE_MOTION;
522 evdev_process_relative(struct evdev_device *device,
523 struct input_event *e, uint64_t time)
525 struct libinput_device *base = &device->base;
529 if (device->pending_event != EVDEV_RELATIVE_MOTION)
530 evdev_flush_pending_event(device, time);
531 device->rel.dx += e->value;
532 device->pending_event = EVDEV_RELATIVE_MOTION;
535 if (device->pending_event != EVDEV_RELATIVE_MOTION)
536 evdev_flush_pending_event(device, time);
537 device->rel.dy += e->value;
538 device->pending_event = EVDEV_RELATIVE_MOTION;
541 evdev_flush_pending_event(device, time);
545 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
546 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
549 evdev_flush_pending_event(device, time);
558 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
559 e->value * DEFAULT_AXIS_STEP_DISTANCE);
569 evdev_process_absolute(struct evdev_device *device,
570 struct input_event *e,
574 evdev_process_touch(device, e, time);
576 evdev_process_absolute_motion(device, e);
581 evdev_need_touch_frame(struct evdev_device *device)
583 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
586 switch (device->pending_event) {
588 case EVDEV_RELATIVE_MOTION:
590 case EVDEV_ABSOLUTE_MT_DOWN:
591 case EVDEV_ABSOLUTE_MT_MOTION:
592 case EVDEV_ABSOLUTE_MT_UP:
593 case EVDEV_ABSOLUTE_TOUCH_DOWN:
594 case EVDEV_ABSOLUTE_TOUCH_UP:
595 case EVDEV_ABSOLUTE_MOTION:
603 evdev_tag_external_mouse(struct evdev_device *device,
604 struct udev_device *udev_device)
608 bustype = libevdev_get_id_bustype(device->evdev);
609 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
610 if (device->seat_caps & EVDEV_DEVICE_POINTER)
611 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
616 evdev_tag_trackpoint(struct evdev_device *device,
617 struct udev_device *udev_device)
619 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
620 device->tags |= EVDEV_TAG_TRACKPOINT;
624 fallback_process(struct evdev_dispatch *dispatch,
625 struct evdev_device *device,
626 struct input_event *event,
629 bool need_frame = false;
631 switch (event->type) {
633 evdev_process_relative(device, event, time);
636 evdev_process_absolute(device, event, time);
639 evdev_process_key(device, event, time);
642 need_frame = evdev_need_touch_frame(device);
643 evdev_flush_pending_event(device, time);
645 touch_notify_frame(&device->base, time);
651 fallback_destroy(struct evdev_dispatch *dispatch)
657 fallback_tag_device(struct evdev_device *device,
658 struct udev_device *udev_device)
660 evdev_tag_external_mouse(device, udev_device);
661 evdev_tag_trackpoint(device, udev_device);
665 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
667 struct evdev_device *device = (struct evdev_device*)libinput_device;
669 return device->abs.absinfo_x && device->abs.absinfo_y;
672 static enum libinput_config_status
673 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
674 const float matrix[6])
676 struct evdev_device *device = (struct evdev_device*)libinput_device;
678 evdev_device_calibrate(device, matrix);
680 return LIBINPUT_CONFIG_STATUS_SUCCESS;
684 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
687 struct evdev_device *device = (struct evdev_device*)libinput_device;
689 matrix_to_farray6(&device->abs.usermatrix, matrix);
691 return !matrix_is_identity(&device->abs.usermatrix);
695 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
698 struct evdev_device *device = (struct evdev_device*)libinput_device;
700 matrix_to_farray6(&device->abs.default_calibration, matrix);
702 return !matrix_is_identity(&device->abs.default_calibration);
705 struct evdev_dispatch_interface fallback_interface = {
708 NULL, /* device_added */
709 NULL, /* device_removed */
710 NULL, /* device_suspended */
711 NULL, /* device_resumed */
716 evdev_sendevents_get_modes(struct libinput_device *device)
718 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED |
719 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
722 static enum libinput_config_status
723 evdev_sendevents_set_mode(struct libinput_device *device,
724 enum libinput_config_send_events_mode mode)
726 struct evdev_device *evdev = (struct evdev_device*)device;
727 struct evdev_dispatch *dispatch = evdev->dispatch;
729 if (mode == dispatch->sendevents.current_mode)
730 return LIBINPUT_CONFIG_STATUS_SUCCESS;
733 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
734 evdev_device_resume(evdev);
736 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
737 evdev_device_suspend(evdev);
740 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
743 dispatch->sendevents.current_mode = mode;
745 return LIBINPUT_CONFIG_STATUS_SUCCESS;
748 static enum libinput_config_send_events_mode
749 evdev_sendevents_get_mode(struct libinput_device *device)
751 struct evdev_device *evdev = (struct evdev_device*)device;
752 struct evdev_dispatch *dispatch = evdev->dispatch;
754 return dispatch->sendevents.current_mode;
757 static enum libinput_config_send_events_mode
758 evdev_sendevents_get_default_mode(struct libinput_device *device)
760 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
764 evdev_left_handed_has(struct libinput_device *device)
766 /* This is only hooked up when we have left-handed configuration, so we
767 * can hardcode 1 here */
772 evdev_change_to_left_handed(struct evdev_device *device)
776 if (device->buttons.want_left_handed == device->buttons.left_handed)
779 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
780 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
781 hw_is_key_down(device, button))
785 device->buttons.left_handed = device->buttons.want_left_handed;
788 static enum libinput_config_status
789 evdev_left_handed_set(struct libinput_device *device, int left_handed)
791 struct evdev_device *evdev_device = (struct evdev_device *)device;
793 evdev_device->buttons.want_left_handed = left_handed ? true : false;
795 evdev_device->buttons.change_to_left_handed(evdev_device);
797 return LIBINPUT_CONFIG_STATUS_SUCCESS;
801 evdev_left_handed_get(struct libinput_device *device)
803 struct evdev_device *evdev_device = (struct evdev_device *)device;
805 /* return the wanted configuration, even if it hasn't taken
807 return evdev_device->buttons.want_left_handed;
811 evdev_left_handed_get_default(struct libinput_device *device)
817 evdev_init_left_handed(struct evdev_device *device,
818 void (*change_to_left_handed)(struct evdev_device *))
820 device->buttons.config_left_handed.has = evdev_left_handed_has;
821 device->buttons.config_left_handed.set = evdev_left_handed_set;
822 device->buttons.config_left_handed.get = evdev_left_handed_get;
823 device->buttons.config_left_handed.get_default = evdev_left_handed_get_default;
824 device->base.config.left_handed = &device->buttons.config_left_handed;
825 device->buttons.left_handed = false;
826 device->buttons.want_left_handed = false;
827 device->buttons.change_to_left_handed = change_to_left_handed;
832 static struct evdev_dispatch *
833 fallback_dispatch_create(struct libinput_device *device)
835 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
836 struct evdev_device *evdev_device = (struct evdev_device *)device;
838 if (dispatch == NULL)
841 dispatch->interface = &fallback_interface;
843 if (evdev_device->buttons.want_left_handed &&
844 evdev_init_left_handed(evdev_device,
845 evdev_change_to_left_handed) == -1) {
850 device->config.calibration = &dispatch->calibration;
852 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
853 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
854 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
855 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
857 device->config.sendevents = &dispatch->sendevents.config;
859 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
860 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
861 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
862 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
863 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
869 evdev_process_event(struct evdev_device *device, struct input_event *e)
871 struct evdev_dispatch *dispatch = device->dispatch;
872 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
874 dispatch->interface->process(dispatch, device, e, time);
878 evdev_device_dispatch_one(struct evdev_device *device,
879 struct input_event *ev)
881 if (!device->mtdev) {
882 evdev_process_event(device, ev);
884 mtdev_put_event(device->mtdev, ev);
885 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
886 while (!mtdev_empty(device->mtdev)) {
887 struct input_event e;
888 mtdev_get_event(device->mtdev, &e);
889 evdev_process_event(device, &e);
896 evdev_sync_device(struct evdev_device *device)
898 struct input_event ev;
902 rc = libevdev_next_event(device->evdev,
903 LIBEVDEV_READ_FLAG_SYNC, &ev);
906 evdev_device_dispatch_one(device, &ev);
907 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
909 return rc == -EAGAIN ? 0 : rc;
913 evdev_device_dispatch(void *data)
915 struct evdev_device *device = data;
916 struct libinput *libinput = device->base.seat->libinput;
917 struct input_event ev;
920 /* If the compositor is repainting, this function is called only once
921 * per frame and we have to process all the events available on the
922 * fd, otherwise there will be input lag. */
924 rc = libevdev_next_event(device->evdev,
925 LIBEVDEV_READ_FLAG_NORMAL, &ev);
926 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
927 if (device->syn_drops_received < 10) {
928 device->syn_drops_received++;
929 log_info(libinput, "SYN_DROPPED event from "
930 "\"%s\" - some input events have "
931 "been lost.\n", device->devname);
932 if (device->syn_drops_received == 10)
933 log_info(libinput, "No longer logging "
934 "SYN_DROPPED events for "
935 "\"%s\"\n", device->devname);
938 /* send one more sync event so we handle all
939 currently pending events before we sync up
940 to the current state */
941 ev.code = SYN_REPORT;
942 evdev_device_dispatch_one(device, &ev);
944 rc = evdev_sync_device(device);
946 rc = LIBEVDEV_READ_STATUS_SUCCESS;
947 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
948 evdev_device_dispatch_one(device, &ev);
950 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
952 if (rc != -EAGAIN && rc != -EINTR) {
953 libinput_remove_source(libinput, device->source);
954 device->source = NULL;
959 evdev_accel_config_available(struct libinput_device *device)
961 /* this function is only called if we set up ptraccel, so we can
962 reply with a resounding "Yes" */
966 static enum libinput_config_status
967 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
969 struct evdev_device *dev = (struct evdev_device *)device;
971 if (!filter_set_speed(dev->pointer.filter, speed))
972 return LIBINPUT_CONFIG_STATUS_INVALID;
974 return LIBINPUT_CONFIG_STATUS_SUCCESS;
978 evdev_accel_config_get_speed(struct libinput_device *device)
980 struct evdev_device *dev = (struct evdev_device *)device;
982 return filter_get_speed(dev->pointer.filter);
986 evdev_accel_config_get_default_speed(struct libinput_device *device)
992 evdev_device_init_pointer_acceleration(struct evdev_device *device)
994 device->pointer.filter =
995 create_pointer_accelerator_filter(
996 pointer_accel_profile_linear);
997 if (!device->pointer.filter)
1000 device->pointer.config.available = evdev_accel_config_available;
1001 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1002 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1003 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1004 device->base.config.accel = &device->pointer.config;
1011 evdev_need_mtdev(struct evdev_device *device)
1013 struct libevdev *evdev = device->evdev;
1015 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1016 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1017 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1021 evdev_tag_device(struct evdev_device *device)
1024 struct udev_device *udev_device = NULL;
1030 udev_device = udev_device_new_from_syspath(udev, device->syspath);
1032 if (device->dispatch->interface->tag_device)
1033 device->dispatch->interface->tag_device(device, udev_device);
1034 udev_device_unref(udev_device);
1040 evdev_configure_device(struct evdev_device *device)
1042 struct libinput *libinput = device->base.seat->libinput;
1043 struct libevdev *evdev = device->evdev;
1044 const struct input_absinfo *absinfo;
1045 struct input_absinfo fixed;
1046 int has_abs, has_rel, has_mt;
1047 int has_button, has_keyboard, has_touch;
1048 struct mt_slot *slots;
1061 if (libevdev_has_event_type(evdev, EV_ABS)) {
1063 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1064 if (absinfo->resolution == 0) {
1066 fixed.resolution = 1;
1067 libevdev_set_abs_info(evdev, ABS_X, &fixed);
1068 device->abs.fake_resolution = 1;
1070 device->abs.absinfo_x = absinfo;
1073 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1074 if (absinfo->resolution == 0) {
1076 fixed.resolution = 1;
1077 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
1078 device->abs.fake_resolution = 1;
1080 device->abs.absinfo_y = absinfo;
1083 /* We only handle the slotted Protocol B in weston.
1084 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1085 require mtdev for conversion. */
1086 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1087 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1088 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1089 if (absinfo->resolution == 0) {
1091 fixed.resolution = 1;
1092 libevdev_set_abs_info(evdev,
1095 device->abs.fake_resolution = 1;
1097 device->abs.absinfo_x = absinfo;
1098 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1099 if (absinfo->resolution == 0) {
1101 fixed.resolution = 1;
1102 libevdev_set_abs_info(evdev,
1105 device->abs.fake_resolution = 1;
1107 device->abs.absinfo_y = absinfo;
1112 if (evdev_need_mtdev(device)) {
1113 device->mtdev = mtdev_new_open(device->fd);
1117 num_slots = device->mtdev->caps.slot.maximum;
1118 if (device->mtdev->caps.slot.minimum < 0 ||
1121 active_slot = device->mtdev->caps.slot.value;
1123 num_slots = libevdev_get_num_slots(device->evdev);
1124 active_slot = libevdev_get_current_slot(evdev);
1127 slots = calloc(num_slots, sizeof(struct mt_slot));
1131 for (slot = 0; slot < num_slots; ++slot) {
1132 slots[slot].seat_slot = -1;
1136 device->mt.slots = slots;
1137 device->mt.slots_len = num_slots;
1138 device->mt.slot = active_slot;
1142 if (libevdev_has_property(evdev, INPUT_PROP_POINTING_STICK)) {
1143 libinput_timer_init(&device->scroll.timer,
1144 device->base.seat->libinput,
1145 evdev_middle_button_scroll_timeout,
1147 device->scroll.has_middle_button_scroll = true;
1150 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1151 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1154 if (libevdev_has_event_type(evdev, EV_KEY)) {
1155 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1156 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1157 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1158 (has_abs || has_mt)) {
1159 device->dispatch = evdev_mt_touchpad_create(device);
1161 "input device '%s', %s is a touchpad\n",
1162 device->devname, device->devnode);
1163 return device->dispatch == NULL ? -1 : 0;
1166 for (i = 0; i < KEY_MAX; i++) {
1167 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1168 switch (get_key_type(i)) {
1169 case EVDEV_KEY_TYPE_NONE:
1171 case EVDEV_KEY_TYPE_KEY:
1174 case EVDEV_KEY_TYPE_BUTTON:
1181 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1184 if (libevdev_has_event_type(evdev, EV_LED))
1187 if ((has_abs || has_rel) && has_button) {
1188 if (evdev_device_init_pointer_acceleration(device) == -1)
1191 device->seat_caps |= EVDEV_DEVICE_POINTER;
1194 "input device '%s', %s is a pointer caps =%s%s%s\n",
1195 device->devname, device->devnode,
1196 has_abs ? " absolute-motion" : "",
1197 has_rel ? " relative-motion": "",
1198 has_button ? " button" : "");
1200 /* want left-handed config option */
1201 device->buttons.want_left_handed = true;
1204 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1206 "input device '%s', %s is a keyboard\n",
1207 device->devname, device->devnode);
1209 if (has_touch && !has_button) {
1210 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1212 "input device '%s', %s is a touch device\n",
1213 device->devname, device->devnode);
1220 evdev_notify_added_device(struct evdev_device *device)
1222 struct libinput_device *dev;
1224 list_for_each(dev, &device->base.seat->devices_list, link) {
1225 struct evdev_device *d = (struct evdev_device*)dev;
1226 if (dev == &device->base)
1229 /* Notify existing device d about addition of device device */
1230 if (d->dispatch->interface->device_added)
1231 d->dispatch->interface->device_added(d, device);
1233 /* Notify new device device about existing device d */
1234 if (device->dispatch->interface->device_added)
1235 device->dispatch->interface->device_added(device, d);
1237 /* Notify new device device if existing device d is suspended */
1238 if (d->suspended && device->dispatch->interface->device_suspended)
1239 device->dispatch->interface->device_suspended(device, d);
1242 notify_added_device(&device->base);
1245 struct evdev_device *
1246 evdev_device_create(struct libinput_seat *seat,
1247 const char *devnode,
1248 const char *sysname,
1249 const char *syspath)
1251 struct libinput *libinput = seat->libinput;
1252 struct evdev_device *device;
1255 int unhandled_device = 0;
1257 /* Use non-blocking mode so that we can loop on read on
1258 * evdev_device_data() until all events on the fd are
1259 * read. mtdev_get() also expects this. */
1260 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1263 "opening input device '%s' failed (%s).\n",
1264 devnode, strerror(-fd));
1268 device = zalloc(sizeof *device);
1272 libinput_device_init(&device->base, seat);
1273 libinput_seat_ref(seat);
1275 rc = libevdev_new_from_fd(fd, &device->evdev);
1279 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1281 device->seat_caps = 0;
1283 device->mtdev = NULL;
1284 device->devnode = strdup(devnode);
1285 device->sysname = strdup(sysname);
1286 device->syspath = strdup(syspath);
1289 device->abs.seat_slot = -1;
1290 device->dispatch = NULL;
1292 device->pending_event = EVDEV_NONE;
1293 device->devname = libevdev_get_name(device->evdev);
1294 device->scroll.threshold = 5.0; /* Default may be overridden */
1295 device->scroll.direction = 0;
1297 matrix_init_identity(&device->abs.calibration);
1298 matrix_init_identity(&device->abs.usermatrix);
1299 matrix_init_identity(&device->abs.default_calibration);
1301 if (evdev_configure_device(device) == -1)
1304 if (device->seat_caps == 0) {
1305 unhandled_device = 1;
1309 /* If the dispatch was not set up use the fallback. */
1310 if (device->dispatch == NULL)
1311 device->dispatch = fallback_dispatch_create(&device->base);
1312 if (device->dispatch == NULL)
1316 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1317 if (!device->source)
1320 list_insert(seat->devices_list.prev, &device->base.link);
1322 evdev_tag_device(device);
1323 evdev_notify_added_device(device);
1329 close_restricted(libinput, fd);
1330 evdev_device_destroy(device);
1332 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1336 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1338 memset(keys, 0, size);
1343 evdev_device_get_output(struct evdev_device *device)
1345 return device->output_name;
1349 evdev_device_get_sysname(struct evdev_device *device)
1351 return device->sysname;
1355 evdev_device_get_name(struct evdev_device *device)
1357 return device->devname;
1361 evdev_device_get_id_product(struct evdev_device *device)
1363 return libevdev_get_id_product(device->evdev);
1367 evdev_device_get_id_vendor(struct evdev_device *device)
1369 return libevdev_get_id_vendor(device->evdev);
1373 evdev_device_set_default_calibration(struct evdev_device *device,
1374 const float calibration[6])
1376 matrix_from_farray6(&device->abs.default_calibration, calibration);
1377 evdev_device_calibrate(device, calibration);
1381 evdev_device_calibrate(struct evdev_device *device,
1382 const float calibration[6])
1384 struct matrix scale,
1389 matrix_from_farray6(&transform, calibration);
1390 device->abs.apply_calibration = !matrix_is_identity(&transform);
1392 if (!device->abs.apply_calibration) {
1393 matrix_init_identity(&device->abs.calibration);
1397 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1398 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1400 /* The transformation matrix is in the form:
1404 * Where a, e are the scale components, a, b, d, e are the rotation
1405 * component (combined with scale) and c and f are the translation
1406 * component. The translation component in the input matrix must be
1407 * normalized to multiples of the device width and height,
1408 * respectively. e.g. c == 1 shifts one device-width to the right.
1410 * We pre-calculate a single matrix to apply to event coordinates:
1411 * M = Un-Normalize * Calibration * Normalize
1413 * Normalize: scales the device coordinates to [0,1]
1414 * Calibration: user-supplied matrix
1415 * Un-Normalize: scales back up to device coordinates
1416 * Matrix maths requires the normalize/un-normalize in reverse
1420 /* back up the user matrix so we can return it on request */
1421 matrix_from_farray6(&device->abs.usermatrix, calibration);
1424 matrix_init_translate(&translate,
1425 device->abs.absinfo_x->minimum,
1426 device->abs.absinfo_y->minimum);
1427 matrix_init_scale(&scale, sx, sy);
1428 matrix_mult(&scale, &translate, &scale);
1431 matrix_mult(&transform, &scale, &transform);
1434 matrix_init_translate(&translate,
1435 -device->abs.absinfo_x->minimum/sx,
1436 -device->abs.absinfo_y->minimum/sy);
1437 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1438 matrix_mult(&scale, &translate, &scale);
1440 /* store final matrix in device */
1441 matrix_mult(&device->abs.calibration, &transform, &scale);
1445 evdev_device_has_capability(struct evdev_device *device,
1446 enum libinput_device_capability capability)
1448 switch (capability) {
1449 case LIBINPUT_DEVICE_CAP_POINTER:
1450 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1451 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1452 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1453 case LIBINPUT_DEVICE_CAP_TOUCH:
1454 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1461 evdev_device_get_size(struct evdev_device *device,
1465 const struct input_absinfo *x, *y;
1467 x = libevdev_get_abs_info(device->evdev, ABS_X);
1468 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1470 if (!x || !y || device->abs.fake_resolution ||
1471 !x->resolution || !y->resolution)
1474 *width = evdev_convert_to_mm(x, x->maximum);
1475 *height = evdev_convert_to_mm(y, y->maximum);
1481 evdev_post_scroll(struct evdev_device *device,
1486 if (dy <= -device->scroll.threshold || dy >= device->scroll.threshold)
1487 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1489 if (dx <= -device->scroll.threshold || dx >= device->scroll.threshold)
1490 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1493 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
1494 pointer_notify_axis(&device->base,
1496 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1501 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
1502 pointer_notify_axis(&device->base,
1504 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1510 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1512 /* terminate scrolling with a zero scroll event */
1513 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1514 pointer_notify_axis(&device->base,
1516 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1518 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1519 pointer_notify_axis(&device->base,
1521 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1524 device->scroll.direction = 0;
1528 release_pressed_keys(struct evdev_device *device)
1530 struct libinput *libinput = device->base.seat->libinput;
1534 if ((time = libinput_now(libinput)) == 0)
1537 for (code = 0; code < KEY_CNT; code++) {
1538 int count = get_key_down_count(device, code);
1541 log_bug_libinput(libinput,
1542 "Key %d is down %d times.\n",
1547 while (get_key_down_count(device, code) > 0) {
1548 switch (get_key_type(code)) {
1549 case EVDEV_KEY_TYPE_NONE:
1551 case EVDEV_KEY_TYPE_KEY:
1552 evdev_keyboard_notify_key(
1556 LIBINPUT_KEY_STATE_RELEASED);
1558 case EVDEV_KEY_TYPE_BUTTON:
1559 evdev_pointer_notify_button(
1562 evdev_to_left_handed(device, code),
1563 LIBINPUT_BUTTON_STATE_RELEASED);
1571 evdev_notify_suspended_device(struct evdev_device *device)
1573 struct libinput_device *it;
1575 if (device->suspended)
1578 list_for_each(it, &device->base.seat->devices_list, link) {
1579 struct evdev_device *d = (struct evdev_device*)it;
1580 if (it == &device->base)
1583 if (d->dispatch->interface->device_suspended)
1584 d->dispatch->interface->device_suspended(d, device);
1587 device->suspended = 1;
1591 evdev_notify_resumed_device(struct evdev_device *device)
1593 struct libinput_device *it;
1595 if (!device->suspended)
1598 list_for_each(it, &device->base.seat->devices_list, link) {
1599 struct evdev_device *d = (struct evdev_device*)it;
1600 if (it == &device->base)
1603 if (d->dispatch->interface->device_resumed)
1604 d->dispatch->interface->device_resumed(d, device);
1607 device->suspended = 0;
1611 evdev_device_suspend(struct evdev_device *device)
1613 evdev_notify_suspended_device(device);
1615 if (device->source) {
1616 libinput_remove_source(device->base.seat->libinput,
1618 device->source = NULL;
1621 release_pressed_keys(device);
1623 if (device->mtdev) {
1624 mtdev_close_delete(device->mtdev);
1625 device->mtdev = NULL;
1628 if (device->fd != -1) {
1629 close_restricted(device->base.seat->libinput, device->fd);
1637 evdev_device_compare_syspath(struct evdev_device *device, int fd)
1639 struct udev *udev = NULL;
1640 struct udev_device *udev_device = NULL;
1641 const char *syspath;
1649 if (fstat(fd, &st) < 0)
1652 udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1656 syspath = udev_device_get_syspath(udev_device);
1657 rc = strcmp(syspath, device->syspath);
1660 udev_device_unref(udev_device);
1667 evdev_device_resume(struct evdev_device *device)
1669 struct libinput *libinput = device->base.seat->libinput;
1672 if (device->fd != -1)
1675 if (device->syspath == NULL)
1678 fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1683 if (evdev_device_compare_syspath(device, fd)) {
1684 close_restricted(libinput, fd);
1690 if (evdev_need_mtdev(device)) {
1691 device->mtdev = mtdev_new_open(device->fd);
1697 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1698 if (!device->source) {
1699 mtdev_close_delete(device->mtdev);
1703 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1705 evdev_notify_resumed_device(device);
1711 evdev_device_remove(struct evdev_device *device)
1713 struct libinput_device *dev;
1715 list_for_each(dev, &device->base.seat->devices_list, link) {
1716 struct evdev_device *d = (struct evdev_device*)dev;;
1717 if (dev == &device->base)
1720 if (d->dispatch->interface->device_removed)
1721 d->dispatch->interface->device_removed(d, device);
1724 evdev_device_suspend(device);
1726 /* A device may be removed while suspended. Free the syspath to
1727 * skip re-opening a different device with the same node */
1728 free(device->syspath);
1729 device->syspath = NULL;
1731 list_remove(&device->base.link);
1733 notify_removed_device(&device->base);
1734 libinput_device_unref(&device->base);
1738 evdev_device_destroy(struct evdev_device *device)
1740 struct evdev_dispatch *dispatch;
1742 dispatch = device->dispatch;
1744 dispatch->interface->destroy(dispatch);
1746 filter_destroy(device->pointer.filter);
1747 libinput_seat_unref(device->base.seat);
1748 libevdev_free(device->evdev);
1749 free(device->mt.slots);
1750 free(device->devnode);
1751 free(device->sysname);
1752 free(device->syspath);