forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
393 lines (336 loc) · 9.61 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
/**
* 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>
namespace reshade
{
static std::mutex s_mutex;
static std::unordered_map<HWND, unsigned int> 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);
}
void input::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);
const auto flags = (no_legacy_keyboard ? 0x1 : 0u) | (no_legacy_mouse ? 0x2 : 0u);
const auto 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)
{
const HWND parent = GetParent(static_cast<HWND>(window));
if (parent != nullptr)
{
window = parent;
}
const std::lock_guard<std::mutex> lock(s_mutex);
const auto 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 << " ...";
const auto instance = std::make_shared<input>(window);
insert.first->second = instance;
return instance;
}
else
{
return insert.first->second.lock();
}
}
void input::uninstall()
{
s_windows.clear();
s_raw_input_windows.clear();
}
bool 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;
if (details.message != WM_INPUT && !is_mouse_message && !is_keyboard_message)
{
return false;
}
const HWND 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);
const auto 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())
{
return false;
}
else if (input_window->second.expired())
{
s_windows.erase(input_window);
return false;
}
RAWINPUT raw_data = { };
UINT raw_data_size = sizeof(raw_data);
const auto 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)
{
case WM_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)
{
case RIM_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;
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;
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;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
input._keys[details.wParam] = 0x88;
break;
case WM_KEYUP:
case WM_SYSKEYUP:
input._keys[details.wParam] = 0x08;
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(HIWORD(details.wParam) < 3);
input._mouse_buttons[2 + HIWORD(details.wParam)] = 0x88;
break;
case WM_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);
}
bool input::is_key_down(unsigned int keycode) const
{
assert(keycode < 256);
return (_keys[keycode] & 0x80) == 0x80;
}
bool input::is_key_down(unsigned int keycode, bool ctrl, bool shift, bool alt) const
{
return is_key_down(keycode) && (!ctrl || is_key_down(VK_CONTROL)) && (!shift || is_key_down(VK_SHIFT)) && (!alt || is_key_down(VK_MENU));
}
bool input::is_key_pressed(unsigned int keycode) const
{
assert(keycode < 256);
return (_keys[keycode] & 0x88) == 0x88;
}
bool 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 input::is_key_released(unsigned int keycode) const
{
assert(keycode < 256);
return (_keys[keycode] & 0x88) == 0x08;
}
bool input::is_any_key_down() const
{
for (unsigned int i = 0; i < 256; i++)
{
if (is_key_down(i))
{
return true;
}
}
return false;
}
bool input::is_any_key_pressed() const
{
return last_key_pressed() != 0;
}
bool input::is_any_key_released() const
{
return last_key_released() != 0;
}
unsigned int input::last_key_pressed() const
{
for (unsigned int i = 0; i < 256; i++)
{
if (is_key_pressed(i))
{
return i;
}
}
return 0;
}
unsigned int input::last_key_released() const
{
for (unsigned int i = 0; i < 256; i++)
{
if (is_key_released(i))
{
return i;
}
}
return 0;
}
bool input::is_mouse_button_down(unsigned int button) const
{
assert(button < 5);
return (_mouse_buttons[button] & 0x80) == 0x80;
}
bool input::is_mouse_button_pressed(unsigned int button) const
{
assert(button < 5);
return (_mouse_buttons[button] & 0x88) == 0x88;
}
bool input::is_mouse_button_released(unsigned int button) const
{
assert(button < 5);
return (_mouse_buttons[button] & 0x88) == 0x08;
}
bool 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 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 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;
}
unsigned short input::key_to_text(unsigned int keycode) const
{
WORD ch = 0;
return ToAscii(keycode, MapVirtualKey(keycode, MAPVK_VK_TO_VSC), _keys, &ch, 0) ? ch : 0;
}
void input::block_mouse_input(bool enable)
{
_block_mouse = enable;
if (enable)
{
ClipCursor(nullptr);
}
}
void input::block_keyboard_input(bool enable)
{
_block_keyboard = enable;
}
void input::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;
}
}
}