forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 11
/
input.cpp
684 lines (581 loc) · 27 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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
* Copyright (C) 2014 Patrick Mours
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "input.hpp"
#include "dll_log.hpp"
#include "hook_manager.hpp"
#include <algorithm>
#include <unordered_map>
#include <Windows.h>
extern bool is_uwp_app();
extern HMODULE g_module_handle;
static std::shared_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)
{
}
void reshade::input::register_window_with_raw_input(window_handle window, bool no_legacy_keyboard, bool no_legacy_mouse)
{
if (is_uwp_app()) // UWP apps never use legacy input messages
no_legacy_keyboard = no_legacy_mouse = true;
assert(window != nullptr);
const std::unique_lock<std::shared_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)
{
assert(window != nullptr);
DWORD process_id = 0;
GetWindowThreadProcessId(static_cast<HWND>(window), &process_id);
if (process_id != GetCurrentProcessId())
{
LOG(WARN) << "Cannot capture input for window " << window << " created by a different process.";
return nullptr;
}
const std::unique_lock<std::shared_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::shared_mutex> lock(s_windows_mutex);
// Remove any expired entry from the list
for (auto it = s_windows.begin(); it != s_windows.end();)
if (it->second.expired())
it = s_windows.erase(it);
else
++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;
const std::shared_ptr<input> input = input_window->second.lock();
// It may happen that the input was destroyed between the removal of expired entries above and here, so need to abort in this case
if (input == nullptr)
return false;
// 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();
// Calculate window client mouse position
ScreenToClient(static_cast<HWND>(input->_window), &details.pt);
// Prevent input threads from modifying input while it is accessed elsewhere
const std::unique_lock<std::shared_mutex> input_lock(input->_mutex);
input->_mouse_position[0] = details.pt.x;
input->_mouse_position[1] = details.pt.y;
switch (details.message)
{
case WM_INPUT:
RAWINPUT raw_data;
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->_keys[VK_LBUTTON] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP)
input->_keys[VK_LBUTTON] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN)
input->_keys[VK_RBUTTON] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP)
input->_keys[VK_RBUTTON] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_MIDDLE_BUTTON_DOWN)
input->_keys[VK_MBUTTON] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_MIDDLE_BUTTON_UP)
input->_keys[VK_MBUTTON] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_DOWN)
input->_keys[VK_XBUTTON1] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_UP)
input->_keys[VK_XBUTTON1] = 0x08;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_DOWN)
input->_keys[VK_XBUTTON2] = 0x88;
else if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_UP)
input->_keys[VK_XBUTTON2] = 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:
if (raw_data.data.keyboard.VKey == 0)
break; // Ignore messages without a valid key code
is_keyboard_message = true;
// Do not block key up messages if the key down one was not blocked previously
if (input->_block_keyboard && (raw_data.data.keyboard.Flags & RI_KEY_BREAK) != 0 && raw_data.data.keyboard.VKey < 0xFF && (input->_keys[raw_data.data.keyboard.VKey] & 0x04) == 0)
is_keyboard_message = false;
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
// Filter out prefix messages without a key code
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 ToUnicode 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
// Since Windows 10 version 1607 this supports the 0x2 flag, which prevents the keyboard state from being changed, so it is not a problem there anymore either way
if (WCHAR ch[3] = {}; (raw_data.data.keyboard.Flags & RI_KEY_BREAK) == 0 && ToUnicode(raw_data.data.keyboard.VKey, raw_data.data.keyboard.MakeCode, input->_keys, ch, 2, 0x2))
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 > 0 && details.wParam < ARRAYSIZE(input->_keys));
input->_keys[details.wParam] = 0x88;
input->_keys_time[details.wParam] = details.time;
if (input->_block_keyboard)
input->_keys[details.wParam] |= 0x04;
break;
case WM_KEYUP:
case WM_SYSKEYUP:
assert(details.wParam > 0 && details.wParam < ARRAYSIZE(input->_keys));
// Do not block key up messages if the key down one was not blocked previously (so key does not get stuck for the application)
if (input->_block_keyboard && (input->_keys[details.wParam] & 0x04) == 0)
is_keyboard_message = false;
input->_keys[details.wParam] = 0x08;
input->_keys_time[details.wParam] = details.time;
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK: // Double clicking generates this sequence: WM_LBUTTONDOWN -> WM_LBUTTONUP -> WM_LBUTTONDBLCLK -> WM_LBUTTONUP, so handle it like a normal down
input->_keys[VK_LBUTTON] = 0x88;
break;
case WM_LBUTTONUP:
input->_keys[VK_LBUTTON] = 0x08;
break;
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
input->_keys[VK_RBUTTON] = 0x88;
break;
case WM_RBUTTONUP:
input->_keys[VK_RBUTTON] = 0x08;
break;
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
input->_keys[VK_MBUTTON] = 0x88;
break;
case WM_MBUTTONUP:
input->_keys[VK_MBUTTON] = 0x08;
break;
case WM_MOUSEWHEEL:
input->_mouse_wheel_delta += GET_WHEEL_DELTA_WPARAM(details.wParam) / WHEEL_DELTA;
break;
case WM_XBUTTONDOWN:
assert(HIWORD(details.wParam) == XBUTTON1 || HIWORD(details.wParam) == XBUTTON2);
input->_keys[VK_XBUTTON1 + (HIWORD(details.wParam) - XBUTTON1)] = 0x88;
break;
case WM_XBUTTONUP:
assert(HIWORD(details.wParam) == XBUTTON1 || HIWORD(details.wParam) == XBUTTON2);
input->_keys[VK_XBUTTON1 + (HIWORD(details.wParam) - XBUTTON1)] = 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 > 0 && keycode < ARRAYSIZE(_keys) && (_keys[keycode] & 0x88) == 0x88;
}
bool reshade::input::is_key_pressed(unsigned int keycode, bool ctrl, bool shift, bool alt, bool force_modifiers) const
{
if (keycode == 0)
return false;
const bool key_down = is_key_pressed(keycode), ctrl_down = is_key_down(VK_CONTROL), shift_down = is_key_down(VK_SHIFT), alt_down = is_key_down(VK_MENU);
if (force_modifiers) // Modifier state is required to match
return key_down && (ctrl == ctrl_down && shift == shift_down && alt == alt_down);
else // Modifier state is optional and only has to match when down
return key_down && (!ctrl || ctrl_down) && (!shift || shift_down) && (!alt || alt_down);
}
bool reshade::input::is_key_released(unsigned int keycode) const
{
assert(keycode < ARRAYSIZE(_keys));
return keycode > 0 && keycode < ARRAYSIZE(_keys) && (_keys[keycode] & 0x88) == 0x08;
}
bool reshade::input::is_any_key_down() const
{
// Skip mouse buttons
for (unsigned int i = VK_XBUTTON2 + 1; 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 = VK_XBUTTON2 + 1; 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 = VK_XBUTTON2 + 1; 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 < 5);
return is_key_down(VK_LBUTTON + button + (button < 2 ? 0 : 1)); // VK_CANCEL is being ignored by runtime
}
bool reshade::input::is_mouse_button_pressed(unsigned int button) const
{
assert(button < 5);
return is_key_pressed(VK_LBUTTON + button + (button < 2 ? 0 : 1)); // VK_CANCEL is being ignored by runtime
}
bool reshade::input::is_mouse_button_released(unsigned int button) const
{
assert(button < 5);
return is_key_released(VK_LBUTTON + button + (button < 2 ? 0 : 1)); // VK_CANCEL is being ignored by runtime
}
bool reshade::input::is_any_mouse_button_down() const
{
for (unsigned int i = 0; i < 5; 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 < 5; 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 < 5; i++)
if (is_mouse_button_released(i))
return true;
return false;
}
void reshade::input::max_mouse_position(unsigned int position[2]) const
{
RECT rect = {};
GetClientRect(static_cast<HWND>(_window), &rect);
position[0] = rect.right;
position[1] = rect.bottom;
}
void reshade::input::next_frame()
{
_frame_count++;
for (uint8_t &state : _keys)
state &= ~0x08;
// Reset any pressed down key states (apart from mouse buttons) that have not been updated for more than 5 seconds
// Do not check mouse buttons here, since 'GetAsyncKeyState' always returns the state of the physical mouse buttons, not the logical ones in case they were remapped
// See https://docs.microsoft.com/windows/win32/api/winuser/nf-winuser-getasynckeystate
// And time is not tracked for mouse buttons anyway
const DWORD time = GetTickCount();
for (unsigned int i = 8; 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] = {
"", "Left Mouse", "Right Mouse", "Cancel", "Middle Mouse", "X1 Mouse", "X2 Mouse", "", "Backspace", "Tab", "", "", "Clear", "Eingabe", "", "",
"Shift", "Strg", "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", "Rollen", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"Shift links", "Shift rechts", "Strg links", "Strg rechts", "Alt links", "Alt rechts", "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] = {
"", "Left Mouse", "Right Mouse", "Cancel", "Middle Mouse", "X1 Mouse", "X2 Mouse", "", "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])
{
assert(key[0] != VK_CONTROL && key[0] != VK_SHIFT && key[0] != VK_MENU);
return (key[1] ? "Ctrl + " : std::string()) + (key[2] ? "Shift + " : std::string()) + (key[3] ? "Alt + " : std::string()) + key_name(key[0]);
}
void reshade::input::block_mouse_input(bool enable)
{
_block_mouse = enable;
// Some games setup ClipCursor with a tiny area which could make the cursor stay in that area instead of the whole window
if (enable)
ClipCursor(nullptr);
}
void reshade::input::block_keyboard_input(bool enable)
{
_block_keyboard = enable;
}
bool is_blocking_mouse_input()
{
const std::shared_lock<std::shared_mutex> lock(s_windows_mutex);
const auto predicate = [](const std::pair<HWND, std::weak_ptr<reshade::input>> &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);
}
bool is_blocking_keyboard_input()
{
const std::shared_lock<std::shared_mutex> lock(s_windows_mutex);
const auto predicate = [](const std::pair<HWND, std::weak_ptr<reshade::input>> &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);
}
extern "C" BOOL WINAPI HookGetMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax)
{
#if 1
// Implement 'GetMessage' with a timeout (see also DLL_PROCESS_DETACH in dllmain.cpp for more explanation)
while (!PeekMessageA(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, PM_REMOVE) && g_module_handle != nullptr)
MsgWaitForMultipleObjects(0, nullptr, FALSE, 500, QS_ALLINPUT);
if (g_module_handle == nullptr && lpMsg->message != WM_QUIT)
std::memset(lpMsg, 0, sizeof(MSG)); // Clear message structure, so application does not process it
#else
static const auto trampoline = reshade::hooks::call(HookGetMessageA);
const BOOL result = trampoline(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
if (result < 0) // If there is an error, the return value is negative (https://docs.microsoft.com/windows/win32/api/winuser/nf-winuser-getmessage)
return result;
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;
}
#endif
return lpMsg->message != WM_QUIT;
}
extern "C" BOOL WINAPI HookGetMessageW(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax)
{
#if 1
while (!PeekMessageW(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, PM_REMOVE) && g_module_handle != nullptr)
MsgWaitForMultipleObjects(0, nullptr, FALSE, 500, QS_ALLINPUT);
if (g_module_handle == nullptr && lpMsg->message != WM_QUIT)
std::memset(lpMsg, 0, sizeof(MSG));
#else
static const auto trampoline = reshade::hooks::call(HookGetMessageW);
const BOOL result = trampoline(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
if (result < 0)
return result;
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;
}
#endif
return lpMsg->message != WM_QUIT;
}
extern "C" 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;
}
extern "C" 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;
}
extern "C" 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);
}
extern "C" 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);
}
extern "C" 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 RAWINPUTDEVICE &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);
}
static const auto trampoline = reshade::hooks::call(HookRegisterRawInputDevices);
if (!trampoline(pRawInputDevices, uiNumDevices, cbSize))
{
LOG(WARN) << "RegisterRawInputDevices" << " failed with error code " << GetLastError() << '.';
return FALSE;
}
return TRUE;
}
static POINT s_last_cursor_position = {};
extern "C" BOOL WINAPI HookClipCursor(const RECT *lpRect)
{
if (is_blocking_mouse_input())
// Some applications clip the mouse cursor, so disable that while we want full control over mouse input
lpRect = nullptr;
static const auto trampoline = reshade::hooks::call(HookClipCursor);
return trampoline(lpRect);
}
extern "C" BOOL WINAPI HookSetCursorPosition(int X, int Y)
{
s_last_cursor_position.x = X;
s_last_cursor_position.y = Y;
if (is_blocking_mouse_input())
return TRUE;
static const auto trampoline = reshade::hooks::call(HookSetCursorPosition);
return trampoline(X, Y);
}
extern "C" 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 = s_last_cursor_position;
return TRUE;
}
static const auto trampoline = reshade::hooks::call(HookGetCursorPosition);
return trampoline(lpPoint);
}