xref: /freebsd-13.1/sys/dev/evdev/evdev_mt.c (revision 759a5cd1)
1 /*-
2  * Copyright (c) 2016 Vladimir Kondratyev <[email protected]>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/lock.h>
31 #include <sys/malloc.h>
32 #include <sys/mutex.h>
33 #include <sys/systm.h>
34 
35 #include <dev/evdev/evdev.h>
36 #include <dev/evdev/evdev_private.h>
37 #include <dev/evdev/input.h>
38 
39 #ifdef DEBUG
40 #define	debugf(fmt, args...)	printf("evdev: " fmt "\n", ##args)
41 #else
42 #define	debugf(fmt, args...)
43 #endif
44 
45 typedef	u_int	slotset_t;
46 
47 _Static_assert(MAX_MT_SLOTS < sizeof(slotset_t) * 8, "MAX_MT_SLOTS too big");
48 
49 #define FOREACHBIT(v, i) \
50 	for ((i) = ffs(v) - 1; (i) != -1; (i) = ffs((v) & (~1 << (i))) - 1)
51 
52 struct {
53 	uint16_t	mt;
54 	uint16_t	st;
55 	int32_t		max;
56 } static evdev_mtstmap[] = {
57 	{ ABS_MT_POSITION_X,	ABS_X,		0 },
58 	{ ABS_MT_POSITION_Y,	ABS_Y,		0 },
59 	{ ABS_MT_PRESSURE,	ABS_PRESSURE,	255 },
60 	{ ABS_MT_TOUCH_MAJOR,	ABS_TOOL_WIDTH,	15 },
61 };
62 
63 struct evdev_mt {
64 	int			last_reported_slot;
65 	u_int			mtst_events;
66 	/* the set of slots with active touches */
67 	slotset_t		touches;
68 	/* the set of slots with unsynchronized state */
69 	slotset_t		frame;
70 	union evdev_mt_slot	slots[];
71 };
72 
73 static void	evdev_mt_send_st_compat(struct evdev_dev *);
74 static void	evdev_mt_send_autorel(struct evdev_dev *);
75 
76 static inline int
77 ffc_slot(struct evdev_dev *evdev, slotset_t slots)
78 {
79 	return (ffs(~slots & (2U << MAXIMAL_MT_SLOT(evdev)) - 1) - 1);
80 }
81 
82 void
83 evdev_mt_init(struct evdev_dev *evdev)
84 {
85 	int slot, slots;
86 
87 	slots = MAXIMAL_MT_SLOT(evdev) + 1;
88 
89 	evdev->ev_mt = malloc(offsetof(struct evdev_mt, slots) +
90 	     sizeof(union evdev_mt_slot) * slots, M_EVDEV, M_WAITOK | M_ZERO);
91 
92 	/* Initialize multitouch protocol type B states */
93 	for (slot = 0; slot < slots; slot++)
94 		evdev->ev_mt->slots[slot].id = -1;
95 
96 	if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
97 		evdev_support_mt_compat(evdev);
98 }
99 
100 void
101 evdev_mt_free(struct evdev_dev *evdev)
102 {
103 	free(evdev->ev_mt, M_EVDEV);
104 }
105 
106 void
107 evdev_mt_sync_frame(struct evdev_dev *evdev)
108 {
109 	if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
110 		evdev_mt_send_autorel(evdev);
111 	if (evdev->ev_report_opened &&
112 	    bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
113 		evdev_mt_send_st_compat(evdev);
114 	evdev->ev_mt->frame = 0;
115 }
116 
117 static void
118 evdev_mt_send_slot(struct evdev_dev *evdev, int slot,
119     union evdev_mt_slot *state)
120 {
121 	int i;
122 	bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT);
123 
124 	EVDEV_LOCK_ASSERT(evdev);
125 	MPASS(type_a || (slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)));
126 	MPASS(!type_a || state != NULL);
127 
128 	if (!type_a) {
129 		evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot);
130 		if (state == NULL) {
131 			evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1);
132 			return;
133 		}
134 	}
135 	bit_foreach_at(evdev->ev_abs_flags, ABS_MT_FIRST, ABS_MT_LAST + 1, i)
136 		evdev_send_event(evdev, EV_ABS, i,
137 		    state->val[ABS_MT_INDEX(i)]);
138 	if (type_a)
139 		evdev_send_event(evdev, EV_SYN, SYN_MT_REPORT, 1);
140 }
141 
142 int
143 evdev_mt_push_slot(struct evdev_dev *evdev, int slot,
144     union evdev_mt_slot *state)
145 {
146 	bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT);
147 
148 	if (type_a && state == NULL)
149 		return (EINVAL);
150 	if (!type_a && (slot < 0 || slot > MAXIMAL_MT_SLOT(evdev)))
151 		return (EINVAL);
152 
153 	EVDEV_ENTER(evdev);
154 	evdev_mt_send_slot(evdev, slot, state);
155 	EVDEV_EXIT(evdev);
156 
157 	return (0);
158 }
159 
160 int
161 evdev_mt_get_last_slot(struct evdev_dev *evdev)
162 {
163 	return (evdev->ev_mt->last_reported_slot);
164 }
165 
166 void
167 evdev_mt_set_last_slot(struct evdev_dev *evdev, int slot)
168 {
169 	struct evdev_mt *mt = evdev->ev_mt;
170 
171 	MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev));
172 
173 	mt->frame |= 1U << slot;
174 	mt->last_reported_slot = slot;
175 }
176 
177 int32_t
178 evdev_mt_get_value(struct evdev_dev *evdev, int slot, int16_t code)
179 {
180 	struct evdev_mt *mt = evdev->ev_mt;
181 
182 	MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev));
183 
184 	return (mt->slots[slot].val[ABS_MT_INDEX(code)]);
185 }
186 
187 void
188 evdev_mt_set_value(struct evdev_dev *evdev, int slot, int16_t code,
189     int32_t value)
190 {
191 	struct evdev_mt *mt = evdev->ev_mt;
192 
193 	MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev));
194 
195 	if (code == ABS_MT_TRACKING_ID) {
196 		if (value != -1)
197 			mt->touches |= 1U << slot;
198 		else
199 			mt->touches &= ~(1U << slot);
200 	}
201 	mt->slots[slot].val[ABS_MT_INDEX(code)] = value;
202 }
203 
204 int
205 evdev_get_mt_slot_by_tracking_id(struct evdev_dev *evdev, int32_t tracking_id)
206 {
207 	struct evdev_mt *mt = evdev->ev_mt;
208 	int slot;
209 
210 	FOREACHBIT(mt->touches, slot)
211 		if (mt->slots[slot].id == tracking_id)
212 			return (slot);
213 	/*
214 	 * Do not allow allocation of new slot in a place of just
215 	 * released one within the same report.
216 	 */
217 	return (ffc_slot(evdev, mt->touches | mt->frame));
218 }
219 
220 static inline int32_t
221 evdev_mt_normalize(int32_t value, int32_t mtmin, int32_t mtmax, int32_t stmax)
222 {
223 	if (stmax != 0 && mtmax != mtmin) {
224 		value = (value - mtmin) * stmax / (mtmax - mtmin);
225 		value = MAX(MIN(value, stmax), 0);
226 	}
227 	return (value);
228 }
229 
230 void
231 evdev_support_mt_compat(struct evdev_dev *evdev)
232 {
233 	struct input_absinfo *ai;
234 	int i;
235 
236 	if (evdev->ev_absinfo == NULL)
237 		return;
238 
239 	evdev_support_event(evdev, EV_KEY);
240 	evdev_support_key(evdev, BTN_TOUCH);
241 
242 	/* Touchscreens should not advertise tap tool capabilities */
243 	if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT))
244 		evdev_support_nfingers(evdev, MAXIMAL_MT_SLOT(evdev) + 1);
245 
246 	/* Echo 0-th MT-slot as ST-slot */
247 	for (i = 0; i < nitems(evdev_mtstmap); i++) {
248 		if (!bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].mt) ||
249 		     bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].st))
250 			continue;
251 		ai = evdev->ev_absinfo + evdev_mtstmap[i].mt;
252 		evdev->ev_mt->mtst_events |= 1U << i;
253 		if (evdev_mtstmap[i].max != 0)
254 			evdev_support_abs(evdev, evdev_mtstmap[i].st,
255 			    0,
256 			    evdev_mtstmap[i].max,
257 			    0,
258 			    evdev_mt_normalize(
259 			      ai->flat, 0, ai->maximum, evdev_mtstmap[i].max),
260 			    0);
261 		else
262 			evdev_support_abs(evdev, evdev_mtstmap[i].st,
263 			    ai->minimum,
264 			    ai->maximum,
265 			    0,
266 			    ai->flat,
267 			    ai->resolution);
268 	}
269 }
270 
271 static void
272 evdev_mt_send_st_compat(struct evdev_dev *evdev)
273 {
274 	struct evdev_mt *mt = evdev->ev_mt;
275 	int nfingers, i, st_slot;
276 
277 	EVDEV_LOCK_ASSERT(evdev);
278 
279 	nfingers = bitcount(mt->touches);
280 	evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0);
281 
282 	/* Send first active MT-slot state as single touch report */
283 	st_slot = ffs(mt->touches) - 1;
284 	if (st_slot != -1)
285 		FOREACHBIT(mt->mtst_events, i)
286 			evdev_send_event(evdev, EV_ABS, evdev_mtstmap[i].st,
287 			    evdev_mt_normalize(evdev_mt_get_value(evdev,
288 			      st_slot, evdev_mtstmap[i].mt),
289 			      evdev->ev_absinfo[evdev_mtstmap[i].mt].minimum,
290 			      evdev->ev_absinfo[evdev_mtstmap[i].mt].maximum,
291 			      evdev_mtstmap[i].max));
292 
293 	/* Touchscreens should not report tool taps */
294 	if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT))
295 		evdev_send_nfingers(evdev, nfingers);
296 
297 	if (nfingers == 0)
298 		evdev_send_event(evdev, EV_ABS, ABS_PRESSURE, 0);
299 }
300 
301 void
302 evdev_push_mt_compat(struct evdev_dev *evdev)
303 {
304 
305 	EVDEV_ENTER(evdev);
306 	evdev_mt_send_st_compat(evdev);
307 	EVDEV_EXIT(evdev);
308 }
309 
310 static void
311 evdev_mt_send_autorel(struct evdev_dev *evdev)
312 {
313 	struct evdev_mt *mt = evdev->ev_mt;
314 	int slot;
315 
316 	EVDEV_LOCK_ASSERT(evdev);
317 
318 	FOREACHBIT(mt->touches & ~mt->frame, slot)
319 		evdev_mt_send_slot(evdev, slot, NULL);
320 }
321 
322 void
323 evdev_mt_push_autorel(struct evdev_dev *evdev)
324 {
325 	EVDEV_ENTER(evdev);
326 	evdev_mt_send_autorel(evdev);
327 	EVDEV_EXIT(evdev);
328 }
329