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