dinput.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. #include "pch.h"
  2. #include "dinput.h"
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // DDWrapers
  6. //
  7. //////////////////////////////////////////////////////////////////////////////
  8. class DIDeviceCaps : public TZeroFillWithSize<DIDEVCAPS> {
  9. public:
  10. };
  11. class DIDeviceInstance : public TZeroFillWithSize<DIDEVICEINSTANCE> {
  12. public:
  13. };
  14. class DIDataFormat : public TZeroFillWithSize<DIDATAFORMAT> {
  15. public:
  16. };
  17. class DIObjectDataFormat : public TZeroFill<DIOBJECTDATAFORMAT> {
  18. public:
  19. };
  20. //////////////////////////////////////////////////////////////////////////////
  21. //
  22. //
  23. //
  24. //////////////////////////////////////////////////////////////////////////////
  25. const DIDATAFORMAT* g_pdfDIMouse;
  26. //////////////////////////////////////////////////////////////////////////////
  27. //
  28. //
  29. //
  30. //////////////////////////////////////////////////////////////////////////////
  31. class DDInputObject : public IObject {
  32. private:
  33. ZString m_strName;
  34. DWORD m_dwType;
  35. GUID m_guidType;
  36. public:
  37. DDInputObject(const ZString& strName, DWORD dwType, const GUID& guidType) :
  38. m_strName(strName),
  39. m_dwType(dwType),
  40. m_guidType(guidType)
  41. {
  42. }
  43. const ZString& GetName() const
  44. {
  45. return m_strName;
  46. }
  47. DWORD GetDWType() const
  48. {
  49. return m_dwType;
  50. }
  51. const GUID& GetGUID() const
  52. {
  53. return m_guidType;
  54. }
  55. };
  56. class ValueDDInputObject : public DDInputObject {
  57. private:
  58. TRef<ModifiableNumber> m_pnumber;
  59. public:
  60. ValueDDInputObject(const ZString& strName, DWORD dwID, const GUID& guidType) :
  61. DDInputObject(strName, dwID, guidType),
  62. m_pnumber(new ModifiableNumber(0))
  63. {
  64. }
  65. ModifiableNumber* GetValue() const
  66. {
  67. return m_pnumber;
  68. }
  69. };
  70. class ButtonDDInputObject : public DDInputObject {
  71. private:
  72. TRef<ModifiableBoolean> m_pbool;
  73. public:
  74. ButtonDDInputObject(const ZString& strName, DWORD dwID, const GUID& guidType) :
  75. DDInputObject(strName, dwID, guidType),
  76. m_pbool(new ModifiableBoolean(false))
  77. {
  78. }
  79. ModifiableBoolean* GetValue() const
  80. {
  81. return m_pbool;
  82. }
  83. };
  84. //////////////////////////////////////////////////////////////////////////////
  85. //
  86. //
  87. //
  88. //////////////////////////////////////////////////////////////////////////////
  89. class MouseInputStreamImpl : public MouseInputStream {
  90. private:
  91. //////////////////////////////////////////////////////////////////////////////
  92. //
  93. //
  94. //
  95. //////////////////////////////////////////////////////////////////////////////
  96. BOOL EnumObjectsCallback(
  97. LPCDIDEVICEOBJECTINSTANCE pddoi
  98. ) {
  99. if (
  100. pddoi->dwType & DIDFT_AXIS
  101. || pddoi->dwType & DIDFT_POV
  102. ) {
  103. int index;
  104. if (pddoi->guidType == GUID_XAxis) {
  105. index = 0;
  106. } else if (pddoi->guidType == GUID_YAxis) {
  107. index = 1;
  108. } else if (pddoi->guidType == GUID_ZAxis) {
  109. index = 2;
  110. } else {
  111. index = -1;
  112. }
  113. ValueDDInputObject* pobject =
  114. new ValueDDInputObject(
  115. pddoi->tszName,
  116. pddoi->dwType,
  117. pddoi->guidType
  118. );
  119. if (index == -1) {
  120. m_vvalueObject.PushEnd(pobject);
  121. } else {
  122. m_vvalueObject.Set(index, pobject);
  123. }
  124. } else if (pddoi->dwType & DIDFT_PSHBUTTON) {
  125. ButtonDDInputObject* pobject =
  126. new ButtonDDInputObject(
  127. pddoi->tszName,
  128. pddoi->dwType,
  129. pddoi->guidType
  130. );
  131. m_vbuttonObject.PushEnd(pobject);
  132. }
  133. return DIENUM_CONTINUE;
  134. }
  135. //////////////////////////////////////////////////////////////////////////////
  136. //
  137. //
  138. //
  139. //////////////////////////////////////////////////////////////////////////////
  140. static BOOL CALLBACK StaticEnumObjectsCallback(
  141. LPCDIDEVICEOBJECTINSTANCE lpddoi,
  142. LPVOID pvRef
  143. ) {
  144. MouseInputStreamImpl* pthis = (MouseInputStreamImpl*)pvRef;
  145. return pthis->EnumObjectsCallback(lpddoi);
  146. }
  147. //////////////////////////////////////////////////////////////////////////////
  148. //
  149. // members
  150. //
  151. //////////////////////////////////////////////////////////////////////////////
  152. TRef<IDirectInputDevice2> m_pdid;
  153. TRef<ButtonEvent::SourceImpl> m_pbuttonEventSource;
  154. DIDeviceCaps m_didc;
  155. DIDeviceInstance m_didi;
  156. TVector<TRef<ValueDDInputObject > > m_vvalueObject;
  157. TVector<TRef<ButtonDDInputObject> > m_vbuttonObject;
  158. Rect m_rect;
  159. Point m_point;
  160. float m_z;
  161. bool m_bBuffered;
  162. bool m_bEnabled;
  163. int m_threshold1;
  164. int m_threshold2;
  165. int m_acceleration;
  166. float m_sensitivity;
  167. public:
  168. //////////////////////////////////////////////////////////////////////////////
  169. //
  170. // Constructor
  171. //
  172. //////////////////////////////////////////////////////////////////////////////
  173. MouseInputStreamImpl(IDirectInputDevice2* pdid, HWND hwnd) :
  174. m_pdid(pdid),
  175. m_rect(0, 0, 0, 0),
  176. m_point(0, 0),
  177. m_vvalueObject(3),
  178. m_bEnabled(false),
  179. m_bBuffered(true),
  180. m_pbuttonEventSource(ButtonEvent::Source::Create())
  181. {
  182. //
  183. // Are we running on NT
  184. //
  185. /*!!!
  186. OSVERSIONINFO osvi = { sizeof(osvi) };
  187. ZVerify(GetVersionEx(&osvi));
  188. if ((VER_PLATFORM_WIN32_NT & osvi.dwPlatformId) != 0) {
  189. m_bBuffered = false;
  190. }
  191. */
  192. //
  193. // Enumerate the buttons and values
  194. //
  195. DDCall(m_pdid->EnumObjects(StaticEnumObjectsCallback, this, DIDFT_ALL));
  196. //
  197. // Setup the device
  198. //
  199. SetupDevice();
  200. //
  201. // We only need mouse input when we are in the foreground
  202. //
  203. DDCall(m_pdid->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND));
  204. //
  205. // Get the mouse acceleration values
  206. //
  207. int pvalue[3];
  208. ZVerify(SystemParametersInfo(SPI_GETMOUSE, 0, pvalue, 0));
  209. m_threshold1 = pvalue[0];
  210. m_threshold2 = pvalue[1];
  211. m_acceleration = pvalue[2];
  212. m_sensitivity = 1.0f;
  213. /* !!! this only works on NT50
  214. int speed;
  215. ZVerify(SystemParametersInfo(SPI_GETMOUSESPEED, 0, &speed, 0));
  216. if (speed <= 2) {
  217. m_sensitivity = float(speed) / 32.0f;
  218. } else if(speed >= 3 && speed <= 10 ) {
  219. m_sensitivity = float(speed-2) / 8.0f;
  220. } else {
  221. m_sensitivity = float(speed-6) / 4.0f;
  222. }
  223. */
  224. }
  225. void SetupDevice()
  226. {
  227. //
  228. // Set the data format
  229. //
  230. DDCall(m_pdid->SetDataFormat(g_pdfDIMouse));
  231. //
  232. // Make some buffer space
  233. //
  234. if (m_bBuffered) {
  235. DIPROPDWORD dipdw;
  236. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  237. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  238. dipdw.diph.dwObj = 0;
  239. dipdw.diph.dwHow = DIPH_DEVICE;
  240. dipdw.dwData = 32;
  241. DDCall(m_pdid->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph));
  242. }
  243. }
  244. //////////////////////////////////////////////////////////////////////////////
  245. //
  246. // Methods
  247. //
  248. //////////////////////////////////////////////////////////////////////////////
  249. void DoClip()
  250. {
  251. if (
  252. m_rect.XMax() > m_rect.XMin()
  253. && m_rect.YMax() > m_rect.YMin()
  254. ) {
  255. if (m_point.X() < m_rect.XMin()) {
  256. m_point.SetX(m_rect.XMin());
  257. }
  258. if (m_point.X() >= m_rect.XMax()) {
  259. m_point.SetX(m_rect.XMax() - 1);
  260. }
  261. if (m_point.Y() < m_rect.YMin()) {
  262. m_point.SetY(m_rect.YMin());
  263. }
  264. if (m_point.Y() >= m_rect.YMax()) {
  265. m_point.SetY(m_rect.YMax() - 1);
  266. }
  267. }
  268. }
  269. float CalculateDelta(int delta)
  270. {
  271. if (abs(delta) > m_threshold1 && m_acceleration >= 1) {
  272. if (abs(delta) > m_threshold2 && m_acceleration >= 2) {
  273. return float(delta) * 4.0f * m_sensitivity;
  274. } else {
  275. return float(delta) * 2.0f * m_sensitivity;
  276. }
  277. }
  278. return float(delta) * m_sensitivity;
  279. }
  280. void DeltaPosition(int& dx, int& dy)
  281. {
  282. if (dx != 0 || dy != 0) {
  283. //ZDebugOutput("MouseMove: (" + ZString(dx) + ", " + ZString(dy) + ")\n");
  284. m_point.SetX(m_point.X() + CalculateDelta(dx));
  285. m_point.SetY(m_point.Y() - CalculateDelta(dy));
  286. dx = 0;
  287. dy = 0;
  288. //
  289. // Clip to the screen rect if required
  290. //
  291. DoClip();
  292. //
  293. // Update outputs
  294. //
  295. m_vvalueObject[0]->GetValue()->SetValue(m_point.X());
  296. m_vvalueObject[1]->GetValue()->SetValue(m_point.Y());
  297. }
  298. }
  299. void DeltaWheel(int dz)
  300. {
  301. if (dz != 0 ) {
  302. //ZDebugOutput("MouseDZ: " + ZString(dz) + "\n");
  303. m_z += float(dz);
  304. if (m_vvalueObject.GetCount() >= 3) {
  305. m_vvalueObject[0]->GetValue()->SetValue(m_z);
  306. }
  307. }
  308. }
  309. void ButtonChanged(int index, bool bDown)
  310. {
  311. //ZDebugOutput("MouseButton: " + ZString(index) + (bDown ? " down" : " up") + "\n");
  312. m_vbuttonObject[index]->GetValue()->SetValue(bDown);
  313. m_pbuttonEventSource->Trigger(ButtonEventData(index, bDown));
  314. }
  315. void UpdateBuffered()
  316. {
  317. //
  318. // Get the data
  319. //
  320. DIDEVICEOBJECTDATA didod;
  321. DWORD count = 1;
  322. int dx = 0;
  323. int dy = 0;
  324. while (count == 1) {
  325. HRESULT hr = m_pdid->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), &didod, &count, 0);
  326. if (FAILED(hr)) {
  327. return;
  328. }
  329. if (count == 1) {
  330. //
  331. // Unpack the data
  332. //
  333. switch (didod.dwOfs) {
  334. case DIMOFS_BUTTON0:
  335. DeltaPosition(dx, dy);
  336. ButtonChanged(0, ((didod.dwData & 0x80) != 0));
  337. break;
  338. case DIMOFS_BUTTON1:
  339. ButtonChanged(1, ((didod.dwData & 0x80) != 0));
  340. break;
  341. case DIMOFS_BUTTON2:
  342. ButtonChanged(2, ((didod.dwData & 0x80) != 0));
  343. break;
  344. case DIMOFS_BUTTON3:
  345. ButtonChanged(3, ((didod.dwData & 0x80) != 0));
  346. break;
  347. case DIMOFS_X:
  348. dx += int(didod.dwData);
  349. break;
  350. case DIMOFS_Y:
  351. dy += int(didod.dwData);
  352. break;
  353. case DIMOFS_Z:
  354. DeltaWheel(int(didod.dwData));
  355. break;
  356. }
  357. }
  358. //
  359. // do the mouse change
  360. //
  361. DeltaPosition(dx, dy);
  362. }
  363. }
  364. void UpdatePolled()
  365. {
  366. //
  367. // Poll the device
  368. //
  369. m_pdid->Poll();
  370. //
  371. // Get the data
  372. //
  373. DIMOUSESTATE dims;
  374. DDCall(m_pdid->GetDeviceState(sizeof(dims), &dims));
  375. //
  376. // Unpack the data
  377. //
  378. int dx = int(dims.lX);
  379. int dy = int(dims.lY);
  380. int dz = int(dims.lZ);
  381. DeltaPosition(dx, dy);
  382. DeltaWheel(dz);
  383. int count = m_vbuttonObject.GetCount();
  384. for (int index = 0; index < count; index++) {
  385. bool bDown = ((dims.rgbButtons[index] & 0x80) != 0);
  386. ModifiableBoolean* pbool = m_vbuttonObject[index]->GetValue();
  387. if (bDown != pbool->GetValue()) {
  388. pbool->SetValue(bDown);
  389. ButtonChanged(index, bDown);
  390. }
  391. }
  392. }
  393. void Update()
  394. {
  395. //ZDebugOutput("Mouse Update\n");
  396. if (m_bEnabled) {
  397. //ZDebugOutput("Mouse Enabled\n");
  398. HRESULT hr = m_pdid->Acquire();
  399. //
  400. // We have to handle the case where another app has captured the
  401. // mouse, since we may have been switched out.
  402. //
  403. if (hr != DIERR_OTHERAPPHASPRIO) {
  404. DDCall(hr);
  405. if (m_bBuffered) {
  406. UpdateBuffered();
  407. } else {
  408. UpdatePolled();
  409. }
  410. }
  411. }
  412. }
  413. //////////////////////////////////////////////////////////////////////////////
  414. //
  415. // MouseInputStream
  416. //
  417. //////////////////////////////////////////////////////////////////////////////
  418. void SetClipRect(const Rect& rect)
  419. {
  420. m_rect = rect;
  421. DoClip();
  422. }
  423. void SetPosition(const Point& point)
  424. {
  425. m_point = point;
  426. DoClip();
  427. }
  428. void SetWheelPosition(float pos)
  429. {
  430. m_z = pos;
  431. }
  432. const Point& GetPosition()
  433. {
  434. return m_point;
  435. }
  436. void SetEnabled(bool bEnabled)
  437. {
  438. if (m_bEnabled != bEnabled) {
  439. m_bEnabled = bEnabled;
  440. if (m_bEnabled) {
  441. //DDCall(m_pdid->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND));
  442. } else {
  443. //DDCall(m_pdid->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND));
  444. DDCall(m_pdid->Unacquire());
  445. }
  446. }
  447. }
  448. //////////////////////////////////////////////////////////////////////////////
  449. //
  450. // InputStream
  451. //
  452. //////////////////////////////////////////////////////////////////////////////
  453. int GetValueCount()
  454. {
  455. return m_vvalueObject.GetCount();
  456. }
  457. int GetButtonCount()
  458. {
  459. return m_vbuttonObject.GetCount();
  460. }
  461. Boolean* IsDown(int id)
  462. {
  463. if (id < m_vbuttonObject.GetCount()) {
  464. return m_vbuttonObject[id]->GetValue();
  465. } else {
  466. return NULL;
  467. }
  468. }
  469. Number* GetValue(int id)
  470. {
  471. if (id < m_vvalueObject.GetCount()) {
  472. return m_vvalueObject[id]->GetValue();
  473. } else {
  474. return NULL;
  475. }
  476. }
  477. ButtonEvent::Source* GetEventSource()
  478. {
  479. return m_pbuttonEventSource;
  480. }
  481. };
  482. //////////////////////////////////////////////////////////////////////////////
  483. //
  484. //
  485. //
  486. //////////////////////////////////////////////////////////////////////////////
  487. class JoystickInputStreamImpl : public JoystickInputStream {
  488. private:
  489. TRef<IDirectInputDevice2> m_pdid;
  490. DIDeviceCaps m_didc;
  491. DIDeviceInstance m_didi;
  492. TVector<TRef<ValueDDInputObject > > m_vvalueObject;
  493. TVector<TRef<ButtonDDInputObject> > m_vbuttonObject;
  494. BYTE* m_pbyteData;
  495. DWORD m_sizeData;
  496. bool m_bFocus;
  497. TRef<IDirectInputEffect> m_peffectBounce;
  498. TRef<IDirectInputEffect> m_peffectFire;
  499. TRef<IDirectInputEffect> m_peffectExplode;
  500. public:
  501. //////////////////////////////////////////////////////////////////////////////
  502. //
  503. //
  504. //
  505. //////////////////////////////////////////////////////////////////////////////
  506. BOOL EnumObjectsCallback(
  507. LPCDIDEVICEOBJECTINSTANCE pddoi
  508. ) {
  509. if (
  510. pddoi->dwType & DIDFT_AXIS
  511. || pddoi->dwType & DIDFT_POV
  512. ) {
  513. int index;
  514. if (pddoi->guidType == GUID_XAxis ) {
  515. index = 0;
  516. } else if (pddoi->guidType == GUID_YAxis ) {
  517. index = 1;
  518. } else if (pddoi->guidType == GUID_Slider) {
  519. index = 2;
  520. } else if (pddoi->guidType == GUID_RzAxis) {
  521. index = 3;
  522. } else if (pddoi->guidType == GUID_POV ) {
  523. index = 4;
  524. } else {
  525. index = -1;
  526. }
  527. ValueDDInputObject* pobject =
  528. new ValueDDInputObject(
  529. pddoi->tszName,
  530. pddoi->dwType,
  531. pddoi->guidType
  532. );
  533. if (index == -1) {
  534. m_vvalueObject.PushEnd(pobject);
  535. } else {
  536. m_vvalueObject.Set(index, pobject);
  537. }
  538. } else if (pddoi->dwType & DIDFT_PSHBUTTON) {
  539. ButtonDDInputObject* pobject =
  540. new ButtonDDInputObject(
  541. pddoi->tszName,
  542. pddoi->dwType,
  543. pddoi->guidType
  544. );
  545. m_vbuttonObject.PushEnd(pobject);
  546. }
  547. return DIENUM_CONTINUE;
  548. }
  549. //////////////////////////////////////////////////////////////////////////////
  550. //
  551. //
  552. //
  553. //////////////////////////////////////////////////////////////////////////////
  554. static BOOL CALLBACK StaticEnumObjectsCallback(
  555. LPCDIDEVICEOBJECTINSTANCE lpddoi,
  556. LPVOID pvRef
  557. ) {
  558. JoystickInputStreamImpl* pthis = (JoystickInputStreamImpl*)pvRef;
  559. return pthis->EnumObjectsCallback(lpddoi);
  560. }
  561. //////////////////////////////////////////////////////////////////////////////
  562. //
  563. // Constructor
  564. //
  565. //////////////////////////////////////////////////////////////////////////////
  566. JoystickInputStreamImpl(IDirectInputDevice2* pdid, HWND hwnd) :
  567. m_pdid(pdid),
  568. m_bFocus(false),
  569. m_vvalueObject(5)
  570. {
  571. DDCall(m_pdid->GetCapabilities(&m_didc));
  572. DDCall(m_pdid->GetDeviceInfo(&m_didi));
  573. //
  574. // Enumerate the buttons and values
  575. //
  576. DDCall(m_pdid->EnumObjects(StaticEnumObjectsCallback, this, DIDFT_ALL));
  577. //
  578. // Remove any holes in the value vector
  579. //
  580. int index;
  581. int countValues = m_vvalueObject.GetCount();
  582. index = 0;
  583. while (index < countValues) {
  584. if (m_vvalueObject[index] == NULL) {
  585. if (index != countValues - 1) {
  586. m_vvalueObject.Set(index, m_vvalueObject[countValues - 1]);
  587. }
  588. countValues--;
  589. m_vvalueObject.SetCount(countValues);
  590. } else {
  591. index++;
  592. }
  593. }
  594. //
  595. // Build the data format
  596. //
  597. int countButtons = m_vbuttonObject.GetCount();
  598. m_sizeData = NextMultipleOf(4, countValues * 4 + countButtons * 1);
  599. DIDataFormat didf;
  600. didf.dwObjSize = sizeof(DIOBJECTDATAFORMAT);
  601. didf.dwFlags = 0;
  602. didf.dwDataSize = m_sizeData;
  603. didf.dwNumObjs = countValues + countButtons;
  604. DIObjectDataFormat* pdiodf = new DIObjectDataFormat[didf.dwNumObjs];
  605. didf.rgodf = pdiodf;
  606. for (index = 0; index < countValues; index++) {
  607. ValueDDInputObject* pobject = m_vvalueObject[index];
  608. DIOBJECTDATAFORMAT& diodf = didf.rgodf[index];
  609. diodf.pguid = (GUID*)&(pobject->GetGUID());
  610. diodf.dwOfs = index * 4;
  611. diodf.dwType = pobject->GetDWType();
  612. diodf.dwFlags = DIDOI_ASPECTPOSITION;
  613. }
  614. for (index = 0; index < countButtons; index++) {
  615. ButtonDDInputObject* pobject = m_vbuttonObject[index];
  616. DIOBJECTDATAFORMAT& diodf = didf.rgodf[countValues + index];
  617. diodf.pguid = (GUID*)&(pobject->GetGUID());
  618. diodf.dwOfs = countValues * 4 + index;
  619. diodf.dwType = pobject->GetDWType();
  620. diodf.dwFlags = DIDOI_ASPECTPOSITION;
  621. }
  622. DDCall(m_pdid->SetDataFormat(&didf));
  623. delete pdiodf;
  624. //
  625. // Allocate a data receptical
  626. //
  627. m_pbyteData = new BYTE[m_sizeData];
  628. //
  629. // We only need joystick input when we are in the foreground
  630. //
  631. DDCall(m_pdid->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND));
  632. //
  633. // Set ranges
  634. //
  635. SetRanges();
  636. }
  637. ~JoystickInputStreamImpl()
  638. {
  639. delete m_pbyteData;
  640. }
  641. //////////////////////////////////////////////////////////////////////////////
  642. //
  643. // Implementation methods
  644. //
  645. //////////////////////////////////////////////////////////////////////////////
  646. void SetRanges()
  647. {
  648. m_pdid->Unacquire();
  649. int countValues = m_vvalueObject.GetCount();
  650. for (int index = 0; index < countValues; index++) {
  651. if (m_vvalueObject[index] != NULL) {
  652. if ((m_vvalueObject[index]->GetDWType() & DIDFT_POV) == 0) {
  653. DIPROPRANGE dipr;
  654. dipr.diph.dwSize = sizeof(dipr);
  655. dipr.diph.dwHeaderSize = sizeof(dipr.diph);
  656. dipr.diph.dwHow = DIPH_BYID;
  657. dipr.diph.dwObj = m_vvalueObject[index]->GetDWType();
  658. dipr.lMin = -100000;
  659. dipr.lMax = 100000;
  660. DDCall(m_pdid->SetProperty(DIPROP_RANGE, &dipr.diph));
  661. }
  662. //
  663. //
  664. //
  665. DIPROPDWORD dipdw;
  666. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  667. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  668. dipdw.diph.dwObj = 0;
  669. dipdw.diph.dwHow = DIPH_DEVICE;
  670. DDCall(m_pdid->GetProperty(DIPROP_BUFFERSIZE, &dipdw.diph));
  671. }
  672. }
  673. }
  674. void Update()
  675. {
  676. HRESULT hr = m_pdid->Acquire();
  677. //
  678. // We have to handle the case where another app has captured the
  679. // joystick, since we may be switched out.
  680. // Or the joystick may have been unplugged.
  681. //
  682. if (SUCCEEDED(hr)) {
  683. //
  684. // Poll the device
  685. //
  686. m_pdid->Poll();
  687. //
  688. // The MS Gamepad will return error from this funtion if the mode button is pressed
  689. // Don't assert in that case. Just don't unpack the data.
  690. //
  691. hr = m_pdid->GetDeviceState(m_sizeData, m_pbyteData);
  692. if (SUCCEEDED(hr)) {
  693. //
  694. // Unpack the data
  695. //
  696. int countValues = m_vvalueObject.GetCount();
  697. int countButtons = m_vbuttonObject.GetCount();
  698. for (int index = 0; index < countValues; index++) {
  699. if ((m_vvalueObject[index]->GetDWType() & DIDFT_POV) != 0) {
  700. int ivalue = ((int*)m_pbyteData)[index];
  701. float value;
  702. if (ivalue == -1) {
  703. value = -2;
  704. } else if (ivalue > 18000) {
  705. value = float(ivalue - 36000) / 18000;
  706. } else {
  707. value = float(ivalue) / 18000;
  708. }
  709. //ZDebugOutput("RawHat: " + ZString(ivalue) + ", " + ZString(value) + "\n");
  710. m_vvalueObject[index]->GetValue()->SetValue(value);
  711. } else {
  712. float value = float(((int*)m_pbyteData)[index]) / 100000;
  713. value = bound(value, -1.0f, 1.0f);
  714. m_vvalueObject[index]->GetValue()->SetValue(value);
  715. }
  716. }
  717. for (index = 0; index < countButtons; index++) {
  718. m_vbuttonObject[index]->GetValue()->SetValue(
  719. m_pbyteData[countValues * 4 + index] != 0
  720. );
  721. }
  722. }
  723. }
  724. }
  725. void SetFocus(bool bFocus)
  726. {
  727. if (m_bFocus != bFocus) {
  728. m_bFocus = bFocus;
  729. if (m_bFocus) {
  730. //
  731. // Aquire the joystick
  732. //
  733. // !!! this doesn't work. I guess that at the time when this gets called
  734. // DInput doesn't think we are foreground yet.
  735. // DDCall(m_pdid->Acquire());
  736. } else {
  737. // !!! this gets called automatically
  738. // DDCall(m_pdid->Unacquire());
  739. }
  740. }
  741. }
  742. //////////////////////////////////////////////////////////////////////////////
  743. //
  744. // Force Feedback
  745. //
  746. //////////////////////////////////////////////////////////////////////////////
  747. static BOOL CALLBACK EnumEffectTypeProc(LPCDIEFFECTINFO pei, LPVOID pv)
  748. {
  749. GUID *pguidEffect = NULL;
  750. // report back the guid of the effect we enumerated
  751. if (pv) {
  752. pguidEffect = (GUID *)pv;
  753. *pguidEffect = pei->guid;
  754. }
  755. // BUGBUG - look at this some more....
  756. return DIENUM_STOP;
  757. }
  758. void CreateEffects()
  759. {
  760. GUID guidEffect;
  761. DIEFFECT diEffect;
  762. DIENVELOPE diEnvelope;
  763. DWORD rgdwAxes[2];
  764. LONG rglDirections[2];
  765. DICONSTANTFORCE dicf;
  766. DIPERIODIC dipf;
  767. //
  768. // initialize DIEFFECT and DIENVELOPE structures
  769. //
  770. ZeroMemory(&diEffect, sizeof(DIEFFECT));
  771. ZeroMemory(&diEnvelope, sizeof(DIENVELOPE));
  772. //
  773. // these fields are the same for all effects we will be creating
  774. //
  775. diEffect.dwSize = sizeof(DIEFFECT);
  776. diEffect.dwSamplePeriod = 0; // use default sample period
  777. diEffect.dwTriggerButton = DIEB_NOTRIGGER;
  778. diEffect.dwTriggerRepeatInterval = 0;
  779. diEffect.rgdwAxes = rgdwAxes;
  780. diEffect.rglDirection = rglDirections;
  781. diEffect.dwGain = 7500; // todo: gain selected by user
  782. //
  783. // both the "bounce" and "fire" effects will be based on the first
  784. // constant force effect enumerated
  785. //
  786. //
  787. // don't check for errors. DInput sometimes return error, but then works anyway.
  788. //
  789. //DDCall(
  790. m_pdid->EnumEffects(
  791. (LPDIENUMEFFECTSCALLBACK)EnumEffectTypeProc,
  792. &guidEffect,
  793. DIEFT_CONSTANTFORCE
  794. //)
  795. );
  796. //
  797. // Create the bounce effect
  798. //
  799. dicf.lMagnitude = 10000;
  800. rgdwAxes[0] = DIJOFS_X;
  801. rgdwAxes[1] = DIJOFS_Y;
  802. rglDirections[0] = 0;
  803. rglDirections[1] = 0;
  804. diEffect.dwFlags = DIEFF_OBJECTOFFSETS | DIEFF_POLAR;
  805. diEffect.dwDuration = 200000;
  806. diEffect.cAxes = 2;
  807. diEffect.lpEnvelope = NULL;
  808. diEffect.cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
  809. diEffect.lpvTypeSpecificParams = &dicf;
  810. //DDCall(
  811. m_pdid->CreateEffect(
  812. guidEffect,
  813. &diEffect,
  814. &m_peffectBounce,
  815. NULL
  816. //)
  817. );
  818. //
  819. // Create the fire effect
  820. //
  821. dicf.lMagnitude = 10000;
  822. rgdwAxes[0] = DIJOFS_Y;
  823. rglDirections[0] = 1;
  824. diEffect.dwFlags = DIEFF_OBJECTOFFSETS | DIEFF_CARTESIAN;
  825. diEffect.dwDuration = 20000;
  826. diEffect.cAxes = 1;
  827. diEffect.lpEnvelope = NULL;
  828. diEffect.cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
  829. diEffect.lpvTypeSpecificParams = &dicf;
  830. //DDCall(
  831. m_pdid->CreateEffect(
  832. guidEffect,
  833. &diEffect,
  834. &m_peffectFire,
  835. NULL
  836. //)
  837. );
  838. //
  839. // the "explode" effect will be based on the first
  840. // periodic effect enumerated
  841. //
  842. //DDCall(
  843. m_pdid->EnumEffects(
  844. (LPDIENUMEFFECTSCALLBACK)EnumEffectTypeProc,
  845. &guidEffect,
  846. DIEFT_PERIODIC
  847. //)
  848. );
  849. //
  850. // Create the explode effect.
  851. // We want to shape the explode effect so that it starts
  852. // at it's peak and then fades out
  853. //
  854. diEnvelope.dwSize = sizeof(DIENVELOPE);
  855. diEnvelope.dwAttackLevel = 0;
  856. diEnvelope.dwAttackTime = 0;
  857. diEnvelope.dwFadeLevel = 0;
  858. diEnvelope.dwFadeTime = 1000000;
  859. dipf.dwMagnitude = 10000;
  860. dipf.lOffset = 0;
  861. dipf.dwPhase = 0;
  862. dipf.dwPeriod = 100000;
  863. rgdwAxes[0] = DIJOFS_X;
  864. rglDirections[0] = 0;
  865. diEffect.dwFlags = DIEFF_OBJECTOFFSETS | DIEFF_CARTESIAN;
  866. diEffect.dwDuration = 1000000;
  867. diEffect.cAxes = 1;
  868. diEffect.lpEnvelope = &diEnvelope;
  869. diEffect.cbTypeSpecificParams = sizeof(DIPERIODIC);
  870. diEffect.lpvTypeSpecificParams = &dipf;
  871. //DDCall(
  872. m_pdid->CreateEffect(
  873. guidEffect,
  874. &diEffect,
  875. &m_peffectExplode,
  876. NULL
  877. //)
  878. );
  879. }
  880. void PlayFFEffect(short effectID, LONG lDirection)
  881. {
  882. //
  883. // !!! Don't check for errors here. Sometimes these functions will return error
  884. //
  885. switch (effectID) {
  886. case 0:
  887. if (m_peffectFire) {
  888. m_peffectFire->Start(1, 0);
  889. }
  890. break;
  891. case 1:
  892. if (m_peffectBounce) {
  893. DIEFFECT diEffect;
  894. LONG rglDirections[2] = { 0, 0 };
  895. ZeroMemory(&diEffect, sizeof(DIEFFECT));
  896. diEffect.dwSize = sizeof(DIEFFECT);
  897. rglDirections[0] = lDirection * 100;
  898. diEffect.dwFlags = DIEFF_OBJECTOFFSETS | DIEFF_POLAR;
  899. diEffect.cAxes = 2;
  900. diEffect.rglDirection = rglDirections;
  901. m_peffectBounce->SetParameters(&diEffect, DIEP_DIRECTION);
  902. m_peffectBounce->Start(1, 0);
  903. }
  904. break;
  905. case 2:
  906. if (m_peffectExplode) {
  907. m_peffectExplode->Start(1, 0);
  908. }
  909. break;
  910. }
  911. }
  912. //////////////////////////////////////////////////////////////////////////////
  913. //
  914. // JoystickInputStream methods
  915. //
  916. //////////////////////////////////////////////////////////////////////////////
  917. bool HasForceFeedback()
  918. {
  919. return (m_didc.dwFlags & DIDC_FORCEFEEDBACK) != 0;
  920. }
  921. ZString GetShortDescription(int index)
  922. {
  923. return "Joy " + ZString(index);
  924. }
  925. ZString GetDescription()
  926. {
  927. return m_didi.tszInstanceName;
  928. }
  929. ZString GetValueDescription(int id)
  930. {
  931. return m_vvalueObject[id]->GetName();
  932. }
  933. //////////////////////////////////////////////////////////////////////////////
  934. //
  935. // InputStream methods
  936. //
  937. //////////////////////////////////////////////////////////////////////////////
  938. int GetValueCount()
  939. {
  940. return m_vvalueObject.GetCount();
  941. }
  942. int GetButtonCount()
  943. {
  944. return m_vbuttonObject.GetCount();
  945. }
  946. Boolean* IsDown(int id)
  947. {
  948. if (id < m_vbuttonObject.GetCount()) {
  949. return m_vbuttonObject[id]->GetValue();
  950. } else {
  951. return NULL;
  952. }
  953. }
  954. Number* GetValue(int id)
  955. {
  956. if (id < m_vvalueObject.GetCount()) {
  957. return m_vvalueObject[id]->GetValue();
  958. } else {
  959. return NULL;
  960. }
  961. }
  962. ButtonEvent::Source* GetEventSource()
  963. {
  964. ZUnimplemented();
  965. return NULL;
  966. }
  967. };
  968. //////////////////////////////////////////////////////////////////////////////
  969. //
  970. //
  971. //
  972. //////////////////////////////////////////////////////////////////////////////
  973. class InputEngineImpl : public InputEngine {
  974. private:
  975. //////////////////////////////////////////////////////////////////////////////
  976. //
  977. //
  978. //
  979. //////////////////////////////////////////////////////////////////////////////
  980. bool EnumDeviceCallback(LPDIDEVICEINSTANCE pdidi)
  981. {
  982. TRef<IDirectInputDevice> pdid;
  983. TRef<IDirectInputDevice2> pdid2;
  984. DDCall(m_pdi->CreateDevice( pdidi->guidInstance, &pdid, NULL));
  985. DDCall(pdid->QueryInterface(IID_IDirectInputDevice2, (void**)&pdid2));
  986. switch (pdidi->dwDevType & 0xff) {
  987. case DIDEVTYPE_MOUSE:
  988. {
  989. if (m_pmouseInputStream == NULL) {
  990. m_pmouseInputStream = new MouseInputStreamImpl(pdid2, m_hwnd);
  991. }
  992. }
  993. break;
  994. case DIDEVTYPE_JOYSTICK:
  995. {
  996. TRef<JoystickInputStreamImpl> pjoystickInputStream =
  997. new JoystickInputStreamImpl(pdid2, m_hwnd);
  998. m_vjoystickInputStream.PushEnd(pjoystickInputStream);
  999. }
  1000. break;
  1001. }
  1002. return DIENUM_CONTINUE;
  1003. }
  1004. //////////////////////////////////////////////////////////////////////////////
  1005. //
  1006. //
  1007. //
  1008. //////////////////////////////////////////////////////////////////////////////
  1009. static BOOL CALLBACK StaticEnumDeviceCallback(LPDIDEVICEINSTANCE pdidi, LPVOID pv)
  1010. {
  1011. InputEngineImpl* pthis = (InputEngineImpl*)pv;
  1012. return pthis->EnumDeviceCallback(pdidi);
  1013. }
  1014. //////////////////////////////////////////////////////////////////////////////
  1015. //
  1016. // data members
  1017. //
  1018. //////////////////////////////////////////////////////////////////////////////
  1019. HWND m_hwnd;
  1020. bool m_bFocus;
  1021. TRef<IDirectInput> m_pdi;
  1022. TVector<TRef<JoystickInputStreamImpl> > m_vjoystickInputStream;
  1023. TRef<MouseInputStreamImpl> m_pmouseInputStream;
  1024. HINSTANCE m_hdinput;
  1025. public:
  1026. //////////////////////////////////////////////////////////////////////////////
  1027. //
  1028. // Constructor
  1029. //
  1030. //////////////////////////////////////////////////////////////////////////////
  1031. typedef HRESULT (WINAPI *PFNDirectInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA *ppDI, LPUNKNOWN punkOuter);
  1032. InputEngineImpl(HWND hwnd) :
  1033. m_hwnd(hwnd),
  1034. m_bFocus(false)
  1035. {
  1036. //
  1037. // Create the direct input object
  1038. //
  1039. #ifdef Dynamic_DInput
  1040. m_hdinput = ::LoadLibrary("dinput.dll");
  1041. ZAssert(m_hdinput != NULL);
  1042. PFNDirectInputCreate pfn = (PFNDirectInputCreate)::GetProcAddress(m_hdinput, "DirectInputCreateA");
  1043. ZAssert(pfn != NULL);
  1044. DDCall(pfn(
  1045. GetModuleHandle(NULL),
  1046. DIRECTINPUT_VERSION,
  1047. &m_pdi,
  1048. NULL
  1049. ));
  1050. //
  1051. // grab the address of a few dinput globals
  1052. //
  1053. g_pdfDIMouse = (DIDATAFORMAT*)::GetProcAddress(m_hdinput, "c_dfDIMouse");
  1054. ZAssert(g_pdfDIMouse != NULL);
  1055. #else
  1056. DDCall(DirectInputCreate(
  1057. GetModuleHandle(NULL),
  1058. DIRECTINPUT_VERSION,
  1059. &m_pdi,
  1060. NULL
  1061. ));
  1062. g_pdfDIMouse = &c_dfDIMouse;
  1063. #endif
  1064. //
  1065. // If we failed then exit the app
  1066. //
  1067. if (m_pdi == NULL) {
  1068. ::MessageBox(NULL, "Error initializing DirectInput. Check your installation", "Error", MB_OK);
  1069. _exit(0);
  1070. }
  1071. //
  1072. // Enumerate the devices
  1073. //
  1074. EnumerateJoysticks();
  1075. }
  1076. //////////////////////////////////////////////////////////////////////////////
  1077. //
  1078. // InputEngine methods
  1079. //
  1080. //////////////////////////////////////////////////////////////////////////////
  1081. void EnumerateJoysticks()
  1082. {
  1083. //
  1084. // Free up the old devices
  1085. //
  1086. m_vjoystickInputStream.SetEmpty();
  1087. //
  1088. // Enumerate all of the devices
  1089. //
  1090. DDCall(m_pdi->EnumDevices(
  1091. 0,//DIDEVTYPE_JOYSTICK,
  1092. (LPDIENUMDEVICESCALLBACK)StaticEnumDeviceCallback,
  1093. this,
  1094. DIEDFL_ATTACHEDONLY
  1095. ));
  1096. }
  1097. int GetJoystickCount()
  1098. {
  1099. return m_vjoystickInputStream.GetCount();
  1100. }
  1101. JoystickInputStream* GetJoystick(int index)
  1102. {
  1103. if (
  1104. index >= 0
  1105. && index < m_vjoystickInputStream.GetCount()
  1106. ) {
  1107. return m_vjoystickInputStream[index];
  1108. } else {
  1109. return NULL;
  1110. }
  1111. }
  1112. MouseInputStream* GetMouse()
  1113. {
  1114. return m_pmouseInputStream;
  1115. }
  1116. void Update()
  1117. {
  1118. if (m_bFocus) {
  1119. m_pmouseInputStream->Update();
  1120. int count = m_vjoystickInputStream.GetCount();
  1121. for(int index = 0; index < count; index++) {
  1122. m_vjoystickInputStream[index]->Update();
  1123. }
  1124. }
  1125. }
  1126. void SetFocus(bool bFocus)
  1127. {
  1128. if (m_bFocus != bFocus) {
  1129. m_bFocus = bFocus;
  1130. int count = m_vjoystickInputStream.GetCount();
  1131. for(int index = 0; index < count; index++) {
  1132. m_vjoystickInputStream[index]->SetFocus(m_bFocus);
  1133. }
  1134. }
  1135. }
  1136. };
  1137. TRef<InputEngine> CreateInputEngine(HWND hwnd)
  1138. {
  1139. return new InputEngineImpl(hwnd);
  1140. }