Mouse.cpp 4.1 KB

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