2
0

Mouse.cpp 4.9 KB

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