Keyboard.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Input/Keyboard.h>
  5. #include <Renderer/Renderer.h>
  6. #include <Core/Profiler.h>
  7. class Renderer;
  8. // Used libraries
  9. #pragma comment ( lib, "dxguid.lib" )
  10. #pragma comment ( lib, "dinput8.lib" )
  11. Keyboard::Keyboard()
  12. {
  13. Reset();
  14. }
  15. Keyboard::~Keyboard()
  16. {
  17. Shutdown();
  18. }
  19. void Keyboard::Reset()
  20. {
  21. mDI = nullptr;
  22. mKeyboard = nullptr;
  23. ResetKeyboard();
  24. }
  25. void Keyboard::ResetKeyboard()
  26. {
  27. memset(&mKeyPressed, 0, sizeof(mKeyPressed));
  28. memset(&mTimeKeyLastReleased, 0, sizeof(mTimeKeyLastReleased));
  29. memset(&mKeyDoubleClicked, 0, sizeof(mKeyDoubleClicked));
  30. memset(&mDOD, 0, sizeof(mDOD));
  31. mDODLength = 0;
  32. mCurrentPosition = 0;
  33. GetKeyboardState(mCurrentWUIState);
  34. memcpy(mPreviousWUIState, mCurrentWUIState, sizeof(mPreviousWUIState));
  35. }
  36. bool Keyboard::Initialize(Renderer *inRenderer)
  37. #ifdef JPH_COMPILER_CLANG
  38. // DIPROP_BUFFERSIZE is a pointer to 1 which causes UBSan: runtime error: reference binding to misaligned address 0x000000000001
  39. __attribute__((no_sanitize("alignment")))
  40. #endif
  41. {
  42. // Create direct input interface
  43. if (FAILED(CoCreateInstance(CLSID_DirectInput8, nullptr, CLSCTX_INPROC_SERVER, IID_IDirectInput8W, (void **)&mDI)))
  44. {
  45. Trace("Unable to create DirectInput interface, DirectX 8.0 is required");
  46. return false;
  47. }
  48. // Initialize direct input interface
  49. if (FAILED(mDI->Initialize((HINSTANCE)GetModuleHandle(nullptr), DIRECTINPUT_VERSION)))
  50. {
  51. Trace("Unable to initialize DirectInput interface, DirectX 8.0 is required");
  52. return false;
  53. }
  54. // Create keyboard device
  55. if (FAILED(mDI->CreateDevice(GUID_SysKeyboard, &mKeyboard, nullptr)))
  56. {
  57. Trace("Unable to get DirectInputDevice interface, DirectX 8.0 is required");
  58. return false;
  59. }
  60. // Set cooperative level for keyboard
  61. if (FAILED(mKeyboard->SetCooperativeLevel(inRenderer->GetWindowHandle(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)))
  62. {
  63. Trace("Unable to set cooperative level for keyboard");
  64. return false;
  65. }
  66. // Set data format
  67. if (FAILED(mKeyboard->SetDataFormat(&c_dfDIKeyboard)))
  68. {
  69. Trace("Unable to set data format to keyboard");
  70. return false;
  71. }
  72. // Create a keyboard buffer
  73. DIPROPDWORD dipdw;
  74. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  75. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  76. dipdw.diph.dwObj = 0;
  77. dipdw.diph.dwHow = DIPH_DEVICE;
  78. dipdw.dwData = BUFFERSIZE;
  79. if (FAILED(mKeyboard->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
  80. {
  81. Trace("Unable to set keyboard buffer size");
  82. return false;
  83. }
  84. // Get keyboard layout
  85. mKeyboardLayout = GetKeyboardLayout(0);
  86. return true;
  87. }
  88. void Keyboard::Shutdown()
  89. {
  90. if (mKeyboard)
  91. {
  92. mKeyboard->Unacquire();
  93. mKeyboard = nullptr;
  94. }
  95. mDI = nullptr;
  96. Reset();
  97. }
  98. void Keyboard::Poll()
  99. {
  100. JPH_PROFILE_FUNCTION();
  101. // Get the state of the keyboard
  102. if (FAILED(mKeyboard->GetDeviceState(sizeof(mKeyPressed), mKeyPressed)))
  103. {
  104. mKeyboard->Acquire();
  105. if (FAILED(mKeyboard->GetDeviceState(sizeof(mKeyPressed), mKeyPressed)))
  106. {
  107. ResetKeyboard();
  108. return;
  109. }
  110. }
  111. // Get the state in a buffer
  112. mDODLength = BUFFERSIZE;
  113. mCurrentPosition = 0;
  114. if (FAILED(mKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  115. {
  116. mKeyboard->Acquire();
  117. if (FAILED(mKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  118. {
  119. ResetKeyboard();
  120. return;
  121. }
  122. }
  123. // Check double clicks
  124. for (DWORD d = 0; d < mDODLength; d++)
  125. {
  126. // Check if this means a button is pressed
  127. if (mDOD[d].dwData & 0x80)
  128. {
  129. if (mDOD[d].dwTimeStamp - mTimeKeyLastReleased[mDOD[d].dwOfs] <= DCLICKTIME)
  130. {
  131. // This is a double click
  132. mTimeKeyLastReleased[mDOD[d].dwOfs] = 0;
  133. mKeyDoubleClicked[mDOD[d].dwOfs] = TRUE;
  134. }
  135. }
  136. else // Remember last time this key was released
  137. mTimeKeyLastReleased[mDOD[d].dwOfs] = mDOD[d].dwTimeStamp;
  138. }
  139. // Get Windows User Interface state, copy current state to previous state and get new current state
  140. memcpy(mPreviousWUIState, mCurrentWUIState, sizeof(mPreviousWUIState));
  141. GetKeyboardState(mCurrentWUIState);
  142. }
  143. int Keyboard::GetFirstKey()
  144. {
  145. mCurrentPosition = 0;
  146. memcpy(mTrackedWUIState, mPreviousWUIState, sizeof(mTrackedWUIState));
  147. return GetNextKey();
  148. }
  149. static void sPress(BYTE &ioValue)
  150. {
  151. ioValue |= 0x80;
  152. ioValue ^= 0x01;
  153. }
  154. static void sRelease(BYTE &ioValue)
  155. {
  156. ioValue &= 0x7f;
  157. }
  158. int
  159. Keyboard::GetNextKey()
  160. {
  161. while (mCurrentPosition < mDODLength)
  162. {
  163. // Get next key
  164. const DIDEVICEOBJECTDATA &current = mDOD[mCurrentPosition];
  165. mCurrentPosition++;
  166. // Check special keys and update current state accordingly
  167. if (current.dwData & 0x80)
  168. switch (current.dwOfs)
  169. {
  170. case DIK_LSHIFT: sPress(mTrackedWUIState[VK_LSHIFT]); sPress(mTrackedWUIState[VK_SHIFT]); break;
  171. case DIK_RSHIFT: sPress(mTrackedWUIState[VK_RSHIFT]); sPress(mTrackedWUIState[VK_SHIFT]); break;
  172. case DIK_LCONTROL: sPress(mTrackedWUIState[VK_LCONTROL]); sPress(mTrackedWUIState[VK_CONTROL]); break;
  173. case DIK_RCONTROL: sPress(mTrackedWUIState[VK_RCONTROL]); sPress(mTrackedWUIState[VK_CONTROL]); break;
  174. case DIK_LALT: sPress(mTrackedWUIState[VK_LMENU]); sPress(mTrackedWUIState[VK_MENU]); break;
  175. case DIK_RALT: sPress(mTrackedWUIState[VK_RMENU]); sPress(mTrackedWUIState[VK_MENU]); break;
  176. case DIK_CAPSLOCK: sPress(mTrackedWUIState[VK_CAPITAL]); break;
  177. }
  178. else
  179. switch (current.dwOfs)
  180. {
  181. case DIK_LSHIFT: sRelease(mTrackedWUIState[VK_LSHIFT]); sRelease(mTrackedWUIState[VK_SHIFT]); break;
  182. case DIK_RSHIFT: sRelease(mTrackedWUIState[VK_RSHIFT]); sRelease(mTrackedWUIState[VK_SHIFT]); break;
  183. case DIK_LCONTROL: sRelease(mTrackedWUIState[VK_LCONTROL]); sRelease(mTrackedWUIState[VK_CONTROL]); break;
  184. case DIK_RCONTROL: sRelease(mTrackedWUIState[VK_RCONTROL]); sRelease(mTrackedWUIState[VK_CONTROL]); break;
  185. case DIK_LALT: sRelease(mTrackedWUIState[VK_LMENU]); sRelease(mTrackedWUIState[VK_MENU]); break;
  186. case DIK_RALT: sRelease(mTrackedWUIState[VK_RMENU]); sRelease(mTrackedWUIState[VK_MENU]); break;
  187. case DIK_CAPSLOCK: sRelease(mTrackedWUIState[VK_CAPITAL]); break;
  188. }
  189. // Return it
  190. if (current.dwData & 0x80)
  191. return current.dwOfs;
  192. }
  193. return 0;
  194. }
  195. uint Keyboard::GetVKValue()
  196. {
  197. JPH_ASSERT(mCurrentPosition > 0, "First call GetFirstKey() to get the first key, then you can convert it to a VK code");
  198. int key = mDOD[mCurrentPosition - 1].dwOfs;
  199. return MapVirtualKeyEx(key, 1, mKeyboardLayout);
  200. }
  201. char Keyboard::GetASCIIValue()
  202. {
  203. WORD result;
  204. JPH_ASSERT(mCurrentPosition > 0, "First call GetFirstKey() to get the first key, then you can convert it to an ASCII code");
  205. int key = mDOD[mCurrentPosition - 1].dwOfs;
  206. if (ToAsciiEx(GetVKValue(), key, mTrackedWUIState, &result, 0, mKeyboardLayout) != 1)
  207. return 0;
  208. return (char)result;
  209. }