/
elantechwin.c
320 lines (228 loc) · 6.79 KB
/
elantechwin.c
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <ntddk.h>
#include <kbdmou.h>
#include <ntddmou.h>
#include <ntdd8042.h>
#include <ntstrsafe.h>
#include "elantechwin.h"
int elantech_packet_check_v4(MOUSE_PACKET_V4 packet)
{
if ((packet[0] & 0x0c) == 0x04 &&
(packet[3] & 0x1f) == 0x11)
return PACKET_V4_HEAD;
if ((packet[0] & 0x0c) == 0x04 &&
(packet[3] & 0x1f) == 0x12)
return PACKET_V4_MOTION;
if ((packet[0] & 0x0c) == 0x04 &&
(packet[3] & 0x1f) == 0x10)
return PACKET_V4_STATUS;
return PACKET_UNKNOWN;
}
void process_packet_status_v4(PHAND_STATUS hand, MOUSE_PACKET_V4 packet)
{
unsigned fingers_state;
int f;
BOOLEAN down;
PFINGER_STATUS finger;
//notify finger state change
fingers_state = packet[1] & 0x1f;
//check state of each finger
for(f = 0; f < ETP_MAX_FINGERS; f++)
{
finger = &(hand->fingers[f]);
//test finger touchdown state
down = (fingers_state & (1 << f)) > 0;
//detect if finger status changed
if(finger->down != down)
{
//store finger status and mark that it has changed
finger->down = down;
finger->down_changed = TRUE;
if(finger->down)
{
//finger pressed down
//increment number of fingers down
hand->num_fingers_down++;
//mark the finger as needing x_start and y_start coordinates
finger->need_start = TRUE;
//store a timestamp for the touch down
KeQuerySystemTime(&(finger->down_timestamp));
}
else
{
//finger released
//decrement number of fingers down
hand->num_fingers_down--;
//releasing the touch, so definitely don't need a start coordinate
finger->need_start = FALSE;
finger->num_moments = 0;
finger->x = 0;
finger->y = 0;
finger->dx = 0;
finger->dy = 0;
}
}
}
}
void process_packet_head_v4(PHAND_STATUS hand, MOUSE_PACKET_V4 packet)
{
int f;
int x, dx, y, dy;
BOOLEAN clickpad_down;
PFINGER_STATUS finger;
PFINGER_MOMENT prev;
unsigned int pressure = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
int traces = (packet[0] & 0xf0) >> 4;
f = ((packet[3] & 0xe0) >> 5) - 1; //what is this really?
if (f < 0)
return;
//helper
finger = &(hand->fingers[f]);
//calculate new x and y positions
x = ((packet[1] & 0x0f) << 8) | packet[2];
y = ((packet[4] & 0x0f) << 8) | packet[5];
//detect status of clickpad
clickpad_down = packet[0] & 0x01;
//if clickpad status has changed..
if(hand->clickpad_down != clickpad_down)
{
hand->clickpad_down = clickpad_down;
hand->clickpad_down_changed = TRUE;
//KeQuerySystemTime(&(hand->clickpad_down_timestamp));
}
//if we need absolute start coordinates
if(finger->need_start)
{
//flag as having assigned the start coordinates
finger->need_start = FALSE;
//set current position to start coords
finger->x = x;
finger->y = y;
//set start coords
finger->x_start = x;
finger->y_start = y;
//null out the dxdy
dx = 0;
dy = 0;
}
else
{
//calculate dx and dy from previous x and y position
dx = x - finger->x;
dy = -1 * (y - finger->y);
//set absolute coordinates
finger->x = x;
finger->y = y;
}
finger->dx = dx;
finger->dy = dy;
finger->traces = traces;
//push all the data into the current finger moment
//advance the moment pointer
finger->moment = finger->moment->next;
//increment number of pointers, but not past the limit
if(++(finger->num_moments) > MOMENT_BUFFER_SIZE)
finger->num_moments = MOMENT_BUFFER_SIZE;
finger->moment->x = x;
finger->moment->y = y;
finger->moment->dx = dx;
finger->moment->dy = dy;
finger->moment->pressure = pressure;
//store the moment's timestamp
KeQuerySystemTime(&(finger->moment->timestamp));
prev = (PFINGER_MOMENT) finger->moment->prev;
//calculate dt of the moment and store it
if(prev->timestamp > 0)
finger->moment->dt = prev->timestamp - finger->moment->timestamp;
}
void process_packet_motion_v4(PHAND_STATUS hand, MOUSE_PACKET_V4 packet)
{
int delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
int f, f2;
int weight;
BOOLEAN clickpad_down;
long dx1, dy1, dx2, dy2;
PFINGER_STATUS finger1, finger2;
//determine which finger
f = ((packet[0] & 0xe0) >> 5) - 1;
if(f < 0)
return;
finger1 = &(hand->fingers[f]);
weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
//detect status of clickpad
clickpad_down = packet[0] & 0x01;
if(hand->clickpad_down != clickpad_down)
{
hand->clickpad_down = clickpad_down;
hand->clickpad_down_changed = TRUE;
}
//Motion packets give us the delta of x, y values of specific fingers,
//but in two's complement. Let the compiler do the conversion for us.
//Also _enlarge_ the numbers to int, in case of overflow.
delta_x1 = (signed char) packet[1];
delta_y1 = (signed char) packet[2];
delta_x2 = (signed char) packet[4];
delta_y2 = (signed char) packet[5];
//manage finger 1
//advance the moment pointer
finger1->moment = finger1->moment->next;
//increment number of pointers, but not past the limit
if(++(finger1->num_moments) > MOMENT_BUFFER_SIZE)
finger1->num_moments = MOMENT_BUFFER_SIZE;
dx1 = delta_x1 * weight;
dy1 = delta_y1 * weight;
finger1->x += dx1;
finger1->y += dy1;
//store the xy coords
finger1->moment->x = finger1->x;
finger1->moment->y = finger1->y;
finger1->moment->dx = dx1;
finger1->moment->dy = dy1;
//store the moment's timestamp
KeQuerySystemTime(&(finger1->moment->timestamp));
//manage finger 2
f2 = ((packet[3] & 0xe0) >> 5) - 1;
finger2 = &(hand->fingers[f2]);
if(f2 >= 0)
{
//advance the moment pointer
finger2->moment = finger2->moment->next;
//increment number of pointers, but not past the limit
if(++(finger2->num_moments) > MOMENT_BUFFER_SIZE)
finger2->num_moments = MOMENT_BUFFER_SIZE;
dx2 = delta_x2 * weight;
dy2 = delta_y2 * weight;
finger2->x += dx2;
finger2->y += dy2;
//store the moment
finger2->moment->x = finger2->x;
finger2->moment->y = finger2->y;
finger2->moment->dx = dx2;
finger2->moment->dy = dy2;
//store the moment's timestamp
KeQuerySystemTime(&(finger2->moment->timestamp));
}
}
unsigned int elantech_convert_res(unsigned int val)
{
return (val * 10 + 790) * 10 / 254;
}
//fast integer approximate distance calculator
UINT32 distance(INT32 dx, INT32 dy)
{
UINT32 min, max;
if ( dx < 0 ) dx = -dx;
if ( dy < 0 ) dy = -dy;
if ( dx < dy )
{
min = dx;
max = dy;
}
else
{
min = dy;
max = dx;
}
// coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min )
return ((( max << 8 ) + ( max << 3 ) - ( max << 4 ) - ( max << 1 ) +
( min << 7 ) - ( min << 5 ) + ( min << 3 ) - ( min << 1 )) >> 8 );
}