Atlas - SDL_dynapi.c

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