-
Notifications
You must be signed in to change notification settings - Fork 11
/
rawinput.cpp
220 lines (177 loc) · 5.02 KB
/
rawinput.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
/*
RInput allows you to override low definition windows mouse input with high definition mouse input.
RInput Copyright (C) 2012, J. Dijkstra (abort@digitalise.net)
This file is part of RInput.
RInput is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RInput is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RInput. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rawinput.h"
// define the to be hooked functions
extern "C" DETOUR_TRAMPOLINE(int __stdcall TrmpGetCursorPos(LPPOINT lpPoint), GetCursorPos);
extern "C" DETOUR_TRAMPOLINE(int __stdcall TrmpSetCursorPos(int X, int Y), SetCursorPos);
// Initialize static variables
bool CRawInput::bRegistered = false;
HWND CRawInput::hwndInput = NULL;
long CRawInput::x = 0;
long CRawInput::y = 0;
bool CRawInput::initialize(WCHAR* pwszError)
{
if (!initWindow(pwszError))
return false;
if (!initInput(pwszError))
return false;
return true;
}
bool CRawInput::initWindow(WCHAR* pwszError)
{
// Register the window to catch WM_INPUT events
WNDCLASSEX wcex;
memset(&wcex, 0, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = (WNDPROC)wpInput;
wcex.lpszClassName = INPUTWINDOW;
if (!RegisterClassEx(&wcex))
{
lstrcpyW(pwszError, L"Failed to register input window!");
return false;
}
// Create the window to catch WM_INPUT events
CRawInput::hwndInput = CreateWindowEx(NULL, INPUTWINDOW, INPUTWINDOW, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
if (!CRawInput::hwndInput)
{
lstrcpyW(pwszError, L"Failed to create input window!");
return false;
}
#ifdef _DEBUG
OutputDebugString("Created Raw Input Window");
#endif
// Unregister the window class
UnregisterClass(INPUTWINDOW, NULL);
return true;
}
bool CRawInput::initInput(WCHAR* pwszError)
{
// Set default coordinates
CRawInput::x = CRawInput::y = 0;
RAWINPUTDEVICE rMouse;
memset(&rMouse, 0, sizeof(RAWINPUTDEVICE));
rMouse.dwFlags = 0;
rMouse.hwndTarget = CRawInput::hwndInput;
rMouse.usUsagePage = 0x01;
rMouse.usUsage = 0x02;
if (!RegisterRawInputDevices(&rMouse, 1, sizeof(RAWINPUTDEVICE)))
{
lstrcpyW(pwszError, L"Failed to register raw input device!");
return false;
}
return (bRegistered = true);
}
unsigned int CRawInput::pollInput()
{
MSG msg;
while (GetMessage(&msg, CRawInput::hwndInput, 0, 0) != 0)
DispatchMessage(&msg);
return msg.message;
}
LRESULT __stdcall CRawInput::wpInput(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INPUT:{
#ifdef _DEBUG
OutputDebugString("WM_INPUT event");
#endif
UINT uiSize = RAWPTRSIZE;
static unsigned char lpb[RAWPTRSIZE];
RAWINPUT* rwInput;
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &uiSize, RAWINPUTHDRSIZE) != -1)
{
rwInput = (RAWINPUT*)lpb;
if (!rwInput->header.dwType)
{
CRawInput::x += rwInput->data.mouse.lLastX;
CRawInput::y += rwInput->data.mouse.lLastY;
}
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default: return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int __stdcall CRawInput::hSetCursorPos(int x, int y)
{
if (!TrmpSetCursorPos(x, y)) return 1;
CRawInput::x = (long)x;
CRawInput::y = (long)y;
#ifdef _DEBUG
OutputDebugString("Set coordinates");
#endif
return 0;
}
int __stdcall CRawInput::hGetCursorPos(LPPOINT lpPoint)
{
lpPoint->x = CRawInput::x;
lpPoint->y = CRawInput::y;
#ifdef _DEBUG
OutputDebugString("Returned coordinates");
#endif
return 0;
}
bool CRawInput::hookLibrary(bool bInstall)
{
if (bInstall)
{
if (!DetourFunctionWithTrampoline((PBYTE)TrmpGetCursorPos, (PBYTE)CRawInput::hGetCursorPos) || !DetourFunctionWithTrampoline((PBYTE)TrmpSetCursorPos, (PBYTE)CRawInput::hSetCursorPos))
return false;
#ifdef _DEBUG
else
OutputDebugString("Hooked GetCursorPos and SetCursorPos");
#endif
}
else
{
if (DetourRemove((PBYTE)TrmpGetCursorPos, (PBYTE)CRawInput::hGetCursorPos))
{
#ifdef _DEBUG
OutputDebugString("Removed GetCursorPos hook");
#endif
}
if (DetourRemove((PBYTE)TrmpSetCursorPos, (PBYTE)CRawInput::hSetCursorPos))
{
#ifdef _DEBUG
OutputDebugString("Removed SetCursorPos hook");
#endif
}
}
return true;
}
void CRawInput::unload()
{
if (bRegistered && CRawInput::hwndInput)
{
RAWINPUTDEVICE rMouse;
memset(&rMouse, 0, sizeof(RAWINPUTDEVICE));
rMouse.dwFlags |= RIDEV_REMOVE;
rMouse.hwndTarget = NULL;
rMouse.usUsagePage = 0x01;
rMouse.usUsage = 0x02;
RegisterRawInputDevices(&rMouse, 1, sizeof(RAWINPUTDEVICE));
DestroyWindow(hwndInput);
#ifdef _DEBUG
OutputDebugString("Unregistered mouse device");
OutputDebugString("Closed Input Window");
#endif
}
}