2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
22 #include "SDL_config.h"
23 #include "SDL_dynapi.h"
29 /* !!! FIXME: Shouldn't these be included in SDL.h? */
30 #include "SDL_shape.h"
31 #include "SDL_syswm.h"
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
42 #define SDL_DYNAPI_VERSION 1
44 static void SDL_InitDynamicAPI(void);
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. */
51 /* behold, the macro salsa! */
53 /* !!! FIXME: ...disabled...until we write it. :) */
54 #define DISABLE_JUMP_MAGIC 1
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); \
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); \
71 return jump_table.SDL_SetError("%s", buf); \
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); \
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); \
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); \
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); \
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)
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
114 /* The jump table! */
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;
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
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
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; \
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())
146 /* !!! FIXME: need the jump magic. */
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(,,)
160 /* !!! FIXME: need the jump magic. */
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);
172 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
174 SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
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. */
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
188 /* Then the external table... */
189 if (output_jump_table != &jump_table) {
190 jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
193 /* Safe to call SDL functions now; jump table is initialized! */
195 return 0; /* success! */
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
206 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
208 HANDLE lib = LoadLibraryA(fname);
211 retval = GetProcAddress(lib, sym);
212 if (retval == NULL) {
219 #elif defined(__HAIKU__)
220 #include <os/kernel/image.h>
221 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
223 image_id lib = load_add_on(fname);
226 if (get_image_symbol(lib, sym, B_SYMBOL_TYPE_TEXT, &retval) != B_NO_ERROR) {
233 #elif defined(unix) || defined(__unix__) || defined(__APPLE__)
235 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
237 void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
240 retval = dlsym(lib, sym);
241 if (retval == NULL) {
248 #error Please define your platform.
253 SDL_InitDynamicAPILocked(void)
255 const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
256 SDL_DYNAPI_ENTRYFN entry = SDL_DYNAPI_entry; /* funcs from here by default. */
259 entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_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;
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. */
279 /* we intentionally never close the newly-loaded lib, of course. */
283 SDL_InitDynamicAPI(void)
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
296 static volatile SDL_bool already_initialized = SDL_FALSE;
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);
306 if (!already_initialized) {
307 SDL_InitDynamicAPILocked();
308 already_initialized = SDL_TRUE;
311 #if !SDL_ATOMIC_DISABLED
312 SDL_AtomicUnlock_REAL(&lock);
316 #endif /* SDL_DYNAMIC_API */
318 /* vi: set ts=4 sw=4 expandtab: */