From a1fcc64baf7988a36deef113fe4f3827ed400d2c Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Thu, 9 Feb 2006 00:53:20 +0000 Subject: [PATCH] poll DInput for number of joysticks instead of polling registry for num HID devices. Fixes us resetting INPUTMAN too early (after Windows sees the HID device but before DirectInput exposes it) --- .../InputHandler/InputHandler_DirectInput.cpp | 42 ++++++++++++------- .../InputHandler/InputHandler_DirectInput.h | 2 +- .../InputHandler_DirectInputHelper.cpp | 4 +- .../InputHandler_DirectInputHelper.h | 2 +- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/stepmania/src/arch/InputHandler/InputHandler_DirectInput.cpp b/stepmania/src/arch/InputHandler/InputHandler_DirectInput.cpp index ae8edf777e..81d89598b7 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_DirectInput.cpp +++ b/stepmania/src/arch/InputHandler/InputHandler_DirectInput.cpp @@ -16,7 +16,7 @@ static vector Devices; /* Number of joysticks found: */ static int g_iNumJoysticks; -static BOOL CALLBACK EnumDevices( const DIDEVICEINSTANCE *pdidInstance, void *pContext ) +static BOOL CALLBACK EnumDevicesCallback( const DIDEVICEINSTANCE *pdidInstance, void *pContext ) { DIDevice device; @@ -60,13 +60,22 @@ static void CheckForDirectInputDebugMode() } } -static int GetNumUsbHidDevices() + +static BOOL CALLBACK CountDevicesCallback( const DIDEVICEINSTANCE *pdidInstance, void *pContext ) { - // The "Enum" key doesn't exist if no hid devices are attached, so it's expected that GetRegValue will sometimes fail. - // TODO: Does this work in Win98 and Win2K? - int i = 0; - RegistryAccess::GetRegValue( "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\HidUsb\\Enum", "Count", i, false ); // don't warn on error - return i; + (*(int*)pContext)++; + return DIENUM_CONTINUE; +} + +static int GetNumJoysticksQuick() +{ + int iCount = 0; + HRESULT hr = g_dinput->EnumDevices( DIDEVTYPE_JOYSTICK, CountDevicesCallback, &iCount, DIEDFL_ATTACHEDONLY ); + if( hr != DI_OK ) + { + LOG->Warn( hr_ssprintf(hr, "g_dinput->EnumDevices") ); + } + return iCount; } InputHandler_DInput::InputHandler_DInput() @@ -76,21 +85,20 @@ InputHandler_DInput::InputHandler_DInput() CheckForDirectInputDebugMode(); m_bShutdown = false; - m_iLastSeenNumUsbHid = GetNumUsbHidDevices(); g_iNumJoysticks = 0; AppInstance inst; - HRESULT hr = DirectInputCreate(inst.Get(), DIRECTINPUT_VERSION, &dinput, NULL); + HRESULT hr = DirectInputCreate(inst.Get(), DIRECTINPUT_VERSION, &g_dinput, NULL); if( hr != DI_OK ) RageException::Throw( hr_ssprintf(hr, "InputHandler_DInput: DirectInputCreate") ); LOG->Trace( "InputHandler_DInput: IDirectInput::EnumDevices(DIDEVTYPE_KEYBOARD)" ); - hr = dinput->EnumDevices( DIDEVTYPE_KEYBOARD, EnumDevices, NULL, DIEDFL_ATTACHEDONLY ); + hr = g_dinput->EnumDevices( DIDEVTYPE_KEYBOARD, EnumDevicesCallback, NULL, DIEDFL_ATTACHEDONLY ); if( hr != DI_OK ) RageException::Throw( hr_ssprintf(hr, "InputHandler_DInput: IDirectInput::EnumDevices") ); LOG->Trace( "InputHandler_DInput: IDirectInput::EnumDevices(DIDEVTYPE_JOYSTICK)" ); - hr = dinput->EnumDevices( DIDEVTYPE_JOYSTICK, EnumDevices, NULL, DIEDFL_ATTACHEDONLY ); + hr = g_dinput->EnumDevices( DIDEVTYPE_JOYSTICK, EnumDevicesCallback, NULL, DIEDFL_ATTACHEDONLY ); if( hr != DI_OK ) RageException::Throw( hr_ssprintf(hr, "InputHandler_DInput: IDirectInput::EnumDevices") ); @@ -116,6 +124,8 @@ InputHandler_DInput::InputHandler_DInput() Devices[i].buffered? "buffered": "unbuffered" ); } + m_iLastSeenNumJoysticks = GetNumJoysticksQuick(); + StartThread(); } @@ -149,8 +159,8 @@ InputHandler_DInput::~InputHandler_DInput() Devices[i].Close(); Devices.clear(); - dinput->Release(); - dinput = NULL; + g_dinput->Release(); + g_dinput = NULL; } void InputHandler_DInput::WindowReset() @@ -505,9 +515,9 @@ void InputHandler_DInput::Update() bool InputHandler_DInput::DevicesChanged() { - int iOldNumUsbHid = m_iLastSeenNumUsbHid; - m_iLastSeenNumUsbHid = GetNumUsbHidDevices(); - return iOldNumUsbHid != m_iLastSeenNumUsbHid; + int iOldNumJoysticks = m_iLastSeenNumJoysticks; + m_iLastSeenNumJoysticks = GetNumJoysticksQuick(); + return iOldNumJoysticks != m_iLastSeenNumJoysticks; } void InputHandler_DInput::InputThreadMain() diff --git a/stepmania/src/arch/InputHandler/InputHandler_DirectInput.h b/stepmania/src/arch/InputHandler/InputHandler_DirectInput.h index 87efb8a46e..afc6d34490 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_DirectInput.h +++ b/stepmania/src/arch/InputHandler/InputHandler_DirectInput.h @@ -18,7 +18,7 @@ public: private: RageThread m_Thread; bool m_bShutdown; - int m_iLastSeenNumUsbHid; // use this to figure out if a device was plugged/unplugged + int m_iLastSeenNumJoysticks; // use this to figure out if a joystick was plugged/unplugged void UpdatePolled( DIDevice &device, const RageTimer &tm ); void UpdateBuffered( DIDevice &device, const RageTimer &tm ); diff --git a/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.cpp b/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.cpp index b0ab49f455..578614bf6f 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.cpp +++ b/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.cpp @@ -10,7 +10,7 @@ #pragma comment(lib, "dxguid.lib") #endif #endif -LPDIRECTINPUT dinput = NULL; +LPDIRECTINPUT g_dinput = NULL; static int ConvertScancodeToKey( int scancode ); static BOOL CALLBACK DIJoystick_EnumDevObjectsProc(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID data); @@ -30,7 +30,7 @@ bool DIDevice::Open() buffered = true; LPDIRECTINPUTDEVICE tmpdevice; - HRESULT hr = dinput->CreateDevice( JoystickInst.guidInstance, &tmpdevice, NULL ); + HRESULT hr = g_dinput->CreateDevice( JoystickInst.guidInstance, &tmpdevice, NULL ); if ( hr != DI_OK ) { LOG->Info( hr_ssprintf(hr, "OpenDevice: IDirectInput_CreateDevice") ); diff --git a/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.h b/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.h index 5f39ceb3d3..af8a277a80 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.h +++ b/stepmania/src/arch/InputHandler/InputHandler_DirectInputHelper.h @@ -5,7 +5,7 @@ #define DIRECTINPUT_VERSION 0x0500 #include -extern LPDIRECTINPUT dinput; +extern LPDIRECTINPUT g_dinput; #define INPUT_QSIZE 32