forked from crosire/reshade
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.cpp
More file actions
Latest commit
393 lines (336 loc) · 9.61 KB
/
Copy pathinput.cpp
File metadata and controls
393 lines (336 loc) · 9.61 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
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
/**
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#include"log.hpp"
#include"input.hpp"
#include<Windows.h>
#include<assert.h>
#include<mutex>
#include<unordered_map>
namespacereshade
{
static std::mutex s_mutex;
static std::unordered_map<HWND, unsignedint> s_raw_input_windows;
static std::unordered_map<HWND, std::weak_ptr<input>> s_windows;
input::input(window_handle window) : _window(window)
{
assert(window != nullptr);
}
voidinput::register_window_with_raw_input(window_handle window, bool no_legacy_keyboard, bool no_legacy_mouse)
{
const std::lock_guard<std::mutex> lock(s_mutex);
constauto flags = (no_legacy_keyboard ? 0x1 : 0u) | (no_legacy_mouse ? 0x2 : 0u);
constauto insert = s_raw_input_windows.emplace(static_cast<HWND>(window), flags);
if (!insert.second)
{
insert.first->second |= flags;
}
}
std::shared_ptr<input> input::register_window(window_handle window)
{
constHWND parent = GetParent(static_cast<HWND>(window));
if (parent != nullptr)
{
window = parent;
}
const std::lock_guard<std::mutex> lock(s_mutex);
constauto insert = s_windows.emplace(static_cast<HWND>(window), std::weak_ptr<input>());
if (insert.second || insert.first->second.expired())
{
LOG(INFO) << "Starting input capture for window " << window << " ...";
constauto instance = std::make_shared<input>(window);
insert.first->second = instance;
return instance;
}
else
{
return insert.first->second.lock();
}
}
voidinput::uninstall()
{
s_windows.clear();
s_raw_input_windows.clear();
}
boolinput::handle_window_message(constvoid *message_data)
{
assert(message_data != nullptr);
MSG details = *static_cast<constMSG *>(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;
if (details.message != WM_INPUT && !is_mouse_message && !is_keyboard_message)
{
returnfalse;
}
constHWND parent = GetParent(details.hwnd);
if (parent != nullptr)
{
details.hwnd = parent;
}
const std::lock_guard<std::mutex> lock(s_mutex);
auto input_window = s_windows.find(details.hwnd);
constauto raw_input_window = s_raw_input_windows.find(details.hwnd);
if (input_window == s_windows.end() && raw_input_window != s_raw_input_windows.end())
{
// This is a raw input message. Just reroute it to the first window for now, since it is rare to have more than one active at a time.
input_window = s_windows.begin();
}
if (input_window == s_windows.end())
{
returnfalse;
}
elseif (input_window->second.expired())
{
s_windows.erase(input_window);
returnfalse;
}
RAWINPUT raw_data = { };
UINT raw_data_size = sizeof(raw_data);
constauto input_lock = input_window->second.lock();
input &input = *input_lock;
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)
{
caseWM_INPUT:
if (GET_RAWINPUT_CODE_WPARAM(details.wParam) != RIM_INPUT ||
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)
{
caseRIM_TYPEMOUSE:
is_mouse_message = true;
if (raw_input_window != s_raw_input_windows.end() && (raw_input_window->second & 0x2) == 0)
break;
if (raw_data.data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN)
input._mouse_buttons[0] = 0x88;
elseif (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;
elseif (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;
elseif (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;
elseif (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;
elseif (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;
caseRIM_TYPEKEYBOARD:
is_keyboard_message = true;
if (raw_input_window != s_raw_input_windows.end() && (raw_input_window->second & 0x1) == 0)
break;
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;
break;
}
break;
caseWM_KEYDOWN:
caseWM_SYSKEYDOWN:
input._keys[details.wParam] = 0x88;
break;
caseWM_KEYUP:
caseWM_SYSKEYUP:
input._keys[details.wParam] = 0x08;
break;
caseWM_LBUTTONDOWN:
input._mouse_buttons[0] = 0x88;
break;
caseWM_LBUTTONUP:
input._mouse_buttons[0] = 0x08;
break;
caseWM_RBUTTONDOWN:
input._mouse_buttons[1] = 0x88;
break;
caseWM_RBUTTONUP:
input._mouse_buttons[1] = 0x08;
break;
caseWM_MBUTTONDOWN:
input._mouse_buttons[2] = 0x88;
break;
caseWM_MBUTTONUP:
input._mouse_buttons[2] = 0x08;
break;
caseWM_MOUSEWHEEL:
input._mouse_wheel_delta += GET_WHEEL_DELTA_WPARAM(details.wParam) / WHEEL_DELTA;
break;
caseWM_XBUTTONDOWN:
assert(HIWORD(details.wParam) < 3);
input._mouse_buttons[2 + HIWORD(details.wParam)] = 0x88;
break;
caseWM_XBUTTONUP:
assert(HIWORD(details.wParam) < 3);
input._mouse_buttons[2 + HIWORD(details.wParam)] = 0x08;
break;
}
return (is_mouse_message && input._block_mouse) || (is_keyboard_message && input._block_keyboard);
}
boolinput::is_key_down(unsignedint keycode) const
{
assert(keycode < 256);
return (_keys[keycode] & 0x80) == 0x80;
}
boolinput::is_key_down(unsignedint keycode, bool ctrl, bool shift, bool alt) const
{
returnis_key_down(keycode) && (!ctrl || is_key_down(VK_CONTROL)) && (!shift || is_key_down(VK_SHIFT)) && (!alt || is_key_down(VK_MENU));
}
boolinput::is_key_pressed(unsignedint keycode) const
{
assert(keycode < 256);
return (_keys[keycode] & 0x88) == 0x88;
}
boolinput::is_key_pressed(unsignedint keycode, bool ctrl, bool shift, bool alt) const
{
returnis_key_pressed(keycode) && (!ctrl || is_key_down(VK_CONTROL)) && (!shift || is_key_down(VK_SHIFT)) && (!alt || is_key_down(VK_MENU));
}
boolinput::is_key_released(unsignedint keycode) const
{
assert(keycode < 256);
return (_keys[keycode] & 0x88) == 0x08;
}
boolinput::is_any_key_down() const
{
for (unsignedint i = 0; i < 256; i++)
{
if (is_key_down(i))
{
returntrue;
}
}
returnfalse;
}
boolinput::is_any_key_pressed() const
{
returnlast_key_pressed() != 0;
}
boolinput::is_any_key_released() const
{
returnlast_key_released() != 0;
}
unsignedintinput::last_key_pressed() const
{
for (unsignedint i = 0; i < 256; i++)
{
if (is_key_pressed(i))
{
return i;
}
}
return0;
}
unsignedintinput::last_key_released() const
{
for (unsignedint i = 0; i < 256; i++)
{
if (is_key_released(i))
{
return i;
}
}
return0;
}
boolinput::is_mouse_button_down(unsignedint button) const
{
assert(button < 5);
return (_mouse_buttons[button] & 0x80) == 0x80;
}
boolinput::is_mouse_button_pressed(unsignedint button) const
{
assert(button < 5);
return (_mouse_buttons[button] & 0x88) == 0x88;
}
boolinput::is_mouse_button_released(unsignedint button) const
{
assert(button < 5);
return (_mouse_buttons[button] & 0x88) == 0x08;
}
boolinput::is_any_mouse_button_down() const
{
for (unsignedint i = 0; i < 5; i++)
{
if (is_mouse_button_down(i))
{
returntrue;
}
}
returnfalse;
}
boolinput::is_any_mouse_button_pressed() const
{
for (unsignedint i = 0; i < 5; i++)
{
if (is_mouse_button_pressed(i))
{
returntrue;
}
}
returnfalse;
}
boolinput::is_any_mouse_button_released() const
{
for (unsignedint i = 0; i < 5; i++)
{
if (is_mouse_button_released(i))
{
returntrue;
}
}
returnfalse;
}
unsignedshortinput::key_to_text(unsignedint keycode) const
{
WORD ch = 0;
returnToAscii(keycode, MapVirtualKey(keycode, MAPVK_VK_TO_VSC), _keys, &ch, 0) ? ch : 0;
}
voidinput::block_mouse_input(bool enable)
{
_block_mouse = enable;
if (enable)
{
ClipCursor(nullptr);
}
}
voidinput::block_keyboard_input(bool enable)
{
_block_keyboard = enable;
}
voidinput::next_frame()
{
for (auto &state : _keys)
{
state &= ~0x8;
}
for (auto &state : _mouse_buttons)
{
state &= ~0x8;
}
_mouse_wheel_delta = 0;
// 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
if ((_keys[VK_SNAPSHOT] & 0x80) == 0 &&
(GetAsyncKeyState(VK_SNAPSHOT) & 0x8000) != 0)
{
_keys[VK_SNAPSHOT] = 0x88;
}
}
}