src/video/x11/SDL_x11dyn.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Apr 2006 19:17:14 +0000
changeset 1648 9f59d4c5aaea
parent 1610 a1ee5944412b
child 1659 14717b52abc0
permissions -rw-r--r--
Mac OS X fat build works! :)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #define DEBUG_DYNAMIC_X11 0
    25 
    26 #include "SDL_x11dyn.h"
    27 
    28 #if DEBUG_DYNAMIC_X11
    29 #include <stdio.h>
    30 #endif
    31 
    32 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
    33 
    34 #include "SDL_name.h"
    35 #include "SDL_loadso.h"
    36 
    37 typedef struct
    38 {
    39     void *lib;
    40     const char *libname;
    41 } x11dynlib;
    42 
    43 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
    44 #define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
    45 #endif
    46 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
    47 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
    48 #endif
    49 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER
    50 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER NULL
    51 #endif
    52 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
    53 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
    54 #endif
    55 
    56 static x11dynlib x11libs[] =
    57 {
    58     { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC },
    59     { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT },
    60     { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER },
    61     { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR },
    62 };
    63 
    64 static void *X11_GetSym(const char *fnname, int *rc)
    65 {
    66 	int i;
    67 	void *fn = NULL;
    68 	for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
    69 		if (x11libs[i].lib != NULL)
    70 		{
    71 			fn = SDL_LoadFunction(x11libs[i].lib, fnname);
    72 			if (fn != NULL)
    73 				break;
    74 		}
    75 	}
    76 
    77 	#if DEBUG_DYNAMIC_X11
    78 	if (fn != NULL)
    79 		printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname, fn);
    80 	else
    81 		printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    82 	#endif
    83 
    84 	if (fn == NULL)
    85 		*rc = 0;  /* kill this module. */
    86 
    87 	return fn;
    88 }
    89 
    90 
    91 /* Define all the function pointers and wrappers... */
    92 #define SDL_X11_MODULE(modname)
    93 #define SDL_X11_SYM(rc,fn,params,args,ret) \
    94 	static rc (*p##fn) params = NULL; \
    95 	rc fn params { ret p##fn args ; }
    96 #include "SDL_x11sym.h"
    97 #undef SDL_X11_MODULE
    98 #undef SDL_X11_SYM
    99 #endif  /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
   100 
   101 /* Annoying varargs entry point... */
   102 #ifdef X_HAVE_UTF8_STRING
   103 XIC (*pXCreateIC)(XIM,...) = NULL;
   104 #endif
   105 
   106 /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
   107 #define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
   108 #define SDL_X11_SYM(rc,fn,params,args,ret)
   109 #include "SDL_x11sym.h"
   110 #undef SDL_X11_MODULE
   111 #undef SDL_X11_SYM
   112 
   113 
   114 static int x11_load_refcount = 0;
   115 
   116 void SDL_X11_UnloadSymbols(void)
   117 {
   118 	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   119 	/* Don't actually unload if more than one module is using the libs... */
   120 	if (x11_load_refcount > 0) {
   121 		if (--x11_load_refcount == 0) {
   122 			int i;
   123 
   124 			/* set all the function pointers to NULL. */
   125 			#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
   126 			#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
   127 			#include "SDL_x11sym.h"
   128 			#undef SDL_X11_MODULE
   129 			#undef SDL_X11_SYM
   130 
   131 			#ifdef X_HAVE_UTF8_STRING
   132 			pXCreateIC = NULL;
   133 			#endif
   134 
   135 			for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   136 				if (x11libs[i].lib != NULL) {
   137 					SDL_UnloadObject(x11libs[i].lib);
   138 					x11libs[i].lib = NULL;
   139 				}
   140 			}
   141 		}
   142 	}
   143 	#endif
   144 }
   145 
   146 /* returns non-zero if all needed symbols were loaded. */
   147 int SDL_X11_LoadSymbols(void)
   148 {
   149 	int rc = 1;  /* always succeed if not using Dynamic X11 stuff. */
   150 
   151 	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   152 	/* deal with multiple modules (dga, x11, etc) needing these symbols... */
   153 	if (x11_load_refcount++ == 0) {
   154 		int i;
   155 		int *thismod = NULL;
   156 		for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   157 			if (x11libs[i].libname != NULL) {
   158 				x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
   159 			}
   160 		}
   161 		#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
   162 		#define SDL_X11_SYM(a,fn,x,y,z) p##fn = X11_GetSym(#fn,thismod);
   163 		#include "SDL_x11sym.h"
   164 		#undef SDL_X11_MODULE
   165 		#undef SDL_X11_SYM
   166 
   167 		#ifdef X_HAVE_UTF8_STRING
   168 		pXCreateIC = X11_GetSym("XCreateIC",&SDL_X11_HAVE_UTF8);
   169 		#endif
   170 
   171 		if (!SDL_X11_HAVE_BASEXLIB) {  /* some required symbol didn't load. */
   172 			SDL_X11_UnloadSymbols();  /* in case something got loaded... */
   173 			rc = 0;
   174 		}
   175 	}
   176 	#else
   177 		#ifdef X_HAVE_UTF8_STRING
   178 		pXCreateIC = XCreateIC;
   179 		#endif
   180 	#endif
   181 
   182 	return rc;
   183 }
   184 
   185 /* end of SDL_x11dyn.c ... */
   186