Mouse.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. memset(&mDOD, 0, sizeof(mDOD));
  28. mDODLength = 0;
  29. mTimeLeftButtonLastReleased = 0;
  30. mLeftButtonDoubleClicked = false;
  31. }
  32. bool Mouse::Initialize(Renderer *inRenderer)
  33. #ifdef JPH_COMPILER_CLANG
  34. // DIPROP_BUFFERSIZE is a pointer to 1 which causes UBSan: runtime error: reference binding to misaligned address 0x000000000001
  35. __attribute__((no_sanitize("alignment")))
  36. #endif
  37. {
  38. // Store renderer
  39. mRenderer = inRenderer;
  40. // Create direct input interface
  41. if (FAILED(CoCreateInstance(CLSID_DirectInput8, nullptr, CLSCTX_INPROC_SERVER, IID_IDirectInput8W, (void **)&mDI)))
  42. {
  43. Trace("Unable to create DirectInput interface, DirectX 8.0 is required");
  44. return false;
  45. }
  46. // Initialize direct input interface
  47. if (FAILED(mDI->Initialize((HINSTANCE)GetModuleHandle(nullptr), DIRECTINPUT_VERSION)))
  48. {
  49. Trace("Unable to initialize DirectInput interface, DirectX 8.0 is required");
  50. return false;
  51. }
  52. // Create Mouse device
  53. if (FAILED(mDI->CreateDevice(GUID_SysMouse, &mMouse, nullptr)))
  54. {
  55. Trace("Unable to get DirectInputDevice interface, DirectX 8.0 is required");
  56. return false;
  57. }
  58. // Set cooperative level for Mouse
  59. SetExclusive(false);
  60. // Set data format
  61. if (FAILED(mMouse->SetDataFormat(&c_dfDIMouse)))
  62. {
  63. Trace("Unable to set data format to mouse");
  64. return false;
  65. }
  66. // Create a mouse buffer
  67. DIPROPDWORD dipdw;
  68. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  69. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  70. dipdw.diph.dwObj = 0;
  71. dipdw.diph.dwHow = DIPH_DEVICE;
  72. dipdw.dwData = BUFFERSIZE;
  73. if (FAILED(mMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
  74. {
  75. Trace("Unable to set mouse buffer size");
  76. return false;
  77. }
  78. return true;
  79. }
  80. void Mouse::Shutdown()
  81. {
  82. if (mMouse)
  83. {
  84. mMouse->Unacquire();
  85. mMouse = nullptr;
  86. }
  87. mDI = nullptr;
  88. Reset();
  89. }
  90. void Mouse::Poll()
  91. {
  92. JPH_PROFILE_FUNCTION();
  93. // Get mouse position using the standard window call
  94. if (!GetCursorPos(&mMousePos))
  95. {
  96. ResetMouse();
  97. return;
  98. }
  99. // Convert to window space
  100. if (!ScreenToClient(mRenderer->GetWindowHandle(), &mMousePos))
  101. {
  102. ResetMouse();
  103. return;
  104. }
  105. // Get relative movement
  106. if (FAILED(mMouse->GetDeviceState(sizeof(mMouseState), &mMouseState)))
  107. {
  108. // Mouse input was lost, reacquire
  109. mMouse->Acquire();
  110. if (FAILED(mMouse->GetDeviceState(sizeof(mMouseState), &mMouseState)))
  111. {
  112. ResetMouse();
  113. return;
  114. }
  115. }
  116. // Get the state in a buffer for checking doubleclicks
  117. if (FAILED(mMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  118. {
  119. // We lost mMouse input, reacquire
  120. mMouse->Acquire();
  121. if (FAILED(mMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  122. {
  123. // Unable to reacquire, reset button info
  124. mTimeLeftButtonLastReleased = 0;
  125. mLeftButtonDoubleClicked = false;
  126. return;
  127. }
  128. }
  129. // Check for double clicks
  130. for (DWORD d = 0; d < mDODLength; d++)
  131. {
  132. // Check if this means left button is pressed
  133. if (mDOD[d].dwOfs == DIMOFS_BUTTON0)
  134. {
  135. if (mDOD[d].dwData & 0x80)
  136. {
  137. if (mDOD[d].dwTimeStamp - mTimeLeftButtonLastReleased <= DCLICKTIME)
  138. {
  139. // This is a double click
  140. mTimeLeftButtonLastReleased = 0;
  141. mLeftButtonDoubleClicked = true;
  142. }
  143. }
  144. else // Remember last time button was released
  145. mTimeLeftButtonLastReleased = mDOD[d].dwTimeStamp;
  146. }
  147. }
  148. }
  149. void Mouse::HideCursor()
  150. {
  151. ::ShowCursor(false);
  152. }
  153. void Mouse::ShowCursor()
  154. {
  155. ::ShowCursor(true);
  156. }
  157. void Mouse::SetExclusive(bool inExclusive)
  158. {
  159. // Set cooperative level for Mouse
  160. if (FAILED(mMouse->SetCooperativeLevel(mRenderer->GetWindowHandle(), (inExclusive? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE) | DISCL_FOREGROUND)))
  161. Trace("Failed to set cooperative level for mouse");
  162. }