src/video/x11/SDL_x11dyn.c
changeset 1168 045f186426e1
child 1171 f84c6f1397cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/x11/SDL_x11dyn.c	Sat Nov 05 19:53:37 2005 +0000
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2004 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +/*#define DEBUG_DYNAMIC_X11 1*/
    1.27 +
    1.28 +#include "SDL_x11dyn.h"
    1.29 +
    1.30 +#ifdef DEBUG_DYNAMIC_X11
    1.31 +#include <stdio.h>
    1.32 +#endif
    1.33 +
    1.34 +#ifdef X11_DYNAMIC
    1.35 +#include <dlfcn.h>
    1.36 +#include "SDL_name.h"
    1.37 +#include "SDL_loadso.h"
    1.38 +static const char *x11_library = X11_DYNAMIC;
    1.39 +static void *x11_handle = NULL;
    1.40 +static const char *x11ext_library = X11EXT_DYNAMIC;
    1.41 +static void *x11ext_handle = NULL;
    1.42 +
    1.43 +static void *X11_GetSym(const char *fnname, int *rc)
    1.44 +{
    1.45 +	void *fn = NULL;
    1.46 +	if (*rc) {  /* haven't already failed on a previous lookup? */
    1.47 +		fn = SDL_LoadFunction(x11_handle, fnname);
    1.48 +		#if DEBUG_DYNAMIC_X11
    1.49 +		if (fn != NULL)
    1.50 +			printf("X11: Found '%s' in libX11 (%p)\n", fnname, fn);
    1.51 +		#endif
    1.52 +
    1.53 +		if (fn == NULL) {  /* not found? Check libX11ext ... */
    1.54 +			fn = SDL_LoadFunction(x11ext_handle, fnname);
    1.55 +			#if DEBUG_DYNAMIC_X11
    1.56 +			if (fn != NULL)
    1.57 +				printf("X11: Found '%s' in libXext (%p)\n", fnname, fn);
    1.58 +			else
    1.59 +				printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    1.60 +			#endif
    1.61 +		}
    1.62 +		*rc = (fn != NULL);
    1.63 +	}
    1.64 +
    1.65 +	return fn;
    1.66 +}
    1.67 +#endif  /* defined X11_DYNAMIC */
    1.68 +
    1.69 +/* Define all the function pointers... */
    1.70 +#define SDL_X11_SYM(ret,fn,params) ret (*p##fn) params = NULL;
    1.71 +#include "SDL_x11sym.h"
    1.72 +#undef SDL_X11_SYM
    1.73 +
    1.74 +static int x11_load_refcount = 0;
    1.75 +
    1.76 +void SDL_X11_UnloadSymbols(void)
    1.77 +{
    1.78 +	/* Don't actually unload if more than one module is using the libs... */
    1.79 +	if (x11_load_refcount > 0) {
    1.80 +		if (--x11_load_refcount == 0) {
    1.81 +			/* set all the function pointers to NULL. */
    1.82 +			#define SDL_X11_SYM(ret,fn,params) p##fn = NULL;
    1.83 +			#include "SDL_x11sym.h"
    1.84 +			#undef SDL_X11_SYM
    1.85 +
    1.86 +			#ifdef X11_DYNAMIC
    1.87 +			if (x11_handle != NULL) {
    1.88 +				SDL_UnloadObject(x11_handle);
    1.89 +				x11_handle = NULL;
    1.90 +			}
    1.91 +			if (x11ext_handle != NULL) {
    1.92 +				SDL_UnloadObject(x11ext_handle);
    1.93 +				x11ext_handle = NULL;
    1.94 +			}
    1.95 +			#endif
    1.96 +		}
    1.97 +	}
    1.98 +}
    1.99 +
   1.100 +/* returns non-zero if all needed symbols were loaded. */
   1.101 +int SDL_X11_LoadSymbols(void)
   1.102 +{
   1.103 +	int rc = 1;
   1.104 +
   1.105 +    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   1.106 +	if (x11_load_refcount++ == 0) {
   1.107 +		#ifdef X11_DYNAMIC
   1.108 +			x11_handle = SDL_LoadObject(x11_library);
   1.109 +			x11ext_handle = SDL_LoadObject(x11ext_library);
   1.110 +			if ((x11_handle != NULL) && (x11ext_handle != NULL)) {
   1.111 +				#define SDL_X11_SYM(r,fn,arg) p##fn = X11_GetSym(#fn, &rc);
   1.112 +				#include "SDL_x11sym.h"
   1.113 +				#undef SDL_X11_SYM
   1.114 +			}
   1.115 +
   1.116 +			if (!rc)
   1.117 +				SDL_X11_UnloadSymbols();  /* in case one of these loaded... */
   1.118 +
   1.119 +		#else
   1.120 +			#define SDL_X11_SYM(r,fn,arg) p##fn = fn;
   1.121 +			#include "SDL_x11sym.h"
   1.122 +			#undef SDL_X11_SYM
   1.123 +		#endif
   1.124 +	}
   1.125 +
   1.126 +	return rc;
   1.127 +}
   1.128 +
   1.129 +/* end of SDL_x11dyn.c ... */
   1.130 +