src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 06 Jan 2006 20:57:59 +0000
changeset 1236 ed252764287a
parent 1235 f60f6f4ffc6f
child 1245 fe7d081a4955
permissions -rw-r--r--
Minor bit of debug output added to dynamic X11 code.
     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!%s\n", fnname
    60 				       required ? "" : " (...but not required!)");
    61 			#endif
    62 		}
    63 		*rc = ((fn != NULL) || (!required));
    64 	}
    65 
    66 	return fn;
    67 }
    68 #endif  /* defined X11_DYNAMIC */
    69 
    70 /* Define all the function pointers... */
    71 #define SDL_X11_SYM(req,ret,fn,params) ret (*p##fn) params = NULL;
    72 #include "SDL_x11sym.h"
    73 #undef SDL_X11_SYM
    74 
    75 static int x11_load_refcount = 0;
    76 
    77 void SDL_X11_UnloadSymbols(void)
    78 {
    79 	/* Don't actually unload if more than one module is using the libs... */
    80 	if (x11_load_refcount > 0) {
    81 		if (--x11_load_refcount == 0) {
    82 			/* set all the function pointers to NULL. */
    83 			#define SDL_X11_SYM(req,ret,fn,params) p##fn = NULL;
    84 			#include "SDL_x11sym.h"
    85 			#undef SDL_X11_SYM
    86 
    87 			#ifdef X11_DYNAMIC
    88 			if (x11_handle != NULL) {
    89 				SDL_UnloadObject(x11_handle);
    90 				x11_handle = NULL;
    91 			}
    92 			if (x11ext_handle != NULL) {
    93 				SDL_UnloadObject(x11ext_handle);
    94 				x11ext_handle = NULL;
    95 			}
    96 			#endif
    97 		}
    98 	}
    99 }
   100 
   101 /* returns non-zero if all needed symbols were loaded. */
   102 int SDL_X11_LoadSymbols(void)
   103 {
   104 	int rc = 1;
   105 
   106     /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   107 	if (x11_load_refcount++ == 0) {
   108 		#ifdef X11_DYNAMIC
   109 			x11_handle = SDL_LoadObject(x11_library);
   110 			x11ext_handle = SDL_LoadObject(x11ext_library);
   111 			rc = ((x11_handle != NULL) && (x11ext_handle != NULL));
   112 			#define SDL_X11_SYM(req,r,fn,arg) p##fn = X11_GetSym(req,#fn, &rc);
   113 			#include "SDL_x11sym.h"
   114 			#undef SDL_X11_SYM
   115 
   116 			if (!rc)
   117 				SDL_X11_UnloadSymbols();  /* in case one of these loaded... */
   118 
   119 		#else
   120 			#define SDL_X11_SYM(req,r,fn,arg) p##fn = fn;
   121 			#include "SDL_x11sym.h"
   122 			#undef SDL_X11_SYM
   123 		#endif
   124 	}
   125 
   126 	return rc;
   127 }
   128 
   129 /* end of SDL_x11dyn.c ... */
   130