diff src/video/x11/SDL_x11dyn.c @ 1593:5115439d67b1

Consolidate all the X11 libraries, so you don't have to update 12 different places in the source when adding a new one. Also handles build systems that don't define a given library.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 23 Mar 2006 02:47:56 +0000
parents 34cca785be57
children a1ee5944412b
line wrap: on
line diff
--- a/src/video/x11/SDL_x11dyn.c	Wed Mar 22 22:42:44 2006 +0000
+++ b/src/video/x11/SDL_x11dyn.c	Thu Mar 23 02:47:56 2006 +0000
@@ -30,41 +30,46 @@
 #endif
 
 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
+
 #include <dlfcn.h>
 #include "SDL_name.h"
 #include "SDL_loadso.h"
-static const char *x11_library = SDL_VIDEO_DRIVER_X11_DYNAMIC;
-static void *x11_handle = NULL;
-static const char *x11ext_library = SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT;
-static void *x11ext_handle = NULL;
-static const char *xrender_library = SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER;
-static void *xrender_handle = NULL;
-static const char *xrandr_library = SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR;
-static void *xrandr_handle = NULL;
 
 typedef struct
 {
     void *lib;
     const char *libname;
-} x11libitem;
+} x11dynlib;
+
+#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
+#endif
+#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
+#endif
+#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER NULL
+#endif
+#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
+#endif
+
+static x11dynlib x11libs[] =
+{
+    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC },
+    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT },
+    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER },
+    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR },
+};
 
 static void *X11_GetSym(const char *fnname, int *rc)
 {
 	int i;
 	void *fn = NULL;
-	const x11libitem libs[] =
-	{
-		{ x11_handle, "libX11" },
-		{ x11ext_handle, "libX11ext" },
-		{ xrender_handle, "libXrender" },
-		{ xrandr_handle, "libXrandr" },
-	};
-
-	for (i = 0; i < (sizeof (libs) / sizeof (libs[0])); i++)
-	{
-		if (libs[i].lib != NULL)
+	for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
+		if (x11libs[i].lib != NULL)
 		{
-			fn = SDL_LoadFunction(libs[i].lib, fnname);
+			fn = SDL_LoadFunction(x11libs[i].lib, fnname);
 			if (fn != NULL)
 				break;
 		}
@@ -72,7 +77,7 @@
 
 	#if DEBUG_DYNAMIC_X11
 	if (fn != NULL)
-	    printf("X11: Found '%s' in %s (%p)\n", fnname, libs[i].libname, fn);
+		printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname, fn);
 	else
 		printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
 	#endif
@@ -115,6 +120,8 @@
 	/* Don't actually unload if more than one module is using the libs... */
 	if (x11_load_refcount > 0) {
 		if (--x11_load_refcount == 0) {
+			int i;
+
 			/* set all the function pointers to NULL. */
 			#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
 			#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
@@ -122,25 +129,15 @@
 			#undef SDL_X11_MODULE
 			#undef SDL_X11_SYM
 
-            #ifdef X_HAVE_UTF8_STRING
-            pXCreateIC = NULL;
-            #endif
+			#ifdef X_HAVE_UTF8_STRING
+			pXCreateIC = NULL;
+			#endif
 
-			if (x11_handle != NULL) {
-				SDL_UnloadObject(x11_handle);
-				x11_handle = NULL;
-			}
-			if (x11ext_handle != NULL) {
-				SDL_UnloadObject(x11ext_handle);
-				x11ext_handle = NULL;
-			}
-			if (xrender_handle != NULL) {
-				SDL_UnloadObject(xrender_handle);
-				xrender_handle = NULL;
-			}
-			if (xrandr_handle != NULL) {
-				SDL_UnloadObject(xrandr_handle);
-				xrandr_handle = NULL;
+			for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
+				if (x11libs[i].lib != NULL) {
+					SDL_UnloadObject(x11libs[i].lib);
+					x11libs[i].lib = NULL;
+				}
 			}
 		}
 	}
@@ -155,11 +152,13 @@
 	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
 	/* deal with multiple modules (dga, x11, etc) needing these symbols... */
 	if (x11_load_refcount++ == 0) {
+		int i;
 		int *thismod = NULL;
-		x11_handle = SDL_LoadObject(x11_library);
-		x11ext_handle = SDL_LoadObject(x11ext_library);
-		xrender_handle = SDL_LoadObject(xrender_library);
-		xrandr_handle = SDL_LoadObject(xrandr_library);
+		for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
+			if (x11libs[i].libname != NULL) {
+				x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
+			}
+		}
 		#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
 		#define SDL_X11_SYM(a,fn,x,y,z) p##fn = X11_GetSym(#fn,thismod);
 		#include "SDL_x11sym.h"