src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 24 Mar 2006 03:49:37 +0000
changeset 1610 a1ee5944412b
parent 1593 5115439d67b1
child 1648 9f59d4c5aaea
permissions -rw-r--r--
SDL_X11_LoadSymbols() was reporting success when required symbols weren't
found!
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
*/
slouken@1402
    22
#include "SDL_config.h"
icculus@1168
    23
icculus@1589
    24
#define DEBUG_DYNAMIC_X11 0
icculus@1168
    25
icculus@1168
    26
#include "SDL_x11dyn.h"
icculus@1168
    27
icculus@1589
    28
#if DEBUG_DYNAMIC_X11
icculus@1168
    29
#include <stdio.h>
icculus@1168
    30
#endif
icculus@1168
    31
slouken@1361
    32
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
icculus@1593
    33
icculus@1168
    34
#include <dlfcn.h>
icculus@1168
    35
#include "SDL_name.h"
icculus@1168
    36
#include "SDL_loadso.h"
icculus@1168
    37
icculus@1575
    38
typedef struct
icculus@1168
    39
{
icculus@1575
    40
    void *lib;
icculus@1575
    41
    const char *libname;
icculus@1593
    42
} x11dynlib;
icculus@1593
    43
icculus@1593
    44
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
icculus@1593
    45
#define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
icculus@1593
    46
#endif
icculus@1593
    47
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
icculus@1593
    48
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
icculus@1593
    49
#endif
icculus@1593
    50
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER
icculus@1593
    51
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER NULL
icculus@1593
    52
#endif
icculus@1593
    53
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
icculus@1593
    54
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
icculus@1593
    55
#endif
icculus@1593
    56
icculus@1593
    57
static x11dynlib x11libs[] =
icculus@1593
    58
{
icculus@1593
    59
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC },
icculus@1593
    60
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT },
icculus@1593
    61
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER },
icculus@1593
    62
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR },
icculus@1593
    63
};
icculus@1575
    64
icculus@1575
    65
static void *X11_GetSym(const char *fnname, int *rc)
icculus@1575
    66
{
icculus@1575
    67
	int i;
icculus@1168
    68
	void *fn = NULL;
icculus@1593
    69
	for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
icculus@1593
    70
		if (x11libs[i].lib != NULL)
icculus@1575
    71
		{
icculus@1593
    72
			fn = SDL_LoadFunction(x11libs[i].lib, fnname);
icculus@1168
    73
			if (fn != NULL)
icculus@1575
    74
				break;
icculus@1168
    75
		}
icculus@1168
    76
	}
icculus@1168
    77
icculus@1575
    78
	#if DEBUG_DYNAMIC_X11
icculus@1575
    79
	if (fn != NULL)
icculus@1593
    80
		printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname, fn);
icculus@1575
    81
	else
icculus@1575
    82
		printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
icculus@1575
    83
	#endif
icculus@1575
    84
icculus@1575
    85
	if (fn == NULL)
icculus@1575
    86
		*rc = 0;  /* kill this module. */
icculus@1575
    87
icculus@1168
    88
	return fn;
icculus@1168
    89
}
icculus@1575
    90
icculus@1575
    91
icculus@1575
    92
/* Define all the function pointers and wrappers... */
icculus@1575
    93
#define SDL_X11_MODULE(modname)
icculus@1575
    94
#define SDL_X11_SYM(rc,fn,params,args,ret) \
icculus@1575
    95
	static rc (*p##fn) params = NULL; \
icculus@1575
    96
	rc fn params { ret p##fn args ; }
icculus@1575
    97
#include "SDL_x11sym.h"
icculus@1575
    98
#undef SDL_X11_MODULE
icculus@1575
    99
#undef SDL_X11_SYM
slouken@1361
   100
#endif  /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
icculus@1168
   101
icculus@1575
   102
/* Annoying varargs entry point... */
icculus@1575
   103
#ifdef X_HAVE_UTF8_STRING
icculus@1575
   104
XIC (*pXCreateIC)(XIM,...) = NULL;
icculus@1575
   105
#endif
icculus@1575
   106
icculus@1575
   107
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
icculus@1575
   108
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
icculus@1575
   109
#define SDL_X11_SYM(rc,fn,params,args,ret)
icculus@1168
   110
#include "SDL_x11sym.h"
icculus@1575
   111
#undef SDL_X11_MODULE
icculus@1168
   112
#undef SDL_X11_SYM
icculus@1168
   113
icculus@1575
   114
icculus@1168
   115
static int x11_load_refcount = 0;
icculus@1168
   116
icculus@1168
   117
void SDL_X11_UnloadSymbols(void)
icculus@1168
   118
{
icculus@1575
   119
	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
icculus@1168
   120
	/* Don't actually unload if more than one module is using the libs... */
icculus@1168
   121
	if (x11_load_refcount > 0) {
icculus@1168
   122
		if (--x11_load_refcount == 0) {
icculus@1593
   123
			int i;
icculus@1593
   124
icculus@1168
   125
			/* set all the function pointers to NULL. */
icculus@1575
   126
			#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
icculus@1575
   127
			#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
icculus@1168
   128
			#include "SDL_x11sym.h"
icculus@1575
   129
			#undef SDL_X11_MODULE
icculus@1168
   130
			#undef SDL_X11_SYM
icculus@1168
   131
icculus@1593
   132
			#ifdef X_HAVE_UTF8_STRING
icculus@1593
   133
			pXCreateIC = NULL;
icculus@1593
   134
			#endif
icculus@1575
   135
icculus@1593
   136
			for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
icculus@1593
   137
				if (x11libs[i].lib != NULL) {
icculus@1593
   138
					SDL_UnloadObject(x11libs[i].lib);
icculus@1593
   139
					x11libs[i].lib = NULL;
icculus@1593
   140
				}
icculus@1589
   141
			}
icculus@1168
   142
		}
icculus@1168
   143
	}
icculus@1575
   144
	#endif
icculus@1168
   145
}
icculus@1168
   146
icculus@1168
   147
/* returns non-zero if all needed symbols were loaded. */
icculus@1168
   148
int SDL_X11_LoadSymbols(void)
icculus@1168
   149
{
icculus@1575
   150
	int rc = 1;  /* always succeed if not using Dynamic X11 stuff. */
icculus@1168
   151
icculus@1575
   152
	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
icculus@1575
   153
	/* deal with multiple modules (dga, x11, etc) needing these symbols... */
icculus@1168
   154
	if (x11_load_refcount++ == 0) {
icculus@1593
   155
		int i;
icculus@1575
   156
		int *thismod = NULL;
icculus@1593
   157
		for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
icculus@1593
   158
			if (x11libs[i].libname != NULL) {
icculus@1593
   159
				x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
icculus@1593
   160
			}
icculus@1593
   161
		}
icculus@1575
   162
		#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
icculus@1575
   163
		#define SDL_X11_SYM(a,fn,x,y,z) p##fn = X11_GetSym(#fn,thismod);
icculus@1575
   164
		#include "SDL_x11sym.h"
icculus@1575
   165
		#undef SDL_X11_MODULE
icculus@1575
   166
		#undef SDL_X11_SYM
icculus@1168
   167
icculus@1575
   168
		#ifdef X_HAVE_UTF8_STRING
icculus@1575
   169
		pXCreateIC = X11_GetSym("XCreateIC",&SDL_X11_HAVE_UTF8);
icculus@1575
   170
		#endif
icculus@1168
   171
icculus@1575
   172
		if (!SDL_X11_HAVE_BASEXLIB) {  /* some required symbol didn't load. */
icculus@1575
   173
			SDL_X11_UnloadSymbols();  /* in case something got loaded... */
icculus@1610
   174
			rc = 0;
icculus@1575
   175
		}
icculus@1575
   176
	}
icculus@1575
   177
	#else
icculus@1575
   178
		#ifdef X_HAVE_UTF8_STRING
icculus@1575
   179
		pXCreateIC = XCreateIC;
icculus@1168
   180
		#endif
icculus@1575
   181
	#endif
icculus@1168
   182
icculus@1168
   183
	return rc;
icculus@1168
   184
}
icculus@1168
   185
icculus@1168
   186
/* end of SDL_x11dyn.c ... */
icculus@1168
   187