Skip to content

Commit

Permalink
Renamed SDL_DisplayMode driverdata to internal
Browse files Browse the repository at this point in the history
This is for consistency with SDL_Surface and gives it a type so we don't have to do casts in SDL code.

I considered switching to an ID and hashing the driver data, etc. but all of that involved a lot of internal code churn and this solution gives us flexibility in how we handle this in the future.

Fixes libsdl-org#10198
  • Loading branch information
slouken committed Jul 16, 2024
1 parent 199e692 commit 9e35629
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
7 changes: 6 additions & 1 deletion include/SDL3/SDL_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ typedef enum SDL_SystemTheme
SDL_SYSTEM_THEME_DARK /**< Dark colored system theme */
} SDL_SystemTheme;

/* Internal display mode data */
typedef struct SDL_DisplayModeData SDL_DisplayModeData;

/**
* The structure that defines a display mode.
*
Expand All @@ -95,7 +98,9 @@ typedef struct SDL_DisplayMode
float refresh_rate; /**< refresh rate (or 0.0f for unspecified) */
int refresh_rate_numerator; /**< precise refresh rate numerator (or 0 for unspecified) */
int refresh_rate_denominator; /**< precise refresh rate denominator */
void *driverdata; /**< driver-specific data, initialize to 0 */

SDL_DisplayModeData *internal; /**< Private */

} SDL_DisplayMode;

/**
Expand Down
12 changes: 6 additions & 6 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,8 @@ void SDL_DelVideoDisplay(SDL_DisplayID displayID, SDL_bool send_event)
SDL_DestroyProperties(display->props);
SDL_free(display->name);
SDL_ResetFullscreenDisplayModes(display);
SDL_free(display->desktop_mode.driverdata);
display->desktop_mode.driverdata = NULL;
SDL_free(display->desktop_mode.internal);
display->desktop_mode.internal = NULL;
SDL_free(display->driverdata);
display->driverdata = NULL;
SDL_free(display);
Expand Down Expand Up @@ -1216,8 +1216,8 @@ void SDL_ResetFullscreenDisplayModes(SDL_VideoDisplay *display)
int i;

for (i = display->num_fullscreen_modes; i--;) {
SDL_free(display->fullscreen_modes[i].driverdata);
display->fullscreen_modes[i].driverdata = NULL;
SDL_free(display->fullscreen_modes[i].internal);
display->fullscreen_modes[i].internal = NULL;
}
SDL_free(display->fullscreen_modes);
display->fullscreen_modes = NULL;
Expand Down Expand Up @@ -1335,8 +1335,8 @@ void SDL_SetDesktopDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode

SDL_copyp(&last_mode, &display->desktop_mode);

if (display->desktop_mode.driverdata) {
SDL_free(display->desktop_mode.driverdata);
if (display->desktop_mode.internal) {
SDL_free(display->desktop_mode.internal);
}
SDL_copyp(&display->desktop_mode, mode);
display->desktop_mode.displayID = display->id;
Expand Down
10 changes: 5 additions & 5 deletions src/video/cocoa/SDL_cocoamodes.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ static SDL_bool GetDisplayMode(SDL_VideoDevice *_this, CGDisplayModeRef vidmode,
mode->h = (int)height;
mode->pixel_density = (float)pixelW / width;
mode->refresh_rate = refreshrate;
mode->driverdata = data;
mode->internal = data;
return SDL_TRUE;
}

Expand Down Expand Up @@ -488,7 +488,7 @@ int Cocoa_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display)

if (GetDisplayMode(_this, moderef, SDL_FALSE, modes, link, &mode)) {
if (!SDL_AddFullscreenDisplayMode(display, &mode)) {
CFRelease(((SDL_DisplayModeData *)mode.driverdata)->modes);
CFRelease(mode.internal->modes);
SDL_free(mode.driverdata);
}
}
Expand Down Expand Up @@ -522,7 +522,7 @@ static CGError SetDisplayModeForDisplay(CGDirectDisplayID display, SDL_DisplayMo
int Cocoa_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
{
SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
SDL_DisplayModeData *data = mode->internal;
CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
CGError result = kCGErrorSuccess;

Expand Down Expand Up @@ -568,11 +568,11 @@ void Cocoa_QuitModes(SDL_VideoDevice *_this)
Cocoa_SetDisplayMode(_this, display, &display->desktop_mode);
}

mode = (SDL_DisplayModeData *)display->desktop_mode.driverdata;
mode = display->desktop_mode.internal;
CFRelease(mode->modes);

for (j = 0; j < display->num_fullscreen_modes; j++) {
mode = (SDL_DisplayModeData *)display->fullscreen_modes[j].driverdata;
mode = display->fullscreen_modes[j].internal;
CFRelease(mode->modes);
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/video/haiku/SDL_bmodes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ static SDL_INLINE SDL_BLooper *_GetBeLooper() {

static SDL_INLINE display_mode * _ExtractBMode(SDL_DisplayMode *mode) {
#if WRAP_BMODE
return ((SDL_DisplayModeData *)mode->driverdata)->bmode;
return mode->internal->bmode;
#else
return (display_mode *)(mode->driverdata);
return (display_mode *)mode->internal;
#endif
}

Expand Down Expand Up @@ -173,14 +173,12 @@ static void _BDisplayModeToSdlDisplayMode(display_mode *bmode, SDL_DisplayMode *
get_refresh_rate(*bmode, &mode->refresh_rate_numerator, &mode->refresh_rate_denominator);

#if WRAP_BMODE
SDL_DisplayModeData *data = (SDL_DisplayModeData*)SDL_calloc(1,
sizeof(SDL_DisplayModeData));
SDL_DisplayModeData *data = (SDL_DisplayModeData*)SDL_calloc(1, sizeof(SDL_DisplayModeData));
data->bmode = bmode;

mode->driverdata = data;

mode->internal = data;
#else
mode->driverdata = bmode;
mode->internal = bmode;
#endif

/* Set the format */
Expand Down
8 changes: 4 additions & 4 deletions src/video/kmsdrm/SDL_kmsdrmvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ static drmModeModeInfo *KMSDRM_GetClosestDisplayMode(SDL_VideoDisplay *display,

closest = SDL_GetClosestFullscreenDisplayMode(display->id, width, height, 0.0f, SDL_FALSE);
if (closest) {
const SDL_DisplayModeData *modedata = (const SDL_DisplayModeData *)closest->driverdata;
const SDL_DisplayModeData *modedata = closest->internal;
drm_mode = &connector->modes[modedata->mode_index];
return drm_mode;
} else {
Expand Down Expand Up @@ -962,7 +962,7 @@ static void KMSDRM_AddDisplay(SDL_VideoDevice *_this, drmModeConnector *connecto
display.desktop_mode.h = dispdata->mode.vdisplay;
CalculateRefreshRate(&dispdata->mode, &display.desktop_mode.refresh_rate_numerator, &display.desktop_mode.refresh_rate_denominator);
display.desktop_mode.format = SDL_PIXELFORMAT_ARGB8888;
display.desktop_mode.driverdata = modedata;
display.desktop_mode.internal = modedata;

/* Add the display to the list of SDL displays. */
display_id = SDL_AddVideoDisplay(&display, SDL_FALSE);
Expand Down Expand Up @@ -1422,7 +1422,7 @@ int KMSDRM_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display)
mode.h = conn->modes[i].vdisplay;
CalculateRefreshRate(&conn->modes[i], &mode.refresh_rate_numerator, &mode.refresh_rate_denominator);
mode.format = SDL_PIXELFORMAT_ARGB8888;
mode.driverdata = modedata;
mode.internal = modedata;

if (!SDL_AddFullscreenDisplayMode(display, &mode)) {
SDL_free(modedata);
Expand All @@ -1438,7 +1438,7 @@ int KMSDRM_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL

SDL_VideoData *viddata = _this->driverdata;
SDL_DisplayData *dispdata = display->driverdata;
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
SDL_DisplayModeData *modedata = mode->internal;
drmModeConnector *conn = dispdata->connector;
int i;

Expand Down
6 changes: 3 additions & 3 deletions src/video/windows/SDL_windowsmodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

static void WIN_UpdateDisplayMode(SDL_VideoDevice *_this, LPCWSTR deviceName, DWORD index, SDL_DisplayMode *mode)
{
SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->internal;
HDC hdc;

data->DeviceMode.dmFields = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS);
Expand Down Expand Up @@ -288,7 +288,7 @@ static SDL_bool WIN_GetDisplayMode(SDL_VideoDevice *_this, void *dxgi_output, HM
}

SDL_zerop(mode);
mode->driverdata = data;
mode->internal = data;
data->DeviceMode = devmode;

mode->format = SDL_PIXELFORMAT_UNKNOWN;
Expand Down Expand Up @@ -825,7 +825,7 @@ static void WIN_LogMonitor(SDL_VideoDevice *_this, HMONITOR mon)
int WIN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
{
SDL_DisplayData *displaydata = display->driverdata;
SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->internal;
LONG status;

#ifdef DEBUG_MODES
Expand Down
8 changes: 4 additions & 4 deletions src/video/x11/SDL_x11modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ static SDL_bool SetXRandRModeInfo(Display *display, XRRScreenResources *res, RRC
mode->h = (info->height * scale_h + 0xffff) >> 16;
}
CalculateXRandRRefreshRate(info, &mode->refresh_rate_numerator, &mode->refresh_rate_denominator);
((SDL_DisplayModeData *)mode->driverdata)->xrandr_mode = modeID;
mode->internal->xrandr_mode = modeID;
#ifdef X11MODES_DEBUG
printf("XRandR mode %d: %dx%d@%d/%dHz\n", (int)modeID,
mode->screen_w, mode->screen_h, mode->refresh_rate_numerator, mode->refresh_rate_denominator);
Expand Down Expand Up @@ -610,7 +610,7 @@ static int X11_AddXRandRDisplay(SDL_VideoDevice *_this, Display *dpy, int screen
}

modedata->xrandr_mode = modeID;
mode.driverdata = modedata;
mode.internal = modedata;

displaydata->screen = screen;
displaydata->visual = vinfo.visual;
Expand Down Expand Up @@ -899,7 +899,7 @@ int X11_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *sdl_display)
if (!modedata) {
continue;
}
mode.driverdata = modedata;
mode.internal = modedata;

if (!SetXRandRModeInfo(display, res, output_info->crtc, output_info->modes[i], &mode) ||
!SDL_AddFullscreenDisplayMode(sdl_display, &mode)) {
Expand Down Expand Up @@ -950,7 +950,7 @@ int X11_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *sdl_display, SD
#ifdef SDL_VIDEO_DRIVER_X11_XRANDR
if (data->use_xrandr) {
Display *display = viddata->display;
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
SDL_DisplayModeData *modedata = mode->internal;
int mm_width, mm_height;
XRRScreenResources *res;
XRROutputInfo *output_info;
Expand Down

0 comments on commit 9e35629

Please sign in to comment.