Mouse.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. bool Mouse::Initialize(Renderer *inRenderer)
  35. #ifdef JPH_COMPILER_CLANG
  36. // DIPROP_BUFFERSIZE is a pointer to 1 which causes UBSan: runtime error: reference binding to misaligned address 0x000000000001
  37. __attribute__((no_sanitize("alignment")))
  38. #endif
  39. {
  40. // Store renderer
  41. mRenderer = inRenderer;
  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 Mouse device
  55. if (FAILED(mDI->CreateDevice(GUID_SysMouse, &mMouse, nullptr)))
  56. {
  57. Trace("Unable to get DirectInputDevice interface, DirectX 8.0 is required");
  58. return false;
  59. }
  60. // Set cooperative level for Mouse
  61. SetExclusive(false);
  62. // Set data format
  63. if (FAILED(mMouse->SetDataFormat(&c_dfDIMouse)))
  64. {
  65. Trace("Unable to set data format to mouse");
  66. return false;
  67. }
  68. // Create a mouse buffer
  69. DIPROPDWORD dipdw;
  70. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  71. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  72. dipdw.diph.dwObj = 0;
  73. dipdw.diph.dwHow = DIPH_DEVICE;
  74. dipdw.dwData = BUFFERSIZE;
  75. if (FAILED(mMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
  76. {
  77. Trace("Unable to set mouse buffer size");
  78. return false;
  79. }
  80. return true;
  81. }
  82. void Mouse::Shutdown()
  83. {
  84. if (mMouse)
  85. {
  86. mMouse->Unacquire();
  87. mMouse = nullptr;
  88. }
  89. mDI = nullptr;
  90. Reset();
  91. }
  92. void Mouse::Poll()
  93. {
  94. JPH_PROFILE_FUNCTION();
  95. // Remember last position
  96. POINT old_mouse_pos = mMousePos;
  97. // Get mouse position using the standard window call
  98. if (!GetCursorPos(&mMousePos))
  99. {
  100. ResetMouse();
  101. return;
  102. }
  103. // If we lost mouse before, we need to reset the old mouse pos to the current one
  104. if (!mMousePosInitialized)
  105. {
  106. old_mouse_pos = mMousePos;
  107. mMousePosInitialized = true;
  108. }
  109. // Convert to window space
  110. if (!ScreenToClient(mRenderer->GetWindowHandle(), &mMousePos))
  111. {
  112. ResetMouse();
  113. return;
  114. }
  115. // Get relative movement
  116. if (FAILED(mMouse->GetDeviceState(sizeof(mMouseState), &mMouseState)))
  117. {
  118. // Mouse input was lost, reacquire
  119. mMouse->Acquire();
  120. if (FAILED(mMouse->GetDeviceState(sizeof(mMouseState), &mMouseState)))
  121. {
  122. ResetMouse();
  123. return;
  124. }
  125. }
  126. // If we're connected through remote desktop then GetDeviceState returns faulty data for lX and lY so we need to use a fallback
  127. if (GetSystemMetrics(SM_REMOTESESSION))
  128. {
  129. // Just use the delta between the current and last mouse position.
  130. // Note that this has the disadvantage that you can no longer rotate any further if you're at the edge of the screen,
  131. // but unfortunately a RDP session doesn't allow capturing the mouse so there doesn't seem to be a workaround for this.
  132. mMouseState.lX = mMousePos.x - old_mouse_pos.x;
  133. mMouseState.lY = mMousePos.y - old_mouse_pos.y;
  134. }
  135. // Get the state in a buffer for checking doubleclicks
  136. if (FAILED(mMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  137. {
  138. // We lost mMouse input, reacquire
  139. mMouse->Acquire();
  140. if (FAILED(mMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  141. {
  142. // Unable to reacquire, reset button info
  143. mTimeLeftButtonLastReleased = 0;
  144. mLeftButtonDoubleClicked = false;
  145. return;
  146. }
  147. }
  148. // Check for double clicks
  149. for (DWORD d = 0; d < mDODLength; d++)
  150. {
  151. // Check if this means left button is pressed
  152. if (mDOD[d].dwOfs == DIMOFS_BUTTON0)
  153. {
  154. if (mDOD[d].dwData & 0x80)
  155. {
  156. if (mDOD[d].dwTimeStamp - mTimeLeftButtonLastReleased <= DCLICKTIME)
  157. {
  158. // This is a double click
  159. mTimeLeftButtonLastReleased = 0;
  160. mLeftButtonDoubleClicked = true;
  161. }
  162. }
  163. else // Remember last time button was released
  164. mTimeLeftButtonLastReleased = mDOD[d].dwTimeStamp;
  165. }
  166. }
  167. }
  168. void Mouse::HideCursor()
  169. {
  170. ::ShowCursor(false);
  171. }
  172. void Mouse::ShowCursor()
  173. {
  174. ::ShowCursor(true);
  175. }
  176. void Mouse::SetExclusive(bool inExclusive)
  177. {
  178. // Set cooperative level for Mouse
  179. if (FAILED(mMouse->SetCooperativeLevel(mRenderer->GetWindowHandle(), (inExclusive? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE) | DISCL_FOREGROUND)))
  180. Trace("Failed to set cooperative level for mouse");
  181. }