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