forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser32.cpp
More file actions
251 lines (191 loc) · 7.09 KB
/
Copy pathuser32.cpp
File metadata and controls
251 lines (191 loc) · 7.09 KB
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
/**
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#include "log.hpp"
#include "hook_manager.hpp"
#include "input.hpp"
#include <assert.h>
#include <Windows.h>
HOOK_EXPORT ATOM WINAPI HookRegisterClassA(const WNDCLASSA *lpWndClass)
{
assert(lpWndClass != nullptr);
auto wndclass = *lpWndClass;
if (wndclass.hInstance == GetModuleHandle(nullptr))
{
LOG(INFO) << "Redirecting '" << "RegisterClassA" << "(" << lpWndClass << ")' ...";
if ((wndclass.style & CS_OWNDC) == 0)
{
LOG(INFO) << "> Adding 'CS_OWNDC' window class style flag to '" << wndclass.lpszClassName << "'.";
wndclass.style |= CS_OWNDC;
}
}
return reshade::hooks::call(&HookRegisterClassA)(&wndclass);
}
HOOK_EXPORT ATOM WINAPI HookRegisterClassW(const WNDCLASSW *lpWndClass)
{
assert(lpWndClass != nullptr);
auto wndclass = *lpWndClass;
if (wndclass.hInstance == GetModuleHandle(nullptr))
{
LOG(INFO) << "Redirecting '" << "RegisterClassW" << "(" << lpWndClass << ")' ...";
if ((wndclass.style & CS_OWNDC) == 0)
{
LOG(INFO) << "> Adding 'CS_OWNDC' window class style flag to '" << wndclass.lpszClassName << "'.";
wndclass.style |= CS_OWNDC;
}
}
return reshade::hooks::call(&HookRegisterClassW)(&wndclass);
}
HOOK_EXPORT ATOM WINAPI HookRegisterClassExA(const WNDCLASSEXA *lpWndClassEx)
{
assert(lpWndClassEx != nullptr);
auto wndclass = *lpWndClassEx;
if (wndclass.hInstance == GetModuleHandle(nullptr))
{
LOG(INFO) << "Redirecting '" << "RegisterClassExA" << "(" << lpWndClassEx << ")' ...";
if ((wndclass.style & CS_OWNDC) == 0)
{
LOG(INFO) << "> Adding 'CS_OWNDC' window class style flag to '" << wndclass.lpszClassName << "'.";
wndclass.style |= CS_OWNDC;
}
}
return reshade::hooks::call(&HookRegisterClassExA)(&wndclass);
}
HOOK_EXPORT ATOM WINAPI HookRegisterClassExW(const WNDCLASSEXW *lpWndClassEx)
{
assert(lpWndClassEx != nullptr);
auto wndclass = *lpWndClassEx;
if (wndclass.hInstance == GetModuleHandle(nullptr))
{
LOG(INFO) << "Redirecting '" << "RegisterClassExW" << "(" << lpWndClassEx << ")' ...";
if ((wndclass.style & CS_OWNDC) == 0)
{
LOG(INFO) << "> Adding 'CS_OWNDC' window class style flag to '" << wndclass.lpszClassName << "'.";
wndclass.style |= CS_OWNDC;
}
}
return reshade::hooks::call(&HookRegisterClassExW)(&wndclass);
}
HOOK_EXPORT BOOL WINAPI HookRegisterRawInputDevices(PCRAWINPUTDEVICE pRawInputDevices, UINT uiNumDevices, UINT cbSize)
{
LOG(INFO) << "Redirecting '" << "RegisterRawInputDevices" << "(" << pRawInputDevices << ", " << uiNumDevices << ", " << cbSize << ")' ...";
for (UINT i = 0; i < uiNumDevices; ++i)
{
const auto &device = pRawInputDevices[i];
LOG(INFO) << "> Dumping device registration at index " << i << ":";
LOG(INFO) << " +-----------------------------------------+-----------------------------------------+";
LOG(INFO) << " | Parameter | Value |";
LOG(INFO) << " +-----------------------------------------+-----------------------------------------+";
LOG(INFO) << " | UsagePage | " << std::setw(39) << std::hex << device.usUsagePage << std::dec << " |";
LOG(INFO) << " | Usage | " << std::setw(39) << std::hex << device.usUsage << std::dec << " |";
LOG(INFO) << " | Flags | " << std::setw(39) << std::hex << device.dwFlags << std::dec << " |";
LOG(INFO) << " | TargetWindow | " << std::setw(39) << device.hwndTarget << " |";
LOG(INFO) << " +-----------------------------------------+-----------------------------------------+";
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(WARNING) << "> 'RegisterRawInputDevices' failed with error code " << GetLastError() << "!";
return FALSE;
}
return TRUE;
}
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))
{
// 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))
{
// 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))
{
// 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))
{
// Change message so it is ignored by the recipient window
lpMsg->message = WM_NULL;
}
return TRUE;
}
POINT last_cursor_position = { };
std::shared_ptr<reshade::input> last_input;
HOOK_EXPORT BOOL WINAPI HookSetCursorPosition(int X, int Y)
{
const HWND hwnd = GetActiveWindow();
if (hwnd != nullptr)
{
last_input = reshade::input::register_window(hwnd);
if (last_input->is_blocking_mouse_input())
{
last_cursor_position.x = X;
last_cursor_position.y = Y;
return TRUE;
}
}
static const auto trampoline = reshade::hooks::call(&HookSetCursorPosition);
return trampoline(X, Y);
}
HOOK_EXPORT BOOL WINAPI HookGetCursorPosition(LPPOINT lpPoint)
{
const HWND hwnd = GetActiveWindow();
if (hwnd != nullptr)
{
last_input = reshade::input::register_window(hwnd);
if (last_input->is_blocking_mouse_input())
{
assert(lpPoint != nullptr);
*lpPoint = last_cursor_position;
return TRUE;
}
}
static const auto trampoline = reshade::hooks::call(&HookGetCursorPosition);
return trampoline(lpPoint);
}