Sign in to follow this  

Direct Input: Detecting New Devices

This topic is 2836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Everyone, I'm using Direct Input for my game controllers and I was wondering if anyone had any advice on how to detect new controllers which are plugged in after they've been enumerated. I've tried just enumerating again but this is quite slow and not something I could call every few seconds for detection. I was wondering if there is some callback for new devices so I don't have to keep enumerating and checking for new ones myself. Any advice would be great! Many Thanks David

Share this post


Link to post
Share on other sites
Thanks for your reply.

That's not gonna do it for me. The menus in my game are quite animated and enumerating causes a noticable jump. I'd need to check every few seconds and thats not gonna look very good. There must be a simple way to check if there has been a new device added or removed without having to enumerate. Then If there is a new/lost device I don't mind a jump while I enumerate to find out what that device is.

Thanks Again
David

Share this post


Link to post
Share on other sites
Quote:
Original post by Nik02
Have you tried to handle WM_DEVICECHANGE?


Thanks Niki02! That seems to be doing the job quite nicely!


Quote:
Original post by Evil Steve
What about running a second thread to constantly enumerate devices (Or maybe wait for a few seconds before each enumeration to avoid thrashing the CPU)?


Also thanks Evil Steve, That's A Good Idea. I'll Give That A Go If WM_DEVICECHANGE Fails Me! (you always have a good answer!).

Many Thanks To You Both!
David


Share this post


Link to post
Share on other sites
This is what I do since I poll each game loop cycle.

/************************************************************************
* desc: Poll the device
*
* ret: HRESULT - result of polling
************************************************************************/

HRESULT CBaseController::Poll()
{
HRESULT hr;

// Poll the device to read the current state
if( FAILED( hr = pDevice->Poll() ) )
{
// DInput is telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done. We
// just re-acquire and try again.
hr = pDevice->Acquire();
while( hr == DIERR_INPUTLOST )
{
hr = pDevice->Acquire();
}

// If we encounter a fatal error, return failure.
if( (hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED) )
{
return E_FAIL;
}

// If another application has control of this device, return success.
// We'll just have to wait our turn to use the joystick.
if( hr == DIERR_OTHERAPPHASPRIO )
{
return E_FAIL;
}

// hr may be DIERR_OTHERAPPHASPRIO or other errors. This
// may occur when the app is minimized or in the process of
// switching, so just try again later
return E_FAIL;
}

return DI_OK;

} // Poll

Share this post


Link to post
Share on other sites

This topic is 2836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this