SDL_mouse.h 15 KB

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