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