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.
29 #include "linux/input.h"
32 #include <mtdev-plumbing.h>
40 #include "libinput-private.h"
42 #define DEFAULT_AXIS_STEP_DISTANCE 10
47 EVDEV_KEY_TYPE_BUTTON,
51 hw_set_key_down(struct evdev_device *device, int code, int pressed)
53 long_set_bit_state(device->hw_key_mask, code, pressed);
57 hw_is_key_down(struct evdev_device *device, int code)
59 return long_bit_is_set(device->hw_key_mask, code);
63 get_key_down_count(struct evdev_device *device, int code)
65 return device->key_count[code];
69 update_key_down_count(struct evdev_device *device, int code, int pressed)
72 assert(code >= 0 && code < KEY_CNT);
75 key_count = ++device->key_count[code];
77 assert(device->key_count[code] > 0);
78 key_count = --device->key_count[code];
82 log_bug_libinput(device->base.seat->libinput,
83 "Key count for %s reached abnormal values\n",
84 libevdev_event_code_get_name(EV_KEY, code));
91 evdev_keyboard_notify_key(struct evdev_device *device,
94 enum libinput_key_state state)
98 down_count = update_key_down_count(device, key, state);
100 if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
101 (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
102 keyboard_notify_key(&device->base, time, key, state);
106 evdev_pointer_notify_button(struct evdev_device *device,
109 enum libinput_button_state state)
113 down_count = update_key_down_count(device, button, state);
115 if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
116 (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0))
117 pointer_notify_button(&device->base, time, button, state);
121 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
123 static const struct {
124 enum libinput_led weston;
127 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
128 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
129 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
131 struct input_event ev[ARRAY_LENGTH(map) + 1];
134 if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
137 memset(ev, 0, sizeof(ev));
138 for (i = 0; i < ARRAY_LENGTH(map); i++) {
140 ev[i].code = map[i].evdev;
141 ev[i].value = !!(leds & map[i].weston);
144 ev[i].code = SYN_REPORT;
146 i = write(device->fd, ev, sizeof ev);
147 (void)i; /* no, we really don't care about the return value */
151 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
153 if (!device->abs.apply_calibration)
156 matrix_mult_vec(&device->abs.calibration, x, y);
160 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
162 return (val - absinfo->minimum) * to_range /
163 (absinfo->maximum - absinfo->minimum + 1);
167 evdev_device_transform_x(struct evdev_device *device,
171 return scale_axis(device->abs.absinfo_x, x, width);
175 evdev_device_transform_y(struct evdev_device *device,
179 return scale_axis(device->abs.absinfo_y, y, height);
183 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
185 struct libinput *libinput = device->base.seat->libinput;
186 struct motion_params motion;
191 struct libinput_device *base = &device->base;
192 struct libinput_seat *seat = base->seat;
194 slot = device->mt.slot;
196 switch (device->pending_event) {
199 case EVDEV_RELATIVE_MOTION:
200 motion.dx = device->rel.dx;
201 motion.dy = device->rel.dy;
205 /* Apply pointer acceleration. */
206 filter_dispatch(device->pointer.filter, &motion, device, time);
208 if (motion.dx == 0.0 && motion.dy == 0.0)
211 pointer_notify_motion(base, time, motion.dx, motion.dy);
213 case EVDEV_ABSOLUTE_MT_DOWN:
214 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
217 if (device->mt.slots[slot].seat_slot != -1) {
218 log_bug_kernel(libinput,
219 "%s: Driver sent multiple touch down for the "
220 "same slot", device->devnode);
224 seat_slot = ffs(~seat->slot_map) - 1;
225 device->mt.slots[slot].seat_slot = seat_slot;
230 seat->slot_map |= 1 << seat_slot;
231 x = device->mt.slots[slot].x;
232 y = device->mt.slots[slot].y;
233 transform_absolute(device, &x, &y);
235 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
237 case EVDEV_ABSOLUTE_MT_MOTION:
238 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
241 seat_slot = device->mt.slots[slot].seat_slot;
242 x = device->mt.slots[slot].x;
243 y = device->mt.slots[slot].y;
248 transform_absolute(device, &x, &y);
249 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
251 case EVDEV_ABSOLUTE_MT_UP:
252 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
255 seat_slot = device->mt.slots[slot].seat_slot;
256 device->mt.slots[slot].seat_slot = -1;
261 seat->slot_map &= ~(1 << seat_slot);
263 touch_notify_touch_up(base, time, slot, seat_slot);
265 case EVDEV_ABSOLUTE_TOUCH_DOWN:
266 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
269 if (device->abs.seat_slot != -1) {
270 log_bug_kernel(libinput,
271 "%s: Driver sent multiple touch down for the "
272 "same slot", device->devnode);
276 seat_slot = ffs(~seat->slot_map) - 1;
277 device->abs.seat_slot = seat_slot;
282 seat->slot_map |= 1 << seat_slot;
286 transform_absolute(device, &cx, &cy);
288 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
290 case EVDEV_ABSOLUTE_MOTION:
293 transform_absolute(device, &cx, &cy);
297 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
298 seat_slot = device->abs.seat_slot;
303 touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
304 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
305 pointer_notify_motion_absolute(base, time, x, y);
308 case EVDEV_ABSOLUTE_TOUCH_UP:
309 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
312 seat_slot = device->abs.seat_slot;
313 device->abs.seat_slot = -1;
318 seat->slot_map &= ~(1 << seat_slot);
320 touch_notify_touch_up(base, time, -1, seat_slot);
323 assert(0 && "Unknown pending event type");
327 device->pending_event = EVDEV_NONE;
330 static enum evdev_key_type
331 get_key_type(uint16_t code)
333 if (code == BTN_TOUCH)
334 return EVDEV_KEY_TYPE_NONE;
336 if (code >= KEY_ESC && code <= KEY_MICMUTE)
337 return EVDEV_KEY_TYPE_KEY;
338 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
339 return EVDEV_KEY_TYPE_BUTTON;
340 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
341 return EVDEV_KEY_TYPE_KEY;
342 if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
343 return EVDEV_KEY_TYPE_BUTTON;
344 return EVDEV_KEY_TYPE_NONE;
348 evdev_process_touch_button(struct evdev_device *device,
349 uint64_t time, int value)
351 if (device->pending_event != EVDEV_NONE &&
352 device->pending_event != EVDEV_ABSOLUTE_MOTION)
353 evdev_flush_pending_event(device, time);
355 device->pending_event = (value ?
356 EVDEV_ABSOLUTE_TOUCH_DOWN :
357 EVDEV_ABSOLUTE_TOUCH_UP);
361 evdev_process_key(struct evdev_device *device,
362 struct input_event *e, uint64_t time)
364 enum evdev_key_type type;
366 /* ignore kernel key repeat */
370 if (e->code == BTN_TOUCH) {
372 evdev_process_touch_button(device, time, e->value);
376 evdev_flush_pending_event(device, time);
378 type = get_key_type(e->code);
380 /* Ignore key release events from the kernel for keys that libinput
381 * never got a pressed event for. */
384 case EVDEV_KEY_TYPE_NONE:
386 case EVDEV_KEY_TYPE_KEY:
387 case EVDEV_KEY_TYPE_BUTTON:
388 if (!hw_is_key_down(device, e->code))
393 hw_set_key_down(device, e->code, e->value);
396 case EVDEV_KEY_TYPE_NONE:
398 case EVDEV_KEY_TYPE_KEY:
399 evdev_keyboard_notify_key(
403 e->value ? LIBINPUT_KEY_STATE_PRESSED :
404 LIBINPUT_KEY_STATE_RELEASED);
406 case EVDEV_KEY_TYPE_BUTTON:
407 evdev_pointer_notify_button(
411 e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
412 LIBINPUT_BUTTON_STATE_RELEASED);
418 evdev_process_touch(struct evdev_device *device,
419 struct input_event *e,
424 evdev_flush_pending_event(device, time);
425 device->mt.slot = e->value;
427 case ABS_MT_TRACKING_ID:
428 if (device->pending_event != EVDEV_NONE &&
429 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
430 evdev_flush_pending_event(device, time);
432 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
434 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
436 case ABS_MT_POSITION_X:
437 device->mt.slots[device->mt.slot].x = e->value;
438 if (device->pending_event == EVDEV_NONE)
439 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
441 case ABS_MT_POSITION_Y:
442 device->mt.slots[device->mt.slot].y = e->value;
443 if (device->pending_event == EVDEV_NONE)
444 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
450 evdev_process_absolute_motion(struct evdev_device *device,
451 struct input_event *e)
455 device->abs.x = e->value;
456 if (device->pending_event == EVDEV_NONE)
457 device->pending_event = EVDEV_ABSOLUTE_MOTION;
460 device->abs.y = e->value;
461 if (device->pending_event == EVDEV_NONE)
462 device->pending_event = EVDEV_ABSOLUTE_MOTION;
468 evdev_process_relative(struct evdev_device *device,
469 struct input_event *e, uint64_t time)
471 struct libinput_device *base = &device->base;
475 if (device->pending_event != EVDEV_RELATIVE_MOTION)
476 evdev_flush_pending_event(device, time);
477 device->rel.dx += e->value;
478 device->pending_event = EVDEV_RELATIVE_MOTION;
481 if (device->pending_event != EVDEV_RELATIVE_MOTION)
482 evdev_flush_pending_event(device, time);
483 device->rel.dy += e->value;
484 device->pending_event = EVDEV_RELATIVE_MOTION;
487 evdev_flush_pending_event(device, time);
491 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
492 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
495 evdev_flush_pending_event(device, time);
504 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
505 e->value * DEFAULT_AXIS_STEP_DISTANCE);
515 evdev_process_absolute(struct evdev_device *device,
516 struct input_event *e,
520 evdev_process_touch(device, e, time);
522 evdev_process_absolute_motion(device, e);
527 evdev_need_touch_frame(struct evdev_device *device)
529 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
532 switch (device->pending_event) {
534 case EVDEV_RELATIVE_MOTION:
536 case EVDEV_ABSOLUTE_MT_DOWN:
537 case EVDEV_ABSOLUTE_MT_MOTION:
538 case EVDEV_ABSOLUTE_MT_UP:
539 case EVDEV_ABSOLUTE_TOUCH_DOWN:
540 case EVDEV_ABSOLUTE_TOUCH_UP:
541 case EVDEV_ABSOLUTE_MOTION:
549 fallback_process(struct evdev_dispatch *dispatch,
550 struct evdev_device *device,
551 struct input_event *event,
556 switch (event->type) {
558 evdev_process_relative(device, event, time);
561 evdev_process_absolute(device, event, time);
564 evdev_process_key(device, event, time);
567 need_frame = evdev_need_touch_frame(device);
568 evdev_flush_pending_event(device, time);
570 touch_notify_frame(&device->base, time);
576 fallback_destroy(struct evdev_dispatch *dispatch)
582 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
584 struct evdev_device *device = (struct evdev_device*)libinput_device;
586 return device->abs.absinfo_x && device->abs.absinfo_y;
589 static enum libinput_config_status
590 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
591 const float matrix[6])
593 struct evdev_device *device = (struct evdev_device*)libinput_device;
595 evdev_device_calibrate(device, matrix);
597 return LIBINPUT_CONFIG_STATUS_SUCCESS;
601 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
604 struct evdev_device *device = (struct evdev_device*)libinput_device;
606 matrix_to_farray6(&device->abs.usermatrix, matrix);
608 return !matrix_is_identity(&device->abs.usermatrix);
612 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
615 struct evdev_device *device = (struct evdev_device*)libinput_device;
617 matrix_to_farray6(&device->abs.default_calibration, matrix);
619 return !matrix_is_identity(&device->abs.default_calibration);
622 struct evdev_dispatch_interface fallback_interface = {
628 evdev_sendevents_get_modes(struct libinput_device *device)
630 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED |
631 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
634 static enum libinput_config_status
635 evdev_sendevents_set_mode(struct libinput_device *device,
636 enum libinput_config_send_events_mode mode)
638 struct evdev_device *evdev = (struct evdev_device*)device;
639 struct evdev_dispatch *dispatch = evdev->dispatch;
641 if (mode == dispatch->sendevents.current_mode)
642 return LIBINPUT_CONFIG_STATUS_SUCCESS;
645 case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
646 evdev_device_resume(evdev);
648 case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
649 evdev_device_suspend(evdev);
652 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
655 dispatch->sendevents.current_mode = mode;
657 return LIBINPUT_CONFIG_STATUS_SUCCESS;
660 static enum libinput_config_send_events_mode
661 evdev_sendevents_get_mode(struct libinput_device *device)
663 struct evdev_device *evdev = (struct evdev_device*)device;
664 struct evdev_dispatch *dispatch = evdev->dispatch;
666 return dispatch->sendevents.current_mode;
669 static enum libinput_config_send_events_mode
670 evdev_sendevents_get_default_mode(struct libinput_device *device)
672 return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
675 static struct evdev_dispatch *
676 fallback_dispatch_create(struct libinput_device *device)
678 struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
679 if (dispatch == NULL)
682 dispatch->interface = &fallback_interface;
684 device->config.calibration = &dispatch->calibration;
686 dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
687 dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
688 dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
689 dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
691 device->config.sendevents = &dispatch->sendevents.config;
693 dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
694 dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
695 dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
696 dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
697 dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
703 evdev_process_event(struct evdev_device *device, struct input_event *e)
705 struct evdev_dispatch *dispatch = device->dispatch;
706 uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
708 dispatch->interface->process(dispatch, device, e, time);
712 evdev_device_dispatch_one(struct evdev_device *device,
713 struct input_event *ev)
715 if (!device->mtdev) {
716 evdev_process_event(device, ev);
718 mtdev_put_event(device->mtdev, ev);
719 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
720 while (!mtdev_empty(device->mtdev)) {
721 struct input_event e;
722 mtdev_get_event(device->mtdev, &e);
723 evdev_process_event(device, &e);
730 evdev_sync_device(struct evdev_device *device)
732 struct input_event ev;
736 rc = libevdev_next_event(device->evdev,
737 LIBEVDEV_READ_FLAG_SYNC, &ev);
740 evdev_device_dispatch_one(device, &ev);
741 } while (rc == LIBEVDEV_READ_STATUS_SYNC);
743 return rc == -EAGAIN ? 0 : rc;
747 evdev_device_dispatch(void *data)
749 struct evdev_device *device = data;
750 struct libinput *libinput = device->base.seat->libinput;
751 struct input_event ev;
754 /* If the compositor is repainting, this function is called only once
755 * per frame and we have to process all the events available on the
756 * fd, otherwise there will be input lag. */
758 rc = libevdev_next_event(device->evdev,
759 LIBEVDEV_READ_FLAG_NORMAL, &ev);
760 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
761 /* send one more sync event so we handle all
762 currently pending events before we sync up
763 to the current state */
764 ev.code = SYN_REPORT;
765 evdev_device_dispatch_one(device, &ev);
767 rc = evdev_sync_device(device);
769 rc = LIBEVDEV_READ_STATUS_SUCCESS;
770 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
771 evdev_device_dispatch_one(device, &ev);
773 } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
775 if (rc != -EAGAIN && rc != -EINTR) {
776 libinput_remove_source(libinput, device->source);
777 device->source = NULL;
782 configure_pointer_acceleration(struct evdev_device *device)
784 device->pointer.filter =
785 create_pointer_accelator_filter(
786 pointer_accel_profile_smooth_simple);
787 if (!device->pointer.filter)
794 evdev_need_mtdev(struct evdev_device *device)
796 struct libevdev *evdev = device->evdev;
798 return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
799 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
800 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
804 evdev_configure_device(struct evdev_device *device)
806 struct libinput *libinput = device->base.seat->libinput;
807 struct libevdev *evdev = device->evdev;
808 const struct input_absinfo *absinfo;
809 struct input_absinfo fixed;
810 int has_abs, has_rel, has_mt;
811 int has_button, has_keyboard, has_touch;
812 struct mt_slot *slots;
825 if (libevdev_has_event_type(evdev, EV_ABS)) {
827 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
828 if (absinfo->resolution == 0) {
830 fixed.resolution = 1;
831 libevdev_set_abs_info(evdev, ABS_X, &fixed);
832 device->abs.fake_resolution = 1;
834 device->abs.absinfo_x = absinfo;
837 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
838 if (absinfo->resolution == 0) {
840 fixed.resolution = 1;
841 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
842 device->abs.fake_resolution = 1;
844 device->abs.absinfo_y = absinfo;
847 /* We only handle the slotted Protocol B in weston.
848 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
849 require mtdev for conversion. */
850 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
851 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
852 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
853 if (absinfo->resolution == 0) {
855 fixed.resolution = 1;
856 libevdev_set_abs_info(evdev,
859 device->abs.fake_resolution = 1;
861 device->abs.absinfo_x = absinfo;
862 absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
863 if (absinfo->resolution == 0) {
865 fixed.resolution = 1;
866 libevdev_set_abs_info(evdev,
869 device->abs.fake_resolution = 1;
871 device->abs.absinfo_y = absinfo;
876 if (evdev_need_mtdev(device)) {
877 device->mtdev = mtdev_new_open(device->fd);
881 num_slots = device->mtdev->caps.slot.maximum;
882 if (device->mtdev->caps.slot.minimum < 0 ||
885 active_slot = device->mtdev->caps.slot.value;
887 num_slots = libevdev_get_num_slots(device->evdev);
888 active_slot = libevdev_get_current_slot(evdev);
891 slots = calloc(num_slots, sizeof(struct mt_slot));
895 for (slot = 0; slot < num_slots; ++slot) {
896 slots[slot].seat_slot = -1;
900 device->mt.slots = slots;
901 device->mt.slots_len = num_slots;
902 device->mt.slot = active_slot;
905 if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
906 libevdev_has_event_code(evdev, EV_REL, REL_Y))
909 if (libevdev_has_event_type(evdev, EV_KEY)) {
910 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
911 libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
912 !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
913 (has_abs || has_mt)) {
914 device->dispatch = evdev_mt_touchpad_create(device);
916 "input device '%s', %s is a touchpad\n",
917 device->devname, device->devnode);
918 return device->dispatch == NULL ? -1 : 0;
921 for (i = 0; i < KEY_MAX; i++) {
922 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
923 switch (get_key_type(i)) {
924 case EVDEV_KEY_TYPE_NONE:
926 case EVDEV_KEY_TYPE_KEY:
929 case EVDEV_KEY_TYPE_BUTTON:
936 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
939 if (libevdev_has_event_type(evdev, EV_LED))
942 if ((has_abs || has_rel) && has_button) {
943 if (configure_pointer_acceleration(device) == -1)
946 device->seat_caps |= EVDEV_DEVICE_POINTER;
949 "input device '%s', %s is a pointer caps =%s%s%s\n",
950 device->devname, device->devnode,
951 has_abs ? " absolute-motion" : "",
952 has_rel ? " relative-motion": "",
953 has_button ? " button" : "");
956 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
958 "input device '%s', %s is a keyboard\n",
959 device->devname, device->devnode);
961 if (has_touch && !has_button) {
962 device->seat_caps |= EVDEV_DEVICE_TOUCH;
964 "input device '%s', %s is a touch device\n",
965 device->devname, device->devnode);
971 struct evdev_device *
972 evdev_device_create(struct libinput_seat *seat,
976 struct libinput *libinput = seat->libinput;
977 struct evdev_device *device;
980 int unhandled_device = 0;
982 /* Use non-blocking mode so that we can loop on read on
983 * evdev_device_data() until all events on the fd are
984 * read. mtdev_get() also expects this. */
985 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
988 "opening input device '%s' failed (%s).\n",
989 devnode, strerror(-fd));
993 device = zalloc(sizeof *device);
997 libinput_device_init(&device->base, seat);
998 libinput_seat_ref(seat);
1000 rc = libevdev_new_from_fd(fd, &device->evdev);
1004 libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1006 device->seat_caps = 0;
1008 device->mtdev = NULL;
1009 device->devnode = strdup(devnode);
1010 device->sysname = strdup(sysname);
1013 device->abs.seat_slot = -1;
1014 device->dispatch = NULL;
1016 device->pending_event = EVDEV_NONE;
1017 device->devname = libevdev_get_name(device->evdev);
1019 matrix_init_identity(&device->abs.calibration);
1020 matrix_init_identity(&device->abs.usermatrix);
1021 matrix_init_identity(&device->abs.default_calibration);
1023 if (evdev_configure_device(device) == -1)
1026 if (device->seat_caps == 0) {
1027 unhandled_device = 1;
1031 /* If the dispatch was not set up use the fallback. */
1032 if (device->dispatch == NULL)
1033 device->dispatch = fallback_dispatch_create(&device->base);
1034 if (device->dispatch == NULL)
1038 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1039 if (!device->source)
1042 list_insert(seat->devices_list.prev, &device->base.link);
1043 notify_added_device(&device->base);
1049 close_restricted(libinput, fd);
1050 evdev_device_destroy(device);
1052 return unhandled_device ? EVDEV_UNHANDLED_DEVICE : NULL;
1056 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1058 memset(keys, 0, size);
1063 evdev_device_get_output(struct evdev_device *device)
1065 return device->output_name;
1069 evdev_device_get_sysname(struct evdev_device *device)
1071 return device->sysname;
1075 evdev_device_get_name(struct evdev_device *device)
1077 return device->devname;
1081 evdev_device_get_id_product(struct evdev_device *device)
1083 return libevdev_get_id_product(device->evdev);
1087 evdev_device_get_id_vendor(struct evdev_device *device)
1089 return libevdev_get_id_vendor(device->evdev);
1093 evdev_device_set_default_calibration(struct evdev_device *device,
1094 const float calibration[6])
1096 matrix_from_farray6(&device->abs.default_calibration, calibration);
1097 evdev_device_calibrate(device, calibration);
1101 evdev_device_calibrate(struct evdev_device *device,
1102 const float calibration[6])
1104 struct matrix scale,
1109 matrix_from_farray6(&transform, calibration);
1110 device->abs.apply_calibration = !matrix_is_identity(&transform);
1112 if (!device->abs.apply_calibration) {
1113 matrix_init_identity(&device->abs.calibration);
1117 sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1118 sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1120 /* The transformation matrix is in the form:
1124 * Where a, e are the scale components, a, b, d, e are the rotation
1125 * component (combined with scale) and c and f are the translation
1126 * component. The translation component in the input matrix must be
1127 * normalized to multiples of the device width and height,
1128 * respectively. e.g. c == 1 shifts one device-width to the right.
1130 * We pre-calculate a single matrix to apply to event coordinates:
1131 * M = Un-Normalize * Calibration * Normalize
1133 * Normalize: scales the device coordinates to [0,1]
1134 * Calibration: user-supplied matrix
1135 * Un-Normalize: scales back up to device coordinates
1136 * Matrix maths requires the normalize/un-normalize in reverse
1140 /* back up the user matrix so we can return it on request */
1141 matrix_from_farray6(&device->abs.usermatrix, calibration);
1144 matrix_init_translate(&translate,
1145 device->abs.absinfo_x->minimum,
1146 device->abs.absinfo_y->minimum);
1147 matrix_init_scale(&scale, sx, sy);
1148 matrix_mult(&scale, &translate, &scale);
1151 matrix_mult(&transform, &scale, &transform);
1154 matrix_init_translate(&translate,
1155 -device->abs.absinfo_x->minimum/sx,
1156 -device->abs.absinfo_y->minimum/sy);
1157 matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1158 matrix_mult(&scale, &translate, &scale);
1160 /* store final matrix in device */
1161 matrix_mult(&device->abs.calibration, &transform, &scale);
1165 evdev_device_has_capability(struct evdev_device *device,
1166 enum libinput_device_capability capability)
1168 switch (capability) {
1169 case LIBINPUT_DEVICE_CAP_POINTER:
1170 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1171 case LIBINPUT_DEVICE_CAP_KEYBOARD:
1172 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1173 case LIBINPUT_DEVICE_CAP_TOUCH:
1174 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1181 evdev_device_get_size(struct evdev_device *device,
1185 const struct input_absinfo *x, *y;
1187 x = libevdev_get_abs_info(device->evdev, ABS_X);
1188 y = libevdev_get_abs_info(device->evdev, ABS_Y);
1190 if (!x || !y || device->abs.fake_resolution ||
1191 !x->resolution || !y->resolution)
1194 *width = evdev_convert_to_mm(x, x->maximum);
1195 *height = evdev_convert_to_mm(y, y->maximum);
1201 release_pressed_keys(struct evdev_device *device)
1203 struct libinput *libinput = device->base.seat->libinput;
1207 if ((time = libinput_now(libinput)) == 0)
1210 for (code = 0; code < KEY_CNT; code++) {
1211 if (get_key_down_count(device, code) > 0) {
1212 switch (get_key_type(code)) {
1213 case EVDEV_KEY_TYPE_NONE:
1215 case EVDEV_KEY_TYPE_KEY:
1216 keyboard_notify_key(
1220 LIBINPUT_KEY_STATE_RELEASED);
1222 case EVDEV_KEY_TYPE_BUTTON:
1223 pointer_notify_button(
1227 LIBINPUT_BUTTON_STATE_RELEASED);
1235 evdev_device_suspend(struct evdev_device *device)
1237 if (device->source) {
1238 libinput_remove_source(device->base.seat->libinput,
1240 device->source = NULL;
1243 release_pressed_keys(device);
1245 if (device->mtdev) {
1246 mtdev_close_delete(device->mtdev);
1247 device->mtdev = NULL;
1250 if (device->fd != -1) {
1251 close_restricted(device->base.seat->libinput, device->fd);
1259 evdev_device_resume(struct evdev_device *device)
1261 struct libinput *libinput = device->base.seat->libinput;
1264 if (device->fd != -1)
1267 fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1274 if (evdev_need_mtdev(device)) {
1275 device->mtdev = mtdev_new_open(device->fd);
1281 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1282 if (!device->source) {
1283 mtdev_close_delete(device->mtdev);
1287 memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1293 evdev_device_remove(struct evdev_device *device)
1295 evdev_device_suspend(device);
1297 list_remove(&device->base.link);
1299 notify_removed_device(&device->base);
1300 libinput_device_unref(&device->base);
1304 evdev_device_destroy(struct evdev_device *device)
1306 struct evdev_dispatch *dispatch;
1308 dispatch = device->dispatch;
1310 dispatch->interface->destroy(dispatch);
1312 filter_destroy(device->pointer.filter);
1313 libinput_seat_unref(device->base.seat);
1314 libevdev_free(device->evdev);
1315 free(device->mt.slots);
1316 free(device->devnode);
1317 free(device->sysname);