From 8f7540ce4330cb44fc5fccd89667682be2bfb2c6 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 16 Jan 2006 19:52:40 +0000 Subject: [PATCH] Mac OS X: Ported OpenGL library code to use frameworks instead of accessing libGL.dylib directly --- src/native/macosx/context.m | 46 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/native/macosx/context.m b/src/native/macosx/context.m index 093057b0..07d87dac 100644 --- a/src/native/macosx/context.m +++ b/src/native/macosx/context.m @@ -37,36 +37,41 @@ * @version $Revision$ */ +#import #import "context.h" -#import -#import +static CFBundleRef opengl_bundle = NULL; -#include -static const struct mach_header *opengl_lib_handle = NULL; - -void *extgl_GetProcAddress(const char *name) -{ - #define BUFFER_SIZE 1024 - char mach_name[BUFFER_SIZE] = "_"; - strncat(mach_name, name, BUFFER_SIZE - 1); - - NSSymbol sym = NSLookupSymbolInImage(opengl_lib_handle, mach_name, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); - void *address = NSAddressOfSymbol(sym); +void *extgl_GetProcAddress(const char *name) { + CFStringRef cf_name = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8); + void *address = CFBundleGetFunctionPointerForName(opengl_bundle, cf_name); + CFRelease(cf_name); if (address == NULL) printfDebug("Could not locate symbol %s\n", name); return address; } -static const struct mach_header *loadImage(const char *lib_name) { - return NSAddImage(lib_name, NSADDIMAGE_OPTION_RETURN_ON_ERROR); +static CFBundleRef loadFramework(JNIEnv *env) { + CFStringRef framework_path = CFSTR("/System/Library/Frameworks/OpenGL.framework"); + if (framework_path == NULL) { + printfDebugJava(env, "Failed to allocate string"); + return NULL; + } + CFURLRef url = CFURLCreateWithFileSystemPath(NULL, framework_path, kCFURLPOSIXPathStyle, TRUE); + if (url == NULL) { + printfDebugJava(env, "Failed to allocate URL"); + return NULL; + } + CFBundleRef opengl_bundle = CFBundleCreate(NULL, url); + CFRelease(url); + return opengl_bundle; } bool extgl_Open(JNIEnv *env) { - if (opengl_lib_handle != NULL) + if (opengl_bundle != NULL) return true; - opengl_lib_handle = loadImage("/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"); - if (opengl_lib_handle != NULL) { + opengl_bundle = loadFramework(env); + if (opengl_bundle != NULL) { return true; } else { throwException(env, "Could not load OpenGL library"); @@ -76,7 +81,10 @@ bool extgl_Open(JNIEnv *env) { void extgl_Close(void) { - opengl_lib_handle = NULL; + if (opengl_bundle != NULL) { + CFRelease(opengl_bundle); + opengl_bundle = NULL; + } } NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool use_display_bpp, bool support_window, bool support_pbuffer, bool double_buffered) {