SDL_mouse.h 18 KB

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