src/video/x11/SDL_x11dyn.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 22 Mar 2006 09:13:10 +0000
changeset 1587 7fd9fc1f2be5
parent 1575 3ba88cb7eb1b
child 1589 34cca785be57
permissions -rw-r--r--
*** empty log message ***
     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 #if 0
    25 #define DEBUG_DYNAMIC_X11 1
    26 #endif
    27 
    28 #include "SDL_x11dyn.h"
    29 
    30 #ifdef DEBUG_DYNAMIC_X11
    31 #include <stdio.h>
    32 #endif
    33 
    34 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
    35 #include <dlfcn.h>
    36 #include "SDL_name.h"
    37 #include "SDL_loadso.h"
    38 static const char *x11_library = SDL_VIDEO_DRIVER_X11_DYNAMIC;
    39 static void *x11_handle = NULL;
    40 static const char *x11ext_library = SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT;
    41 static void *x11ext_handle = NULL;
    42 
    43 typedef struct
    44 {
    45     void *lib;
    46     const char *libname;
    47 } x11libitem;
    48 
    49 static void *X11_GetSym(const char *fnname, int *rc)
    50 {
    51 	int i;
    52 	void *fn = NULL;
    53 	const x11libitem libs[] =
    54 	{
    55 		{ x11_handle, "libX11" },
    56 		{ x11ext_handle, "libX11ext" },
    57 	};
    58 
    59 	for (i = 0; i < (sizeof (libs) / sizeof (libs[0])); i++)
    60 	{
    61 		if (libs[i].lib != NULL)
    62 		{
    63 			fn = SDL_LoadFunction(libs[i].lib, fnname);
    64 			if (fn != NULL)
    65 				break;
    66 		}
    67 	}
    68 
    69 	#if DEBUG_DYNAMIC_X11
    70 	if (fn != NULL)
    71 	    printf("X11: Found '%s' in %s (%p)\n", fnname, libs[i].libname, fn);
    72 	else
    73 		printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    74 	#endif
    75 
    76 	if (fn == NULL)
    77 		*rc = 0;  /* kill this module. */
    78 
    79 	return fn;
    80 }
    81 
    82 
    83 /* Define all the function pointers and wrappers... */
    84 #define SDL_X11_MODULE(modname)
    85 #define SDL_X11_SYM(rc,fn,params,args,ret) \
    86 	static rc (*p##fn) params = NULL; \
    87 	rc fn params { ret p##fn args ; }
    88 #include "SDL_x11sym.h"
    89 #undef SDL_X11_MODULE
    90 #undef SDL_X11_SYM
    91 #endif  /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
    92 
    93 /* Annoying varargs entry point... */
    94 #ifdef X_HAVE_UTF8_STRING
    95 XIC (*pXCreateIC)(XIM,...) = NULL;
    96 #endif
    97 
    98 /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
    99 #define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
   100 #define SDL_X11_SYM(rc,fn,params,args,ret)
   101 #include "SDL_x11sym.h"
   102 #undef SDL_X11_MODULE
   103 #undef SDL_X11_SYM
   104 
   105 
   106 static int x11_load_refcount = 0;
   107 
   108 void SDL_X11_UnloadSymbols(void)
   109 {
   110 	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   111 	/* Don't actually unload if more than one module is using the libs... */
   112 	if (x11_load_refcount > 0) {
   113 		if (--x11_load_refcount == 0) {
   114 			/* set all the function pointers to NULL. */
   115 			#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
   116 			#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
   117 			#include "SDL_x11sym.h"
   118 			#undef SDL_X11_MODULE
   119 			#undef SDL_X11_SYM
   120 
   121             #ifdef X_HAVE_UTF8_STRING
   122             pXCreateIC = NULL;
   123             #endif
   124 
   125 			if (x11_handle != NULL) {
   126 				SDL_UnloadObject(x11_handle);
   127 				x11_handle = NULL;
   128 			}
   129 			if (x11ext_handle != NULL) {
   130 				SDL_UnloadObject(x11ext_handle);
   131 				x11ext_handle = NULL;
   132 			}
   133 		}
   134 	}
   135 	#endif
   136 }
   137 
   138 /* returns non-zero if all needed symbols were loaded. */
   139 int SDL_X11_LoadSymbols(void)
   140 {
   141 	int rc = 1;  /* always succeed if not using Dynamic X11 stuff. */
   142 
   143 	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   144 	/* deal with multiple modules (dga, x11, etc) needing these symbols... */
   145 	if (x11_load_refcount++ == 0) {
   146 		int *thismod = NULL;
   147 		x11_handle = SDL_LoadObject(x11_library);
   148 		x11ext_handle = SDL_LoadObject(x11ext_library);
   149 		#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
   150 		#define SDL_X11_SYM(a,fn,x,y,z) p##fn = X11_GetSym(#fn,thismod);
   151 		#include "SDL_x11sym.h"
   152 		#undef SDL_X11_MODULE
   153 		#undef SDL_X11_SYM
   154 
   155 		#ifdef X_HAVE_UTF8_STRING
   156 		pXCreateIC = X11_GetSym("XCreateIC",&SDL_X11_HAVE_UTF8);
   157 		#endif
   158 
   159 		if (!SDL_X11_HAVE_BASEXLIB) {  /* some required symbol didn't load. */
   160 			SDL_X11_UnloadSymbols();  /* in case something got loaded... */
   161 		}
   162 	}
   163 	#else
   164 		#ifdef X_HAVE_UTF8_STRING
   165 		pXCreateIC = XCreateIC;
   166 		#endif
   167 	#endif
   168 
   169 	return rc;
   170 }
   171 
   172 /* end of SDL_x11dyn.c ... */
   173