diff src/video/x11/SDL_x11gl.c @ 1191:2bd4cec0de63

Seperate glX from HAVE_OPENGL, for platforms that have both an X server and a more official way to do OpenGL, explicitly check for glX on Mac OS X, and use SDL_LoadObject for platforms that have glX but don't have dlopen().
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 23 Nov 2005 11:46:36 +0000
parents e8e8dcb68e7a
children b81f54c3963f
line wrap: on
line diff
--- a/src/video/x11/SDL_x11gl.c	Wed Nov 23 07:29:56 2005 +0000
+++ b/src/video/x11/SDL_x11gl.c	Wed Nov 23 11:46:36 2005 +0000
@@ -37,6 +37,8 @@
 #if defined(sgi)
 /* IRIX doesn't have a GL library versioning system */
 #define DEFAULT_OPENGL	"libGL.so"
+#elif defined(MACOSX)
+#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
 #else
 #define DEFAULT_OPENGL	"libGL.so.1"
 #endif
@@ -50,7 +52,7 @@
 /* return the preferred visual to use for openGL graphics */
 XVisualInfo *X11_GL_GetVisual(_THIS)
 {
-#ifdef HAVE_OPENGL
+#ifdef HAVE_OPENGL_X11
 	/* 64 seems nice. */
 	int attribs[64];
 	int i;
@@ -175,7 +177,7 @@
 int X11_GL_CreateWindow(_THIS, int w, int h)
 {
 	int retval;
-#ifdef HAVE_OPENGL
+#ifdef HAVE_OPENGL_X11
 	XSetWindowAttributes attributes;
 	unsigned long mask;
 	unsigned long black;
@@ -207,7 +209,7 @@
 int X11_GL_CreateContext(_THIS)
 {
 	int retval;
-#ifdef HAVE_OPENGL
+#ifdef HAVE_OPENGL_X11
 	/* We do this to create a clean separation between X and GLX errors. */
 	pXSync( SDL_Display, False );
 	glx_context = this->gl_data->glXCreateContext(GFX_Display, 
@@ -233,7 +235,7 @@
 
 void X11_GL_Shutdown(_THIS)
 {
-#ifdef HAVE_OPENGL
+#ifdef HAVE_OPENGL_X11
 	/* Clean up OpenGL */
 	if( glx_context ) {
 		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
@@ -247,10 +249,10 @@
 		glx_context = NULL;
 	}
 	gl_active = 0;
-#endif /* HAVE_OPENGL */
+#endif /* HAVE_OPENGL_X11 */
 }
 
-#ifdef HAVE_OPENGL
+#ifdef HAVE_OPENGL_X11
 
 static int ExtensionSupported(const char *extension)
 {
@@ -395,13 +397,19 @@
 	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
 }
 
-#endif /* HAVE_OPENGL */
+#endif /* HAVE_OPENGL_X11 */
 
 void X11_GL_UnloadLibrary(_THIS)
 {
-#ifdef HAVE_OPENGL
+#ifdef HAVE_OPENGL_X11
 	if ( this->gl_config.driver_loaded ) {
+
+		/* !!! FIXME: Can we just use SDL_UnloadObject() everywhere? */
+		#ifdef USE_DLOPEN
 		dlclose(this->gl_config.dll_handle);
+		#else
+		SDL_UnloadObject(this->gl_config.dll_handle);
+		#endif
 
 		this->gl_data->glXGetProcAddress = NULL;
 		this->gl_data->glXChooseVisual = NULL;
@@ -416,16 +424,26 @@
 #endif
 }
 
-#ifdef HAVE_OPENGL
+#ifdef HAVE_OPENGL_X11
 
-/* If this is wrong, please put some #ifdefs for your platform! */
-#define DEFAULT_GL_DRIVER_PATH "libGL.so.1"
+static void *do_dlsym(void *handle, const char *name)
+{
+	/* !!! FIXME: Can we just use SDL_LoadFunction() everywhere? */
+#ifdef USE_DLOPEN
+	return dlsym(handle, name);
+#else
+	return SDL_LoadFunction(handle, name);
+#endif
+}
+
+#if defined(__OpenBSD__) && !defined(__ELF__)
+#define do_dlsym(x,y) do_dlsym(x, "_" y)
+#endif
 
 /* Passing a NULL path means load pointers from the application */
 int X11_GL_LoadLibrary(_THIS, const char* path) 
 {
-	void* handle;
-	int dlopen_flags;
+	void* handle = NULL;
 
 	if ( gl_active ) {
 		SDL_SetError("OpenGL context already created");
@@ -433,24 +451,26 @@
 	}
 
 	if ( path == NULL ) {
-		path = DEFAULT_GL_DRIVER_PATH;
-	}
-
-#ifdef RTLD_GLOBAL
-	dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
-#else
-	dlopen_flags = RTLD_LAZY;
-#endif
-	handle = dlopen(path, dlopen_flags);
-	/* Catch the case where the application isn't linked with GL */
-	if ( (dlsym(handle, "glXChooseVisual") == NULL) && (path == NULL) ) {
-		dlclose(handle);
 		path = getenv("SDL_VIDEO_GL_DRIVER");
 		if ( path == NULL ) {
 			path = DEFAULT_OPENGL;
 		}
+	}
+
+	/* !!! FIXME: Can we just use SDL_LoadObject() everywhere? */
+	#ifdef USE_DLOPEN
+	{
+		#ifdef RTLD_GLOBAL
+			int dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
+		#else
+			int dlopen_flags = RTLD_LAZY;
+		#endif
 		handle = dlopen(path, dlopen_flags);
 	}
+	#else
+		handle = SDL_LoadObject(path);
+	#endif
+
 	if ( handle == NULL ) {
 		SDL_SetError("Could not load OpenGL library");
 		return -1;
@@ -461,25 +481,25 @@
 
 	/* Load new function pointers */
 	this->gl_data->glXGetProcAddress =
-		(void *(*)(const GLubyte *)) dlsym(handle, "glXGetProcAddressARB");
+		(void *(*)(const GLubyte *)) do_dlsym(handle, "glXGetProcAddressARB");
 	this->gl_data->glXChooseVisual =
-		(XVisualInfo *(*)(Display *, int, int *)) dlsym(handle, "glXChooseVisual");
+		(XVisualInfo *(*)(Display *, int, int *)) do_dlsym(handle, "glXChooseVisual");
 	this->gl_data->glXCreateContext =
-		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) dlsym(handle, "glXCreateContext");
+		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) do_dlsym(handle, "glXCreateContext");
 	this->gl_data->glXDestroyContext =
-		(void (*)(Display *, GLXContext)) dlsym(handle, "glXDestroyContext");
+		(void (*)(Display *, GLXContext)) do_dlsym(handle, "glXDestroyContext");
 	this->gl_data->glXMakeCurrent =
-		(int (*)(Display *, GLXDrawable, GLXContext)) dlsym(handle, "glXMakeCurrent");
+		(int (*)(Display *, GLXDrawable, GLXContext)) do_dlsym(handle, "glXMakeCurrent");
 	this->gl_data->glXSwapBuffers =
-		(void (*)(Display *, GLXDrawable)) dlsym(handle, "glXSwapBuffers");
+		(void (*)(Display *, GLXDrawable)) do_dlsym(handle, "glXSwapBuffers");
 	this->gl_data->glXGetConfig =
-		(int (*)(Display *, XVisualInfo *, int, int *)) dlsym(handle, "glXGetConfig");
+		(int (*)(Display *, XVisualInfo *, int, int *)) do_dlsym(handle, "glXGetConfig");
 	this->gl_data->glXQueryExtensionsString =
-		(const char *(*)(Display *, int)) dlsym(handle, "glXQueryExtensionsString");
+		(const char *(*)(Display *, int)) do_dlsym(handle, "glXQueryExtensionsString");
 	
 	/* We don't compare below for this in case we're not using Mesa. */
 	this->gl_data->glXReleaseBuffersMESA =
-		(void (*)(Display *, GLXDrawable)) dlsym( handle, "glXReleaseBuffersMESA" );
+		(void (*)(Display *, GLXDrawable)) do_dlsym( handle, "glXReleaseBuffersMESA" );
 	
 	
 	if ( (this->gl_data->glXChooseVisual == NULL) || 
@@ -515,15 +535,15 @@
 		return this->gl_data->glXGetProcAddress(proc);
 	}
 #if defined(__OpenBSD__) && !defined(__ELF__)
-#undef dlsym(x,y);
+#undef do_dlsym
 #endif
-	retval = dlsym(handle, proc);
+	retval = do_dlsym(handle, proc);
 	if (!retval && strlen(proc) <= 1022) {
 		procname[0] = '_';
 		strcpy(procname + 1, proc);
-		retval = dlsym(handle, procname);
+		retval = do_dlsym(handle, procname);
 	}
 	return retval;
 }
 
-#endif /* HAVE_OPENGL */
+#endif /* HAVE_OPENGL_X11 */