JoystickDevice.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #ifndef _XBOX
  2. #include "JoystickDevice.h"
  3. #include "..\..\common_h\Render.h"
  4. #include "..\..\common_h\controls.h"
  5. const long AxisMaxValue = 10000;
  6. JoystickDevice::JoystickDevice(IDirectInput8A* di, const DIDEVICEINSTANCEA& deviceInst, const ControlsIniParser& parser) :
  7. m_Device(NULL),
  8. m_Controls(__FILE__, __LINE__)
  9. {
  10. Assert(di);
  11. di->CreateDevice(deviceInst.guidInstance, &m_Device, NULL);
  12. Assert(m_Device);
  13. HWND hWnd = (HWND)api->Storage().GetLong("system.hWnd");
  14. // m_Device->SetCooperativeLevel(hWnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE);
  15. m_Device->SetCooperativeLevel(hWnd,DISCL_FOREGROUND|DISCL_EXCLUSIVE);
  16. DIDEVCAPS deviceCaps = {0};
  17. deviceCaps.dwSize = sizeof(deviceCaps);
  18. m_Device->GetCapabilities(&deviceCaps);
  19. // ставим предельное число, т.к. трактуем оси иначе чем DirectInput
  20. // (24 обычных оси + 8 осей для слайдеров)
  21. m_NumAxes = 24+8;
  22. m_NumButtons = deviceCaps.dwButtons;
  23. m_NumPOVs = deviceCaps.dwPOVs;
  24. // Установить мин/макс значения для осей
  25. DIPROPRANGE diprg;
  26. diprg.diph.dwSize = sizeof(DIPROPRANGE);
  27. diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  28. diprg.diph.dwHow = DIPH_BYID;
  29. for (unsigned int i = 0; i < m_NumAxes; ++i)
  30. {
  31. diprg.diph.dwObj = DIDFT_ABSAXIS | (i << 8);
  32. diprg.lMin = -AxisMaxValue;
  33. diprg.lMax = AxisMaxValue;
  34. m_Device->SetProperty( DIPROP_RANGE, &diprg.diph );
  35. }
  36. HRESULT hr = m_Device->SetDataFormat(&c_dfDIJoystick2);
  37. hr=m_Device->Acquire();
  38. if (parser.GetMappingTable(deviceInst.tszProductName).items.Size() > 0)
  39. InitControls(parser.GetMappingTable(deviceInst.tszProductName));
  40. else
  41. InitControls(parser.GetMappingTable("Default"));
  42. DIPROPDWORD dip;
  43. dip.diph.dwSize = sizeof(DIPROPDWORD);
  44. dip.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  45. dip.diph.dwObj = 0;
  46. dip.diph.dwHow = DIPH_DEVICE;
  47. dip.dwData = FALSE;
  48. m_Device->SetProperty(DIPROP_AUTOCENTER,&dip.diph);
  49. lef = NULL;
  50. ref = NULL;
  51. DWORD rgdwAxes [2] = {DIJOFS_X,DIJOFS_Y};
  52. LONG rglDirection[2] = {0,0};
  53. DIRAMPFORCE cf = {0,0};
  54. DIEFFECT eff; ZeroMemory(&eff,sizeof(eff));
  55. eff.dwSize = sizeof(DIEFFECT);
  56. eff.dwFlags = DIEFF_CARTESIAN|DIEFF_OBJECTOFFSETS;
  57. eff.dwDuration = INFINITE;
  58. eff.dwSamplePeriod = 0;
  59. eff.dwGain = DI_FFNOMINALMAX;
  60. eff.dwTriggerButton = DIEB_NOTRIGGER;
  61. eff.dwTriggerRepeatInterval = 0;
  62. // eff.cAxes = axesN;
  63. eff.cAxes = 2;
  64. eff.rgdwAxes = rgdwAxes;
  65. eff.rglDirection = rglDirection;
  66. eff.lpEnvelope = 0;
  67. eff.cbTypeSpecificParams = sizeof(DIRAMPFORCE);
  68. eff.lpvTypeSpecificParams = &cf;
  69. eff.dwStartDelay = 0;
  70. eff.dwDuration = INFINITE;
  71. m_Device->CreateEffect(GUID_RampForce,&eff,&lef,NULL);
  72. m_Device->CreateEffect(GUID_RampForce,&eff,&ref,NULL);
  73. if( lef ) lef->Start(1,0);
  74. if( ref ) ref->Start(1,0);
  75. }
  76. JoystickDevice::~JoystickDevice()
  77. {
  78. if (m_Device)
  79. {
  80. m_Device->Unacquire();
  81. m_Device->Release();
  82. m_Device = NULL;
  83. }
  84. // if( lef ) lef->Release();
  85. // if( ref ) ref->Release();
  86. }
  87. unsigned int JoystickDevice::GetAxisDataOffset(unsigned int index)
  88. {
  89. DIJOYSTATE2 state;
  90. unsigned int Offsets[] =
  91. {
  92. (char*)&(state.lX) - (char*)&state,
  93. (char*)&(state.lY) - (char*)&state,
  94. (char*)&(state.lZ) - (char*)&state,
  95. (char*)&(state.lRx) - (char*)&state,
  96. (char*)&(state.lRy) - (char*)&state,
  97. (char*)&(state.lRz) - (char*)&state,
  98. (char*)&(state.rglSlider[0]) - (char*)&state,
  99. (char*)&(state.rglSlider[1]) - (char*)&state,
  100. (char*)&(state.lVX) - (char*)&state,
  101. (char*)&(state.lVY) - (char*)&state,
  102. (char*)&(state.lVZ) - (char*)&state,
  103. (char*)&(state.lVRx) - (char*)&state,
  104. (char*)&(state.lVRy) - (char*)&state,
  105. (char*)&(state.lVRz) - (char*)&state,
  106. (char*)&(state.rglVSlider[0]) - (char*)&state,
  107. (char*)&(state.rglVSlider[1]) - (char*)&state,
  108. (char*)&(state.lAX) - (char*)&state,
  109. (char*)&(state.lAY) - (char*)&state,
  110. (char*)&(state.lAZ) - (char*)&state,
  111. (char*)&(state.lARx) - (char*)&state,
  112. (char*)&(state.lARy) - (char*)&state,
  113. (char*)&(state.lARz) - (char*)&state,
  114. (char*)&(state.rglASlider[0]) - (char*)&state,
  115. (char*)&(state.rglASlider[1]) - (char*)&state,
  116. (char*)&(state.lFX) - (char*)&state,
  117. (char*)&(state.lFY) - (char*)&state,
  118. (char*)&(state.lFZ) - (char*)&state,
  119. (char*)&(state.lFRx) - (char*)&state,
  120. (char*)&(state.lFRy) - (char*)&state,
  121. (char*)&(state.lFRz) - (char*)&state,
  122. (char*)&(state.rglFSlider[0]) - (char*)&state,
  123. (char*)&(state.rglFSlider[1]) - (char*)&state,
  124. };
  125. return Offsets[index];
  126. }
  127. float JoystickDevice::GetButtonData(unsigned int dataOffset)
  128. {
  129. unsigned char value = *(unsigned char*)(((char*)&m_Data) + dataOffset);
  130. return (float)((value & 0x80) >> 7);
  131. }
  132. float JoystickDevice::GetAxisData(JoystickDevice::Control::Type axisType, unsigned int dataOffset)
  133. {
  134. long value = *(long*)(((char*)&m_Data) + dataOffset);
  135. if (abs(value) < AxisMaxValue/10)
  136. return 0.0f;
  137. return value / (float)AxisMaxValue;
  138. }
  139. float JoystickDevice::GetPOVData(unsigned int dataOffset, bool isPovH)
  140. {
  141. long value = *(long*)(((char*)&m_Data) + dataOffset);
  142. if ( (LOWORD(value) == 0xFFFF) ) return 0.0f;
  143. if (isPovH)
  144. {
  145. float ret[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f };
  146. return ret[value / 4500];
  147. }
  148. else
  149. {
  150. float ret[] = { 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f, 0.0f, 1.0f };
  151. return ret[value / 4500];
  152. }
  153. }
  154. static const char* GetMappedName(const char* hwName, const ControlsIniParser::Table& table, bool& inverted)
  155. {
  156. string s1, s2;
  157. for (unsigned int i = 0; i < table.items.Size(); ++i)
  158. {
  159. s1 = table.items[i].hwName;
  160. s2 = hwName;
  161. s1.Upper();
  162. s2.Upper();
  163. if (s1.FindSubStr(s2) != -1)
  164. {
  165. inverted = s1[0] == '-';
  166. return table.items[i].logName.c_str();
  167. }
  168. }
  169. inverted = false;
  170. return hwName;
  171. }
  172. void JoystickDevice::InitControls( const ControlsIniParser::Table& table )
  173. {
  174. Control ctrl;
  175. ctrl.value = 0.0f;
  176. ctrl.type = Control::Button;
  177. DIJOYSTATE2 state;
  178. for (unsigned int i = 0; i < m_NumButtons; ++i)
  179. {
  180. ctrl.name.Format("hw_b%d", i+1);
  181. ctrl.name = GetMappedName(ctrl.name, table, ctrl.inverted);
  182. ctrl.dataOffset = (char*)&(state.rgbButtons[i]) - (char*)&state;
  183. m_Controls.Add(ctrl);
  184. }
  185. for (unsigned int i = 0; i < m_NumAxes; ++i)
  186. {
  187. ctrl.name.Format("hw_Axis%d", i+1);
  188. ctrl.name = GetMappedName(ctrl.name, table, ctrl.inverted);
  189. ctrl.dataOffset = GetAxisDataOffset(i);
  190. ctrl.type = Control::Axis;
  191. m_Controls.Add(ctrl);
  192. }
  193. for (unsigned int i = 0; i < m_NumPOVs; ++i)
  194. {
  195. ctrl.type = Control::POV_H;
  196. ctrl.name.Format("hw_PovH%d", i+1);
  197. ctrl.name = GetMappedName(ctrl.name, table, ctrl.inverted);
  198. ctrl.dataOffset = (char*)&(state.rgdwPOV[i]) - (char*)&state;
  199. m_Controls.Add(ctrl);
  200. ctrl.type = Control::POV_V;
  201. ctrl.name.Format("hw_PovV%d", i+1);
  202. ctrl.name = GetMappedName(ctrl.name, table, ctrl.inverted);
  203. ctrl.dataOffset = (char*)&(state.rgdwPOV[i]) - (char*)&state;
  204. m_Controls.Add(ctrl);
  205. }
  206. ///////////////////////////////
  207. indexStart = GetIndex("gp_B7");
  208. }
  209. void JoystickDevice::Update(float DeltaTime)
  210. {
  211. if (!m_Device) return;
  212. HRESULT hr = m_Device->Poll();
  213. hr=m_Device->GetDeviceState( sizeof(m_Data), &m_Data);
  214. if (SUCCEEDED(hr))
  215. {
  216. for (unsigned int i = 0; i < m_Controls.Size(); ++i)
  217. {
  218. if (m_Controls[i].type == Control::Button)
  219. {
  220. m_Controls[i].value = GetButtonData(m_Controls[i].dataOffset);
  221. }
  222. else
  223. if (m_Controls[i].type == Control::Axis)
  224. {
  225. m_Controls[i].value = GetAxisData(m_Controls[i].type, m_Controls[i].dataOffset);
  226. }
  227. else
  228. if (m_Controls[i].type == Control::POV_H)
  229. {
  230. m_Controls[i].value = GetPOVData(m_Controls[i].dataOffset, true);
  231. }
  232. else
  233. if (m_Controls[i].type == Control::POV_V)
  234. {
  235. m_Controls[i].value = GetPOVData(m_Controls[i].dataOffset, false);
  236. }
  237. }
  238. }
  239. else
  240. for (unsigned int i = 0; i < m_Controls.Size(); ++i)
  241. m_Controls[i].value = 0.0f;
  242. if (FAILED(hr))
  243. m_Device->Acquire();
  244. }
  245. void JoystickDevice::EndFrame()
  246. {
  247. // if (GetAsyncKeyState(VK_NUMPAD0) & 0x8000)
  248. if( api->DebugKeyState(VK_NUMPAD0))
  249. {
  250. IRender* render = (IRender*)api->GetService("DX9Render");
  251. for (unsigned int i = 0; i < m_Controls.Size(); ++i)
  252. render->GetSystemFont()->Print(0.0f, i*15.0f, "%s : %0.2f", m_Controls[i].name.GetBuffer(), m_Controls[i].value);
  253. }
  254. }
  255. long JoystickDevice::GetIndex(const char* deviceControl)
  256. {
  257. for (unsigned int i = 0; i < m_Controls.Size(); ++i)
  258. if ( m_Controls[i].name == deviceControl )
  259. return i;
  260. return INVALID_CODE;
  261. }
  262. float JoystickDevice::GetRawValue(long controlIndex) const
  263. {
  264. if ( (unsigned long)controlIndex >= m_Controls.Size() )
  265. return 0.0f;
  266. return m_Controls[controlIndex].inverted ? -m_Controls[controlIndex].value : m_Controls[controlIndex].value;
  267. }
  268. bool JoystickDevice::IsStartPressed() const
  269. {
  270. return fabsf(GetRawValue(indexStart)) > 0.001f;
  271. }
  272. bool JoystickDevice::IsDisconnected() const
  273. {
  274. HRESULT hr = m_Device->Poll();
  275. if (SUCCEEDED(hr) || hr == DIERR_NOTACQUIRED)
  276. return false;
  277. return true;
  278. }
  279. bool JoystickDevice::SetValues(ValueType type, const void *values, long nBytes)
  280. {
  281. if( type == FFRotorSpeed )
  282. {
  283. long n = nBytes/sizeof(float);
  284. Assert(n >= 2)
  285. float ls = ((float *)values)[0];
  286. float rs = ((float *)values)[1];
  287. LONG rglDirection[2] = {0,0};
  288. ls *= -10000.0f; if( ls < -10000.0f ) ls = -10000.0f;
  289. rs *= 10000.0f; if( rs > 10000.0f ) rs = 10000.0f;
  290. DIRAMPFORCE cf;
  291. DIEFFECT eff; ZeroMemory(&eff,sizeof(eff));
  292. eff.dwSize = sizeof(DIEFFECT);
  293. eff.dwFlags = DIEFF_CARTESIAN|DIEFF_OBJECTOFFSETS;
  294. // eff.cAxes = axesN;
  295. eff.cAxes = 2;
  296. eff.rglDirection = rglDirection;
  297. eff.lpEnvelope = 0;
  298. eff.cbTypeSpecificParams = sizeof(DIRAMPFORCE);
  299. eff.lpvTypeSpecificParams = &cf;
  300. eff.dwStartDelay = 0;
  301. eff.dwDuration = INFINITE;
  302. cf.lStart = (LONG)ls;
  303. cf.lEnd = (LONG)ls;
  304. if( lef )
  305. lef->SetParameters(&eff,DIEP_TYPESPECIFICPARAMS|DIEP_START);
  306. cf.lStart = (LONG)rs;
  307. cf.lEnd = (LONG)rs;
  308. if( ref )
  309. ref->SetParameters(&eff,DIEP_TYPESPECIFICPARAMS|DIEP_START);
  310. return true;
  311. }
  312. else
  313. return false;
  314. }
  315. #endif