summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/evdev/evdev_mt.c
blob: d0eb48f4aafe40aa385e9d4b39bf4f7e80d8627c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * Copyright (c) 2016 Vladimir Kondratyev <wulf@FreeBSD.org>
 * 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 <sys/param.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/systm.h>

#include <dev/evdev/evdev.h>
#include <dev/evdev/evdev_private.h>
#include <dev/evdev/input.h>

#ifdef DEBUG
#define	debugf(fmt, args...)	printf("evdev: " fmt "\n", ##args)
#else
#define	debugf(fmt, args...)
#endif

static uint16_t evdev_fngmap[] = {
	BTN_TOOL_FINGER,
	BTN_TOOL_DOUBLETAP,
	BTN_TOOL_TRIPLETAP,
	BTN_TOOL_QUADTAP,
	BTN_TOOL_QUINTTAP,
};

static uint16_t evdev_mtstmap[][2] = {
	{ ABS_MT_POSITION_X, ABS_X },
	{ ABS_MT_POSITION_Y, ABS_Y },
	{ ABS_MT_PRESSURE, ABS_PRESSURE },
	{ ABS_MT_TOUCH_MAJOR, ABS_TOOL_WIDTH },
};

struct evdev_mt_slot {
	uint64_t ev_report;
	int32_t ev_mt_states[MT_CNT];
};

struct evdev_mt {
	int32_t	ev_mt_last_reported_slot;
	struct evdev_mt_slot ev_mt_slots[];
};

void
evdev_mt_init(struct evdev_dev *evdev)
{
	int32_t slot, slots;

	slots = MAXIMAL_MT_SLOT(evdev) + 1;

	evdev->ev_mt = malloc(offsetof(struct evdev_mt, ev_mt_slots) +
	     sizeof(struct evdev_mt_slot) * slots, M_EVDEV, M_WAITOK | M_ZERO);

	/* Initialize multitouch protocol type B states */
	for (slot = 0; slot < slots; slot++) {
		/*
		 * .ev_report should not be initialized to initial value of
		 * report counter (0) as it brokes free slot detection in
		 * evdev_get_mt_slot_by_tracking_id. So initialize it to -1
		 */
		evdev->ev_mt->ev_mt_slots[slot] = (struct evdev_mt_slot) {
			.ev_report = 0xFFFFFFFFFFFFFFFFULL,
			.ev_mt_states[ABS_MT_INDEX(ABS_MT_TRACKING_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);
}

int32_t
evdev_get_last_mt_slot(struct evdev_dev *evdev)
{

	return (evdev->ev_mt->ev_mt_last_reported_slot);
}

void
evdev_set_last_mt_slot(struct evdev_dev *evdev, int32_t slot)
{

	evdev->ev_mt->ev_mt_slots[slot].ev_report = evdev->ev_report_count;
	evdev->ev_mt->ev_mt_last_reported_slot = slot;
}

inline int32_t
evdev_get_mt_value(struct evdev_dev *evdev, int32_t slot, int16_t code)
{

	return (evdev->ev_mt->
	    ev_mt_slots[slot].ev_mt_states[ABS_MT_INDEX(code)]);
}

inline void
evdev_set_mt_value(struct evdev_dev *evdev, int32_t slot, int16_t code,
    int32_t value)
{

	evdev->ev_mt->ev_mt_slots[slot].ev_mt_states[ABS_MT_INDEX(code)] =
	    value;
}

int32_t
evdev_get_mt_slot_by_tracking_id(struct evdev_dev *evdev, int32_t tracking_id)
{
	int32_t tr_id, slot, free_slot = -1;

	for (slot = 0; slot <= MAXIMAL_MT_SLOT(evdev); slot++) {
		tr_id = evdev_get_mt_value(evdev, slot, ABS_MT_TRACKING_ID);
		if (tr_id == tracking_id)
			return (slot);
		/*
		 * Its possible that slot will be reassigned in a place of just
		 * released one within the same report. To avoid this compare
		 * report counter with slot`s report number updated with each
		 * ABS_MT_TRACKING_ID change.
		 */
		if (free_slot == -1 && tr_id == -1 &&
		    evdev->ev_mt->ev_mt_slots[slot].ev_report !=
		    evdev->ev_report_count)
			free_slot = slot;
	}

	return (free_slot);
}

void
evdev_support_nfingers(struct evdev_dev *evdev, int32_t nfingers)
{
	int32_t i;

	for (i = 0; i < MIN(nitems(evdev_fngmap), nfingers); i++)
		evdev_support_key(evdev, evdev_fngmap[i]);
}

void
evdev_support_mt_compat(struct evdev_dev *evdev)
{
	int32_t 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][0]))
			evdev_support_abs(evdev, evdev_mtstmap[i][1],
			    evdev->ev_absinfo[evdev_mtstmap[i][0]].value,
			    evdev->ev_absinfo[evdev_mtstmap[i][0]].minimum,
			    evdev->ev_absinfo[evdev_mtstmap[i][0]].maximum,
			    evdev->ev_absinfo[evdev_mtstmap[i][0]].fuzz,
			    evdev->ev_absinfo[evdev_mtstmap[i][0]].flat,
			    evdev->ev_absinfo[evdev_mtstmap[i][0]].resolution);
}

static int32_t
evdev_count_fingers(struct evdev_dev *evdev)
{
	int32_t nfingers = 0, i;

	for (i = 0; i <= MAXIMAL_MT_SLOT(evdev); i++)
		if (evdev_get_mt_value(evdev, i, ABS_MT_TRACKING_ID) != -1)
			nfingers++;

	return (nfingers);
}

static void
evdev_send_nfingers(struct evdev_dev *evdev, int32_t nfingers)
{
	int32_t i;

	EVDEV_LOCK_ASSERT(evdev);

	if (nfingers > nitems(evdev_fngmap))
		nfingers = nitems(evdev_fngmap);

	for (i = 0; i < nitems(evdev_fngmap); i++)
		evdev_send_event(evdev, EV_KEY, evdev_fngmap[i],
		    nfingers == i + 1);
}

void
evdev_push_nfingers(struct evdev_dev *evdev, int32_t nfingers)
{

	EVDEV_ENTER(evdev);
	evdev_send_nfingers(evdev, nfingers);
	EVDEV_EXIT(evdev);
}

void
evdev_send_mt_compat(struct evdev_dev *evdev)
{
	int32_t nfingers, i;

	EVDEV_LOCK_ASSERT(evdev);

	nfingers = evdev_count_fingers(evdev);
	evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0);

	if (evdev_get_mt_value(evdev, 0, ABS_MT_TRACKING_ID) != -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][1]))
				evdev_send_event(evdev, EV_ABS,
				    evdev_mtstmap[i][1],
				    evdev_get_mt_value(evdev, 0,
				    evdev_mtstmap[i][0]));

	/* 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_send_mt_compat(evdev);
	EVDEV_EXIT(evdev);
}

void
evdev_send_mt_autorel(struct evdev_dev *evdev)
{
	int32_t slot;

	EVDEV_LOCK_ASSERT(evdev);

	for (slot = 0; slot <= MAXIMAL_MT_SLOT(evdev); slot++) {
		if (evdev->ev_mt->ev_mt_slots[slot].ev_report !=
		    evdev->ev_report_count &&
		    evdev_get_mt_value(evdev, slot, ABS_MT_TRACKING_ID) != -1){
			evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot);
			evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID,
			    -1);
		}
	}
}