src/joystick/windows/SDL_dxjoystick.c
changeset 6965 ab4bb4e99961
parent 6885 700f1b25f77f
child 6971 5ded971ee761
equal deleted inserted replaced
6964:c8aa24f05042 6965:ab4bb4e99961
    71 static SDL_cond *s_condJoystickThread = NULL;
    71 static SDL_cond *s_condJoystickThread = NULL;
    72 static SDL_mutex *s_mutexJoyStickEnum = NULL;
    72 static SDL_mutex *s_mutexJoyStickEnum = NULL;
    73 static SDL_Thread *s_threadJoystick = NULL;
    73 static SDL_Thread *s_threadJoystick = NULL;
    74 static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
    74 static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
    75 static HANDLE s_pXInputDLL = 0;
    75 static HANDLE s_pXInputDLL = 0;
       
    76 static SDL_bool s_bXInputEnabled = SDL_TRUE;
    76 
    77 
    77 extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
    78 extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
    78                                        LPDIRECTINPUT * ppDI,
    79                                        LPDIRECTINPUT * ppDI,
    79                                        LPUNKNOWN punkOuter);
    80                                        LPUNKNOWN punkOuter);
    80 struct JoyStick_DeviceData_
    81 struct JoyStick_DeviceData_
   362 	UINT                    iDevice        = 0;
   363 	UINT                    iDevice        = 0;
   363 	VARIANT                 var;
   364 	VARIANT                 var;
   364 	HRESULT                 hr;
   365 	HRESULT                 hr;
   365 	DWORD bCleanupCOM;
   366 	DWORD bCleanupCOM;
   366 
   367 
       
   368     if (!s_bXInputEnabled)
       
   369     {
       
   370         return SDL_FALSE;
       
   371     }
       
   372 
   367 	SDL_memset( pDevices, 0x0, sizeof(pDevices) );
   373 	SDL_memset( pDevices, 0x0, sizeof(pDevices) );
   368 
   374 
   369 	// CoInit if needed
   375 	// CoInit if needed
   370 	hr = CoInitialize(NULL);
   376 	hr = CoInitialize(NULL);
   371 	bCleanupCOM = SUCCEEDED(hr);
   377 	bCleanupCOM = SUCCEEDED(hr);
   567 				TranslateMessage(&messages);
   573 				TranslateMessage(&messages);
   568 				DispatchMessage(&messages);
   574 				DispatchMessage(&messages);
   569 			}
   575 			}
   570 		}
   576 		}
   571 
   577 
   572 		if ( XINPUTGETCAPABILITIES )
   578 		if ( s_bXInputEnabled && XINPUTGETCAPABILITIES )
   573 		{
   579 		{
   574 			// scan for any change in XInput devices
   580 			// scan for any change in XInput devices
   575 			for ( userId = 0; userId < 4; userId++ )
   581 			for ( userId = 0; userId < 4; userId++ )
   576 			{
   582 			{
   577 				XINPUT_CAPABILITIES	capabilities;
   583 				XINPUT_CAPABILITIES	capabilities;
   625 int
   631 int
   626 SDL_SYS_JoystickInit(void)
   632 SDL_SYS_JoystickInit(void)
   627 {
   633 {
   628     HRESULT result;
   634     HRESULT result;
   629     HINSTANCE instance;
   635     HINSTANCE instance;
       
   636 	const char *env = SDL_GetHint(SD_HINT_XINPUT_ENABLED);
       
   637 	if (env && !SDL_atoi(env)) {
       
   638 		s_bXInputEnabled = SDL_FALSE;
       
   639 	}
   630 
   640 
   631     result = WIN_CoInitialize();
   641     result = WIN_CoInitialize();
   632     if (FAILED(result)) {
   642     if (FAILED(result)) {
   633         SetDIerror("CoInitialize", result);
   643         SetDIerror("CoInitialize", result);
   634         return (-1);
   644         return (-1);
   664 	s_mutexJoyStickEnum = SDL_CreateMutex();
   674 	s_mutexJoyStickEnum = SDL_CreateMutex();
   665 	s_condJoystickThread = SDL_CreateCond();
   675 	s_condJoystickThread = SDL_CreateCond();
   666 	s_bDeviceAdded = SDL_TRUE; // force a scan of the system for joysticks this first time
   676 	s_bDeviceAdded = SDL_TRUE; // force a scan of the system for joysticks this first time
   667 	SDL_SYS_JoystickDetect();
   677 	SDL_SYS_JoystickDetect();
   668 
   678 
   669 	// try to load XInput support if available
   679     if (s_bXInputEnabled) {
   670 	s_pXInputDLL = LoadLibrary( L"XInput1_3.dll" );
   680 		// try to load XInput support if available
   671 	if ( !s_pXInputDLL )
   681 		s_pXInputDLL = LoadLibrary( L"XInput1_3.dll" );
   672 		s_pXInputDLL = LoadLibrary( L"bin\\XInput1_3.dll" );
   682 		if ( !s_pXInputDLL )
   673 	if ( s_pXInputDLL )
   683 			s_pXInputDLL = LoadLibrary( L"bin\\XInput1_3.dll" );
   674 	{
   684 		if ( s_pXInputDLL )
   675 		// 100 is the ordinal for _XInputGetStateEx, which returns the same struct as XinputGetState, but with extra data in wButtons for the guide button, we think...
       
   676 		PC_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, (LPCSTR)100 );
       
   677 		PC_XInputSetState = (XInputSetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputSetState" );
       
   678 		PC_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetCapabilities" );
       
   679 		if ( !PC_XInputGetState || !PC_XInputSetState || !PC_XInputGetCapabilities )
       
   680 		{
   685 		{
   681 			SDL_SYS_JoystickQuit();
   686 			// 100 is the ordinal for _XInputGetStateEx, which returns the same struct as XinputGetState, but with extra data in wButtons for the guide button, we think...
   682 			SDL_SetError("GetProcAddress() failed when loading XInput.", GetLastError());
   687 			PC_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, (LPCSTR)100 );
   683 			return (-1);
   688 			PC_XInputSetState = (XInputSetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputSetState" );
   684 		}
   689 			PC_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetCapabilities" );
   685 	}
   690 			if ( !PC_XInputGetState || !PC_XInputSetState || !PC_XInputGetCapabilities )
       
   691 			{
       
   692 				SDL_SYS_JoystickQuit();
       
   693 				SDL_SetError("GetProcAddress() failed when loading XInput.", GetLastError());
       
   694 				return (-1);
       
   695 			}
       
   696 		}
       
   697     }
   686 
   698 
   687 
   699 
   688 	if ( !s_threadJoystick )
   700 	if ( !s_threadJoystick )
   689 	{
   701 	{
   690 		s_bJoystickThreadQuit = SDL_FALSE;
   702 		s_bJoystickThreadQuit = SDL_FALSE;
   932     dipdw.diph.dwSize = sizeof(DIPROPDWORD);
   944     dipdw.diph.dwSize = sizeof(DIPROPDWORD);
   933     dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
   945     dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
   934 
   946 
   935     /* allocate memory for system specific hardware data */
   947     /* allocate memory for system specific hardware data */
   936 	joystick->instance_id = joystickdevice->nInstanceID;
   948 	joystick->instance_id = joystickdevice->nInstanceID;
       
   949     joystick->closed = 0;
   937     joystick->hwdata =
   950     joystick->hwdata =
   938         (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
   951         (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
   939     if (joystick->hwdata == NULL) {
   952     if (joystick->hwdata == NULL) {
   940         SDL_OutOfMemory();
   953         SDL_OutOfMemory();
   941         return (-1);
   954         return (-1);
   957 		{
   970 		{
   958 			if ( joysticklist->bXInputDevice && joysticklist->XInputUserId == userId )
   971 			if ( joysticklist->bXInputDevice && joysticklist->XInputUserId == userId )
   959 				userId++;
   972 				userId++;
   960 		}
   973 		}
   961 
   974 
   962 		if ( XINPUTGETCAPABILITIES )
   975 		if ( s_bXInputEnabled && XINPUTGETCAPABILITIES )
   963 		{
   976 		{
   964 			result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities );
   977 			result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities );
   965 			if ( result == ERROR_SUCCESS )
   978 			if ( result == ERROR_SUCCESS )
   966 			{
   979 			{
   967 				SDL_bool bIsSupported = SDL_FALSE;
   980 				SDL_bool bIsSupported = SDL_FALSE;