SDL_mouse.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  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. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * \file SDL_mouse.h
  20. *
  21. * Include file for SDL mouse event handling.
  22. */
  23. #ifndef SDL_mouse_h_
  24. #define SDL_mouse_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_video.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. typedef Uint32 SDL_MouseID;
  34. typedef struct SDL_Cursor SDL_Cursor; /**< Implementation dependent */
  35. /**
  36. * Cursor types for SDL_CreateSystemCursor().
  37. */
  38. typedef enum
  39. {
  40. SDL_SYSTEM_CURSOR_ARROW, /**< Arrow */
  41. SDL_SYSTEM_CURSOR_IBEAM, /**< I-beam */
  42. SDL_SYSTEM_CURSOR_WAIT, /**< Wait */
  43. SDL_SYSTEM_CURSOR_CROSSHAIR, /**< Crosshair */
  44. SDL_SYSTEM_CURSOR_WAITARROW, /**< Small wait cursor (or Wait if not available) */
  45. SDL_SYSTEM_CURSOR_SIZENWSE, /**< Double arrow pointing northwest and southeast */
  46. SDL_SYSTEM_CURSOR_SIZENESW, /**< Double arrow pointing northeast and southwest */
  47. SDL_SYSTEM_CURSOR_SIZEWE, /**< Double arrow pointing west and east */
  48. SDL_SYSTEM_CURSOR_SIZENS, /**< Double arrow pointing north and south */
  49. SDL_SYSTEM_CURSOR_SIZEALL, /**< Four pointed arrow pointing north, south, east, and west */
  50. SDL_SYSTEM_CURSOR_NO, /**< Slashed circle or crossbones */
  51. SDL_SYSTEM_CURSOR_HAND, /**< Hand */
  52. SDL_NUM_SYSTEM_CURSORS
  53. } SDL_SystemCursor;
  54. /**
  55. * Scroll direction types for the Scroll event
  56. */
  57. typedef enum
  58. {
  59. SDL_MOUSEWHEEL_NORMAL, /**< The scroll direction is normal */
  60. SDL_MOUSEWHEEL_FLIPPED /**< The scroll direction is flipped / natural */
  61. } SDL_MouseWheelDirection;
  62. /* Function prototypes */
  63. /**
  64. * Get the window which currently has mouse focus.
  65. *
  66. * \returns the window with mouse focus.
  67. *
  68. * \since This function is available since SDL 3.0.0.
  69. */
  70. extern DECLSPEC SDL_Window * SDLCALL SDL_GetMouseFocus(void);
  71. /**
  72. * Retrieve the current state of the mouse.
  73. *
  74. * The current button state is returned as a button bitmask, which can be
  75. * tested using the `SDL_BUTTON(X)` macros (where `X` is generally 1 for the
  76. * left, 2 for middle, 3 for the right button), and `x` and `y` are set to the
  77. * mouse cursor position relative to the focus window. You can pass NULL for
  78. * either `x` or `y`.
  79. *
  80. * \param x the x coordinate of the mouse cursor position relative to the
  81. * focus window
  82. * \param y the y coordinate of the mouse cursor position relative to the
  83. * focus window
  84. * \returns a 32-bit button bitmask of the current button state.
  85. *
  86. * \since This function is available since SDL 3.0.0.
  87. *
  88. * \sa SDL_GetGlobalMouseState
  89. * \sa SDL_GetRelativeMouseState
  90. * \sa SDL_PumpEvents
  91. */
  92. extern DECLSPEC Uint32 SDLCALL SDL_GetMouseState(float *x, float *y);
  93. /**
  94. * Get the current state of the mouse in relation to the desktop.
  95. *
  96. * This works similarly to SDL_GetMouseState(), but the coordinates will be
  97. * reported relative to the top-left of the desktop. This can be useful if you
  98. * need to track the mouse outside of a specific window and SDL_CaptureMouse()
  99. * doesn't fit your needs. For example, it could be useful if you need to
  100. * track the mouse while dragging a window, where coordinates relative to a
  101. * window might not be in sync at all times.
  102. *
  103. * Note: SDL_GetMouseState() returns the mouse position as SDL understands it
  104. * from the last pump of the event queue. This function, however, queries the
  105. * OS for the current mouse position, and as such, might be a slightly less
  106. * efficient function. Unless you know what you're doing and have a good
  107. * reason to use this function, you probably want SDL_GetMouseState() instead.
  108. *
  109. * \param x filled in with the current X coord relative to the desktop; can be
  110. * NULL
  111. * \param y filled in with the current Y coord relative to the desktop; can be
  112. * NULL
  113. * \returns the current button state as a bitmask which can be tested using
  114. * the SDL_BUTTON(X) macros.
  115. *
  116. * \since This function is available since SDL 3.0.0.
  117. *
  118. * \sa SDL_CaptureMouse
  119. */
  120. extern DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(float *x, float *y);
  121. /**
  122. * Retrieve the relative state of the mouse.
  123. *
  124. * The current button state is returned as a button bitmask, which can be
  125. * tested using the `SDL_BUTTON(X)` macros (where `X` is generally 1 for the
  126. * left, 2 for middle, 3 for the right button), and `x` and `y` are set to the
  127. * mouse deltas since the last call to SDL_GetRelativeMouseState() or since
  128. * event initialization. You can pass NULL for either `x` or `y`.
  129. *
  130. * \param x a pointer filled with the last recorded x coordinate of the mouse
  131. * \param y a pointer filled with the last recorded y coordinate of the mouse
  132. * \returns a 32-bit button bitmask of the relative button state.
  133. *
  134. * \since This function is available since SDL 3.0.0.
  135. *
  136. * \sa SDL_GetMouseState
  137. */
  138. extern DECLSPEC Uint32 SDLCALL SDL_GetRelativeMouseState(float *x, float *y);
  139. /**
  140. * Move the mouse cursor to the given position within the window.
  141. *
  142. * This function generates a mouse motion event if relative mode is not
  143. * enabled. If relative mode is enabled, you can force mouse events for the
  144. * warp by setting the SDL_HINT_MOUSE_RELATIVE_WARP_MOTION hint.
  145. *
  146. * Note that this function will appear to succeed, but not actually move the
  147. * mouse when used over Microsoft Remote Desktop.
  148. *
  149. * \param window the window to move the mouse into, or NULL for the current
  150. * mouse focus
  151. * \param x the x coordinate within the window
  152. * \param y the y coordinate within the window
  153. *
  154. * \since This function is available since SDL 3.0.0.
  155. *
  156. * \sa SDL_WarpMouseGlobal
  157. */
  158. extern DECLSPEC void SDLCALL SDL_WarpMouseInWindow(SDL_Window * window,
  159. float x, float y);
  160. /**
  161. * Move the mouse to the given position in global screen space.
  162. *
  163. * This function generates a mouse motion event.
  164. *
  165. * A failure of this function usually means that it is unsupported by a
  166. * platform.
  167. *
  168. * Note that this function will appear to succeed, but not actually move the
  169. * mouse when used over Microsoft Remote Desktop.
  170. *
  171. * \param x the x coordinate
  172. * \param y the y coordinate
  173. * \returns 0 on success or a negative error code on failure; call
  174. * SDL_GetError() for more information.
  175. *
  176. * \since This function is available since SDL 3.0.0.
  177. *
  178. * \sa SDL_WarpMouseInWindow
  179. */
  180. extern DECLSPEC int SDLCALL SDL_WarpMouseGlobal(float x, float y);
  181. /**
  182. * Set relative mouse mode.
  183. *
  184. * While the mouse is in relative mode, the cursor is hidden, the mouse
  185. * position is constrained to the window, and SDL will report continuous
  186. * relative mouse motion even if the mouse is at the edge of the window.
  187. *
  188. * This function will flush any pending mouse motion.
  189. *
  190. * \param enabled SDL_TRUE to enable relative mode, SDL_FALSE to disable.
  191. * \returns 0 on success or a negative error code on failure; call
  192. * SDL_GetError() for more information.
  193. *
  194. * \since This function is available since SDL 3.0.0.
  195. *
  196. * \sa SDL_GetRelativeMouseMode
  197. */
  198. extern DECLSPEC int SDLCALL SDL_SetRelativeMouseMode(SDL_bool enabled);
  199. /**
  200. * Capture the mouse and to track input outside an SDL window.
  201. *
  202. * Capturing enables your app to obtain mouse events globally, instead of just
  203. * within your window. Not all video targets support this function. When
  204. * capturing is enabled, the current window will get all mouse events, but
  205. * unlike relative mode, no change is made to the cursor and it is not
  206. * restrained to your window.
  207. *
  208. * This function may also deny mouse input to other windows--both those in
  209. * your application and others on the system--so you should use this function
  210. * sparingly, and in small bursts. For example, you might want to track the
  211. * mouse while the user is dragging something, until the user releases a mouse
  212. * button. It is not recommended that you capture the mouse for long periods
  213. * of time, such as the entire time your app is running. For that, you should
  214. * probably use SDL_SetRelativeMouseMode() or SDL_SetWindowGrab(), depending
  215. * on your goals.
  216. *
  217. * While captured, mouse events still report coordinates relative to the
  218. * current (foreground) window, but those coordinates may be outside the
  219. * bounds of the window (including negative values). Capturing is only allowed
  220. * for the foreground window. If the window loses focus while capturing, the
  221. * capture will be disabled automatically.
  222. *
  223. * While capturing is enabled, the current window will have the
  224. * `SDL_WINDOW_MOUSE_CAPTURE` flag set.
  225. *
  226. * Please note that as of SDL 2.0.22, SDL will attempt to "auto capture" the
  227. * mouse while the user is pressing a button; this is to try and make mouse
  228. * behavior more consistent between platforms, and deal with the common case
  229. * of a user dragging the mouse outside of the window. This means that if you
  230. * are calling SDL_CaptureMouse() only to deal with this situation, you no
  231. * longer have to (although it is safe to do so). If this causes problems for
  232. * your app, you can disable auto capture by setting the
  233. * `SDL_HINT_MOUSE_AUTO_CAPTURE` hint to zero.
  234. *
  235. * \param enabled SDL_TRUE to enable capturing, SDL_FALSE to disable.
  236. * \returns 0 on success or a negative error code on failure; call
  237. * SDL_GetError() for more information.
  238. *
  239. * \since This function is available since SDL 3.0.0.
  240. *
  241. * \sa SDL_GetGlobalMouseState
  242. */
  243. extern DECLSPEC int SDLCALL SDL_CaptureMouse(SDL_bool enabled);
  244. /**
  245. * Query whether relative mouse mode is enabled.
  246. *
  247. * \returns SDL_TRUE if relative mode is enabled or SDL_FALSE otherwise.
  248. *
  249. * \since This function is available since SDL 3.0.0.
  250. *
  251. * \sa SDL_SetRelativeMouseMode
  252. */
  253. extern DECLSPEC SDL_bool SDLCALL SDL_GetRelativeMouseMode(void);
  254. /**
  255. * Create a cursor using the specified bitmap data and mask (in MSB format).
  256. *
  257. * `mask` has to be in MSB (Most Significant Bit) format.
  258. *
  259. * The cursor width (`w`) must be a multiple of 8 bits.
  260. *
  261. * The cursor is created in black and white according to the following:
  262. *
  263. * - data=0, mask=1: white
  264. * - data=1, mask=1: black
  265. * - data=0, mask=0: transparent
  266. * - data=1, mask=0: inverted color if possible, black if not.
  267. *
  268. * Cursors created with this function must be freed with SDL_DestroyCursor().
  269. *
  270. * If you want to have a color cursor, or create your cursor from an
  271. * SDL_Surface, you should use SDL_CreateColorCursor(). Alternately, you can
  272. * hide the cursor and draw your own as part of your game's rendering, but it
  273. * will be bound to the framerate.
  274. *
  275. * Also, since SDL 2.0.0, SDL_CreateSystemCursor() is available, which
  276. * provides twelve readily available system cursors to pick from.
  277. *
  278. * \param data the color value for each pixel of the cursor
  279. * \param mask the mask value for each pixel of the cursor
  280. * \param w the width of the cursor
  281. * \param h the height of the cursor
  282. * \param hot_x the X-axis location of the upper left corner of the cursor
  283. * relative to the actual mouse position
  284. * \param hot_y the Y-axis location of the upper left corner of the cursor
  285. * relative to the actual mouse position
  286. * \returns a new cursor with the specified parameters on success or NULL on
  287. * failure; call SDL_GetError() for more information.
  288. *
  289. * \since This function is available since SDL 3.0.0.
  290. *
  291. * \sa SDL_DestroyCursor
  292. * \sa SDL_SetCursor
  293. */
  294. extern DECLSPEC SDL_Cursor *SDLCALL SDL_CreateCursor(const Uint8 * data,
  295. const Uint8 * mask,
  296. int w, int h, int hot_x,
  297. int hot_y);
  298. /**
  299. * Create a color cursor.
  300. *
  301. * \param surface an SDL_Surface structure representing the cursor image
  302. * \param hot_x the x position of the cursor hot spot
  303. * \param hot_y the y position of the cursor hot spot
  304. * \returns the new cursor on success or NULL on failure; call SDL_GetError()
  305. * for more information.
  306. *
  307. * \since This function is available since SDL 3.0.0.
  308. *
  309. * \sa SDL_CreateCursor
  310. * \sa SDL_DestroyCursor
  311. */
  312. extern DECLSPEC SDL_Cursor *SDLCALL SDL_CreateColorCursor(SDL_Surface *surface,
  313. int hot_x,
  314. int hot_y);
  315. /**
  316. * Create a system cursor.
  317. *
  318. * \param id an SDL_SystemCursor enum value
  319. * \returns a cursor on success or NULL on failure; call SDL_GetError() for
  320. * more information.
  321. *
  322. * \since This function is available since SDL 3.0.0.
  323. *
  324. * \sa SDL_DestroyCursor
  325. */
  326. extern DECLSPEC SDL_Cursor *SDLCALL SDL_CreateSystemCursor(SDL_SystemCursor id);
  327. /**
  328. * Set the active cursor.
  329. *
  330. * This function sets the currently active cursor to the specified one. If the
  331. * cursor is currently visible, the change will be immediately represented on
  332. * the display. SDL_SetCursor(NULL) can be used to force cursor redraw, if
  333. * this is desired for any reason.
  334. *
  335. * \param cursor a cursor to make active
  336. * \returns 0 on success or a negative error code on failure; call
  337. * SDL_GetError() for more information.
  338. *
  339. * \since This function is available since SDL 3.0.0.
  340. *
  341. * \sa SDL_CreateCursor
  342. * \sa SDL_GetCursor
  343. */
  344. extern DECLSPEC int SDLCALL SDL_SetCursor(SDL_Cursor * cursor);
  345. /**
  346. * Get the active cursor.
  347. *
  348. * This function returns a pointer to the current cursor which is owned by the
  349. * library. It is not necessary to free the cursor with SDL_DestroyCursor().
  350. *
  351. * \returns the active cursor or NULL if there is no mouse.
  352. *
  353. * \since This function is available since SDL 3.0.0.
  354. *
  355. * \sa SDL_SetCursor
  356. */
  357. extern DECLSPEC SDL_Cursor *SDLCALL SDL_GetCursor(void);
  358. /**
  359. * Get the default cursor.
  360. *
  361. * You do not have to call SDL_DestroyCursor() on the return value, but it is
  362. * safe to do so.
  363. *
  364. * \returns the default cursor on success or NULL on failure.
  365. *
  366. * \since This function is available since SDL 3.0.0.
  367. *
  368. * \sa SDL_CreateSystemCursor
  369. */
  370. extern DECLSPEC SDL_Cursor *SDLCALL SDL_GetDefaultCursor(void);
  371. /**
  372. * Free a previously-created cursor.
  373. *
  374. * Use this function to free cursor resources created with SDL_CreateCursor(),
  375. * SDL_CreateColorCursor() or SDL_CreateSystemCursor().
  376. *
  377. * \param cursor the cursor to free
  378. *
  379. * \since This function is available since SDL 3.0.0.
  380. *
  381. * \sa SDL_CreateColorCursor
  382. * \sa SDL_CreateCursor
  383. * \sa SDL_CreateSystemCursor
  384. */
  385. extern DECLSPEC void SDLCALL SDL_DestroyCursor(SDL_Cursor * cursor);
  386. /**
  387. * Show the cursor.
  388. *
  389. * \returns 0 on success or a negative error code on failure; call
  390. * SDL_GetError() for more information.
  391. *
  392. * \since This function is available since SDL 3.0.0.
  393. *
  394. * \sa SDL_CursorVisible
  395. * \sa SDL_HideCursor
  396. */
  397. extern DECLSPEC int SDLCALL SDL_ShowCursor(void);
  398. /**
  399. * Hide the cursor.
  400. *
  401. * \returns 0 on success or a negative error code on failure; call
  402. * SDL_GetError() for more information.
  403. *
  404. * \since This function is available since SDL 3.0.0.
  405. *
  406. * \sa SDL_CursorVisible
  407. * \sa SDL_ShowCursor
  408. */
  409. extern DECLSPEC int SDLCALL SDL_HideCursor(void);
  410. /**
  411. * Return whether the cursor is currently being shown.
  412. *
  413. * \returns `SDL_TRUE` if the cursor is being shown, or `SDL_FALSE` if the
  414. * cursor is hidden.
  415. *
  416. * \since This function is available since SDL 3.0.0.
  417. *
  418. * \sa SDL_HideCursor
  419. * \sa SDL_ShowCursor
  420. */
  421. extern DECLSPEC SDL_bool SDLCALL SDL_CursorVisible(void);
  422. /**
  423. * Used as a mask when testing buttons in buttonstate.
  424. *
  425. * - Button 1: Left mouse button
  426. * - Button 2: Middle mouse button
  427. * - Button 3: Right mouse button
  428. */
  429. #define SDL_BUTTON(X) (1 << ((X)-1))
  430. #define SDL_BUTTON_LEFT 1
  431. #define SDL_BUTTON_MIDDLE 2
  432. #define SDL_BUTTON_RIGHT 3
  433. #define SDL_BUTTON_X1 4
  434. #define SDL_BUTTON_X2 5
  435. #define SDL_BUTTON_LMASK SDL_BUTTON(SDL_BUTTON_LEFT)
  436. #define SDL_BUTTON_MMASK SDL_BUTTON(SDL_BUTTON_MIDDLE)
  437. #define SDL_BUTTON_RMASK SDL_BUTTON(SDL_BUTTON_RIGHT)
  438. #define SDL_BUTTON_X1MASK SDL_BUTTON(SDL_BUTTON_X1)
  439. #define SDL_BUTTON_X2MASK SDL_BUTTON(SDL_BUTTON_X2)
  440. /* Ends C function definitions when using C++ */
  441. #ifdef __cplusplus
  442. }
  443. #endif
  444. #include <SDL3/SDL_close_code.h>
  445. #endif /* SDL_mouse_h_ */