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.
icculus@1168
     1
/*
icculus@1168
     2
    SDL - Simple DirectMedia Layer
icculus@1168
     3
    Copyright (C) 1997-2004 Sam Lantinga
icculus@1168
     4
icculus@1168
     5
    This library is free software; you can redistribute it and/or
icculus@1168
     6
    modify it under the terms of the GNU Library General Public
icculus@1168
     7
    License as published by the Free Software Foundation; either
icculus@1168
     8
    version 2 of the License, or (at your option) any later version.
icculus@1168
     9
icculus@1168
    10
    This library is distributed in the hope that it will be useful,
icculus@1168
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1168
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1168
    13
    Library General Public License for more details.
icculus@1168
    14
icculus@1168
    15
    You should have received a copy of the GNU Library General Public
icculus@1168
    16
    License along with this library; if not, write to the Free
icculus@1168
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
icculus@1168
    18
icculus@1168
    19
    Sam Lantinga
icculus@1168
    20
    slouken@libsdl.org
icculus@1168
    21
*/
icculus@1168
    22
icculus@1235
    23
#if 0
icculus@1235
    24
#define DEBUG_DYNAMIC_X11 1
icculus@1235
    25
#endif
icculus@1168
    26
icculus@1226
    27
#define __SDL_NO_REDEFINE_X11_HEADER_SYMS 1
icculus@1168
    28
#include "SDL_x11dyn.h"
icculus@1168
    29
icculus@1168
    30
#ifdef DEBUG_DYNAMIC_X11
icculus@1168
    31
#include <stdio.h>
icculus@1168
    32
#endif
icculus@1168
    33
icculus@1168
    34
#ifdef X11_DYNAMIC
icculus@1168
    35
#include <dlfcn.h>
icculus@1168
    36
#include "SDL_name.h"
icculus@1168
    37
#include "SDL_loadso.h"
icculus@1168
    38
static const char *x11_library = X11_DYNAMIC;
icculus@1168
    39
static void *x11_handle = NULL;
icculus@1168
    40
static const char *x11ext_library = X11EXT_DYNAMIC;
icculus@1168
    41
static void *x11ext_handle = NULL;
icculus@1168
    42
icculus@1235
    43
static void *X11_GetSym(int required, const char *fnname, int *rc)
icculus@1168
    44
{
icculus@1168
    45
	void *fn = NULL;
icculus@1168
    46
	if (*rc) {  /* haven't already failed on a previous lookup? */
icculus@1168
    47
		fn = SDL_LoadFunction(x11_handle, fnname);
icculus@1168
    48
		#if DEBUG_DYNAMIC_X11
icculus@1168
    49
		if (fn != NULL)
icculus@1168
    50
			printf("X11: Found '%s' in libX11 (%p)\n", fnname, fn);
icculus@1168
    51
		#endif
icculus@1168
    52
icculus@1168
    53
		if (fn == NULL) {  /* not found? Check libX11ext ... */
icculus@1168
    54
			fn = SDL_LoadFunction(x11ext_handle, fnname);
icculus@1168
    55
			#if DEBUG_DYNAMIC_X11
icculus@1168
    56
			if (fn != NULL)
icculus@1168
    57
				printf("X11: Found '%s' in libXext (%p)\n", fnname, fn);
icculus@1168
    58
			else
icculus@1168
    59
				printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
icculus@1168
    60
			#endif
icculus@1168
    61
		}
icculus@1235
    62
		*rc = ((fn != NULL) || (!required));
icculus@1168
    63
	}
icculus@1168
    64
icculus@1168
    65
	return fn;
icculus@1168
    66
}
icculus@1168
    67
#endif  /* defined X11_DYNAMIC */
icculus@1168
    68
icculus@1168
    69
/* Define all the function pointers... */
icculus@1235
    70
#define SDL_X11_SYM(req,ret,fn,params) ret (*p##fn) params = NULL;
icculus@1168
    71
#include "SDL_x11sym.h"
icculus@1168
    72
#undef SDL_X11_SYM
icculus@1168
    73
icculus@1168
    74
static int x11_load_refcount = 0;
icculus@1168
    75
icculus@1168
    76
void SDL_X11_UnloadSymbols(void)
icculus@1168
    77
{
icculus@1168
    78
	/* Don't actually unload if more than one module is using the libs... */
icculus@1168
    79
	if (x11_load_refcount > 0) {
icculus@1168
    80
		if (--x11_load_refcount == 0) {
icculus@1168
    81
			/* set all the function pointers to NULL. */
icculus@1235
    82
			#define SDL_X11_SYM(req,ret,fn,params) p##fn = NULL;
icculus@1168
    83
			#include "SDL_x11sym.h"
icculus@1168
    84
			#undef SDL_X11_SYM
icculus@1168
    85
icculus@1168
    86
			#ifdef X11_DYNAMIC
icculus@1168
    87
			if (x11_handle != NULL) {
icculus@1168
    88
				SDL_UnloadObject(x11_handle);
icculus@1168
    89
				x11_handle = NULL;
icculus@1168
    90
			}
icculus@1168
    91
			if (x11ext_handle != NULL) {
icculus@1168
    92
				SDL_UnloadObject(x11ext_handle);
icculus@1168
    93
				x11ext_handle = NULL;
icculus@1168
    94
			}
icculus@1168
    95
			#endif
icculus@1168
    96
		}
icculus@1168
    97
	}
icculus@1168
    98
}
icculus@1168
    99
icculus@1168
   100
/* returns non-zero if all needed symbols were loaded. */
icculus@1168
   101
int SDL_X11_LoadSymbols(void)
icculus@1168
   102
{
icculus@1168
   103
	int rc = 1;
icculus@1168
   104
icculus@1168
   105
    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
icculus@1168
   106
	if (x11_load_refcount++ == 0) {
icculus@1168
   107
		#ifdef X11_DYNAMIC
icculus@1168
   108
			x11_handle = SDL_LoadObject(x11_library);
icculus@1168
   109
			x11ext_handle = SDL_LoadObject(x11ext_library);
icculus@1171
   110
			rc = ((x11_handle != NULL) && (x11ext_handle != NULL));
icculus@1235
   111
			#define SDL_X11_SYM(req,r,fn,arg) p##fn = X11_GetSym(req,#fn, &rc);
icculus@1171
   112
			#include "SDL_x11sym.h"
icculus@1171
   113
			#undef SDL_X11_SYM
icculus@1168
   114
icculus@1168
   115
			if (!rc)
icculus@1168
   116
				SDL_X11_UnloadSymbols();  /* in case one of these loaded... */
icculus@1168
   117
icculus@1168
   118
		#else
icculus@1235
   119
			#define SDL_X11_SYM(req,r,fn,arg) p##fn = fn;
icculus@1168
   120
			#include "SDL_x11sym.h"
icculus@1168
   121
			#undef SDL_X11_SYM
icculus@1168
   122
		#endif
icculus@1168
   123
	}
icculus@1168
   124
icculus@1168
   125
	return rc;
icculus@1168
   126
}
icculus@1168
   127
icculus@1168
   128
/* end of SDL_x11dyn.c ... */
icculus@1168
   129