/src/ois/src/win32/Win32JoyStick.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 688 lines · 489 code · 103 blank · 96 comment · 157 complexity · de3b5defe323dc848581d00fc5d4b971 MD5 · raw file

  1. /*
  2. The zlib/libpng License
  3. Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
  4. This software is provided 'as-is', without any express or implied warranty. In no event will
  5. the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose, including commercial
  7. applications, and to alter it and redistribute it freely, subject to the following
  8. restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that
  10. you wrote the original software. If you use this software in a product,
  11. an acknowledgment in the product documentation would be appreciated but is
  12. not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "win32/Win32JoyStick.h"
  18. #include "win32/Win32InputManager.h"
  19. #include "win32/Win32ForceFeedback.h"
  20. #include "OISEvents.h"
  21. #include "OISException.h"
  22. #include <cassert>
  23. // Only if xinput support is enabled
  24. #ifdef OIS_WIN32_XINPUT_SUPPORT
  25. #include <wbemidl.h>
  26. #include <oleauto.h>
  27. //#include <wmsstd.h>
  28. #ifndef SAFE_RELEASE
  29. #define SAFE_RELEASE(x) \
  30. if(x != NULL) \
  31. { \
  32. x->Release(); \
  33. x = NULL; \
  34. }
  35. #endif
  36. #pragma comment(lib, "xinput.lib")
  37. #endif
  38. //DX Only defines macros for the JOYSTICK not JOYSTICK2, so fix it
  39. #undef DIJOFS_BUTTON
  40. #undef DIJOFS_POV
  41. #define DIJOFS_BUTTON(n) (FIELD_OFFSET(DIJOYSTATE2, rgbButtons) + (n))
  42. #define DIJOFS_POV(n) (FIELD_OFFSET(DIJOYSTATE2, rgdwPOV)+(n)*sizeof(DWORD))
  43. #define DIJOFS_SLIDER0(n) (FIELD_OFFSET(DIJOYSTATE2, rglSlider)+(n) * sizeof(LONG))
  44. #define DIJOFS_SLIDER1(n) (FIELD_OFFSET(DIJOYSTATE2, rglVSlider)+(n) * sizeof(LONG))
  45. #define DIJOFS_SLIDER2(n) (FIELD_OFFSET(DIJOYSTATE2, rglASlider)+(n) * sizeof(LONG))
  46. #define DIJOFS_SLIDER3(n) (FIELD_OFFSET(DIJOYSTATE2, rglFSlider)+(n) * sizeof(LONG))
  47. #define XINPUT_TRANSLATED_BUTTON_COUNT 12
  48. #define XINPUT_TRANSLATED_AXIS_COUNT 6
  49. using namespace OIS;
  50. //--------------------------------------------------------------------------------------------------//
  51. Win32JoyStick::Win32JoyStick( InputManager* creator, IDirectInput8* pDI, bool buffered, DWORD coopSettings, const JoyStickInfo &info ) :
  52. JoyStick(info.vendor, buffered, info.devId, creator),
  53. mDirectInput(pDI),
  54. coopSetting(coopSettings),
  55. mJoyStick(0),
  56. mJoyInfo(info),
  57. mFfDevice(0)
  58. {
  59. }
  60. //--------------------------------------------------------------------------------------------------//
  61. Win32JoyStick::~Win32JoyStick()
  62. {
  63. delete mFfDevice;
  64. if(mJoyStick)
  65. {
  66. mJoyStick->Unacquire();
  67. mJoyStick->Release();
  68. mJoyStick = 0;
  69. }
  70. //Return joystick to pool
  71. static_cast<Win32InputManager*>(mCreator)->_returnJoyStick(mJoyInfo);
  72. }
  73. //--------------------------------------------------------------------------------------------------//
  74. void Win32JoyStick::_initialize()
  75. {
  76. if (mJoyInfo.isXInput)
  77. {
  78. _enumerate();
  79. }
  80. else
  81. {
  82. //Clear old state
  83. mState.mAxes.clear();
  84. delete mFfDevice;
  85. mFfDevice = 0;
  86. DIPROPDWORD dipdw;
  87. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  88. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  89. dipdw.diph.dwObj = 0;
  90. dipdw.diph.dwHow = DIPH_DEVICE;
  91. dipdw.dwData = JOYSTICK_DX_BUFFERSIZE;
  92. if(FAILED(mDirectInput->CreateDevice(mJoyInfo.deviceID, &mJoyStick, NULL)))
  93. OIS_EXCEPT( E_General, "Win32JoyStick::_initialize() >> Could not initialize joy device!");
  94. if(FAILED(mJoyStick->SetDataFormat(&c_dfDIJoystick2)))
  95. OIS_EXCEPT( E_General, "Win32JoyStick::_initialize() >> data format error!");
  96. HWND hwin = ((Win32InputManager*)mCreator)->getWindowHandle();
  97. if(FAILED(mJoyStick->SetCooperativeLevel( hwin, coopSetting)))
  98. OIS_EXCEPT( E_General, "Win32JoyStick::_initialize() >> failed to set cooperation level!");
  99. if( FAILED(mJoyStick->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)) )
  100. OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set buffer size property" );
  101. //Enumerate all axes/buttons/sliders/etc before aquiring
  102. _enumerate();
  103. mState.clear();
  104. capture();
  105. }
  106. }
  107. //--------------------------------------------------------------------------------------------------//
  108. void Win32JoyStick::_enumerate()
  109. {
  110. if (mJoyInfo.isXInput)
  111. {
  112. mPOVs = 1;
  113. mState.mButtons.resize(XINPUT_TRANSLATED_BUTTON_COUNT);
  114. mState.mAxes.resize(XINPUT_TRANSLATED_AXIS_COUNT);
  115. }
  116. else
  117. {
  118. // Get joystick capabilities.
  119. mDIJoyCaps.dwSize = sizeof(DIDEVCAPS);
  120. if( FAILED(mJoyStick->GetCapabilities(&mDIJoyCaps)) )
  121. OIS_EXCEPT( E_General, "Win32JoyStick::_enumerate >> Failed to get capabilities" );
  122. mPOVs = (short)mDIJoyCaps.dwPOVs;
  123. mState.mButtons.resize(mDIJoyCaps.dwButtons);
  124. mState.mAxes.resize(mDIJoyCaps.dwAxes);
  125. //Reset the axis mapping enumeration value
  126. _AxisNumber = 0;
  127. //Enumerate Force Feedback (if any)
  128. mJoyStick->EnumEffects(DIEnumEffectsCallback, this, DIEFT_ALL);
  129. //Enumerate and set axis constraints (and check FF Axes)
  130. mJoyStick->EnumObjects(DIEnumDeviceObjectsCallback, this, DIDFT_AXIS);
  131. }
  132. }
  133. //--------------------------------------------------------------------------------------------------//
  134. BOOL CALLBACK Win32JoyStick::DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
  135. {
  136. Win32JoyStick* _this = (Win32JoyStick*)pvRef;
  137. //Setup mappings
  138. DIPROPPOINTER diptr;
  139. diptr.diph.dwSize = sizeof(DIPROPPOINTER);
  140. diptr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  141. diptr.diph.dwHow = DIPH_BYID;
  142. diptr.diph.dwObj = lpddoi->dwType;
  143. //Add a magic number to recognise we set seomthing
  144. diptr.uData = 0x13130000 | _this->_AxisNumber;
  145. //Check if axis is slider, if so, do not treat as regular axis
  146. if(GUID_Slider == lpddoi->guidType)
  147. {
  148. ++_this->mSliders;
  149. //Decrease Axes, since this slider shows up in a different place
  150. _this->mState.mAxes.pop_back();
  151. }
  152. else if (FAILED(_this->mJoyStick->SetProperty(DIPROP_APPDATA, &diptr.diph)))
  153. { //If for some reason we could not set needed user data, just ignore this axis
  154. return DIENUM_CONTINUE;
  155. }
  156. //Increase for next time through
  157. if(GUID_Slider != lpddoi->guidType)
  158. _this->_AxisNumber += 1;
  159. //Set range
  160. DIPROPRANGE diprg;
  161. diprg.diph.dwSize = sizeof(DIPROPRANGE);
  162. diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  163. diprg.diph.dwHow = DIPH_BYID;
  164. diprg.diph.dwObj = lpddoi->dwType;
  165. diprg.lMin = MIN_AXIS;
  166. diprg.lMax = MAX_AXIS;
  167. if (FAILED(_this->mJoyStick->SetProperty(DIPROP_RANGE, &diprg.diph)))
  168. OIS_EXCEPT( E_General, "Win32JoyStick::_DIEnumDeviceObjectsCallback >> Failed to set min/max range property" );
  169. //Check if FF Axes, and if so, increment counter
  170. if((lpddoi->dwFlags & DIDOI_FFACTUATOR) != 0 )
  171. {
  172. if( _this->mFfDevice )
  173. {
  174. _this->mFfDevice->_addFFAxis();
  175. }
  176. }
  177. //Force the flags for gain and auto-center support to true,
  178. //as DInput has no API to query the device for these capabilities
  179. //(the only way to know is to try them ...)
  180. if( _this->mFfDevice )
  181. {
  182. _this->mFfDevice->_setGainSupport(true);
  183. _this->mFfDevice->_setAutoCenterSupport(true);
  184. }
  185. return DIENUM_CONTINUE;
  186. }
  187. //--------------------------------------------------------------------------------------------------//
  188. BOOL CALLBACK Win32JoyStick::DIEnumEffectsCallback(LPCDIEFFECTINFO pdei, LPVOID pvRef)
  189. {
  190. Win32JoyStick* _this = (Win32JoyStick*)pvRef;
  191. //Create the FF instance only after we know there is at least one effect type
  192. if( _this->mFfDevice == 0 )
  193. _this->mFfDevice = new Win32ForceFeedback(_this->mJoyStick, &_this->mDIJoyCaps);
  194. _this->mFfDevice->_addEffectSupport(pdei);
  195. return DIENUM_CONTINUE;
  196. }
  197. //--------------------------------------------------------------------------------------------------//
  198. void Win32JoyStick::capture()
  199. {
  200. #ifdef OIS_WIN32_XINPUT_SUPPORT
  201. //handle xbox controller differently
  202. if (mJoyInfo.isXInput)
  203. {
  204. captureXInput();
  205. return;
  206. }
  207. #endif
  208. //handle directinput based devices
  209. DIDEVICEOBJECTDATA diBuff[JOYSTICK_DX_BUFFERSIZE];
  210. DWORD entries = JOYSTICK_DX_BUFFERSIZE;
  211. // Poll the device to read the current state
  212. HRESULT hr = mJoyStick->Poll();
  213. if( hr == DI_OK )
  214. hr = mJoyStick->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
  215. if( hr != DI_OK )
  216. {
  217. hr = mJoyStick->Acquire();
  218. while( hr == DIERR_INPUTLOST )
  219. hr = mJoyStick->Acquire();
  220. // Poll the device to read the current state
  221. mJoyStick->Poll();
  222. hr = mJoyStick->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
  223. //Perhaps the user just tabbed away
  224. if( FAILED(hr) )
  225. return;
  226. }
  227. bool axisMoved[24] = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
  228. false,false,false,false,false,false,false,false};
  229. bool sliderMoved[4] = {false,false,false,false};
  230. //Loop through all the events
  231. for(unsigned int i = 0; i < entries; ++i)
  232. {
  233. //This may seem outof order, but is in order of the way these variables
  234. //are declared in the JoyStick State 2 structure.
  235. switch(diBuff[i].dwOfs)
  236. {
  237. //------ slider -//
  238. case DIJOFS_SLIDER0(0):
  239. sliderMoved[0] = true;
  240. mState.mSliders[0].abX = diBuff[i].dwData;
  241. break;
  242. case DIJOFS_SLIDER0(1):
  243. sliderMoved[0] = true;
  244. mState.mSliders[0].abY = diBuff[i].dwData;
  245. break;
  246. //----- Max 4 POVs Next ---------------//
  247. case DIJOFS_POV(0):
  248. if(!_changePOV(0,diBuff[i]))
  249. return;
  250. break;
  251. case DIJOFS_POV(1):
  252. if(!_changePOV(1,diBuff[i]))
  253. return;
  254. break;
  255. case DIJOFS_POV(2):
  256. if(!_changePOV(2,diBuff[i]))
  257. return;
  258. break;
  259. case DIJOFS_POV(3):
  260. if(!_changePOV(3,diBuff[i]))
  261. return;
  262. break;
  263. case DIJOFS_SLIDER1(0):
  264. sliderMoved[1] = true;
  265. mState.mSliders[1].abX = diBuff[i].dwData;
  266. break;
  267. case DIJOFS_SLIDER1(1):
  268. sliderMoved[1] = true;
  269. mState.mSliders[1].abY = diBuff[i].dwData;
  270. break;
  271. case DIJOFS_SLIDER2(0):
  272. sliderMoved[2] = true;
  273. mState.mSliders[2].abX = diBuff[i].dwData;
  274. break;
  275. case DIJOFS_SLIDER2(1):
  276. sliderMoved[2] = true;
  277. mState.mSliders[2].abY = diBuff[i].dwData;
  278. break;
  279. case DIJOFS_SLIDER3(0):
  280. sliderMoved[3] = true;
  281. mState.mSliders[3].abX = diBuff[i].dwData;
  282. break;
  283. case DIJOFS_SLIDER3(1):
  284. sliderMoved[3] = true;
  285. mState.mSliders[3].abY = diBuff[i].dwData;
  286. break;
  287. //-----------------------------------------//
  288. default:
  289. //Handle Button Events Easily using the DX Offset Macros
  290. if( diBuff[i].dwOfs >= DIJOFS_BUTTON(0) && diBuff[i].dwOfs < DIJOFS_BUTTON(128) )
  291. {
  292. if(!_doButtonClick((diBuff[i].dwOfs - DIJOFS_BUTTON(0)), diBuff[i]))
  293. return;
  294. }
  295. else if((short)(diBuff[i].uAppData >> 16) == 0x1313)
  296. { //If it was nothing else, might be axis enumerated earlier (determined by magic number)
  297. int axis = (int)(0x0000FFFF & diBuff[i].uAppData); //Mask out the high bit
  298. assert( axis >= 0 && axis < (int)mState.mAxes.size() && "Axis out of range!");
  299. if(axis >= 0 && axis < (int)mState.mAxes.size())
  300. {
  301. mState.mAxes[axis].abs = diBuff[i].dwData;
  302. axisMoved[axis] = true;
  303. }
  304. }
  305. break;
  306. } //end case
  307. } //end for
  308. //Check to see if any of the axes values have changed.. if so send events
  309. if( mBuffered && mListener && entries > 0 )
  310. {
  311. JoyStickEvent temp(this, mState);
  312. //Update axes
  313. for( int i = 0; i < 24; ++i )
  314. if( axisMoved[i] )
  315. if( mListener->axisMoved( temp, i ) == false )
  316. return;
  317. //Now update sliders
  318. for( int i = 0; i < 4; ++i )
  319. if( sliderMoved[i] )
  320. if( mListener->sliderMoved( temp, i ) == false )
  321. return;
  322. }
  323. }
  324. //--------------------------------------------------------------------------------------------------//
  325. void Win32JoyStick::captureXInput()
  326. {
  327. #ifdef OIS_WIN32_XINPUT_SUPPORT
  328. XINPUT_STATE inputState;
  329. if (XInputGetState((DWORD)mJoyInfo.xInputDev, &inputState) != ERROR_SUCCESS)
  330. memset(&inputState, 0, sizeof(inputState));
  331. //Sticks and triggers
  332. int value;
  333. bool axisMoved[XINPUT_TRANSLATED_AXIS_COUNT] = {false,false,false,false,false,false};
  334. //LeftY
  335. value = -(int)inputState.Gamepad.sThumbLY;
  336. mState.mAxes[0].rel = value - mState.mAxes[0].abs;
  337. mState.mAxes[0].abs = value;
  338. if(mState.mAxes[0].rel != 0)
  339. axisMoved[0] = true;
  340. //LeftX
  341. mState.mAxes[1].rel = inputState.Gamepad.sThumbLX - mState.mAxes[1].abs;
  342. mState.mAxes[1].abs = inputState.Gamepad.sThumbLX;
  343. if(mState.mAxes[1].rel != 0)
  344. axisMoved[1] = true;
  345. //RightY
  346. value = -(int)inputState.Gamepad.sThumbRY;
  347. mState.mAxes[2].rel = value - mState.mAxes[2].abs;
  348. mState.mAxes[2].abs = value;
  349. if(mState.mAxes[2].rel != 0)
  350. axisMoved[2] = true;
  351. //RightX
  352. mState.mAxes[3].rel = inputState.Gamepad.sThumbRX - mState.mAxes[3].abs;
  353. mState.mAxes[3].abs = inputState.Gamepad.sThumbRX;
  354. if(mState.mAxes[3].rel != 0)
  355. axisMoved[3] = true;
  356. //Left trigger
  357. value = inputState.Gamepad.bLeftTrigger * 129;
  358. if(value > JoyStick::MAX_AXIS)
  359. value = JoyStick::MAX_AXIS;
  360. mState.mAxes[4].rel = value - mState.mAxes[4].abs;
  361. mState.mAxes[4].abs = value;
  362. if(mState.mAxes[4].rel != 0)
  363. axisMoved[4] = true;
  364. //Right trigger
  365. value = (int)inputState.Gamepad.bRightTrigger * 129;
  366. if(value > JoyStick::MAX_AXIS)
  367. value = JoyStick::MAX_AXIS;
  368. mState.mAxes[5].rel = value - mState.mAxes[5].abs;
  369. mState.mAxes[5].abs = value;
  370. if(mState.mAxes[5].rel != 0)
  371. axisMoved[5] = true;
  372. //POV
  373. int previousPov = mState.mPOV[0].direction;
  374. int& pov = mState.mPOV[0].direction;
  375. pov = Pov::Centered;
  376. if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
  377. pov |= Pov::North;
  378. else if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
  379. pov |= Pov::South;
  380. if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
  381. pov |= Pov::West;
  382. else if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
  383. pov |= Pov::East;
  384. //Buttons - The first 4 buttons don't need to be checked since they represent the dpad
  385. bool previousButtons[XINPUT_TRANSLATED_BUTTON_COUNT];
  386. std::copy(mState.mButtons.begin(), mState.mButtons.end(), previousButtons);
  387. for (size_t i = 0; i < XINPUT_TRANSLATED_BUTTON_COUNT; i++)
  388. mState.mButtons[i] = (inputState.Gamepad.wButtons & (1 << (i + 4))) != 0;
  389. //Send events
  390. if (mBuffered && mListener)
  391. {
  392. JoyStickEvent joystickEvent(this, mState);
  393. //Axes
  394. for (int i = 0; i < XINPUT_TRANSLATED_AXIS_COUNT; i++)
  395. {
  396. if (axisMoved[i] && !mListener->axisMoved(joystickEvent, i))
  397. return;
  398. }
  399. //POV
  400. if (previousPov != pov && !mListener->povMoved(joystickEvent, 0))
  401. return;
  402. //Buttons
  403. for (int i = 0; i < XINPUT_TRANSLATED_BUTTON_COUNT; i++)
  404. {
  405. if (!previousButtons[i] && mState.mButtons[i])
  406. {
  407. if (!mListener->buttonPressed(joystickEvent, i))
  408. return;
  409. }
  410. else if (previousButtons[i] && !mState.mButtons[i])
  411. {
  412. if (!mListener->buttonReleased(joystickEvent, i))
  413. return;
  414. }
  415. }
  416. }
  417. #endif
  418. }
  419. //--------------------------------------------------------------------------------------------------//
  420. bool Win32JoyStick::_doButtonClick( int button, DIDEVICEOBJECTDATA& di )
  421. {
  422. if( di.dwData & 0x80 )
  423. {
  424. mState.mButtons[button] = true;
  425. if( mBuffered && mListener )
  426. return mListener->buttonPressed( JoyStickEvent( this, mState ), button );
  427. }
  428. else
  429. {
  430. mState.mButtons[button] = false;
  431. if( mBuffered && mListener )
  432. return mListener->buttonReleased( JoyStickEvent( this, mState ), button );
  433. }
  434. return true;
  435. }
  436. //--------------------------------------------------------------------------------------------------//
  437. bool Win32JoyStick::_changePOV( int pov, DIDEVICEOBJECTDATA& di )
  438. {
  439. //Some drivers report a value of 65,535, instead of —1,
  440. //for the center position
  441. if(LOWORD(di.dwData) == 0xFFFF)
  442. {
  443. mState.mPOV[pov].direction = Pov::Centered;
  444. }
  445. else
  446. {
  447. switch(di.dwData)
  448. {
  449. case 0: mState.mPOV[pov].direction = Pov::North; break;
  450. case 4500: mState.mPOV[pov].direction = Pov::NorthEast; break;
  451. case 9000: mState.mPOV[pov].direction = Pov::East; break;
  452. case 13500: mState.mPOV[pov].direction = Pov::SouthEast; break;
  453. case 18000: mState.mPOV[pov].direction = Pov::South; break;
  454. case 22500: mState.mPOV[pov].direction = Pov::SouthWest; break;
  455. case 27000: mState.mPOV[pov].direction = Pov::West; break;
  456. case 31500: mState.mPOV[pov].direction = Pov::NorthWest; break;
  457. }
  458. }
  459. if( mBuffered && mListener )
  460. return mListener->povMoved( JoyStickEvent( this, mState ), pov );
  461. return true;
  462. }
  463. //--------------------------------------------------------------------------------------------------//
  464. void Win32JoyStick::setBuffered(bool buffered)
  465. {
  466. mBuffered = buffered;
  467. }
  468. //--------------------------------------------------------------------------------------------------//
  469. Interface* Win32JoyStick::queryInterface(Interface::IType type)
  470. {
  471. if( mFfDevice && type == Interface::ForceFeedback )
  472. return mFfDevice;
  473. else
  474. return 0;
  475. }
  476. //--------------------------------------------------------------------------------------------------//
  477. #ifdef OIS_WIN32_XINPUT_SUPPORT
  478. void Win32JoyStick::CheckXInputDevices(JoyStickInfoList &joys)
  479. {
  480. IWbemLocator* pIWbemLocator = NULL;
  481. IEnumWbemClassObject* pEnumDevices = NULL;
  482. IWbemClassObject* pDevices[20] = {0};
  483. IWbemServices* pIWbemServices = NULL;
  484. BSTR bstrNamespace = NULL;
  485. BSTR bstrDeviceID = NULL;
  486. BSTR bstrClassName = NULL;
  487. DWORD uReturned = 0;
  488. bool bIsXinputDevice= false;
  489. DWORD iDevice = 0;
  490. int xDevice = 0;
  491. VARIANT var;
  492. HRESULT hr;
  493. if(joys.size() == 0)
  494. return;
  495. // CoInit if needed
  496. hr = CoInitialize(NULL);
  497. bool bCleanupCOM = SUCCEEDED(hr);
  498. // Create WMI
  499. hr = CoCreateInstance(__uuidof(WbemLocator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IWbemLocator), (LPVOID*)&pIWbemLocator);
  500. if( FAILED(hr) || pIWbemLocator == NULL )
  501. goto LCleanup;
  502. bstrNamespace = SysAllocString( L"\\\\.\\root\\cimv2" );
  503. if( bstrNamespace == NULL )
  504. goto LCleanup;
  505. bstrClassName = SysAllocString( L"Win32_PNPEntity" );
  506. if( bstrClassName == NULL )
  507. goto LCleanup;
  508. bstrDeviceID = SysAllocString( L"DeviceID" );
  509. if( bstrDeviceID == NULL )
  510. goto LCleanup;
  511. // Connect to WMI
  512. hr = pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, 0L, 0L, NULL, NULL, &pIWbemServices );
  513. if( FAILED(hr) || pIWbemServices == NULL )
  514. goto LCleanup;
  515. // Switch security level to IMPERSONATE.
  516. CoSetProxyBlanket(pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );
  517. hr = pIWbemServices->CreateInstanceEnum( bstrClassName, 0, NULL, &pEnumDevices );
  518. if( FAILED(hr) || pEnumDevices == NULL )
  519. goto LCleanup;
  520. // Loop over all devices
  521. for( ;; )
  522. {
  523. // Get 20 at a time
  524. hr = pEnumDevices->Next(5000, 20, pDevices, &uReturned);
  525. if( FAILED(hr) )
  526. goto LCleanup;
  527. if( uReturned == 0 )
  528. break;
  529. for(iDevice = 0; iDevice < uReturned; iDevice++)
  530. {
  531. // For each device, get its device ID
  532. hr = pDevices[iDevice]->Get(bstrDeviceID, 0L, &var, NULL, NULL);
  533. if(SUCCEEDED(hr) && var.vt == VT_BSTR && var.bstrVal != NULL)
  534. {
  535. // Check if the device ID contains "IG_". If it does, then it's an XInput device - This information can not be found from DirectInput
  536. if(wcsstr(var.bstrVal, L"IG_"))
  537. {
  538. // If it does, then get the VID/PID from var.bstrVal
  539. DWORD dwPid = 0, dwVid = 0;
  540. WCHAR* strVid = wcsstr( var.bstrVal, L"VID_" );
  541. if(strVid && swscanf_s( strVid, L"VID_%4X", &dwVid ) != 1)
  542. dwVid = 0;
  543. WCHAR* strPid = wcsstr( var.bstrVal, L"PID_" );
  544. if(strPid && swscanf_s( strPid, L"PID_%4X", &dwPid ) != 1)
  545. dwPid = 0;
  546. // Compare the VID/PID to the DInput device
  547. DWORD dwVidPid = MAKELONG(dwVid, dwPid);
  548. for(JoyStickInfoList::iterator i = joys.begin(); i != joys.end(); ++i)
  549. {
  550. if(!i->isXInput && dwVidPid == i->productGuid.Data1)
  551. {
  552. i->isXInput = true;
  553. i->xInputDev = xDevice;
  554. ++xDevice;
  555. }
  556. }
  557. if(joys.size() == 0)
  558. goto LCleanup;
  559. }
  560. }
  561. SAFE_RELEASE(pDevices[iDevice]);
  562. }
  563. }
  564. LCleanup:
  565. if(bstrNamespace)
  566. SysFreeString(bstrNamespace);
  567. if(bstrDeviceID)
  568. SysFreeString(bstrDeviceID);
  569. if(bstrClassName)
  570. SysFreeString(bstrClassName);
  571. for(iDevice=0; iDevice < 20; iDevice++)
  572. SAFE_RELEASE(pDevices[iDevice]);
  573. SAFE_RELEASE(pEnumDevices);
  574. SAFE_RELEASE(pIWbemLocator);
  575. SAFE_RELEASE(pIWbemServices);
  576. if(bCleanupCOM)
  577. CoUninitialize();
  578. }
  579. #endif