comparison src/video/x11/SDL_x11dyn.c @ 1168:045f186426e1

Dynamically load X11 libraries like we currently do for alsa, esd, etc. This allows you to run an SDL program on a system without Xlib, since it'll just report the x11 target unavailable at runtime.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 05 Nov 2005 19:53:37 +0000
parents
children f84c6f1397cd
comparison
equal deleted inserted replaced
1167:435c2e481299 1168:045f186426e1
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
23 /*#define DEBUG_DYNAMIC_X11 1*/
24
25 #include "SDL_x11dyn.h"
26
27 #ifdef DEBUG_DYNAMIC_X11
28 #include <stdio.h>
29 #endif
30
31 #ifdef X11_DYNAMIC
32 #include <dlfcn.h>
33 #include "SDL_name.h"
34 #include "SDL_loadso.h"
35 static const char *x11_library = X11_DYNAMIC;
36 static void *x11_handle = NULL;
37 static const char *x11ext_library = X11EXT_DYNAMIC;
38 static void *x11ext_handle = NULL;
39
40 static void *X11_GetSym(const char *fnname, int *rc)
41 {
42 void *fn = NULL;
43 if (*rc) { /* haven't already failed on a previous lookup? */
44 fn = SDL_LoadFunction(x11_handle, fnname);
45 #if DEBUG_DYNAMIC_X11
46 if (fn != NULL)
47 printf("X11: Found '%s' in libX11 (%p)\n", fnname, fn);
48 #endif
49
50 if (fn == NULL) { /* not found? Check libX11ext ... */
51 fn = SDL_LoadFunction(x11ext_handle, fnname);
52 #if DEBUG_DYNAMIC_X11
53 if (fn != NULL)
54 printf("X11: Found '%s' in libXext (%p)\n", fnname, fn);
55 else
56 printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
57 #endif
58 }
59 *rc = (fn != NULL);
60 }
61
62 return fn;
63 }
64 #endif /* defined X11_DYNAMIC */
65
66 /* Define all the function pointers... */
67 #define SDL_X11_SYM(ret,fn,params) ret (*p##fn) params = NULL;
68 #include "SDL_x11sym.h"
69 #undef SDL_X11_SYM
70
71 static int x11_load_refcount = 0;
72
73 void SDL_X11_UnloadSymbols(void)
74 {
75 /* Don't actually unload if more than one module is using the libs... */
76 if (x11_load_refcount > 0) {
77 if (--x11_load_refcount == 0) {
78 /* set all the function pointers to NULL. */
79 #define SDL_X11_SYM(ret,fn,params) p##fn = NULL;
80 #include "SDL_x11sym.h"
81 #undef SDL_X11_SYM
82
83 #ifdef X11_DYNAMIC
84 if (x11_handle != NULL) {
85 SDL_UnloadObject(x11_handle);
86 x11_handle = NULL;
87 }
88 if (x11ext_handle != NULL) {
89 SDL_UnloadObject(x11ext_handle);
90 x11ext_handle = NULL;
91 }
92 #endif
93 }
94 }
95 }
96
97 /* returns non-zero if all needed symbols were loaded. */
98 int SDL_X11_LoadSymbols(void)
99 {
100 int rc = 1;
101
102 /* deal with multiple modules (dga, x11, etc) needing these symbols... */
103 if (x11_load_refcount++ == 0) {
104 #ifdef X11_DYNAMIC
105 x11_handle = SDL_LoadObject(x11_library);
106 x11ext_handle = SDL_LoadObject(x11ext_library);
107 if ((x11_handle != NULL) && (x11ext_handle != NULL)) {
108 #define SDL_X11_SYM(r,fn,arg) p##fn = X11_GetSym(#fn, &rc);
109 #include "SDL_x11sym.h"
110 #undef SDL_X11_SYM
111 }
112
113 if (!rc)
114 SDL_X11_UnloadSymbols(); /* in case one of these loaded... */
115
116 #else
117 #define SDL_X11_SYM(r,fn,arg) p##fn = fn;
118 #include "SDL_x11sym.h"
119 #undef SDL_X11_SYM
120 #endif
121 }
122
123 return rc;
124 }
125
126 /* end of SDL_x11dyn.c ... */
127