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