1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 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
22 #include "SDL_config.h"
23 #include "SDL_dynapi.h"
24
25 #if SDL_DYNAMIC_API
26
27 #include "SDL.h"
28
29 /* !!! FIXME: Shouldn't these be included in SDL.h? */
30 #include "SDL_shape.h"
31 #include "SDL_syswm.h"
32
33 /* This is the version of the dynamic API. This doesn't match the SDL version
34 and should not change until there's been a major revamp in API/ABI.
35 So 2.0.5 adds functions over 2.0.4? This number doesn't change;
36 the sizeof (jump_table) changes instead. But 2.1.0 changes how a function
37 works in an incompatible way or removes a function? This number changes,
38 since sizeof (jump_table) isn't sufficient anymore. It's likely
39 we'll forget to bump every time we add a function, so this is the
40 failsafe switch for major API change decisions. Respect it and use it
41 sparingly. */
42 #define SDL_DYNAPI_VERSION 1
43
44 static void SDL_InitDynamicAPI(void);
45
46
47 /* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
48 Even self-contained stuff might call SDL_Error and break everything. */
49
50
51 /* behold, the macro salsa! */
52
53 /* !!! FIXME: ...disabled...until we write it. :) */
54 #define DISABLE_JUMP_MAGIC 1
55
56 #if DISABLE_JUMP_MAGIC
57 /* Can't use the macro for varargs nonsense. This is atrocious. */
58 #define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio) \
59 _static void SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
60 va_list ap; initcall; va_start(ap, fmt); \
61 jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
62 va_end(ap); \
63 }
64
65 #define SDL_DYNAPI_VARARGS(_static, name, initcall) \
66 _static int SDL_SetError##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
67 char buf[512]; /* !!! FIXME: dynamic allocation */ \
68 va_list ap; initcall; va_start(ap, fmt); \
69 jump_table.SDL_vsnprintf(buf, sizeof (buf), fmt, ap); \
70 va_end(ap); \
71 return jump_table.SDL_SetError("%s", buf); \
72 } \
73 _static int SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) { \
74 int retval; va_list ap; initcall; va_start(ap, fmt); \
75 retval = jump_table.SDL_vsscanf(buf, fmt, ap); \
76 va_end(ap); \
77 return retval; \
78 } \
79 _static int SDL_snprintf##name(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
80 int retval; va_list ap; initcall; va_start(ap, fmt); \
81 retval = jump_table.SDL_vsnprintf(buf, maxlen, fmt, ap); \
82 va_end(ap); \
83 return retval; \
84 } \
85 _static void SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
86 va_list ap; initcall; va_start(ap, fmt); \
87 jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \
88 va_end(ap); \
89 } \
90 _static void SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
91 va_list ap; initcall; va_start(ap, fmt); \
92 jump_table.SDL_LogMessageV(category, priority, fmt, ap); \
93 va_end(ap); \
94 } \
95 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Verbose, VERBOSE) \
96 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Debug, DEBUG) \
97 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Info, INFO) \
98 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Warn, WARN) \
99 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Error, ERROR) \
100 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Critical, CRITICAL)
101 #endif
102
103
104 /* Typedefs for function pointers for jump table, and predeclare funcs */
105 /* The DEFAULT funcs will init jump table and then call real function. */
106 /* The REAL funcs are the actual functions, name-mangled to not clash. */
107 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
108 typedef rc (*SDL_DYNAPIFN_##fn) params; \
109 static rc fn##_DEFAULT params; \
110 extern rc fn##_REAL params;
111 #include "SDL_dynapi_procs.h"
112 #undef SDL_DYNAPI_PROC
113
114 /* The jump table! */
115 typedef struct {
116 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) SDL_DYNAPIFN_##fn fn;
117 #include "SDL_dynapi_procs.h"
118 #undef SDL_DYNAPI_PROC
119 } SDL_DYNAPI_jump_table;
120
121 /* Predeclare the default functions for initializing the jump table. */
122 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) static rc fn##_DEFAULT params;
123 #include "SDL_dynapi_procs.h"
124 #undef SDL_DYNAPI_PROC
125
126 /* The actual jump table. */
127 static SDL_DYNAPI_jump_table jump_table = {
128 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) fn##_DEFAULT,
129 #include "SDL_dynapi_procs.h"
130 #undef SDL_DYNAPI_PROC
131 };
132
133 /* Default functions init the function table then call right thing. */
134 #if DISABLE_JUMP_MAGIC
135 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
136 static rc fn##_DEFAULT params { \
137 SDL_InitDynamicAPI(); \
138 ret jump_table.fn args; \
139 }
140 #define SDL_DYNAPI_PROC_NO_VARARGS 1
141 #include "SDL_dynapi_procs.h"
142 #undef SDL_DYNAPI_PROC
143 #undef SDL_DYNAPI_PROC_NO_VARARGS
144 SDL_DYNAPI_VARARGS(static, _DEFAULT, SDL_InitDynamicAPI())
145 #else
146 /* !!! FIXME: need the jump magic. */
147 #error Write me.
148 #endif
149
150 /* Public API functions to jump into the jump table. */
151 #if DISABLE_JUMP_MAGIC
152 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
153 rc fn params { ret jump_table.fn args; }
154 #define SDL_DYNAPI_PROC_NO_VARARGS 1
155 #include "SDL_dynapi_procs.h"
156 #undef SDL_DYNAPI_PROC
157 #undef SDL_DYNAPI_PROC_NO_VARARGS
158 SDL_DYNAPI_VARARGS(,,)
159 #else
160 /* !!! FIXME: need the jump magic. */
161 #error Write me.
162 #endif
163
164
165
166 /* Here's the exported entry point that fills in the jump table. */
167 /* Use specific types when an "int" might suffice to keep this sane. */
168 typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
169 extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
170
171 Sint32
SDL_DYNAPI_entry(Uint32 apiver,void * table,Uint32 tablesize)172 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
173 {
174 SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
175
176 if (apiver != SDL_DYNAPI_VERSION) {
177 /* !!! FIXME: can maybe handle older versions? */
178 return -1; /* not compatible. */
179 } else if (tablesize > sizeof (jump_table)) {
180 return -1; /* newer version of SDL with functions we can't provide. */
181 }
182
183 /* Init our jump table first. */
184 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) jump_table.fn = fn##_REAL;
185 #include "SDL_dynapi_procs.h"
186 #undef SDL_DYNAPI_PROC
187
188 /* Then the external table... */
189 if (output_jump_table != &jump_table) {
190 jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
191 }
192
193 /* Safe to call SDL functions now; jump table is initialized! */
194
195 return 0; /* success! */
196 }
197
198
199 /* Obviously we can't use SDL_LoadObject() to load SDL. :) */
200 /* Also obviously, we never close the loaded library. */
201 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
202 #ifndef WIN32_LEAN_AND_MEAN
203 #define WIN32_LEAN_AND_MEAN 1
204 #endif
205 #include <windows.h>
get_sdlapi_entry(const char * fname,const char * sym)206 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
207 {
208 HANDLE lib = LoadLibraryA(fname);
209 void *retval = NULL;
210 if (lib) {
211 retval = GetProcAddress(lib, sym);
212 if (retval == NULL) {
213 FreeLibrary(lib);
214 }
215 }
216 return retval;
217 }
218
219 #elif defined(__HAIKU__)
220 #include <os/kernel/image.h>
get_sdlapi_entry(const char * fname,const char * sym)221 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
222 {
223 image_id lib = load_add_on(fname);
224 void *retval = NULL;
225 if (lib >= 0) {
226 if (get_image_symbol(lib, sym, B_SYMBOL_TYPE_TEXT, &retval) != B_NO_ERROR) {
227 unload_add_on(lib);
228 retval = NULL;
229 }
230 }
231 return retval;
232 }
233 #elif defined(unix) || defined(__unix__) || defined(__APPLE__)
234 #include <dlfcn.h>
get_sdlapi_entry(const char * fname,const char * sym)235 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
236 {
237 void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
238 void *retval = NULL;
239 if (lib != NULL) {
240 retval = dlsym(lib, sym);
241 if (retval == NULL) {
242 dlclose(lib);
243 }
244 }
245 return retval;
246 }
247 #else
248 #error Please define your platform.
249 #endif
250
251
252 static void
SDL_InitDynamicAPILocked(void)253 SDL_InitDynamicAPILocked(void)
254 {
255 const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
256 SDL_DYNAPI_ENTRYFN entry = SDL_DYNAPI_entry; /* funcs from here by default. */
257
258 if (libname) {
259 entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
260 if (!entry) {
261 /* !!! FIXME: fail to startup here instead? */
262 /* !!! FIXME: definitely warn user. */
263 /* Just fill in the function pointers from this library. */
264 entry = SDL_DYNAPI_entry;
265 }
266 }
267
268 if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
269 /* !!! FIXME: fail to startup here instead? */
270 /* !!! FIXME: definitely warn user. */
271 /* Just fill in the function pointers from this library. */
272 if (entry != SDL_DYNAPI_entry) {
273 if (!SDL_DYNAPI_entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table))) {
274 /* !!! FIXME: now we're screwed. Should definitely abort now. */
275 }
276 }
277 }
278
279 /* we intentionally never close the newly-loaded lib, of course. */
280 }
281
282 static void
SDL_InitDynamicAPI(void)283 SDL_InitDynamicAPI(void)
284 {
285 /* So the theory is that every function in the jump table defaults to
286 * calling this function, and then replaces itself with a version that
287 * doesn't call this function anymore. But it's possible that, in an
288 * extreme corner case, you can have a second thread hit this function
289 * while the jump table is being initialized by the first.
290 * In this case, a spinlock is really painful compared to what spinlocks
291 * _should_ be used for, but this would only happen once, and should be
292 * insanely rare, as you would have to spin a thread outside of SDL (as
293 * SDL_CreateThread() would also call this function before building the
294 * new thread).
295 */
296 static SDL_bool already_initialized = SDL_FALSE;
297
298 /* SDL_AtomicLock calls SDL mutex functions to emulate if
299 SDL_ATOMIC_DISABLED, which we can't do here, so in such a
300 configuration, you're on your own. */
301 #if !SDL_ATOMIC_DISABLED
302 static SDL_SpinLock lock = 0;
303 SDL_AtomicLock_REAL(&lock);
304 #endif
305
306 if (!already_initialized) {
307 SDL_InitDynamicAPILocked();
308 already_initialized = SDL_TRUE;
309 }
310
311 #if !SDL_ATOMIC_DISABLED
312 SDL_AtomicUnlock_REAL(&lock);
313 #endif
314 }
315
316 #endif /* SDL_DYNAMIC_API */
317
318 /* vi: set ts=4 sw=4 expandtab: */
319
320