Replaced deprecated CoreGraphics calls in video mode enumeration.

This commit is contained in:
Camilla Berglund 2012-03-05 16:27:53 +01:00
parent c0db61bd6e
commit 7af82fdade
4 changed files with 69 additions and 35 deletions

View File

@ -312,6 +312,7 @@ version of GLFW.</p>
<li>[Cocoa] Added support for joysticks</li> <li>[Cocoa] Added support for joysticks</li>
<li>[Cocoa] Postponed menu creation to first window creation</li> <li>[Cocoa] Postponed menu creation to first window creation</li>
<li>[Cocoa] Replaced <code>NSDate</code> time source with <code>mach_absolute_time</code></li> <li>[Cocoa] Replaced <code>NSDate</code> time source with <code>mach_absolute_time</code></li>
<li>[Cocoa] Replaced deprecated CoreGraphics calls in video mode enumeration</li>
<li>[Cocoa] Bugfix: The loop condition for saving video modes used the wrong index variable</li> <li>[Cocoa] Bugfix: The loop condition for saving video modes used the wrong index variable</li>
<li>[Cocoa] Bugfix: The OpenGL framework was not retrieved, making glfwGetProcAddress crash</li> <li>[Cocoa] Bugfix: The OpenGL framework was not retrieved, making glfwGetProcAddress crash</li>
<li>[Cocoa] Bugfix: <code>glfwInit</code> changed the current directory for unbundled executables</li> <li>[Cocoa] Bugfix: <code>glfwInit</code> changed the current directory for unbundled executables</li>

View File

@ -34,38 +34,59 @@
// Check whether the display mode should be included in enumeration // Check whether the display mode should be included in enumeration
//======================================================================== //========================================================================
static BOOL modeIsGood(NSDictionary* mode) static GLboolean modeIsGood(CGDisplayModeRef mode)
{ {
// This is a bit controversial, if you've got something other than an uint32_t flags = CGDisplayModeGetIOFlags(mode);
// LCD computer monitor as an output device you might not want these if (!(flags & kDisplayModeValidFlag) || !(flags & kDisplayModeSafeFlag))
// checks. You might also want to reject modes which are interlaced, return GL_FALSE;
// or TV out. There is no one-size-fits-all policy that can work here.
// This seems like a decent compromise, but certain applications may if (flags & kDisplayModeInterlacedFlag)
// wish to patch this... return GL_FALSE;
return [[mode objectForKey:(id)kCGDisplayBitsPerPixel] intValue] >= 15 &&
[mode objectForKey:(id)kCGDisplayModeIsSafeForHardware] != nil && if (flags & kDisplayModeTelevisionFlag)
[mode objectForKey:(id)kCGDisplayModeIsStretched] == nil; return GL_FALSE;
if (flags & kDisplayModeStretchedFlag)
return GL_FALSE;
CFStringRef format = CGDisplayModeCopyPixelEncoding(mode);
if (CFStringCompare(format, CFSTR(IO16BitDirectPixels), 0) &&
CFStringCompare(format, CFSTR(IO32BitDirectPixels), 0))
{
CFRelease(format);
return GL_FALSE;
}
CFRelease(format);
return GL_TRUE;
} }
//======================================================================== //========================================================================
// Convert Core Graphics display mode to GLFW video mode // Convert Core Graphics display mode to GLFW video mode
//======================================================================== //========================================================================
static GLFWvidmode vidmodeFromCGDisplayMode(NSDictionary* mode) static GLFWvidmode vidmodeFromCGDisplayMode(CGDisplayModeRef mode)
{ {
unsigned int width =
[[mode objectForKey:(id)kCGDisplayWidth] unsignedIntValue];
unsigned int height =
[[mode objectForKey:(id)kCGDisplayHeight] unsignedIntValue];
unsigned int bps =
[[mode objectForKey:(id)kCGDisplayBitsPerSample] unsignedIntValue];
GLFWvidmode result; GLFWvidmode result;
result.width = width; result.width = CGDisplayModeGetWidth(mode);
result.height = height; result.height = CGDisplayModeGetHeight(mode);
result.redBits = bps;
result.greenBits = bps; CFStringRef format = CGDisplayModeCopyPixelEncoding(mode);
result.blueBits = bps;
if (CFStringCompare(format, CFSTR(IO16BitDirectPixels), 0) == 0)
{
result.redBits = 5;
result.greenBits = 5;
result.blueBits = 5;
}
else
{
result.redBits = 8;
result.greenBits = 8;
result.blueBits = 8;
}
CFRelease(format);
return result; return result;
} }
@ -80,17 +101,23 @@ static GLFWvidmode vidmodeFromCGDisplayMode(NSDictionary* mode)
int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount) int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
{ {
NSArray* modes = (NSArray*) CGDisplayAvailableModes(CGMainDisplayID()); CGDisplayModeRef mode;
unsigned int i, j = 0, n = [modes count]; CFArrayRef modes;
CFIndex count, i;
int stored = 0;
for (i = 0; i < n && j < (unsigned)maxcount; i++) modes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), NULL);
count = CFArrayGetCount(modes);
for (i = 0; i < count && stored < maxcount; i++)
{ {
NSDictionary *mode = [modes objectAtIndex:i]; mode = (CGDisplayModeRef) CFArrayGetValueAtIndex(modes, i);
if (modeIsGood(mode)) if (modeIsGood(mode))
list[j++] = vidmodeFromCGDisplayMode(mode); list[stored++] = vidmodeFromCGDisplayMode(mode);
} }
return j; CFRelease(modes);
return stored;
} }
//======================================================================== //========================================================================

View File

@ -91,8 +91,7 @@ int _glfwPlatformInit(void)
changeToResourcesDirectory(); changeToResourcesDirectory();
_glfwLibrary.NS.desktopMode = _glfwLibrary.NS.desktopMode = CGDisplayCopyDisplayMode(CGMainDisplayID());
(NSDictionary*) CGDisplayCurrentMode(CGMainDisplayID());
// Save the original gamma ramp // Save the original gamma ramp
_glfwLibrary.originalRampSize = CGDisplayGammaTableCapacity(CGMainDisplayID()); _glfwLibrary.originalRampSize = CGDisplayGammaTableCapacity(CGMainDisplayID());
@ -117,6 +116,12 @@ int _glfwPlatformTerminate(void)
// Restore the original gamma ramp // Restore the original gamma ramp
_glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp); _glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp);
if (_glfwLibrary.NS.desktopMode)
{
CFRelease(_glfwLibrary.NS.desktopMode);
_glfwLibrary.NS.desktopMode = NULL;
}
[NSApp setDelegate:nil]; [NSApp setDelegate:nil];
[_glfwLibrary.NS.delegate release]; [_glfwLibrary.NS.delegate release];
_glfwLibrary.NS.delegate = nil; _glfwLibrary.NS.delegate = nil;

View File

@ -37,6 +37,7 @@
#if defined(__OBJC__) #if defined(__OBJC__)
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#else #else
#include <ApplicationServices/ApplicationServices.h>
typedef void* id; typedef void* id;
#endif #endif
@ -90,10 +91,10 @@ typedef struct _GLFWlibraryNS
} timer; } timer;
// dlopen handle for dynamically loading OpenGL extension entry points // dlopen handle for dynamically loading OpenGL extension entry points
void* OpenGLFramework; void* OpenGLFramework;
id desktopMode; CGDisplayModeRef desktopMode;
id delegate; id delegate;
id autoreleasePool; id autoreleasePool;
} _GLFWlibraryNS; } _GLFWlibraryNS;