KeyboardWin.cpp 6.9 KB

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