forked from elishacloud/dinputto8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDirectInputDeviceX.cpp
474 lines (354 loc) · 12.7 KB
/
IDirectInputDeviceX.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* Copyright (C) 2020 Elisha Riedlinger
*
* This software is provided 'as-is', without any express or implied warranty. In no event will the
* authors be held liable for any damages arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you wrote the
* original software. If you use this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
* being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "dinputto8.h"
const DIDATAFORMAT c_dfDIKeyboard = {
sizeof(DIDATAFORMAT),
sizeof(DIOBJECTDATAFORMAT),
DIDF_RELAXIS,
MAX_KEYBAORD,
sizeof(dfDIKeyboard) / sizeof(*dfDIKeyboard),
(LPDIOBJECTDATAFORMAT)dfDIKeyboard
};
HRESULT m_IDirectInputDeviceX::QueryInterface(REFIID riid, LPVOID FAR * ppvObj, DWORD DirectXVersion)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
DWORD DxVersion = (CheckWrapperType(riid)) ? GetGUIDVersion(riid) : DirectXVersion;
return ProxyQueryInterface(ProxyInterface, riid, ppvObj, GetWrapperType(DxVersion), GetWrapperInterfaceX(DxVersion));
}
LPVOID m_IDirectInputDeviceX::GetWrapperInterfaceX(DWORD DirectXVersion)
{
switch (DirectXVersion)
{
case 1:
return WrapperInterface;
case 2:
return WrapperInterface2;
case 7:
return WrapperInterface7;
default:
return nullptr;
}
}
ULONG m_IDirectInputDeviceX::AddRef()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->AddRef();
}
ULONG m_IDirectInputDeviceX::Release()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
ULONG ref = ProxyInterface->Release();
if (ref == 0)
{
delete this;
}
return ref;
}
HRESULT m_IDirectInputDeviceX::GetCapabilities(LPDIDEVCAPS lpDIDevCaps)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->GetCapabilities(lpDIDevCaps);
}
HRESULT m_IDirectInputDeviceX::EnumObjectsA(LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback, LPVOID pvRef, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceA()->EnumObjects(lpCallback, pvRef, dwFlags);
}
HRESULT m_IDirectInputDeviceX::EnumObjectsW(LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback, LPVOID pvRef, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceW()->EnumObjects(lpCallback, pvRef, dwFlags);
}
HRESULT m_IDirectInputDeviceX::GetProperty(REFGUID rguidProp, LPDIPROPHEADER pdiph)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->GetProperty(rguidProp, pdiph);
}
HRESULT m_IDirectInputDeviceX::SetProperty(REFGUID rguidProp, LPCDIPROPHEADER pdiph)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->SetProperty(rguidProp, pdiph);
}
HRESULT m_IDirectInputDeviceX::Acquire()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
if (!CanAquireDevice)
{
CanAquireDevice = true;
return DIERR_NOTINITIALIZED;
}
return ProxyInterface->Acquire();
}
HRESULT m_IDirectInputDeviceX::Unacquire()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->Unacquire();
}
HRESULT m_IDirectInputDeviceX::GetDeviceState(DWORD cbData, LPVOID lpvData)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
// Handle data format offset
if (Offset)
{
UCHAR tmp[MAX_KEYBAORD] = {};
HRESULT hr = ProxyInterface->GetDeviceState(MAX_KEYBAORD, tmp);
if (SUCCEEDED(hr))
{
ZeroMemory(lpvData, cbData);
for (DWORD x = Offset; x < min(cbData, Offset + MAX_KEYBAORD); x++)
{
((UCHAR*)lpvData)[x] = tmp[x - Offset];
}
}
return hr;
}
return ProxyInterface->GetDeviceState(cbData, lpvData);
}
HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
// If just peeking at data
if (dwFlags == DIGDD_PEEK || !rgdod)
{
return ProxyInterface->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), rgdod, pdwInOut, dwFlags);
}
// Check for valid parameters
if (!pdwInOut || !*pdwInOut || *pdwInOut == (DWORD)-1 || !cbObjectData)
{
return DIERR_INVALIDPARAM;
}
EnterCriticalSection(&dics);
// Check the size of the array
if (*pdwInOut > pdod.size())
{
pdod.resize(*pdwInOut);
Logging::LogDebug() << __FUNCTION__ << " Update dod memory! " << *pdwInOut;
}
HRESULT hr = ProxyInterface->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), &pdod[0], pdwInOut, dwFlags);
// Copy array
if (SUCCEEDED(hr))
{
for (UINT x = 0; x < *pdwInOut; x++)
{
CopyMemory((void*)((DWORD)rgdod + (cbObjectData * x)), &pdod[x], cbObjectData);
}
}
LeaveCriticalSection(&dics);
return hr;
}
HRESULT m_IDirectInputDeviceX::SetDataFormat(LPCDIDATAFORMAT lpdf)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
// Fix unsupported flags
if (lpdf && lpdf->dwNumObjs && lpdf->dwObjSize == sizeof(DIOBJECTDATAFORMAT))
{
// Handle data format offset
if (lpdf->dwNumObjs < MAX_KEYBAORD && (lpdf->rgodf[0].dwType & DIDFT_BUTTON) && lpdf->rgodf[0].dwOfs != 0 && *lpdf->rgodf[0].pguid == GUID_Key)
{
Offset = lpdf->rgodf[0].dwOfs - 1;
if (SUCCEEDED(ProxyInterface->SetDataFormat(&c_dfDIKeyboard)))
{
return DI_OK;
}
}
EnterCriticalSection(&dics);
if (rgodf.size() < lpdf->dwNumObjs)
{
rgodf.resize(lpdf->dwNumObjs);
}
df = {
sizeof(DIDATAFORMAT),
lpdf->dwObjSize,
lpdf->dwFlags,
lpdf->dwDataSize,
lpdf->dwNumObjs,
&rgodf[0] };
for (DWORD x = 0; x < df.dwNumObjs; x++)
{
rgodf[x].pguid = lpdf->rgodf[x].pguid;
rgodf[x].dwOfs = lpdf->rgodf[x].dwOfs;
rgodf[x].dwType = ((lpdf->rgodf[x].dwType & DIDFT_ANYINSTANCE) == 0xFF00) ? lpdf->rgodf[x].dwType | DIDFT_ANYINSTANCE : lpdf->rgodf[x].dwType;
rgodf[x].dwFlags = lpdf->rgodf[x].dwFlags;
}
Offset = 0;
HRESULT hr = ProxyInterface->SetDataFormat(&df);
LeaveCriticalSection(&dics);
return hr;
}
return ProxyInterface->SetDataFormat(lpdf);
}
HRESULT m_IDirectInputDeviceX::SetEventNotification(HANDLE hEvent)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->SetEventNotification(hEvent);
}
HRESULT m_IDirectInputDeviceX::SetCooperativeLevel(HWND hwnd, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
HRESULT hr = ProxyInterface->SetCooperativeLevel(hwnd, dwFlags);
if (SUCCEEDED(hr))
{
CanAquireDevice = true;
}
return hr;
}
HRESULT m_IDirectInputDeviceX::GetObjectInfoA(LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceA()->GetObjectInfo(pdidoi, dwObj, dwHow);
}
HRESULT m_IDirectInputDeviceX::GetObjectInfoW(LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceW()->GetObjectInfo(pdidoi, dwObj, dwHow);
}
HRESULT m_IDirectInputDeviceX::GetDeviceInfoA(LPDIDEVICEINSTANCEA pdidi)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
HRESULT hr = GetProxyInterfaceA()->GetDeviceInfo(pdidi);
if (SUCCEEDED(hr) && pdidi && pdidi->dwSize)
{
pdidi->dwDevType = ConvertDevTypeTo7(GET_DIDEVICE_TYPE(pdidi->dwDevType));
}
return hr;
}
HRESULT m_IDirectInputDeviceX::GetDeviceInfoW(LPDIDEVICEINSTANCEW pdidi)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
HRESULT hr = GetProxyInterfaceW()->GetDeviceInfo(pdidi);
if (SUCCEEDED(hr) && pdidi && pdidi->dwSize)
{
pdidi->dwDevType = ConvertDevTypeTo7(GET_DIDEVICE_TYPE(pdidi->dwDevType));
}
return hr;
}
HRESULT m_IDirectInputDeviceX::RunControlPanel(HWND hwndOwner, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->RunControlPanel(hwndOwner, dwFlags);
}
HRESULT m_IDirectInputDeviceX::Initialize(HINSTANCE hinst, DWORD dwVersion, REFGUID rguid)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->Initialize(hinst, dwVersion, rguid);
}
HRESULT m_IDirectInputDeviceX::CreateEffect(REFGUID rguid, LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT * ppdeff, LPUNKNOWN punkOuter)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
HRESULT hr = ProxyInterface->CreateEffect(rguid, lpeff, ppdeff, punkOuter);
if (SUCCEEDED(hr) && ppdeff)
{
*ppdeff = new m_IDirectInputEffect((IDirectInputEffect*)*ppdeff);
}
return hr;
}
HRESULT m_IDirectInputDeviceX::EnumEffectsA(LPDIENUMEFFECTSCALLBACKA lpCallback, LPVOID pvRef, DWORD dwEffType)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceA()->EnumEffects(lpCallback, pvRef, dwEffType);
}
HRESULT m_IDirectInputDeviceX::EnumEffectsW(LPDIENUMEFFECTSCALLBACKW lpCallback, LPVOID pvRef, DWORD dwEffType)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceW()->EnumEffects(lpCallback, pvRef, dwEffType);
}
HRESULT m_IDirectInputDeviceX::GetEffectInfoA(LPDIEFFECTINFOA pdei, REFGUID rguid)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceA()->GetEffectInfo(pdei, rguid);
}
HRESULT m_IDirectInputDeviceX::GetEffectInfoW(LPDIEFFECTINFOW pdei, REFGUID rguid)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceW()->GetEffectInfo(pdei, rguid);
}
HRESULT m_IDirectInputDeviceX::GetForceFeedbackState(LPDWORD pdwOut)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->GetForceFeedbackState(pdwOut);
}
HRESULT m_IDirectInputDeviceX::SendForceFeedbackCommand(DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->SendForceFeedbackCommand(dwFlags);
}
HRESULT m_IDirectInputDeviceX::EnumCreatedEffectObjects(LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID pvRef, DWORD fl)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
if (!lpCallback)
{
return DIERR_INVALIDPARAM;
}
ENUMEFFECT CallbackContext;
CallbackContext.pvRef = pvRef;
CallbackContext.lpCallback = lpCallback;
return ProxyInterface->EnumCreatedEffectObjects(m_IDirectInputEnumEffect::EnumEffectCallback, &CallbackContext, fl);
}
HRESULT m_IDirectInputDeviceX::Escape(LPDIEFFESCAPE pesc)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return ProxyInterface->Escape(pesc);
}
HRESULT m_IDirectInputDeviceX::Poll()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
HRESULT hr = ProxyInterface->Poll();
return (hr == DI_NOEFFECT) ? DI_OK : hr;
}
HRESULT m_IDirectInputDeviceX::SendDeviceData(DWORD cbObjectData, LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut, DWORD fl)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
if (!pdwInOut || !rgdod || !cbObjectData)
{
return DIERR_INVALIDPARAM;
}
EnterCriticalSection(&dics);
// Check the size of the array
if (*pdwInOut > pdod.size())
{
pdod.resize(*pdwInOut);
Logging::LogDebug() << __FUNCTION__ << " Update dod memory! " << *pdwInOut;
}
// Copy array
ZeroMemory(&pdod[0], sizeof(DIDEVICEOBJECTDATA) * pdod.size());
for (UINT x = 0; x < *pdwInOut; x++)
{
CopyMemory(&pdod[x], (void*)((DWORD)rgdod + (cbObjectData * x)), cbObjectData);
}
HRESULT hr = ProxyInterface->SendDeviceData(sizeof(DIDEVICEOBJECTDATA), &pdod[0], pdwInOut, fl);
LeaveCriticalSection(&dics);
return hr;
}
HRESULT m_IDirectInputDeviceX::EnumEffectsInFileA(LPCSTR lpszFileName, LPDIENUMEFFECTSINFILECALLBACK pec, LPVOID pvRef, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceA()->EnumEffectsInFile(lpszFileName, pec, pvRef, dwFlags);
}
HRESULT m_IDirectInputDeviceX::EnumEffectsInFileW(LPCWSTR lpszFileName, LPDIENUMEFFECTSINFILECALLBACK pec, LPVOID pvRef, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceW()->EnumEffectsInFile(lpszFileName, pec, pvRef, dwFlags);
}
HRESULT m_IDirectInputDeviceX::WriteEffectToFileA(LPCSTR lpszFileName, DWORD dwEntries, LPDIFILEEFFECT rgDiFileEft, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceA()->WriteEffectToFile(lpszFileName, dwEntries, rgDiFileEft, dwFlags);
}
HRESULT m_IDirectInputDeviceX::WriteEffectToFileW(LPCWSTR lpszFileName, DWORD dwEntries, LPDIFILEEFFECT rgDiFileEft, DWORD dwFlags)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";
return GetProxyInterfaceW()->WriteEffectToFile(lpszFileName, dwEntries, rgDiFileEft, dwFlags);
}