diff --git a/sys/dev/evdev/evdev.h b/sys/dev/evdev/evdev.h index fe21f8cea4c2..64bf75f04efd 100644 --- a/sys/dev/evdev/evdev.h +++ b/sys/dev/evdev/evdev.h @@ -1,218 +1,242 @@ /*- * Copyright (c) 2014 Jakub Wojciech Klama * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _DEV_EVDEV_EVDEV_H #define _DEV_EVDEV_EVDEV_H #include #include #include #include #include #define NAMELEN 80 struct evdev_dev; typedef int (evdev_open_t)(struct evdev_dev *); typedef int (evdev_close_t)(struct evdev_dev *); typedef void (evdev_event_t)(struct evdev_dev *, uint16_t, uint16_t, int32_t); typedef void (evdev_keycode_t)(struct evdev_dev *, struct input_keymap_entry *); /* * Keyboard and mouse events recipient mask. * evdev_rcpt_mask variable should be respected by keyboard and mouse drivers * that are able to send events through both evdev and sysmouse/kbdmux * interfaces so user can choose prefered one to not receive one event twice. */ #define EVDEV_RCPT_SYSMOUSE (1<<0) #define EVDEV_RCPT_KBDMUX (1<<1) #define EVDEV_RCPT_HW_MOUSE (1<<2) #define EVDEV_RCPT_HW_KBD (1<<3) extern int evdev_rcpt_mask; /* * Sysmouse protocol does not support horizontal wheel movement reporting. * To overcome this limitation different drivers use different sysmouse proto * extensions. Set kern.evdev.sysmouse_t_axis to tell sysmouse evdev driver * which protocol extension is used. * 0 - do not extract horizontal wheel movement (default). * 1 - ums(4) horizontal wheel encoding. T-axis is mapped to buttons 6 and 7 * 2 - psm(4) wheels encoding: z = 1,-1 - vert. wheel, z = 2,-2 - horiz. wheel */ enum { EVDEV_SYSMOUSE_T_AXIS_NONE = 0, EVDEV_SYSMOUSE_T_AXIS_UMS = 1, EVDEV_SYSMOUSE_T_AXIS_PSM = 2, }; extern int evdev_sysmouse_t_axis; #define ABS_MT_FIRST ABS_MT_TOUCH_MAJOR #define ABS_MT_LAST ABS_MT_TOOL_Y #define ABS_IS_MT(x) ((x) >= ABS_MT_FIRST && (x) <= ABS_MT_LAST) #define ABS_MT_INDEX(x) ((x) - ABS_MT_FIRST) #define MT_CNT (ABS_MT_INDEX(ABS_MT_LAST) + 1) /* Multitouch protocol type A */ #define MAX_MT_REPORTS 5 /* Multitouch protocol type B interface */ #define MAX_MT_SLOTS 16 #define EVDEV_FLAG_SOFTREPEAT 0x00 /* use evdev to repeat keys */ #define EVDEV_FLAG_MT_STCOMPAT 0x01 /* autogenerate ST-compatible events * for MT protocol type B reports */ #define EVDEV_FLAG_MT_AUTOREL 0x02 /* Autorelease MT-slots not listed in * current MT protocol type B report */ #define EVDEV_FLAG_EXT_EPOCH 0x03 /* evdev_push_* is allways called with * input (global) epoch entered */ #define EVDEV_FLAG_MAX 0x1F #define EVDEV_FLAG_CNT (EVDEV_FLAG_MAX + 1) struct evdev_methods { evdev_open_t *ev_open; evdev_close_t *ev_close; evdev_event_t *ev_event; evdev_keycode_t *ev_get_keycode; evdev_keycode_t *ev_set_keycode; }; +union evdev_mt_slot { + int32_t val[MT_CNT]; + struct { + int32_t maj; /* ABS_MT_TOUCH_MAJOR */ + int32_t min; /* ABS_MT_TOUCH_MINOR */ + int32_t w_maj; /* ABS_MT_WIDTH_MAJOR */ + int32_t w_min; /* ABS_MT_WIDTH_MINOR */ + int32_t ori; /* ABS_MT_ORIENTATION */ + int32_t x; /* ABS_MT_POSITION_X */ + int32_t y; /* ABS_MT_POSITION_Y */ + int32_t type; /* ABS_MT_TOOL_TYPE */ + int32_t blob_id; /* ABS_MT_BLOB_ID */ + int32_t id; /* ABS_MT_TRACKING_ID */ + int32_t p; /* ABS_MT_PRESSURE */ + int32_t dist; /* ABS_MT_DISTANCE */ + int32_t tool_x; /* ABS_MT_TOOL_X */ + int32_t tool_y; /* ABS_MT_TOOL_Y */ + }; +}; +_Static_assert(offsetof(union evdev_mt_slot, tool_y) == + offsetof(union evdev_mt_slot, val[ABS_MT_INDEX(ABS_MT_TOOL_Y)]), + "evdev_mt_slot array members does not match their structure aliases"); + /* Input device interface: */ struct evdev_dev *evdev_alloc(void); void evdev_free(struct evdev_dev *); void evdev_set_name(struct evdev_dev *, const char *); void evdev_set_id(struct evdev_dev *, uint16_t, uint16_t, uint16_t, uint16_t); void evdev_set_phys(struct evdev_dev *, const char *); void evdev_set_serial(struct evdev_dev *, const char *); void evdev_set_methods(struct evdev_dev *, void *, const struct evdev_methods *); int evdev_register(struct evdev_dev *); int evdev_register_mtx(struct evdev_dev *, struct mtx *); int evdev_unregister(struct evdev_dev *); int evdev_push_event(struct evdev_dev *, uint16_t, uint16_t, int32_t); void evdev_support_prop(struct evdev_dev *, uint16_t); void evdev_support_event(struct evdev_dev *, uint16_t); void evdev_support_key(struct evdev_dev *, uint16_t); void evdev_support_rel(struct evdev_dev *, uint16_t); void evdev_support_abs(struct evdev_dev *, uint16_t, int32_t, int32_t, int32_t, int32_t, int32_t); void evdev_support_msc(struct evdev_dev *, uint16_t); void evdev_support_led(struct evdev_dev *, uint16_t); void evdev_support_snd(struct evdev_dev *, uint16_t); void evdev_support_sw(struct evdev_dev *, uint16_t); void evdev_set_repeat_params(struct evdev_dev *, uint16_t, int); int evdev_set_report_size(struct evdev_dev *, size_t); void evdev_set_flag(struct evdev_dev *, uint16_t); void *evdev_get_softc(struct evdev_dev *); /* Multitouch related functions: */ int evdev_get_mt_slot_by_tracking_id(struct evdev_dev *, int32_t); void evdev_support_mt_compat(struct evdev_dev *); void evdev_push_mt_compat(struct evdev_dev *); +int evdev_mt_push_slot(struct evdev_dev *, int, union evdev_mt_slot *); void evdev_mt_push_autorel(struct evdev_dev *); static inline int evdev_mt_id_to_slot(struct evdev_dev *evdev, int32_t id) { return (evdev_get_mt_slot_by_tracking_id(evdev, id)); } /* Utility functions: */ uint16_t evdev_hid2key(int); void evdev_support_all_known_keys(struct evdev_dev *); uint16_t evdev_scancode2key(int *, int); void evdev_push_mouse_btn(struct evdev_dev *, int); void evdev_push_leds(struct evdev_dev *, int); void evdev_push_repeats(struct evdev_dev *, keyboard_t *); void evdev_support_nfingers(struct evdev_dev *, int); void evdev_push_nfingers(struct evdev_dev *, int); /* Event reporting shortcuts: */ static __inline int evdev_sync(struct evdev_dev *evdev) { return (evdev_push_event(evdev, EV_SYN, SYN_REPORT, 1)); } static __inline int evdev_mt_sync(struct evdev_dev *evdev) { return (evdev_push_event(evdev, EV_SYN, SYN_MT_REPORT, 1)); } static __inline int evdev_push_key(struct evdev_dev *evdev, uint16_t code, int32_t value) { return (evdev_push_event(evdev, EV_KEY, code, value != 0)); } static __inline int evdev_push_rel(struct evdev_dev *evdev, uint16_t code, int32_t value) { return (evdev_push_event(evdev, EV_REL, code, value)); } static __inline int evdev_push_abs(struct evdev_dev *evdev, uint16_t code, int32_t value) { return (evdev_push_event(evdev, EV_ABS, code, value)); } static __inline int evdev_push_msc(struct evdev_dev *evdev, uint16_t code, int32_t value) { return (evdev_push_event(evdev, EV_MSC, code, value)); } static __inline int evdev_push_led(struct evdev_dev *evdev, uint16_t code, int32_t value) { return (evdev_push_event(evdev, EV_LED, code, value != 0)); } static __inline int evdev_push_snd(struct evdev_dev *evdev, uint16_t code, int32_t value) { return (evdev_push_event(evdev, EV_SND, code, value)); } static __inline int evdev_push_sw(struct evdev_dev *evdev, uint16_t code, int32_t value) { return (evdev_push_event(evdev, EV_SW, code, value != 0)); } #endif /* _DEV_EVDEV_EVDEV_H */ diff --git a/sys/dev/evdev/evdev_mt.c b/sys/dev/evdev/evdev_mt.c index a3600e837960..6f5cce4a008d 100644 --- a/sys/dev/evdev/evdev_mt.c +++ b/sys/dev/evdev/evdev_mt.c @@ -1,293 +1,328 @@ /*- * Copyright (c) 2016 Vladimir Kondratyev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #ifdef DEBUG #define debugf(fmt, args...) printf("evdev: " fmt "\n", ##args) #else #define debugf(fmt, args...) #endif typedef u_int slotset_t; _Static_assert(MAX_MT_SLOTS < sizeof(slotset_t) * 8, "MAX_MT_SLOTS too big"); #define FOREACHBIT(v, i) \ for ((i) = ffs(v) - 1; (i) != -1; (i) = ffs((v) & (~1 << (i))) - 1) struct { uint16_t mt; uint16_t st; int32_t max; } static evdev_mtstmap[] = { { ABS_MT_POSITION_X, ABS_X, 0 }, { ABS_MT_POSITION_Y, ABS_Y, 0 }, { ABS_MT_PRESSURE, ABS_PRESSURE, 255 }, { ABS_MT_TOUCH_MAJOR, ABS_TOOL_WIDTH, 15 }, }; -struct evdev_mt_slot { - int32_t val[MT_CNT]; -}; - struct evdev_mt { int last_reported_slot; u_int mtst_events; /* the set of slots with active touches */ slotset_t touches; /* the set of slots with unsynchronized state */ slotset_t frame; - struct evdev_mt_slot slots[]; + union evdev_mt_slot slots[]; }; static void evdev_mt_send_st_compat(struct evdev_dev *); static void evdev_mt_send_autorel(struct evdev_dev *); static inline int ffc_slot(struct evdev_dev *evdev, slotset_t slots) { return (ffs(~slots & (2U << MAXIMAL_MT_SLOT(evdev)) - 1) - 1); } void evdev_mt_init(struct evdev_dev *evdev) { int slot, slots; slots = MAXIMAL_MT_SLOT(evdev) + 1; evdev->ev_mt = malloc(offsetof(struct evdev_mt, slots) + - sizeof(struct evdev_mt_slot) * slots, M_EVDEV, M_WAITOK | M_ZERO); + sizeof(union evdev_mt_slot) * slots, M_EVDEV, M_WAITOK | M_ZERO); /* Initialize multitouch protocol type B states */ for (slot = 0; slot < slots; slot++) - evdev->ev_mt->slots[slot].val[ABS_MT_INDEX(ABS_MT_TRACKING_ID)] - = -1; + evdev->ev_mt->slots[slot].id = -1; if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) evdev_support_mt_compat(evdev); } void evdev_mt_free(struct evdev_dev *evdev) { free(evdev->ev_mt, M_EVDEV); } void evdev_mt_sync_frame(struct evdev_dev *evdev) { if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL)) evdev_mt_send_autorel(evdev); if (evdev->ev_report_opened && bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) evdev_mt_send_st_compat(evdev); evdev->ev_mt->frame = 0; } +static void +evdev_mt_send_slot(struct evdev_dev *evdev, int slot, + union evdev_mt_slot *state) +{ + int i; + bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); + + EVDEV_LOCK_ASSERT(evdev); + MPASS(type_a || (slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev))); + MPASS(!type_a || state != NULL); + + if (!type_a) { + evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot); + if (state == NULL) { + evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1); + return; + } + } + bit_foreach_at(evdev->ev_abs_flags, ABS_MT_FIRST, ABS_MT_LAST + 1, i) + evdev_send_event(evdev, EV_ABS, i, + state->val[ABS_MT_INDEX(i)]); + if (type_a) + evdev_send_event(evdev, EV_SYN, SYN_MT_REPORT, 1); +} + +int +evdev_mt_push_slot(struct evdev_dev *evdev, int slot, + union evdev_mt_slot *state) +{ + bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); + + if (type_a && state == NULL) + return (EINVAL); + if (!type_a && (slot < 0 || slot > MAXIMAL_MT_SLOT(evdev))) + return (EINVAL); + + EVDEV_ENTER(evdev); + evdev_mt_send_slot(evdev, slot, state); + EVDEV_EXIT(evdev); + + return (0); +} + int evdev_mt_get_last_slot(struct evdev_dev *evdev) { return (evdev->ev_mt->last_reported_slot); } void evdev_mt_set_last_slot(struct evdev_dev *evdev, int slot) { struct evdev_mt *mt = evdev->ev_mt; MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); mt->frame |= 1U << slot; mt->last_reported_slot = slot; } int32_t evdev_mt_get_value(struct evdev_dev *evdev, int slot, int16_t code) { struct evdev_mt *mt = evdev->ev_mt; MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); return (mt->slots[slot].val[ABS_MT_INDEX(code)]); } void evdev_mt_set_value(struct evdev_dev *evdev, int slot, int16_t code, int32_t value) { struct evdev_mt *mt = evdev->ev_mt; MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); if (code == ABS_MT_TRACKING_ID) { if (value != -1) mt->touches |= 1U << slot; else mt->touches &= ~(1U << slot); } mt->slots[slot].val[ABS_MT_INDEX(code)] = value; } int evdev_get_mt_slot_by_tracking_id(struct evdev_dev *evdev, int32_t tracking_id) { struct evdev_mt *mt = evdev->ev_mt; int slot; FOREACHBIT(mt->touches, slot) - if (evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID) == - tracking_id) + if (mt->slots[slot].id == tracking_id) return (slot); /* * Do not allow allocation of new slot in a place of just * released one within the same report. */ return (ffc_slot(evdev, mt->touches | mt->frame)); } static inline int32_t evdev_mt_normalize(int32_t value, int32_t mtmin, int32_t mtmax, int32_t stmax) { if (stmax != 0 && mtmax != mtmin) { value = (value - mtmin) * stmax / (mtmax - mtmin); value = MAX(MIN(value, stmax), 0); } return (value); } void evdev_support_mt_compat(struct evdev_dev *evdev) { struct input_absinfo *ai; int i; if (evdev->ev_absinfo == NULL) return; evdev_support_event(evdev, EV_KEY); evdev_support_key(evdev, BTN_TOUCH); /* Touchscreens should not advertise tap tool capabilities */ if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) evdev_support_nfingers(evdev, MAXIMAL_MT_SLOT(evdev) + 1); /* Echo 0-th MT-slot as ST-slot */ for (i = 0; i < nitems(evdev_mtstmap); i++) { if (!bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].mt) || bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].st)) continue; ai = evdev->ev_absinfo + evdev_mtstmap[i].mt; evdev->ev_mt->mtst_events |= 1U << i; if (evdev_mtstmap[i].max != 0) evdev_support_abs(evdev, evdev_mtstmap[i].st, 0, evdev_mtstmap[i].max, 0, evdev_mt_normalize( ai->flat, 0, ai->maximum, evdev_mtstmap[i].max), 0); else evdev_support_abs(evdev, evdev_mtstmap[i].st, ai->minimum, ai->maximum, 0, ai->flat, ai->resolution); } } static void evdev_mt_send_st_compat(struct evdev_dev *evdev) { struct evdev_mt *mt = evdev->ev_mt; int nfingers, i, st_slot; EVDEV_LOCK_ASSERT(evdev); nfingers = bitcount(mt->touches); evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0); /* Send first active MT-slot state as single touch report */ st_slot = ffs(mt->touches) - 1; if (st_slot != -1) FOREACHBIT(mt->mtst_events, i) evdev_send_event(evdev, EV_ABS, evdev_mtstmap[i].st, evdev_mt_normalize(evdev_mt_get_value(evdev, st_slot, evdev_mtstmap[i].mt), evdev->ev_absinfo[evdev_mtstmap[i].mt].minimum, evdev->ev_absinfo[evdev_mtstmap[i].mt].maximum, evdev_mtstmap[i].max)); /* Touchscreens should not report tool taps */ if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) evdev_send_nfingers(evdev, nfingers); if (nfingers == 0) evdev_send_event(evdev, EV_ABS, ABS_PRESSURE, 0); } void evdev_push_mt_compat(struct evdev_dev *evdev) { EVDEV_ENTER(evdev); evdev_mt_send_st_compat(evdev); EVDEV_EXIT(evdev); } static void evdev_mt_send_autorel(struct evdev_dev *evdev) { struct evdev_mt *mt = evdev->ev_mt; int slot; EVDEV_LOCK_ASSERT(evdev); - FOREACHBIT(mt->touches & ~mt->frame, slot) { - evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot); - evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1); - } + FOREACHBIT(mt->touches & ~mt->frame, slot) + evdev_mt_send_slot(evdev, slot, NULL); } void evdev_mt_push_autorel(struct evdev_dev *evdev) { EVDEV_ENTER(evdev); evdev_mt_send_autorel(evdev); EVDEV_EXIT(evdev); }