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