1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22
23 #if SDL_VIDEO_DRIVER_X11
24
25 #define DEBUG_DYNAMIC_X11 0
26
27 #include "SDL_x11dyn.h"
28
29 #if DEBUG_DYNAMIC_X11
30 #include <stdio.h>
31 #endif
32
33 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
34
35 #include "SDL_name.h"
36 #include "SDL_loadso.h"
37
38 typedef struct
39 {
40 void *lib;
41 const char *libname;
42 } x11dynlib;
43
44 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
45 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
46 #endif
47 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR
48 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR NULL
49 #endif
50 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA
51 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA NULL
52 #endif
53 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2
54 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 NULL
55 #endif
56 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
57 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
58 #endif
59 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS
60 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS NULL
61 #endif
62 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE
63 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE NULL
64 #endif
65
66 static x11dynlib x11libs[] = {
67 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
68 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
69 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR},
70 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA},
71 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2},
72 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
73 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS},
74 {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE}
75 };
76
77 static void *
X11_GetSym(const char * fnname,int * pHasModule)78 X11_GetSym(const char *fnname, int *pHasModule)
79 {
80 int i;
81 void *fn = NULL;
82 for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
83 if (x11libs[i].lib != NULL) {
84 fn = SDL_LoadFunction(x11libs[i].lib, fnname);
85 if (fn != NULL)
86 break;
87 }
88 }
89
90 #if DEBUG_DYNAMIC_X11
91 if (fn != NULL)
92 printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname, fn);
93 else
94 printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
95 #endif
96
97 if (fn == NULL)
98 *pHasModule = 0; /* kill this module. */
99
100 return fn;
101 }
102
103 #endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
104
105 /* Define all the function pointers and wrappers... */
106 #define SDL_X11_SYM(rc,fn,params,args,ret) SDL_DYNX11FN_##fn X11_##fn = NULL;
107 #include "SDL_x11sym.h"
108
109 /* Annoying varargs entry point... */
110 #ifdef X_HAVE_UTF8_STRING
111 SDL_DYNX11FN_XCreateIC X11_XCreateIC = NULL;
112 SDL_DYNX11FN_XGetICValues X11_XGetICValues = NULL;
113 #endif
114
115 /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
116 #define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 0;
117 #include "SDL_x11sym.h"
118
119 static int x11_load_refcount = 0;
120
121 void
SDL_X11_UnloadSymbols(void)122 SDL_X11_UnloadSymbols(void)
123 {
124 /* Don't actually unload if more than one module is using the libs... */
125 if (x11_load_refcount > 0) {
126 if (--x11_load_refcount == 0) {
127 int i;
128
129 /* set all the function pointers to NULL. */
130 #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 0;
131 #define SDL_X11_SYM(rc,fn,params,args,ret) X11_##fn = NULL;
132 #include "SDL_x11sym.h"
133
134 #ifdef X_HAVE_UTF8_STRING
135 X11_XCreateIC = NULL;
136 X11_XGetICValues = NULL;
137 #endif
138
139 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
140 for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
141 if (x11libs[i].lib != NULL) {
142 SDL_UnloadObject(x11libs[i].lib);
143 x11libs[i].lib = NULL;
144 }
145 }
146 #endif
147 }
148 }
149 }
150
151 /* returns non-zero if all needed symbols were loaded. */
152 int
SDL_X11_LoadSymbols(void)153 SDL_X11_LoadSymbols(void)
154 {
155 int rc = 1; /* always succeed if not using Dynamic X11 stuff. */
156
157 /* deal with multiple modules (dga, x11, etc) needing these symbols... */
158 if (x11_load_refcount++ == 0) {
159 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
160 int i;
161 int *thismod = NULL;
162 for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
163 if (x11libs[i].libname != NULL) {
164 x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
165 }
166 }
167
168 #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1; /* default yes */
169 #include "SDL_x11sym.h"
170
171 #define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
172 #define SDL_X11_SYM(a,fn,x,y,z) X11_##fn = (SDL_DYNX11FN_##fn) X11_GetSym(#fn,thismod);
173 #include "SDL_x11sym.h"
174
175 #ifdef X_HAVE_UTF8_STRING
176 X11_XCreateIC = (SDL_DYNX11FN_XCreateIC)
177 X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8);
178 X11_XGetICValues = (SDL_DYNX11FN_XGetICValues)
179 X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8);
180 #endif
181
182 if (SDL_X11_HAVE_BASEXLIB) {
183 /* all required symbols loaded. */
184 SDL_ClearError();
185 } else {
186 /* in case something got loaded... */
187 SDL_X11_UnloadSymbols();
188 rc = 0;
189 }
190
191 #else /* no dynamic X11 */
192
193 #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1; /* default yes */
194 #define SDL_X11_SYM(a,fn,x,y,z) X11_##fn = (SDL_DYNX11FN_##fn) fn;
195 #include "SDL_x11sym.h"
196
197 #ifdef X_HAVE_UTF8_STRING
198 X11_XCreateIC = XCreateIC;
199 X11_XGetICValues = XGetICValues;
200 #endif
201 #endif
202 }
203
204 return rc;
205 }
206
207 #endif /* SDL_VIDEO_DRIVER_X11 */
208
209 /* vi: set ts=4 sw=4 expandtab: */
210