KeyboardWin.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Input/Win/KeyboardWin.h>
  6. #include <Renderer/Renderer.h>
  7. #include <Jolt/Core/Profiler.h>
  8. KeyboardWin::KeyboardWin()
  9. {
  10. Reset();
  11. }
  12. KeyboardWin::~KeyboardWin()
  13. {
  14. Shutdown();
  15. }
  16. void KeyboardWin::Reset()
  17. {
  18. mDI = nullptr;
  19. mKeyboard = nullptr;
  20. ResetKeyboard();
  21. }
  22. void KeyboardWin::ResetKeyboard()
  23. {
  24. memset(&mKeyPressed, 0, sizeof(mKeyPressed));
  25. memset(&mDOD, 0, sizeof(mDOD));
  26. mDODLength = 0;
  27. mCurrentPosition = 0;
  28. }
  29. bool KeyboardWin::Initialize(Renderer *inRenderer)
  30. #ifdef JPH_COMPILER_CLANG
  31. // DIPROP_BUFFERSIZE is a pointer to 1 which causes UBSan: runtime error: reference binding to misaligned address 0x000000000001
  32. __attribute__((no_sanitize("alignment")))
  33. #endif
  34. {
  35. // Create direct input interface
  36. if (FAILED(CoCreateInstance(CLSID_DirectInput8, nullptr, CLSCTX_INPROC_SERVER, IID_IDirectInput8W, (void **)&mDI)))
  37. {
  38. Trace("Unable to create DirectInput interface, DirectX 8.0 is required");
  39. return false;
  40. }
  41. // Initialize direct input interface
  42. if (FAILED(mDI->Initialize((HINSTANCE)GetModuleHandle(nullptr), DIRECTINPUT_VERSION)))
  43. {
  44. Trace("Unable to initialize DirectInput interface, DirectX 8.0 is required");
  45. return false;
  46. }
  47. // Create keyboard device
  48. if (FAILED(mDI->CreateDevice(GUID_SysKeyboard, &mKeyboard, nullptr)))
  49. {
  50. Trace("Unable to get DirectInputDevice interface, DirectX 8.0 is required");
  51. return false;
  52. }
  53. // Set cooperative level for keyboard
  54. if (FAILED(mKeyboard->SetCooperativeLevel(inRenderer->GetWindowHandle(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)))
  55. {
  56. Trace("Unable to set cooperative level for keyboard");
  57. return false;
  58. }
  59. // Set data format
  60. if (FAILED(mKeyboard->SetDataFormat(&c_dfDIKeyboard)))
  61. {
  62. Trace("Unable to set data format to keyboard");
  63. return false;
  64. }
  65. // Create a keyboard buffer
  66. DIPROPDWORD dipdw;
  67. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  68. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  69. dipdw.diph.dwObj = 0;
  70. dipdw.diph.dwHow = DIPH_DEVICE;
  71. dipdw.dwData = BUFFERSIZE;
  72. if (FAILED(mKeyboard->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
  73. {
  74. Trace("Unable to set keyboard buffer size");
  75. return false;
  76. }
  77. return true;
  78. }
  79. void KeyboardWin::Shutdown()
  80. {
  81. if (mKeyboard)
  82. {
  83. mKeyboard->Unacquire();
  84. mKeyboard = nullptr;
  85. }
  86. mDI = nullptr;
  87. Reset();
  88. }
  89. void KeyboardWin::Poll()
  90. {
  91. JPH_PROFILE_FUNCTION();
  92. // Get the state of the keyboard
  93. if (FAILED(mKeyboard->GetDeviceState(sizeof(mKeyPressed), mKeyPressed)))
  94. {
  95. mKeyboard->Acquire();
  96. if (FAILED(mKeyboard->GetDeviceState(sizeof(mKeyPressed), mKeyPressed)))
  97. {
  98. ResetKeyboard();
  99. return;
  100. }
  101. }
  102. // Get the state in a buffer
  103. mDODLength = BUFFERSIZE;
  104. mCurrentPosition = 0;
  105. if (FAILED(mKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  106. {
  107. mKeyboard->Acquire();
  108. if (FAILED(mKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  109. {
  110. ResetKeyboard();
  111. return;
  112. }
  113. }
  114. }
  115. EKey KeyboardWin::GetFirstKey()
  116. {
  117. mCurrentPosition = 0;
  118. return GetNextKey();
  119. }
  120. EKey KeyboardWin::GetNextKey()
  121. {
  122. while (mCurrentPosition < mDODLength)
  123. {
  124. // Get next key
  125. const DIDEVICEOBJECTDATA &current = mDOD[mCurrentPosition];
  126. mCurrentPosition++;
  127. // Return it
  128. if (current.dwData & 0x80)
  129. return ToKey(current.dwOfs);
  130. }
  131. return EKey::Invalid;
  132. }
  133. EKey KeyboardWin::ToKey(int inValue) const
  134. {
  135. switch (inValue)
  136. {
  137. case DIK_A: return EKey::A;
  138. case DIK_B: return EKey::B;
  139. case DIK_C: return EKey::C;
  140. case DIK_D: return EKey::D;
  141. case DIK_E: return EKey::E;
  142. case DIK_F: return EKey::F;
  143. case DIK_G: return EKey::G;
  144. case DIK_H: return EKey::H;
  145. case DIK_I: return EKey::I;
  146. case DIK_J: return EKey::J;
  147. case DIK_K: return EKey::K;
  148. case DIK_L: return EKey::L;
  149. case DIK_M: return EKey::M;
  150. case DIK_N: return EKey::N;
  151. case DIK_O: return EKey::O;
  152. case DIK_P: return EKey::P;
  153. case DIK_Q: return EKey::Q;
  154. case DIK_R: return EKey::R;
  155. case DIK_S: return EKey::S;
  156. case DIK_T: return EKey::T;
  157. case DIK_U: return EKey::U;
  158. case DIK_V: return EKey::V;
  159. case DIK_W: return EKey::W;
  160. case DIK_X: return EKey::X;
  161. case DIK_Y: return EKey::Y;
  162. case DIK_Z: return EKey::Z;
  163. case DIK_0: return EKey::Num0;
  164. case DIK_1: return EKey::Num1;
  165. case DIK_2: return EKey::Num2;
  166. case DIK_3: return EKey::Num3;
  167. case DIK_4: return EKey::Num4;
  168. case DIK_5: return EKey::Num5;
  169. case DIK_6: return EKey::Num6;
  170. case DIK_7: return EKey::Num7;
  171. case DIK_8: return EKey::Num8;
  172. case DIK_9: return EKey::Num9;
  173. case DIK_SPACE: return EKey::Space;
  174. case DIK_COMMA: return EKey::Comma;
  175. case DIK_PERIOD: return EKey::Period;
  176. case DIK_ESCAPE: return EKey::Escape;
  177. case DIK_LSHIFT: return EKey::LShift;
  178. case DIK_RSHIFT: return EKey::RShift;
  179. case DIK_LCONTROL: return EKey::LControl;
  180. case DIK_RCONTROL: return EKey::RControl;
  181. case DIK_LALT: return EKey::LAlt;
  182. case DIK_RALT: return EKey::RAlt;
  183. case DIK_LEFT: return EKey::Left;
  184. case DIK_RIGHT: return EKey::Right;
  185. case DIK_UP: return EKey::Up;
  186. case DIK_DOWN: return EKey::Down;
  187. case DIK_RETURN: return EKey::Return;
  188. default: return EKey::Unknown;
  189. }
  190. }
  191. int KeyboardWin::FromKey(EKey inKey) const
  192. {
  193. switch (inKey)
  194. {
  195. case EKey::A: return DIK_A;
  196. case EKey::B: return DIK_B;
  197. case EKey::C: return DIK_C;
  198. case EKey::D: return DIK_D;
  199. case EKey::E: return DIK_E;
  200. case EKey::F: return DIK_F;
  201. case EKey::G: return DIK_G;
  202. case EKey::H: return DIK_H;
  203. case EKey::I: return DIK_I;
  204. case EKey::J: return DIK_J;
  205. case EKey::K: return DIK_K;
  206. case EKey::L: return DIK_L;
  207. case EKey::M: return DIK_M;
  208. case EKey::N: return DIK_N;
  209. case EKey::O: return DIK_O;
  210. case EKey::P: return DIK_P;
  211. case EKey::Q: return DIK_Q;
  212. case EKey::R: return DIK_R;
  213. case EKey::S: return DIK_S;
  214. case EKey::T: return DIK_T;
  215. case EKey::U: return DIK_U;
  216. case EKey::V: return DIK_V;
  217. case EKey::W: return DIK_W;
  218. case EKey::X: return DIK_X;
  219. case EKey::Y: return DIK_Y;
  220. case EKey::Z: return DIK_Z;
  221. case EKey::Num0: return DIK_0;
  222. case EKey::Num1: return DIK_1;
  223. case EKey::Num2: return DIK_2;
  224. case EKey::Num3: return DIK_3;
  225. case EKey::Num4: return DIK_4;
  226. case EKey::Num5: return DIK_5;
  227. case EKey::Num6: return DIK_6;
  228. case EKey::Num7: return DIK_7;
  229. case EKey::Num8: return DIK_8;
  230. case EKey::Num9: return DIK_9;
  231. case EKey::Space: return DIK_SPACE;
  232. case EKey::Comma: return DIK_COMMA;
  233. case EKey::Period: return DIK_PERIOD;
  234. case EKey::Escape: return DIK_ESCAPE;
  235. case EKey::LShift: return DIK_LSHIFT;
  236. case EKey::RShift: return DIK_RSHIFT;
  237. case EKey::LControl: return DIK_LCONTROL;
  238. case EKey::RControl: return DIK_RCONTROL;
  239. case EKey::LAlt: return DIK_LALT;
  240. case EKey::RAlt: return DIK_RALT;
  241. case EKey::Left: return DIK_LEFT;
  242. case EKey::Right: return DIK_RIGHT;
  243. case EKey::Up: return DIK_UP;
  244. case EKey::Down: return DIK_DOWN;
  245. case EKey::Return: return DIK_RETURN;
  246. case EKey::Invalid:
  247. case EKey::Unknown:
  248. default:
  249. return 0;
  250. }
  251. }