Rewrite SDL_DINPUT_JoystickPresent method to avoid costly IDirectInput8_CreateDevice calls
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
diff --git a/src/joystick/windows/SDL_dinputjoystick.c b/src/joystick/windows/SDL_dinputjoystick.c
index d2170c5..8552d9f 100644
--- a/src/joystick/windows/SDL_dinputjoystick.c
+++ b/src/joystick/windows/SDL_dinputjoystick.c
@@ -537,60 +537,28 @@ SDL_DINPUT_JoystickDetect(JoyStick_DeviceData **pContext)
IDirectInput8_EnumDevices(dinput, DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, pContext, DIEDFL_ATTACHEDONLY);
}
-typedef struct
-{
- Uint16 vendor;
- Uint16 product;
- Uint16 version;
- SDL_bool present;
-} EnumJoystickPresentData;
-
-static BOOL CALLBACK
-EnumJoystickPresentCallback(LPCDIDEVICEINSTANCE pDeviceInstance, LPVOID pContext)
+SDL_bool
+SDL_DINPUT_JoystickPresent(Uint16 vendor_id, Uint16 product_id, Uint16 version_number)
{
-#define CHECK(exp) { if(!(exp)) goto err; }
- EnumJoystickPresentData *data = (EnumJoystickPresentData *)pContext;
+ JoyStick_DeviceData* joystick = SYS_Joystick;
Uint16 vendor = 0;
Uint16 product = 0;
- LPDIRECTINPUTDEVICE8 device = NULL;
- BOOL ret = DIENUM_CONTINUE;
-
- /* We are only supporting HID devices. */
- CHECK((pDeviceInstance->dwDevType & DIDEVTYPE_HID) != 0);
-
- CHECK(SUCCEEDED(IDirectInput8_CreateDevice(dinput, &pDeviceInstance->guidInstance, &device, NULL)));
- CHECK(QueryDeviceInfo(device, &vendor, &product));
-
- CHECK(data->vendor == vendor && data->product == product);
-
- data->present = SDL_TRUE;
- ret = DIENUM_STOP;
-
-err:
- if (device) {
- IDirectInputDevice8_Release(device);
- }
+ Uint16 version = 0;
- return ret;
-#undef CHECK
-}
+ while (joystick) {
+ SDL_GetJoystickGUIDInfo(joystick->guid, &vendor, &product, &version);
-SDL_bool
-SDL_DINPUT_JoystickPresent(Uint16 vendor, Uint16 product, Uint16 version)
-{
- EnumJoystickPresentData data;
+ if (!joystick->bXInputDevice &&
+ vendor == vendor_id &&
+ product == product_id &&
+ version == version_number) {
+ return SDL_TRUE;
+ }
- if (dinput == NULL) {
- return SDL_FALSE;
+ joystick = joystick->pNext;
}
- data.vendor = vendor;
- data.product = product;
- data.version = version;
- data.present = SDL_FALSE;
- IDirectInput8_EnumDevices(dinput, DI8DEVCLASS_GAMECTRL, EnumJoystickPresentCallback, &data, DIEDFL_ATTACHEDONLY);
-
- return data.present;
+ return SDL_FALSE;
}
static BOOL CALLBACK