src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 04 Jan 2006 22:57:50 +0000
changeset 1226 d05306f0dc21
parent 1171 f84c6f1397cd
child 1235 f60f6f4ffc6f
permissions -rw-r--r--
More dynamic X11 fixes...catches some code in Xlib headers that caused
dependencies...
     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 
    23 /*#define DEBUG_DYNAMIC_X11 1*/
    24 
    25 #define __SDL_NO_REDEFINE_X11_HEADER_SYMS 1
    26 #include "SDL_x11dyn.h"
    27 
    28 #ifdef DEBUG_DYNAMIC_X11
    29 #include <stdio.h>
    30 #endif
    31 
    32 #ifdef X11_DYNAMIC
    33 #include <dlfcn.h>
    34 #include "SDL_name.h"
    35 #include "SDL_loadso.h"
    36 static const char *x11_library = X11_DYNAMIC;
    37 static void *x11_handle = NULL;
    38 static const char *x11ext_library = X11EXT_DYNAMIC;
    39 static void *x11ext_handle = NULL;
    40 
    41 static void *X11_GetSym(const char *fnname, int *rc)
    42 {
    43 	void *fn = NULL;
    44 	if (*rc) {  /* haven't already failed on a previous lookup? */
    45 		fn = SDL_LoadFunction(x11_handle, fnname);
    46 		#if DEBUG_DYNAMIC_X11
    47 		if (fn != NULL)
    48 			printf("X11: Found '%s' in libX11 (%p)\n", fnname, fn);
    49 		#endif
    50 
    51 		if (fn == NULL) {  /* not found? Check libX11ext ... */
    52 			fn = SDL_LoadFunction(x11ext_handle, fnname);
    53 			#if DEBUG_DYNAMIC_X11
    54 			if (fn != NULL)
    55 				printf("X11: Found '%s' in libXext (%p)\n", fnname, fn);
    56 			else
    57 				printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    58 			#endif
    59 		}
    60 		*rc = (fn != NULL);
    61 	}
    62 
    63 	return fn;
    64 }
    65 #endif  /* defined X11_DYNAMIC */
    66 
    67 /* Define all the function pointers... */
    68 #define SDL_X11_SYM(ret,fn,params) ret (*p##fn) params = NULL;
    69 #include "SDL_x11sym.h"
    70 #undef SDL_X11_SYM
    71 
    72 static int x11_load_refcount = 0;
    73 
    74 void SDL_X11_UnloadSymbols(void)
    75 {
    76 	/* Don't actually unload if more than one module is using the libs... */
    77 	if (x11_load_refcount > 0) {
    78 		if (--x11_load_refcount == 0) {
    79 			/* set all the function pointers to NULL. */
    80 			#define SDL_X11_SYM(ret,fn,params) p##fn = NULL;
    81 			#include "SDL_x11sym.h"
    82 			#undef SDL_X11_SYM
    83 
    84 			#ifdef X11_DYNAMIC
    85 			if (x11_handle != NULL) {
    86 				SDL_UnloadObject(x11_handle);
    87 				x11_handle = NULL;
    88 			}
    89 			if (x11ext_handle != NULL) {
    90 				SDL_UnloadObject(x11ext_handle);
    91 				x11ext_handle = NULL;
    92 			}
    93 			#endif
    94 		}
    95 	}
    96 }
    97 
    98 /* returns non-zero if all needed symbols were loaded. */
    99 int SDL_X11_LoadSymbols(void)
   100 {
   101 	int rc = 1;
   102 
   103     /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   104 	if (x11_load_refcount++ == 0) {
   105 		#ifdef X11_DYNAMIC
   106 			x11_handle = SDL_LoadObject(x11_library);
   107 			x11ext_handle = SDL_LoadObject(x11ext_library);
   108 			rc = ((x11_handle != NULL) && (x11ext_handle != NULL));
   109 			#define SDL_X11_SYM(r,fn,arg) p##fn = X11_GetSym(#fn, &rc);
   110 			#include "SDL_x11sym.h"
   111 			#undef SDL_X11_SYM
   112 
   113 			if (!rc)
   114 				SDL_X11_UnloadSymbols();  /* in case one of these loaded... */
   115 
   116 		#else
   117 			#define SDL_X11_SYM(r,fn,arg) p##fn = fn;
   118 			#include "SDL_x11sym.h"
   119 			#undef SDL_X11_SYM
   120 		#endif
   121 	}
   122 
   123 	return rc;
   124 }
   125 
   126 /* end of SDL_x11dyn.c ... */
   127