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);
131 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
133 static const struct {
134 enum libinput_led weston;
137 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
138 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
139 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
141 struct input_event ev[ARRAY_LENGTH(map) + 1];
144 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
147 memset(ev, 0, sizeof(ev));
148 for (i = 0; i < ARRAY_LENGTH(map); i++) {
150 ev[i].code = map[i].evdev;
151 ev[i].value = !!(leds & map[i].weston);
154 ev[i].code = SYN_REPORT;
156 i = write(device->fd, ev, sizeof ev);
157 (void)i; /* no, we really don't care about the return value */
161 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
163 if (!device->abs.apply_calibration)
166 matrix_mult_vec(&device->abs.calibration, x, y);
170 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
172 return (val - absinfo->minimum) * to_range /
173 (absinfo->maximum - absinfo->minimum + 1);
177 evdev_device_transform_x(struct evdev_device *device,
181 return scale_axis(device->abs.absinfo_x, x, width);
185 evdev_device_transform_y(struct evdev_device *device,
189 return scale_axis(device->abs.absinfo_y, y, height);
193 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
195 struct libinput *libinput = device->base.seat->libinput;
196 struct motion_params motion;
201 struct libinput_device *base = &device->base;
202 struct libinput_seat *seat = base->seat;
204 slot = device->mt.slot;
206 switch (device->pending_event) {
209 case EVDEV_RELATIVE_MOTION:
210 motion.dx = device->rel.dx / ((double)device->dpi / DEFAULT_MOUSE_DPI);
211 motion.dy = device->rel.dy / ((double)device->dpi / DEFAULT_MOUSE_DPI);
215 /* Use unaccelerated deltas for pointing stick scroll */
216 if (device->scroll.has_middle_button_scroll &&
217 hw_is_key_down(device, BTN_MIDDLE)) {
218 if (device->scroll.middle_button_scroll_active)
219 evdev_post_scroll(device, time,
220 motion.dx, motion.dy);
224 /* Apply pointer acceleration. */
225 filter_dispatch(device->pointer.filter, &motion, device, time);
227 if (motion.dx == 0.0 && motion.dy == 0.0)
230 pointer_notify_motion(base, time, motion.dx, motion.dy);
232 case EVDEV_ABSOLUTE_MT_DOWN:
233 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
236 if (device->mt.slots[slot].seat_slot != -1) {
237 log_bug_kernel(libinput,
238 "%s: Driver sent multiple touch down for the "
239 "same slot", device->devnode);
243 seat_slot = ffs(~seat->slot_map) - 1;
244 device->mt.slots[slot].seat_slot = seat_slot;
249 seat->slot_map |= 1 << seat_slot;
250 x = device->mt.slots[slot].x;
251 y = device->mt.slots[slot].y;
252 transform_absolute(device, &x, &y);
254 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
256 case EVDEV_ABSOLUTE_MT_MOTION:
257 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
260 seat_slot = device->mt.slots[slot].seat_slot;
261 x = device->mt.slots[slot].x;
262 y = device->mt.slots[slot].y;
267 transform_absolute(device, &x, &y);
268 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
270 case EVDEV_ABSOLUTE_MT_UP:
271 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
274 seat_slot = device->mt.slots[slot].seat_slot;
275 device->mt.slots[slot].seat_slot = -1;
280 seat->slot_map &= ~(1 << seat_slot);
282 touch_notify_touch_up(base, time, slot, seat_slot);
284 case EVDEV_ABSOLUTE_TOUCH_DOWN:
285 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
288 if (device->abs.seat_slot != -1) {
289 log_bug_kernel(libinput,
290 "%s: Driver sent multiple touch down for the "
291 "same slot", device->devnode);
295 seat_slot = ffs(~seat->slot_map) - 1;
296 device->abs.seat_slot = seat_slot;
301 seat->slot_map |= 1 << seat_slot;
305 transform_absolute(device, &cx, &cy);
307 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
309 case EVDEV_ABSOLUTE_MOTION:
312 transform_absolute(device, &cx, &cy);
316 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
317 seat_slot = device->abs.seat_slot;
322 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
323 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
324 pointer_notify_motion_absolute(base, time, x, y);
327 case EVDEV_ABSOLUTE_TOUCH_UP:
328 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
331 seat_slot = device->abs.seat_slot;
332 device->abs.seat_slot = -1;
337 seat->slot_map &= ~(1 << seat_slot);
339 touch_notify_touch_up(base, time, -1, seat_slot);
342 assert(0 && "Unknown pending event type");
346 device->pending_event = EVDEV_NONE;
349 static enum evdev_key_type
350 get_key_type(uint16_t code)
352 if (code == BTN_TOUCH)
353 return EVDEV_KEY_TYPE_NONE;
355 if (code >= KEY_ESC && code <= KEY_MICMUTE)
356 return EVDEV_KEY_TYPE_KEY;
357 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
358 return EVDEV_KEY_TYPE_BUTTON;
359 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
360 return EVDEV_KEY_TYPE_KEY;
361 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
362 return EVDEV_KEY_TYPE_BUTTON;
363 return EVDEV_KEY_TYPE_NONE;
367 evdev_middle_button_scroll_timeout(uint64_t time, void *data)
369 struct evdev_device *device = data;
371 device->scroll.middle_button_scroll_active = true;
375 evdev_middle_button_scroll_button(struct evdev_device *device,
376 uint64_t time, int is_press)
379 libinput_timer_set(&device->scroll.timer,
380 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
382 libinput_timer_cancel(&device->scroll.timer);
383 if (device->scroll.middle_button_scroll_active) {
384 evdev_stop_scroll(device, time);
385 device->scroll.middle_button_scroll_active = false;
387 /* If the button is released quickly enough emit the
388 * button press/release events. */
389 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
390 LIBINPUT_BUTTON_STATE_PRESSED);
391 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
392 LIBINPUT_BUTTON_STATE_RELEASED);
398 evdev_process_touch_button(struct evdev_device *device,
399 uint64_t time, int value)
401 if (device->pending_event != EVDEV_NONE &&
402 device->pending_event != EVDEV_ABSOLUTE_MOTION)
403 evdev_flush_pending_event(device, time);
405 device->pending_event = (value ?
406 EVDEV_ABSOLUTE_TOUCH_DOWN :
407 EVDEV_ABSOLUTE_TOUCH_UP);
411 evdev_process_key(struct evdev_device *device,
412 struct input_event *e, uint64_t time)
414 enum evdev_key_type type;
416 /* ignore kernel key repeat */
420 if (e->code == BTN_TOUCH) {
422 evdev_process_touch_button(device, time, e->value);
426 evdev_flush_pending_event(device, time);
428 type = get_key_type(e->code);
430 /* Ignore key release events from the kernel for keys that libinput
431 * never got a pressed event for. */
434 case EVDEV_KEY_TYPE_NONE:
436 case EVDEV_KEY_TYPE_KEY:
437 case EVDEV_KEY_TYPE_BUTTON:
438 if (!hw_is_key_down(device, e->code))
443 hw_set_key_down(device, e->code, e->value);
446 case EVDEV_KEY_TYPE_NONE:
448 case EVDEV_KEY_TYPE_KEY:
449 evdev_keyboard_notify_key(
453 e->value ? LIBINPUT_KEY_STATE_PRESSED :
454 LIBINPUT_KEY_STATE_RELEASED);
456 case EVDEV_KEY_TYPE_BUTTON:
457 if (device->scroll.has_middle_button_scroll &&
458 e->code == BTN_MIDDLE) {
459 evdev_middle_button_scroll_button(device, time,
463 evdev_pointer_notify_button(
466 evdev_to_left_handed(device, e->code),
467 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
468 LIBINPUT_BUTTON_STATE_RELEASED);
474 evdev_process_touch(struct evdev_device *device,
475 struct input_event *e,
480 evdev_flush_pending_event(device, time);
481 device->mt.slot = e->value;
483 case ABS_MT_TRACKING_ID:
484 if (device->pending_event != EVDEV_NONE &&
485 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
486 evdev_flush_pending_event(device, time);
488 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
490 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
492 case ABS_MT_POSITION_X:
493 device->mt.slots[device->mt.slot].x = e->value;
494 if (device->pending_event == EVDEV_NONE)
495 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
497 case ABS_MT_POSITION_Y:
498 device->mt.slots[device->mt.slot].y = e->value;
499 if (device->pending_event == EVDEV_NONE)
500 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
506 evdev_process_absolute_motion(struct evdev_device *device,
507 struct input_event *e)
511 device->abs.x = e->value;
512 if (device->pending_event == EVDEV_NONE)
513 device->pending_event = EVDEV_ABSOLUTE_MOTION;
516 device->abs.y = e->value;
517 if (device->pending_event == EVDEV_NONE)
518 device->pending_event = EVDEV_ABSOLUTE_MOTION;
524 evdev_process_relative(struct evdev_device *device,
525 struct input_event *e, uint64_t time)
527 struct libinput_device *base = &device->base;
531 if (device->pending_event != EVDEV_RELATIVE_MOTION)
532 evdev_flush_pending_event(device, time);
533 device->rel.dx += e->value;
534 device->pending_event = EVDEV_RELATIVE_MOTION;
537 if (device->pending_event != EVDEV_RELATIVE_MOTION)
538 evdev_flush_pending_event(device, time);
539 device->rel.dy += e->value;
540 device->pending_event = EVDEV_RELATIVE_MOTION;
543 evdev_flush_pending_event(device, time);
547 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
548 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
551 evdev_flush_pending_event(device, time);
560 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
561 e->value * DEFAULT_AXIS_STEP_DISTANCE);
571 evdev_process_absolute(struct evdev_device *device,
572 struct input_event *e,
576 evdev_process_touch(device, e, time);
578 evdev_process_absolute_motion(device, e);
583 evdev_need_touch_frame(struct evdev_device *device)
585 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
588 switch (device->pending_event) {
590 case EVDEV_RELATIVE_MOTION:
592 case EVDEV_ABSOLUTE_MT_DOWN:
593 case EVDEV_ABSOLUTE_MT_MOTION:
594 case EVDEV_ABSOLUTE_MT_UP:
595 case EVDEV_ABSOLUTE_TOUCH_DOWN:
596 case EVDEV_ABSOLUTE_TOUCH_UP:
597 case EVDEV_ABSOLUTE_MOTION:
605 evdev_tag_external_mouse(struct evdev_device *device,
606 struct udev_device *udev_device)
610 bustype = libevdev_get_id_bustype(device->evdev);
611 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
612 if (device->seat_caps & EVDEV_DEVICE_POINTER)
613 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
618 evdev_tag_trackpoint(struct evdev_device *device,
619 struct udev_device *udev_device)
621 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
622 device->tags |= EVDEV_TAG_TRACKPOINT;
626 fallback_process(struct evdev_dispatch *dispatch,
627 struct evdev_device *device,
628 struct input_event *event,
631 bool need_frame = false;
633 switch (event->type) {
635 evdev_process_relative(device, event, time);
638 evdev_process_absolute(device, event, time);
641 evdev_process_key(device, event, time);
644 need_frame = evdev_need_touch_frame(device);
645 evdev_flush_pending_event(device, time);
647 touch_notify_frame(&device->base, time);
653 fallback_destroy(struct evdev_dispatch *dispatch)
659 fallback_tag_device(struct evdev_device *device,
660 struct udev_device *udev_device)
662 evdev_tag_external_mouse(device, udev_device);
663 evdev_tag_trackpoint(device, udev_device);
667 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
669 struct evdev_device *device = (struct evdev_device*)libinput_device;
671 return device->abs.absinfo_x && device->abs.absinfo_y;
674 static enum libinput_config_status
675 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
676 const float matrix[6])
678 struct evdev_device *device = (struct evdev_device*)libinput_device;
680 evdev_device_calibrate(device, matrix);
682 return LIBINPUT_CONFIG_STATUS_SUCCESS;
686 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
689 struct evdev_device *device = (struct evdev_device*)libinput_device;
691 matrix_to_farray6(&device->abs.usermatrix, matrix);
693 return !matrix_is_identity(&device->abs.usermatrix);
697 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
700 struct evdev_device *device = (struct evdev_device*)libinput_device;
702 matrix_to_farray6(&device->abs.default_calibration, matrix);
704 return !matrix_is_identity(&device->abs.default_calibration);
707 struct evdev_dispatch_interface fallback_interface = {
710 NULL, /* device_added */
711 NULL, /* device_removed */
712 NULL, /* device_suspended */
713 NULL, /* device_resumed */
718 evdev_sendevents_get_modes(struct libinput_device *device)
720 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED |
721 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
724 static enum libinput_config_status
725 evdev_sendevents_set_mode(struct libinput_device *device,
726 enum libinput_config_send_events_mode mode)
728 struct evdev_device *evdev = (struct evdev_device*)device;
729 struct evdev_dispatch *dispatch = evdev->dispatch;
731 if (mode == dispatch->sendevents.current_mode)
732 return LIBINPUT_CONFIG_STATUS_SUCCESS;
735 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
736 evdev_device_resume(evdev);
738 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
739 evdev_device_suspend(evdev);
742 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
745 dispatch->sendevents.current_mode = mode;
747 return LIBINPUT_CONFIG_STATUS_SUCCESS;
750 static enum libinput_config_send_events_mode
751 evdev_sendevents_get_mode(struct libinput_device *device)
753 struct evdev_device *evdev = (struct evdev_device*)device;
754 struct evdev_dispatch *dispatch = evdev->dispatch;
756 return dispatch->sendevents.current_mode;
759 static enum libinput_config_send_events_mode
760 evdev_sendevents_get_default_mode(struct libinput_device *device)
762 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
766 evdev_left_handed_has(struct libinput_device *device)
768 /* This is only hooked up when we have left-handed configuration, so we
769 * can hardcode 1 here */
774 evdev_change_to_left_handed(struct evdev_device *device)
778 if (device->buttons.want_left_handed == device->buttons.left_handed)
781 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
782 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
783 hw_is_key_down(device, button))
787 device->buttons.left_handed = device->buttons.want_left_handed;
790 static enum libinput_config_status
791 evdev_left_handed_set(struct libinput_device *device, int left_handed)
793 struct evdev_device *evdev_device = (struct evdev_device *)device;
795 evdev_device->buttons.want_left_handed = left_handed ? true : false;
797 evdev_device->buttons.change_to_left_handed(evdev_device);
799 return LIBINPUT_CONFIG_STATUS_SUCCESS;
803 evdev_left_handed_get(struct libinput_device *device)
805 struct evdev_device *evdev_device = (struct evdev_device *)device;
807 /* return the wanted configuration, even if it hasn't taken
809 return evdev_device->buttons.want_left_handed;
813 evdev_left_handed_get_default(struct libinput_device *device)
819 evdev_init_left_handed(struct evdev_device *device,
820 void (*change_to_left_handed)(struct evdev_device *))
822 device->buttons.config_left_handed.has = evdev_left_handed_has;
823 device->buttons.config_left_handed.set = evdev_left_handed_set;
824 device->buttons.config_left_handed.get = evdev_left_handed_get;
825 device->buttons.config_left_handed.get_default = evdev_left_handed_get_default;
826 device->base.config.left_handed = &device->buttons.config_left_handed;
827 device->buttons.left_handed = false;
828 device->buttons.want_left_handed = false;
829 device->buttons.change_to_left_handed = change_to_left_handed;
834 static struct evdev_dispatch *
835 fallback_dispatch_create(struct libinput_device *device)
837 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
838 struct evdev_device *evdev_device = (struct evdev_device *)device;
840 if (dispatch == NULL)
843 dispatch->interface = &fallback_interface;
845 if (evdev_device->buttons.want_left_handed &&
846 evdev_init_left_handed(evdev_device,
847 evdev_change_to_left_handed) == -1) {
852 device->config.calibration = &dispatch->calibration;
854 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
855 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
856 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
857 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
859 device->config.sendevents = &dispatch->sendevents.config;
861 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
862 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
863 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
864 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
865 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
871 evdev_process_event(struct evdev_device *device, struct input_event *e)
873 struct evdev_dispatch *dispatch = device->dispatch;
874 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
876 dispatch->interface->process(dispatch, device, e, time);
880 evdev_device_dispatch_one(struct evdev_device *device,
881 struct input_event *ev)
883 if (!device->mtdev) {
884 evdev_process_event(device, ev);
886 mtdev_put_event(device->mtdev, ev);
887 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
888 while (!mtdev_empty(device->mtdev)) {
889 struct input_event e;
890 mtdev_get_event(device->mtdev, &e);
891 evdev_process_event(device, &e);
898 evdev_sync_device(struct evdev_device *device)
900 struct input_event ev;
904 rc = libevdev_next_event(device->evdev,
905 LIBEVDEV_READ_FLAG_SYNC, &ev);
908 evdev_device_dispatch_one(device, &ev);
909 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
911 return rc == -EAGAIN ? 0 : rc;
915 evdev_device_dispatch(void *data)
917 struct evdev_device *device = data;
918 struct libinput *libinput = device->base.seat->libinput;
919 struct input_event ev;
922 /* If the compositor is repainting, this function is called only once
923 * per frame and we have to process all the events available on the
924 * fd, otherwise there will be input lag. */
926 rc = libevdev_next_event(device->evdev,
927 LIBEVDEV_READ_FLAG_NORMAL, &ev);
928 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
929 if (device->syn_drops_received < 10) {
930 device->syn_drops_received++;
931 log_info(libinput, "SYN_DROPPED event from "
932 "\"%s\" - some input events have "
933 "been lost.\n", device->devname);
934 if (device->syn_drops_received == 10)
935 log_info(libinput, "No longer logging "
936 "SYN_DROPPED events for "
937 "\"%s\"\n", device->devname);
940 /* send one more sync event so we handle all
941 currently pending events before we sync up
942 to the current state */
943 ev.code = SYN_REPORT;
944 evdev_device_dispatch_one(device, &ev);
946 rc = evdev_sync_device(device);
948 rc = LIBEVDEV_READ_STATUS_SUCCESS;
949 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
950 evdev_device_dispatch_one(device, &ev);
952 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
954 if (rc != -EAGAIN && rc != -EINTR) {
955 libinput_remove_source(libinput, device->source);
956 device->source = NULL;
961 evdev_accel_config_available(struct libinput_device *device)
963 /* this function is only called if we set up ptraccel, so we can
964 reply with a resounding "Yes" */
968 static enum libinput_config_status
969 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
971 struct evdev_device *dev = (struct evdev_device *)device;
973 if (!filter_set_speed(dev->pointer.filter, speed))
974 return LIBINPUT_CONFIG_STATUS_INVALID;
976 return LIBINPUT_CONFIG_STATUS_SUCCESS;
980 evdev_accel_config_get_speed(struct libinput_device *device)
982 struct evdev_device *dev = (struct evdev_device *)device;
984 return filter_get_speed(dev->pointer.filter);
988 evdev_accel_config_get_default_speed(struct libinput_device *device)
994 evdev_device_init_pointer_acceleration(struct evdev_device *device)
996 device->pointer.filter =
997 create_pointer_accelerator_filter(
998 pointer_accel_profile_linear);
999 if (!device->pointer.filter)
1002 device->pointer.config.available = evdev_accel_config_available;
1003 device->pointer.config.set_speed = evdev_accel_config_set_speed;
1004 device->pointer.config.get_speed = evdev_accel_config_get_speed;
1005 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1006 device->base.config.accel = &device->pointer.config;
1013 evdev_need_mtdev(struct evdev_device *device)
1015 struct libevdev *evdev = device->evdev;
1017 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1018 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1019 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1023 evdev_tag_device(struct evdev_device *device)
1026 struct udev_device *udev_device = NULL;
1032 udev_device = udev_device_new_from_syspath(udev, device->syspath);
1034 if (device->dispatch->interface->tag_device)
1035 device->dispatch->interface->tag_device(device, udev_device);
1036 udev_device_unref(udev_device);
1042 evdev_configure_device(struct evdev_device *device)
1044 struct libinput *libinput = device->base.seat->libinput;
1045 struct libevdev *evdev = device->evdev;
1046 const struct input_absinfo *absinfo;
1047 struct input_absinfo fixed;
1048 int has_abs, has_rel, has_mt;
1049 int has_button, has_keyboard, has_touch;
1050 struct mt_slot *slots;
1063 if (libevdev_has_event_type(evdev, EV_ABS)) {
1065 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1066 if (absinfo->resolution == 0) {
1068 fixed.resolution = 1;
1069 libevdev_set_abs_info(evdev, ABS_X, &fixed);
1070 device->abs.fake_resolution = 1;
1072 device->abs.absinfo_x = absinfo;
1075 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1076 if (absinfo->resolution == 0) {
1078 fixed.resolution = 1;
1079 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
1080 device->abs.fake_resolution = 1;
1082 device->abs.absinfo_y = absinfo;
1085 /* We only handle the slotted Protocol B in weston.
1086 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1087 require mtdev for conversion. */
1088 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1089 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1090 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1091 if (absinfo->resolution == 0) {
1093 fixed.resolution = 1;
1094 libevdev_set_abs_info(evdev,
1097 device->abs.fake_resolution = 1;
1099 device->abs.absinfo_x = absinfo;
1100 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1101 if (absinfo->resolution == 0) {
1103 fixed.resolution = 1;
1104 libevdev_set_abs_info(evdev,
1107 device->abs.fake_resolution = 1;
1109 device->abs.absinfo_y = absinfo;
1114 if (evdev_need_mtdev(device)) {
1115 device->mtdev = mtdev_new_open(device->fd);
1119 num_slots = device->mtdev->caps.slot.maximum;
1120 if (device->mtdev->caps.slot.minimum < 0 ||
1123 active_slot = device->mtdev->caps.slot.value;
1125 num_slots = libevdev_get_num_slots(device->evdev);
1126 active_slot = libevdev_get_current_slot(evdev);
1129 slots = calloc(num_slots, sizeof(struct mt_slot));
1133 for (slot = 0; slot < num_slots; ++slot) {
1134 slots[slot].seat_slot = -1;
1138 device->mt.slots = slots;
1139 device->mt.slots_len = num_slots;
1140 device->mt.slot = active_slot;
1144 if (libevdev_has_property(evdev, INPUT_PROP_POINTING_STICK)) {
1145 libinput_timer_init(&device->scroll.timer,
1146 device->base.seat->libinput,
1147 evdev_middle_button_scroll_timeout,
1149 device->scroll.has_middle_button_scroll = true;
1152 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1153 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1156 if (libevdev_has_event_type(evdev, EV_KEY)) {
1157 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1158 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1159 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1160 (has_abs || has_mt)) {
1161 device->dispatch = evdev_mt_touchpad_create(device);
1163 "input device '%s', %s is a touchpad\n",
1164 device->devname, device->devnode);
1165 return device->dispatch == NULL ? -1 : 0;
1168 for (i = 0; i < KEY_MAX; i++) {
1169 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1170 switch (get_key_type(i)) {
1171 case EVDEV_KEY_TYPE_NONE:
1173 case EVDEV_KEY_TYPE_KEY:
1176 case EVDEV_KEY_TYPE_BUTTON:
1183 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1186 if (libevdev_has_event_type(evdev, EV_LED))
1189 if ((has_abs || has_rel) && has_button) {
1190 if (evdev_device_init_pointer_acceleration(device) == -1)
1193 device->seat_caps |= EVDEV_DEVICE_POINTER;
1196 "input device '%s', %s is a pointer caps =%s%s%s\n",
1197 device->devname, device->devnode,
1198 has_abs ? " absolute-motion" : "",
1199 has_rel ? " relative-motion": "",
1200 has_button ? " button" : "");
1202 /* want left-handed config option */
1203 device->buttons.want_left_handed = true;
1206 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1208 "input device '%s', %s is a keyboard\n",
1209 device->devname, device->devnode);
1211 if (has_touch && !has_button) {
1212 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1214 "input device '%s', %s is a touch device\n",
1215 device->devname, device->devnode);
1222 evdev_notify_added_device(struct evdev_device *device)
1224 struct libinput_device *dev;
1226 list_for_each(dev, &device->base.seat->devices_list, link) {
1227 struct evdev_device *d = (struct evdev_device*)dev;
1228 if (dev == &device->base)
1231 /* Notify existing device d about addition of device device */
1232 if (d->dispatch->interface->device_added)
1233 d->dispatch->interface->device_added(d, device);
1235 /* Notify new device device about existing device d */
1236 if (device->dispatch->interface->device_added)
1237 device->dispatch->interface->device_added(device, d);
1239 /* Notify new device device if existing device d is suspended */
1240 if (d->suspended && device->dispatch->interface->device_suspended)
1241 device->dispatch->interface->device_suspended(device, d);
1244 notify_added_device(&device->base);
1247 struct evdev_device *
1248 evdev_device_create(struct libinput_seat *seat,
1249 const char *devnode,
1250 const char *sysname,
1251 const char *syspath)
1253 struct libinput *libinput = seat->libinput;
1254 struct evdev_device *device;
1257 int unhandled_device = 0;
1259 /* Use non-blocking mode so that we can loop on read on
1260 * evdev_device_data() until all events on the fd are
1261 * read. mtdev_get() also expects this. */
1262 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1265 "opening input device '%s' failed (%s).\n",
1266 devnode, strerror(-fd));
1270 device = zalloc(sizeof *device);
1274 libinput_device_init(&device->base, seat);
1275 libinput_seat_ref(seat);
1277 rc = libevdev_new_from_fd(fd, &device->evdev);
1281 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1283 device->seat_caps = 0;
1285 device->mtdev = NULL;
1286 device->devnode = strdup(devnode);
1287 device->sysname = strdup(sysname);
1288 device->syspath = strdup(syspath);
1291 device->abs.seat_slot = -1;
1292 device->dispatch = NULL;
1294 device->pending_event = EVDEV_NONE;
1295 device->devname = libevdev_get_name(device->evdev);
1296 device->scroll.threshold = 5.0; /* Default may be overridden */
1297 device->scroll.direction = 0;
1298 device->dpi = DEFAULT_MOUSE_DPI;
1300 matrix_init_identity(&device->abs.calibration);
1301 matrix_init_identity(&device->abs.usermatrix);
1302 matrix_init_identity(&device->abs.default_calibration);
1304 if (evdev_configure_device(device) == -1)
1307 if (device->seat_caps == 0) {
1308 unhandled_device = 1;
1312 /* If the dispatch was not set up use the fallback. */
1313 if (device->dispatch == NULL)
1314 device->dispatch = fallback_dispatch_create(&device->base);
1315 if (device->dispatch == NULL)
1319 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1320 if (!device->source)
1323 list_insert(seat->devices_list.prev, &device->base.link);
1325 evdev_tag_device(device);
1326 evdev_notify_added_device(device);
1332 close_restricted(libinput, fd);
1333 evdev_device_destroy(device);
1335 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1339 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1341 memset(keys, 0, size);
1346 evdev_device_get_output(struct evdev_device *device)
1348 return device->output_name;
1352 evdev_device_get_sysname(struct evdev_device *device)
1354 return device->sysname;
1358 evdev_device_get_name(struct evdev_device *device)
1360 return device->devname;
1364 evdev_device_get_id_product(struct evdev_device *device)
1366 return libevdev_get_id_product(device->evdev);
1370 evdev_device_get_id_vendor(struct evdev_device *device)
1372 return libevdev_get_id_vendor(device->evdev);
1376 evdev_device_set_default_calibration(struct evdev_device *device,
1377 const float calibration[6])
1379 matrix_from_farray6(&device->abs.default_calibration, calibration);
1380 evdev_device_calibrate(device, calibration);
1384 evdev_device_calibrate(struct evdev_device *device,
1385 const float calibration[6])
1387 struct matrix scale,
1392 matrix_from_farray6(&transform, calibration);
1393 device->abs.apply_calibration = !matrix_is_identity(&transform);
1395 if (!device->abs.apply_calibration) {
1396 matrix_init_identity(&device->abs.calibration);
1400 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1401 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1403 /* The transformation matrix is in the form:
1407 * Where a, e are the scale components, a, b, d, e are the rotation
1408 * component (combined with scale) and c and f are the translation
1409 * component. The translation component in the input matrix must be
1410 * normalized to multiples of the device width and height,
1411 * respectively. e.g. c == 1 shifts one device-width to the right.
1413 * We pre-calculate a single matrix to apply to event coordinates:
1414 * M = Un-Normalize * Calibration * Normalize
1416 * Normalize: scales the device coordinates to [0,1]
1417 * Calibration: user-supplied matrix
1418 * Un-Normalize: scales back up to device coordinates
1419 * Matrix maths requires the normalize/un-normalize in reverse
1423 /* back up the user matrix so we can return it on request */
1424 matrix_from_farray6(&device->abs.usermatrix, calibration);
1427 matrix_init_translate(&translate,
1428 device->abs.absinfo_x->minimum,
1429 device->abs.absinfo_y->minimum);
1430 matrix_init_scale(&scale, sx, sy);
1431 matrix_mult(&scale, &translate, &scale);
1434 matrix_mult(&transform, &scale, &transform);
1437 matrix_init_translate(&translate,
1438 -device->abs.absinfo_x->minimum/sx,
1439 -device->abs.absinfo_y->minimum/sy);
1440 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1441 matrix_mult(&scale, &translate, &scale);
1443 /* store final matrix in device */
1444 matrix_mult(&device->abs.calibration, &transform, &scale);
1448 evdev_device_has_capability(struct evdev_device *device,
1449 enum libinput_device_capability capability)
1451 switch (capability) {
1452 case LIBINPUT_DEVICE_CAP_POINTER:
1453 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1454 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1455 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1456 case LIBINPUT_DEVICE_CAP_TOUCH:
1457 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1464 evdev_device_get_size(struct evdev_device *device,
1468 const struct input_absinfo *x, *y;
1470 x = libevdev_get_abs_info(device->evdev, ABS_X);
1471 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1473 if (!x || !y || device->abs.fake_resolution ||
1474 !x->resolution || !y->resolution)
1477 *width = evdev_convert_to_mm(x, x->maximum);
1478 *height = evdev_convert_to_mm(y, y->maximum);
1484 evdev_post_scroll(struct evdev_device *device,
1489 if (dy <= -device->scroll.threshold || dy >= device->scroll.threshold)
1490 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1492 if (dx <= -device->scroll.threshold || dx >= device->scroll.threshold)
1493 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1496 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
1497 pointer_notify_axis(&device->base,
1499 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1504 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
1505 pointer_notify_axis(&device->base,
1507 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1513 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1515 /* terminate scrolling with a zero scroll event */
1516 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1517 pointer_notify_axis(&device->base,
1519 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1521 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1522 pointer_notify_axis(&device->base,
1524 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1527 device->scroll.direction = 0;
1531 release_pressed_keys(struct evdev_device *device)
1533 struct libinput *libinput = device->base.seat->libinput;
1537 if ((time = libinput_now(libinput)) == 0)
1540 for (code = 0; code < KEY_CNT; code++) {
1541 int count = get_key_down_count(device, code);
1544 log_bug_libinput(libinput,
1545 "Key %d is down %d times.\n",
1550 while (get_key_down_count(device, code) > 0) {
1551 switch (get_key_type(code)) {
1552 case EVDEV_KEY_TYPE_NONE:
1554 case EVDEV_KEY_TYPE_KEY:
1555 evdev_keyboard_notify_key(
1559 LIBINPUT_KEY_STATE_RELEASED);
1561 case EVDEV_KEY_TYPE_BUTTON:
1562 evdev_pointer_notify_button(
1565 evdev_to_left_handed(device, code),
1566 LIBINPUT_BUTTON_STATE_RELEASED);
1574 evdev_notify_suspended_device(struct evdev_device *device)
1576 struct libinput_device *it;
1578 if (device->suspended)
1581 list_for_each(it, &device->base.seat->devices_list, link) {
1582 struct evdev_device *d = (struct evdev_device*)it;
1583 if (it == &device->base)
1586 if (d->dispatch->interface->device_suspended)
1587 d->dispatch->interface->device_suspended(d, device);
1590 device->suspended = 1;
1594 evdev_notify_resumed_device(struct evdev_device *device)
1596 struct libinput_device *it;
1598 if (!device->suspended)
1601 list_for_each(it, &device->base.seat->devices_list, link) {
1602 struct evdev_device *d = (struct evdev_device*)it;
1603 if (it == &device->base)
1606 if (d->dispatch->interface->device_resumed)
1607 d->dispatch->interface->device_resumed(d, device);
1610 device->suspended = 0;
1614 evdev_device_suspend(struct evdev_device *device)
1616 evdev_notify_suspended_device(device);
1618 if (device->source) {
1619 libinput_remove_source(device->base.seat->libinput,
1621 device->source = NULL;
1624 release_pressed_keys(device);
1626 if (device->mtdev) {
1627 mtdev_close_delete(device->mtdev);
1628 device->mtdev = NULL;
1631 if (device->fd != -1) {
1632 close_restricted(device->base.seat->libinput, device->fd);
1640 evdev_device_compare_syspath(struct evdev_device *device, int fd)
1642 struct udev *udev = NULL;
1643 struct udev_device *udev_device = NULL;
1644 const char *syspath;
1652 if (fstat(fd, &st) < 0)
1655 udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1659 syspath = udev_device_get_syspath(udev_device);
1660 rc = strcmp(syspath, device->syspath);
1663 udev_device_unref(udev_device);
1670 evdev_device_resume(struct evdev_device *device)
1672 struct libinput *libinput = device->base.seat->libinput;
1675 if (device->fd != -1)
1678 if (device->syspath == NULL)
1681 fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1686 if (evdev_device_compare_syspath(device, fd)) {
1687 close_restricted(libinput, fd);
1693 if (evdev_need_mtdev(device)) {
1694 device->mtdev = mtdev_new_open(device->fd);
1700 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1701 if (!device->source) {
1702 mtdev_close_delete(device->mtdev);
1706 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1708 evdev_notify_resumed_device(device);
1714 evdev_device_remove(struct evdev_device *device)
1716 struct libinput_device *dev;
1718 list_for_each(dev, &device->base.seat->devices_list, link) {
1719 struct evdev_device *d = (struct evdev_device*)dev;;
1720 if (dev == &device->base)
1723 if (d->dispatch->interface->device_removed)
1724 d->dispatch->interface->device_removed(d, device);
1727 evdev_device_suspend(device);
1729 /* A device may be removed while suspended. Free the syspath to
1730 * skip re-opening a different device with the same node */
1731 free(device->syspath);
1732 device->syspath = NULL;
1734 list_remove(&device->base.link);
1736 notify_removed_device(&device->base);
1737 libinput_device_unref(&device->base);
1741 evdev_device_destroy(struct evdev_device *device)
1743 struct evdev_dispatch *dispatch;
1745 dispatch = device->dispatch;
1747 dispatch->interface->destroy(dispatch);
1749 filter_destroy(device->pointer.filter);
1750 libinput_seat_unref(device->base.seat);
1751 libevdev_free(device->evdev);
1752 free(device->mt.slots);
1753 free(device->devnode);
1754 free(device->sysname);
1755 free(device->syspath);