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);
123 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
125 static const struct {
126 enum libinput_led weston;
129 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
130 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
131 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
133 struct input_event ev[ARRAY_LENGTH(map) + 1];
136 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
139 memset(ev, 0, sizeof(ev));
140 for (i = 0; i < ARRAY_LENGTH(map); i++) {
142 ev[i].code = map[i].evdev;
143 ev[i].value = !!(leds & map[i].weston);
146 ev[i].code = SYN_REPORT;
148 i = write(device->fd, ev, sizeof ev);
149 (void)i; /* no, we really don't care about the return value */
153 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
155 if (!device->abs.apply_calibration)
158 matrix_mult_vec(&device->abs.calibration, x, y);
162 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
164 return (val - absinfo->minimum) * to_range /
165 (absinfo->maximum - absinfo->minimum + 1);
169 evdev_device_transform_x(struct evdev_device *device,
173 return scale_axis(device->abs.absinfo_x, x, width);
177 evdev_device_transform_y(struct evdev_device *device,
181 return scale_axis(device->abs.absinfo_y, y, height);
185 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
187 struct libinput *libinput = device->base.seat->libinput;
188 struct motion_params motion;
193 struct libinput_device *base = &device->base;
194 struct libinput_seat *seat = base->seat;
196 slot = device->mt.slot;
198 switch (device->pending_event) {
201 case EVDEV_RELATIVE_MOTION:
202 motion.dx = device->rel.dx;
203 motion.dy = device->rel.dy;
207 /* Use unaccelerated deltas for pointing stick scroll */
208 if (device->scroll.has_middle_button_scroll &&
209 hw_is_key_down(device, BTN_MIDDLE)) {
210 if (device->scroll.middle_button_scroll_active)
211 evdev_post_scroll(device, time,
212 motion.dx, motion.dy);
216 /* Apply pointer acceleration. */
217 filter_dispatch(device->pointer.filter, &motion, device, time);
219 if (motion.dx == 0.0 && motion.dy == 0.0)
222 pointer_notify_motion(base, time, motion.dx, motion.dy);
224 case EVDEV_ABSOLUTE_MT_DOWN:
225 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
228 if (device->mt.slots[slot].seat_slot != -1) {
229 log_bug_kernel(libinput,
230 "%s: Driver sent multiple touch down for the "
231 "same slot", device->devnode);
235 seat_slot = ffs(~seat->slot_map) - 1;
236 device->mt.slots[slot].seat_slot = seat_slot;
241 seat->slot_map |= 1 << seat_slot;
242 x = device->mt.slots[slot].x;
243 y = device->mt.slots[slot].y;
244 transform_absolute(device, &x, &y);
246 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
248 case EVDEV_ABSOLUTE_MT_MOTION:
249 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
252 seat_slot = device->mt.slots[slot].seat_slot;
253 x = device->mt.slots[slot].x;
254 y = device->mt.slots[slot].y;
259 transform_absolute(device, &x, &y);
260 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
262 case EVDEV_ABSOLUTE_MT_UP:
263 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
266 seat_slot = device->mt.slots[slot].seat_slot;
267 device->mt.slots[slot].seat_slot = -1;
272 seat->slot_map &= ~(1 << seat_slot);
274 touch_notify_touch_up(base, time, slot, seat_slot);
276 case EVDEV_ABSOLUTE_TOUCH_DOWN:
277 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
280 if (device->abs.seat_slot != -1) {
281 log_bug_kernel(libinput,
282 "%s: Driver sent multiple touch down for the "
283 "same slot", device->devnode);
287 seat_slot = ffs(~seat->slot_map) - 1;
288 device->abs.seat_slot = seat_slot;
293 seat->slot_map |= 1 << seat_slot;
297 transform_absolute(device, &cx, &cy);
299 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
301 case EVDEV_ABSOLUTE_MOTION:
304 transform_absolute(device, &cx, &cy);
308 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
309 seat_slot = device->abs.seat_slot;
314 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
315 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
316 pointer_notify_motion_absolute(base, time, x, y);
319 case EVDEV_ABSOLUTE_TOUCH_UP:
320 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
323 seat_slot = device->abs.seat_slot;
324 device->abs.seat_slot = -1;
329 seat->slot_map &= ~(1 << seat_slot);
331 touch_notify_touch_up(base, time, -1, seat_slot);
334 assert(0 && "Unknown pending event type");
338 device->pending_event = EVDEV_NONE;
341 static enum evdev_key_type
342 get_key_type(uint16_t code)
344 if (code == BTN_TOUCH)
345 return EVDEV_KEY_TYPE_NONE;
347 if (code >= KEY_ESC && code <= KEY_MICMUTE)
348 return EVDEV_KEY_TYPE_KEY;
349 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
350 return EVDEV_KEY_TYPE_BUTTON;
351 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
352 return EVDEV_KEY_TYPE_KEY;
353 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
354 return EVDEV_KEY_TYPE_BUTTON;
355 return EVDEV_KEY_TYPE_NONE;
359 evdev_middle_button_scroll_timeout(uint64_t time, void *data)
361 struct evdev_device *device = data;
363 device->scroll.middle_button_scroll_active = true;
367 evdev_middle_button_scroll_button(struct evdev_device *device,
368 uint64_t time, int is_press)
371 libinput_timer_set(&device->scroll.timer,
372 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
374 libinput_timer_cancel(&device->scroll.timer);
375 if (device->scroll.middle_button_scroll_active) {
376 evdev_stop_scroll(device, time);
377 device->scroll.middle_button_scroll_active = false;
379 /* If the button is released quickly enough emit the
380 * button press/release events. */
381 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
382 LIBINPUT_BUTTON_STATE_PRESSED);
383 evdev_pointer_notify_button(device, time, BTN_MIDDLE,
384 LIBINPUT_BUTTON_STATE_RELEASED);
390 evdev_process_touch_button(struct evdev_device *device,
391 uint64_t time, int value)
393 if (device->pending_event != EVDEV_NONE &&
394 device->pending_event != EVDEV_ABSOLUTE_MOTION)
395 evdev_flush_pending_event(device, time);
397 device->pending_event = (value ?
398 EVDEV_ABSOLUTE_TOUCH_DOWN :
399 EVDEV_ABSOLUTE_TOUCH_UP);
403 evdev_process_key(struct evdev_device *device,
404 struct input_event *e, uint64_t time)
406 enum evdev_key_type type;
408 /* ignore kernel key repeat */
412 if (e->code == BTN_TOUCH) {
414 evdev_process_touch_button(device, time, e->value);
418 evdev_flush_pending_event(device, time);
420 type = get_key_type(e->code);
422 /* Ignore key release events from the kernel for keys that libinput
423 * never got a pressed event for. */
426 case EVDEV_KEY_TYPE_NONE:
428 case EVDEV_KEY_TYPE_KEY:
429 case EVDEV_KEY_TYPE_BUTTON:
430 if (!hw_is_key_down(device, e->code))
435 hw_set_key_down(device, e->code, e->value);
438 case EVDEV_KEY_TYPE_NONE:
440 case EVDEV_KEY_TYPE_KEY:
441 evdev_keyboard_notify_key(
445 e->value ? LIBINPUT_KEY_STATE_PRESSED :
446 LIBINPUT_KEY_STATE_RELEASED);
448 case EVDEV_KEY_TYPE_BUTTON:
449 if (device->scroll.has_middle_button_scroll &&
450 e->code == BTN_MIDDLE) {
451 evdev_middle_button_scroll_button(device, time,
455 evdev_pointer_notify_button(
459 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
460 LIBINPUT_BUTTON_STATE_RELEASED);
466 evdev_process_touch(struct evdev_device *device,
467 struct input_event *e,
472 evdev_flush_pending_event(device, time);
473 device->mt.slot = e->value;
475 case ABS_MT_TRACKING_ID:
476 if (device->pending_event != EVDEV_NONE &&
477 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
478 evdev_flush_pending_event(device, time);
480 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
482 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
484 case ABS_MT_POSITION_X:
485 device->mt.slots[device->mt.slot].x = e->value;
486 if (device->pending_event == EVDEV_NONE)
487 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
489 case ABS_MT_POSITION_Y:
490 device->mt.slots[device->mt.slot].y = e->value;
491 if (device->pending_event == EVDEV_NONE)
492 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
498 evdev_process_absolute_motion(struct evdev_device *device,
499 struct input_event *e)
503 device->abs.x = e->value;
504 if (device->pending_event == EVDEV_NONE)
505 device->pending_event = EVDEV_ABSOLUTE_MOTION;
508 device->abs.y = e->value;
509 if (device->pending_event == EVDEV_NONE)
510 device->pending_event = EVDEV_ABSOLUTE_MOTION;
516 evdev_process_relative(struct evdev_device *device,
517 struct input_event *e, uint64_t time)
519 struct libinput_device *base = &device->base;
523 if (device->pending_event != EVDEV_RELATIVE_MOTION)
524 evdev_flush_pending_event(device, time);
525 device->rel.dx += e->value;
526 device->pending_event = EVDEV_RELATIVE_MOTION;
529 if (device->pending_event != EVDEV_RELATIVE_MOTION)
530 evdev_flush_pending_event(device, time);
531 device->rel.dy += e->value;
532 device->pending_event = EVDEV_RELATIVE_MOTION;
535 evdev_flush_pending_event(device, time);
539 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
540 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
543 evdev_flush_pending_event(device, time);
552 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
553 e->value * DEFAULT_AXIS_STEP_DISTANCE);
563 evdev_process_absolute(struct evdev_device *device,
564 struct input_event *e,
568 evdev_process_touch(device, e, time);
570 evdev_process_absolute_motion(device, e);
575 evdev_need_touch_frame(struct evdev_device *device)
577 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
580 switch (device->pending_event) {
582 case EVDEV_RELATIVE_MOTION:
584 case EVDEV_ABSOLUTE_MT_DOWN:
585 case EVDEV_ABSOLUTE_MT_MOTION:
586 case EVDEV_ABSOLUTE_MT_UP:
587 case EVDEV_ABSOLUTE_TOUCH_DOWN:
588 case EVDEV_ABSOLUTE_TOUCH_UP:
589 case EVDEV_ABSOLUTE_MOTION:
597 evdev_tag_external_mouse(struct evdev_device *device,
598 struct udev_device *udev_device)
602 bustype = libevdev_get_id_bustype(device->evdev);
603 if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
604 if (device->seat_caps & EVDEV_DEVICE_POINTER)
605 device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
610 evdev_tag_trackpoint(struct evdev_device *device,
611 struct udev_device *udev_device)
613 if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
614 device->tags |= EVDEV_TAG_TRACKPOINT;
618 fallback_process(struct evdev_dispatch *dispatch,
619 struct evdev_device *device,
620 struct input_event *event,
625 switch (event->type) {
627 evdev_process_relative(device, event, time);
630 evdev_process_absolute(device, event, time);
633 evdev_process_key(device, event, time);
636 need_frame = evdev_need_touch_frame(device);
637 evdev_flush_pending_event(device, time);
639 touch_notify_frame(&device->base, time);
645 fallback_destroy(struct evdev_dispatch *dispatch)
651 fallback_tag_device(struct evdev_device *device,
652 struct udev_device *udev_device)
654 evdev_tag_external_mouse(device, udev_device);
655 evdev_tag_trackpoint(device, udev_device);
659 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
661 struct evdev_device *device = (struct evdev_device*)libinput_device;
663 return device->abs.absinfo_x && device->abs.absinfo_y;
666 static enum libinput_config_status
667 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
668 const float matrix[6])
670 struct evdev_device *device = (struct evdev_device*)libinput_device;
672 evdev_device_calibrate(device, matrix);
674 return LIBINPUT_CONFIG_STATUS_SUCCESS;
678 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
681 struct evdev_device *device = (struct evdev_device*)libinput_device;
683 matrix_to_farray6(&device->abs.usermatrix, matrix);
685 return !matrix_is_identity(&device->abs.usermatrix);
689 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
692 struct evdev_device *device = (struct evdev_device*)libinput_device;
694 matrix_to_farray6(&device->abs.default_calibration, matrix);
696 return !matrix_is_identity(&device->abs.default_calibration);
699 struct evdev_dispatch_interface fallback_interface = {
702 NULL, /* device_added */
703 NULL, /* device_removed */
704 NULL, /* device_suspended */
705 NULL, /* device_resumed */
710 evdev_sendevents_get_modes(struct libinput_device *device)
712 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED |
713 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
716 static enum libinput_config_status
717 evdev_sendevents_set_mode(struct libinput_device *device,
718 enum libinput_config_send_events_mode mode)
720 struct evdev_device *evdev = (struct evdev_device*)device;
721 struct evdev_dispatch *dispatch = evdev->dispatch;
723 if (mode == dispatch->sendevents.current_mode)
724 return LIBINPUT_CONFIG_STATUS_SUCCESS;
727 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
728 evdev_device_resume(evdev);
730 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
731 evdev_device_suspend(evdev);
734 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
737 dispatch->sendevents.current_mode = mode;
739 return LIBINPUT_CONFIG_STATUS_SUCCESS;
742 static enum libinput_config_send_events_mode
743 evdev_sendevents_get_mode(struct libinput_device *device)
745 struct evdev_device *evdev = (struct evdev_device*)device;
746 struct evdev_dispatch *dispatch = evdev->dispatch;
748 return dispatch->sendevents.current_mode;
751 static enum libinput_config_send_events_mode
752 evdev_sendevents_get_default_mode(struct libinput_device *device)
754 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
757 static struct evdev_dispatch *
758 fallback_dispatch_create(struct libinput_device *device)
760 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
761 if (dispatch == NULL)
764 dispatch->interface = &fallback_interface;
766 device->config.calibration = &dispatch->calibration;
768 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
769 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
770 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
771 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
773 device->config.sendevents = &dispatch->sendevents.config;
775 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
776 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
777 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
778 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
779 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
785 evdev_process_event(struct evdev_device *device, struct input_event *e)
787 struct evdev_dispatch *dispatch = device->dispatch;
788 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
790 dispatch->interface->process(dispatch, device, e, time);
794 evdev_device_dispatch_one(struct evdev_device *device,
795 struct input_event *ev)
797 if (!device->mtdev) {
798 evdev_process_event(device, ev);
800 mtdev_put_event(device->mtdev, ev);
801 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
802 while (!mtdev_empty(device->mtdev)) {
803 struct input_event e;
804 mtdev_get_event(device->mtdev, &e);
805 evdev_process_event(device, &e);
812 evdev_sync_device(struct evdev_device *device)
814 struct input_event ev;
818 rc = libevdev_next_event(device->evdev,
819 LIBEVDEV_READ_FLAG_SYNC, &ev);
822 evdev_device_dispatch_one(device, &ev);
823 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
825 return rc == -EAGAIN ? 0 : rc;
829 evdev_device_dispatch(void *data)
831 struct evdev_device *device = data;
832 struct libinput *libinput = device->base.seat->libinput;
833 struct input_event ev;
836 /* If the compositor is repainting, this function is called only once
837 * per frame and we have to process all the events available on the
838 * fd, otherwise there will be input lag. */
840 rc = libevdev_next_event(device->evdev,
841 LIBEVDEV_READ_FLAG_NORMAL, &ev);
842 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
843 /* send one more sync event so we handle all
844 currently pending events before we sync up
845 to the current state */
846 ev.code = SYN_REPORT;
847 evdev_device_dispatch_one(device, &ev);
849 rc = evdev_sync_device(device);
851 rc = LIBEVDEV_READ_STATUS_SUCCESS;
852 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
853 evdev_device_dispatch_one(device, &ev);
855 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
857 if (rc != -EAGAIN && rc != -EINTR) {
858 libinput_remove_source(libinput, device->source);
859 device->source = NULL;
864 evdev_accel_config_available(struct libinput_device *device)
866 /* this function is only called if we set up ptraccel, so we can
867 reply with a resounding "Yes" */
871 static enum libinput_config_status
872 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
874 struct evdev_device *dev = (struct evdev_device *)device;
876 if (!filter_set_speed(dev->pointer.filter, speed))
877 return LIBINPUT_CONFIG_STATUS_INVALID;
879 return LIBINPUT_CONFIG_STATUS_SUCCESS;
883 evdev_accel_config_get_speed(struct libinput_device *device)
885 struct evdev_device *dev = (struct evdev_device *)device;
887 return filter_get_speed(dev->pointer.filter);
891 evdev_accel_config_get_default_speed(struct libinput_device *device)
897 evdev_device_init_pointer_acceleration(struct evdev_device *device)
899 device->pointer.filter =
900 create_pointer_accelator_filter(
901 pointer_accel_profile_linear);
902 if (!device->pointer.filter)
905 device->pointer.config.available = evdev_accel_config_available;
906 device->pointer.config.set_speed = evdev_accel_config_set_speed;
907 device->pointer.config.get_speed = evdev_accel_config_get_speed;
908 device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
909 device->base.config.accel = &device->pointer.config;
916 evdev_need_mtdev(struct evdev_device *device)
918 struct libevdev *evdev = device->evdev;
920 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
921 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
922 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
926 evdev_tag_device(struct evdev_device *device)
929 struct udev_device *udev_device = NULL;
935 udev_device = udev_device_new_from_syspath(udev, device->syspath);
937 if (device->dispatch->interface->tag_device)
938 device->dispatch->interface->tag_device(device, udev_device);
939 udev_device_unref(udev_device);
945 evdev_configure_device(struct evdev_device *device)
947 struct libinput *libinput = device->base.seat->libinput;
948 struct libevdev *evdev = device->evdev;
949 const struct input_absinfo *absinfo;
950 struct input_absinfo fixed;
951 int has_abs, has_rel, has_mt;
952 int has_button, has_keyboard, has_touch;
953 struct mt_slot *slots;
966 if (libevdev_has_event_type(evdev, EV_ABS)) {
968 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
969 if (absinfo->resolution == 0) {
971 fixed.resolution = 1;
972 libevdev_set_abs_info(evdev, ABS_X, &fixed);
973 device->abs.fake_resolution = 1;
975 device->abs.absinfo_x = absinfo;
978 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
979 if (absinfo->resolution == 0) {
981 fixed.resolution = 1;
982 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
983 device->abs.fake_resolution = 1;
985 device->abs.absinfo_y = absinfo;
988 /* We only handle the slotted Protocol B in weston.
989 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
990 require mtdev for conversion. */
991 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
992 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
993 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
994 if (absinfo->resolution == 0) {
996 fixed.resolution = 1;
997 libevdev_set_abs_info(evdev,
1000 device->abs.fake_resolution = 1;
1002 device->abs.absinfo_x = absinfo;
1003 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1004 if (absinfo->resolution == 0) {
1006 fixed.resolution = 1;
1007 libevdev_set_abs_info(evdev,
1010 device->abs.fake_resolution = 1;
1012 device->abs.absinfo_y = absinfo;
1017 if (evdev_need_mtdev(device)) {
1018 device->mtdev = mtdev_new_open(device->fd);
1022 num_slots = device->mtdev->caps.slot.maximum;
1023 if (device->mtdev->caps.slot.minimum < 0 ||
1026 active_slot = device->mtdev->caps.slot.value;
1028 num_slots = libevdev_get_num_slots(device->evdev);
1029 active_slot = libevdev_get_current_slot(evdev);
1032 slots = calloc(num_slots, sizeof(struct mt_slot));
1036 for (slot = 0; slot < num_slots; ++slot) {
1037 slots[slot].seat_slot = -1;
1041 device->mt.slots = slots;
1042 device->mt.slots_len = num_slots;
1043 device->mt.slot = active_slot;
1047 if (libevdev_has_property(evdev, INPUT_PROP_POINTING_STICK)) {
1048 libinput_timer_init(&device->scroll.timer,
1049 device->base.seat->libinput,
1050 evdev_middle_button_scroll_timeout,
1052 device->scroll.has_middle_button_scroll = true;
1055 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1056 libevdev_has_event_code(evdev, EV_REL, REL_Y))
1059 if (libevdev_has_event_type(evdev, EV_KEY)) {
1060 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1061 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1062 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1063 (has_abs || has_mt)) {
1064 device->dispatch = evdev_mt_touchpad_create(device);
1066 "input device '%s', %s is a touchpad\n",
1067 device->devname, device->devnode);
1068 return device->dispatch == NULL ? -1 : 0;
1071 for (i = 0; i < KEY_MAX; i++) {
1072 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1073 switch (get_key_type(i)) {
1074 case EVDEV_KEY_TYPE_NONE:
1076 case EVDEV_KEY_TYPE_KEY:
1079 case EVDEV_KEY_TYPE_BUTTON:
1086 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1089 if (libevdev_has_event_type(evdev, EV_LED))
1092 if ((has_abs || has_rel) && has_button) {
1093 if (evdev_device_init_pointer_acceleration(device) == -1)
1096 device->seat_caps |= EVDEV_DEVICE_POINTER;
1099 "input device '%s', %s is a pointer caps =%s%s%s\n",
1100 device->devname, device->devnode,
1101 has_abs ? " absolute-motion" : "",
1102 has_rel ? " relative-motion": "",
1103 has_button ? " button" : "");
1106 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1108 "input device '%s', %s is a keyboard\n",
1109 device->devname, device->devnode);
1111 if (has_touch && !has_button) {
1112 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1114 "input device '%s', %s is a touch device\n",
1115 device->devname, device->devnode);
1122 evdev_notify_added_device(struct evdev_device *device)
1124 struct libinput_device *dev;
1126 list_for_each(dev, &device->base.seat->devices_list, link) {
1127 struct evdev_device *d = (struct evdev_device*)dev;
1128 if (dev == &device->base)
1131 /* Notify existing device d about addition of device device */
1132 if (d->dispatch->interface->device_added)
1133 d->dispatch->interface->device_added(d, device);
1135 /* Notify new device device about existing device d */
1136 if (device->dispatch->interface->device_added)
1137 device->dispatch->interface->device_added(device, d);
1139 /* Notify new device device if existing device d is suspended */
1140 if (d->suspended && device->dispatch->interface->device_suspended)
1141 device->dispatch->interface->device_suspended(device, d);
1144 notify_added_device(&device->base);
1147 struct evdev_device *
1148 evdev_device_create(struct libinput_seat *seat,
1149 const char *devnode,
1150 const char *sysname,
1151 const char *syspath)
1153 struct libinput *libinput = seat->libinput;
1154 struct evdev_device *device;
1157 int unhandled_device = 0;
1159 /* Use non-blocking mode so that we can loop on read on
1160 * evdev_device_data() until all events on the fd are
1161 * read. mtdev_get() also expects this. */
1162 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1165 "opening input device '%s' failed (%s).\n",
1166 devnode, strerror(-fd));
1170 device = zalloc(sizeof *device);
1174 libinput_device_init(&device->base, seat);
1175 libinput_seat_ref(seat);
1177 rc = libevdev_new_from_fd(fd, &device->evdev);
1181 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1183 device->seat_caps = 0;
1185 device->mtdev = NULL;
1186 device->devnode = strdup(devnode);
1187 device->sysname = strdup(sysname);
1188 device->syspath = strdup(syspath);
1191 device->abs.seat_slot = -1;
1192 device->dispatch = NULL;
1194 device->pending_event = EVDEV_NONE;
1195 device->devname = libevdev_get_name(device->evdev);
1196 device->scroll.threshold = 5.0; /* Default may be overridden */
1197 device->scroll.direction = 0;
1199 matrix_init_identity(&device->abs.calibration);
1200 matrix_init_identity(&device->abs.usermatrix);
1201 matrix_init_identity(&device->abs.default_calibration);
1203 if (evdev_configure_device(device) == -1)
1206 if (device->seat_caps == 0) {
1207 unhandled_device = 1;
1211 /* If the dispatch was not set up use the fallback. */
1212 if (device->dispatch == NULL)
1213 device->dispatch = fallback_dispatch_create(&device->base);
1214 if (device->dispatch == NULL)
1218 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1219 if (!device->source)
1222 list_insert(seat->devices_list.prev, &device->base.link);
1224 evdev_tag_device(device);
1225 evdev_notify_added_device(device);
1231 close_restricted(libinput, fd);
1232 evdev_device_destroy(device);
1234 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1238 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1240 memset(keys, 0, size);
1245 evdev_device_get_output(struct evdev_device *device)
1247 return device->output_name;
1251 evdev_device_get_sysname(struct evdev_device *device)
1253 return device->sysname;
1257 evdev_device_get_name(struct evdev_device *device)
1259 return device->devname;
1263 evdev_device_get_id_product(struct evdev_device *device)
1265 return libevdev_get_id_product(device->evdev);
1269 evdev_device_get_id_vendor(struct evdev_device *device)
1271 return libevdev_get_id_vendor(device->evdev);
1275 evdev_device_set_default_calibration(struct evdev_device *device,
1276 const float calibration[6])
1278 matrix_from_farray6(&device->abs.default_calibration, calibration);
1279 evdev_device_calibrate(device, calibration);
1283 evdev_device_calibrate(struct evdev_device *device,
1284 const float calibration[6])
1286 struct matrix scale,
1291 matrix_from_farray6(&transform, calibration);
1292 device->abs.apply_calibration = !matrix_is_identity(&transform);
1294 if (!device->abs.apply_calibration) {
1295 matrix_init_identity(&device->abs.calibration);
1299 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1300 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1302 /* The transformation matrix is in the form:
1306 * Where a, e are the scale components, a, b, d, e are the rotation
1307 * component (combined with scale) and c and f are the translation
1308 * component. The translation component in the input matrix must be
1309 * normalized to multiples of the device width and height,
1310 * respectively. e.g. c == 1 shifts one device-width to the right.
1312 * We pre-calculate a single matrix to apply to event coordinates:
1313 * M = Un-Normalize * Calibration * Normalize
1315 * Normalize: scales the device coordinates to [0,1]
1316 * Calibration: user-supplied matrix
1317 * Un-Normalize: scales back up to device coordinates
1318 * Matrix maths requires the normalize/un-normalize in reverse
1322 /* back up the user matrix so we can return it on request */
1323 matrix_from_farray6(&device->abs.usermatrix, calibration);
1326 matrix_init_translate(&translate,
1327 device->abs.absinfo_x->minimum,
1328 device->abs.absinfo_y->minimum);
1329 matrix_init_scale(&scale, sx, sy);
1330 matrix_mult(&scale, &translate, &scale);
1333 matrix_mult(&transform, &scale, &transform);
1336 matrix_init_translate(&translate,
1337 -device->abs.absinfo_x->minimum/sx,
1338 -device->abs.absinfo_y->minimum/sy);
1339 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1340 matrix_mult(&scale, &translate, &scale);
1342 /* store final matrix in device */
1343 matrix_mult(&device->abs.calibration, &transform, &scale);
1347 evdev_device_has_capability(struct evdev_device *device,
1348 enum libinput_device_capability capability)
1350 switch (capability) {
1351 case LIBINPUT_DEVICE_CAP_POINTER:
1352 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1353 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1354 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1355 case LIBINPUT_DEVICE_CAP_TOUCH:
1356 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1363 evdev_device_get_size(struct evdev_device *device,
1367 const struct input_absinfo *x, *y;
1369 x = libevdev_get_abs_info(device->evdev, ABS_X);
1370 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1372 if (!x || !y || device->abs.fake_resolution ||
1373 !x->resolution || !y->resolution)
1376 *width = evdev_convert_to_mm(x, x->maximum);
1377 *height = evdev_convert_to_mm(y, y->maximum);
1383 evdev_post_scroll(struct evdev_device *device,
1388 if (dy <= -device->scroll.threshold || dy >= device->scroll.threshold)
1389 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1391 if (dx <= -device->scroll.threshold || dx >= device->scroll.threshold)
1392 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1395 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
1396 pointer_notify_axis(&device->base,
1398 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1403 (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
1404 pointer_notify_axis(&device->base,
1406 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1412 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1414 /* terminate scrolling with a zero scroll event */
1415 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1416 pointer_notify_axis(&device->base,
1418 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1420 if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1421 pointer_notify_axis(&device->base,
1423 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1426 device->scroll.direction = 0;
1430 release_pressed_keys(struct evdev_device *device)
1432 struct libinput *libinput = device->base.seat->libinput;
1436 if ((time = libinput_now(libinput)) == 0)
1439 for (code = 0; code < KEY_CNT; code++) {
1440 int count = get_key_down_count(device, code);
1443 log_bug_libinput(libinput,
1444 "Key %d is down %d times.\n",
1449 while (get_key_down_count(device, code) > 0) {
1450 switch (get_key_type(code)) {
1451 case EVDEV_KEY_TYPE_NONE:
1453 case EVDEV_KEY_TYPE_KEY:
1454 evdev_keyboard_notify_key(
1458 LIBINPUT_KEY_STATE_RELEASED);
1460 case EVDEV_KEY_TYPE_BUTTON:
1461 evdev_pointer_notify_button(
1465 LIBINPUT_BUTTON_STATE_RELEASED);
1473 evdev_notify_suspended_device(struct evdev_device *device)
1475 struct libinput_device *it;
1477 if (device->suspended)
1480 list_for_each(it, &device->base.seat->devices_list, link) {
1481 struct evdev_device *d = (struct evdev_device*)it;
1482 if (it == &device->base)
1485 if (d->dispatch->interface->device_suspended)
1486 d->dispatch->interface->device_suspended(d, device);
1489 device->suspended = 1;
1493 evdev_notify_resumed_device(struct evdev_device *device)
1495 struct libinput_device *it;
1497 if (!device->suspended)
1500 list_for_each(it, &device->base.seat->devices_list, link) {
1501 struct evdev_device *d = (struct evdev_device*)it;
1502 if (it == &device->base)
1505 if (d->dispatch->interface->device_resumed)
1506 d->dispatch->interface->device_resumed(d, device);
1509 device->suspended = 0;
1513 evdev_device_suspend(struct evdev_device *device)
1515 evdev_notify_suspended_device(device);
1517 if (device->source) {
1518 libinput_remove_source(device->base.seat->libinput,
1520 device->source = NULL;
1523 release_pressed_keys(device);
1525 if (device->mtdev) {
1526 mtdev_close_delete(device->mtdev);
1527 device->mtdev = NULL;
1530 if (device->fd != -1) {
1531 close_restricted(device->base.seat->libinput, device->fd);
1539 evdev_device_compare_syspath(struct evdev_device *device, int fd)
1541 struct udev *udev = NULL;
1542 struct udev_device *udev_device = NULL;
1543 const char *syspath;
1551 if (fstat(fd, &st) < 0)
1554 udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1558 syspath = udev_device_get_syspath(udev_device);
1559 rc = strcmp(syspath, device->syspath);
1562 udev_device_unref(udev_device);
1569 evdev_device_resume(struct evdev_device *device)
1571 struct libinput *libinput = device->base.seat->libinput;
1574 if (device->fd != -1)
1577 if (device->syspath == NULL)
1580 fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1585 if (evdev_device_compare_syspath(device, fd)) {
1586 close_restricted(libinput, fd);
1592 if (evdev_need_mtdev(device)) {
1593 device->mtdev = mtdev_new_open(device->fd);
1599 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1600 if (!device->source) {
1601 mtdev_close_delete(device->mtdev);
1605 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1607 evdev_notify_resumed_device(device);
1613 evdev_device_remove(struct evdev_device *device)
1615 struct libinput_device *dev;
1617 list_for_each(dev, &device->base.seat->devices_list, link) {
1618 struct evdev_device *d = (struct evdev_device*)dev;;
1619 if (dev == &device->base)
1622 if (d->dispatch->interface->device_removed)
1623 d->dispatch->interface->device_removed(d, device);
1626 evdev_device_suspend(device);
1628 /* A device may be removed while suspended. Free the syspath to
1629 * skip re-opening a different device with the same node */
1630 free(device->syspath);
1631 device->syspath = NULL;
1633 list_remove(&device->base.link);
1635 notify_removed_device(&device->base);
1636 libinput_device_unref(&device->base);
1640 evdev_device_destroy(struct evdev_device *device)
1642 struct evdev_dispatch *dispatch;
1644 dispatch = device->dispatch;
1646 dispatch->interface->destroy(dispatch);
1648 filter_destroy(device->pointer.filter);
1649 libinput_seat_unref(device->base.seat);
1650 libevdev_free(device->evdev);
1651 free(device->mt.slots);
1652 free(device->devnode);
1653 free(device->sysname);
1654 free(device->syspath);