-
Notifications
You must be signed in to change notification settings - Fork 2
/
DirectInput.cpp
417 lines (363 loc) · 9.93 KB
/
DirectInput.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
#include "stdafx.h"
#include <Windows.h>
#define DIRECTINPUT_VERSION 0x0700
#include <dinput.h>
#include <vector>
#include <cstdint>
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")
void PrintLog(const char *text)
{
#if 0
static HANDLE file = INVALID_HANDLE_VALUE;
if (file == INVALID_HANDLE_VALUE)
file = CreateFile(L"C:\\Development\\dinput.txt", GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
DWORD written = 0;
WriteFile(file, text, strlen(text), &written, nullptr);
#endif
}
class MouseMovement
{
public:
MouseMovement()
{
WNDCLASSEX class_desc = { 0 };
class_desc.cbSize = sizeof(WNDCLASSEX);
class_desc.style = 0;
class_desc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
class_desc.lpfnWndProc = &MouseMovement::static_window_proc;
class_desc.lpszClassName = L"MouseMovement";
ATOM class_atom = RegisterClassEx(&class_desc);
DWORD ex_style = 0;
DWORD style = WS_POPUPWINDOW;
window_handle = CreateWindowEx(ex_style, L"MouseMovement", L"MouseMovement", style, CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, 0, 0, (HINSTANCE)GetModuleHandle(0), this);
if (window_handle == 0)
return;
// See http://msdn.microsoft.com/en-us/library/ms789918.aspx
#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE ((USHORT) 0x02)
#endif
#ifndef HID_USAGE_GENERIC_JOYSTICK
#define HID_USAGE_GENERIC_JOYSTICK ((USHORT) 0x04)
#endif
#ifndef HID_USAGE_GENERIC_GAMEPAD
#define HID_USAGE_GENERIC_GAMEPAD ((USHORT) 0x05)
#endif
#ifndef RIDEV_INPUTSINK
#define RIDEV_INPUTSINK (0x100)
#endif
RAWINPUTDEVICE rid;
rid.usUsagePage = HID_USAGE_PAGE_GENERIC;
rid.usUsage = HID_USAGE_GENERIC_MOUSE;
rid.dwFlags = RIDEV_INPUTSINK;
rid.hwndTarget = window_handle;
BOOL result = RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
if (result == FALSE)
{
DestroyWindow(window_handle);
window_handle = 0;
}
}
~MouseMovement()
{
if (window_handle)
DestroyWindow(window_handle);
}
int x = 0;
int y = 0;
private:
LRESULT window_proc(HWND window_handle, UINT message_id, WPARAM wparam, LPARAM lparam)
{
if (message_id == WM_INPUT)
{
HRAWINPUT handle = (HRAWINPUT)lparam;
UINT size = 0;
UINT result = GetRawInputData(handle, RID_INPUT, 0, &size, sizeof(RAWINPUTHEADER));
if (result == 0 && size > 0)
{
std::vector<uint8_t*> buffer(size);
result = GetRawInputData(handle, RID_INPUT, buffer.data(), &size, sizeof(RAWINPUTHEADER));
if (result >= 0)
{
RAWINPUT *rawinput = (RAWINPUT*)buffer.data();
if (rawinput->header.dwType == RIM_TYPEMOUSE)
{
x += rawinput->data.mouse.lLastX;
y += rawinput->data.mouse.lLastY;
}
}
}
return DefWindowProc(window_handle, message_id, wparam, lparam);
}
else
{
return DefWindowProc(window_handle, message_id, wparam, lparam);
}
}
static LRESULT CALLBACK static_window_proc(HWND window_handle, UINT message_id, WPARAM wparam, LPARAM lparam)
{
MouseMovement *self = 0;
if (message_id == WM_CREATE)
{
LPCREATESTRUCT create_struct = (LPCREATESTRUCT)lparam;
self = (MouseMovement *)create_struct->lpCreateParams;
SetWindowLongPtr(window_handle, GWLP_USERDATA, (LONG_PTR)self);
}
else
{
self = (MouseMovement *)GetWindowLongPtr(window_handle, GWLP_USERDATA);
}
if (self)
return self->window_proc(window_handle, message_id, wparam, lparam);
else
return DefWindowProc(window_handle, message_id, wparam, lparam);
}
HWND window_handle;
};
class DirectInputDeviceImplA : public IDirectInputDeviceA
{
public:
/*** IUnknown methods ***/
STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID * ppvObj)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOINTERFACE;
}
STDMETHOD_(ULONG, AddRef)(THIS)
{
return ++refcount;
}
STDMETHOD_(ULONG, Release)(THIS)
{
ULONG result = --refcount;
if (result == 0)
{
delete this;
return 0;
}
else
{
return result;
}
}
/*** IDirectInputDeviceA methods ***/
STDMETHOD(GetCapabilities)(THIS_ LPDIDEVCAPS)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(EnumObjects)(THIS_ LPDIENUMDEVICEOBJECTSCALLBACKA, LPVOID, DWORD)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(GetProperty)(THIS_ REFGUID, LPDIPROPHEADER)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(SetProperty)(THIS_ REFGUID guid, LPCDIPROPHEADER header)
{
PrintLog(__FUNCTION__ "\r\n");
if (&guid == &DIPROP_AXISMODE)
{
// Sets the axis mode. The value being set (DIPROPAXISMODE_ABS or DIPROPAXISMODE_REL) must be specified in the dwData member of the associated DIPROPDWORD structure.
}
else if (&guid == &DIPROP_BUFFERSIZE)
{
// Sets the input buffer size. The value being set must be specified in the dwData member of the associated DIPROPDWORD structure.
}
else
{
if (&guid == &DIPROP_GRANULARITY) PrintLog("DIPROP_GRANULARITY\r\n");
else if (&guid == &DIPROP_RANGE) PrintLog("DIPROP_RANGE\r\n");
else if (&guid == &DIPROP_DEADZONE) PrintLog("DIPROP_DEADZONE\r\n");
else if (&guid == &DIPROP_SATURATION) PrintLog("DIPROP_SATURATION\r\n");
else if (&guid == &DIPROP_FFGAIN) PrintLog("DIPROP_FFGAIN\r\n");
else if (&guid == &DIPROP_FFLOAD) PrintLog("DIPROP_FFLOAD\r\n");
else if (&guid == &DIPROP_AUTOCENTER) PrintLog("DIPROP_AUTOCENTER\r\n");
else if (&guid == &DIPROP_CALIBRATIONMODE) PrintLog("DIPROP_CALIBRATIONMODE\r\n");
else if (&guid == &DIPROP_CALIBRATION) PrintLog("DIPROP_CALIBRATION\r\n");
else if (&guid == &DIPROP_PHYSICALRANGE) PrintLog("DIPROP_PHYSICALRANGE\r\n");
else if (&guid == &DIPROP_LOGICALRANGE) PrintLog("DIPROP_LOGICALRANGE\r\n");
else PrintLog("Unknown DIPROP\r\n");
}
return DI_OK;
}
STDMETHOD(Acquire)(THIS)
{
return S_OK;
}
STDMETHOD(Unacquire)(THIS)
{
return S_OK;
}
STDMETHOD(GetDeviceState)(THIS_ DWORD size, LPVOID data)
{
DIMOUSESTATE *state = (DIMOUSESTATE*)data;
state->lX = movement.x;
state->lY = movement.y;
state->lZ = 0;
state->rgbButtons[0] = 0;
state->rgbButtons[1] = 0;
state->rgbButtons[2] = 0;
state->rgbButtons[3] = 0;
return DI_OK;
}
STDMETHOD(GetDeviceData)(THIS_ DWORD cbObjectData, LPDIDEVICEOBJECTDATA objectData, LPDWORD pdwInOut, DWORD dwFlags)
{
*pdwInOut = 0;
return DI_OK;
}
STDMETHOD(SetDataFormat)(THIS_ LPCDIDATAFORMAT format)
{
if (format->dwFlags & DIDF_ABSAXIS)
{
PrintLog("DIDF_ABSAXIS\r\n");
}
else if (format->dwFlags & DIDF_RELAXIS)
{
PrintLog("DIDF_RELAXIS\r\n");
}
if (format->dwObjSize == c_dfDIMouse.dwObjSize && format->dwDataSize == c_dfDIMouse.dwDataSize && format->dwNumObjs == c_dfDIMouse.dwNumObjs)
{
PrintLog("Looks like c_dfDIMouse\r\n");
}
if (format->dwObjSize == c_dfDIMouse2.dwObjSize && format->dwDataSize == c_dfDIMouse2.dwDataSize && format->dwNumObjs == c_dfDIMouse2.dwNumObjs)
{
PrintLog("Looks like c_dfDIMouse2\r\n");
}
return DI_OK;
}
STDMETHOD(SetEventNotification)(THIS_ HANDLE)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(SetCooperativeLevel)(THIS_ HWND, DWORD)
{
return S_OK;
}
STDMETHOD(GetObjectInfo)(THIS_ LPDIDEVICEOBJECTINSTANCEA, DWORD, DWORD)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(GetDeviceInfo)(THIS_ LPDIDEVICEINSTANCEA)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(RunControlPanel)(THIS_ HWND, DWORD)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(Initialize)(THIS_ HINSTANCE, DWORD, REFGUID)
{
PrintLog(__FUNCTION__ "\r\n");
return S_OK;
}
int refcount = 1;
MouseMovement movement;
};
class DirectInputImplA : public IDirectInputA
{
public:
/*** IUnknown methods ***/
STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID * ppvObj)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOINTERFACE;
}
STDMETHOD_(ULONG, AddRef)(THIS)
{
return ++refcount;
}
STDMETHOD_(ULONG, Release)(THIS)
{
ULONG result = --refcount;
if (result == 0)
{
delete this;
return 0;
}
else
{
return result;
}
}
/*** IDirectInputA methods ***/
STDMETHOD(CreateDevice)(THIS_ REFGUID guid, LPDIRECTINPUTDEVICEA *device, LPUNKNOWN)
{
if (guid == GUID_SysMouse)
{
*device = new DirectInputDeviceImplA();
return S_OK;
}
else
{
return DIERR_DEVICENOTREG;
}
}
STDMETHOD(EnumDevices)(THIS_ DWORD dword0, LPDIENUMDEVICESCALLBACKA callback, LPVOID userdata, DWORD dword1)
{
PrintLog(__FUNCTION__ "\r\n");
DIDEVICEINSTANCEA instance = { };
instance.dwSize = sizeof(DIDEVICEINSTANCEA);
instance.guidInstance = GUID_SysMouse;
instance.guidProduct = GUID_SysMouse;
instance.dwDevType = DIDEVTYPE_MOUSE;
strcpy(instance.tszInstanceName, "Mouse");
strcpy(instance.tszProductName, "Mouse");
instance.guidFFDriver = GUID_SysMouse;
instance.wUsagePage = 0;
instance.wUsage = 0;
callback(&instance, userdata);
return S_OK;
}
STDMETHOD(GetDeviceStatus)(THIS_ REFGUID guid)
{
PrintLog(__FUNCTION__ "\r\n");
if (guid == GUID_SysMouse) return DI_OK;
return DIERR_DEVICENOTREG;
}
STDMETHOD(RunControlPanel)(THIS_ HWND, DWORD)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDMETHOD(Initialize)(THIS_ HINSTANCE, DWORD)
{
PrintLog(__FUNCTION__ "\r\n");
return S_OK;
}
int refcount = 1;
};
HRESULT WINAPI DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA* lplpDirectInput, LPUNKNOWN punkOuter)
{
*lplpDirectInput = new DirectInputImplA();
return S_OK;
}
HRESULT WINAPI DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW* lplpDirectInput, LPUNKNOWN punkOuter)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
HRESULT WINAPI DirectInputCreateEx(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID *ppvOut, LPUNKNOWN punkOuter)
{
PrintLog(__FUNCTION__ "\r\n");
return E_NOTIMPL;
}
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
PrintLog(__FUNCTION__ "\r\n");
return CLASS_E_CLASSNOTAVAILABLE;
}
STDAPI DllCanUnloadNow()
{
return S_FALSE;
}