For DOS Protected Mode version of sensor core software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

488 lines
18 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "inputController.h"
  2. inputController::inputController(){
  3. HRESULT hr;
  4. m_bInitDX = false;
  5. m_bFilterOutXinputDevices = false;
  6. m_bQeryDeviceDone = false;
  7. m_bDeviceIsConnected = false;
  8. m_pXInputDeviceList = NULL;
  9. m_pDI = NULL;
  10. m_pJoystick = NULL;
  11. // Register with the DirectInput subsystem and get a pointer
  12. // to a IDirectInput interface we can use.
  13. // Create a DInput object
  14. if( FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, ( void** )&m_pDI, NULL ) ) ){
  15. m_bInitDX = false;
  16. return;
  17. } else {
  18. m_bInitDX = true;
  19. };
  20. if( m_bFilterOutXinputDevices ){
  21. SetupForIsXInputDevice();
  22. };
  23. DIJOYCONFIG PreferredJoyCfg = {0};
  24. DI_ENUM_CONTEXT enumContext;
  25. enumContext.pPreferredJoyCfg = &PreferredJoyCfg;
  26. enumContext.bPreferredJoyCfgValid = false;
  27. IDirectInputJoyConfig8* pJoyConfig = NULL;
  28. if( FAILED( hr = m_pDI->QueryInterface( IID_IDirectInputJoyConfig8, ( void** )&pJoyConfig ) ) ){
  29. m_bQeryDeviceDone = false;
  30. return;
  31. } else {
  32. m_bQeryDeviceDone = true;
  33. };
  34. PreferredJoyCfg.dwSize = sizeof( PreferredJoyCfg );
  35. if( SUCCEEDED( pJoyConfig->GetConfig( 0, &PreferredJoyCfg, DIJC_GUIDINSTANCE ) ) ){
  36. // This function is expected to fail if no joystick is attached
  37. enumContext.bPreferredJoyCfgValid = true;
  38. };
  39. SAFE_RELEASE( pJoyConfig );
  40. // Look for a simple joystick we can use for this sample program.
  41. if( FAILED( hr = m_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, &enumContext, DIEDFL_ATTACHEDONLY ) ) ){
  42. return;
  43. };
  44. if( m_bFilterOutXinputDevices ) CleanupForIsXInputDevice();
  45. // Make sure we got a joystick
  46. if( !m_pJoystick ){
  47. MessageBox( nullptr, TEXT( "Joystick not found. The sample will now exit." ), TEXT( "DirectInput Sample" ), MB_ICONERROR | MB_OK );
  48. return;
  49. };
  50. // Set the data format to "simple joystick" - a predefined data format
  51. //
  52. // A data format specifies which controls on a device we are interested in,
  53. // and how they should be reported. This tells DInput that we will be
  54. // passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState().
  55. if( FAILED( hr = m_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ){
  56. return;
  57. };
  58. // Set the cooperative level to let DInput know how this device should
  59. // interact with the system and with other DInput applications.
  60. if( FAILED( hr = m_pJoystick->SetCooperativeLevel( hDlg, DISCL_EXCLUSIVE | DISCL_FOREGROUND ) ) ){
  61. return;
  62. };
  63. // Enumerate the joystick objects. The callback function enabled user
  64. // interface elements for objects that are found, and sets the min/max
  65. // values property for discovered axes.
  66. //if( FAILED( hr = m_pJoystick->EnumObjects( EnumObjectsCallback, ( void* )hDlg, DIDFT_ALL ) ) ){
  67. //
  68. // return;
  69. //};
  70. m_bDeviceIsConnected = true;
  71. };
  72. //-----------------------------------------------------------------------------
  73. // Enum each PNP device using WMI and check each device ID to see if it contains
  74. // "IG_" (ex. "VID_045E&PID_028E&IG_00"). If it does, then it�s an XInput device
  75. // Unfortunately this information can not be found by just using DirectInput.
  76. // Checking against a VID/PID of 0x028E/0x045E won't find 3rd party or future
  77. // XInput devices.
  78. //
  79. // This function stores the list of xinput devices in a linked list
  80. // at g_pXInputDeviceList, and IsXInputDevice() searchs that linked list
  81. //-----------------------------------------------------------------------------
  82. HRESULT inputController::SetupForIsXInputDevice( void ){
  83. IWbemServices *pIWbemServices = void;
  84. IEnumWbemClassObject *pEnumDevices = void;
  85. IWbemLocator *pIWbemLocator = void;
  86. IWbemClassObject *pDevices[20] = {0};
  87. BSTR bstrDeviceID = NULL;
  88. BSTR bstrClassName = NULL;
  89. BSTR bstrNamespace = NULL;
  90. DWORD uReturned = 0;
  91. bool bCleanupCOM = false;
  92. UINT iDevice = 0;
  93. VARIANT var;
  94. HRESULT hr;
  95. // CoInit if needed
  96. hr = CoInitialize( NULL );
  97. bCleanupCOM = SUCCEEDED( hr );
  98. // Create WMI
  99. hr = CoCreateInstance( __uuidof( WbemLocator ),
  100. NULL,
  101. CLSCTX_INPROC_SERVER,
  102. __uuidof( IWbemLocator ),
  103. ( LPVOID* )&pIWbemLocator );
  104. if( FAILED( hr ) || pIWbemLocator == NULL )
  105. goto LCleanup;
  106. // Create BSTRs for WMI
  107. bstrNamespace = SysAllocString( L"\\\\.\\root\\cimv2" ); if( bstrNamespace == NULL ) goto LCleanup;
  108. bstrDeviceID = SysAllocString( L"DeviceID" ); if( bstrDeviceID == NULL ) goto LCleanup;
  109. bstrClassName = SysAllocString( L"Win32_PNPEntity" ); if( bstrClassName == NULL ) goto LCleanup;
  110. // Connect to WMI
  111. hr = pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, 0L,
  112. 0L, NULL, NULL, &pIWbemServices );
  113. if( FAILED( hr ) || pIWbemServices == NULL )
  114. goto LCleanup;
  115. // Switch security level to IMPERSONATE
  116. (void)CoSetProxyBlanket( pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
  117. RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0 );
  118. // Get list of Win32_PNPEntity devices
  119. hr = pIWbemServices->CreateInstanceEnum( bstrClassName, 0, NULL, &pEnumDevices );
  120. if( FAILED( hr ) || pEnumDevices == NULL ) goto LCleanup;
  121. // Loop over all devices
  122. for(;;) {
  123. // Get 20 at a time
  124. hr = pEnumDevices->Next( 10000, 20, pDevices, &uReturned );
  125. if( FAILED( hr ) ) goto LCleanup;
  126. if( uReturned == 0 ) break;
  127. for( iDevice = 0; iDevice < uReturned; iDevice++ ){
  128. if ( !pDevices[iDevice] ) continue;
  129. // For each device, get its device ID
  130. hr = pDevices[iDevice]->Get( bstrDeviceID, 0L, &var, NULL, NULL );
  131. if( SUCCEEDED( hr ) && var.vt == VT_BSTR && var.bstrVal != NULL ) {
  132. // Check if the device ID contains "IG_". If it does, then it�s an XInput device
  133. // Unfortunately this information can not be found by just using DirectInput
  134. if( wcsstr( var.bstrVal, L"IG_" ) ) {
  135. // If it does, then get the VID/PID from var.bstrVal
  136. DWORD dwPid = 0, dwVid = 0;
  137. WCHAR* strVid = wcsstr( var.bstrVal, L"VID_" );
  138. if( strVid && swscanf( strVid, L"VID_%4X", &dwVid ) != 1 )
  139. dwVid = 0;
  140. WCHAR* strPid = wcsstr( var.bstrVal, L"PID_" );
  141. if( strPid && swscanf( strPid, L"PID_%4X", &dwPid ) != 1 )
  142. dwPid = 0;
  143. DWORD dwVidPid = MAKELONG( dwVid, dwPid );
  144. // Add the VID/PID to a linked list
  145. XINPUT_DEVICE_NODE* pNewNode = new XINPUT_DEVICE_NODE;
  146. if( pNewNode ){
  147. pNewNode->dwVidPid = dwVidPid;
  148. pNewNode->pNext = g_pXInputDeviceList;
  149. g_pXInputDeviceList = pNewNode;
  150. };
  151. };
  152. };
  153. SAFE_RELEASE( pDevices[iDevice] );
  154. };
  155. };
  156. LCleanup:
  157. if( bstrNamespace ) SysFreeString( bstrNamespace );
  158. if( bstrDeviceID ) SysFreeString( bstrDeviceID );
  159. if( bstrClassName ) SysFreeString( bstrClassName );
  160. for( iDevice = 0; iDevice < 20; iDevice++ ) SAFE_RELEASE( pDevices[iDevice] );
  161. SAFE_RELEASE( pEnumDevices );
  162. SAFE_RELEASE( pIWbemLocator );
  163. SAFE_RELEASE( pIWbemServices );
  164. return hr;
  165. };
  166. //-----------------------------------------------------------------------------
  167. // Returns true if the DirectInput device is also an XInput device.
  168. // Call SetupForIsXInputDevice() before, and CleanupForIsXInputDevice() after
  169. //-----------------------------------------------------------------------------
  170. bool inputController::IsXInputDevice( const GUID* pGuidProductFromDirectInput ){
  171. // Check each xinput device to see if this device's vid/pid matches
  172. XINPUT_DEVICE_NODE* pNode = g_pXInputDeviceList;
  173. while( pNode )
  174. {
  175. if( pNode->dwVidPid == pGuidProductFromDirectInput->Data1 )
  176. return true;
  177. pNode = pNode->pNext;
  178. }
  179. return false;
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Cleanup needed for IsXInputDevice()
  183. //-----------------------------------------------------------------------------
  184. void inputController::CleanupForIsXInputDevice( void ){
  185. // Cleanup linked list
  186. XINPUT_DEVICE_NODE* pNode = g_pXInputDeviceList;
  187. while( pNode )
  188. {
  189. XINPUT_DEVICE_NODE* pDelete = pNode;
  190. pNode = pNode->pNext;
  191. SAFE_DELETE( pDelete );
  192. }
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Name: EnumJoysticksCallback()
  196. // Desc: Called once for each enumerated joystick. If we find one, create a
  197. // device interface on it so we can play with it.
  198. //-----------------------------------------------------------------------------
  199. BOOL CALLBACK inputController::EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, void* pContext ){
  200. auto pEnumContext = reinterpret_cast<DI_ENUM_CONTEXT*>( pContext );
  201. HRESULT hr;
  202. if( g_bFilterOutXinputDevices && IsXInputDevice( &pdidInstance->guidProduct ) )
  203. return DIENUM_CONTINUE;
  204. // Skip anything other than the perferred joystick device as defined by the control panel.
  205. // Instead you could store all the enumerated joysticks and let the user pick.
  206. if( pEnumContext->bPreferredJoyCfgValid &&
  207. !IsEqualGUID( pdidInstance->guidInstance, pEnumContext->pPreferredJoyCfg->guidInstance ) )
  208. return DIENUM_CONTINUE;
  209. // Obtain an interface to the enumerated joystick.
  210. hr = g_pDI->CreateDevice( pdidInstance->guidInstance, &g_pJoystick, nullptr );
  211. // If it failed, then we can't use this joystick. (Maybe the user unplugged
  212. // it while we were in the middle of enumerating it.)
  213. if( FAILED( hr ) )
  214. return DIENUM_CONTINUE;
  215. // Stop enumeration. Note: we're just taking the first joystick we get. You
  216. // could store all the enumerated joysticks and let the user pick.
  217. return DIENUM_STOP;
  218. }
  219. //-----------------------------------------------------------------------------
  220. // Name: EnumObjectsCallback()
  221. // Desc: Callback function for enumerating objects (axes, buttons, POVs) on a
  222. // joystick. This function enables user interface elements for objects
  223. // that are found to exist, and scales axes min/max values.
  224. //-----------------------------------------------------------------------------
  225. BOOL CALLBACK inputController::EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext ){
  226. HWND hDlg = ( HWND )pContext;
  227. static int nSliderCount = 0; // Number of returned slider controls
  228. static int nPOVCount = 0; // Number of returned POV controls
  229. // For axes that are returned, set the DIPROP_RANGE property for the
  230. // enumerated axis in order to scale min/max values.
  231. if( pdidoi->dwType & DIDFT_AXIS )
  232. {
  233. DIPROPRANGE diprg;
  234. diprg.diph.dwSize = sizeof( DIPROPRANGE );
  235. diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER );
  236. diprg.diph.dwHow = DIPH_BYID;
  237. diprg.diph.dwObj = pdidoi->dwType; // Specify the enumerated axis
  238. diprg.lMin = -1000;
  239. diprg.lMax = +1000;
  240. // Set the range for the axis
  241. if( FAILED( g_pJoystick->SetProperty( DIPROP_RANGE, &diprg.diph ) ) )
  242. return DIENUM_STOP;
  243. }
  244. // Set the UI to reflect what objects the joystick supports
  245. if( pdidoi->guidType == GUID_XAxis )
  246. {
  247. EnableWindow( GetDlgItem( hDlg, IDC_X_AXIS ), TRUE );
  248. EnableWindow( GetDlgItem( hDlg, IDC_X_AXIS_TEXT ), TRUE );
  249. }
  250. if( pdidoi->guidType == GUID_YAxis )
  251. {
  252. EnableWindow( GetDlgItem( hDlg, IDC_Y_AXIS ), TRUE );
  253. EnableWindow( GetDlgItem( hDlg, IDC_Y_AXIS_TEXT ), TRUE );
  254. }
  255. if( pdidoi->guidType == GUID_ZAxis )
  256. {
  257. EnableWindow( GetDlgItem( hDlg, IDC_Z_AXIS ), TRUE );
  258. EnableWindow( GetDlgItem( hDlg, IDC_Z_AXIS_TEXT ), TRUE );
  259. }
  260. if( pdidoi->guidType == GUID_RxAxis )
  261. {
  262. EnableWindow( GetDlgItem( hDlg, IDC_X_ROT ), TRUE );
  263. EnableWindow( GetDlgItem( hDlg, IDC_X_ROT_TEXT ), TRUE );
  264. }
  265. if( pdidoi->guidType == GUID_RyAxis )
  266. {
  267. EnableWindow( GetDlgItem( hDlg, IDC_Y_ROT ), TRUE );
  268. EnableWindow( GetDlgItem( hDlg, IDC_Y_ROT_TEXT ), TRUE );
  269. }
  270. if( pdidoi->guidType == GUID_RzAxis )
  271. {
  272. EnableWindow( GetDlgItem( hDlg, IDC_Z_ROT ), TRUE );
  273. EnableWindow( GetDlgItem( hDlg, IDC_Z_ROT_TEXT ), TRUE );
  274. }
  275. if( pdidoi->guidType == GUID_Slider )
  276. {
  277. switch( nSliderCount++ )
  278. {
  279. case 0 :
  280. EnableWindow( GetDlgItem( hDlg, IDC_SLIDER0 ), TRUE );
  281. EnableWindow( GetDlgItem( hDlg, IDC_SLIDER0_TEXT ), TRUE );
  282. break;
  283. case 1 :
  284. EnableWindow( GetDlgItem( hDlg, IDC_SLIDER1 ), TRUE );
  285. EnableWindow( GetDlgItem( hDlg, IDC_SLIDER1_TEXT ), TRUE );
  286. break;
  287. }
  288. }
  289. if( pdidoi->guidType == GUID_POV )
  290. {
  291. switch( nPOVCount++ )
  292. {
  293. case 0 :
  294. EnableWindow( GetDlgItem( hDlg, IDC_POV0 ), TRUE );
  295. EnableWindow( GetDlgItem( hDlg, IDC_POV0_TEXT ), TRUE );
  296. break;
  297. case 1 :
  298. EnableWindow( GetDlgItem( hDlg, IDC_POV1 ), TRUE );
  299. EnableWindow( GetDlgItem( hDlg, IDC_POV1_TEXT ), TRUE );
  300. break;
  301. case 2 :
  302. EnableWindow( GetDlgItem( hDlg, IDC_POV2 ), TRUE );
  303. EnableWindow( GetDlgItem( hDlg, IDC_POV2_TEXT ), TRUE );
  304. break;
  305. case 3 :
  306. EnableWindow( GetDlgItem( hDlg, IDC_POV3 ), TRUE );
  307. EnableWindow( GetDlgItem( hDlg, IDC_POV3_TEXT ), TRUE );
  308. break;
  309. }
  310. }
  311. return DIENUM_CONTINUE;
  312. }
  313. //-----------------------------------------------------------------------------
  314. // Name: UpdateInputState()
  315. // Desc: Get the input device's state and display it.
  316. //-----------------------------------------------------------------------------
  317. HRESULT inputController::UpdateInputState( HWND hDlg ){
  318. HRESULT hr;
  319. TCHAR strText[512] = {0}; // Device state text
  320. DIJOYSTATE2 js; // DInput joystick state
  321. if( !g_pJoystick )
  322. return S_OK;
  323. // Poll the device to read the current state
  324. hr = g_pJoystick->Poll();
  325. if( FAILED( hr ) )
  326. {
  327. // DInput is telling us that the input stream has been
  328. // interrupted. We aren't tracking any state between polls, so
  329. // we don't have any special reset that needs to be done. We
  330. // just re-acquire and try again.
  331. hr = g_pJoystick->Acquire();
  332. while( hr == DIERR_INPUTLOST )
  333. hr = g_pJoystick->Acquire();
  334. // hr may be DIERR_OTHERAPPHASPRIO or other errors. This
  335. // may occur when the app is minimized or in the process of
  336. // switching, so just try again later
  337. return S_OK;
  338. }
  339. // Get the input's device state
  340. if( FAILED( hr = g_pJoystick->GetDeviceState( sizeof( DIJOYSTATE2 ), &js ) ) )
  341. return hr; // The device should have been acquired during the Poll()
  342. // Display joystick state to dialog
  343. // Axes
  344. _stprintf_s( strText, 512, TEXT( "%ld" ), js.lX );
  345. SetWindowText( GetDlgItem( hDlg, IDC_X_AXIS ), strText );
  346. _stprintf_s( strText, 512, TEXT( "%ld" ), js.lY );
  347. SetWindowText( GetDlgItem( hDlg, IDC_Y_AXIS ), strText );
  348. _stprintf_s( strText, 512, TEXT( "%ld" ), js.lZ );
  349. SetWindowText( GetDlgItem( hDlg, IDC_Z_AXIS ), strText );
  350. _stprintf_s( strText, 512, TEXT( "%ld" ), js.lRx );
  351. SetWindowText( GetDlgItem( hDlg, IDC_X_ROT ), strText );
  352. _stprintf_s( strText, 512, TEXT( "%ld" ), js.lRy );
  353. SetWindowText( GetDlgItem( hDlg, IDC_Y_ROT ), strText );
  354. _stprintf_s( strText, 512, TEXT( "%ld" ), js.lRz );
  355. SetWindowText( GetDlgItem( hDlg, IDC_Z_ROT ), strText );
  356. // Slider controls
  357. _stprintf_s( strText, 512, TEXT( "%ld" ), js.rglSlider[0] );
  358. SetWindowText( GetDlgItem( hDlg, IDC_SLIDER0 ), strText );
  359. _stprintf_s( strText, 512, TEXT( "%ld" ), js.rglSlider[1] );
  360. SetWindowText( GetDlgItem( hDlg, IDC_SLIDER1 ), strText );
  361. // Points of view
  362. _stprintf_s( strText, 512, TEXT( "%lu" ), js.rgdwPOV[0] );
  363. SetWindowText( GetDlgItem( hDlg, IDC_POV0 ), strText );
  364. _stprintf_s( strText, 512, TEXT( "%lu" ), js.rgdwPOV[1] );
  365. SetWindowText( GetDlgItem( hDlg, IDC_POV1 ), strText );
  366. _stprintf_s( strText, 512, TEXT( "%lu" ), js.rgdwPOV[2] );
  367. SetWindowText( GetDlgItem( hDlg, IDC_POV2 ), strText );
  368. _stprintf_s( strText, 512, TEXT( "%lu" ), js.rgdwPOV[3] );
  369. SetWindowText( GetDlgItem( hDlg, IDC_POV3 ), strText );
  370. // Fill up text with which buttons are pressed
  371. _tcscpy_s( strText, 512, TEXT( "" ) );
  372. for( int i = 0; i < 128; i++ )
  373. {
  374. if( js.rgbButtons[i] & 0x80 )
  375. {
  376. TCHAR sz[128];
  377. _stprintf_s( sz, 128, TEXT( "%02d " ), i );
  378. _tcscat_s( strText, 512, sz );
  379. }
  380. }
  381. SetWindowText( GetDlgItem( hDlg, IDC_BUTTONS ), strText );
  382. return S_OK;
  383. }
  384. //-----------------------------------------------------------------------------
  385. // Name: ~inputController()
  386. // Desc: Initialize the DirectInput variables.
  387. //-----------------------------------------------------------------------------
  388. inputController::~inputController(){
  389. // Unacquire the device one last time just in case
  390. // the app tried to exit while the device is still acquired.
  391. if( g_pJoystick )
  392. g_pJoystick->Unacquire();
  393. // Release any DirectInput objects.
  394. SAFE_RELEASE( g_pJoystick );
  395. SAFE_RELEASE( g_pDI );
  396. };