forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
612 lines (522 loc) · 23.2 KB
/
input.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#include "dll_log.hpp"
#include "input.hpp"
#include "hook_manager.hpp"
#include <mutex>
#include <cassert>
#include <algorithm>
#include <unordered_map>
#include <Windows.h>
static std::mutex s_windows_mutex;
static std::unordered_map<HWND, unsigned int> s_raw_input_windows;
static std::unordered_map<HWND, std::weak_ptr<reshade::input>> s_windows;
reshade::input::input(window_handle window)
: _window(window)
{
assert(window != nullptr);
}
#if RESHADE_UWP
static bool is_uwp_app()
{
const auto GetCurrentPackageFullName = reinterpret_cast<LONG(WINAPI*)(UINT32*, PWSTR)>(
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetCurrentPackageFullName"));
if (GetCurrentPackageFullName == nullptr)
return false;
// This will return APPMODEL_ERROR_NO_PACKAGE if not a packaged UWP app
UINT32 length = 0;
return GetCurrentPackageFullName(&length, nullptr) == ERROR_INSUFFICIENT_BUFFER;
}
#endif
void reshade::input::register_window_with_raw_input(window_handle window, bool no_legacy_keyboard, bool no_legacy_mouse)
{
#if RESHADE_UWP
if (is_uwp_app()) // UWP apps never use legacy input messages
no_legacy_keyboard = no_legacy_mouse = true;
#endif
const std::lock_guard<std::mutex> lock(s_windows_mutex);
const auto flags = (no_legacy_keyboard ? 0x1u : 0u) | (no_legacy_mouse ? 0x2u : 0u);
const auto insert = s_raw_input_windows.emplace(static_cast<HWND>(window), flags);
if (!insert.second) insert.first->second |= flags;
}
std::shared_ptr<reshade::input> reshade::input::register_window(window_handle window)
{
const std::lock_guard<std::mutex> lock(s_windows_mutex);
const auto insert = s_windows.emplace(static_cast<HWND>(window), std::weak_ptr<input>());
if (insert.second || insert.first->second.expired())
{
#if RESHADE_VERBOSE_LOG
LOG(DEBUG) << "Starting input capture for window " << window << " ...";
#endif
const auto instance = std::make_shared<input>(window);
insert.first->second = instance;
return instance;
}
else
{
return insert.first->second.lock();
}
}
bool reshade::input::handle_window_message(const void *message_data)
{
assert(message_data != nullptr);
MSG details = *static_cast<const MSG *>(message_data);
bool is_mouse_message = details.message >= WM_MOUSEFIRST && details.message <= WM_MOUSELAST;
bool is_keyboard_message = details.message >= WM_KEYFIRST && details.message <= WM_KEYLAST;
// Ignore messages that are not related to mouse or keyboard input
if (details.message != WM_INPUT && !is_mouse_message && !is_keyboard_message)
return false;
// Guard access to windows list against race conditions
std::unique_lock<std::mutex> lock(s_windows_mutex);
// Remove any expired entry from the list
for (auto it = s_windows.begin(); it != s_windows.end();)
it->second.expired() ? it = s_windows.erase(it) : ++it;
// Look up the window in the list of known input windows
auto input_window = s_windows.find(details.hwnd);
const auto raw_input_window = s_raw_input_windows.find(details.hwnd);
if (input_window == s_windows.end())
{
// Walk through the window chain and until an known window is found
EnumChildWindows(details.hwnd, [](HWND hwnd, LPARAM lparam) -> BOOL {
auto &input_window = *reinterpret_cast<decltype(s_windows)::iterator *>(lparam);
// Return true to continue enumeration
return (input_window = s_windows.find(hwnd)) == s_windows.end();
}, reinterpret_cast<LPARAM>(&input_window));
}
if (input_window == s_windows.end())
{
// Some applications handle input in a child window to the main render window
if (const HWND parent = GetParent(details.hwnd); parent != NULL)
input_window = s_windows.find(parent);
}
if (input_window == s_windows.end() && raw_input_window != s_raw_input_windows.end())
{
// Reroute this raw input message to the window with the most rendering
input_window = std::max_element(s_windows.begin(), s_windows.end(),
[](auto lhs, auto rhs) { return lhs.second.lock()->_frame_count < rhs.second.lock()->_frame_count; });
}
if (input_window == s_windows.end())
return false;
RAWINPUT raw_data = {};
const std::shared_ptr<input> input = input_window->second.lock();
// At this point we have a shared pointer to the input object and no longer reference any memory from the windows list, so can release the lock
lock.unlock();
// Prevent input threads from modifying input while it is accessed elsewhere
const std::lock_guard<std::mutex> input_lock = input->lock();
// Calculate window client mouse position
ScreenToClient(static_cast<HWND>(input->_window), &details.pt);
input->_mouse_position[0] = details.pt.x;
input->_mouse_position[1] = details.pt.y;
switch (details.message)
{
case WM_INPUT:
if (UINT raw_data_size = sizeof(raw_data);
GET_RAWINPUT_CODE_WPARAM(details.wParam) != RIM_INPUT || // Ignore all input sink messages (when window is not focused)
GetRawInputData(reinterpret_cast<HRAWINPUT>(details.lParam), RID_INPUT, &raw_data, &raw_data_size, sizeof(raw_data.header)) == UINT(-1))
break;
switch (raw_data.header.dwType)
{
case RIM_TYPEMOUSE:
is_mouse_message = true;
if (raw_input_window == s_raw_input_windows.end() || (raw_input_window->second & 0x2) == 0)
break; // Input is already handled (since legacy mouse messages are enabled), so nothing to do here
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN)
input->_mouse_buttons[0] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP)
input->_mouse_buttons[0] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN)
input->_mouse_buttons[1] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP)
input->_mouse_buttons[1] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_MIDDLE_BUTTON_DOWN)
input->_mouse_buttons[2] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_MIDDLE_BUTTON_UP)
input->_mouse_buttons[2] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_DOWN)
input->_mouse_buttons[3] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_UP)
input->_mouse_buttons[3] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_DOWN)
input->_mouse_buttons[4] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_UP)
input->_mouse_buttons[4] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_WHEEL)
input->_mouse_wheel_delta += static_cast<short>(raw_data.data.mouse.usButtonData) / WHEEL_DELTA;
break;
case RIM_TYPEKEYBOARD:
is_keyboard_message = true;
if (raw_input_window == s_raw_input_windows.end() || (raw_input_window->second & 0x1) == 0)
break; // Input is already handled by 'WM_KEYDOWN' and friends (since legacy keyboard messages are enabled), so nothing to do here
if (raw_data.data.keyboard.VKey != 0xFF)
input->_keys[raw_data.data.keyboard.VKey] = (raw_data.data.keyboard.Flags & RI_KEY_BREAK) == 0 ? 0x88 : 0x08,
input->_keys_time[raw_data.data.keyboard.VKey] = details.time;
// No 'WM_CHAR' messages are sent if legacy keyboard messages are disabled, so need to generate text input manually here
// Cannot use the ToAscii function always as it seems to reset dead key state and thus calling it can break subsequent application input, should be fine here though since the application is already explicitly using raw input
if (WORD ch = 0; (raw_data.data.keyboard.Flags & RI_KEY_BREAK) == 0 && ToAscii(raw_data.data.keyboard.VKey, raw_data.data.keyboard.MakeCode, input->_keys, &ch, 0))
input->_text_input += ch;
break;
}
break;
case WM_CHAR:
input->_text_input += static_cast<wchar_t>(details.wParam);
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
assert(details.wParam < ARRAYSIZE(input->_keys));
input->_keys[details.wParam] = 0x88;
input->_keys_time[details.wParam] = details.time;
break;
case WM_KEYUP:
case WM_SYSKEYUP:
assert(details.wParam < ARRAYSIZE(input->_keys));
input->_keys[details.wParam] = 0x08;
input->_keys_time[details.wParam] = details.time;
break;
case WM_LBUTTONDOWN:
input->_mouse_buttons[0] = 0x88;
break;
case WM_LBUTTONUP:
input->_mouse_buttons[0] = 0x08;
break;
case WM_RBUTTONDOWN:
input->_mouse_buttons[1] = 0x88;
break;
case WM_RBUTTONUP:
input->_mouse_buttons[1] = 0x08;
break;
case WM_MBUTTONDOWN:
input->_mouse_buttons[2] = 0x88;
break;
case WM_MBUTTONUP:
input->_mouse_buttons[2] = 0x08;
break;
case WM_MOUSEWHEEL:
input->_mouse_wheel_delta += GET_WHEEL_DELTA_WPARAM(details.wParam) / WHEEL_DELTA;
break;
case WM_XBUTTONDOWN:
assert(2 + HIWORD(details.wParam) < ARRAYSIZE(input->_mouse_buttons));
input->_mouse_buttons[2 + HIWORD(details.wParam)] = 0x88;
break;
case WM_XBUTTONUP:
assert(2 + HIWORD(details.wParam) < ARRAYSIZE(input->_mouse_buttons));
input->_mouse_buttons[2 + HIWORD(details.wParam)] = 0x08;
break;
}
return (is_mouse_message && input->_block_mouse) || (is_keyboard_message && input->_block_keyboard);
}
bool reshade::input::is_key_down(unsigned int keycode) const
{
assert(keycode < ARRAYSIZE(_keys));
return keycode < ARRAYSIZE(_keys) && (_keys[keycode] & 0x80) == 0x80;
}
bool reshade::input::is_key_pressed(unsigned int keycode) const
{
assert(keycode < ARRAYSIZE(_keys));
return keycode < ARRAYSIZE(_keys) && (_keys[keycode] & 0x88) == 0x88;
}
bool reshade::input::is_key_pressed(unsigned int keycode, bool ctrl, bool shift, bool alt) const
{
return is_key_pressed(keycode) && ctrl == is_key_down(VK_CONTROL) && shift == is_key_down(VK_SHIFT) && alt == is_key_down(VK_MENU);
}
bool reshade::input::is_key_released(unsigned int keycode) const
{
assert(keycode < ARRAYSIZE(_keys));
return keycode < ARRAYSIZE(_keys) && (_keys[keycode] & 0x88) == 0x08;
}
bool reshade::input::is_any_key_down() const
{
for (unsigned int i = 0; i < ARRAYSIZE(_keys); i++)
if (is_key_down(i))
return true;
return false;
}
bool reshade::input::is_any_key_pressed() const
{
return last_key_pressed() != 0;
}
bool reshade::input::is_any_key_released() const
{
return last_key_released() != 0;
}
unsigned int reshade::input::last_key_pressed() const
{
for (unsigned int i = 0; i < ARRAYSIZE(_keys); i++)
if (is_key_pressed(i))
return i;
return 0;
}
unsigned int reshade::input::last_key_released() const
{
for (unsigned int i = 0; i < ARRAYSIZE(_keys); i++)
if (is_key_released(i))
return i;
return 0;
}
bool reshade::input::is_mouse_button_down(unsigned int button) const
{
assert(button < ARRAYSIZE(_mouse_buttons));
return button < ARRAYSIZE(_mouse_buttons) && (_mouse_buttons[button] & 0x80) == 0x80;
}
bool reshade::input::is_mouse_button_pressed(unsigned int button) const
{
assert(button < ARRAYSIZE(_mouse_buttons));
return button < ARRAYSIZE(_mouse_buttons) && (_mouse_buttons[button] & 0x88) == 0x88;
}
bool reshade::input::is_mouse_button_released(unsigned int button) const
{
assert(button < ARRAYSIZE(_mouse_buttons));
return button < ARRAYSIZE(_mouse_buttons) && (_mouse_buttons[button] & 0x88) == 0x08;
}
bool reshade::input::is_any_mouse_button_down() const
{
for (unsigned int i = 0; i < ARRAYSIZE(_mouse_buttons); i++)
if (is_mouse_button_down(i))
return true;
return false;
}
bool reshade::input::is_any_mouse_button_pressed() const
{
for (unsigned int i = 0; i < ARRAYSIZE(_mouse_buttons); i++)
if (is_mouse_button_pressed(i))
return true;
return false;
}
bool reshade::input::is_any_mouse_button_released() const
{
for (unsigned int i = 0; i < ARRAYSIZE(_mouse_buttons); i++)
if (is_mouse_button_released(i))
return true;
return false;
}
void reshade::input::block_mouse_input(bool enable)
{
_block_mouse = enable;
// Some applications clip the mouse cursor, so disable that while we want full control over mouse input
if (enable)
ClipCursor(nullptr);
}
void reshade::input::block_keyboard_input(bool enable)
{
_block_keyboard = enable;
}
void reshade::input::next_frame()
{
_frame_count++;
for (auto &state : _keys)
state &= ~0x8;
for (auto &state : _mouse_buttons)
state &= ~0x8;
// Reset any pressed down key states that have not been updated for more than 5 seconds
const DWORD time = GetTickCount();
for (unsigned int i = 0; i < 256; ++i)
if ((_keys[i] & 0x80) != 0 &&
(time - _keys_time[i]) > 5000 &&
(GetAsyncKeyState(i) & 0x8000) == 0)
_keys[i] = 0x08;
_text_input.clear();
_mouse_wheel_delta = 0;
_last_mouse_position[0] = _mouse_position[0];
_last_mouse_position[1] = _mouse_position[1];
// Update caps lock state
_keys[VK_CAPITAL] |= GetKeyState(VK_CAPITAL) & 0x1;
// Update modifier key state
if ((_keys[VK_MENU] & 0x88) != 0 &&
(GetKeyState(VK_MENU) & 0x8000) == 0)
_keys[VK_MENU] = 0x08;
// Update print screen state (there is no key down message, but the key up one is received via the message queue)
if ((_keys[VK_SNAPSHOT] & 0x80) == 0 &&
(GetAsyncKeyState(VK_SNAPSHOT) & 0x8000) != 0)
_keys[VK_SNAPSHOT] = 0x88,
_keys_time[VK_SNAPSHOT] = time;
}
std::string reshade::input::key_name(unsigned int keycode)
{
if (keycode >= 256)
return std::string();
static const char *keyboard_keys_german[256] = {
"", "", "", "Cancel", "", "", "", "", "Backspace", "Tab", "", "", "Clear", "Enter", "", "",
"Shift", "Control", "Alt", "Pause", "Caps Lock", "", "", "", "", "", "", "Escape", "", "", "", "",
"Leertaste", "Bild auf", "Bild ab", "Ende", "Pos 1", "Left Arrow", "Up Arrow", "Right Arrow", "Down Arrow", "Select", "", "", "Druck", "Einfg", "Entf", "Hilfe",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "", "", "", "", "",
"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Left Windows", "Right Windows", "Apps", "", "Sleep",
"Numpad 0", "Numpad 1", "Numpad 2", "Numpad 3", "Numpad 4", "Numpad 5", "Numpad 6", "Numpad 7", "Numpad 8", "Numpad 9", "Numpad *", "Numpad +", "", "Numpad -", "Numpad ,", "Numpad /",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16",
"F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", "", "", "", "", "", "", "", "",
"Num Lock", "Scroll Lock", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"Left Shift", "Right Shift", "Left Control", "Right Control", "Left Menu", "Right Menu", "Browser Back", "Browser Forward", "Browser Refresh", "Browser Stop", "Browser Search", "Browser Favorites", "Browser Home", "Volume Mute", "Volume Down", "Volume Up",
"Next Track", "Previous Track", "Media Stop", "Media Play/Pause", "Mail", "Media Select", "Launch App 1", "Launch App 2", "", "", u8"Ü", "OEM +", "OEM ,", "OEM -", "OEM .", "OEM #",
u8"Ö", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", u8"OEM ß", "OEM ^", u8"OEM ´", u8"Ä", "OEM 8",
"", "", "OEM <", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "Attn", "CrSel", "ExSel", "Erase EOF", "Play", "Zoom", "", "PA1", "OEM Clear", ""
};
static const char *keyboard_keys_international[256] = {
"", "", "", "Cancel", "", "", "", "", "Backspace", "Tab", "", "", "Clear", "Enter", "", "",
"Shift", "Control", "Alt", "Pause", "Caps Lock", "", "", "", "", "", "", "Escape", "", "", "", "",
"Space", "Page Up", "Page Down", "End", "Home", "Left Arrow", "Up Arrow", "Right Arrow", "Down Arrow", "Select", "", "", "Print Screen", "Insert", "Delete", "Help",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "", "", "", "", "",
"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Left Windows", "Right Windows", "Apps", "", "Sleep",
"Numpad 0", "Numpad 1", "Numpad 2", "Numpad 3", "Numpad 4", "Numpad 5", "Numpad 6", "Numpad 7", "Numpad 8", "Numpad 9", "Numpad *", "Numpad +", "", "Numpad -", "Numpad Decimal", "Numpad /",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16",
"F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", "", "", "", "", "", "", "", "",
"Num Lock", "Scroll Lock", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"Left Shift", "Right Shift", "Left Control", "Right Control", "Left Menu", "Right Menu", "Browser Back", "Browser Forward", "Browser Refresh", "Browser Stop", "Browser Search", "Browser Favorites", "Browser Home", "Volume Mute", "Volume Down", "Volume Up",
"Next Track", "Previous Track", "Media Stop", "Media Play/Pause", "Mail", "Media Select", "Launch App 1", "Launch App 2", "", "", "OEM ;", "OEM +", "OEM ,", "OEM -", "OEM .", "OEM /",
"OEM ~", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "OEM [", "OEM \\", "OEM ]", "OEM '", "OEM 8",
"", "", "OEM <", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "Attn", "CrSel", "ExSel", "Erase EOF", "Play", "Zoom", "", "PA1", "OEM Clear", ""
};
const LANGID language = LOWORD(GetKeyboardLayout(0));
return ((language & 0xFF) == LANG_GERMAN) ?
keyboard_keys_german[keycode] : keyboard_keys_international[keycode];
}
std::string reshade::input::key_name(const unsigned int key[4])
{
return (key[1] ? "Ctrl + " : std::string()) + (key[2] ? "Shift + " : std::string()) + (key[3] ? "Alt + " : std::string()) + key_name(key[0]);
}
static inline bool is_blocking_mouse_input()
{
const auto predicate = [](auto input_window) {
return !input_window.second.expired() && input_window.second.lock()->is_blocking_mouse_input();
};
return std::any_of(s_windows.cbegin(), s_windows.cend(), predicate);
}
static inline bool is_blocking_keyboard_input()
{
const auto predicate = [](auto input_window) {
return !input_window.second.expired() && input_window.second.lock()->is_blocking_keyboard_input();
};
return std::any_of(s_windows.cbegin(), s_windows.cend(), predicate);
}
HOOK_EXPORT BOOL WINAPI HookGetMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax)
{
static const auto trampoline = reshade::hooks::call(HookGetMessageA);
if (!trampoline(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax))
return FALSE;
assert(lpMsg != nullptr);
if (lpMsg->hwnd != nullptr && reshade::input::handle_window_message(lpMsg))
{
// We still want 'WM_CHAR' messages, so translate message
TranslateMessage(lpMsg);
// Change message so it is ignored by the recipient window
lpMsg->message = WM_NULL;
}
return TRUE;
}
HOOK_EXPORT BOOL WINAPI HookGetMessageW(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax)
{
static const auto trampoline = reshade::hooks::call(HookGetMessageW);
if (!trampoline(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax))
return FALSE;
assert(lpMsg != nullptr);
if (lpMsg->hwnd != nullptr && reshade::input::handle_window_message(lpMsg))
{
// We still want 'WM_CHAR' messages, so translate message
TranslateMessage(lpMsg);
// Change message so it is ignored by the recipient window
lpMsg->message = WM_NULL;
}
return TRUE;
}
HOOK_EXPORT BOOL WINAPI HookPeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
{
static const auto trampoline = reshade::hooks::call(HookPeekMessageA);
if (!trampoline(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg))
return FALSE;
assert(lpMsg != nullptr);
if (lpMsg->hwnd != nullptr && (wRemoveMsg & PM_REMOVE) != 0 && reshade::input::handle_window_message(lpMsg))
{
// We still want 'WM_CHAR' messages, so translate message
TranslateMessage(lpMsg);
// Change message so it is ignored by the recipient window
lpMsg->message = WM_NULL;
}
return TRUE;
}
HOOK_EXPORT BOOL WINAPI HookPeekMessageW(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
{
static const auto trampoline = reshade::hooks::call(HookPeekMessageW);
if (!trampoline(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg))
return FALSE;
assert(lpMsg != nullptr);
if (lpMsg->hwnd != nullptr && (wRemoveMsg & PM_REMOVE) != 0 && reshade::input::handle_window_message(lpMsg))
{
// We still want 'WM_CHAR' messages, so translate message
TranslateMessage(lpMsg);
// Change message so it is ignored by the recipient window
lpMsg->message = WM_NULL;
}
return TRUE;
}
HOOK_EXPORT BOOL WINAPI HookPostMessageA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
// Do not allow mouse movement simulation while we block input
if (is_blocking_mouse_input() && Msg == WM_MOUSEMOVE)
return TRUE;
static const auto trampoline = reshade::hooks::call(HookPostMessageA);
return trampoline(hWnd, Msg, wParam, lParam);
}
HOOK_EXPORT BOOL WINAPI HookPostMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (is_blocking_mouse_input() && Msg == WM_MOUSEMOVE)
return TRUE;
static const auto trampoline = reshade::hooks::call(HookPostMessageW);
return trampoline(hWnd, Msg, wParam, lParam);
}
HOOK_EXPORT BOOL WINAPI HookRegisterRawInputDevices(PCRAWINPUTDEVICE pRawInputDevices, UINT uiNumDevices, UINT cbSize)
{
#if RESHADE_VERBOSE_LOG
LOG(DEBUG) << "Redirecting RegisterRawInputDevices" << '(' << "pRawInputDevices = " << pRawInputDevices << ", uiNumDevices = " << uiNumDevices << ", cbSize = " << cbSize << ')' << " ...";
#endif
for (UINT i = 0; i < uiNumDevices; ++i)
{
const auto &device = pRawInputDevices[i];
#if RESHADE_VERBOSE_LOG
LOG(DEBUG) << "> Dumping device registration at index " << i << ":";
LOG(DEBUG) << " +-----------------------------------------+-----------------------------------------+";
LOG(DEBUG) << " | Parameter | Value |";
LOG(DEBUG) << " +-----------------------------------------+-----------------------------------------+";
LOG(DEBUG) << " | UsagePage | " << std::setw(39) << std::hex << device.usUsagePage << std::dec << " |";
LOG(DEBUG) << " | Usage | " << std::setw(39) << std::hex << device.usUsage << std::dec << " |";
LOG(DEBUG) << " | Flags | " << std::setw(39) << std::hex << device.dwFlags << std::dec << " |";
LOG(DEBUG) << " | TargetWindow | " << std::setw(39) << device.hwndTarget << " |";
LOG(DEBUG) << " +-----------------------------------------+-----------------------------------------+";
#endif
if (device.usUsagePage != 1 || device.hwndTarget == nullptr)
continue;
reshade::input::register_window_with_raw_input(device.hwndTarget, device.usUsage == 0x06 && (device.dwFlags & RIDEV_NOLEGACY) != 0, device.usUsage == 0x02 && (device.dwFlags & RIDEV_NOLEGACY) != 0);
}
if (!reshade::hooks::call(HookRegisterRawInputDevices)(pRawInputDevices, uiNumDevices, cbSize))
{
LOG(WARN) << "RegisterRawInputDevices failed with error code " << GetLastError() << '!';
return FALSE;
}
return TRUE;
}
static POINT last_cursor_position = {};
HOOK_EXPORT BOOL WINAPI HookSetCursorPosition(int X, int Y)
{
last_cursor_position.x = X;
last_cursor_position.y = Y;
if (is_blocking_mouse_input())
return TRUE;
static const auto trampoline = reshade::hooks::call(HookSetCursorPosition);
return trampoline(X, Y);
}
HOOK_EXPORT BOOL WINAPI HookGetCursorPosition(LPPOINT lpPoint)
{
if (is_blocking_mouse_input())
{
assert(lpPoint != nullptr);
// Just return the last cursor position before we started to block mouse input, to stop it from moving
*lpPoint = last_cursor_position;
return TRUE;
}
static const auto trampoline = reshade::hooks::call(HookGetCursorPosition);
return trampoline(lpPoint);
}