src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 13 Jun 2007 07:54:10 +0000
branchSDL-1.2
changeset 3957 b1d5b56aa549
parent 1874 9e85b267f8f3
child 3958 85b6fb6a5e3c
permissions -rw-r--r--
Date: Mon, 4 Jun 2007 06:17:59 +0000 (UTC)
From: George Gensure <werkt0@gmail.com>
Subject: [SDL] error removal patch for SDL_x11dyn.c

I found that calling SDL_GetError when an error hasn't necessarily occurred is
still reporting problems from loadso regarding dynamic functions in X11. I've
added the following to my local copy to avoid revealing the 'many library
lookup' approach adopted by x11dyn.c:
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 "SDL_name.h"
icculus@1168
    35
#include "SDL_loadso.h"
icculus@1168
    36
icculus@1575
    37
typedef struct
icculus@1168
    38
{
icculus@1575
    39
    void *lib;
icculus@1575
    40
    const char *libname;
icculus@1593
    41
} x11dynlib;
icculus@1593
    42
icculus@1593
    43
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
icculus@1593
    44
#define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
icculus@1593
    45
#endif
icculus@1593
    46
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
icculus@1593
    47
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
icculus@1593
    48
#endif
icculus@1593
    49
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER
icculus@1593
    50
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER NULL
icculus@1593
    51
#endif
icculus@1593
    52
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
icculus@1593
    53
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
icculus@1593
    54
#endif
icculus@1593
    55
icculus@1593
    56
static x11dynlib x11libs[] =
icculus@1593
    57
{
icculus@1593
    58
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC },
icculus@1593
    59
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT },
icculus@1593
    60
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER },
icculus@1593
    61
    { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR },
icculus@1593
    62
};
icculus@1575
    63
slouken@1842
    64
static void X11_GetSym(const char *fnname, int *rc, void **fn)
icculus@1575
    65
{
icculus@1575
    66
	int i;
icculus@1593
    67
	for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
icculus@1593
    68
		if (x11libs[i].lib != NULL)
icculus@1575
    69
		{
slouken@1842
    70
			*fn = SDL_LoadFunction(x11libs[i].lib, fnname);
slouken@1842
    71
			if (*fn != NULL)
icculus@1575
    72
				break;
icculus@1168
    73
		}
icculus@1168
    74
	}
icculus@1168
    75
icculus@3957
    76
	if (*fn != NULL)
icculus@3957
    77
		SDL_ClearError();
icculus@3957
    78
	else
icculus@3957
    79
		SDL_SetError("Failed to load function %s from x11libs", fnname);
icculus@3957
    80
icculus@1575
    81
	#if DEBUG_DYNAMIC_X11
slouken@1842
    82
	if (*fn != NULL)
slouken@1842
    83
		printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname, *fn);
icculus@1575
    84
	else
icculus@1575
    85
		printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
icculus@1575
    86
	#endif
icculus@1575
    87
slouken@1842
    88
	if (*fn == NULL)
icculus@1575
    89
		*rc = 0;  /* kill this module. */
icculus@1168
    90
}
icculus@1575
    91
icculus@1575
    92
icculus@1575
    93
/* Define all the function pointers and wrappers... */
icculus@1575
    94
#define SDL_X11_MODULE(modname)
icculus@1575
    95
#define SDL_X11_SYM(rc,fn,params,args,ret) \
icculus@1575
    96
	static rc (*p##fn) params = NULL; \
icculus@1575
    97
	rc fn params { ret p##fn args ; }
icculus@1575
    98
#include "SDL_x11sym.h"
icculus@1575
    99
#undef SDL_X11_MODULE
icculus@1575
   100
#undef SDL_X11_SYM
slouken@1361
   101
#endif  /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
icculus@1168
   102
icculus@1575
   103
/* Annoying varargs entry point... */
icculus@1575
   104
#ifdef X_HAVE_UTF8_STRING
icculus@1575
   105
XIC (*pXCreateIC)(XIM,...) = NULL;
icculus@1575
   106
#endif
icculus@1575
   107
icculus@1575
   108
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
icculus@1575
   109
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
icculus@1575
   110
#define SDL_X11_SYM(rc,fn,params,args,ret)
icculus@1168
   111
#include "SDL_x11sym.h"
icculus@1575
   112
#undef SDL_X11_MODULE
icculus@1168
   113
#undef SDL_X11_SYM
icculus@1168
   114
icculus@1575
   115
icculus@1168
   116
static int x11_load_refcount = 0;
icculus@1168
   117
icculus@1168
   118
void SDL_X11_UnloadSymbols(void)
icculus@1168
   119
{
icculus@1575
   120
	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
icculus@1168
   121
	/* Don't actually unload if more than one module is using the libs... */
icculus@1168
   122
	if (x11_load_refcount > 0) {
icculus@1168
   123
		if (--x11_load_refcount == 0) {
icculus@1593
   124
			int i;
icculus@1593
   125
icculus@1168
   126
			/* set all the function pointers to NULL. */
icculus@1575
   127
			#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
icculus@1575
   128
			#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
icculus@1168
   129
			#include "SDL_x11sym.h"
icculus@1575
   130
			#undef SDL_X11_MODULE
icculus@1168
   131
			#undef SDL_X11_SYM
icculus@1168
   132
icculus@1593
   133
			#ifdef X_HAVE_UTF8_STRING
icculus@1593
   134
			pXCreateIC = NULL;
icculus@1593
   135
			#endif
icculus@1575
   136
icculus@1593
   137
			for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
icculus@1593
   138
				if (x11libs[i].lib != NULL) {
icculus@1593
   139
					SDL_UnloadObject(x11libs[i].lib);
icculus@1593
   140
					x11libs[i].lib = NULL;
icculus@1593
   141
				}
icculus@1589
   142
			}
icculus@1168
   143
		}
icculus@1168
   144
	}
icculus@1575
   145
	#endif
icculus@1168
   146
}
icculus@1168
   147
icculus@1168
   148
/* returns non-zero if all needed symbols were loaded. */
icculus@1168
   149
int SDL_X11_LoadSymbols(void)
icculus@1168
   150
{
icculus@1575
   151
	int rc = 1;  /* always succeed if not using Dynamic X11 stuff. */
icculus@1168
   152
icculus@1575
   153
	#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
icculus@1575
   154
	/* deal with multiple modules (dga, x11, etc) needing these symbols... */
icculus@1168
   155
	if (x11_load_refcount++ == 0) {
icculus@1593
   156
		int i;
icculus@1575
   157
		int *thismod = NULL;
icculus@1593
   158
		for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
icculus@1593
   159
			if (x11libs[i].libname != NULL) {
icculus@1593
   160
				x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
icculus@1593
   161
			}
icculus@1593
   162
		}
icculus@1575
   163
		#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
slouken@1842
   164
		#define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
icculus@1575
   165
		#include "SDL_x11sym.h"
icculus@1575
   166
		#undef SDL_X11_MODULE
icculus@1575
   167
		#undef SDL_X11_SYM
icculus@1168
   168
icculus@1575
   169
		#ifdef X_HAVE_UTF8_STRING
slouken@1842
   170
		X11_GetSym("XCreateIC",&SDL_X11_HAVE_UTF8,(void **)&pXCreateIC);
icculus@1575
   171
		#endif
icculus@1168
   172
icculus@1575
   173
		if (!SDL_X11_HAVE_BASEXLIB) {  /* some required symbol didn't load. */
icculus@1575
   174
			SDL_X11_UnloadSymbols();  /* in case something got loaded... */
icculus@1610
   175
			rc = 0;
icculus@1575
   176
		}
icculus@1575
   177
	}
icculus@1575
   178
	#else
icculus@1874
   179
		#if DEBUG_DYNAMIC_X11
icculus@1874
   180
		printf("X11: No dynamic X11 support in this build of SDL.\n");
icculus@1874
   181
		#endif
icculus@1575
   182
		#ifdef X_HAVE_UTF8_STRING
icculus@1575
   183
		pXCreateIC = XCreateIC;
icculus@1168
   184
		#endif
icculus@1575
   185
	#endif
icculus@1168
   186
icculus@1168
   187
	return rc;
icculus@1168
   188
}
icculus@1168
   189
icculus@1168
   190
/* end of SDL_x11dyn.c ... */
icculus@1168
   191