Skip to content

Commit

Permalink
hint for which system cursor to use as default
Browse files Browse the repository at this point in the history
Co-Authored-By: Sam Lantinga <slouken@libsdl.org>
  • Loading branch information
expikr and slouken committed Nov 25, 2024
1 parent 3c13bae commit d55e6df
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 34 deletions.
11 changes: 11 additions & 0 deletions include/SDL3/SDL_hints.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/events/SDL_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/events/SDL_mouse_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/video/android/SDL_androidmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 6 additions & 19 deletions src/video/cocoa/SDL_cocoamouse.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,6 @@ + (NSCursor *)invisibleCursor
}
@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 {
Expand Down Expand Up @@ -229,6 +210,12 @@ + (NSCursor *)invisibleCursor
}
}

static SDL_Cursor *Cocoa_CreateDefaultCursor(void)
{
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
return Cocoa_CreateSystemCursor(id);
}

static void Cocoa_FreeCursor(SDL_Cursor *cursor)
{
@autoreleasepool {
Expand Down
4 changes: 3 additions & 1 deletion src/video/emscripten/SDL_emscriptenmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/video/haiku/SDL_bvideo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/video/wayland/SDL_waylandmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions src/video/windows/SDL_windowsmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/video/x11/SDL_x11mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d55e6df

Please sign in to comment.