winDirectInput.cc 29 KB

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