Cleaned up Snow Leopard display mode rework. SDL-1.2
authorRyan C. Gordon <icculus@icculus.org>
Tue, 23 Aug 2011 06:04:54 -0400
branchSDL-1.2
changeset 5639 72659c5c8616
parent 5637 d39fe8e7d26a
child 5645 80e036f88a6e
Cleaned up Snow Leopard display mode rework.
src/video/quartz/SDL_QuartzVideo.h
src/video/quartz/SDL_QuartzVideo.m
--- a/src/video/quartz/SDL_QuartzVideo.h	Tue Aug 23 02:57:36 2011 -0400
+++ b/src/video/quartz/SDL_QuartzVideo.h	Tue Aug 23 06:04:54 2011 -0400
@@ -85,7 +85,6 @@
 
 /* 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 @@
 #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)
--- a/src/video/quartz/SDL_QuartzVideo.m	Tue Aug 23 02:57:36 2011 -0400
+++ b/src/video/quartz/SDL_QuartzVideo.m	Tue Aug 23 06:04:54 2011 -0400
@@ -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 @@
 };
 
 
+/* !!! 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 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 @@
         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 @@
         }
 
         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 @@
         CFNumberGetValue (
             CFDictionaryGetValue (vidmode, kCGDisplayHeight),
             kCFNumberSInt32Type, h);
+    }
 #endif
-    }
 
     /* we only care about the 32-bit modes... */
     if (*bpp != 32) {
@@ -241,9 +277,6 @@
     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 @@
     }
 #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 @@
     /* 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 @@
         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
-    }
 
     num_modes = CFArrayGetCount (mode_list);
 
@@ -403,12 +432,7 @@
                 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 @@
         }
     }
 
-#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 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
-    }
 
     return kCGErrorFailure;
 }
@@ -554,8 +576,8 @@
         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 @@
             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
-    }
+
     return best;
 }
 
@@ -618,11 +643,7 @@
         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);