src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 22 Mar 2006 11:13:58 +0000
changeset 1589 34cca785be57
parent 1587 7fd9fc1f2be5
child 1593 5115439d67b1
permissions -rw-r--r--
Xrandr support in the X11 target.

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