x11: Minor cleanup in new mouse cursor selection code.

- Move legacy name choice to a separate function, so we can `return` a
  string in one line instead of assign a variable and `break` for each item.
- Have the case statement cover SDL_NUM_SYSTEM_CURSORS, and not `default`, so
  compiler will (maybe) warn us if an enum value is added but not included
  here.
- Only choose a legacy name if necessary.

(cherry picked from commit df00a7dd4c3deb03839e799187a3c75fc4e8854b)
main
Ryan C. Gordon 2024-02-16 08:37:23 -05:00
parent b986bc8be9
commit 6472e36264
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 47 additions and 83 deletions

View File

@ -213,97 +213,61 @@ static SDL_Cursor *X11_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
return cursor;
}
static unsigned int GetLegacySystemCursorShape(SDL_SystemCursor id)
{
switch (id) {
/* X Font Cursors reference: */
/* http://tronche.com/gui/x/xlib/appendix/b/ */
case SDL_SYSTEM_CURSOR_ARROW: return XC_left_ptr;
case SDL_SYSTEM_CURSOR_IBEAM: return XC_xterm;
case SDL_SYSTEM_CURSOR_WAIT: return XC_watch;
case SDL_SYSTEM_CURSOR_CROSSHAIR: return XC_tcross;
case SDL_SYSTEM_CURSOR_WAITARROW: return XC_watch;
case SDL_SYSTEM_CURSOR_SIZENWSE: return XC_top_left_corner;
case SDL_SYSTEM_CURSOR_SIZENESW: return XC_top_right_corner;
case SDL_SYSTEM_CURSOR_SIZEWE: return XC_sb_h_double_arrow;
case SDL_SYSTEM_CURSOR_SIZENS: return XC_sb_v_double_arrow;
case SDL_SYSTEM_CURSOR_SIZEALL: return XC_fleur;
case SDL_SYSTEM_CURSOR_NO: return XC_pirate;
case SDL_SYSTEM_CURSOR_HAND: return XC_hand2;
case SDL_SYSTEM_CURSOR_WINDOW_TOPLEFT: return XC_top_left_corner;
case SDL_SYSTEM_CURSOR_WINDOW_TOP: return XC_top_side;
case SDL_SYSTEM_CURSOR_WINDOW_TOPRIGHT: return XC_top_right_corner;
case SDL_SYSTEM_CURSOR_WINDOW_RIGHT: return XC_right_side;
case SDL_SYSTEM_CURSOR_WINDOW_BOTTOMRIGHT: return XC_bottom_right_corner;
case SDL_SYSTEM_CURSOR_WINDOW_BOTTOM: return XC_bottom_side;
case SDL_SYSTEM_CURSOR_WINDOW_BOTTOMLEFT: return XC_bottom_left_corner;
case SDL_SYSTEM_CURSOR_WINDOW_LEFT: return XC_left_side;
case SDL_NUM_SYSTEM_CURSORS: break; /* so the compiler might notice if an enum value is missing here. */
}
SDL_assert(0);
return 0;
}
static SDL_Cursor *X11_CreateSystemCursor(SDL_SystemCursor id)
{
SDL_Cursor *cursor = NULL;
unsigned int shape = 0;
const char *xcursorname = SDL_GetCSSCursorName(id, NULL);
switch (id) {
default:
SDL_assert(0);
return NULL;
/* X Font Cursors reference: */
/* http://tronche.com/gui/x/xlib/appendix/b/ */
case SDL_SYSTEM_CURSOR_ARROW:
shape = XC_left_ptr;
break;
case SDL_SYSTEM_CURSOR_IBEAM:
shape = XC_xterm;
break;
case SDL_SYSTEM_CURSOR_WAIT:
shape = XC_watch;
break;
case SDL_SYSTEM_CURSOR_CROSSHAIR:
shape = XC_tcross;
break;
case SDL_SYSTEM_CURSOR_WAITARROW:
shape = XC_watch;
break;
case SDL_SYSTEM_CURSOR_SIZENWSE:
shape = XC_top_left_corner;
break;
case SDL_SYSTEM_CURSOR_SIZENESW:
shape = XC_top_right_corner;
break;
case SDL_SYSTEM_CURSOR_SIZEWE:
shape = XC_sb_h_double_arrow;
break;
case SDL_SYSTEM_CURSOR_SIZENS:
shape = XC_sb_v_double_arrow;
break;
case SDL_SYSTEM_CURSOR_SIZEALL:
shape = XC_fleur;
break;
case SDL_SYSTEM_CURSOR_NO:
shape = XC_pirate;
break;
case SDL_SYSTEM_CURSOR_HAND:
shape = XC_hand2;
break;
case SDL_SYSTEM_CURSOR_WINDOW_TOPLEFT:
shape = XC_top_left_corner;
break;
case SDL_SYSTEM_CURSOR_WINDOW_TOP:
shape = XC_top_side;
break;
case SDL_SYSTEM_CURSOR_WINDOW_TOPRIGHT:
shape = XC_top_right_corner;
break;
case SDL_SYSTEM_CURSOR_WINDOW_RIGHT:
shape = XC_right_side;
break;
case SDL_SYSTEM_CURSOR_WINDOW_BOTTOMRIGHT:
shape = XC_bottom_right_corner;
break;
case SDL_SYSTEM_CURSOR_WINDOW_BOTTOM:
shape = XC_bottom_side;
break;
case SDL_SYSTEM_CURSOR_WINDOW_BOTTOMLEFT:
shape = XC_bottom_left_corner;
break;
case SDL_SYSTEM_CURSOR_WINDOW_LEFT:
shape = XC_left_side;
break;
}
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
Display *dpy = GetDisplay();
Cursor x11_cursor = None;
Display *dpy = GetDisplay();
Cursor x11_cursor = None;
#ifdef SDL_VIDEO_DRIVER_X11_XCURSOR
SDL_assert(xcursorname != NULL);
if (SDL_X11_HAVE_XCURSOR) {
x11_cursor = X11_XcursorLibraryLoadCursor(dpy, xcursorname);
}
if (SDL_X11_HAVE_XCURSOR) {
x11_cursor = X11_XcursorLibraryLoadCursor(dpy, SDL_GetCSSCursorName(id, NULL));
}
#endif
if (x11_cursor == None) {
x11_cursor = X11_XCreateFontCursor(dpy, shape);
}
if (x11_cursor == None) {
x11_cursor = X11_XCreateFontCursor(dpy, GetLegacySystemCursorShape(id));
}
cursor->driverdata = (void *)(uintptr_t)x11_cursor;
if (x11_cursor != None) {
cursor = SDL_calloc(1, sizeof(*cursor));
if (!cursor) {
SDL_OutOfMemory();
} else {
cursor->driverdata = (void *)(uintptr_t)x11_cursor;
}
}
return cursor;