SDL_DINPUT_JoystickPresent() needs to do the full device enumeration It is called from WGI before the normal joystick detection has been run, so it needs to actually enumerate currently connected devices. We can skip the logic checking for other drivers also supporting this device, because that logic is duplicated from the call site.
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
diff --git a/src/joystick/windows/SDL_dinputjoystick.c b/src/joystick/windows/SDL_dinputjoystick.c
index 98c6499..557cab0 100644
--- a/src/joystick/windows/SDL_dinputjoystick.c
+++ b/src/joystick/windows/SDL_dinputjoystick.c
@@ -425,7 +425,7 @@ SDL_DINPUT_JoystickInit(void)
/* helper function for direct input, gets called for each connected joystick */
static BOOL CALLBACK
-EnumJoysticksCallback(LPCDIDEVICEINSTANCE pDeviceInstance, LPVOID pContext)
+EnumJoystickDetectCallback(LPCDIDEVICEINSTANCE pDeviceInstance, LPVOID pContext)
{
#define CHECK(exp) { if(!(exp)) goto err; }
JoyStick_DeviceData *pNewJoystick = NULL;
@@ -534,31 +534,56 @@ err:
void
SDL_DINPUT_JoystickDetect(JoyStick_DeviceData **pContext)
{
- IDirectInput8_EnumDevices(dinput, DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, pContext, DIEDFL_ATTACHEDONLY);
+ IDirectInput8_EnumDevices(dinput, DI8DEVCLASS_GAMECTRL, EnumJoystickDetectCallback, pContext, DIEDFL_ATTACHEDONLY);
}
-SDL_bool
-SDL_DINPUT_JoystickPresent(Uint16 vendor_id, Uint16 product_id, Uint16 version_number)
+/* helper function for direct input, gets called for each connected joystick */
+typedef struct
{
- JoyStick_DeviceData* joystick = SYS_Joystick;
+ Uint16 vendor;
+ Uint16 product;
+ SDL_bool present;
+} Joystick_PresentData;
+
+static BOOL CALLBACK
+EnumJoystickPresentCallback(LPCDIDEVICEINSTANCE pDeviceInstance, LPVOID pContext)
+{
+#define CHECK(exp) { if(!(exp)) goto err; }
+ Joystick_PresentData *pData = (Joystick_PresentData *)pContext;
Uint16 vendor = 0;
Uint16 product = 0;
- Uint16 version = 0;
+ LPDIRECTINPUTDEVICE8 device = NULL;
- while (joystick) {
- SDL_GetJoystickGUIDInfo(joystick->guid, &vendor, &product, &version);
+ /* We are only supporting HID devices. */
+ CHECK((pDeviceInstance->dwDevType & DIDEVTYPE_HID) != 0);
- if (!joystick->bXInputDevice &&
- vendor == vendor_id &&
- product == product_id &&
- version == version_number) {
- return SDL_TRUE;
- }
+ CHECK(SUCCEEDED(IDirectInput8_CreateDevice(dinput, &pDeviceInstance->guidInstance, &device, NULL)));
+ CHECK(QueryDeviceInfo(device, &vendor, &product));
- joystick = joystick->pNext;
+ if (vendor == pData->vendor && product == pData->product) {
+ pData->present = SDL_TRUE;
+ return DIENUM_STOP; /* get next device, please */
}
- return SDL_FALSE;
+err:
+ if (device) {
+ IDirectInputDevice8_Release(device);
+ }
+
+ return DIENUM_CONTINUE; /* get next device, please */
+#undef CHECK
+}
+
+SDL_bool
+SDL_DINPUT_JoystickPresent(Uint16 vendor_id, Uint16 product_id, Uint16 version_number)
+{
+ Joystick_PresentData data;
+
+ data.vendor = vendor_id;
+ data.product = product_id;
+ data.present = SDL_FALSE;
+ IDirectInput8_EnumDevices(dinput, DI8DEVCLASS_GAMECTRL, EnumJoystickPresentCallback, &data, DIEDFL_ATTACHEDONLY);
+ return data.present;
}
static BOOL CALLBACK