You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
12 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /***************************************************************************
  2. * Copyright (C) 2014 M Wellings *
  3. * info@openforeveryone.co.uk *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License version 2 as *
  7. * published by the Free Software Foundation *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include "qgamecontroller.h"
  20. #include "qgamecontroller_win_p.h"
  21. #include <QtCore/QDebug>
  22. QGameControllerPrivate *joysticktoenume; //TODO: Dispense with this global pointer.
  23. LPDIRECTINPUT8 g_pDI = nullptr;
  24. QGameControllerPrivate::QGameControllerPrivate(uint id, QGameController *q) :
  25. q_ptr(q)
  26. {
  27. ID=id;
  28. Valid=false;
  29. Axis=0;
  30. Buttons=0;
  31. // qDebug("QGameController::QGameController(%i)", ID);
  32. HRESULT hr;
  33. g_pJoystick = nullptr;
  34. // Register with DirectInput to get a pointer to an IDirectInput
  35. if (g_pDI==nullptr)
  36. {
  37. qDebug() << "Setting up directinput";
  38. if( FAILED( hr = DirectInput8Create( GetModuleHandle( nullptr ), DIRECTINPUT_VERSION,
  39. IID_IDirectInput8, ( VOID** )&g_pDI, nullptr ) ) )
  40. // return hr;
  41. qDebug() << "Error 1";
  42. }
  43. DIJOYCONFIG PreferredJoyCfg = {0};
  44. DI_ENUM_CONTEXT enumContext;
  45. enumContext.pPreferredJoyCfg = &PreferredJoyCfg;
  46. enumContext.bPreferredJoyCfgValid = false;
  47. IDirectInputJoyConfig8* pJoyConfig = nullptr;
  48. if( FAILED( hr = g_pDI->QueryInterface( IID_IDirectInputJoyConfig8, ( void** )&pJoyConfig ) ) )
  49. // return hr;
  50. qDebug() << "Error 2";
  51. PreferredJoyCfg.dwSize = sizeof( PreferredJoyCfg );
  52. if( SUCCEEDED( pJoyConfig->GetConfig( 0, &PreferredJoyCfg, DIJC_GUIDINSTANCE ) ) ) // This function is expected to fail if no joystick is attached
  53. enumContext.bPreferredJoyCfgValid = true;
  54. else
  55. qDebug() << "bPreferredJoyCfgValid == false";
  56. SAFE_RELEASE( pJoyConfig );
  57. joysticktoenume=this;
  58. enumCounter=0;
  59. if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL,
  60. EnumJoysticksCallback,
  61. &enumContext, DIEDFL_ATTACHEDONLY ) ) )
  62. qDebug() << "Error 3";
  63. // Make sure we got a joystick
  64. if( !g_pJoystick )
  65. {
  66. // qDebug() << "Joystick not found";
  67. return;
  68. }
  69. // qDebug() << "Setting data format";
  70. if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) )
  71. qDebug() << "Error 4";
  72. if( FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback,
  73. ( VOID* )NULL, DIDFT_ALL ) ) )
  74. {
  75. qDebug() << "Error 6";
  76. return;
  77. }
  78. DIDEVICEINSTANCE joystickinfo;
  79. joystickinfo.dwSize=sizeof(joystickinfo);
  80. if( FAILED( hr = g_pJoystick->GetDeviceInfo(&joystickinfo)))
  81. qDebug() << "Error 6";
  82. if (hr==DIERR_INVALIDPARAM)
  83. qDebug() << "DIERR_INVALIDPARAM";
  84. Description = QString::fromUtf16((ushort*)&(joystickinfo.tszProductName));
  85. // qDebug() << description() << QString::fromUtf16((ushort*)&(joystickinfo.tszInstaxnceName));
  86. Valid=true;
  87. }
  88. BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
  89. VOID* pContext )
  90. {
  91. // qDebug() << "EnumJoysticksCallback()" << joysticktoenume->enumCounter;
  92. if (joysticktoenume->enumCounter!=joysticktoenume->ID)
  93. {
  94. // qDebug() << "Skiping" << joysticktoenume->enumCounter;
  95. joysticktoenume->enumCounter++;
  96. return DIENUM_CONTINUE;
  97. }
  98. // auto pEnumContext = reinterpret_cast<DI_ENUM_CONTEXT*>( pContext );
  99. // if( pEnumContext->bPreferredJoyCfgValid &&
  100. // !IsEqualGUID( pdidInstance->guidInstance, pEnumContext->pPreferredJoyCfg->guidInstance ) )
  101. // {
  102. // //Maybe we should mark this as prefered in QGameController
  103. // }
  104. // Get an interface to this joystick.
  105. HRESULT hr;
  106. hr = g_pDI->CreateDevice( pdidInstance->guidInstance, &(joysticktoenume->g_pJoystick), nullptr );
  107. if( FAILED( hr ) )
  108. {
  109. qDebug() << "CreateDevice failed";
  110. return DIENUM_CONTINUE;
  111. }
  112. // qDebug() << "EnumJoysticksCallback() Joystick found";
  113. return DIENUM_STOP;
  114. }
  115. BOOL CALLBACK EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,
  116. VOID* pContext )
  117. {
  118. //Set the range for axis
  119. if( pdidoi->dwType & DIDFT_AXIS )
  120. {
  121. DIPROPRANGE diprg;
  122. diprg.diph.dwSize = sizeof( DIPROPRANGE );
  123. diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER );
  124. diprg.diph.dwHow = DIPH_BYID;
  125. diprg.diph.dwObj = pdidoi->dwType; // Specify the enumerated axis
  126. diprg.lMin = -1000;
  127. diprg.lMax = +1000;
  128. if( FAILED( joysticktoenume->g_pJoystick->SetProperty( DIPROP_RANGE, &diprg.diph ) ) )
  129. return DIENUM_STOP;
  130. }
  131. // qDebug() << "JSObject Found";
  132. // Set the UI to reflect what objects the joystick supports
  133. if( pdidoi->guidType == GUID_XAxis )
  134. {
  135. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  136. // qDebug() << "Axis found";
  137. joysticktoenume->Axis++;
  138. }
  139. if( pdidoi->guidType == GUID_YAxis )
  140. {
  141. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  142. // qDebug() << "Axis found";
  143. joysticktoenume->Axis++;
  144. }
  145. if( pdidoi->guidType == GUID_ZAxis )
  146. {
  147. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  148. // qDebug() << "Axis found";
  149. joysticktoenume->Axis++;
  150. }
  151. if( pdidoi->guidType == GUID_RxAxis )
  152. {
  153. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  154. // qDebug() << "Axis found";
  155. joysticktoenume->Axis++;
  156. }
  157. if( pdidoi->guidType == GUID_RyAxis )
  158. {
  159. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  160. // qDebug() << "Axis found";
  161. joysticktoenume->Axis++;
  162. }
  163. if( pdidoi->guidType == GUID_RzAxis )
  164. {
  165. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  166. // qDebug() << "Axis found";
  167. joysticktoenume->Axis++;
  168. }
  169. if( pdidoi->guidType == GUID_Slider )
  170. {
  171. // qDebug() << "Slider found";
  172. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  173. joysticktoenume->Axis++;
  174. }
  175. if( pdidoi->guidType == GUID_POV )
  176. {
  177. // qDebug() << "POV found";
  178. //We add two axis to represent this pov
  179. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  180. joysticktoenume->Axis++;
  181. joysticktoenume->DIaxisGIIDs.push_back(pdidoi->guidType);
  182. joysticktoenume->Axis++;
  183. }
  184. if( pdidoi->guidType == GUID_Button )
  185. {
  186. // qDebug() << "Button found";
  187. joysticktoenume->Buttons++;
  188. }
  189. if( pdidoi->guidType == GUID_Key )
  190. {
  191. // qDebug() << "Key found";
  192. }
  193. if( pdidoi->guidType == GUID_Unknown )
  194. {
  195. // qDebug() << "Unknown object found";
  196. }
  197. return DIENUM_CONTINUE;
  198. }
  199. void QGameControllerPrivate::readGameController()
  200. {
  201. Q_Q(QGameController);
  202. HRESULT hr;
  203. DIJOYSTATE2 js; // DInput joystick state
  204. if( !g_pJoystick )
  205. return;
  206. hr = g_pJoystick->Poll();
  207. if( FAILED( hr ) )
  208. {
  209. hr = g_pJoystick->Acquire();
  210. while( hr == DIERR_INPUTLOST )
  211. hr = g_pJoystick->Acquire();
  212. return;
  213. }
  214. if( FAILED( hr = g_pJoystick->GetDeviceState( sizeof( DIJOYSTATE2 ), &js ) ) )
  215. return;
  216. // Axes
  217. float value = 0;
  218. int slider = 0;
  219. int pov = 0;
  220. for (uint axisid = 0; axisid<Axis; axisid++)
  221. {
  222. // qDebug("Reading joystick %i axis %i of %i, %i", id(), axisid, Axis, axisGIIDs.count());
  223. Q_ASSERT(axisid<DIaxisGIIDs.count());
  224. GUID guidForThisAxis = DIaxisGIIDs.at(axisid);
  225. if (guidForThisAxis==GUID_XAxis)
  226. value = (float)(js.lX);
  227. else if(guidForThisAxis==GUID_YAxis)
  228. value = (float)(js.lY);
  229. else if(guidForThisAxis==GUID_ZAxis)
  230. value = (float)(js.lZ);
  231. else if(guidForThisAxis==GUID_RxAxis)
  232. value = (float)(js.lRx);
  233. else if(guidForThisAxis==GUID_RyAxis)
  234. value = (float)(js.lRy);
  235. else if(guidForThisAxis==GUID_RzAxis)
  236. value = (float)(js.lRz);
  237. else if(guidForThisAxis==GUID_Slider)
  238. {
  239. value = (float)(js.rglSlider[slider]);
  240. slider++;
  241. }
  242. if(guidForThisAxis==GUID_POV)
  243. {
  244. DWORD povvalue = js.rgdwPOV[pov];
  245. float valueX=0;
  246. float valueY=0;
  247. if (povvalue==-1 || (LOWORD(povvalue) == 0xFFFF))
  248. {
  249. // qDebug("POV %i is centred %i.", pov, povvalue);
  250. }
  251. else
  252. {
  253. int povdegs = (int)povvalue/100;
  254. float povrads = (float)povdegs/360.0*2*3.141593;
  255. // qDebug("POV %i now %i (%i deg, %f rads).", pov, povvalue, povdegs, povrads);
  256. valueX=sin(povrads);
  257. valueY=cos(povrads);
  258. //Dissable these lines to get pov on circle.
  259. valueX=qRound(valueX);
  260. valueY=qRound(valueY);
  261. }
  262. if (valueX!=AxisValues.value(axisid))
  263. {
  264. AxisValues.insert(axisid,valueX);
  265. QGameControllerAxisEvent *event=new QGameControllerAxisEvent(ID, axisid, valueX);
  266. // qDebug("Axis %i moved to %f.", axisid, valueX);
  267. emit(q->gameControllerAxisEvent(event));
  268. }if (valueY!=AxisValues.value(axisid+1))
  269. {
  270. AxisValues.insert(axisid+1,valueY);
  271. QGameControllerAxisEvent *event=new QGameControllerAxisEvent(ID, axisid+1, valueY);
  272. // qDebug("Axis %i moved to %f.", axisid+1, valueY);
  273. emit(q->gameControllerAxisEvent(event));
  274. }
  275. axisid++; //We have dealt with 2 axis in one go.
  276. pov++;
  277. }else
  278. {
  279. value=value/1000.0;
  280. if (value!=AxisValues.value(axisid))
  281. {
  282. AxisValues.insert(axisid,value);
  283. QGameControllerAxisEvent *event=new QGameControllerAxisEvent(ID, axisid, value);
  284. // qDebug("Axis %i moved to %f.", axisid, value);
  285. emit(q->gameControllerAxisEvent(event));
  286. }
  287. }
  288. }
  289. //Buttons
  290. for( int i = 0; i < Buttons; i++ )
  291. {
  292. if( js.rgbButtons[i] & 0x80 )
  293. {
  294. if (ButtonValues.value(i)!=true)
  295. {
  296. ButtonValues.insert(i,true);
  297. // qDebug("Button %i pressed.", i);
  298. QGameControllerButtonEvent* event=new QGameControllerButtonEvent(ID, i, true);
  299. emit(q->gameControllerButtonEvent((QGameControllerButtonEvent*)event));
  300. }
  301. }else
  302. {
  303. if (ButtonValues.value(i)!=false)
  304. {
  305. ButtonValues.insert(i,false);
  306. // qDebug("Button %i released.", i);
  307. QGameControllerButtonEvent* event=new QGameControllerButtonEvent(ID, i, false);
  308. emit(q->gameControllerButtonEvent((QGameControllerButtonEvent*)event));
  309. }
  310. }
  311. }
  312. }