SDL_dynapi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_config.h"
  19. #include "SDL_dynapi.h"
  20. #if SDL_DYNAMIC_API
  21. #if defined(__OS2__)
  22. #define INCL_DOS
  23. #define INCL_DOSERRORS
  24. #include <os2.h>
  25. #include <dos.h>
  26. #endif
  27. #include "SDL.h"
  28. /* These headers have system specific definitions, so aren't included above */
  29. #include "SDL_syswm.h"
  30. #include "SDL_vulkan.h"
  31. /* This is the version of the dynamic API. This doesn't match the SDL version
  32. and should not change until there's been a major revamp in API/ABI.
  33. So 2.0.5 adds functions over 2.0.4? This number doesn't change;
  34. the sizeof (jump_table) changes instead. But 2.1.0 changes how a function
  35. works in an incompatible way or removes a function? This number changes,
  36. since sizeof (jump_table) isn't sufficient anymore. It's likely
  37. we'll forget to bump every time we add a function, so this is the
  38. failsafe switch for major API change decisions. Respect it and use it
  39. sparingly. */
  40. #define SDL_DYNAPI_VERSION 1
  41. static void SDL_InitDynamicAPI(void);
  42. /* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
  43. Even self-contained stuff might call SDL_Error and break everything. */
  44. /* behold, the macro salsa! */
  45. /* !!! FIXME: ...disabled...until we write it. :) */
  46. #define DISABLE_JUMP_MAGIC 1
  47. #if DISABLE_JUMP_MAGIC
  48. /* Can't use the macro for varargs nonsense. This is atrocious. */
  49. #define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio) \
  50. _static void SDLCALL SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
  51. va_list ap; initcall; va_start(ap, fmt); \
  52. jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
  53. va_end(ap); \
  54. }
  55. #define SDL_DYNAPI_VARARGS(_static, name, initcall) \
  56. _static int SDLCALL SDL_SetError##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
  57. char buf[512]; /* !!! FIXME: dynamic allocation */ \
  58. va_list ap; initcall; va_start(ap, fmt); \
  59. jump_table.SDL_vsnprintf(buf, sizeof (buf), fmt, ap); \
  60. va_end(ap); \
  61. return jump_table.SDL_SetError("%s", buf); \
  62. } \
  63. _static int SDLCALL SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) { \
  64. int retval; va_list ap; initcall; va_start(ap, fmt); \
  65. retval = jump_table.SDL_vsscanf(buf, fmt, ap); \
  66. va_end(ap); \
  67. return retval; \
  68. } \
  69. _static int SDLCALL SDL_snprintf##name(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
  70. int retval; va_list ap; initcall; va_start(ap, fmt); \
  71. retval = jump_table.SDL_vsnprintf(buf, maxlen, fmt, ap); \
  72. va_end(ap); \
  73. return retval; \
  74. } \
  75. _static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
  76. va_list ap; initcall; va_start(ap, fmt); \
  77. jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \
  78. va_end(ap); \
  79. } \
  80. _static void SDLCALL SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
  81. va_list ap; initcall; va_start(ap, fmt); \
  82. jump_table.SDL_LogMessageV(category, priority, fmt, ap); \
  83. va_end(ap); \
  84. } \
  85. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Verbose, VERBOSE) \
  86. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Debug, DEBUG) \
  87. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Info, INFO) \
  88. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Warn, WARN) \
  89. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Error, ERROR) \
  90. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Critical, CRITICAL)
  91. #endif
  92. /* Typedefs for function pointers for jump table, and predeclare funcs */
  93. /* The DEFAULT funcs will init jump table and then call real function. */
  94. /* The REAL funcs are the actual functions, name-mangled to not clash. */
  95. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
  96. typedef rc (SDLCALL *SDL_DYNAPIFN_##fn) params; \
  97. static rc SDLCALL fn##_DEFAULT params; \
  98. extern rc SDLCALL fn##_REAL params;
  99. #include "SDL_dynapi_procs.h"
  100. #undef SDL_DYNAPI_PROC
  101. /* The jump table! */
  102. typedef struct {
  103. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) SDL_DYNAPIFN_##fn fn;
  104. #include "SDL_dynapi_procs.h"
  105. #undef SDL_DYNAPI_PROC
  106. } SDL_DYNAPI_jump_table;
  107. /* Predeclare the default functions for initializing the jump table. */
  108. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) static rc SDLCALL fn##_DEFAULT params;
  109. #include "SDL_dynapi_procs.h"
  110. #undef SDL_DYNAPI_PROC
  111. /* The actual jump table. */
  112. static SDL_DYNAPI_jump_table jump_table = {
  113. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) fn##_DEFAULT,
  114. #include "SDL_dynapi_procs.h"
  115. #undef SDL_DYNAPI_PROC
  116. };
  117. /* Default functions init the function table then call right thing. */
  118. #if DISABLE_JUMP_MAGIC
  119. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
  120. static rc SDLCALL fn##_DEFAULT params { \
  121. SDL_InitDynamicAPI(); \
  122. ret jump_table.fn args; \
  123. }
  124. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  125. #include "SDL_dynapi_procs.h"
  126. #undef SDL_DYNAPI_PROC
  127. #undef SDL_DYNAPI_PROC_NO_VARARGS
  128. SDL_DYNAPI_VARARGS(static, _DEFAULT, SDL_InitDynamicAPI())
  129. #else
  130. /* !!! FIXME: need the jump magic. */
  131. #error Write me.
  132. #endif
  133. /* Public API functions to jump into the jump table. */
  134. #if DISABLE_JUMP_MAGIC
  135. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
  136. rc SDLCALL fn params { ret jump_table.fn args; }
  137. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  138. #include "SDL_dynapi_procs.h"
  139. #undef SDL_DYNAPI_PROC
  140. #undef SDL_DYNAPI_PROC_NO_VARARGS
  141. SDL_DYNAPI_VARARGS(,,)
  142. #else
  143. /* !!! FIXME: need the jump magic. */
  144. #error Write me.
  145. #endif
  146. /* we make this a static function so we can call the correct one without the
  147. system's dynamic linker resolving to the wrong version of this. */
  148. static Sint32
  149. initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
  150. {
  151. SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
  152. if (apiver != SDL_DYNAPI_VERSION) {
  153. /* !!! FIXME: can maybe handle older versions? */
  154. return -1; /* not compatible. */
  155. } else if (tablesize > sizeof (jump_table)) {
  156. return -1; /* newer version of SDL with functions we can't provide. */
  157. }
  158. /* Init our jump table first. */
  159. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) jump_table.fn = fn##_REAL;
  160. #include "SDL_dynapi_procs.h"
  161. #undef SDL_DYNAPI_PROC
  162. /* Then the external table... */
  163. if (output_jump_table != &jump_table) {
  164. jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
  165. }
  166. /* Safe to call SDL functions now; jump table is initialized! */
  167. return 0; /* success! */
  168. }
  169. /* Here's the exported entry point that fills in the jump table. */
  170. /* Use specific types when an "int" might suffice to keep this sane. */
  171. typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
  172. extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
  173. Sint32
  174. SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
  175. {
  176. return initialize_jumptable(apiver, table, tablesize);
  177. }
  178. /* Obviously we can't use SDL_LoadObject() to load SDL. :) */
  179. /* Also obviously, we never close the loaded library. */
  180. #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
  181. #ifndef WIN32_LEAN_AND_MEAN
  182. #define WIN32_LEAN_AND_MEAN 1
  183. #endif
  184. #include <windows.h>
  185. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  186. {
  187. HANDLE lib = LoadLibraryA(fname);
  188. void *retval = NULL;
  189. if (lib) {
  190. retval = GetProcAddress(lib, sym);
  191. if (retval == NULL) {
  192. FreeLibrary(lib);
  193. }
  194. }
  195. return retval;
  196. }
  197. #elif defined(unix) || defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__) || defined(__QNX__)
  198. #include <dlfcn.h>
  199. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  200. {
  201. void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
  202. void *retval = NULL;
  203. if (lib != NULL) {
  204. retval = dlsym(lib, sym);
  205. if (retval == NULL) {
  206. dlclose(lib);
  207. }
  208. }
  209. return retval;
  210. }
  211. #elif defined(__OS2__)
  212. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  213. {
  214. HMODULE hmodule;
  215. PFN retval = NULL;
  216. char error[256];
  217. if (DosLoadModule(error, sizeof(error), fname, &hmodule) == NO_ERROR) {
  218. if (DosQueryProcAddr(hmodule, 0, sym, &retval) != NO_ERROR) {
  219. DosFreeModule(hmodule);
  220. }
  221. }
  222. return (void *)retval;
  223. }
  224. #else
  225. #error Please define your platform.
  226. #endif
  227. static void dynapi_warn(const char *msg)
  228. {
  229. const char *caption = "SDL Dynamic API Failure!";
  230. /* SDL_ShowSimpleMessageBox() is a too heavy for here. */
  231. #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
  232. MessageBoxA(NULL, msg, caption, MB_OK | MB_ICONERROR);
  233. #elif defined(HAVE_STDIO_H)
  234. fprintf(stderr, "\n\n%s\n%s\n\n", caption, msg);
  235. fflush(stderr);
  236. #endif
  237. }
  238. /* This is not declared in any header, although it is shared between some
  239. parts of SDL, because we don't want anything calling it without an
  240. extremely good reason. */
  241. #if defined(__WATCOMC__)
  242. void SDL_ExitProcess(int exitcode);
  243. #pragma aux SDL_ExitProcess aborts;
  244. #endif
  245. SDL_NORETURN void SDL_ExitProcess(int exitcode);
  246. static void
  247. SDL_InitDynamicAPILocked(void)
  248. {
  249. const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
  250. SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
  251. SDL_bool use_internal = SDL_TRUE;
  252. if (libname) {
  253. entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
  254. if (!entry) {
  255. dynapi_warn("Couldn't load overriding SDL library. Please fix or remove the SDL_DYNAMIC_API environment variable. Using the default SDL.");
  256. /* Just fill in the function pointers from this library, later. */
  257. }
  258. }
  259. if (entry) {
  260. if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
  261. 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.");
  262. /* Just fill in the function pointers from this library, later. */
  263. } else {
  264. use_internal = SDL_FALSE; /* We overrode SDL! Don't use the internal version! */
  265. }
  266. }
  267. /* Just fill in the function pointers from this library. */
  268. if (use_internal) {
  269. if (initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
  270. /* Now we're screwed. Should definitely abort now. */
  271. dynapi_warn("Failed to initialize internal SDL dynapi. As this would otherwise crash, we have to abort now.");
  272. SDL_ExitProcess(86);
  273. }
  274. }
  275. /* we intentionally never close the newly-loaded lib, of course. */
  276. }
  277. static void
  278. SDL_InitDynamicAPI(void)
  279. {
  280. /* So the theory is that every function in the jump table defaults to
  281. * calling this function, and then replaces itself with a version that
  282. * doesn't call this function anymore. But it's possible that, in an
  283. * extreme corner case, you can have a second thread hit this function
  284. * while the jump table is being initialized by the first.
  285. * In this case, a spinlock is really painful compared to what spinlocks
  286. * _should_ be used for, but this would only happen once, and should be
  287. * insanely rare, as you would have to spin a thread outside of SDL (as
  288. * SDL_CreateThread() would also call this function before building the
  289. * new thread).
  290. */
  291. static SDL_bool already_initialized = SDL_FALSE;
  292. /* SDL_AtomicLock calls SDL mutex functions to emulate if
  293. SDL_ATOMIC_DISABLED, which we can't do here, so in such a
  294. configuration, you're on your own. */
  295. #if !SDL_ATOMIC_DISABLED
  296. static SDL_SpinLock lock = 0;
  297. SDL_AtomicLock_REAL(&lock);
  298. #endif
  299. if (!already_initialized) {
  300. SDL_InitDynamicAPILocked();
  301. already_initialized = SDL_TRUE;
  302. }
  303. #if !SDL_ATOMIC_DISABLED
  304. SDL_AtomicUnlock_REAL(&lock);
  305. #endif
  306. }
  307. #endif /* SDL_DYNAMIC_API */
  308. /* vi: set ts=4 sw=4 expandtab: */