MouseWin.cpp 4.3 KB

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