Lazily initialize the WGI gamepad support Initializing "Windows.Gaming.Input.Gamepad" will put Bluetooth PS4 controllers into enhanced report mode, which breaks any game using DirectInput. Let's wait to do this until absolutely necessary. (cherry picked from commit 785f57eb9102206e74ac7f9b363a37cbf037cf9d) (cherry picked from commit de849d5e6f23a7621471befb69f1b036bc346f5e)
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
diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c
index 218c0fb..4144fa4 100644
--- a/src/joystick/windows/SDL_windows_gaming_input.c
+++ b/src/joystick/windows/SDL_windows_gaming_input.c
@@ -101,6 +101,9 @@ static const IID IID_IRacingWheelStatics = { 0x3AC12CD5, 0x581B, 0x4936, { 0x9F,
static const IID IID_IRacingWheelStatics2 = { 0xE666BCAA, 0xEDFD, 0x4323, { 0xA9, 0xF6, 0x3C, 0x38, 0x40, 0x48, 0xD1, 0xED } };
/*static const IID IID_IRacingWheel = { 0xF546656F, 0xE106, 0x4C82, { 0xA9, 0x0F, 0x55, 0x40, 0x12, 0x90, 0x4B, 0x85 } };*/
+typedef HRESULT(WINAPI *WindowsCreateStringReference_t)(PCWSTR sourceString, UINT32 length, HSTRING_HEADER *hstringHeader, HSTRING *string);
+typedef HRESULT(WINAPI *RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void **factory);
+
extern SDL_bool SDL_XINPUT_Enabled(void);
extern SDL_bool SDL_DINPUT_JoystickPresent(Uint16 vendor, Uint16 product, Uint16 version);
@@ -251,6 +254,136 @@ static ULONG STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_Release(__FI
return rc;
}
+static void WGI_LoadRawGameControllerStatics()
+{
+ WindowsCreateStringReference_t WindowsCreateStringReferenceFunc = NULL;
+ RoGetActivationFactory_t RoGetActivationFactoryFunc = NULL;
+ HRESULT hr;
+
+#ifdef __WINRT__
+ WindowsCreateStringReferenceFunc = WindowsCreateStringReference;
+ RoGetActivationFactoryFunc = RoGetActivationFactory;
+#else
+ {
+ WindowsCreateStringReferenceFunc = (WindowsCreateStringReference_t)WIN_LoadComBaseFunction("WindowsCreateStringReference");
+ RoGetActivationFactoryFunc = (RoGetActivationFactory_t)WIN_LoadComBaseFunction("RoGetActivationFactory");
+ }
+#endif /* __WINRT__ */
+ if (WindowsCreateStringReferenceFunc && RoGetActivationFactoryFunc) {
+ PCWSTR pNamespace;
+ HSTRING_HEADER hNamespaceStringHeader;
+ HSTRING hNamespaceString;
+
+ pNamespace = L"Windows.Gaming.Input.RawGameController";
+ hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
+ if (SUCCEEDED(hr)) {
+ hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IRawGameControllerStatics, (void **)&wgi.statics);
+ if (!SUCCEEDED(hr)) {
+ SDL_SetError("Couldn't find IRawGameControllerStatics: 0x%lx", hr);
+ }
+ }
+ }
+}
+
+static void WGI_LoadOtherControllerStatics()
+{
+ WindowsCreateStringReference_t WindowsCreateStringReferenceFunc = NULL;
+ RoGetActivationFactory_t RoGetActivationFactoryFunc = NULL;
+ HRESULT hr;
+
+#ifdef __WINRT__
+ WindowsCreateStringReferenceFunc = WindowsCreateStringReference;
+ RoGetActivationFactoryFunc = RoGetActivationFactory;
+#else
+ {
+ WindowsCreateStringReferenceFunc = (WindowsCreateStringReference_t)WIN_LoadComBaseFunction("WindowsCreateStringReference");
+ RoGetActivationFactoryFunc = (RoGetActivationFactory_t)WIN_LoadComBaseFunction("RoGetActivationFactory");
+ }
+#endif /* __WINRT__ */
+ if (WindowsCreateStringReferenceFunc && RoGetActivationFactoryFunc) {
+ PCWSTR pNamespace;
+ HSTRING_HEADER hNamespaceStringHeader;
+ HSTRING hNamespaceString;
+
+ pNamespace = L"Windows.Gaming.Input.ArcadeStick";
+ hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
+ if (SUCCEEDED(hr)) {
+ hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IArcadeStickStatics, (void **)&wgi.arcade_stick_statics);
+ if (SUCCEEDED(hr)) {
+ __x_ABI_CWindows_CGaming_CInput_CIArcadeStickStatics_QueryInterface(wgi.arcade_stick_statics, &IID_IArcadeStickStatics2, (void **)&wgi.arcade_stick_statics2);
+ } else {
+ SDL_SetError("Couldn't find IID_IArcadeStickStatics: 0x%lx", hr);
+ }
+ }
+
+ pNamespace = L"Windows.Gaming.Input.FlightStick";
+ hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
+ if (SUCCEEDED(hr)) {
+ hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IFlightStickStatics, (void **)&wgi.flight_stick_statics);
+ if (!SUCCEEDED(hr)) {
+ SDL_SetError("Couldn't find IID_IFlightStickStatics: 0x%lx", hr);
+ }
+ }
+
+ pNamespace = L"Windows.Gaming.Input.Gamepad";
+ hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
+ if (SUCCEEDED(hr)) {
+ hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IGamepadStatics, (void **)&wgi.gamepad_statics);
+ if (SUCCEEDED(hr)) {
+ __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_QueryInterface(wgi.gamepad_statics, &IID_IGamepadStatics2, (void **)&wgi.gamepad_statics2);
+ } else {
+ SDL_SetError("Couldn't find IGamepadStatics: 0x%lx", hr);
+ }
+ }
+
+ pNamespace = L"Windows.Gaming.Input.RacingWheel";
+ hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
+ if (SUCCEEDED(hr)) {
+ hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IRacingWheelStatics, (void **)&wgi.racing_wheel_statics);
+ if (SUCCEEDED(hr)) {
+ __x_ABI_CWindows_CGaming_CInput_CIRacingWheelStatics_QueryInterface(wgi.racing_wheel_statics, &IID_IRacingWheelStatics2, (void **)&wgi.racing_wheel_statics2);
+ } else {
+ SDL_SetError("Couldn't find IRacingWheelStatics: 0x%lx", hr);
+ }
+ }
+ }
+}
+
+static SDL_JoystickType GetGameControllerType(__x_ABI_CWindows_CGaming_CInput_CIGameController *gamecontroller)
+{
+ __x_ABI_CWindows_CGaming_CInput_CIArcadeStick *arcade_stick = NULL;
+ __x_ABI_CWindows_CGaming_CInput_CIFlightStick *flight_stick = NULL;
+ __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad = NULL;
+ __x_ABI_CWindows_CGaming_CInput_CIRacingWheel *racing_wheel = NULL;
+
+ /* Wait to initialize these interfaces until we need them.
+ * Initializing the gamepad interface will switch Bluetooth PS4 controllers into enhanced mode, breaking DirectInput
+ */
+ WGI_LoadOtherControllerStatics();
+
+ if (wgi.gamepad_statics2 && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics2_FromGameController(wgi.gamepad_statics2, gamecontroller, &gamepad)) && gamepad) {
+ __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
+ return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
+ }
+
+ if (wgi.arcade_stick_statics2 && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIArcadeStickStatics2_FromGameController(wgi.arcade_stick_statics2, gamecontroller, &arcade_stick)) && arcade_stick) {
+ __x_ABI_CWindows_CGaming_CInput_CIArcadeStick_Release(arcade_stick);
+ return SDL_JOYSTICK_TYPE_ARCADE_STICK;
+ }
+
+ if (wgi.flight_stick_statics && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIFlightStickStatics_FromGameController(wgi.flight_stick_statics, gamecontroller, &flight_stick)) && flight_stick) {
+ __x_ABI_CWindows_CGaming_CInput_CIFlightStick_Release(flight_stick);
+ return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
+ }
+
+ if (wgi.racing_wheel_statics2 && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIRacingWheelStatics2_FromGameController(wgi.racing_wheel_statics2, gamecontroller, &racing_wheel)) && racing_wheel) {
+ __x_ABI_CWindows_CGaming_CInput_CIRacingWheel_Release(racing_wheel);
+ return SDL_JOYSTICK_TYPE_WHEEL;
+ }
+
+ return SDL_JOYSTICK_TYPE_UNKNOWN;
+}
+
static HRESULT STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_InvokeAdded(__FIEventHandler_1_Windows__CGaming__CInput__CRawGameController *This, IInspectable *sender, __x_ABI_CWindows_CGaming_CInput_CIRawGameController *e)
{
HRESULT hr;
@@ -313,38 +446,6 @@ static HRESULT STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_InvokeAdde
name = SDL_strdup("");
}
- hr = __x_ABI_CWindows_CGaming_CInput_CIRawGameController_QueryInterface(controller, &IID_IGameController, (void **)&gamecontroller);
- if (SUCCEEDED(hr)) {
- __x_ABI_CWindows_CGaming_CInput_CIArcadeStick *arcade_stick = NULL;
- __x_ABI_CWindows_CGaming_CInput_CIFlightStick *flight_stick = NULL;
- __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad = NULL;
- __x_ABI_CWindows_CGaming_CInput_CIRacingWheel *racing_wheel = NULL;
- boolean wireless;
-
- if (wgi.gamepad_statics2 && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics2_FromGameController(wgi.gamepad_statics2, gamecontroller, &gamepad)) && gamepad) {
- type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
- __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
- } else if (wgi.arcade_stick_statics2 && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIArcadeStickStatics2_FromGameController(wgi.arcade_stick_statics2, gamecontroller, &arcade_stick)) && arcade_stick) {
- type = SDL_JOYSTICK_TYPE_ARCADE_STICK;
- __x_ABI_CWindows_CGaming_CInput_CIArcadeStick_Release(arcade_stick);
- } else if (wgi.flight_stick_statics && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIFlightStickStatics_FromGameController(wgi.flight_stick_statics, gamecontroller, &flight_stick)) && flight_stick) {
- type = SDL_JOYSTICK_TYPE_FLIGHT_STICK;
- __x_ABI_CWindows_CGaming_CInput_CIFlightStick_Release(flight_stick);
- } else if (wgi.racing_wheel_statics2 && SUCCEEDED(__x_ABI_CWindows_CGaming_CInput_CIRacingWheelStatics2_FromGameController(wgi.racing_wheel_statics2, gamecontroller, &racing_wheel)) && racing_wheel) {
- type = SDL_JOYSTICK_TYPE_WHEEL;
- __x_ABI_CWindows_CGaming_CInput_CIRacingWheel_Release(racing_wheel);
- }
-
- hr = __x_ABI_CWindows_CGaming_CInput_CIGameController_get_IsWireless(gamecontroller, &wireless);
- if (SUCCEEDED(hr) && wireless) {
- bus = SDL_HARDWARE_BUS_BLUETOOTH;
- }
-
- __x_ABI_CWindows_CGaming_CInput_CIGameController_Release(gamecontroller);
- }
-
- guid = SDL_CreateJoystickGUID(bus, vendor, product, version, name, 'w', (Uint8)type);
-
#ifdef SDL_JOYSTICK_HIDAPI
if (!ignore_joystick && HIDAPI_IsDevicePresent(vendor, product, version, name)) {
ignore_joystick = SDL_TRUE;
@@ -365,13 +466,29 @@ static HRESULT STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_InvokeAdde
ignore_joystick = SDL_TRUE;
}
- if (!ignore_joystick && SDL_ShouldIgnoreJoystick(name, guid)) {
- ignore_joystick = SDL_TRUE;
+ if (!ignore_joystick) {
+ hr = __x_ABI_CWindows_CGaming_CInput_CIRawGameController_QueryInterface(controller, &IID_IGameController, (void **)&gamecontroller);
+ if (SUCCEEDED(hr)) {
+ boolean wireless;
+
+ type = GetGameControllerType(gamecontroller);
+
+ hr = __x_ABI_CWindows_CGaming_CInput_CIGameController_get_IsWireless(gamecontroller, &wireless);
+ if (SUCCEEDED(hr) && wireless) {
+ bus = SDL_HARDWARE_BUS_BLUETOOTH;
+ }
+
+ __x_ABI_CWindows_CGaming_CInput_CIGameController_Release(gamecontroller);
+ }
+
+ guid = SDL_CreateJoystickGUID(bus, vendor, product, version, name, 'w', (Uint8)type);
+
+ if (SDL_ShouldIgnoreJoystick(name, guid)) {
+ ignore_joystick = SDL_TRUE;
+ }
}
- if (ignore_joystick) {
- SDL_free(name);
- } else {
+ if (!ignore_joystick) {
/* New device, add it */
WindowsGamingInputControllerState *controllers = SDL_realloc(wgi.controllers, sizeof(wgi.controllers[0]) * (wgi.controller_count + 1));
if (controllers) {
@@ -398,6 +515,8 @@ static HRESULT STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_InvokeAdde
} else {
SDL_free(name);
}
+ } else {
+ SDL_free(name);
}
__x_ABI_CWindows_CGaming_CInput_CIRawGameController_Release(controller);
@@ -476,11 +595,6 @@ static RawGameControllerDelegate controller_removed = {
static int WGI_JoystickInit(void)
{
- typedef HRESULT(WINAPI * WindowsCreateStringReference_t)(PCWSTR sourceString, UINT32 length, HSTRING_HEADER * hstringHeader, HSTRING * string);
- typedef HRESULT(WINAPI * RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void **factory);
-
- WindowsCreateStringReference_t WindowsCreateStringReferenceFunc = NULL;
- RoGetActivationFactory_t RoGetActivationFactoryFunc = NULL;
HRESULT hr;
if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_WGI, SDL_TRUE)) {
@@ -515,71 +629,7 @@ static int WGI_JoystickInit(void)
}
#endif
-#ifdef __WINRT__
- WindowsCreateStringReferenceFunc = WindowsCreateStringReference;
- RoGetActivationFactoryFunc = RoGetActivationFactory;
-#else
- {
- WindowsCreateStringReferenceFunc = (WindowsCreateStringReference_t)WIN_LoadComBaseFunction("WindowsCreateStringReference");
- RoGetActivationFactoryFunc = (RoGetActivationFactory_t)WIN_LoadComBaseFunction("RoGetActivationFactory");
- }
-#endif /* __WINRT__ */
- if (WindowsCreateStringReferenceFunc && RoGetActivationFactoryFunc) {
- PCWSTR pNamespace;
- HSTRING_HEADER hNamespaceStringHeader;
- HSTRING hNamespaceString;
-
- pNamespace = L"Windows.Gaming.Input.RawGameController";
- hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
- if (SUCCEEDED(hr)) {
- hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IRawGameControllerStatics, (void **)&wgi.statics);
- if (!SUCCEEDED(hr)) {
- SDL_SetError("Couldn't find IRawGameControllerStatics: 0x%lx", hr);
- }
- }
-
- pNamespace = L"Windows.Gaming.Input.ArcadeStick";
- hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
- if (SUCCEEDED(hr)) {
- hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IArcadeStickStatics, (void **)&wgi.arcade_stick_statics);
- if (SUCCEEDED(hr)) {
- __x_ABI_CWindows_CGaming_CInput_CIArcadeStickStatics_QueryInterface(wgi.arcade_stick_statics, &IID_IArcadeStickStatics2, (void **)&wgi.arcade_stick_statics2);
- } else {
- SDL_SetError("Couldn't find IID_IArcadeStickStatics: 0x%lx", hr);
- }
- }
-
- pNamespace = L"Windows.Gaming.Input.FlightStick";
- hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
- if (SUCCEEDED(hr)) {
- hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IFlightStickStatics, (void **)&wgi.flight_stick_statics);
- if (!SUCCEEDED(hr)) {
- SDL_SetError("Couldn't find IID_IFlightStickStatics: 0x%lx", hr);
- }
- }
-
- pNamespace = L"Windows.Gaming.Input.Gamepad";
- hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
- if (SUCCEEDED(hr)) {
- hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IGamepadStatics, (void **)&wgi.gamepad_statics);
- if (SUCCEEDED(hr)) {
- __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_QueryInterface(wgi.gamepad_statics, &IID_IGamepadStatics2, (void **)&wgi.gamepad_statics2);
- } else {
- SDL_SetError("Couldn't find IGamepadStatics: 0x%lx", hr);
- }
- }
-
- pNamespace = L"Windows.Gaming.Input.RacingWheel";
- hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
- if (SUCCEEDED(hr)) {
- hr = RoGetActivationFactoryFunc(hNamespaceString, &IID_IRacingWheelStatics, (void **)&wgi.racing_wheel_statics);
- if (SUCCEEDED(hr)) {
- __x_ABI_CWindows_CGaming_CInput_CIRacingWheelStatics_QueryInterface(wgi.racing_wheel_statics, &IID_IRacingWheelStatics2, (void **)&wgi.racing_wheel_statics2);
- } else {
- SDL_SetError("Couldn't find IRacingWheelStatics: 0x%lx", hr);
- }
- }
- }
+ WGI_LoadRawGameControllerStatics();
if (wgi.statics) {
__FIVectorView_1_Windows__CGaming__CInput__CRawGameController *controllers;