winDirectInput.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformWin32/platformWin32.h"
  23. #include "platform/platformVideo.h"
  24. #include "platformWin32/winDirectInput.h"
  25. #include "platformWin32/winDInputDevice.h"
  26. #include "platform/event.h"
  27. #include "console/console.h"
  28. #include "console/consoleTypes.h"
  29. #include "input/actionMap.h"
  30. #include "game/gameInterface.h"
  31. #include <Xinput.h>
  32. //------------------------------------------------------------------------------
  33. // Static class variables:
  34. bool DInputManager::smKeyboardEnabled = true;
  35. bool DInputManager::smMouseEnabled = false;
  36. bool DInputManager::smJoystickEnabled = false;
  37. bool DInputManager::smXInputEnabled = false;
  38. // Type definitions:
  39. typedef HRESULT (WINAPI* FN_DirectInputCreate)(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID *ppvOut, LPUNKNOWN punkOuter);
  40. //------------------------------------------------------------------------------
  41. DInputManager::DInputManager()
  42. {
  43. mEnabled = false;
  44. mDInputLib = NULL;
  45. mDInputInterface = NULL;
  46. mKeyboardActive = mMouseActive = mJoystickActive = false;
  47. mXInputActive = true;
  48. mXInputLib = NULL;
  49. for(S32 i=0; i<4; i++)
  50. mLastDisconnectTime[i] = -1;
  51. }
  52. //------------------------------------------------------------------------------
  53. void DInputManager::init()
  54. {
  55. Con::addVariable( "pref::Input::KeyboardEnabled", TypeBool, &smKeyboardEnabled );
  56. Con::addVariable( "pref::Input::MouseEnabled", TypeBool, &smMouseEnabled );
  57. Con::addVariable( "pref::Input::JoystickEnabled", TypeBool, &smJoystickEnabled );
  58. Con::addVariable( "pref::Input::XInputEnabled", TypeBool, &smXInputEnabled );
  59. }
  60. //------------------------------------------------------------------------------
  61. bool DInputManager::enable()
  62. {
  63. FN_DirectInputCreate fnDInputCreate;
  64. disable();
  65. mXInputLib = LoadLibrary( dT("xinput9_1_0.dll") );
  66. if( mXInputLib )
  67. {
  68. mfnXInputGetState = (FN_XInputGetState) GetProcAddress( mXInputLib, "XInputGetState" );
  69. mfnXInputSetState = (FN_XInputSetState) GetProcAddress( mXInputLib, "XInputSetState" );
  70. if ( mfnXInputGetState && mfnXInputSetState )
  71. {
  72. mXInputStateReset = true;
  73. mXInputDeadZoneOn = true;
  74. smXInputEnabled = true;
  75. }
  76. }
  77. else
  78. {
  79. Con::warnf("Unable to enable XInput.");
  80. }
  81. mDInputLib = LoadLibrary( dT("DInput8.dll") );
  82. if ( mDInputLib )
  83. {
  84. fnDInputCreate = (FN_DirectInputCreate) GetProcAddress( mDInputLib, "DirectInput8Create" );
  85. if ( fnDInputCreate )
  86. {
  87. bool result = SUCCEEDED( fnDInputCreate( winState.appInstance, DIRECTINPUT_VERSION,
  88. IID_IDirectInput8, reinterpret_cast<void**>(&mDInputInterface), NULL ));
  89. if ( result )
  90. {
  91. enumerateDevices();
  92. mEnabled = true;
  93. return true;
  94. }
  95. }
  96. }
  97. disable();
  98. return false;
  99. }
  100. //------------------------------------------------------------------------------
  101. void DInputManager::disable()
  102. {
  103. unacquire( SI_ANY, SI_ANY );
  104. DInputDevice* dptr;
  105. iterator ptr = begin();
  106. while ( ptr != end() )
  107. {
  108. dptr = dynamic_cast<DInputDevice*>( *ptr );
  109. if ( dptr )
  110. {
  111. removeObject( dptr );
  112. //if ( dptr->getManager() )
  113. //dptr->getManager()->unregisterObject( dptr );
  114. delete dptr;
  115. ptr = begin();
  116. }
  117. else
  118. ptr++;
  119. }
  120. if ( mDInputInterface )
  121. {
  122. mDInputInterface->Release();
  123. mDInputInterface = NULL;
  124. }
  125. if ( mDInputLib )
  126. {
  127. FreeLibrary( mDInputLib );
  128. mDInputLib = NULL;
  129. }
  130. if ( mfnXInputGetState )
  131. {
  132. mXInputStateReset = true;
  133. mfnXInputGetState = NULL;
  134. mfnXInputSetState = NULL;
  135. }
  136. if ( mXInputLib )
  137. {
  138. FreeLibrary( mXInputLib );
  139. mXInputLib = NULL;
  140. }
  141. mEnabled = false;
  142. }
  143. //------------------------------------------------------------------------------
  144. void DInputManager::onDeleteNotify( SimObject* object )
  145. {
  146. Parent::onDeleteNotify( object );
  147. }
  148. //------------------------------------------------------------------------------
  149. bool DInputManager::onAdd()
  150. {
  151. if ( !Parent::onAdd() )
  152. return false;
  153. acquire( SI_ANY, SI_ANY );
  154. return true;
  155. }
  156. //------------------------------------------------------------------------------
  157. void DInputManager::onRemove()
  158. {
  159. unacquire( SI_ANY, SI_ANY );
  160. Parent::onRemove();
  161. }
  162. //------------------------------------------------------------------------------
  163. bool DInputManager::acquire( U8 deviceType, U8 deviceID )
  164. {
  165. bool anyActive = false;
  166. DInputDevice* dptr;
  167. for ( iterator ptr = begin(); ptr != end(); ptr++ )
  168. {
  169. dptr = dynamic_cast<DInputDevice*>( *ptr );
  170. if ( dptr
  171. && ( ( deviceType == SI_ANY ) || ( dptr->getDeviceType() == deviceType ) )
  172. && ( ( deviceID == SI_ANY ) || ( dptr->getDeviceID() == deviceID ) ) )
  173. {
  174. if ( dptr->acquire() )
  175. anyActive = true;
  176. }
  177. }
  178. return anyActive;
  179. }
  180. //------------------------------------------------------------------------------
  181. void DInputManager::unacquire( U8 deviceType, U8 deviceID )
  182. {
  183. DInputDevice* dptr;
  184. for ( iterator ptr = begin(); ptr != end(); ptr++ )
  185. {
  186. dptr = dynamic_cast<DInputDevice*>( *ptr );
  187. if ( dptr
  188. && ( ( deviceType == SI_ANY ) || ( dptr->getDeviceType() == deviceType ) )
  189. && ( ( deviceID == SI_ANY ) || ( dptr->getDeviceID() == deviceID ) ) )
  190. dptr->unacquire();
  191. }
  192. }
  193. //------------------------------------------------------------------------------
  194. void DInputManager::process()
  195. {
  196. if ( isXInputActive() )
  197. processXInput();
  198. DInputDevice* dptr;
  199. for ( iterator ptr = begin(); ptr != end(); ptr++ )
  200. {
  201. dptr = dynamic_cast<DInputDevice*>( *ptr );
  202. if ( dptr )
  203. dptr->process();
  204. }
  205. }
  206. //------------------------------------------------------------------------------
  207. void DInputManager::enumerateDevices()
  208. {
  209. if ( mDInputInterface )
  210. {
  211. DInputDevice::init();
  212. DInputDevice::smDInputInterface = mDInputInterface;
  213. mDInputInterface->EnumDevices( DI8DEVTYPE_KEYBOARD, EnumDevicesProc, this, DIEDFL_ATTACHEDONLY );
  214. mDInputInterface->EnumDevices( DI8DEVTYPE_MOUSE, EnumDevicesProc, this, DIEDFL_ATTACHEDONLY );
  215. mDInputInterface->EnumDevices( DI8DEVTYPE_JOYSTICK, EnumDevicesProc, this, DIEDFL_ATTACHEDONLY );
  216. mDInputInterface->EnumDevices( DI8DEVTYPE_GAMEPAD, EnumDevicesProc, this, DIEDFL_ATTACHEDONLY );
  217. }
  218. }
  219. //------------------------------------------------------------------------------
  220. BOOL CALLBACK DInputManager::EnumDevicesProc( const DIDEVICEINSTANCE* pddi, LPVOID pvRef )
  221. {
  222. DInputManager* manager = (DInputManager*) pvRef;
  223. DInputDevice* newDevice = new DInputDevice( pddi );
  224. manager->addObject( newDevice );
  225. if ( !newDevice->create() )
  226. {
  227. manager->removeObject( newDevice );
  228. delete newDevice;
  229. }
  230. return (DIENUM_CONTINUE);
  231. }
  232. //------------------------------------------------------------------------------
  233. bool DInputManager::enableKeyboard()
  234. {
  235. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  236. if ( !mgr || !mgr->isEnabled() )
  237. return( false );
  238. if ( smKeyboardEnabled && mgr->isKeyboardActive() )
  239. return( true );
  240. smKeyboardEnabled = true;
  241. if ( Input::isActive() )
  242. smKeyboardEnabled = mgr->activateKeyboard();
  243. if ( smKeyboardEnabled )
  244. {
  245. Con::printf( "DirectInput keyboard enabled." );
  246. }
  247. else
  248. {
  249. Con::warnf( "DirectInput keyboard failed to enable!" );
  250. }
  251. return( smKeyboardEnabled );
  252. }
  253. //------------------------------------------------------------------------------
  254. void DInputManager::disableKeyboard()
  255. {
  256. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  257. if ( !mgr || !mgr->isEnabled() || !smKeyboardEnabled )
  258. return;
  259. mgr->deactivateKeyboard();
  260. smKeyboardEnabled = false;
  261. Con::printf( "DirectInput keyboard disabled." );
  262. }
  263. //------------------------------------------------------------------------------
  264. bool DInputManager::isKeyboardEnabled()
  265. {
  266. return( smKeyboardEnabled );
  267. }
  268. //------------------------------------------------------------------------------
  269. bool DInputManager::activateKeyboard()
  270. {
  271. if ( !mEnabled || !Input::isActive() || !smKeyboardEnabled )
  272. return( false );
  273. // Acquire only one keyboard:
  274. mKeyboardActive = acquire( KeyboardDeviceType, 0 );
  275. return( mKeyboardActive );
  276. }
  277. //------------------------------------------------------------------------------
  278. void DInputManager::deactivateKeyboard()
  279. {
  280. if ( mEnabled && mKeyboardActive )
  281. {
  282. unacquire( KeyboardDeviceType, SI_ANY );
  283. mKeyboardActive = false;
  284. }
  285. }
  286. //------------------------------------------------------------------------------
  287. bool DInputManager::enableMouse()
  288. {
  289. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  290. if ( !mgr || !mgr->isEnabled() )
  291. return( false );
  292. if ( smMouseEnabled && mgr->isMouseActive() )
  293. return( true );
  294. smMouseEnabled = true;
  295. if ( Input::isActive() )
  296. smMouseEnabled = mgr->activateMouse();
  297. if ( smMouseEnabled )
  298. {
  299. Con::printf( "DirectInput mouse enabled." );
  300. }
  301. else
  302. {
  303. Con::warnf( "DirectInput mouse failed to enable!" );
  304. }
  305. return( smMouseEnabled );
  306. }
  307. //------------------------------------------------------------------------------
  308. void DInputManager::disableMouse()
  309. {
  310. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  311. if ( !mgr || !mgr->isEnabled() || !smMouseEnabled )
  312. return;
  313. mgr->deactivateMouse();
  314. smMouseEnabled = false;
  315. Con::printf( "DirectInput mouse disabled." );
  316. }
  317. //------------------------------------------------------------------------------
  318. bool DInputManager::isMouseEnabled()
  319. {
  320. return( smMouseEnabled );
  321. }
  322. //------------------------------------------------------------------------------
  323. bool DInputManager::activateMouse()
  324. {
  325. if ( !mEnabled || !Input::isActive() || !smMouseEnabled )
  326. return( false );
  327. mMouseActive = acquire( MouseDeviceType, SI_ANY );
  328. return( mMouseActive );
  329. }
  330. //------------------------------------------------------------------------------
  331. void DInputManager::deactivateMouse()
  332. {
  333. if ( mEnabled && mMouseActive )
  334. {
  335. unacquire( MouseDeviceType, SI_ANY );
  336. mMouseActive = false;
  337. }
  338. }
  339. //------------------------------------------------------------------------------
  340. bool DInputManager::enableJoystick()
  341. {
  342. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  343. if ( !mgr || !mgr->isEnabled() )
  344. return( false );
  345. if ( smJoystickEnabled && mgr->isJoystickActive() )
  346. return( true );
  347. smJoystickEnabled = true;
  348. if ( Input::isActive() )
  349. smJoystickEnabled = mgr->activateJoystick();
  350. if ( smJoystickEnabled )
  351. {
  352. Con::printf( "DirectInput joystick enabled." );
  353. }
  354. else
  355. {
  356. Con::warnf( "DirectInput joystick failed to enable!" );
  357. }
  358. return( smJoystickEnabled );
  359. }
  360. //------------------------------------------------------------------------------
  361. void DInputManager::disableJoystick()
  362. {
  363. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  364. if ( !mgr || !mgr->isEnabled() || !smJoystickEnabled )
  365. return;
  366. mgr->deactivateJoystick();
  367. smJoystickEnabled = false;
  368. Con::printf( "DirectInput joystick disabled." );
  369. }
  370. //------------------------------------------------------------------------------
  371. bool DInputManager::isJoystickEnabled()
  372. {
  373. return( smJoystickEnabled );
  374. }
  375. //------------------------------------------------------------------------------
  376. bool DInputManager::activateJoystick()
  377. {
  378. if ( !mEnabled || !Input::isActive() || !smJoystickEnabled )
  379. return( false );
  380. mJoystickActive = acquire( JoystickDeviceType, SI_ANY );
  381. return( mJoystickActive );
  382. }
  383. //------------------------------------------------------------------------------
  384. void DInputManager::deactivateJoystick()
  385. {
  386. if ( mEnabled && mJoystickActive )
  387. {
  388. unacquire( JoystickDeviceType, SI_ANY );
  389. mJoystickActive = false;
  390. }
  391. }
  392. //------------------------------------------------------------------------------
  393. const char* DInputManager::getJoystickAxesString( U32 deviceID )
  394. {
  395. DInputDevice* dptr;
  396. for ( iterator ptr = begin(); ptr != end(); ptr++ )
  397. {
  398. dptr = dynamic_cast<DInputDevice*>( *ptr );
  399. if ( dptr && ( dptr->getDeviceType() == JoystickDeviceType ) && ( dptr->getDeviceID() == deviceID ) )
  400. return( dptr->getJoystickAxesString() );
  401. }
  402. return( "" );
  403. }
  404. bool DInputManager::enableXInput()
  405. {
  406. // Largely, this series of functions is identical to the Joystick versions,
  407. // except that XInput cannot be "activated" or "deactivated". You either have
  408. // the DLL or you don't. Beyond that, you have up to four controllers
  409. // connected at any given time
  410. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  411. if ( !mgr || !mgr->isEnabled() )
  412. return( false );
  413. if ( mgr->isXInputActive() )
  414. return( true );
  415. if ( Input::isActive() )
  416. mgr->activateXInput();
  417. if ( smXInputEnabled )
  418. {
  419. Con::printf( "XInput enabled." );
  420. }
  421. else
  422. {
  423. Con::warnf( "XInput failed to enable!" );
  424. }
  425. return( smXInputEnabled );
  426. }
  427. //------------------------------------------------------------------------------
  428. void DInputManager::disableXInput()
  429. {
  430. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  431. if ( !mgr || !mgr->isEnabled())
  432. return;
  433. mgr->deactivateXInput();
  434. Con::printf( "XInput disabled." );
  435. }
  436. //------------------------------------------------------------------------------
  437. bool DInputManager::isXInputEnabled()
  438. {
  439. return( smXInputEnabled );
  440. }
  441. //------------------------------------------------------------------------------
  442. bool DInputManager::isXInputConnected(int controllerID)
  443. {
  444. if (controllerID >= XINPUT_MAX_CONTROLLERS || controllerID < 0)
  445. {
  446. Con::warnf("Invalid device index: %d. Index should be between 0 and %d.", controllerID, XINPUT_MAX_CONTROLLERS - 1);
  447. return false;
  448. }
  449. return( mXInputStateNew[controllerID].bConnected );
  450. }
  451. int DInputManager::getXInputState(int controllerID, int property, bool current)
  452. {
  453. if (controllerID >= XINPUT_MAX_CONTROLLERS || controllerID < 0)
  454. {
  455. Con::warnf("Invalid device index: %d. Index should be between 0 and %d.", controllerID, XINPUT_MAX_CONTROLLERS - 1);
  456. return -1;
  457. }
  458. int retVal;
  459. switch(property)
  460. {
  461. #define CHECK_PROP_ANALOG(prop, stateTest) \
  462. case prop: (current) ? retVal = mXInputStateNew[controllerID].state.Gamepad.##stateTest : retVal = mXInputStateOld[controllerID].state.Gamepad.##stateTest; return retVal;
  463. CHECK_PROP_ANALOG(XI_THUMBLX, sThumbLX)
  464. CHECK_PROP_ANALOG(XI_THUMBLY, sThumbLY)
  465. CHECK_PROP_ANALOG(XI_THUMBRX, sThumbRX)
  466. CHECK_PROP_ANALOG(XI_THUMBRY, sThumbRY)
  467. CHECK_PROP_ANALOG(XI_LEFT_TRIGGER, bLeftTrigger)
  468. CHECK_PROP_ANALOG(XI_RIGHT_TRIGGER, bRightTrigger)
  469. #undef CHECK_PROP_ANALOG
  470. #define CHECK_PROP_DIGITAL(prop, stateTest) \
  471. case prop: (current) ? retVal = (( mXInputStateNew[controllerID].state.Gamepad.wButtons & stateTest ) != 0 ) : retVal = (( mXInputStateOld[controllerID].state.Gamepad.wButtons & stateTest ) != 0 ); return retVal;
  472. CHECK_PROP_DIGITAL(SI_UPOV, XINPUT_GAMEPAD_DPAD_UP)
  473. CHECK_PROP_DIGITAL(SI_DPOV, XINPUT_GAMEPAD_DPAD_DOWN)
  474. CHECK_PROP_DIGITAL(SI_LPOV, XINPUT_GAMEPAD_DPAD_LEFT)
  475. CHECK_PROP_DIGITAL(SI_RPOV, XINPUT_GAMEPAD_DPAD_RIGHT)
  476. CHECK_PROP_DIGITAL(XI_START, XINPUT_GAMEPAD_START)
  477. CHECK_PROP_DIGITAL(XI_BACK, XINPUT_GAMEPAD_BACK)
  478. CHECK_PROP_DIGITAL(XI_LEFT_THUMB, XINPUT_GAMEPAD_LEFT_THUMB)
  479. CHECK_PROP_DIGITAL(XI_RIGHT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB)
  480. CHECK_PROP_DIGITAL(XI_LEFT_SHOULDER, XINPUT_GAMEPAD_LEFT_SHOULDER)
  481. CHECK_PROP_DIGITAL(XI_RIGHT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER)
  482. CHECK_PROP_DIGITAL(XI_A, XINPUT_GAMEPAD_A)
  483. CHECK_PROP_DIGITAL(XI_B, XINPUT_GAMEPAD_B)
  484. CHECK_PROP_DIGITAL(XI_X, XINPUT_GAMEPAD_X)
  485. CHECK_PROP_DIGITAL(XI_Y, XINPUT_GAMEPAD_Y)
  486. #undef CHECK_PROP_DIGITAL
  487. }
  488. return -1;
  489. }
  490. //------------------------------------------------------------------------------
  491. bool DInputManager::activateXInput()
  492. {
  493. if ( !mEnabled || !Input::isActive())
  494. return( false );
  495. mXInputActive = true; //acquire( GamepadDeviceType, SI_ANY );
  496. return( mXInputActive );
  497. }
  498. //------------------------------------------------------------------------------
  499. void DInputManager::deactivateXInput()
  500. {
  501. if ( mEnabled && mXInputActive )
  502. {
  503. unacquire( GamepadDeviceType, SI_ANY );
  504. mXInputActive = false;
  505. }
  506. }
  507. //------------------------------------------------------------------------------
  508. bool DInputManager::rumble( const char *pDeviceName, float x, float y )
  509. {
  510. // Determine the device
  511. U32 deviceType;
  512. U32 deviceInst;
  513. // Find the requested device
  514. if ( !ActionMap::getDeviceTypeAndInstance(pDeviceName, deviceType, deviceInst) )
  515. {
  516. Con::printf("DInputManager::rumble: unknown device: %s", pDeviceName);
  517. return false;
  518. }
  519. // clamp (x, y) to the range of [0 ... 1] each
  520. x = mClampF(x, 0.f, 1.f);
  521. y = mClampF(y, 0.f, 1.f);
  522. // Easy path for xinput devices.
  523. if(deviceType == GamepadDeviceType)
  524. {
  525. XINPUT_VIBRATION vibration;
  526. vibration.wLeftMotorSpeed = static_cast<int>(x * 65535);
  527. vibration.wRightMotorSpeed = static_cast<int>(y * 65535);
  528. return ( mfnXInputSetState(deviceInst, &vibration) == ERROR_SUCCESS );
  529. }
  530. switch ( deviceType )
  531. {
  532. case JoystickDeviceType:
  533. // Find the device and shake it!
  534. DInputDevice* dptr;
  535. for ( iterator ptr = begin(); ptr != end(); ptr++ )
  536. {
  537. dptr = dynamic_cast<DInputDevice*>( *ptr );
  538. if ( dptr )
  539. {
  540. if (deviceType == dptr->getDeviceType() && deviceInst == dptr->getDeviceID())
  541. {
  542. dptr->rumble(x, y);
  543. return true;
  544. }
  545. }
  546. }
  547. // We should never get here... something's really messed up
  548. Con::errorf( "DInputManager::rumbleJoystick - Couldn't find device to rumble! This should never happen!\n" );
  549. return false;
  550. default:
  551. Con::printf("DInputManager::rumble - only supports joystick and xinput/gamepad devices");
  552. return false;
  553. }
  554. }
  555. void DInputManager::buildXInputEvent( U32 deviceInst, XInputEventType objType, InputObjectInstances objInst, InputActionType action, float fValue )
  556. {
  557. InputEvent newEvent;
  558. newEvent.deviceType = GamepadDeviceType;
  559. newEvent.deviceInst = deviceInst;
  560. newEvent.objType = objType;
  561. newEvent.objInst = objInst;
  562. newEvent.action = action;
  563. newEvent.fValues[0] = fValue;
  564. //we need to find the gameinterface object from here
  565. Game->postEvent(newEvent);
  566. }
  567. // The next three functions: fireXInputConnectEvent, fireXInputMoveEvent, and fireXInputButtonEvent
  568. // determine whether a "delta" has occurred between the last captured controller state and the
  569. // currently captured controller state and only if so, do we fire an event. The shortcutter
  570. // "mXInputStateReset" is the exception and is true whenever DirectInput gets reset (because
  571. // the user ALT-TABBED away, for example). That means that after every context switch,
  572. // you will get a full set of updates on the "true" state of the controller.
  573. inline void DInputManager::fireXInputConnectEvent( int controllerID, bool condition, bool connected )
  574. {
  575. if ( mXInputStateReset || condition )
  576. {
  577. buildXInputEvent( controllerID, XI_BUTTON, XI_CONNECT, connected ? XI_MAKE : XI_BREAK, 0);
  578. }
  579. }
  580. inline void DInputManager::fireXInputMoveEvent( int controllerID, bool condition, InputObjectInstances objInst, float fValue )
  581. {
  582. if ( mXInputStateReset || condition )
  583. {
  584. /*
  585. //Uncomment if you want real-time Input displayed to the console
  586. const char *objName;
  587. switch (objInst)
  588. {
  589. case XI_THUMBLX: objName = "THUMBLX"; break;
  590. case XI_THUMBLY: objName = "THUMBLY"; break;
  591. case XI_THUMBRX: objName = "THUMBRX"; break;
  592. case XI_THUMBRY: objName = "THUMBRY"; break;
  593. case XI_LEFT_TRIGGER: objName = "LEFT_TRIGGER"; break;
  594. case XI_RIGHT_TRIGGER: objName = "RIGHT_TRIGGER"; break;
  595. default: objName = "UNKNOWN"; break;
  596. }
  597. Con::printf("%s %.1f", objName, fValue);
  598. */
  599. buildXInputEvent( controllerID, XI_AXIS, objInst, XI_MOVE, fValue );
  600. }
  601. }
  602. inline void DInputManager::fireXInputButtonEvent( int controllerID, bool forceFire, int button, InputObjectInstances objInst )
  603. {
  604. if ( mXInputStateReset || forceFire || ((mXInputStateNew[controllerID].state.Gamepad.wButtons & button) != (mXInputStateOld[controllerID].state.Gamepad.wButtons & button)) )
  605. {
  606. InputActionType action = ((mXInputStateNew[controllerID].state.Gamepad.wButtons & button) != 0) ? XI_MAKE : XI_BREAK;
  607. /*
  608. //Uncomment if you want real-time Input displayed to the console
  609. char *objName;
  610. objName = "";
  611. switch (objInst)
  612. {
  613. case XI_DPAD_UP: objName = "DPAD_UP"; break;
  614. case XI_DPAD_DOWN: objName = "DPAD_DOWN"; break;
  615. case XI_DPAD_LEFT: objName = "DPAD_LEFT"; break;
  616. case XI_DPAD_RIGHT: objName = "DPAD_RIGHT"; break;
  617. case XI_START: objName = "START"; break;
  618. case XI_BACK: objName = "BACK"; break;
  619. case XI_LEFT_THUMB: objName = "LEFT_THUMB"; break;
  620. case XI_RIGHT_THUMB: objName = "RIGHT_THUMB"; break;
  621. case XI_LEFT_SHOULDER: objName = "LEFT_SHOULDER"; break;
  622. case XI_RIGHT_SHOULDER: objName = "RIGHT_SHOULDER"; break;
  623. case XI_A: objName = "A"; break;
  624. case XI_B: objName = "B"; break;
  625. case XI_X: objName = "X"; break;
  626. case XI_Y: objName = "Y"; break;
  627. default: Con::printf("%u", objInst); break;
  628. }
  629. Con::printf("%s", objName);
  630. */
  631. buildXInputEvent( controllerID, XI_BUTTON, objInst, action, ( action == XI_MAKE ? 1.0f : 0.0f ) );
  632. }
  633. }
  634. void DInputManager::processXInput( void )
  635. {
  636. const U32 curTime = Platform::getRealMilliseconds();
  637. // We only want to check one disconnected device per frame.
  638. bool foundDisconnected = false;
  639. if ( mfnXInputGetState )
  640. {
  641. for ( int i=0; i<4; i++ )
  642. {
  643. // Calling XInputGetState on a disconnected controller takes a fair
  644. // amount of time (probably because it tries to locate it), so we
  645. // add a delay - only check every 250ms or so.
  646. if(mLastDisconnectTime[i] != -1)
  647. {
  648. // If it's not -1, then it was disconnected list time we checked.
  649. // So skip until it's time.
  650. if((curTime-mLastDisconnectTime[i]) < csmDisconnectedSkipDelay)
  651. {
  652. continue;
  653. }
  654. // If we already checked a disconnected controller, don't try any
  655. // further potentially disconnected devices.
  656. if(foundDisconnected)
  657. {
  658. // If we're skipping this, defer it out by the skip delay
  659. // so we don't get clumped checks.
  660. mLastDisconnectTime[i] += csmDisconnectedSkipDelay;
  661. continue;
  662. }
  663. }
  664. mXInputStateOld[i] = mXInputStateNew[i];
  665. mXInputStateNew[i].bConnected = ( mfnXInputGetState( i, &mXInputStateNew[i].state ) == ERROR_SUCCESS );
  666. // Update the last connected time.
  667. if(mXInputStateNew[i].bConnected)
  668. mLastDisconnectTime[i] = -1;
  669. else
  670. {
  671. foundDisconnected = true;
  672. mLastDisconnectTime[i] = curTime;
  673. }
  674. // trim the controller's thumbsticks to zero if they are within the deadzone
  675. if( mXInputDeadZoneOn )
  676. {
  677. // Zero value if thumbsticks are within the dead zone
  678. if(mXInputStateNew[i].state.Gamepad.sThumbLX < XINPUT_DEADZONE && mXInputStateNew[i].state.Gamepad.sThumbLX > -XINPUT_DEADZONE)
  679. mXInputStateNew[i].state.Gamepad.sThumbLX = 0;
  680. if(mXInputStateNew[i].state.Gamepad.sThumbLY < XINPUT_DEADZONE && mXInputStateNew[i].state.Gamepad.sThumbLY > -XINPUT_DEADZONE)
  681. mXInputStateNew[i].state.Gamepad.sThumbLY = 0;
  682. if(mXInputStateNew[i].state.Gamepad.sThumbRX < XINPUT_DEADZONE && mXInputStateNew[i].state.Gamepad.sThumbRX > -XINPUT_DEADZONE)
  683. mXInputStateNew[i].state.Gamepad.sThumbRX = 0;
  684. if(mXInputStateNew[i].state.Gamepad.sThumbRY < XINPUT_DEADZONE && mXInputStateNew[i].state.Gamepad.sThumbRY > -XINPUT_DEADZONE)
  685. mXInputStateNew[i].state.Gamepad.sThumbRY = 0;
  686. }
  687. // this controller was connected or disconnected
  688. bool bJustConnected = ( ( mXInputStateOld[i].bConnected != mXInputStateNew[i].bConnected ) && ( mXInputStateNew[i].bConnected ) );
  689. fireXInputConnectEvent( i, (mXInputStateOld[i].bConnected != mXInputStateNew[i].bConnected), mXInputStateNew[i].bConnected );
  690. // If this controller is disconnected, stop reporting events for it
  691. if ( !mXInputStateNew[i].bConnected )
  692. continue;
  693. // == LEFT THUMBSTICK ==
  694. fireXInputMoveEvent( i, ( bJustConnected ) || (mXInputStateNew[i].state.Gamepad.sThumbLX != mXInputStateOld[i].state.Gamepad.sThumbLX), XI_THUMBLX, (mXInputStateNew[i].state.Gamepad.sThumbLX / 32768.0f) );
  695. fireXInputMoveEvent( i, ( bJustConnected ) || (mXInputStateNew[i].state.Gamepad.sThumbLY != mXInputStateOld[i].state.Gamepad.sThumbLY), XI_THUMBLY, (mXInputStateNew[i].state.Gamepad.sThumbLY / 32768.0f) );
  696. // == RIGHT THUMBSTICK ==
  697. fireXInputMoveEvent( i, ( bJustConnected ) || (mXInputStateNew[i].state.Gamepad.sThumbRX != mXInputStateOld[i].state.Gamepad.sThumbRX), XI_THUMBRX, (mXInputStateNew[i].state.Gamepad.sThumbRX / 32768.0f) );
  698. fireXInputMoveEvent( i, ( bJustConnected ) || (mXInputStateNew[i].state.Gamepad.sThumbRY != mXInputStateOld[i].state.Gamepad.sThumbRY), XI_THUMBRY, (mXInputStateNew[i].state.Gamepad.sThumbRY / 32768.0f) );
  699. // == LEFT & RIGHT REAR TRIGGERS ==
  700. fireXInputMoveEvent( i, ( bJustConnected ) || (mXInputStateNew[i].state.Gamepad.bLeftTrigger != mXInputStateOld[i].state.Gamepad.bLeftTrigger), XI_LEFT_TRIGGER, (mXInputStateNew[i].state.Gamepad.bLeftTrigger / 255.0f) );
  701. fireXInputMoveEvent( i, ( bJustConnected ) || (mXInputStateNew[i].state.Gamepad.bRightTrigger != mXInputStateOld[i].state.Gamepad.bRightTrigger), XI_RIGHT_TRIGGER, (mXInputStateNew[i].state.Gamepad.bRightTrigger / 255.0f) );
  702. // == BUTTONS: DPAD ==
  703. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_DPAD_UP, SI_UPOV );
  704. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_DPAD_DOWN, SI_DPOV );
  705. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_DPAD_LEFT, SI_LPOV );
  706. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_DPAD_RIGHT, SI_RPOV );
  707. // == BUTTONS: START & BACK ==
  708. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_START, XI_START );
  709. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_BACK, XI_BACK );
  710. // == BUTTONS: LEFT AND RIGHT THUMBSTICK ==
  711. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_LEFT_THUMB, XI_LEFT_THUMB );
  712. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_RIGHT_THUMB, XI_RIGHT_THUMB );
  713. // == BUTTONS: LEFT AND RIGHT SHOULDERS (formerly WHITE and BLACK on Xbox 1) ==
  714. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_LEFT_SHOULDER, XI_LEFT_SHOULDER );
  715. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_RIGHT_SHOULDER, XI_RIGHT_SHOULDER );
  716. // == BUTTONS: A, B, X, and Y ==
  717. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_A, XI_A );
  718. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_B, XI_B );
  719. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_X, XI_X );
  720. fireXInputButtonEvent( i, bJustConnected, XINPUT_GAMEPAD_Y, XI_Y );
  721. }
  722. if ( mXInputStateReset )
  723. mXInputStateReset = false;
  724. }
  725. }