Mouse.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Mouse.h"
  22. #include "window/sdl/Window.h"
  23. // SDL
  24. #if __has_include(<SDL3/SDL_version.h>)
  25. #include <SDL3/SDL_mouse.h>
  26. #include <SDL3/SDL_version.h>
  27. #else
  28. #include <SDL_mouse.h>
  29. #include <SDL_version.h>
  30. #endif
  31. namespace love
  32. {
  33. namespace mouse
  34. {
  35. namespace sdl
  36. {
  37. #if SDL_VERSION_ATLEAST(3, 0, 0)
  38. static SDL_Window *getSDLWindow()
  39. {
  40. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  41. if (window)
  42. return (SDL_Window *) window->getHandle();
  43. return nullptr;
  44. }
  45. #endif
  46. // SDL reports mouse coordinates in the window coordinate system in OS X, but
  47. // we want them in pixel coordinates (may be different with high-DPI enabled.)
  48. static void windowToDPICoords(double *x, double *y)
  49. {
  50. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  51. if (window)
  52. window->windowToDPICoords(x, y);
  53. }
  54. // And vice versa for setting mouse coordinates.
  55. static void DPIToWindowCoords(double *x, double *y)
  56. {
  57. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  58. if (window)
  59. window->DPIToWindowCoords(x, y);
  60. }
  61. static void clampToWindow(double *x, double *y)
  62. {
  63. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  64. if (window)
  65. window->clampPositionInWindow(x, y);
  66. }
  67. Mouse::Mouse()
  68. : love::mouse::Mouse("love.mouse.sdl")
  69. , curCursor(nullptr)
  70. {
  71. // SDL may need the video subsystem in order to clean up the cursor when
  72. // quitting. Subsystems are reference-counted.
  73. SDL_InitSubSystem(SDL_INIT_VIDEO);
  74. }
  75. Mouse::~Mouse()
  76. {
  77. if (curCursor.get())
  78. setCursor();
  79. for (auto &c : systemCursors)
  80. c.second->release();
  81. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  82. }
  83. love::mouse::Cursor *Mouse::newCursor(love::image::ImageData *data, int hotx, int hoty)
  84. {
  85. return new Cursor(data, hotx, hoty);
  86. }
  87. love::mouse::Cursor *Mouse::getSystemCursor(Cursor::SystemCursor cursortype)
  88. {
  89. Cursor *cursor = nullptr;
  90. auto it = systemCursors.find(cursortype);
  91. if (it != systemCursors.end())
  92. cursor = it->second;
  93. else
  94. {
  95. cursor = new Cursor(cursortype);
  96. systemCursors[cursortype] = cursor;
  97. }
  98. return cursor;
  99. }
  100. void Mouse::setCursor(love::mouse::Cursor *cursor)
  101. {
  102. curCursor.set(cursor);
  103. SDL_SetCursor((SDL_Cursor *) cursor->getHandle());
  104. }
  105. void Mouse::setCursor()
  106. {
  107. curCursor.set(nullptr);
  108. SDL_SetCursor(SDL_GetDefaultCursor());
  109. }
  110. love::mouse::Cursor *Mouse::getCursor() const
  111. {
  112. return curCursor.get();
  113. }
  114. bool Mouse::isCursorSupported() const
  115. {
  116. return SDL_GetDefaultCursor() != nullptr;
  117. }
  118. void Mouse::getPosition(double &x, double &y) const
  119. {
  120. #if SDL_VERSION_ATLEAST(3, 0, 0)
  121. float mx, my;
  122. SDL_GetMouseState(&mx, &my);
  123. #else
  124. int mx, my;
  125. SDL_GetMouseState(&mx, &my);
  126. #endif
  127. x = (double) mx;
  128. y = (double) my;
  129. // SDL reports mouse coordinates outside the window bounds when click-and-
  130. // dragging. For compatibility we clamp instead since user code may not be
  131. // able to handle out-of-bounds coordinates. SDL has a hint to turn off
  132. // auto capture, but it doesn't report the mouse's position at the edge of
  133. // the window if the mouse moves fast enough when it's off.
  134. clampToWindow(&x, &y);
  135. windowToDPICoords(&x, &y);
  136. }
  137. void Mouse::setPosition(double x, double y)
  138. {
  139. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  140. SDL_Window *handle = nullptr;
  141. if (window)
  142. handle = (SDL_Window *) window->getHandle();
  143. DPIToWindowCoords(&x, &y);
  144. SDL_WarpMouseInWindow(handle, (int) x, (int) y);
  145. // SDL_WarpMouse doesn't directly update SDL's internal mouse state in Linux
  146. // and Windows, so we call SDL_PumpEvents now to make sure the next
  147. // getPosition call always returns the updated state.
  148. SDL_PumpEvents();
  149. }
  150. void Mouse::getGlobalPosition(double &x, double &y, int &displayindex) const
  151. {
  152. #if SDL_VERSION_ATLEAST(3, 0, 0)
  153. float globalx, globaly;
  154. #else
  155. int globalx, globaly;
  156. #endif
  157. SDL_GetGlobalMouseState(&globalx, &globaly);
  158. auto mx = globalx;
  159. auto my = globaly;
  160. #if SDL_VERSION_ATLEAST(3, 0, 0)
  161. int displaycount = 0;
  162. SDL_DisplayID *displays = SDL_GetDisplays(&displaycount);
  163. for (displayindex = 0; displayindex < displaycount; displayindex++)
  164. {
  165. SDL_Rect r = {};
  166. SDL_GetDisplayBounds(displays[displayindex], &r);
  167. SDL_FRect frect = {(float)r.x, (float)r.y, (float)r.w, (float)r.h};
  168. mx -= frect.x;
  169. my -= frect.y;
  170. SDL_FPoint p = { globalx, globaly };
  171. if (SDL_PointInRectFloat(&p, &frect))
  172. break;
  173. }
  174. #else
  175. int displaycount = SDL_GetNumVideoDisplays();
  176. for (displayindex = 0; displayindex < displaycount; displayindex++)
  177. {
  178. SDL_Rect rect = {};
  179. SDL_GetDisplayBounds(displayindex, &rect);
  180. mx -= rect.x;
  181. my -= rect.y;
  182. SDL_Point p = { globalx, globaly };
  183. if (SDL_PointInRect(&p, &rect))
  184. break;
  185. }
  186. #endif
  187. if (displayindex >= displaycount)
  188. displayindex = 0;
  189. x = (double)mx;
  190. y = (double)my;
  191. }
  192. void Mouse::setVisible(bool visible)
  193. {
  194. #if SDL_VERSION_ATLEAST(3, 0, 0)
  195. if (visible)
  196. SDL_ShowCursor();
  197. else
  198. SDL_HideCursor();
  199. #else
  200. SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
  201. #endif
  202. }
  203. bool Mouse::isDown(const std::vector<int> &buttons) const
  204. {
  205. Uint32 buttonstate = SDL_GetMouseState(nullptr, nullptr);
  206. for (int button : buttons)
  207. {
  208. if (button <= 0)
  209. continue;
  210. // We use button index 2 to represent the right mouse button, but SDL
  211. // uses 2 to represent the middle mouse button.
  212. switch (button)
  213. {
  214. case 2:
  215. button = SDL_BUTTON_RIGHT;
  216. break;
  217. case 3:
  218. button = SDL_BUTTON_MIDDLE;
  219. break;
  220. }
  221. #if SDL_VERSION_ATLEAST(3, 0, 0)
  222. if (buttonstate & SDL_BUTTON_MASK(button))
  223. #else
  224. if (buttonstate & SDL_BUTTON(button))
  225. #endif
  226. return true;
  227. }
  228. return false;
  229. }
  230. bool Mouse::isVisible() const
  231. {
  232. #if SDL_VERSION_ATLEAST(3, 0, 0)
  233. return SDL_CursorVisible();
  234. #else
  235. return SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE;
  236. #endif
  237. }
  238. void Mouse::setGrabbed(bool grab)
  239. {
  240. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  241. if (window)
  242. window->setMouseGrab(grab);
  243. }
  244. bool Mouse::isGrabbed() const
  245. {
  246. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  247. if (window)
  248. return window->isMouseGrabbed();
  249. else
  250. return false;
  251. }
  252. bool Mouse::setRelativeMode(bool relative)
  253. {
  254. #if SDL_VERSION_ATLEAST(3, 0, 0)
  255. SDL_Window *sdlwindow = getSDLWindow();
  256. if (sdlwindow == nullptr)
  257. return false;
  258. return SDL_SetWindowRelativeMouseMode(sdlwindow, relative);
  259. #else
  260. return SDL_SetRelativeMouseMode(relative ? SDL_TRUE : SDL_FALSE) == 0;
  261. #endif
  262. }
  263. bool Mouse::getRelativeMode() const
  264. {
  265. #if SDL_VERSION_ATLEAST(3, 0, 0)
  266. SDL_Window *sdlwindow = getSDLWindow();
  267. if (sdlwindow == nullptr)
  268. return false;
  269. return SDL_GetWindowRelativeMouseMode(sdlwindow);
  270. #else
  271. return SDL_GetRelativeMouseMode() != SDL_FALSE;
  272. #endif
  273. }
  274. } // sdl
  275. } // mouse
  276. } // love