From 0361c9a010977858f07119c9e72986b162b94f7f Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 27 Nov 2012 09:19:09 -0800 Subject: [PATCH] Alfred Reynolds - add scanning of XInput for controller connectedness so we can detect when the wireless controller turns on and off, the usb side doesn't change when the controller goes away --- src/joystick/windows/SDL_dxjoystick.c | 30 ++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/joystick/windows/SDL_dxjoystick.c b/src/joystick/windows/SDL_dxjoystick.c index f1df40730..8d50f2018 100644 --- a/src/joystick/windows/SDL_dxjoystick.c +++ b/src/joystick/windows/SDL_dxjoystick.c @@ -528,6 +528,9 @@ SDL_JoystickThread(void *_data) HWND messageWindow = 0; HDEVNOTIFY hNotify = 0; DEV_BROADCAST_DEVICEINTERFACE dbh; + SDL_bool bOpenedXInputDevices[4]; + + SDL_memset( bOpenedXInputDevices, 0x0, sizeof(bOpenedXInputDevices) ); result = WIN_CoInitialize(); @@ -557,6 +560,9 @@ SDL_JoystickThread(void *_data) while ( s_bJoystickThreadQuit == SDL_FALSE ) { MSG messages; + Uint8 userId; + int nCurrentOpenedXInputDevices = 0; + int nNewOpenedXInputDevices = 0; SDL_CondWaitTimeout( s_condJoystickThread, s_mutexJoyStickEnum, 300 ); while ( s_bJoystickThreadQuit == SDL_FALSE && PeekMessage(&messages, messageWindow, 0, 0, PM_NOREMOVE) ) @@ -567,7 +573,29 @@ SDL_JoystickThread(void *_data) } } - if ( s_pKnownJoystickGUIDs && s_bWindowsDeviceChanged ) + // scan for any change in XInput devices + for ( userId = 0; userId < 4; userId++ ) + { + XINPUT_CAPABILITIES capabilities; + DWORD result; + + if ( bOpenedXInputDevices[userId] == SDL_TRUE ) + nCurrentOpenedXInputDevices++; + + result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities ); + if ( result == ERROR_SUCCESS ) + { + bOpenedXInputDevices[userId] = SDL_TRUE; + nNewOpenedXInputDevices++; + } + else + { + bOpenedXInputDevices[userId] = SDL_FALSE; + } + } + + + if ( s_pKnownJoystickGUIDs && ( s_bWindowsDeviceChanged || nNewOpenedXInputDevices != nCurrentOpenedXInputDevices ) ) { SDL_Delay( 300 ); // wait for direct input to find out about this device