Repository navigation
Expand file tree
/
Copy pathWrapperIDirectInput.cpp
More file actions
511 lines (451 loc) · 22.2 KB
/
Copy pathWrapperIDirectInput.cpp
File metadata and controls
511 lines (451 loc) · 22.2 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/***************************************************************************************************
* Xidi
* DirectInput interface for XInput controllers.
* Fixes issues associated with certain XInput-based controllers.
***************************************************************************************************
* Authored by Samuel Grossman
* Copyright (c) 2016-2026
***********************************************************************************************//**
* @file WrapperIDirectInput.cpp
* Implementation of the wrapper class for IDirectInput.
**************************************************************************************************/
#include "WrapperIDirectInput.h"
#include <cstddef>
#include <cstdlib>
#include <optional>
#include <unordered_set>
#include <Infra/Core/Message.h>
#include <Infra/Core/TemporaryBuffer.h>
#include "ApiDirectInput.h"
#include "ApiGUID.h"
#include "ControllerIdentification.h"
#include "Mapper.h"
#include "PhysicalController.h"
#include "PhysicalControllerBackend.h"
#include "Strings.h"
#include "VirtualController.h"
#include "VirtualDirectInputDevice.h"
namespace Xidi
{
/// Contains all information required to intercept callbacks to EnumDevices.
template <EDirectInputVersion diVersion> struct SEnumDevicesCallbackInfo
{
/// #WrapperIDirectInputBase instance that invoked the enumeration.
WrapperIDirectInputBase<diVersion>* instance;
/// Application-specified callback that should be invoked.
typename DirectInputTypes<diVersion>::EnumDevicesCallbackType lpCallback;
/// Application-specified argument to be provided to the application-specified callback.
LPVOID pvRef;
/// Indicates if the application requested that enumeration continue or stop.
BOOL callbackReturnCode;
/// Holds device identifiers seen during device enumeration.
std::unordered_set<GUID> seenInstanceIdentifiers;
};
template <EDirectInputVersion diVersion> WrapperIDirectInputBase<diVersion>::
WrapperIDirectInputBase(DirectInputTypes<diVersion>::IDirectInputType* underlyingDIObject)
: underlyingDIObject(underlyingDIObject)
{}
template <EDirectInputVersion diVersion> HRESULT __stdcall WrapperIDirectInputBase<
diVersion>::QueryInterface(REFIID riid, LPVOID* ppvObj)
{
void* interfacePtr = nullptr;
const HRESULT result = underlyingDIObject->QueryInterface(riid, &interfacePtr);
if (S_OK == result)
{
if (true == DirectInputTypes<diVersion>::IsCompatibleDirectInputIID(riid))
*ppvObj = this;
else
*ppvObj = interfacePtr;
}
return result;
}
template <EDirectInputVersion diVersion> ULONG __stdcall WrapperIDirectInputBase<
diVersion>::AddRef(void)
{
return underlyingDIObject->AddRef();
}
template <EDirectInputVersion diVersion> ULONG __stdcall WrapperIDirectInputBase<
diVersion>::Release(void)
{
const ULONG numRemainingRefs = underlyingDIObject->Release();
if (0 == numRemainingRefs) delete this;
return numRemainingRefs;
}
template <EDirectInputVersion diVersion> HRESULT __stdcall WrapperIDirectInputBase<diVersion>::
CreateDevice(
REFGUID rguid,
DirectInputTypes<diVersion>::IDirectInputDeviceCompatType** lplpDirectInputDevice,
LPUNKNOWN pUnkOuter)
{
// WinMM's internal implementation depends on the DInput version of `CreateDevice`. When using
// the HookModule form of Xidi, calls to the system version of WinMM will end up calling this
// function. If called on an XInput controller, this function will fail, which prevents WinMM
// from enumerating it. Using the WinMM wrapper directly, however, results in the XInput
// controller being enumerated in WinMM and then ultimately being filtered out in the WinMM
// wrapper. Ultimately the outcome is the same, the distiction is just whether the filtering
// happens in DirectInput or in the WinMM wrapper.
// Check if the specified instance GUID is an Xidi virtual controller GUID.
const std::optional<Controller::TControllerIdentifier> maybeVirtualControllerId =
VirtualControllerIdFromInstanceGuid(rguid);
if (false == maybeVirtualControllerId.has_value())
{
// Not a virtual controller GUID, so just create the device as requested by the application.
// However, first dump some information about the device.
typename DirectInputTypes<diVersion>::IDirectInputDeviceCompatType* createdDevice = nullptr;
const HRESULT createDeviceResult =
underlyingDIObject->CreateDevice(rguid, &createdDevice, pUnkOuter);
if (DI_OK == createDeviceResult)
{
if (GUID_SysKeyboard == rguid)
{
Infra::Message::Output(
Infra::Message::ESeverity::Info,
L"Binding to the system keyboard device. Xidi will not handle communication with it.");
}
else if (GUID_SysMouse == rguid)
{
Infra::Message::Output(
Infra::Message::ESeverity::Info,
L"Binding to the system mouse device. Xidi will not handle communication with it.");
}
else
{
typename DirectInputTypes<diVersion>::DeviceInstanceType deviceInfo = {
.dwSize = sizeof(typename DirectInputTypes<diVersion>::DeviceInstanceType)};
const HRESULT deviceInfoResult = createdDevice->GetDeviceInfo(&deviceInfo);
const bool deviceSupportsConfiguredBackend =
DoesDirectInputControllerSupportConfiguredBackend<diVersion>(
underlyingDIObject, rguid);
if (true == deviceSupportsConfiguredBackend)
{
if (Infra::Message::WillOutputMessageOfSeverity(Infra::Message::ESeverity::Info))
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Info,
L"Attempting to bind directly to supported device \"%s\" with instance GUID %s. Xidi hides devices supported by physical controller backend \"%.*s\" and therefore is rejecting this request.",
Infra::TemporaryString(deviceInfo.tszProductName).AsCString(),
Strings::GuidToString(deviceInfo.guidInstance).AsCString(),
static_cast<int>(
Controller::GetPhysicalControllerBackend()->PluginName().length()),
Controller::GetPhysicalControllerBackend()->PluginName().data());
}
createdDevice->Release();
return DIERR_DEVICENOTREG;
}
if (Infra::Message::WillOutputMessageOfSeverity(Infra::Message::ESeverity::Info))
{
if (DI_OK == deviceInfoResult)
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Info,
L"Binding to device \"%s\" with instance GUID %s, which is not supported by physical controller backend \"%.*s\". Xidi will not handle communication with it.",
Infra::TemporaryString(deviceInfo.tszProductName).AsCString(),
Strings::GuidToString(deviceInfo.guidInstance).AsCString(),
static_cast<int>(
Controller::GetPhysicalControllerBackend()->PluginName().length()),
Controller::GetPhysicalControllerBackend()->PluginName().data());
}
else
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Info,
L"Binding to an unknown device with instance GUID %s. Xidi will not handle communication with it.",
Strings::GuidToString(deviceInfo.guidInstance).AsCString());
}
}
}
}
else
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Info,
L"Failed (result = 0x%08x) to bind to a device unsupported by physical controller backend \"%.*s\".",
static_cast<unsigned int>(createDeviceResult),
static_cast<int>(Controller::GetPhysicalControllerBackend()->PluginName().length()),
Controller::GetPhysicalControllerBackend()->PluginName().data());
}
if (nullptr != createdDevice) *lplpDirectInputDevice = createdDevice;
return createDeviceResult;
}
else
{
const Controller::TControllerIdentifier virtualControllerId =
maybeVirtualControllerId.value();
// Is a virtual controller GUID, so create a virtual controller wrapped with a DirectInput
// interface.
VirtualDirectInputDevice<diVersion>* const newDirectInputDeviceInterfaceObject =
new VirtualDirectInputDevice<diVersion>(
std::make_unique<Controller::VirtualController>(virtualControllerId));
*lplpDirectInputDevice = newDirectInputDeviceInterfaceObject;
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Info,
L"Binding to Xidi virtual controller %u with interface object %u.",
(virtualControllerId + 1),
newDirectInputDeviceInterfaceObject->ObjectIdentifier());
if (nullptr != pUnkOuter)
Infra::Message::Output(
Infra::Message::ESeverity::Warning,
L"Application requested COM aggregation, which is not implemented, while binding to a Xidi virtual controller.");
return DI_OK;
}
}
template <EDirectInputVersion diVersion> HRESULT __stdcall WrapperIDirectInputBase<diVersion>::
EnumDevices(
DWORD dwDevType,
DirectInputTypes<diVersion>::EnumDevicesCallbackType lpCallback,
LPVOID pvRef,
DWORD dwFlags)
{
const DWORD gameControllerDevClass = 4; // DI8DEVCLASS_GAMECTRL, DIDEVTYPE_JOYSTICK
const DWORD allDevicesDevClass = 0; // DI8DEVCLASS_ALL, undefined for legacy
const BOOL gameControllersRequested =
((allDevicesDevClass == dwDevType) || (gameControllerDevClass == dwDevType));
const bool forceFeedbackControllersOnly = (0 != (dwFlags & DIEDFL_FORCEFEEDBACK));
SEnumDevicesCallbackInfo<diVersion> callbackInfo;
callbackInfo.instance = this;
callbackInfo.lpCallback = lpCallback;
callbackInfo.pvRef = pvRef;
callbackInfo.callbackReturnCode = DIENUM_CONTINUE;
callbackInfo.seenInstanceIdentifiers.clear();
HRESULT enumResult = DI_OK;
Infra::Message::Output(
Infra::Message::ESeverity::Debug, L"Starting to enumerate DirectInput devices.");
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Debug,
L"Enumerate: dwDevType = 0x%08x, dwFlags = 0x%08x.",
dwDevType,
dwFlags);
// Enumerating game controllers requires some manipulation.
if (gameControllersRequested)
{
// First scan the system for any game controllers supported by the configured physical
// controller backend that also match the enumeration request. Such devices, when enumerated
// via DirectInput, do not report supporting force feedback, even though Xidi does implement
// such support. For this reason any filtering by force feedback support must be removed while
// using the system-supplied interfaces to scan for supported devices.
enumResult = underlyingDIObject->EnumDevices(
dwDevType,
&WrapperIDirectInputBase<diVersion>::CallbackEnumGameControllersSupportedDeviceScan,
(LPVOID)&callbackInfo,
(dwFlags & ~(DIEDFL_FORCEFEEDBACK)));
if (DI_OK != enumResult) return enumResult;
const BOOL systemHasSupportedDevices = (0 != callbackInfo.seenInstanceIdentifiers.size());
if (systemHasSupportedDevices)
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Debug,
L"Enumerate: System has devices supported by physical controller backend \"%.*s\", so Xidi virtual controllers are being presented to the application before other controllers.",
static_cast<int>(Controller::GetPhysicalControllerBackend()->PluginName().length()),
Controller::GetPhysicalControllerBackend()->PluginName().data());
else
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Debug,
L"Enumerate: System has no devices supported by physical controller backend \"%.*s\", so Xidi virtual controllers are being presented to the application after other controllers.",
static_cast<int>(Controller::GetPhysicalControllerBackend()->PluginName().length()),
Controller::GetPhysicalControllerBackend()->PluginName().data());
// Second, if the system has controllers supported by the configured physical controller
// backend, enumerate the virtual controllers now. These will be the first controllers seen by
// the application.
if (systemHasSupportedDevices)
{
callbackInfo.callbackReturnCode =
EnumerateVirtualControllers<diVersion>(lpCallback, pvRef, forceFeedbackControllersOnly);
if (DIENUM_STOP == callbackInfo.callbackReturnCode)
{
Infra::Message::Output(
Infra::Message::ESeverity::Debug, L"Application has terminated enumeration.");
return enumResult;
}
}
// Third, enumerate all other game controllers, filtering out those that are supported by the
// configured physical controller backend.
enumResult = underlyingDIObject->EnumDevices(
gameControllerDevClass, &CallbackEnumDevicesFiltered, (LPVOID)&callbackInfo, dwFlags);
if (DI_OK != enumResult) return enumResult;
if (DIENUM_STOP == callbackInfo.callbackReturnCode)
{
Infra::Message::Output(
Infra::Message::ESeverity::Debug, L"Application has terminated enumeration.");
return enumResult;
}
// Finally, if the system did not have any controllers supported by the configured physical
// controller backend, enumerate the virtual controllers now. These will be the last
// controllers seen by the application.
if (!systemHasSupportedDevices)
{
callbackInfo.callbackReturnCode =
EnumerateVirtualControllers<diVersion>(lpCallback, pvRef, forceFeedbackControllersOnly);
if (DIENUM_STOP == callbackInfo.callbackReturnCode)
{
Infra::Message::Output(
Infra::Message::ESeverity::Debug, L"Application has terminated enumeration.");
return enumResult;
}
}
}
// Enumerate anything else the application requested, filtering out game controllers.
enumResult = underlyingDIObject->EnumDevices(
dwDevType, &CallbackEnumDevicesFiltered, (LPVOID)&callbackInfo, dwFlags);
if (DI_OK != enumResult) return enumResult;
if (DIENUM_STOP == callbackInfo.callbackReturnCode)
{
Infra::Message::Output(
Infra::Message::ESeverity::Debug, L"Application has terminated enumeration.");
return enumResult;
}
Infra::Message::Output(
Infra::Message::ESeverity::Debug, L"Finished enumerating DirectInput devices.");
return enumResult;
}
template <EDirectInputVersion diVersion> HRESULT __stdcall WrapperIDirectInputBase<diVersion>::
FindDevice(
REFGUID rguidClass,
DirectInputTypes<diVersion>::ConstStringType ptszName,
LPGUID pguidInstance)
{
return underlyingDIObject->FindDevice(rguidClass, ptszName, pguidInstance);
}
template <EDirectInputVersion diVersion> HRESULT __stdcall WrapperIDirectInputBase<
diVersion>::GetDeviceStatus(REFGUID rguidInstance)
{
// Check if the specified instance GUID is an XInput GUID.
const std::optional<Controller::TControllerIdentifier> maybeVirtualControllerId =
VirtualControllerIdFromInstanceGuid(rguidInstance);
if (false == maybeVirtualControllerId.has_value())
{
// Not an XInput GUID, so ask the underlying implementation for status.
return underlyingDIObject->GetDeviceStatus(rguidInstance);
}
else
{
// Is an XInput GUID, so specify that it is connected.
return DI_OK;
}
}
template <EDirectInputVersion diVersion> HRESULT __stdcall WrapperIDirectInputBase<
diVersion>::Initialize(HINSTANCE hinst, DWORD dwVersion)
{
return underlyingDIObject->Initialize(hinst, dwVersion);
}
template <EDirectInputVersion diVersion> HRESULT __stdcall WrapperIDirectInputBase<
diVersion>::RunControlPanel(HWND hwndOwner, DWORD dwFlags)
{
return underlyingDIObject->RunControlPanel(hwndOwner, dwFlags);
}
template <EDirectInputVersion diVersion> BOOL __stdcall WrapperIDirectInputBase<diVersion>::
CallbackEnumGameControllersSupportedDeviceScan(
const DirectInputTypes<diVersion>::DeviceInstanceType* lpddi, LPVOID pvRef)
{
SEnumDevicesCallbackInfo<diVersion>* callbackInfo = (SEnumDevicesCallbackInfo<diVersion>*)pvRef;
// If the present controller supports XInput, indicate such by adding it to the set of instance
// identifiers of interest.
if (DoesDirectInputControllerSupportConfiguredBackend<diVersion>(
callbackInfo->instance->underlyingDIObject, lpddi->guidInstance))
{
callbackInfo->seenInstanceIdentifiers.insert(lpddi->guidInstance);
if (Infra::Message::WillOutputMessageOfSeverity(Infra::Message::ESeverity::Debug))
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Debug,
L"Enumerate: DirectInput device \"%s\" with instance GUID %s supports physical controller backend \"%.*s\" and will not be presented to the application.",
Infra::TemporaryString(lpddi->tszProductName).AsCString(),
Strings::GuidToString(lpddi->guidInstance).AsCString(),
static_cast<int>(Controller::GetPhysicalControllerBackend()->PluginName().length()),
Controller::GetPhysicalControllerBackend()->PluginName().data());
}
}
else
{
if (Infra::Message::WillOutputMessageOfSeverity(Infra::Message::ESeverity::Debug))
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Debug,
L"Enumerate: DirectInput device \"%s\" with instance GUID %s does not support physical controller backend \"%.*s\" and may be presented to the application.",
Infra::TemporaryString(lpddi->tszProductName).AsCString(),
Strings::GuidToString(lpddi->guidInstance).AsCString(),
static_cast<int>(Controller::GetPhysicalControllerBackend()->PluginName().length()),
Controller::GetPhysicalControllerBackend()->PluginName().data());
}
}
return DIENUM_CONTINUE;
}
template <EDirectInputVersion diVersion> BOOL __stdcall WrapperIDirectInputBase<diVersion>::
CallbackEnumDevicesFiltered(
const DirectInputTypes<diVersion>::DeviceInstanceType* lpddi, LPVOID pvRef)
{
SEnumDevicesCallbackInfo<diVersion>* callbackInfo = (SEnumDevicesCallbackInfo<diVersion>*)pvRef;
// If the device has not been seen already, add it to the set and present it to the application.
if (0 == callbackInfo->seenInstanceIdentifiers.count(lpddi->guidInstance))
{
if (Infra::Message::WillOutputMessageOfSeverity(Infra::Message::ESeverity::Info))
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Info,
L"Enumerate: Presenting DirectInput device \"%s\" (instance GUID %s) to the application.",
Infra::TemporaryString(lpddi->tszProductName).AsCString(),
Strings::GuidToString(lpddi->guidInstance).AsCString());
}
callbackInfo->seenInstanceIdentifiers.insert(lpddi->guidInstance);
callbackInfo->callbackReturnCode =
((BOOL(FAR PASCAL*)(const DirectInputTypes<diVersion>::DeviceInstanceType*, LPVOID))(
callbackInfo->lpCallback))(lpddi, callbackInfo->pvRef);
return callbackInfo->callbackReturnCode;
}
else
{
// Otherwise, just skip the device and move onto the next one.
return DIENUM_CONTINUE;
}
}
template <EDirectInputVersion diVersion>
requires (DirectInputVersionIs8<diVersion>)
HRESULT __stdcall WrapperIDirectInputVersion8Only<diVersion>::ConfigureDevices(
LPDICONFIGUREDEVICESCALLBACK lpdiCallback,
DirectInputTypes<diVersion>::ConfigureDevicesParamsType* lpdiCDParams,
DWORD dwFlags,
LPVOID pvRefData)
{
return this->underlyingDIObject->ConfigureDevices(
lpdiCallback, lpdiCDParams, dwFlags, pvRefData);
}
template <EDirectInputVersion diVersion>
requires (DirectInputVersionIs8<diVersion>)
HRESULT __stdcall WrapperIDirectInputVersion8Only<diVersion>::EnumDevicesBySemantics(
DirectInputTypes<diVersion>::ConstStringType ptszUserName,
DirectInputTypes<diVersion>::ActionFormatType* lpdiActionFormat,
DirectInputTypes<diVersion>::EnumDevicesBySemanticsCallbackType lpCallback,
LPVOID pvRef,
DWORD dwFlags)
{
// Operation not supported.
return DIERR_UNSUPPORTED;
}
template <EDirectInputVersion diVersion>
requires (DirectInputVersionIsLegacy<diVersion>)
HRESULT __stdcall WrapperIDirectInputVersionLegacyOnly<diVersion>::CreateDeviceEx(
REFGUID rguid, REFIID riid, LPVOID* lplpDirectInputDevice, LPUNKNOWN pUnkOuter)
{
if (false == DirectInputTypes<diVersion>::IsCompatibleDirectInputDeviceIID(riid))
{
Infra::Message::Output(
Infra::Message::ESeverity::Warning, L"CreateDeviceEx failed due to an invalid IID.");
return DIERR_NOINTERFACE;
}
return this->CreateDevice(
rguid,
reinterpret_cast<typename DirectInputTypes<diVersion>::IDirectInputDeviceCompatType**>(
lplpDirectInputDevice),
pUnkOuter);
}
template class WrapperIDirectInputBase<EDirectInputVersion::k8A>;
template class WrapperIDirectInputBase<EDirectInputVersion::k8W>;
template class WrapperIDirectInputBase<EDirectInputVersion::kLegacyA>;
template class WrapperIDirectInputBase<EDirectInputVersion::kLegacyW>;
template class WrapperIDirectInputVersion8Only<EDirectInputVersion::k8A>;
template class WrapperIDirectInputVersion8Only<EDirectInputVersion::k8W>;
template class WrapperIDirectInputVersionLegacyOnly<EDirectInputVersion::kLegacyA>;
template class WrapperIDirectInputVersionLegacyOnly<EDirectInputVersion::kLegacyW>;
template class WrapperIDirectInput<EDirectInputVersion::k8A>;
template class WrapperIDirectInput<EDirectInputVersion::k8W>;
template class WrapperIDirectInput<EDirectInputVersion::kLegacyA>;
template class WrapperIDirectInput<EDirectInputVersion::kLegacyW>;
} // namespace Xidi