src/video/x11/SDL_x11dyn.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1245 fe7d081a4955
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
     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 #include "SDL_config.h"
    28 
    29 #define __SDL_NO_REDEFINE_X11_HEADER_SYMS 1
    30 #include "SDL_x11dyn.h"
    31 
    32 #ifdef DEBUG_DYNAMIC_X11
    33 #include <stdio.h>
    34 #endif
    35 
    36 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
    37 #include <dlfcn.h>
    38 #include "SDL_name.h"
    39 #include "SDL_loadso.h"
    40 static const char *x11_library = SDL_VIDEO_DRIVER_X11_DYNAMIC;
    41 static void *x11_handle = NULL;
    42 static const char *x11ext_library = SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT;
    43 static void *x11ext_handle = NULL;
    44 
    45 static void *X11_GetSym(int required, const char *fnname, int *rc)
    46 {
    47 	void *fn = NULL;
    48 	if (*rc) {  /* haven't already failed on a previous lookup? */
    49 		fn = SDL_LoadFunction(x11_handle, fnname);
    50 		#if DEBUG_DYNAMIC_X11
    51 		if (fn != NULL)
    52 			printf("X11: Found '%s' in libX11 (%p)\n", fnname, fn);
    53 		#endif
    54 
    55 		if (fn == NULL) {  /* not found? Check libX11ext ... */
    56 			fn = SDL_LoadFunction(x11ext_handle, fnname);
    57 			#if DEBUG_DYNAMIC_X11
    58 			if (fn != NULL)
    59 				printf("X11: Found '%s' in libXext (%p)\n", fnname, fn);
    60 			else
    61 				printf("X11: Symbol '%s' NOT FOUND!%s\n", fnname,
    62 				       required ? "" : " (...but not required!)");
    63 			#endif
    64 		}
    65 		*rc = ((fn != NULL) || (!required));
    66 	}
    67 
    68 	return fn;
    69 }
    70 #endif  /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
    71 
    72 /* Define all the function pointers... */
    73 #define SDL_X11_SYM(req,ret,fn,params) ret (*p##fn) params = NULL;
    74 #include "SDL_x11sym.h"
    75 #undef SDL_X11_SYM
    76 
    77 static int x11_load_refcount = 0;
    78 
    79 void SDL_X11_UnloadSymbols(void)
    80 {
    81 	/* Don't actually unload if more than one module is using the libs... */
    82 	if (x11_load_refcount > 0) {
    83 		if (--x11_load_refcount == 0) {
    84 			/* set all the function pointers to NULL. */
    85 			#define SDL_X11_SYM(req,ret,fn,params) p##fn = NULL;
    86 			#include "SDL_x11sym.h"
    87 			#undef SDL_X11_SYM
    88 
    89 			#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
    90 			if (x11_handle != NULL) {
    91 				SDL_UnloadObject(x11_handle);
    92 				x11_handle = NULL;
    93 			}
    94 			if (x11ext_handle != NULL) {
    95 				SDL_UnloadObject(x11ext_handle);
    96 				x11ext_handle = NULL;
    97 			}
    98 			#endif
    99 		}
   100 	}
   101 }
   102 
   103 /* returns non-zero if all needed symbols were loaded. */
   104 int SDL_X11_LoadSymbols(void)
   105 {
   106 	int rc = 1;
   107 
   108     /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   109 	if (x11_load_refcount++ == 0) {
   110 		#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   111 			x11_handle = SDL_LoadObject(x11_library);
   112 			x11ext_handle = SDL_LoadObject(x11ext_library);
   113 			rc = ((x11_handle != NULL) && (x11ext_handle != NULL));
   114 			#define SDL_X11_SYM(req,r,fn,arg) p##fn = X11_GetSym(req,#fn, &rc);
   115 			#include "SDL_x11sym.h"
   116 			#undef SDL_X11_SYM
   117 
   118 			if (!rc)
   119 				SDL_X11_UnloadSymbols();  /* in case one of these loaded... */
   120 
   121 		#else
   122 			#define SDL_X11_SYM(req,r,fn,arg) p##fn = fn;
   123 			#include "SDL_x11sym.h"
   124 			#undef SDL_X11_SYM
   125 		#endif
   126 	}
   127 
   128 	return rc;
   129 }
   130 
   131 /* end of SDL_x11dyn.c ... */
   132