src/joystick/windows/SDL_dxjoystick.c
changeset 6712 05f046f5886b
parent 6710 e650705e2c1d
child 6716 1616f6b3738c
     1.1 --- a/src/joystick/windows/SDL_dxjoystick.c	Tue Nov 27 21:40:46 2012 -0800
     1.2 +++ b/src/joystick/windows/SDL_dxjoystick.c	Wed Nov 28 11:52:38 2012 -0800
     1.3 @@ -498,24 +498,6 @@
     1.4  }
     1.5  
     1.6  
     1.7 -/*  helper func to create a hidden, message only window for the joystick detect thread
     1.8 - */
     1.9 -HWND CreateHiddenJoystickDetectWindow() {
    1.10 -	WNDCLASSEX wincl;
    1.11 -	HWND hMessageWindow = 0;
    1.12 -	SDL_memset( &wincl, 0x0, sizeof(wincl) );
    1.13 -	wincl.hInstance = GetModuleHandle( NULL );
    1.14 -	wincl.lpszClassName = L"Message";
    1.15 -	wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc;      // This function is called by windows
    1.16 -	wincl.cbSize = sizeof (WNDCLASSEX);
    1.17 -
    1.18 -	if (!RegisterClassEx (&wincl))
    1.19 -		return 0;
    1.20 -
    1.21 -	hMessageWindow = (HWND)CreateWindowEx( 0,  L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL );
    1.22 -	return hMessageWindow;
    1.23 -}
    1.24 -
    1.25  DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, \
    1.26  	0xC0, 0x4F, 0xB9, 0x51, 0xED);
    1.27  
    1.28 @@ -529,12 +511,26 @@
    1.29  	HDEVNOTIFY hNotify = 0;
    1.30  	DEV_BROADCAST_DEVICEINTERFACE dbh;
    1.31  	SDL_bool bOpenedXInputDevices[4];
    1.32 +	WNDCLASSEX wincl;
    1.33  
    1.34  	SDL_memset( bOpenedXInputDevices, 0x0, sizeof(bOpenedXInputDevices) );
    1.35  
    1.36  	result = WIN_CoInitialize();
    1.37  
    1.38 -	messageWindow = CreateHiddenJoystickDetectWindow();
    1.39 +	SDL_memset( &wincl, 0x0, sizeof(wincl) );
    1.40 +	wincl.hInstance = GetModuleHandle( NULL );
    1.41 +	wincl.lpszClassName = L"Message";
    1.42 +	wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc;      // This function is called by windows
    1.43 +	wincl.cbSize = sizeof (WNDCLASSEX);
    1.44 +
    1.45 +	if (!RegisterClassEx (&wincl))
    1.46 +	{		
    1.47 +		SDL_SetError("Failed to create register class for joystick autodetect.",
    1.48 +		GetLastError());
    1.49 +		return -1;
    1.50 +	}
    1.51 +
    1.52 +	messageWindow = (HWND)CreateWindowEx( 0,  L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL );
    1.53  	if ( !messageWindow )
    1.54  	{
    1.55  		SDL_SetError("Failed to create message window for joystick autodetect.",
    1.56 @@ -611,6 +607,8 @@
    1.57  
    1.58  	if ( messageWindow )
    1.59  		DestroyWindow( messageWindow );
    1.60 +
    1.61 +	UnregisterClass( wincl.lpszClassName, wincl.hInstance );
    1.62  	messageWindow = 0;
    1.63  	WIN_CoUninitialize();
    1.64  	return 1;
    1.65 @@ -720,22 +718,25 @@
    1.66  	EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
    1.67  {
    1.68  	JoyStick_DeviceData *pNewJoystick;
    1.69 +	JoyStick_DeviceData *pPrevJoystick = NULL;
    1.70  	SDL_bool bXInputDevice;
    1.71  	pNewJoystick = *(JoyStick_DeviceData **)pContext;
    1.72  	while ( pNewJoystick )
    1.73  	{
    1.74  		if ( !SDL_memcmp( &pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance) ) )
    1.75  		{
    1.76 -			if ( SYS_Joystick )
    1.77 -			{
    1.78 -				pNewJoystick->pNext = SYS_Joystick;
    1.79 -			}
    1.80 -			SYS_Joystick = pNewJoystick;
    1.81  			/* if we are replacing the front of the list then update it */
    1.82  			if ( pNewJoystick == *(JoyStick_DeviceData **)pContext ) 
    1.83  			{
    1.84  				*(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
    1.85  			}
    1.86 +			else if ( pPrevJoystick )
    1.87 +			{
    1.88 +				pPrevJoystick->pNext = pNewJoystick->pNext;
    1.89 +			}
    1.90 +
    1.91 +			pNewJoystick->pNext = SYS_Joystick;
    1.92 +			SYS_Joystick = pNewJoystick;
    1.93  
    1.94  			s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
    1.95  			s_iNewGUID++;
    1.96 @@ -745,6 +746,7 @@
    1.97  				return DIENUM_STOP; 
    1.98  		}
    1.99  
   1.100 +		pPrevJoystick = pNewJoystick;
   1.101  		pNewJoystick = pNewJoystick->pNext;
   1.102  	}
   1.103  
   1.104 @@ -756,17 +758,17 @@
   1.105  
   1.106  	if ( bXInputDevice )
   1.107  	{
   1.108 -		SDL_memset(&(pNewJoystick->dxdevice), 0x0,
   1.109 -			sizeof(DIDEVICEINSTANCE));
   1.110  		pNewJoystick->bXInputDevice = SDL_TRUE;
   1.111  		pNewJoystick->XInputUserId = INVALID_XINPUT_USERID;
   1.112  	}
   1.113  	else
   1.114  	{
   1.115  		pNewJoystick->bXInputDevice = SDL_FALSE;
   1.116 -		SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
   1.117 -			sizeof(DIDEVICEINSTANCE));
   1.118  	}
   1.119 +	
   1.120 +	SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
   1.121 +		sizeof(DIDEVICEINSTANCE));
   1.122 +
   1.123  	pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
   1.124  	pNewJoystick->send_add_event = 1;
   1.125  	pNewJoystick->nInstanceID = ++s_nInstanceID;