-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inputs.cpp
512 lines (448 loc) · 16.5 KB
/
Inputs.cpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include "pch.h"
#include "libWSAController.h"
#include "Inputs.h"
#include "VirtualTouch.h"
#include "CallbackLogger.h"
static size_t inner_id = 0;
static size_t m_count = 0;
constexpr size_t max_retry_times = 2048;
enum InputType
{
KeyPress = -1,
TimerStart = 100,
TimerCheck = 200,
LeftClick = 0,
LeftDown = 1,
LeftUp = 2,
MouseMove = 3,
MouseMoveWithKeyPressed = 4,
TouchClick = 5,
TouchDown = 6,
TouchMove = 7,
TouchUp = 8,
};
#define msg_left_click(x,y) m_msgs.push(MsgUnit{ 0, 0, MAKELPARAM(x, y + m_caption_height) })
#define msg_left_down(x,y) m_msgs.push(MsgUnit{ 1, 0, MAKELPARAM(x, y + m_caption_height) })
#define msg_left_up(x,y) m_msgs.push(MsgUnit{ 2, 0, MAKELPARAM(x, y + m_caption_height) })
#define msg_left_hover(x,y) m_msgs.push(MsgUnit{ 3, 0, MAKELPARAM(x, y + m_caption_height) })
#define msg_left_hover_key(x,y,wparam) m_msgs.push(MsgUnit{ 4, wparam, MAKELPARAM(x, y + m_caption_height) })
#define msg_tic() m_msgs.push(MsgUnit{ 100, 0, 0 })
#define msg_toc_wait(time_stamp) m_msgs.push({ 200, (WPARAM)time_stamp, 0 })
#define msg_touch_click(x,y) m_msgs.push(MsgUnit{ 5, 0, MAKELPARAM(x, y + m_caption_height) })
#define msg_touch_down(x,y,new_id) m_msgs.push(MsgUnit{ 6, new_id = inner_id++, MAKELPARAM(x, y + m_caption_height) })
#define msg_touch_update(x,y,id) m_msgs.push(MsgUnit{ 7, id, MAKELPARAM(x, y + m_caption_height) })
#define msg_touch_up(x,y,id) m_msgs.push(MsgUnit{ 8, id, MAKELPARAM(x, y + m_caption_height) })
inline double InputInjector::linear_interpolate(double& sx, double& sy, double dx, double dy,
double time_stamp, double dur_slice, double n, UINT64 touched)
{
for (int i = 1; i <= n; i++) {
sx += dx, sy += dy;
msg_toc_wait(time_stamp);
if (touched == UINT64_MAX) msg_left_hover(sx, sy);
else msg_touch_update(sx, sy, touched);
time_stamp += dur_slice;
}
return time_stamp;
}
struct RunInputInjectorMainEssential
{
std::queue<InputInjector::MsgUnit>* pMsgs;
HWND hWnd;
};
std::atomic<HWND> last_captured_hwnd = 0;
void RunInputInjectorMain(InputInjector* self)
{
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
using namespace std::chrono;
time_point<high_resolution_clock, milliseconds> tic;
time_point<high_resolution_clock, milliseconds> toc;
std::optional<TouchedPointID> m_touch;
uint64_t retry_times = 0;
callback(std::format("InputInjector({:#16x})", UINT64(self)), "INF", "Create thread!");
while (!self->m_inited && retry_times < max_retry_times) {
retry_times++;
std::this_thread::yield();
}
if (retry_times == max_retry_times) {
callback(std::format("InputInjector({:#16x})", UINT64(self)), "ERR", "Failed to run thread!!");
self->stop_requested = true;
return;
}
retry_times = 0;
self->stop_possible = true;
HWND m_hwnd = self->m_hwnd;
auto& m_msgs = self->m_msgs;
callback(std::format("InputInjector({:#16x})", UINT64(self)), "INF", "Thread Succeeded!!");
while (!self->stop_requested) {
if (self->Empty()) {
last_captured_hwnd.store(NULL);
std::this_thread::yield();
continue;
}
for (HWND cur_hwnd = last_captured_hwnd.load();
cur_hwnd != 0 && cur_hwnd != m_hwnd;
cur_hwnd = last_captured_hwnd.load());
SetForegroundWindow(m_hwnd);
last_captured_hwnd.store(m_hwnd);
InputInjector::MsgUnit& command = m_msgs.front();
switch (command.type) {
case InputType::KeyPress:
SendMessage(m_hwnd, WM_KEYDOWN, command.wParam, 0);
Sleep(80);
SendMessage(m_hwnd, WM_KEYUP, command.wParam, 0);
Sleep(80);
break;
case InputType::LeftClick:
if (!SuperToucher::MouseLeftDown())
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_KEYDOWN, VK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_LBUTTONDOWN, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
Sleep(80);
if (!SuperToucher::MouseLeftUp())
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_LBUTTONUP, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_KEYUP, VK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSELEAVE, 0, 0);
Sleep(80);
break;
case InputType::TimerStart:
tic = time_point_cast<milliseconds>(high_resolution_clock::now());
break;
case InputType::TimerCheck:
toc = time_point_cast<milliseconds>(high_resolution_clock::now());
while (toc - tic <= milliseconds{ command.wParam }) {
toc = time_point_cast<milliseconds>(high_resolution_clock::now());
}
break;
case InputType::LeftDown:
if (!SuperToucher::MouseLeftDown())
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_KEYDOWN, VK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_LBUTTONDOWN, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
break;
case InputType::LeftUp:
if (!SuperToucher::MouseLeftUp())
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_LBUTTONUP, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_KEYUP, VK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_MOUSEHOVER, 0, command.lParam);
SendMessage(m_hwnd, WM_MOUSELEAVE, 0, 0);
Sleep(80);
break;
case InputType::MouseMove:
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_MOUSEMOVE, MK_LBUTTON, command.lParam);
break;
case InputType::MouseMoveWithKeyPressed:
SendMessage(m_hwnd, WM_MOUSEHOVER, MK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_MOUSEMOVE, MK_LBUTTON, command.lParam);
SendMessage(m_hwnd, WM_KEYDOWN, VK_ESCAPE, 0);
SendMessage(m_hwnd, WM_KEYUP, VK_ESCAPE, 0);
break;
}
POINTS touch_pt_s = MAKEPOINTS(command.lParam);
POINT touch_pt = { touch_pt_s.x, touch_pt_s.y };
if (self->HasTouch(command.wParam))
m_touch = self->m_touchids[command.wParam];
switch (command.type)
{
case InputType::TouchClick:
ClientToScreen(m_hwnd, &touch_pt);
m_touch = SuperToucher::Down(m_hwnd, touch_pt.x, touch_pt.y);
if (!m_touch)
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
Sleep(80);
SuperToucher::Up(*m_touch, touch_pt.x, touch_pt.y);
Sleep(20);
SendMessage(m_hwnd, WM_MOUSELEAVE, 0, 0);
break;
case InputType::TouchDown:
ClientToScreen(m_hwnd, &touch_pt);
m_touch = SuperToucher::Down(m_hwnd, touch_pt.x, touch_pt.y);
if (!m_touch)
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
self->m_touchids[command.wParam] = *m_touch;
break;
case InputType::TouchMove:
ClientToScreen(m_hwnd, &touch_pt);
if (!SuperToucher::Update(*m_touch, touch_pt.x, touch_pt.y))
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
break;
case InputType::TouchUp:
ClientToScreen(m_hwnd, &touch_pt);
if (!SuperToucher::Up(*m_touch, touch_pt.x, touch_pt.y))
{
if (retry_times > max_retry_times)
throw std::runtime_error("touch err!");
retry_times++;
std::this_thread::yield();
continue;
}
self->m_touchids.erase(*m_touch);
SendMessage(m_hwnd, WM_MOUSELEAVE, 0, 0);
break;
}
m_msgs.pop();
retry_times = 0;
}
}
bool InputInjector::Attach(HWND hwnd, DWORD processId)
{
Release();
m_hwnd = hwnd;
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
auto wndStyle = GetWindowLong(hwnd, GWL_STYLE);
auto wndExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
auto dpi = GetDpiForWindow(hwnd);
RECT wantedSize = { .left = 0, .top = 0, .right = WindowWidthDefault, .bottom = WindowHeightDefault };
RECT wantedWindow = wantedSize, wantedClient = wantedSize;
GetClientRect(hwnd, &wantedClient);
GetWindowRect(hwnd, &wantedWindow);
wantedWindow.bottom -= wantedWindow.top;
wantedWindow.right -= wantedWindow.left;
wantedWindow.left = wantedWindow.top = 0;
RECT testCaption = { 0, 0, 100, 100 };
AdjustWindowRectExForDpi(&testCaption, wndStyle, false, wndExStyle, dpi);
auto black = wantedWindow.bottom - wantedClient.bottom;
int caption_height = testCaption.bottom - testCaption.top - 100 - black;
std::mt19937_64 random_engine{ std::random_device {}() };
std::uniform_real_distribution<double> dist(-5, 5);
if (!SuperToucher::IsReady() && !SuperToucher::Attach(processId)) return false;
UgencyClear();
m_inited = true;
while (!stop_possible) std::this_thread::yield();
if (stop_requested) {
m_inited = false;
callbackfe("Cannot create touch thread!!");
return false;
}
m_count++;
return true;
}
void InputInjector::Release()
{
StopThread();
while (!Empty()) m_msgs.pop();
if (m_inited) m_count--;
if (m_count == 0)
SuperToucher::Release();
}
bool InputInjector::MouseLeftClick(int x, int y)
{
if (!m_inited) return false;
if (Exceeded()) return false;
msg_left_click(x, y);
return true;
}
bool InputInjector::MouseLeftSwipe(double sx, double sy, double ex, double ey, double dur)
{
if (!m_inited) return false;
if (Exceeded()) return false;
constexpr int nslices = 32;
auto dx = (ex - sx) / nslices, dy = (ey - sy) / nslices;
auto dur_slice = dur / nslices;
msg_left_down(sx, sy);
msg_tic();
linear_interpolate(sx, sy, dx, dy, 0, dur_slice, nslices);
msg_left_up(sx, sy);
return true;
}
bool InputInjector::MouseLeftSwipePrecisely(double sx, double sy, double ex, double ey, double dur)
{
if (!m_inited) return false;
if (Exceeded()) return false;
constexpr int nslices = 16;
constexpr int neslices = 32;
constexpr int extra_slice_size = 3;
constexpr double time_ratio = 3 / 8.;
ex += extra_slice_size, ey += extra_slice_size;
auto dx = (ex - sx) / nslices, dy = (ey - sy) / nslices;
auto ux = std::abs(dx) / dx, uy = std::abs(dy) / dy;
auto dur_slice = dur * time_ratio / nslices;
msg_left_down(sx, sy);
msg_tic();
auto time_stamp = linear_interpolate(sx, sy, dx, dy, 0, dur_slice, nslices);
dx = -ux * extra_slice_size / neslices, dy = -uy * extra_slice_size / neslices;
time_stamp = linear_interpolate(sx, sy, dx, dy, time_stamp, dur_slice, neslices);
linear_interpolate(sx, sy, 0, 0, time_stamp, 0, 10);
msg_left_up(sx, sy);
return true;
}
void InputInjector::Wait()
{
while (!Empty());
SuperToucher::WaitTouch();
}
bool InputInjector::TouchClick(int x, int y)
{
if (!m_inited) return false;
if (Exceeded()) return false;
msg_touch_click(x, y);
return true;
}
bool InputInjector::TouchSwipe(double sx, double sy, double ex, double ey, double dur)
{
if (!m_inited) return false;
if (Exceeded()) return false;
constexpr int nslices = 16;
auto dx = (ex - sx) / nslices, dy = (ey - sy) / nslices;
auto dur_slice = dur / nslices;
UINT64 touched = UINT64_MAX;
msg_touch_down(sx, sy, touched);
msg_tic();
linear_interpolate(sx, sy, dx, dy, 0, dur_slice, nslices, touched);
msg_touch_up(sx, sy, touched);
return true;
}
bool InputInjector::TouchSwipePrecisely(double sx, double sy, double ex, double ey, double dur)
{
if (!m_inited) return false;
if (Exceeded()) return false;
constexpr int nslices = 8;
constexpr int neslices = 16;
constexpr int extra_slice_size = 3;
constexpr double time_ratio = 3 / 8.;
ex += extra_slice_size, ey += extra_slice_size;
auto dx = (ex - sx) / nslices, dy = (ey - sy) / nslices;
auto ux = std::abs(dx) / dx, uy = std::abs(dy) / dy;
auto dur_slice = dur * time_ratio / nslices;
UINT64 touched = UINT64_MAX;
msg_touch_down(sx, sy, touched);
msg_tic();
auto time_stamp = linear_interpolate(sx, sy, dx, dy, 0, dur_slice, nslices, touched);
dx = -ux * extra_slice_size / neslices, dy = -uy * extra_slice_size / neslices;
time_stamp = linear_interpolate(sx, sy, dx, dy, time_stamp, dur_slice, neslices, touched);
linear_interpolate(sx, sy, 0, 0, time_stamp, 0, 10, touched);
msg_touch_up(sx, sy, touched);
return true;
}
void InputInjector::UgencyClear()
{
if (stop_possible) StopThread();
stop_requested = false;
while (!Empty()) m_msgs.pop();
SuperToucher::MouseLeftUp();
//RunInputInjectorMainEssential ess{ m_msgs.get(), m_hwnd};
m_thread = std::thread(RunInputInjectorMain, this);
}
bool InputInjector::MouseLeftDown(int x, int y)
{
if (!m_inited) return false;
if (Exceeded()) return false;
msg_left_down(x, y);
return true;
}
bool InputInjector::MouseMove(int x, int y)
{
if (!m_inited) return false;
if (Exceeded()) return false;
msg_left_hover(x, y);
return true;
}
bool InputInjector::MouseLeftUp(int x, int y)
{
if (!m_inited) return false;
if (Exceeded()) return false;
msg_left_up(x, y);
return true;
}
UINT64 InputInjector::TouchDown(int x, int y)
{
LogTraceFunction;
if (!m_inited) return false;
if (Exceeded()) return false;
UINT64 id;
msg_touch_down(x, y, id);
return id;
}
bool InputInjector::TouchMove(int x, int y, UINT64 id)
{
if (!m_inited) return false;
if (Exceeded()) return false;
if (!HasTouch(id)) {
callbackfe("Invalid input id")
return false;
}
msg_touch_update(x, y, id);
return true;
}
bool InputInjector::TouchUp(int x, int y, UINT64 id)
{
LogTraceFunction;
if (!m_inited) return false;
if (Exceeded()) return false;
if (!HasTouch(id)) {
callbackfe("Invalid input id")
return false;
}
msg_touch_up(x, y, id);
return true;
}
bool InputInjector::Interrupt()
{
UgencyClear();
for (auto& i : m_touchids)
{
if (!SuperToucher::Up(i.second, 0, 0))
{
callbackfe("Error when try to interrupting touch event!!!!");
return false;
}
}
return true;
}