2
0

Keyboard.cpp 6.6 KB

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