Mouse.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/Mouse.h>
  6. #include <Renderer/Renderer.h>
  7. #include <Jolt/Core/Profiler.h>
  8. Mouse::Mouse()
  9. {
  10. Reset();
  11. }
  12. Mouse::~Mouse()
  13. {
  14. Shutdown();
  15. }
  16. void
  17. Mouse::Reset()
  18. {
  19. mDI = nullptr;
  20. mMouse = nullptr;
  21. mMousePos.x = 0;
  22. mMousePos.y = 0;
  23. ResetMouse();
  24. }
  25. void Mouse::ResetMouse()
  26. {
  27. memset(&mMouseState, 0, sizeof(mMouseState));
  28. mMousePosInitialized = false;
  29. memset(&mDOD, 0, sizeof(mDOD));
  30. mDODLength = 0;
  31. mTimeLeftButtonLastReleased = 0;
  32. mLeftButtonDoubleClicked = false;
  33. }
  34. void Mouse::DetectParsecRunning()
  35. {
  36. mIsParsecRunning = false;
  37. if (SC_HANDLE manager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT))
  38. {
  39. if (SC_HANDLE service = OpenServiceA(manager, "Parsec", SERVICE_QUERY_STATUS))
  40. {
  41. SERVICE_STATUS status;
  42. if (QueryServiceStatus(service, &status))
  43. {
  44. mIsParsecRunning = status.dwCurrentState == SERVICE_RUNNING;
  45. }
  46. CloseServiceHandle(service);
  47. }
  48. CloseServiceHandle(manager);
  49. }
  50. }
  51. bool Mouse::Initialize(Renderer *inRenderer)
  52. #ifdef JPH_COMPILER_CLANG
  53. // DIPROP_BUFFERSIZE is a pointer to 1 which causes UBSan: runtime error: reference binding to misaligned address 0x000000000001
  54. __attribute__((no_sanitize("alignment")))
  55. #endif
  56. {
  57. // Store renderer
  58. mRenderer = inRenderer;
  59. // Create direct input interface
  60. if (FAILED(CoCreateInstance(CLSID_DirectInput8, nullptr, CLSCTX_INPROC_SERVER, IID_IDirectInput8W, (void **)&mDI)))
  61. {
  62. Trace("Unable to create DirectInput interface, DirectX 8.0 is required");
  63. return false;
  64. }
  65. // Initialize direct input interface
  66. if (FAILED(mDI->Initialize((HINSTANCE)GetModuleHandle(nullptr), DIRECTINPUT_VERSION)))
  67. {
  68. Trace("Unable to initialize DirectInput interface, DirectX 8.0 is required");
  69. return false;
  70. }
  71. // Create Mouse device
  72. if (FAILED(mDI->CreateDevice(GUID_SysMouse, &mMouse, nullptr)))
  73. {
  74. Trace("Unable to get DirectInputDevice interface, DirectX 8.0 is required");
  75. return false;
  76. }
  77. // Set cooperative level for Mouse
  78. SetExclusive(false);
  79. // Set data format
  80. if (FAILED(mMouse->SetDataFormat(&c_dfDIMouse)))
  81. {
  82. Trace("Unable to set data format to mouse");
  83. return false;
  84. }
  85. // Create a mouse buffer
  86. DIPROPDWORD dipdw;
  87. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  88. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  89. dipdw.diph.dwObj = 0;
  90. dipdw.diph.dwHow = DIPH_DEVICE;
  91. dipdw.dwData = BUFFERSIZE;
  92. if (FAILED(mMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
  93. {
  94. Trace("Unable to set mouse buffer size");
  95. return false;
  96. }
  97. // Check if the parsec service is running
  98. DetectParsecRunning();
  99. return true;
  100. }
  101. void Mouse::Shutdown()
  102. {
  103. if (mMouse)
  104. {
  105. mMouse->Unacquire();
  106. mMouse = nullptr;
  107. }
  108. mDI = nullptr;
  109. Reset();
  110. }
  111. void Mouse::Poll()
  112. {
  113. JPH_PROFILE_FUNCTION();
  114. // Remember last position
  115. POINT old_mouse_pos = mMousePos;
  116. // Get mouse position using the standard window call
  117. if (!GetCursorPos(&mMousePos))
  118. {
  119. ResetMouse();
  120. return;
  121. }
  122. // If we lost mouse before, we need to reset the old mouse pos to the current one
  123. if (!mMousePosInitialized)
  124. {
  125. old_mouse_pos = mMousePos;
  126. mMousePosInitialized = true;
  127. }
  128. // Convert to window space
  129. if (!ScreenToClient(mRenderer->GetWindowHandle(), &mMousePos))
  130. {
  131. ResetMouse();
  132. return;
  133. }
  134. // Get relative movement
  135. if (FAILED(mMouse->GetDeviceState(sizeof(mMouseState), &mMouseState)))
  136. {
  137. // Mouse input was lost, reacquire
  138. mMouse->Acquire();
  139. if (FAILED(mMouse->GetDeviceState(sizeof(mMouseState), &mMouseState)))
  140. {
  141. ResetMouse();
  142. return;
  143. }
  144. }
  145. // If we're connected through remote desktop or Parsec then GetDeviceState returns faulty data for lX and lY so we need to use a fallback
  146. if (GetSystemMetrics(SM_REMOTESESSION) || mIsParsecRunning)
  147. {
  148. // Just use the delta between the current and last mouse position.
  149. // Note that this has the disadvantage that you can no longer rotate any further if you're at the edge of the screen,
  150. // but unfortunately a RDP session doesn't allow capturing the mouse so there doesn't seem to be a workaround for this.
  151. mMouseState.lX = mMousePos.x - old_mouse_pos.x;
  152. mMouseState.lY = mMousePos.y - old_mouse_pos.y;
  153. }
  154. // Get the state in a buffer for checking doubleclicks
  155. if (FAILED(mMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  156. {
  157. // We lost mMouse input, reacquire
  158. mMouse->Acquire();
  159. if (FAILED(mMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  160. {
  161. // Unable to reacquire, reset button info
  162. mTimeLeftButtonLastReleased = 0;
  163. mLeftButtonDoubleClicked = false;
  164. return;
  165. }
  166. }
  167. // Check for double clicks
  168. for (DWORD d = 0; d < mDODLength; d++)
  169. {
  170. // Check if this means left button is pressed
  171. if (mDOD[d].dwOfs == DIMOFS_BUTTON0)
  172. {
  173. if (mDOD[d].dwData & 0x80)
  174. {
  175. if (mDOD[d].dwTimeStamp - mTimeLeftButtonLastReleased <= DCLICKTIME)
  176. {
  177. // This is a double click
  178. mTimeLeftButtonLastReleased = 0;
  179. mLeftButtonDoubleClicked = true;
  180. }
  181. }
  182. else // Remember last time button was released
  183. mTimeLeftButtonLastReleased = mDOD[d].dwTimeStamp;
  184. }
  185. }
  186. }
  187. void Mouse::HideCursor()
  188. {
  189. ::ShowCursor(false);
  190. }
  191. void Mouse::ShowCursor()
  192. {
  193. ::ShowCursor(true);
  194. }
  195. void Mouse::SetExclusive(bool inExclusive)
  196. {
  197. // Set cooperative level for Mouse
  198. if (FAILED(mMouse->SetCooperativeLevel(mRenderer->GetWindowHandle(), (inExclusive? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE) | DISCL_FOREGROUND)))
  199. Trace("Failed to set cooperative level for mouse");
  200. }