src/video/x11/SDL_x11dyn.c
changeset 1575 3ba88cb7eb1b
parent 1402 d910939febfa
child 1587 7fd9fc1f2be5
     1.1 --- a/src/video/x11/SDL_x11dyn.c	Wed Mar 22 04:51:44 2006 +0000
     1.2 +++ b/src/video/x11/SDL_x11dyn.c	Wed Mar 22 05:00:59 2006 +0000
     1.3 @@ -21,11 +21,10 @@
     1.4  */
     1.5  #include "SDL_config.h"
     1.6  
     1.7 -#if 0
     1.8 +#if 1
     1.9  #define DEBUG_DYNAMIC_X11 1
    1.10  #endif
    1.11  
    1.12 -#define __SDL_NO_REDEFINE_X11_HEADER_SYMS 1
    1.13  #include "SDL_x11dyn.h"
    1.14  
    1.15  #ifdef DEBUG_DYNAMIC_X11
    1.16 @@ -41,51 +40,88 @@
    1.17  static const char *x11ext_library = SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT;
    1.18  static void *x11ext_handle = NULL;
    1.19  
    1.20 -static void *X11_GetSym(int required, const char *fnname, int *rc)
    1.21 +typedef struct
    1.22  {
    1.23 +    void *lib;
    1.24 +    const char *libname;
    1.25 +} x11libitem;
    1.26 +
    1.27 +static void *X11_GetSym(const char *fnname, int *rc)
    1.28 +{
    1.29 +	int i;
    1.30  	void *fn = NULL;
    1.31 -	if (*rc) {  /* haven't already failed on a previous lookup? */
    1.32 -		fn = SDL_LoadFunction(x11_handle, fnname);
    1.33 -		#if DEBUG_DYNAMIC_X11
    1.34 -		if (fn != NULL)
    1.35 -			printf("X11: Found '%s' in libX11 (%p)\n", fnname, fn);
    1.36 -		#endif
    1.37 +	const x11libitem libs[] =
    1.38 +	{
    1.39 +		{ x11_handle, "libX11" },
    1.40 +		{ x11ext_handle, "libX11ext" },
    1.41 +	};
    1.42  
    1.43 -		if (fn == NULL) {  /* not found? Check libX11ext ... */
    1.44 -			fn = SDL_LoadFunction(x11ext_handle, fnname);
    1.45 -			#if DEBUG_DYNAMIC_X11
    1.46 +	for (i = 0; i < (sizeof (libs) / sizeof (libs[0])); i++)
    1.47 +	{
    1.48 +		if (libs[i].lib != NULL)
    1.49 +		{
    1.50 +			fn = SDL_LoadFunction(libs[i].lib, fnname);
    1.51  			if (fn != NULL)
    1.52 -				printf("X11: Found '%s' in libXext (%p)\n", fnname, fn);
    1.53 -			else
    1.54 -				printf("X11: Symbol '%s' NOT FOUND!%s\n", fnname,
    1.55 -				       required ? "" : " (...but not required!)");
    1.56 -			#endif
    1.57 +				break;
    1.58  		}
    1.59 -		*rc = ((fn != NULL) || (!required));
    1.60  	}
    1.61  
    1.62 +	#if DEBUG_DYNAMIC_X11
    1.63 +	if (fn != NULL)
    1.64 +	    printf("X11: Found '%s' in %s (%p)\n", fnname, libs[i].libname, fn);
    1.65 +	else
    1.66 +		printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    1.67 +	#endif
    1.68 +
    1.69 +	if (fn == NULL)
    1.70 +		*rc = 0;  /* kill this module. */
    1.71 +
    1.72  	return fn;
    1.73  }
    1.74 +
    1.75 +
    1.76 +/* Define all the function pointers and wrappers... */
    1.77 +#define SDL_X11_MODULE(modname)
    1.78 +#define SDL_X11_SYM(rc,fn,params,args,ret) \
    1.79 +	static rc (*p##fn) params = NULL; \
    1.80 +	rc fn params { ret p##fn args ; }
    1.81 +#include "SDL_x11sym.h"
    1.82 +#undef SDL_X11_MODULE
    1.83 +#undef SDL_X11_SYM
    1.84  #endif  /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
    1.85  
    1.86 -/* Define all the function pointers... */
    1.87 -#define SDL_X11_SYM(req,ret,fn,params) ret (*p##fn) params = NULL;
    1.88 +/* Annoying varargs entry point... */
    1.89 +#ifdef X_HAVE_UTF8_STRING
    1.90 +XIC (*pXCreateIC)(XIM,...) = NULL;
    1.91 +#endif
    1.92 +
    1.93 +/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
    1.94 +#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
    1.95 +#define SDL_X11_SYM(rc,fn,params,args,ret)
    1.96  #include "SDL_x11sym.h"
    1.97 +#undef SDL_X11_MODULE
    1.98  #undef SDL_X11_SYM
    1.99  
   1.100 +
   1.101  static int x11_load_refcount = 0;
   1.102  
   1.103  void SDL_X11_UnloadSymbols(void)
   1.104  {
   1.105 +	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   1.106  	/* Don't actually unload if more than one module is using the libs... */
   1.107  	if (x11_load_refcount > 0) {
   1.108  		if (--x11_load_refcount == 0) {
   1.109  			/* set all the function pointers to NULL. */
   1.110 -			#define SDL_X11_SYM(req,ret,fn,params) p##fn = NULL;
   1.111 +			#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
   1.112 +			#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
   1.113  			#include "SDL_x11sym.h"
   1.114 +			#undef SDL_X11_MODULE
   1.115  			#undef SDL_X11_SYM
   1.116  
   1.117 -			#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   1.118 +            #ifdef X_HAVE_UTF8_STRING
   1.119 +            pXCreateIC = NULL;
   1.120 +            #endif
   1.121 +
   1.122  			if (x11_handle != NULL) {
   1.123  				SDL_UnloadObject(x11_handle);
   1.124  				x11_handle = NULL;
   1.125 @@ -94,35 +130,41 @@
   1.126  				SDL_UnloadObject(x11ext_handle);
   1.127  				x11ext_handle = NULL;
   1.128  			}
   1.129 -			#endif
   1.130  		}
   1.131  	}
   1.132 +	#endif
   1.133  }
   1.134  
   1.135  /* returns non-zero if all needed symbols were loaded. */
   1.136  int SDL_X11_LoadSymbols(void)
   1.137  {
   1.138 -	int rc = 1;
   1.139 +	int rc = 1;  /* always succeed if not using Dynamic X11 stuff. */
   1.140  
   1.141 -    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   1.142 +	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   1.143 +	/* deal with multiple modules (dga, x11, etc) needing these symbols... */
   1.144  	if (x11_load_refcount++ == 0) {
   1.145 -		#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   1.146 -			x11_handle = SDL_LoadObject(x11_library);
   1.147 -			x11ext_handle = SDL_LoadObject(x11ext_library);
   1.148 -			rc = ((x11_handle != NULL) && (x11ext_handle != NULL));
   1.149 -			#define SDL_X11_SYM(req,r,fn,arg) p##fn = X11_GetSym(req,#fn, &rc);
   1.150 -			#include "SDL_x11sym.h"
   1.151 -			#undef SDL_X11_SYM
   1.152 +		int *thismod = NULL;
   1.153 +		x11_handle = SDL_LoadObject(x11_library);
   1.154 +		x11ext_handle = SDL_LoadObject(x11ext_library);
   1.155 +		#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
   1.156 +		#define SDL_X11_SYM(a,fn,x,y,z) p##fn = X11_GetSym(#fn,thismod);
   1.157 +		#include "SDL_x11sym.h"
   1.158 +		#undef SDL_X11_MODULE
   1.159 +		#undef SDL_X11_SYM
   1.160  
   1.161 -			if (!rc)
   1.162 -				SDL_X11_UnloadSymbols();  /* in case one of these loaded... */
   1.163 +		#ifdef X_HAVE_UTF8_STRING
   1.164 +		pXCreateIC = X11_GetSym("XCreateIC",&SDL_X11_HAVE_UTF8);
   1.165 +		#endif
   1.166  
   1.167 -		#else
   1.168 -			#define SDL_X11_SYM(req,r,fn,arg) p##fn = fn;
   1.169 -			#include "SDL_x11sym.h"
   1.170 -			#undef SDL_X11_SYM
   1.171 +		if (!SDL_X11_HAVE_BASEXLIB) {  /* some required symbol didn't load. */
   1.172 +			SDL_X11_UnloadSymbols();  /* in case something got loaded... */
   1.173 +		}
   1.174 +	}
   1.175 +	#else
   1.176 +		#ifdef X_HAVE_UTF8_STRING
   1.177 +		pXCreateIC = XCreateIC;
   1.178  		#endif
   1.179 -	}
   1.180 +	#endif
   1.181  
   1.182  	return rc;
   1.183  }