2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 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 #define INCL_DOSERRORS
36 /* These headers have system specific definitions, so aren't included above */
37 #include "SDL_syswm.h"
38 #include "SDL_vulkan.h"
40 /* This is the version of the dynamic API. This doesn't match the SDL version
41 and should not change until there's been a major revamp in API/ABI.
42 So 2.0.5 adds functions over 2.0.4? This number doesn't change;
43 the sizeof (jump_table) changes instead. But 2.1.0 changes how a function
44 works in an incompatible way or removes a function? This number changes,
45 since sizeof (jump_table) isn't sufficient anymore. It's likely
46 we'll forget to bump every time we add a function, so this is the
47 failsafe switch for major API change decisions. Respect it and use it
49 #define SDL_DYNAPI_VERSION 1
51 static void SDL_InitDynamicAPI(void);
54 /* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
55 Even self-contained stuff might call SDL_Error and break everything. */
58 /* behold, the macro salsa! */
60 /* !!! FIXME: ...disabled...until we write it. :) */
61 #define DISABLE_JUMP_MAGIC 1
63 #if DISABLE_JUMP_MAGIC
64 /* Can't use the macro for varargs nonsense. This is atrocious. */
65 #define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio) \
66 _static void SDLCALL SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
67 va_list ap; initcall; va_start(ap, fmt); \
68 jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
72 #define SDL_DYNAPI_VARARGS(_static, name, initcall) \
73 _static int SDLCALL SDL_SetError##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
74 char buf[512]; /* !!! FIXME: dynamic allocation */ \
75 va_list ap; initcall; va_start(ap, fmt); \
76 jump_table.SDL_vsnprintf(buf, sizeof (buf), fmt, ap); \
78 return jump_table.SDL_SetError("%s", buf); \
80 _static int SDLCALL SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) { \
81 int retval; va_list ap; initcall; va_start(ap, fmt); \
82 retval = jump_table.SDL_vsscanf(buf, fmt, ap); \
86 _static int SDLCALL SDL_snprintf##name(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
87 int retval; va_list ap; initcall; va_start(ap, fmt); \
88 retval = jump_table.SDL_vsnprintf(buf, maxlen, fmt, ap); \
92 _static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
93 va_list ap; initcall; va_start(ap, fmt); \
94 jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \
97 _static void SDLCALL SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
98 va_list ap; initcall; va_start(ap, fmt); \
99 jump_table.SDL_LogMessageV(category, priority, fmt, ap); \
102 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Verbose, VERBOSE) \
103 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Debug, DEBUG) \
104 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Info, INFO) \
105 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Warn, WARN) \
106 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Error, ERROR) \
107 SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Critical, CRITICAL)
111 /* Typedefs for function pointers for jump table, and predeclare funcs */
112 /* The DEFAULT funcs will init jump table and then call real function. */
113 /* The REAL funcs are the actual functions, name-mangled to not clash. */
114 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
115 typedef rc (SDLCALL *SDL_DYNAPIFN_##fn) params; \
116 static rc SDLCALL fn##_DEFAULT params; \
117 extern rc SDLCALL fn##_REAL params;
118 #include "SDL_dynapi_procs.h"
119 #undef SDL_DYNAPI_PROC
121 /* The jump table! */
123 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) SDL_DYNAPIFN_##fn fn;
124 #include "SDL_dynapi_procs.h"
125 #undef SDL_DYNAPI_PROC
126 } SDL_DYNAPI_jump_table;
128 /* Predeclare the default functions for initializing the jump table. */
129 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) static rc SDLCALL fn##_DEFAULT params;
130 #include "SDL_dynapi_procs.h"
131 #undef SDL_DYNAPI_PROC
133 /* The actual jump table. */
134 static SDL_DYNAPI_jump_table jump_table = {
135 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) fn##_DEFAULT,
136 #include "SDL_dynapi_procs.h"
137 #undef SDL_DYNAPI_PROC
140 /* Default functions init the function table then call right thing. */
141 #if DISABLE_JUMP_MAGIC
142 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
143 static rc SDLCALL fn##_DEFAULT params { \
144 SDL_InitDynamicAPI(); \
145 ret jump_table.fn args; \
147 #define SDL_DYNAPI_PROC_NO_VARARGS 1
148 #include "SDL_dynapi_procs.h"
149 #undef SDL_DYNAPI_PROC
150 #undef SDL_DYNAPI_PROC_NO_VARARGS
151 SDL_DYNAPI_VARARGS(static, _DEFAULT, SDL_InitDynamicAPI())
153 /* !!! FIXME: need the jump magic. */
157 /* Public API functions to jump into the jump table. */
158 #if DISABLE_JUMP_MAGIC
159 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
160 rc SDLCALL fn params { ret jump_table.fn args; }
161 #define SDL_DYNAPI_PROC_NO_VARARGS 1
162 #include "SDL_dynapi_procs.h"
163 #undef SDL_DYNAPI_PROC
164 #undef SDL_DYNAPI_PROC_NO_VARARGS
165 SDL_DYNAPI_VARARGS(,,)
167 /* !!! FIXME: need the jump magic. */
171 /* we make this a static function so we can call the correct one without the
172 system's dynamic linker resolving to the wrong version of this. */
174 initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
176 SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
178 if (apiver != SDL_DYNAPI_VERSION) {
179 /* !!! FIXME: can maybe handle older versions? */
180 return -1; /* not compatible. */
181 } else if (tablesize > sizeof (jump_table)) {
182 return -1; /* newer version of SDL with functions we can't provide. */
185 /* Init our jump table first. */
186 #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) jump_table.fn = fn##_REAL;
187 #include "SDL_dynapi_procs.h"
188 #undef SDL_DYNAPI_PROC
190 /* Then the external table... */
191 if (output_jump_table != &jump_table) {
192 jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
195 /* Safe to call SDL functions now; jump table is initialized! */
197 return 0; /* success! */
201 /* Here's the exported entry point that fills in the jump table. */
202 /* Use specific types when an "int" might suffice to keep this sane. */
203 typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
204 extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
207 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
209 return initialize_jumptable(apiver, table, tablesize);
213 /* Obviously we can't use SDL_LoadObject() to load SDL. :) */
214 /* Also obviously, we never close the loaded library. */
215 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
216 #ifndef WIN32_LEAN_AND_MEAN
217 #define WIN32_LEAN_AND_MEAN 1
220 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
222 HANDLE lib = LoadLibraryA(fname);
225 retval = GetProcAddress(lib, sym);
226 if (retval == NULL) {
233 #elif defined(unix) || defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__) || defined(__QNX__)
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 #elif defined(__OS2__)
249 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
254 if (DosLoadModule(error, sizeof(error), fname, &hmodule) == NO_ERROR) {
255 if (DosQueryProcAddr(hmodule, 0, sym, &retval) != NO_ERROR) {
256 DosFreeModule(hmodule);
259 return (void *)retval;
263 #error Please define your platform.
267 static void dynapi_warn(const char *msg)
269 const char *caption = "SDL Dynamic API Failure!";
270 /* SDL_ShowSimpleMessageBox() is a too heavy for here. */
271 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
272 MessageBoxA(NULL, msg, caption, MB_OK | MB_ICONERROR);
274 fprintf(stderr, "\n\n%s\n%s\n\n", caption, msg);
279 /* This is not declared in any header, although it is shared between some
280 parts of SDL, because we don't want anything calling it without an
281 extremely good reason. */
282 #if defined(__WATCOMC__)
283 void SDL_ExitProcess(int exitcode);
284 #pragma aux SDL_ExitProcess aborts;
286 SDL_NORETURN void SDL_ExitProcess(int exitcode);
290 SDL_InitDynamicAPILocked(void)
292 const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
293 SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
294 SDL_bool use_internal = SDL_TRUE;
297 entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
299 dynapi_warn("Couldn't load overriding SDL library. Please fix or remove the SDL_DYNAMIC_API environment variable. Using the default SDL.");
300 /* Just fill in the function pointers from this library, later. */
305 if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
306 dynapi_warn("Couldn't override SDL library. Using a newer SDL build might help. Please fix or remove the SDL_DYNAMIC_API environment variable. Using the default SDL.");
307 /* Just fill in the function pointers from this library, later. */
309 use_internal = SDL_FALSE; /* We overrode SDL! Don't use the internal version! */
313 /* Just fill in the function pointers from this library. */
315 if (initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
316 /* Now we're screwed. Should definitely abort now. */
317 dynapi_warn("Failed to initialize internal SDL dynapi. As this would otherwise crash, we have to abort now.");
322 /* we intentionally never close the newly-loaded lib, of course. */
326 SDL_InitDynamicAPI(void)
328 /* So the theory is that every function in the jump table defaults to
329 * calling this function, and then replaces itself with a version that
330 * doesn't call this function anymore. But it's possible that, in an
331 * extreme corner case, you can have a second thread hit this function
332 * while the jump table is being initialized by the first.
333 * In this case, a spinlock is really painful compared to what spinlocks
334 * _should_ be used for, but this would only happen once, and should be
335 * insanely rare, as you would have to spin a thread outside of SDL (as
336 * SDL_CreateThread() would also call this function before building the
339 static SDL_bool already_initialized = SDL_FALSE;
341 /* SDL_AtomicLock calls SDL mutex functions to emulate if
342 SDL_ATOMIC_DISABLED, which we can't do here, so in such a
343 configuration, you're on your own. */
344 #if !SDL_ATOMIC_DISABLED
345 static SDL_SpinLock lock = 0;
346 SDL_AtomicLock_REAL(&lock);
349 if (!already_initialized) {
350 SDL_InitDynamicAPILocked();
351 already_initialized = SDL_TRUE;
354 #if !SDL_ATOMIC_DISABLED
355 SDL_AtomicUnlock_REAL(&lock);
359 #endif /* SDL_DYNAMIC_API */
361 /* vi: set ts=4 sw=4 expandtab: */