-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchProcessor.cs
354 lines (309 loc) · 11.2 KB
/
TouchProcessor.cs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using Linearstar.Windows.RawInput;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Drawing;
using System.Timers;
namespace PreciseThreeFingersDrag
{
public class TouchProcessor : IDisposable
{
public enum CooldownDelay
{
None = 1,
Short = 300,
Long = 900,
}
public enum State
{
CLEAR,
ONE_FINGER_IDLE,
ONE_FINGER_MOVING,
TWO_FINGER_IDLE,
TWO_FINGER_MOVING,
THREE_FINGER_IDLE,
THREE_FINGER_MOVING,
THREE_FINGER_DRAG,
THREE_FINGER_DRAG_COOLDOWN,
FOUR_OR_MORE,
};
private State state = State.CLEAR;
private double accumulatedDistance;
private readonly System.Timers.Timer dragCooldownTimer;
public CooldownDelay DragCooldownDelay
{
get => dragCooldownTimer.Interval switch
{
> 900 => CooldownDelay.Long,
< 1 => CooldownDelay.None,
_ => CooldownDelay.Short
};
set => dragCooldownTimer.Interval = (double)value;
}
private RawInputDigitizerContact[] previousContacts;
private Point? previousCenterPoint;
int newContactsCount = 0;
int previousContactCount = 0;
public TouchProcessor()
{
dragCooldownTimer = new System.Timers.Timer((double)CooldownDelay.Short)
{
AutoReset = false
};
dragCooldownTimer.Elapsed += DragCooldownTimer_Elapsed;
previousContacts = new RawInputDigitizerContact[] { };
previousContactCount = 0;
}
public void Register(IntPtr hwnd)
{
RawInputDevice.RegisterDevice(HidUsageAndPage.TouchPad, RawInputDeviceFlags.InputSink, hwnd);
RawInputDevice.RegisterDevice(HidUsageAndPage.Mouse, RawInputDeviceFlags.InputSink, hwnd);
}
public void Update(RawInputMouseData data)
{
if (
data.Mouse.Buttons.HasFlag(Linearstar.Windows.RawInput.Native.RawMouseButtonFlags.LeftButtonUp)
)
{
if (state is State.THREE_FINGER_DRAG or State.THREE_FINGER_DRAG_COOLDOWN)
{
SetState(State.CLEAR);
dragCooldownTimer.Stop();
}
Thread.Sleep(100);
}
}
public void Update(RawInputDigitizerData data)
{
if (data.ContactsCount == 0)
{
// Junk data. If last finger leaves touchpad, the contact is still present with "IsButtonDown" switch off
return;
}
RawInputDigitizerContact[] newContacts = data.Contacts
.Where(c => c.IsButtonDown.GetValueOrDefault(false) && c.Kind == RawInputDigitizerContactKind.Finger)
.ToArray();
newContactsCount = data.ContactsCount;
if (newContacts.Length == 0)
{
newContactsCount = 0;
}
// From nothing
if (state == State.CLEAR)
{
SetState(newContactsCount switch
{
0 => State.CLEAR,
1 => State.ONE_FINGER_IDLE,
2 => State.TWO_FINGER_IDLE,
3 => State.THREE_FINGER_IDLE,
_ => State.FOUR_OR_MORE,
});
accumulatedDistance = 0;
previousContacts = newContacts;
previousCenterPoint = null;
previousContactCount = newContactsCount;
return;
}
// While cooldown - reset on any movement no matter how many fingers registered
if (state == State.THREE_FINGER_DRAG_COOLDOWN)
{
for (int i = 0; i < newContacts.Length && i < previousContacts.Length; i++)
{
Point newPoint = new(newContacts[i].X, newContacts[i].Y);
Point previousPoint = new(previousContacts[i].X, previousContacts[i].Y);
Point diff = newPoint - (Size)previousPoint;
accumulatedDistance += 1 / InvSqrt((diff.X * diff.X) + (diff.Y * diff.Y));
}
if (accumulatedDistance > 15)
{
SetState(newContactsCount switch
{
0 => State.CLEAR,
1 => State.ONE_FINGER_IDLE,
2 => State.TWO_FINGER_IDLE,
3 => State.THREE_FINGER_IDLE,
_ => State.FOUR_OR_MORE,
});
MouseInject.LeftButtonPressed = false;
dragCooldownTimer.Stop();
}
}
// Same fingers
if (newContactsCount > 0 && previousContactCount == newContactsCount)
{
Point center = CalculateCenterPoint(newContacts);
Point diff = new();
if (previousCenterPoint.HasValue)
{
diff = center - (Size)previousCenterPoint.Value;
accumulatedDistance += 1 / InvSqrt((diff.X * diff.X) + (diff.Y * diff.Y));
}
previousCenterPoint = center;
previousContacts = newContacts;
previousContactCount = newContactsCount;
if (accumulatedDistance > 15)
{
SetState(state switch
{
State.ONE_FINGER_IDLE => State.ONE_FINGER_MOVING,
State.TWO_FINGER_IDLE => State.TWO_FINGER_MOVING,
State.THREE_FINGER_IDLE => State.THREE_FINGER_MOVING,
_ => state
});
}
if (state == State.THREE_FINGER_MOVING)
{
MouseInject.LeftButtonPressed = true;
SetState(State.THREE_FINGER_DRAG);
}
if (state == State.THREE_FINGER_DRAG)
{
MouseInject.Move(diff);
}
return;
}
// Added fingers
if (previousContactCount < newContactsCount)
{
if (state == State.THREE_FINGER_DRAG_COOLDOWN)
{
if (newContactsCount == 3)
{
SetState(State.THREE_FINGER_DRAG);
dragCooldownTimer.Stop();
}
else
{
// keep this state
}
}
else if (state == State.THREE_FINGER_DRAG && newContactsCount == 3)
{
// keep this state
}
else if (state == State.TWO_FINGER_MOVING)
{
// keep this state
}
else
{
SetState(newContactsCount switch
{
0 => State.CLEAR,
1 => State.ONE_FINGER_IDLE,
2 => State.TWO_FINGER_IDLE,
3 => State.THREE_FINGER_IDLE,
_ => State.FOUR_OR_MORE,
});
accumulatedDistance = 0;
}
previousContacts = newContacts;
previousCenterPoint = null;
previousContactCount = newContactsCount;
return;
}
// Removed fingers
if (previousContactCount > newContactsCount)
{
if (state == State.THREE_FINGER_DRAG)
{
if (newContactsCount == 2)
{
// keep dragging
}
else
{
SetState(State.THREE_FINGER_DRAG_COOLDOWN);
dragCooldownTimer.Start();
accumulatedDistance = 0;
}
}
else if (state == State.THREE_FINGER_DRAG_COOLDOWN)
{
// keep this state
}
else if (state == State.FOUR_OR_MORE && newContactsCount == 3)
{
SetState(State.THREE_FINGER_DRAG);
}
else if (state == State.TWO_FINGER_MOVING && newContactsCount >= 2)
{
// keep this state
}
else
{
SetState(newContactsCount switch
{
0 => State.CLEAR,
1 => State.ONE_FINGER_IDLE,
2 => State.TWO_FINGER_IDLE,
3 => State.THREE_FINGER_IDLE,
_ => State.FOUR_OR_MORE,
});
accumulatedDistance = 0;
}
previousContacts = newContacts;
previousCenterPoint = null;
previousContactCount = newContactsCount;
return;
}
}
public delegate void StateChangeEvent(object sender, State newState);
public event StateChangeEvent? StateChange;
private void SetState(State newState)
{
state = newState;
StateChange?.Invoke(this, newState);
}
private float InvSqrt(float x)
{
float xhalf = 0.5f * x;
int i = BitConverter.SingleToInt32Bits(x);
i = 0x5f3759df - (i >> 1);
x = BitConverter.Int32BitsToSingle(i);
x *= 1.5f - (xhalf * x * x);
return x;
}
protected Point CalculateCenterPoint(RawInputDigitizerContact[] contacts)
{
if (contacts.Length == 0)
{
throw new ArgumentException("empty list of contacts");
}
if (contacts.Length == 1)
{
return new Point(contacts[0].X, contacts[0].Y);
}
Point p = new(0, 0);
foreach (RawInputDigitizerContact contact in contacts)
{
p.X += contact.X;
p.Y += contact.Y;
}
p.X /= contacts.Length;
p.Y /= contacts.Length;
return p;
}
private void DragCooldownTimer_Elapsed(object? sender, ElapsedEventArgs e)
{
SetState(State.CLEAR);
if (MouseInject.LeftButtonPressed)
{
MouseInject.LeftButtonPressed = false;
}
}
public bool IsDisposed { get; private set; }
~TouchProcessor()
{
Dispose();
}
public void Dispose()
{
if (!IsDisposed)
{
RawInputDevice.UnregisterDevice(HidUsageAndPage.TouchPad);
RawInputDevice.UnregisterDevice(HidUsageAndPage.Mouse);
}
}
}
}