Skip to content
Permalink
Browse files

Cleaned up Snow Leopard display mode rework.

  • Loading branch information
icculus committed Aug 23, 2011
1 parent 8935c3c commit 6b2c12874b539f51fd5398f1ab6e8a0de2836141
Showing with 79 additions and 60 deletions.
  1. +0 −2 src/video/quartz/SDL_QuartzVideo.h
  2. +79 −58 src/video/quartz/SDL_QuartzVideo.m
@@ -85,7 +85,6 @@ CGLContextObj QZ_GetCGLContextObj(NSOpenGLContext *nsctx);

/* Main driver structure to store required state information */
typedef struct SDL_PrivateVideoData {
BOOL snow_leopard_or_later;
BOOL allow_screensaver; /* 0 == disable screensaver */
CGDirectDisplayID display; /* 0 == main display (only support single display) */
const void *mode; /* current mode of the display */
@@ -126,7 +125,6 @@ typedef struct SDL_PrivateVideoData {
#define display_id (this->hidden->display)
#define mode (this->hidden->mode)
#define save_mode (this->hidden->save_mode)
#define snow_leopard_or_later (this->hidden->snow_leopard_or_later)
#define allow_screensaver (this->hidden->allow_screensaver)
#define gl_context (this->hidden->gl_context)
#define device_width (this->hidden->width)
@@ -24,7 +24,8 @@
#include "SDL_QuartzVideo.h"
#include "SDL_QuartzWindow.h"

#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1060 /* Fixed in Snow Leopard */
/* Fixed in Snow Leopard */
#if __MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
/*
Add methods to get at private members of NSScreen.
Since there is a bug in Apple's screen switching code
@@ -98,6 +99,44 @@ static int QZ_SetColors (_THIS, int first_color,
};


/* !!! FIXME: clean out the pre-10.6 code when it makes sense to do so. */
#define FORCE_OLD_API 0 || (MAC_OS_X_VERSION_MAX_ALLOWED < 1060)

#if FORCE_OLD_API
#undef MAC_OS_X_VERSION_MIN_REQUIRED
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_5
#endif

static inline BOOL IS_SNOW_LEOPARD_OR_LATER(_THIS)
{
#if FORCE_OLD_API
return NO;
#else
return (system_version >= 0x1060);
#endif
}

static void QZ_ReleaseDisplayMode(_THIS, const void *moderef)
{
/* we only own these references in the 10.6+ API. */
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (IS_SNOW_LEOPARD_OR_LATER(this)) {
CGDisplayModeRelease((CGDisplayModeRef) moderef);
}
#endif
}

static void QZ_ReleaseDisplayModeList(_THIS, CFArrayRef mode_list)
{
/* we only own these references in the 10.6+ API. */
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (IS_SNOW_LEOPARD_OR_LATER(this)) {
CFRelease(mode_list);
}
#endif
}


/* Bootstrap functions */
static int QZ_Available ()
{
@@ -178,14 +217,9 @@ static int QZ_Available ()

static void QZ_DeleteDevice (SDL_VideoDevice *device)
{
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
_THIS = device;
if (snow_leopard_or_later) {
CGDisplayModeRelease((CGDisplayModeRef) save_mode); /* NULL is ok */
CGDisplayModeRelease((CGDisplayModeRef) mode); /* NULL is ok */
}
#endif

QZ_ReleaseDisplayMode(this, save_mode);
QZ_ReleaseDisplayMode(this, mode);
SDL_free (device->hidden);
SDL_free (device);
}
@@ -197,8 +231,8 @@ static void QZ_GetModeInfo(_THIS, const void *_mode, Uint32 *w, Uint32 *h, Uint3
return;
}

if (snow_leopard_or_later) {
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (IS_SNOW_LEOPARD_OR_LATER(this)) {
CGDisplayModeRef vidmode = (CGDisplayModeRef) _mode;
CFStringRef fmt = CGDisplayModeCopyPixelEncoding(vidmode);

@@ -212,9 +246,11 @@ static void QZ_GetModeInfo(_THIS, const void *_mode, Uint32 *w, Uint32 *h, Uint3
}

CFRelease(fmt);
}
#endif
} else {
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)

#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
if (!IS_SNOW_LEOPARD_OR_LATER(this)) {
CFDictionaryRef vidmode = (CFDictionaryRef) _mode;
CFNumberGetValue (
CFDictionaryGetValue (vidmode, kCGDisplayBitsPerPixel),
@@ -227,8 +263,8 @@ static void QZ_GetModeInfo(_THIS, const void *_mode, Uint32 *w, Uint32 *h, Uint3
CFNumberGetValue (
CFDictionaryGetValue (vidmode, kCGDisplayHeight),
kCFNumberSInt32Type, h);
#endif
}
#endif

/* we only care about the 32-bit modes... */
if (*bpp != 32) {
@@ -241,9 +277,6 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format)
NSRect r = NSMakeRect(0.0, 0.0, 0.0, 0.0);
const char *env = NULL;

/* we don't set this from system_version; you might not have the SDK. */
snow_leopard_or_later = NO;

if ( Gestalt(gestaltSystemVersion, &system_version) != noErr )
system_version = 0;

@@ -263,15 +296,14 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format)
}
#endif

#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
if (CGDisplayCopyDisplayMode != NULL) {
snow_leopard_or_later = YES;
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (IS_SNOW_LEOPARD_OR_LATER(this)) {
save_mode = CGDisplayCopyDisplayMode(display_id);
}
#endif

#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
if (!snow_leopard_or_later) {
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
if (!IS_SNOW_LEOPARD_OR_LATER(this)) {
save_mode = CGDisplayCurrentMode(display_id);
}
#endif
@@ -296,12 +328,7 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format)
/* Gather some information that is useful to know about the display */
QZ_GetModeInfo(this, save_mode, &device_width, &device_height, &device_bpp);
if (device_bpp == 0) {
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
if (snow_leopard_or_later) {
CGDisplayModeRelease((CGDisplayModeRef) save_mode);
}
#endif

QZ_ReleaseDisplayMode(this, save_mode);
save_mode = NULL;
SDL_SetError("Unsupported display mode");
return -1;
@@ -353,15 +380,17 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format)
client_mode_list = NULL;
}

if (snow_leopard_or_later) {
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (IS_SNOW_LEOPARD_OR_LATER(this)) {
mode_list = CGDisplayCopyAllDisplayModes(display_id, NULL);
}
#endif
} else {
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)

#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
if (!IS_SNOW_LEOPARD_OR_LATER(this)) {
mode_list = CGDisplayAvailableModes(display_id);
#endif
}
#endif

num_modes = CFArrayGetCount (mode_list);

@@ -403,12 +432,7 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format)
rect = (SDL_Rect*) SDL_malloc (sizeof(**client_mode_list));

if (client_mode_list == NULL || rect == NULL) {
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
/* we own this memory in 10.6+ */
if (snow_leopard_or_later) {
CFRelease(mode_list);
}
#endif
QZ_ReleaseDisplayModeList(this, mode_list);
SDL_OutOfMemory ();
return NULL;
}
@@ -423,11 +447,7 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format)
}
}

#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
if (snow_leopard_or_later) {
CFRelease(mode_list); /* we own this memory in 10.6+ */
}
#endif
QZ_ReleaseDisplayModeList(this, mode_list);

/* Sort list largest to smallest (by area) */
{
@@ -464,15 +484,17 @@ static SDL_bool QZ_WindowPosition(_THIS, int *x, int *y)

static CGError QZ_SetDisplayMode(_THIS, const void *vidmode)
{
if (snow_leopard_or_later) {
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (IS_SNOW_LEOPARD_OR_LATER(this)) {
return CGDisplaySetDisplayMode(display_id, (CGDisplayModeRef) vidmode, NULL);
}
#endif
} else {
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)

#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
if (!IS_SNOW_LEOPARD_OR_LATER(this)) {
return CGDisplaySwitchToMode(display_id, (CFDictionaryRef) vidmode);
#endif
}
#endif

return kCGErrorFailure;
}
@@ -554,8 +576,8 @@ static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop)
return NULL;
}

if (snow_leopard_or_later) {
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (IS_SNOW_LEOPARD_OR_LATER(this)) {
/* apparently, we have to roll our own now. :/ */
CFArrayRef mode_list = CGDisplayCopyAllDisplayModes(display_id, NULL);
if (mode_list != NULL) {
@@ -575,16 +597,19 @@ static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop)
CGDisplayModeRetain((CGDisplayModeRef) best); /* NULL is ok */
CFRelease(mode_list);
}
}
#endif
} else {
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)

#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
if (!IS_SNOW_LEOPARD_OR_LATER(this)) {
boolean_t exact = 0;
best = CGDisplayBestModeForParameters(display_id, bpp, w, h, &exact);
if (!exact) {
best = NULL;
}
#endif
}
#endif

return best;
}

@@ -618,11 +643,7 @@ static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop)
goto ERR_NO_MATCH;
}

#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
if (snow_leopard_or_later) {
CGDisplayModeRelease((CGDisplayModeRef) mode); /* NULL is ok */
}
#endif
QZ_ReleaseDisplayMode(this, mode); /* NULL is okay. */

/* See if requested mode exists */
mode = QZ_BestMode(this, bpp, width, height);

0 comments on commit 6b2c128

Please sign in to comment.