summaryrefslogtreecommitdiff
path: root/sys/dev/wscons/wsmouseinput.h
blob: 5665fe4cfd51bec2880bde0fff42fe65b10d55ef (plain)
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
/* $OpenBSD: wsmouseinput.h,v 1.15 2021/03/21 16:20:49 bru Exp $ */

/*
 * Copyright (c) 2015, 2016 Ulf Brosziewski
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * wsmouse input processing - private header
 */

#ifndef _WSMOUSEINPUT_H_
#define _WSMOUSEINPUT_H_

#ifdef _KERNEL

struct position {
	int x;
	int y;
	int dx;			/* unfiltered coordinate deltas */
	int dy;
	int acc_dx;		/* delta sums used for filtering */
	int acc_dy;
};

struct btn_state {
	u_int buttons;
	u_int sync;
};

struct motion_state {
	int dx;			/* mouse input, or filtered deltas */
	int dy;
	int dz;
	int dw;
	struct position pos;
	u_int sync;
};
#define SYNC_DELTAS		(1 << 0)
#define SYNC_X			(1 << 1)
#define SYNC_Y			(1 << 2)
#define SYNC_POSITION		(SYNC_X | SYNC_Y)

struct touch_state {
	int pressure;
	int contacts;
	int width;
	u_int sync;

	int min_pressure;
	int prev_contacts;
};
#define SYNC_PRESSURE		(1 << 0)
#define SYNC_CONTACTS		(1 << 1)
#define SYNC_TOUCH_WIDTH	(1 << 2)

struct mt_slot {
	struct position pos;
	int pressure;
	int id;		/* tracking ID */
};
#define MTS_TOUCH	0
#define MTS_X		1
#define MTS_Y		2
#define MTS_PRESSURE	3

#define MTS_SIZE	4

struct mt_state {
	/* the set of slots with active touches */
	u_int touches;
	/* the set of slots with unsynchronized state */
	u_int frame;

	int num_slots;
	struct mt_slot *slots;
	/* the sets of changes per slot axis */
	u_int sync[MTS_SIZE];

	int num_touches;

	/* pointer control */
	u_int ptr;
	u_int ptr_cycle;
	u_int prev_ptr;
	u_int ptr_mask;

	/* a buffer for the MT tracking function */
	int *matrix;
};


struct axis_filter {
	/* A scale factor in [*.12] fixed-point format */
	int scale;
	int rmdr;
	/* Invert coordinates. */
	int inv;
	/* Hysteresis limit, and weighted delta average. */
	int hysteresis;
	int avg;
	int avg_rmdr;
	/* A [*.12] coefficient for "magnitudes", used for deceleration. */
	int mag_scale;
	int dclr_rmdr;
	/* Ignore deltas that are greater than this limit. */
	int dmax;
};

struct interval {
	long avg;	/* average update interval in nanoseconds */
	long sum;
	int samples;
	struct timespec ts;
	int track;
};

struct wsmouseinput {
	u_int flags;

	struct btn_state btn;
	struct btn_state sbtn;	/* softbuttons */
	struct motion_state motion;
	struct touch_state touch;
	struct mt_state mt;

	struct { /* Parameters and state of various input filters. */
		struct axis_filter h;
		struct axis_filter v;

		int dclr;	/* deceleration threshold */
		int mag;	/* weighted average of delta magnitudes */
		u_int mode;	/* hysteresis type, smoothing factor */

		int ratio;	/* X/Y ratio */

		int swapxy;
		int tracking_maxdist;
		int pressure_lo;
		int pressure_hi;
	} filter;

	struct wsmousehw hw;
	struct interval intv;
	struct wstpad *tp;

	struct wseventvar **evar;
};
/* wsmouseinput.flags */
#define TPAD_COMPAT_MODE	(1 << 0)
#define TPAD_NATIVE_MODE	(1 << 1)
#define MT_TRACKING		(1 << 2)
#define REVERSE_SCROLLING	(1 << 3)
#define RESYNC			(1 << 16)
#define TRACK_INTERVAL		(1 << 17)
#define CONFIGURED		(1 << 18)
#define LOG_INPUT		(1 << 19)
#define LOG_EVENTS		(1 << 20)

/* filter.mode (bit 0-2: smoothing factor, bit 3-n: unused) */
#define SMOOTHING_MASK		7
#define FILTER_MODE_DEFAULT	0

struct evq_access {
	struct wseventvar *evar;
	struct timespec ts;
	int put;
	int result;
};
#define EVQ_RESULT_OVERFLOW	-1
#define EVQ_RESULT_NONE		0
#define EVQ_RESULT_SUCCESS	1


void wsmouse_evq_put(struct evq_access *, int, int);
void wsmouse_log_events(struct wsmouseinput *, struct evq_access *);
int wsmouse_hysteresis(struct wsmouseinput *, struct position *);
void wsmouse_input_reset(struct wsmouseinput *);
void wsmouse_input_cleanup(struct wsmouseinput *);

void wstpad_compat_convert(struct wsmouseinput *, struct evq_access *);
void wstpad_init_deceleration(struct wsmouseinput *);
int wstpad_configure(struct wsmouseinput *);
void wstpad_reset(struct wsmouseinput *);
void wstpad_cleanup(struct wsmouseinput *);

int wstpad_get_param(struct wsmouseinput *, int, int *);
int wstpad_set_param(struct wsmouseinput *, int, int);


#define FOREACHBIT(v, i) \
    for ((i) = ffs(v) - 1; (i) != -1; (i) = ffs((v) & (~1 << (i))) - 1)

#define DELTA_X_EV(input) ((input)->filter.swapxy ? \
    WSCONS_EVENT_MOUSE_DELTA_Y : WSCONS_EVENT_MOUSE_DELTA_X)
#define DELTA_Y_EV(input) ((input)->filter.swapxy ? \
    WSCONS_EVENT_MOUSE_DELTA_X : WSCONS_EVENT_MOUSE_DELTA_Y)
#define ABS_X_EV(input) ((input)->filter.swapxy ? \
    WSCONS_EVENT_MOUSE_ABSOLUTE_Y : WSCONS_EVENT_MOUSE_ABSOLUTE_X)
#define ABS_Y_EV(input) ((input)->filter.swapxy ? \
    WSCONS_EVENT_MOUSE_ABSOLUTE_X : WSCONS_EVENT_MOUSE_ABSOLUTE_Y)
#define DELTA_Z_EV	WSCONS_EVENT_MOUSE_DELTA_Z
#define DELTA_W_EV	WSCONS_EVENT_MOUSE_DELTA_W
#define VSCROLL_EV	WSCONS_EVENT_VSCROLL
#define HSCROLL_EV	WSCONS_EVENT_HSCROLL
#define ABS_Z_EV	WSCONS_EVENT_TOUCH_PRESSURE
#define ABS_W_EV	WSCONS_EVENT_TOUCH_CONTACTS
#define BTN_DOWN_EV	WSCONS_EVENT_MOUSE_DOWN
#define BTN_UP_EV	WSCONS_EVENT_MOUSE_UP
#define SYNC_EV		WSCONS_EVENT_SYNC

/* matrix size + buffer size for wsmouse_matching */
#define MATRIX_SIZE(slots) (((slots) + 6) * (slots) * sizeof(int))

#define IS_TOUCHPAD(input)			\
    ((input)->hw.hw_type == WSMOUSEHW_TOUCHPAD	\
    || (input)->hw.hw_type == WSMOUSEHW_CLICKPAD)

/* Extract a four-digit millisecond value from a timespec. */
#define LOGTIME(tsp) \
    ((int) (((tsp)->tv_sec % 10) * 1000 + ((tsp)->tv_nsec / 1000000)))

#define DEVNAME(input) ((char *) (input)	\
    - offsetof(struct wsmouse_softc, sc_input)	\
    + offsetof(struct device, dv_xname))

#endif /* _KERNEL */

#endif /* _WSMOUSEINPUT_H_ */