hint for which system cursor to use as default

Co-Authored-By: Sam Lantinga <slouken@libsdl.org>
This commit is contained in:
expikr 2024-11-25 10:54:27 +08:00 committed by Sam Lantinga
parent 3c13bae64f
commit d55e6dfc5e
10 changed files with 54 additions and 34 deletions

View file

@ -2401,6 +2401,17 @@ extern "C" {
*/
#define SDL_HINT_MOUSE_DOUBLE_CLICK_TIME "SDL_MOUSE_DOUBLE_CLICK_TIME"
/**
* A variable setting which system cursor to use as the default cursor.
* This should be an integer corresponding to the SDL_SystemCursor enum.
* The default value is zero (SDL_SYSTEM_CURSOR_DEFAULT).
*
* This hint needs to be set before SDL_Init().
*
* \since This hint is available since SDL 3.1.3.
*/
#define SDL_HINT_MOUSE_DEFAULT_SYSTEM_CURSOR "SDL_MOUSE_DEFAULT_SYSTEM_CURSOR"
/**
* A variable controlling whether warping a hidden mouse cursor will activate
* relative mouse mode.

View file

@ -440,6 +440,19 @@ void SDL_SetDefaultCursor(SDL_Cursor *cursor)
}
}
SDL_SystemCursor SDL_GetDefaultSystemCursor(void)
{
SDL_SystemCursor id = SDL_SYSTEM_CURSOR_DEFAULT;
const char *value = SDL_GetHint(SDL_HINT_MOUSE_DEFAULT_SYSTEM_CURSOR);
if (value) {
int index = SDL_atoi(value);
if (0 <= index && index < (int)SDL_SYSTEM_CURSOR_COUNT) {
id = (SDL_SystemCursor)index;
}
}
return id;
}
SDL_Mouse *SDL_GetMouse(void)
{
return &SDL_mouse;

View file

@ -158,6 +158,9 @@ extern SDL_Mouse *SDL_GetMouse(void);
// Set the default mouse cursor
extern void SDL_SetDefaultCursor(SDL_Cursor *cursor);
// Get the preferred default system cursor
extern SDL_SystemCursor SDL_GetDefaultSystemCursor(void);
// Set the mouse focus window
extern void SDL_SetMouseFocus(SDL_Window *window);

View file

@ -75,7 +75,8 @@ static SDL_Cursor *Android_WrapCursor(int custom_cursor, int system_cursor)
static SDL_Cursor *Android_CreateDefaultCursor(void)
{
return Android_WrapCursor(0, SDL_SYSTEM_CURSOR_DEFAULT);
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
return Android_WrapCursor(0, id);
}
static SDL_Cursor *Android_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)

View file

@ -63,25 +63,6 @@
}
@end
static SDL_Cursor *Cocoa_CreateDefaultCursor(void)
{
@autoreleasepool {
NSCursor *nscursor;
SDL_Cursor *cursor = NULL;
nscursor = [NSCursor arrowCursor];
if (nscursor) {
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
cursor->internal = (void *)CFBridgingRetain(nscursor);
}
}
return cursor;
}
}
static SDL_Cursor *Cocoa_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
{
@autoreleasepool {
@ -229,6 +210,12 @@ static SDL_Cursor *Cocoa_CreateSystemCursor(SDL_SystemCursor id)
}
}
static SDL_Cursor *Cocoa_CreateDefaultCursor(void)
{
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
return Cocoa_CreateSystemCursor(id);
}
static void Cocoa_FreeCursor(SDL_Cursor *cursor)
{
@autoreleasepool {

View file

@ -62,7 +62,9 @@ static SDL_Cursor *Emscripten_CreateCursorFromString(const char *cursor_str, boo
static SDL_Cursor *Emscripten_CreateDefaultCursor(void)
{
return Emscripten_CreateCursorFromString("default", false);
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
const char *cursor_name = SDL_GetCSSCursorName(id, NULL);
return Emscripten_CreateCursorFromString(cursor_name, false);
}
EM_JS_DEPS(sdlmouse, "$stringToUTF8,$UTF8ToString");

View file

@ -180,7 +180,8 @@ static SDL_Cursor * HAIKU_CreateSystemCursor(SDL_SystemCursor id)
static SDL_Cursor * HAIKU_CreateDefaultCursor()
{
return HAIKU_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
return HAIKU_CreateSystemCursor(id);
}
static void HAIKU_FreeCursor(SDL_Cursor * cursor)

View file

@ -592,7 +592,8 @@ static SDL_Cursor *Wayland_CreateSystemCursor(SDL_SystemCursor id)
static SDL_Cursor *Wayland_CreateDefaultCursor(void)
{
return Wayland_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
return Wayland_CreateSystemCursor(id);
}
static void Wayland_FreeCursorData(SDL_CursorData *d)

View file

@ -84,11 +84,6 @@ static SDL_Cursor *WIN_CreateCursorAndData(HCURSOR hcursor)
return cursor;
}
static SDL_Cursor *WIN_CreateDefaultCursor(void)
{
return WIN_CreateCursorAndData(LoadCursor(NULL, IDC_ARROW));
}
static bool IsMonochromeSurface(SDL_Surface *surface)
{
int x, y;
@ -342,6 +337,12 @@ static SDL_Cursor *WIN_CreateSystemCursor(SDL_SystemCursor id)
return WIN_CreateCursorAndData(LoadCursor(NULL, name));
}
static SDL_Cursor *WIN_CreateDefaultCursor(void)
{
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
return WIN_CreateSystemCursor(id);
}
static void WIN_FreeCursor(SDL_Cursor *cursor)
{
SDL_CursorData *data = cursor->internal;

View file

@ -89,12 +89,6 @@ static SDL_Cursor *X11_CreateCursorAndData(Cursor x11_cursor)
return cursor;
}
static SDL_Cursor *X11_CreateDefaultCursor(void)
{
// None is used to indicate the default cursor
return X11_CreateCursorAndData(None);
}
#ifdef SDL_VIDEO_DRIVER_X11_XCURSOR
static Cursor X11_CreateXCursorCursor(SDL_Surface *surface, int hot_x, int hot_y)
{
@ -279,6 +273,12 @@ static SDL_Cursor *X11_CreateSystemCursor(SDL_SystemCursor id)
return cursor;
}
static SDL_Cursor *X11_CreateDefaultCursor(void)
{
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
return X11_CreateSystemCursor(id);
}
static void X11_FreeCursor(SDL_Cursor *cursor)
{
Cursor x11_cursor = cursor->internal->cursor;