SDL 3.0
SDL_mouse.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 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 * # CategoryMouse
24 *
25 * Any GUI application has to deal with the mouse, and SDL provides functions
26 * to manage mouse input and the displayed cursor.
27 *
28 * Most interactions with the mouse will come through the event subsystem.
29 * Moving a mouse generates an SDL_EVENT_MOUSE_MOTION event, pushing a button
30 * generates SDL_EVENT_MOUSE_BUTTON_DOWN, etc, but one can also query the
31 * current state of the mouse at any time with SDL_GetMouseState().
32 *
33 * For certain games, it's useful to disassociate the mouse cursor from mouse
34 * input. An FPS, for example, would not want the player's motion to stop as
35 * the mouse hits the edge of the window. For these scenarios, use
36 * SDL_SetWindowRelativeMouseMode(), which hides the cursor, grabs mouse input
37 * to the window, and reads mouse input no matter how far it moves.
38 *
39 * Games that want the system to track the mouse but want to draw their own
40 * cursor can use SDL_HideCursor() and SDL_ShowCursor(). It might be more
41 * efficient to let the system manage the cursor, if possible, using
42 * SDL_SetCursor() with a custom image made through SDL_CreateColorCursor(),
43 * or perhaps just a specific system cursor from SDL_CreateSystemCursor().
44 *
45 * SDL can, on many platforms, differentiate between multiple connected mice,
46 * allowing for interesting input scenarios and multiplayer games. They can be
47 * enumerated with SDL_GetMice(), and SDL will send SDL_EVENT_MOUSE_ADDED and
48 * SDL_EVENT_MOUSE_REMOVED events as they are connected and unplugged.
49 *
50 * Since many apps only care about basic mouse input, SDL offers a virtual
51 * mouse device for touch and pen input, which often can make a desktop
52 * application work on a touchscreen phone without any code changes. Apps that
53 * care about touch/pen separately from mouse input should filter out events
54 * with a `which` field of SDL_TOUCH_MOUSEID/SDL_PEN_MOUSEID.
55 */
56
57#ifndef SDL_mouse_h_
58#define SDL_mouse_h_
59
60#include <SDL3/SDL_stdinc.h>
61#include <SDL3/SDL_error.h>
62#include <SDL3/SDL_surface.h>
63#include <SDL3/SDL_video.h>
64
65#include <SDL3/SDL_begin_code.h>
66/* Set up for C function definitions, even when using C++ */
67#ifdef __cplusplus
68extern "C" {
69#endif
70
71/**
72 * This is a unique ID for a mouse for the time it is connected to the system,
73 * and is never reused for the lifetime of the application.
74 *
75 * If the mouse is disconnected and reconnected, it will get a new ID.
76 *
77 * The value 0 is an invalid ID.
78 *
79 * \since This datatype is available since SDL 3.2.0.
80 */
82
83/**
84 * The structure used to identify an SDL cursor.
85 *
86 * This is opaque data.
87 *
88 * \since This struct is available since SDL 3.2.0.
89 */
90typedef struct SDL_Cursor SDL_Cursor;
91
92/**
93 * Cursor types for SDL_CreateSystemCursor().
94 *
95 * \since This enum is available since SDL 3.2.0.
96 */
97typedef enum SDL_SystemCursor
98{
99 SDL_SYSTEM_CURSOR_DEFAULT, /**< Default cursor. Usually an arrow. */
100 SDL_SYSTEM_CURSOR_TEXT, /**< Text selection. Usually an I-beam. */
101 SDL_SYSTEM_CURSOR_WAIT, /**< Wait. Usually an hourglass or watch or spinning ball. */
102 SDL_SYSTEM_CURSOR_CROSSHAIR, /**< Crosshair. */
103 SDL_SYSTEM_CURSOR_PROGRESS, /**< Program is busy but still interactive. Usually it's WAIT with an arrow. */
104 SDL_SYSTEM_CURSOR_NWSE_RESIZE, /**< Double arrow pointing northwest and southeast. */
105 SDL_SYSTEM_CURSOR_NESW_RESIZE, /**< Double arrow pointing northeast and southwest. */
106 SDL_SYSTEM_CURSOR_EW_RESIZE, /**< Double arrow pointing west and east. */
107 SDL_SYSTEM_CURSOR_NS_RESIZE, /**< Double arrow pointing north and south. */
108 SDL_SYSTEM_CURSOR_MOVE, /**< Four pointed arrow pointing north, south, east, and west. */
109 SDL_SYSTEM_CURSOR_NOT_ALLOWED, /**< Not permitted. Usually a slashed circle or crossbones. */
110 SDL_SYSTEM_CURSOR_POINTER, /**< Pointer that indicates a link. Usually a pointing hand. */
111 SDL_SYSTEM_CURSOR_NW_RESIZE, /**< Window resize top-left. This may be a single arrow or a double arrow like NWSE_RESIZE. */
112 SDL_SYSTEM_CURSOR_N_RESIZE, /**< Window resize top. May be NS_RESIZE. */
113 SDL_SYSTEM_CURSOR_NE_RESIZE, /**< Window resize top-right. May be NESW_RESIZE. */
114 SDL_SYSTEM_CURSOR_E_RESIZE, /**< Window resize right. May be EW_RESIZE. */
115 SDL_SYSTEM_CURSOR_SE_RESIZE, /**< Window resize bottom-right. May be NWSE_RESIZE. */
116 SDL_SYSTEM_CURSOR_S_RESIZE, /**< Window resize bottom. May be NS_RESIZE. */
117 SDL_SYSTEM_CURSOR_SW_RESIZE, /**< Window resize bottom-left. May be NESW_RESIZE. */
118 SDL_SYSTEM_CURSOR_W_RESIZE, /**< Window resize left. May be EW_RESIZE. */
119 SDL_SYSTEM_CURSOR_CONTEXT_MENU, /**< A context menu is available for the object under the cursor. */
120 SDL_SYSTEM_CURSOR_HELP, /**< Help is available for the object under the cursor. */
121 SDL_SYSTEM_CURSOR_CELL, /**< A set of cells may be selected. */
122 SDL_SYSTEM_CURSOR_VERTICAL_TEXT,/**< Text selection. May be TEXT */
123 SDL_SYSTEM_CURSOR_ALIAS, /**< A shortcut is to be created. */
124 SDL_SYSTEM_CURSOR_COPY, /**< Something is to be copied. */
125 SDL_SYSTEM_CURSOR_NO_DROP, /**< The dragged item cannot be dropped at this location. May be NOT_ALLOWED. */
126 SDL_SYSTEM_CURSOR_GRAB, /**< The object under the cursor can be grabbed */
127 SDL_SYSTEM_CURSOR_GRABBING, /**< An object is currently being grabbed. */
128 SDL_SYSTEM_CURSOR_COL_RESIZE, /**< Column resize. May be EW_RESIZE. */
129 SDL_SYSTEM_CURSOR_ROW_RESIZE, /**< Row resize. May be NS_RESIZE. */
130 SDL_SYSTEM_CURSOR_ALL_SCROLL, /**< Four pointed arrow pointing north, south, east, and west. */
132 SDL_SYSTEM_CURSOR_ZOOM_OUT, /**< Zoom out. */
135
136/**
137 * Scroll direction types for the Scroll event
138 *
139 * \since This enum is available since SDL 3.2.0.
140 */
142{
143 SDL_MOUSEWHEEL_NORMAL, /**< The scroll direction is normal */
144 SDL_MOUSEWHEEL_FLIPPED /**< The scroll direction is flipped / natural */
146
147/**
148 * Animated cursor frame info.
149 *
150 * \since This struct is available since SDL 3.4.0.
151 */
153{
154 SDL_Surface *surface; /**< The surface data for this frame */
155 Uint32 duration; /**< The frame duration in milliseconds (a duration of 0 is infinite) */
157
158/**
159 * A bitmask of pressed mouse buttons, as reported by SDL_GetMouseState, etc.
160 *
161 * - Button 1: Left mouse button
162 * - Button 2: Middle mouse button
163 * - Button 3: Right mouse button
164 * - Button 4: Side mouse button 1
165 * - Button 5: Side mouse button 2
166 *
167 * \since This datatype is available since SDL 3.2.0.
168 *
169 * \sa SDL_GetMouseState
170 * \sa SDL_GetGlobalMouseState
171 * \sa SDL_GetRelativeMouseState
172 */
174
175#define SDL_BUTTON_LEFT 1
176#define SDL_BUTTON_MIDDLE 2
177#define SDL_BUTTON_RIGHT 3
178#define SDL_BUTTON_X1 4
179#define SDL_BUTTON_X2 5
180
181#define SDL_BUTTON_MASK(X) (1u << ((X)-1))
182#define SDL_BUTTON_LMASK SDL_BUTTON_MASK(SDL_BUTTON_LEFT)
183#define SDL_BUTTON_MMASK SDL_BUTTON_MASK(SDL_BUTTON_MIDDLE)
184#define SDL_BUTTON_RMASK SDL_BUTTON_MASK(SDL_BUTTON_RIGHT)
185#define SDL_BUTTON_X1MASK SDL_BUTTON_MASK(SDL_BUTTON_X1)
186#define SDL_BUTTON_X2MASK SDL_BUTTON_MASK(SDL_BUTTON_X2)
187
188/**
189 * A callback used to transform mouse motion delta from raw values.
190 *
191 * This is called during SDL's handling of platform mouse events to scale the
192 * values of the resulting motion delta.
193 *
194 * \param userdata what was passed as `userdata` to
195 * SDL_SetRelativeMouseTransform().
196 * \param timestamp the associated time at which this mouse motion event was
197 * received.
198 * \param window the associated window to which this mouse motion event was
199 * addressed.
200 * \param mouseID the associated mouse from which this mouse motion event was
201 * emitted.
202 * \param x pointer to a variable that will be treated as the resulting x-axis
203 * motion.
204 * \param y pointer to a variable that will be treated as the resulting y-axis
205 * motion.
206 *
207 * \threadsafety This callback is called by SDL's internal mouse input
208 * processing procedure, which may be a thread separate from the
209 * main event loop that is run at realtime priority. Stalling
210 * this thread with too much work in the callback can therefore
211 * potentially freeze the entire system. Care should be taken
212 * with proper synchronization practices when adding other side
213 * effects beyond mutation of the x and y values.
214 *
215 * \since This datatype is available since SDL 3.4.0.
216 *
217 * \sa SDL_SetRelativeMouseTransform
218 */
219typedef void (SDLCALL *SDL_MouseMotionTransformCallback)(
220 void *userdata,
221 Uint64 timestamp,
223 SDL_MouseID mouseID,
224 float *x, float *y
225);
226
227/* Function prototypes */
228
229/**
230 * Return whether a mouse is currently connected.
231 *
232 * \returns true if a mouse is connected, false otherwise.
233 *
234 * \threadsafety This function should only be called on the main thread.
235 *
236 * \since This function is available since SDL 3.2.0.
237 *
238 * \sa SDL_GetMice
239 */
240extern SDL_DECLSPEC bool SDLCALL SDL_HasMouse(void);
241
242/**
243 * Get a list of currently connected mice.
244 *
245 * Note that this will include any device or virtual driver that includes
246 * mouse functionality, including some game controllers, KVM switches, etc.
247 * You should wait for input from a device before you consider it actively in
248 * use.
249 *
250 * \param count a pointer filled in with the number of mice returned, may be
251 * NULL.
252 * \returns a 0 terminated array of mouse instance IDs or NULL on failure;
253 * call SDL_GetError() for more information. This should be freed
254 * with SDL_free() when it is no longer needed.
255 *
256 * \threadsafety This function should only be called on the main thread.
257 *
258 * \since This function is available since SDL 3.2.0.
259 *
260 * \sa SDL_GetMouseNameForID
261 * \sa SDL_HasMouse
262 */
263extern SDL_DECLSPEC SDL_MouseID * SDLCALL SDL_GetMice(int *count);
264
265/**
266 * Get the name of a mouse.
267 *
268 * This function returns "" if the mouse doesn't have a name.
269 *
270 * \param instance_id the mouse instance ID.
271 * \returns the name of the selected mouse, or NULL on failure; call
272 * SDL_GetError() for more information.
273 *
274 * \threadsafety This function should only be called on the main thread.
275 *
276 * \since This function is available since SDL 3.2.0.
277 *
278 * \sa SDL_GetMice
279 */
280extern SDL_DECLSPEC const char * SDLCALL SDL_GetMouseNameForID(SDL_MouseID instance_id);
281
282/**
283 * Get the window which currently has mouse focus.
284 *
285 * \returns the window with mouse focus.
286 *
287 * \threadsafety This function should only be called on the main thread.
288 *
289 * \since This function is available since SDL 3.2.0.
290 */
291extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetMouseFocus(void);
292
293/**
294 * Query SDL's cache for the synchronous mouse button state and the
295 * window-relative SDL-cursor position.
296 *
297 * This function returns the cached synchronous state as SDL understands it
298 * from the last pump of the event queue.
299 *
300 * To query the platform for immediate asynchronous state, use
301 * SDL_GetGlobalMouseState.
302 *
303 * Passing non-NULL pointers to `x` or `y` will write the destination with
304 * respective x or y coordinates relative to the focused window.
305 *
306 * In Relative Mode, the SDL-cursor's position usually contradicts the
307 * platform-cursor's position as manually calculated from
308 * SDL_GetGlobalMouseState() and SDL_GetWindowPosition.
309 *
310 * \param x a pointer to receive the SDL-cursor's x-position from the focused
311 * window's top left corner, can be NULL if unused.
312 * \param y a pointer to receive the SDL-cursor's y-position from the focused
313 * window's top left corner, can be NULL if unused.
314 * \returns a 32-bit bitmask of the button state that can be bitwise-compared
315 * against the SDL_BUTTON_MASK(X) macro.
316 *
317 * \threadsafety This function should only be called on the main thread.
318 *
319 * \since This function is available since SDL 3.2.0.
320 *
321 * \sa SDL_GetGlobalMouseState
322 * \sa SDL_GetRelativeMouseState
323 */
324extern SDL_DECLSPEC SDL_MouseButtonFlags SDLCALL SDL_GetMouseState(float *x, float *y);
325
326/**
327 * Query the platform for the asynchronous mouse button state and the
328 * desktop-relative platform-cursor position.
329 *
330 * This function immediately queries the platform for the most recent
331 * asynchronous state, more costly than retrieving SDL's cached state in
332 * SDL_GetMouseState().
333 *
334 * Passing non-NULL pointers to `x` or `y` will write the destination with
335 * respective x or y coordinates relative to the desktop.
336 *
337 * In Relative Mode, the platform-cursor's position usually contradicts the
338 * SDL-cursor's position as manually calculated from SDL_GetMouseState() and
339 * SDL_GetWindowPosition.
340 *
341 * This function can be useful if you need to track the mouse outside of a
342 * specific window and SDL_CaptureMouse() doesn't fit your needs. For example,
343 * it could be useful if you need to track the mouse while dragging a window,
344 * where coordinates relative to a window might not be in sync at all times.
345 *
346 * \param x a pointer to receive the platform-cursor's x-position from the
347 * desktop's top left corner, can be NULL if unused.
348 * \param y a pointer to receive the platform-cursor's y-position from the
349 * desktop's top left corner, can be NULL if unused.
350 * \returns a 32-bit bitmask of the button state that can be bitwise-compared
351 * against the SDL_BUTTON_MASK(X) macro.
352 *
353 * \threadsafety This function should only be called on the main thread.
354 *
355 * \since This function is available since SDL 3.2.0.
356 *
357 * \sa SDL_CaptureMouse
358 * \sa SDL_GetMouseState
359 * \sa SDL_GetGlobalMouseState
360 */
361extern SDL_DECLSPEC SDL_MouseButtonFlags SDLCALL SDL_GetGlobalMouseState(float *x, float *y);
362
363/**
364 * Query SDL's cache for the synchronous mouse button state and accumulated
365 * mouse delta since last call.
366 *
367 * This function returns the cached synchronous state as SDL understands it
368 * from the last pump of the event queue.
369 *
370 * To query the platform for immediate asynchronous state, use
371 * SDL_GetGlobalMouseState.
372 *
373 * Passing non-NULL pointers to `x` or `y` will write the destination with
374 * respective x or y deltas accumulated since the last call to this function
375 * (or since event initialization).
376 *
377 * This function is useful for reducing overhead by processing relative mouse
378 * inputs in one go per-frame instead of individually per-event, at the
379 * expense of losing the order between events within the frame (e.g. quickly
380 * pressing and releasing a button within the same frame).
381 *
382 * \param x a pointer to receive the x mouse delta accumulated since last
383 * call, can be NULL if unused.
384 * \param y a pointer to receive the y mouse delta accumulated since last
385 * call, can be NULL if unused.
386 * \returns a 32-bit bitmask of the button state that can be bitwise-compared
387 * against the SDL_BUTTON_MASK(X) macro.
388 *
389 * \threadsafety This function should only be called on the main thread.
390 *
391 * \since This function is available since SDL 3.2.0.
392 *
393 * \sa SDL_GetMouseState
394 * \sa SDL_GetGlobalMouseState
395 */
396extern SDL_DECLSPEC SDL_MouseButtonFlags SDLCALL SDL_GetRelativeMouseState(float *x, float *y);
397
398/**
399 * Move the mouse cursor to the given position within the window.
400 *
401 * This function generates a mouse motion event if relative mode is not
402 * enabled. If relative mode is enabled, you can force mouse events for the
403 * warp by setting the SDL_HINT_MOUSE_RELATIVE_WARP_MOTION hint.
404 *
405 * Note that this function will appear to succeed, but not actually move the
406 * mouse when used over Microsoft Remote Desktop.
407 *
408 * \param window the window to move the mouse into, or NULL for the current
409 * mouse focus.
410 * \param x the x coordinate within the window.
411 * \param y the y coordinate within the window.
412 *
413 * \threadsafety This function should only be called on the main thread.
414 *
415 * \since This function is available since SDL 3.2.0.
416 *
417 * \sa SDL_WarpMouseGlobal
418 */
419extern SDL_DECLSPEC void SDLCALL SDL_WarpMouseInWindow(SDL_Window *window,
420 float x, float y);
421
422/**
423 * Move the mouse to the given position in global screen space.
424 *
425 * This function generates a mouse motion event.
426 *
427 * A failure of this function usually means that it is unsupported by a
428 * platform.
429 *
430 * Note that this function will appear to succeed, but not actually move the
431 * mouse when used over Microsoft Remote Desktop.
432 *
433 * \param x the x coordinate.
434 * \param y the y coordinate.
435 * \returns true on success or false on failure; call SDL_GetError() for more
436 * information.
437 *
438 * \threadsafety This function should only be called on the main thread.
439 *
440 * \since This function is available since SDL 3.2.0.
441 *
442 * \sa SDL_WarpMouseInWindow
443 */
444extern SDL_DECLSPEC bool SDLCALL SDL_WarpMouseGlobal(float x, float y);
445
446/**
447 * Set a user-defined function by which to transform relative mouse inputs.
448 *
449 * This overrides the relative system scale and relative speed scale hints.
450 * Should be called prior to enabling relative mouse mode, fails otherwise.
451 *
452 * \param callback a callback used to transform relative mouse motion, or NULL
453 * for default behavior.
454 * \param userdata a pointer that will be passed to `callback`.
455 * \returns true on success or false on failure; call SDL_GetError() for more
456 * information.
457 *
458 * \threadsafety This function should only be called on the main thread.
459 *
460 * \since This function is available since SDL 3.4.0.
461 */
462extern SDL_DECLSPEC bool SDLCALL SDL_SetRelativeMouseTransform(SDL_MouseMotionTransformCallback callback, void *userdata);
463
464/**
465 * Set relative mouse mode for a window.
466 *
467 * While the window has focus and relative mouse mode is enabled, the cursor
468 * is hidden, the mouse position is constrained to the window, and SDL will
469 * report continuous relative mouse motion even if the mouse is at the edge of
470 * the window.
471 *
472 * If you'd like to keep the mouse position fixed while in relative mode you
473 * can use SDL_SetWindowMouseRect(). If you'd like the cursor to be at a
474 * specific location when relative mode ends, you should use
475 * SDL_WarpMouseInWindow() before disabling relative mode.
476 *
477 * This function will flush any pending mouse motion for this window.
478 *
479 * \param window the window to change.
480 * \param enabled true to enable relative mode, false to disable.
481 * \returns true on success or false on failure; call SDL_GetError() for more
482 * information.
483 *
484 * \threadsafety This function should only be called on the main thread.
485 *
486 * \since This function is available since SDL 3.2.0.
487 *
488 * \sa SDL_GetWindowRelativeMouseMode
489 */
490extern SDL_DECLSPEC bool SDLCALL SDL_SetWindowRelativeMouseMode(SDL_Window *window, bool enabled);
491
492/**
493 * Query whether relative mouse mode is enabled for a window.
494 *
495 * \param window the window to query.
496 * \returns true if relative mode is enabled for a window or false otherwise.
497 *
498 * \threadsafety This function should only be called on the main thread.
499 *
500 * \since This function is available since SDL 3.2.0.
501 *
502 * \sa SDL_SetWindowRelativeMouseMode
503 */
504extern SDL_DECLSPEC bool SDLCALL SDL_GetWindowRelativeMouseMode(SDL_Window *window);
505
506/**
507 * Capture the mouse and to track input outside an SDL window.
508 *
509 * Capturing enables your app to obtain mouse events globally, instead of just
510 * within your window. Not all video targets support this function. When
511 * capturing is enabled, the current window will get all mouse events, but
512 * unlike relative mode, no change is made to the cursor and it is not
513 * restrained to your window.
514 *
515 * This function may also deny mouse input to other windows--both those in
516 * your application and others on the system--so you should use this function
517 * sparingly, and in small bursts. For example, you might want to track the
518 * mouse while the user is dragging something, until the user releases a mouse
519 * button. It is not recommended that you capture the mouse for long periods
520 * of time, such as the entire time your app is running. For that, you should
521 * probably use SDL_SetWindowRelativeMouseMode() or SDL_SetWindowMouseGrab(),
522 * depending on your goals.
523 *
524 * While captured, mouse events still report coordinates relative to the
525 * current (foreground) window, but those coordinates may be outside the
526 * bounds of the window (including negative values). Capturing is only allowed
527 * for the foreground window. If the window loses focus while capturing, the
528 * capture will be disabled automatically.
529 *
530 * While capturing is enabled, the current window will have the
531 * `SDL_WINDOW_MOUSE_CAPTURE` flag set.
532 *
533 * Please note that SDL will attempt to "auto capture" the mouse while the
534 * user is pressing a button; this is to try and make mouse behavior more
535 * consistent between platforms, and deal with the common case of a user
536 * dragging the mouse outside of the window. This means that if you are
537 * calling SDL_CaptureMouse() only to deal with this situation, you do not
538 * have to (although it is safe to do so). If this causes problems for your
539 * app, you can disable auto capture by setting the
540 * `SDL_HINT_MOUSE_AUTO_CAPTURE` hint to zero.
541 *
542 * \param enabled true to enable capturing, false to disable.
543 * \returns true on success or false on failure; call SDL_GetError() for more
544 * information.
545 *
546 * \threadsafety This function should only be called on the main thread.
547 *
548 * \since This function is available since SDL 3.2.0.
549 *
550 * \sa SDL_GetGlobalMouseState
551 */
552extern SDL_DECLSPEC bool SDLCALL SDL_CaptureMouse(bool enabled);
553
554/**
555 * Create a cursor using the specified bitmap data and mask (in MSB format).
556 *
557 * `mask` has to be in MSB (Most Significant Bit) format.
558 *
559 * The cursor width (`w`) must be a multiple of 8 bits.
560 *
561 * The cursor is created in black and white according to the following:
562 *
563 * - data=0, mask=1: white
564 * - data=1, mask=1: black
565 * - data=0, mask=0: transparent
566 * - data=1, mask=0: inverted color if possible, black if not.
567 *
568 * Cursors created with this function must be freed with SDL_DestroyCursor().
569 *
570 * If you want to have a color cursor, or create your cursor from an
571 * SDL_Surface, you should use SDL_CreateColorCursor(). Alternately, you can
572 * hide the cursor and draw your own as part of your game's rendering, but it
573 * will be bound to the framerate.
574 *
575 * Also, SDL_CreateSystemCursor() is available, which provides several
576 * readily-available system cursors to pick from.
577 *
578 * \param data the color value for each pixel of the cursor.
579 * \param mask the mask value for each pixel of the cursor.
580 * \param w the width of the cursor.
581 * \param h the height of the cursor.
582 * \param hot_x the x-axis offset from the left of the cursor image to the
583 * mouse x position, in the range of 0 to `w` - 1.
584 * \param hot_y the y-axis offset from the top of the cursor image to the
585 * mouse y position, in the range of 0 to `h` - 1.
586 * \returns a new cursor with the specified parameters on success or NULL on
587 * failure; call SDL_GetError() for more information.
588 *
589 * \threadsafety This function should only be called on the main thread.
590 *
591 * \since This function is available since SDL 3.2.0.
592 *
593 * \sa SDL_CreateAnimatedCursor
594 * \sa SDL_CreateColorCursor
595 * \sa SDL_CreateSystemCursor
596 * \sa SDL_DestroyCursor
597 * \sa SDL_SetCursor
598 */
599extern SDL_DECLSPEC SDL_Cursor * SDLCALL SDL_CreateCursor(const Uint8 *data,
600 const Uint8 *mask,
601 int w, int h, int hot_x,
602 int hot_y);
603
604/**
605 * Create a color cursor.
606 *
607 * If this function is passed a surface with alternate representations added
608 * with SDL_AddSurfaceAlternateImage(), the surface will be interpreted as the
609 * content to be used for 100% display scale, and the alternate
610 * representations will be used for high DPI situations if
611 * SDL_HINT_MOUSE_DPI_SCALE_CURSORS is enabled. For example, if the original
612 * surface is 32x32, then on a 2x macOS display or 200% display scale on
613 * Windows, a 64x64 version of the image will be used, if available. If a
614 * matching version of the image isn't available, the closest larger size
615 * image will be downscaled to the appropriate size and be used instead, if
616 * available. Otherwise, the closest smaller image will be upscaled and be
617 * used instead.
618 *
619 * \param surface an SDL_Surface structure representing the cursor image.
620 * \param hot_x the x position of the cursor hot spot.
621 * \param hot_y the y position of the cursor hot spot.
622 * \returns the new cursor on success or NULL on failure; call SDL_GetError()
623 * for more information.
624 *
625 * \threadsafety This function should only be called on the main thread.
626 *
627 * \since This function is available since SDL 3.2.0.
628 *
629 * \sa SDL_AddSurfaceAlternateImage
630 * \sa SDL_CreateAnimatedCursor
631 * \sa SDL_CreateCursor
632 * \sa SDL_CreateSystemCursor
633 * \sa SDL_DestroyCursor
634 * \sa SDL_SetCursor
635 */
636extern SDL_DECLSPEC SDL_Cursor * SDLCALL SDL_CreateColorCursor(SDL_Surface *surface,
637 int hot_x,
638 int hot_y);
639
640/**
641 * Create an animated color cursor.
642 *
643 * Animated cursors are composed of a sequential array of frames, specified as
644 * surfaces and durations in an array of SDL_CursorFrameInfo structs. The hot
645 * spot coordinates are universal to all frames, and all frames must have the
646 * same dimensions.
647 *
648 * Frame durations are specified in milliseconds. A duration of 0 implies an
649 * infinite frame time, and the animation will stop on that frame. To create a
650 * one-shot animation, set the duration of the last frame in the sequence to
651 * 0.
652 *
653 * If this function is passed surfaces with alternate representations added
654 * with SDL_AddSurfaceAlternateImage(), the surfaces will be interpreted as
655 * the content to be used for 100% display scale, and the alternate
656 * representations will be used for high DPI situations. For example, if the
657 * original surfaces are 32x32, then on a 2x macOS display or 200% display
658 * scale on Windows, a 64x64 version of the image will be used, if available.
659 * If a matching version of the image isn't available, the closest larger size
660 * image will be downscaled to the appropriate size and be used instead, if
661 * available. Otherwise, the closest smaller image will be upscaled and be
662 * used instead.
663 *
664 * If the underlying platform does not support animated cursors, this function
665 * will fall back to creating a static color cursor using the first frame in
666 * the sequence.
667 *
668 * \param frames an array of cursor images composing the animation.
669 * \param frame_count the number of frames in the sequence.
670 * \param hot_x the x position of the cursor hot spot.
671 * \param hot_y the y position of the cursor hot spot.
672 * \returns the new cursor on success or NULL on failure; call SDL_GetError()
673 * for more information.
674 *
675 * \threadsafety This function should only be called on the main thread.
676 *
677 * \since This function is available since SDL 3.4.0.
678 *
679 * \sa SDL_AddSurfaceAlternateImage
680 * \sa SDL_CreateCursor
681 * \sa SDL_CreateColorCursor
682 * \sa SDL_CreateSystemCursor
683 * \sa SDL_DestroyCursor
684 * \sa SDL_SetCursor
685 */
686extern SDL_DECLSPEC SDL_Cursor *SDLCALL SDL_CreateAnimatedCursor(SDL_CursorFrameInfo *frames,
687 int frame_count,
688 int hot_x,
689 int hot_y);
690
691/**
692 * Create a system cursor.
693 *
694 * \param id an SDL_SystemCursor enum value.
695 * \returns a cursor on success or NULL on failure; call SDL_GetError() for
696 * more information.
697 *
698 * \threadsafety This function should only be called on the main thread.
699 *
700 * \since This function is available since SDL 3.2.0.
701 *
702 * \sa SDL_DestroyCursor
703 */
704extern SDL_DECLSPEC SDL_Cursor * SDLCALL SDL_CreateSystemCursor(SDL_SystemCursor id);
705
706/**
707 * Set the active cursor.
708 *
709 * This function sets the currently active cursor to the specified one. If the
710 * cursor is currently visible, the change will be immediately represented on
711 * the display. SDL_SetCursor(NULL) can be used to force cursor redraw, if
712 * this is desired for any reason.
713 *
714 * \param cursor a cursor to make active.
715 * \returns true on success or false on failure; call SDL_GetError() for more
716 * information.
717 *
718 * \threadsafety This function should only be called on the main thread.
719 *
720 * \since This function is available since SDL 3.2.0.
721 *
722 * \sa SDL_GetCursor
723 */
724extern SDL_DECLSPEC bool SDLCALL SDL_SetCursor(SDL_Cursor *cursor);
725
726/**
727 * Get the active cursor.
728 *
729 * This function returns a pointer to the current cursor which is owned by the
730 * library. It is not necessary to free the cursor with SDL_DestroyCursor().
731 *
732 * \returns the active cursor or NULL if there is no mouse.
733 *
734 * \threadsafety This function should only be called on the main thread.
735 *
736 * \since This function is available since SDL 3.2.0.
737 *
738 * \sa SDL_SetCursor
739 */
740extern SDL_DECLSPEC SDL_Cursor * SDLCALL SDL_GetCursor(void);
741
742/**
743 * Get the default cursor.
744 *
745 * You do not have to call SDL_DestroyCursor() on the return value, but it is
746 * safe to do so.
747 *
748 * \returns the default cursor on success or NULL on failure; call
749 * SDL_GetError() for more information.
750 *
751 * \threadsafety This function should only be called on the main thread.
752 *
753 * \since This function is available since SDL 3.2.0.
754 */
755extern SDL_DECLSPEC SDL_Cursor * SDLCALL SDL_GetDefaultCursor(void);
756
757/**
758 * Free a previously-created cursor.
759 *
760 * Use this function to free cursor resources created with SDL_CreateCursor(),
761 * SDL_CreateColorCursor() or SDL_CreateSystemCursor().
762 *
763 * \param cursor the cursor to free.
764 *
765 * \threadsafety This function should only be called on the main thread.
766 *
767 * \since This function is available since SDL 3.2.0.
768 *
769 * \sa SDL_CreateAnimatedCursor
770 * \sa SDL_CreateColorCursor
771 * \sa SDL_CreateCursor
772 * \sa SDL_CreateSystemCursor
773 */
774extern SDL_DECLSPEC void SDLCALL SDL_DestroyCursor(SDL_Cursor *cursor);
775
776/**
777 * Show the cursor.
778 *
779 * \returns true on success or false on failure; call SDL_GetError() for more
780 * information.
781 *
782 * \threadsafety This function should only be called on the main thread.
783 *
784 * \since This function is available since SDL 3.2.0.
785 *
786 * \sa SDL_CursorVisible
787 * \sa SDL_HideCursor
788 */
789extern SDL_DECLSPEC bool SDLCALL SDL_ShowCursor(void);
790
791/**
792 * Hide the cursor.
793 *
794 * \returns true on success or false on failure; call SDL_GetError() for more
795 * information.
796 *
797 * \threadsafety This function should only be called on the main thread.
798 *
799 * \since This function is available since SDL 3.2.0.
800 *
801 * \sa SDL_CursorVisible
802 * \sa SDL_ShowCursor
803 */
804extern SDL_DECLSPEC bool SDLCALL SDL_HideCursor(void);
805
806/**
807 * Return whether the cursor is currently being shown.
808 *
809 * \returns `true` if the cursor is being shown, or `false` if the cursor is
810 * hidden.
811 *
812 * \threadsafety This function should only be called on the main thread.
813 *
814 * \since This function is available since SDL 3.2.0.
815 *
816 * \sa SDL_HideCursor
817 * \sa SDL_ShowCursor
818 */
819extern SDL_DECLSPEC bool SDLCALL SDL_CursorVisible(void);
820
821/* Ends C function definitions when using C++ */
822#ifdef __cplusplus
823}
824#endif
825#include <SDL3/SDL_close_code.h>
826
827#endif /* SDL_mouse_h_ */
SDL_MouseButtonFlags SDL_GetRelativeMouseState(float *x, float *y)
void SDL_DestroyCursor(SDL_Cursor *cursor)
SDL_MouseButtonFlags SDL_GetGlobalMouseState(float *x, float *y)
SDL_Cursor * SDL_GetCursor(void)
bool SDL_HideCursor(void)
Uint32 SDL_MouseID
Definition SDL_mouse.h:81
SDL_Window * SDL_GetMouseFocus(void)
bool SDL_GetWindowRelativeMouseMode(SDL_Window *window)
SDL_MouseButtonFlags SDL_GetMouseState(float *x, float *y)
bool SDL_SetWindowRelativeMouseMode(SDL_Window *window, bool enabled)
SDL_SystemCursor
Definition SDL_mouse.h:98
@ SDL_SYSTEM_CURSOR_CELL
Definition SDL_mouse.h:121
@ SDL_SYSTEM_CURSOR_NWSE_RESIZE
Definition SDL_mouse.h:104
@ SDL_SYSTEM_CURSOR_POINTER
Definition SDL_mouse.h:110
@ SDL_SYSTEM_CURSOR_EW_RESIZE
Definition SDL_mouse.h:106
@ SDL_SYSTEM_CURSOR_VERTICAL_TEXT
Definition SDL_mouse.h:122
@ SDL_SYSTEM_CURSOR_SE_RESIZE
Definition SDL_mouse.h:115
@ SDL_SYSTEM_CURSOR_NS_RESIZE
Definition SDL_mouse.h:107
@ SDL_SYSTEM_CURSOR_ALL_SCROLL
Definition SDL_mouse.h:130
@ SDL_SYSTEM_CURSOR_GRABBING
Definition SDL_mouse.h:127
@ SDL_SYSTEM_CURSOR_N_RESIZE
Definition SDL_mouse.h:112
@ SDL_SYSTEM_CURSOR_ALIAS
Definition SDL_mouse.h:123
@ SDL_SYSTEM_CURSOR_ZOOM_OUT
Definition SDL_mouse.h:132
@ SDL_SYSTEM_CURSOR_E_RESIZE
Definition SDL_mouse.h:114
@ SDL_SYSTEM_CURSOR_MOVE
Definition SDL_mouse.h:108
@ SDL_SYSTEM_CURSOR_NE_RESIZE
Definition SDL_mouse.h:113
@ SDL_SYSTEM_CURSOR_DEFAULT
Definition SDL_mouse.h:99
@ SDL_SYSTEM_CURSOR_NESW_RESIZE
Definition SDL_mouse.h:105
@ SDL_SYSTEM_CURSOR_HELP
Definition SDL_mouse.h:120
@ SDL_SYSTEM_CURSOR_TEXT
Definition SDL_mouse.h:100
@ SDL_SYSTEM_CURSOR_COUNT
Definition SDL_mouse.h:133
@ SDL_SYSTEM_CURSOR_W_RESIZE
Definition SDL_mouse.h:118
@ SDL_SYSTEM_CURSOR_WAIT
Definition SDL_mouse.h:101
@ SDL_SYSTEM_CURSOR_COPY
Definition SDL_mouse.h:124
@ SDL_SYSTEM_CURSOR_SW_RESIZE
Definition SDL_mouse.h:117
@ SDL_SYSTEM_CURSOR_S_RESIZE
Definition SDL_mouse.h:116
@ SDL_SYSTEM_CURSOR_GRAB
Definition SDL_mouse.h:126
@ SDL_SYSTEM_CURSOR_NW_RESIZE
Definition SDL_mouse.h:111
@ SDL_SYSTEM_CURSOR_NOT_ALLOWED
Definition SDL_mouse.h:109
@ SDL_SYSTEM_CURSOR_CONTEXT_MENU
Definition SDL_mouse.h:119
@ SDL_SYSTEM_CURSOR_ROW_RESIZE
Definition SDL_mouse.h:129
@ SDL_SYSTEM_CURSOR_NO_DROP
Definition SDL_mouse.h:125
@ SDL_SYSTEM_CURSOR_CROSSHAIR
Definition SDL_mouse.h:102
@ SDL_SYSTEM_CURSOR_ZOOM_IN
Definition SDL_mouse.h:131
@ SDL_SYSTEM_CURSOR_COL_RESIZE
Definition SDL_mouse.h:128
@ SDL_SYSTEM_CURSOR_PROGRESS
Definition SDL_mouse.h:103
void(* SDL_MouseMotionTransformCallback)(void *userdata, Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, float *x, float *y)
Definition SDL_mouse.h:219
bool SDL_CaptureMouse(bool enabled)
struct SDL_Cursor SDL_Cursor
Definition SDL_mouse.h:90
const char * SDL_GetMouseNameForID(SDL_MouseID instance_id)
SDL_Cursor * SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
bool SDL_SetRelativeMouseTransform(SDL_MouseMotionTransformCallback callback, void *userdata)
bool SDL_WarpMouseGlobal(float x, float y)
void SDL_WarpMouseInWindow(SDL_Window *window, float x, float y)
SDL_MouseID * SDL_GetMice(int *count)
SDL_Cursor * SDL_CreateAnimatedCursor(SDL_CursorFrameInfo *frames, int frame_count, int hot_x, int hot_y)
bool SDL_ShowCursor(void)
SDL_Cursor * SDL_CreateCursor(const Uint8 *data, const Uint8 *mask, int w, int h, int hot_x, int hot_y)
SDL_Cursor * SDL_CreateSystemCursor(SDL_SystemCursor id)
Uint32 SDL_MouseButtonFlags
Definition SDL_mouse.h:173
SDL_MouseWheelDirection
Definition SDL_mouse.h:142
@ SDL_MOUSEWHEEL_NORMAL
Definition SDL_mouse.h:143
@ SDL_MOUSEWHEEL_FLIPPED
Definition SDL_mouse.h:144
bool SDL_SetCursor(SDL_Cursor *cursor)
bool SDL_CursorVisible(void)
SDL_Cursor * SDL_GetDefaultCursor(void)
bool SDL_HasMouse(void)
uint8_t Uint8
Definition SDL_stdinc.h:459
uint64_t Uint64
Definition SDL_stdinc.h:517
uint32_t Uint32
Definition SDL_stdinc.h:495
struct SDL_Window SDL_Window
Definition SDL_video.h:175
static SDL_Window * window
Definition hello.c:16
SDL_Surface * surface
Definition SDL_mouse.h:154