SDL_dynapi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  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 "build_config/SDL_build_config.h"
  19. #include "SDL_dynapi.h"
  20. #if SDL_DYNAMIC_API
  21. #define SDL_DYNAMIC_API_ENVVAR "SDL3_DYNAMIC_API"
  22. #ifdef HAVE_STDIO_H
  23. #include <stdio.h>
  24. #endif
  25. #include <SDL3/SDL.h>
  26. #define SDL_MAIN_NOIMPL /* don't drag in header-only implementation of SDL_main */
  27. #include <SDL3/SDL_main.h>
  28. /* These headers have system specific definitions, so aren't included above */
  29. #include <SDL3/SDL_syswm.h>
  30. #include <SDL3/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 2
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. static void SDL_InitDynamicAPI(void);
  45. /* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
  46. Even self-contained stuff might call SDL_Error and break everything. */
  47. /* behold, the macro salsa! */
  48. /* !!! FIXME: ...disabled...until we write it. :) */
  49. #define DISABLE_JUMP_MAGIC 1
  50. #if DISABLE_JUMP_MAGIC
  51. /* Can't use the macro for varargs nonsense. This is atrocious. */
  52. #define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio) \
  53. _static void SDLCALL SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  54. { \
  55. va_list ap; \
  56. initcall; \
  57. va_start(ap, fmt); \
  58. jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
  59. va_end(ap); \
  60. }
  61. #define SDL_DYNAPI_VARARGS(_static, name, initcall) \
  62. _static int SDLCALL SDL_SetError##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  63. { \
  64. char buf[128], *str = buf; \
  65. int result; \
  66. va_list ap; \
  67. initcall; \
  68. va_start(ap, fmt); \
  69. result = jump_table.SDL_vsnprintf(buf, sizeof(buf), fmt, ap); \
  70. va_end(ap); \
  71. if (result >= 0 && (size_t)result >= sizeof(buf)) { \
  72. size_t len = (size_t)result + 1; \
  73. str = (char *)jump_table.SDL_malloc(len); \
  74. if (str) { \
  75. va_start(ap, fmt); \
  76. result = jump_table.SDL_vsnprintf(str, len, fmt, ap); \
  77. va_end(ap); \
  78. } \
  79. } \
  80. if (result >= 0) { \
  81. result = jump_table.SDL_SetError("%s", str); \
  82. } \
  83. if (str != buf) { \
  84. jump_table.SDL_free(str); \
  85. } \
  86. return result; \
  87. } \
  88. _static int SDLCALL SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) \
  89. { \
  90. int retval; \
  91. va_list ap; \
  92. initcall; \
  93. va_start(ap, fmt); \
  94. retval = jump_table.SDL_vsscanf(buf, fmt, ap); \
  95. va_end(ap); \
  96. return retval; \
  97. } \
  98. _static int SDLCALL SDL_snprintf##name(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  99. { \
  100. int retval; \
  101. va_list ap; \
  102. initcall; \
  103. va_start(ap, fmt); \
  104. retval = jump_table.SDL_vsnprintf(buf, maxlen, fmt, ap); \
  105. va_end(ap); \
  106. return retval; \
  107. } \
  108. _static int SDLCALL SDL_asprintf##name(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  109. { \
  110. int retval; \
  111. va_list ap; \
  112. initcall; \
  113. va_start(ap, fmt); \
  114. retval = jump_table.SDL_vasprintf(strp, fmt, ap); \
  115. va_end(ap); \
  116. return retval; \
  117. } \
  118. _static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  119. { \
  120. va_list ap; \
  121. initcall; \
  122. va_start(ap, fmt); \
  123. jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \
  124. va_end(ap); \
  125. } \
  126. _static void SDLCALL SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  127. { \
  128. va_list ap; \
  129. initcall; \
  130. va_start(ap, fmt); \
  131. jump_table.SDL_LogMessageV(category, priority, fmt, ap); \
  132. va_end(ap); \
  133. } \
  134. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Verbose, VERBOSE) \
  135. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Debug, DEBUG) \
  136. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Info, INFO) \
  137. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Warn, WARN) \
  138. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Error, ERROR) \
  139. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Critical, CRITICAL)
  140. #endif
  141. /* Typedefs for function pointers for jump table, and predeclare funcs */
  142. /* The DEFAULT funcs will init jump table and then call real function. */
  143. /* The REAL funcs are the actual functions, name-mangled to not clash. */
  144. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  145. typedef rc(SDLCALL *SDL_DYNAPIFN_##fn) params; \
  146. static rc SDLCALL fn##_DEFAULT params; \
  147. extern rc SDLCALL fn##_REAL params;
  148. #include "SDL_dynapi_procs.h"
  149. #undef SDL_DYNAPI_PROC
  150. /* The jump table! */
  151. typedef struct
  152. {
  153. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) SDL_DYNAPIFN_##fn fn;
  154. #include "SDL_dynapi_procs.h"
  155. #undef SDL_DYNAPI_PROC
  156. } SDL_DYNAPI_jump_table;
  157. /* Predeclare the default functions for initializing the jump table. */
  158. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) static rc SDLCALL fn##_DEFAULT params;
  159. #include "SDL_dynapi_procs.h"
  160. #undef SDL_DYNAPI_PROC
  161. /* The actual jump table. */
  162. static SDL_DYNAPI_jump_table jump_table = {
  163. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) fn##_DEFAULT,
  164. #include "SDL_dynapi_procs.h"
  165. #undef SDL_DYNAPI_PROC
  166. };
  167. /* Default functions init the function table then call right thing. */
  168. #if DISABLE_JUMP_MAGIC
  169. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  170. static rc SDLCALL fn##_DEFAULT params \
  171. { \
  172. SDL_InitDynamicAPI(); \
  173. ret jump_table.fn args; \
  174. }
  175. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  176. #include "SDL_dynapi_procs.h"
  177. #undef SDL_DYNAPI_PROC
  178. #undef SDL_DYNAPI_PROC_NO_VARARGS
  179. SDL_DYNAPI_VARARGS(static, _DEFAULT, SDL_InitDynamicAPI())
  180. #else
  181. /* !!! FIXME: need the jump magic. */
  182. #error Write me.
  183. #endif
  184. /* Public API functions to jump into the jump table. */
  185. #if DISABLE_JUMP_MAGIC
  186. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  187. rc SDLCALL fn params \
  188. { \
  189. ret jump_table.fn args; \
  190. }
  191. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  192. #include "SDL_dynapi_procs.h"
  193. #undef SDL_DYNAPI_PROC
  194. #undef SDL_DYNAPI_PROC_NO_VARARGS
  195. SDL_DYNAPI_VARARGS(, , )
  196. #else
  197. /* !!! FIXME: need the jump magic. */
  198. #error Write me.
  199. #endif
  200. #define ENABLE_SDL_CALL_LOGGING 0
  201. #if ENABLE_SDL_CALL_LOGGING
  202. static int SDLCALL SDL_SetError_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  203. {
  204. char buf[512]; /* !!! FIXME: dynamic allocation */
  205. va_list ap;
  206. SDL_Log_REAL("SDL3CALL SDL_SetError");
  207. va_start(ap, fmt);
  208. SDL_vsnprintf_REAL(buf, sizeof(buf), fmt, ap);
  209. va_end(ap);
  210. return SDL_SetError_REAL("%s", buf);
  211. }
  212. static int SDLCALL SDL_sscanf_LOGSDLCALLS(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
  213. {
  214. int retval;
  215. va_list ap;
  216. SDL_Log_REAL("SDL3CALL SDL_sscanf");
  217. va_start(ap, fmt);
  218. retval = SDL_vsscanf_REAL(buf, fmt, ap);
  219. va_end(ap);
  220. return retval;
  221. }
  222. static int SDLCALL SDL_snprintf_LOGSDLCALLS(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  223. {
  224. int retval;
  225. va_list ap;
  226. SDL_Log_REAL("SDL3CALL SDL_snprintf");
  227. va_start(ap, fmt);
  228. retval = SDL_vsnprintf_REAL(buf, maxlen, fmt, ap);
  229. va_end(ap);
  230. return retval;
  231. }
  232. static int SDLCALL SDL_asprintf_LOGSDLCALLS(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  233. {
  234. int retval;
  235. va_list ap;
  236. SDL_Log_REAL("SDL3CALL SDL_asprintf");
  237. va_start(ap, fmt);
  238. retval = SDL_vasprintf_REAL(strp, fmt, ap);
  239. va_end(ap);
  240. return retval;
  241. }
  242. static void SDLCALL SDL_Log_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  243. {
  244. va_list ap;
  245. SDL_Log_REAL("SDL3CALL SDL_Log");
  246. va_start(ap, fmt);
  247. SDL_LogMessageV_REAL(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
  248. va_end(ap);
  249. }
  250. static void SDLCALL SDL_LogMessage_LOGSDLCALLS(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  251. {
  252. va_list ap;
  253. SDL_Log_REAL("SDL3CALL SDL_LogMessage");
  254. va_start(ap, fmt);
  255. SDL_LogMessageV_REAL(category, priority, fmt, ap);
  256. va_end(ap);
  257. }
  258. #define SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(logname, prio) \
  259. static void SDLCALL SDL_Log##logname##_LOGSDLCALLS(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  260. { \
  261. va_list ap; \
  262. va_start(ap, fmt); \
  263. SDL_Log_REAL("SDL3CALL SDL_Log%s", #logname); \
  264. SDL_LogMessageV_REAL(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
  265. va_end(ap); \
  266. }
  267. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Verbose, VERBOSE)
  268. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Debug, DEBUG)
  269. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Info, INFO)
  270. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Warn, WARN)
  271. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Error, ERROR)
  272. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Critical, CRITICAL)
  273. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  274. rc SDLCALL fn##_LOGSDLCALLS params \
  275. { \
  276. SDL_Log_REAL("SDL3CALL %s", #fn); \
  277. ret fn##_REAL args; \
  278. }
  279. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  280. #include "SDL_dynapi_procs.h"
  281. #undef SDL_DYNAPI_PROC
  282. #undef SDL_DYNAPI_PROC_NO_VARARGS
  283. #endif
  284. /* we make this a static function so we can call the correct one without the
  285. system's dynamic linker resolving to the wrong version of this. */
  286. static Sint32 initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
  287. {
  288. SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *)table;
  289. if (apiver != SDL_DYNAPI_VERSION) {
  290. /* !!! FIXME: can maybe handle older versions? */
  291. return -1; /* not compatible. */
  292. } else if (tablesize > sizeof(jump_table)) {
  293. return -1; /* newer version of SDL with functions we can't provide. */
  294. }
  295. /* Init our jump table first. */
  296. #if ENABLE_SDL_CALL_LOGGING
  297. {
  298. const char *env = SDL_getenv_REAL("SDL_DYNAPI_LOG_CALLS");
  299. const SDL_bool log_calls = (env && SDL_atoi_REAL(env));
  300. if (log_calls) {
  301. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_LOGSDLCALLS;
  302. #include "SDL_dynapi_procs.h"
  303. #undef SDL_DYNAPI_PROC
  304. } else {
  305. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_REAL;
  306. #include "SDL_dynapi_procs.h"
  307. #undef SDL_DYNAPI_PROC
  308. }
  309. }
  310. #else
  311. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_REAL;
  312. #include "SDL_dynapi_procs.h"
  313. #undef SDL_DYNAPI_PROC
  314. #endif
  315. /* Then the external table... */
  316. if (output_jump_table != &jump_table) {
  317. jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
  318. }
  319. /* Safe to call SDL functions now; jump table is initialized! */
  320. return 0; /* success! */
  321. }
  322. /* Here's the exported entry point that fills in the jump table. */
  323. /* Use specific types when an "int" might suffice to keep this sane. */
  324. typedef Sint32(SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
  325. extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
  326. Sint32
  327. SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
  328. {
  329. return initialize_jumptable(apiver, table, tablesize);
  330. }
  331. #ifdef __cplusplus
  332. }
  333. #endif
  334. /* Obviously we can't use SDL_LoadObject() to load SDL. :) */
  335. /* Also obviously, we never close the loaded library. */
  336. #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
  337. #ifndef WIN32_LEAN_AND_MEAN
  338. #define WIN32_LEAN_AND_MEAN 1
  339. #endif
  340. #include <windows.h>
  341. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  342. {
  343. HMODULE lib = LoadLibraryA(fname);
  344. void *retval = NULL;
  345. if (lib) {
  346. retval = GetProcAddress(lib, sym);
  347. if (retval == NULL) {
  348. FreeLibrary(lib);
  349. }
  350. }
  351. return retval;
  352. }
  353. #elif defined(unix) || defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__)
  354. #include <dlfcn.h>
  355. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  356. {
  357. void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
  358. void *retval = NULL;
  359. if (lib != NULL) {
  360. retval = dlsym(lib, sym);
  361. if (retval == NULL) {
  362. dlclose(lib);
  363. }
  364. }
  365. return retval;
  366. }
  367. #else
  368. #error Please define your platform.
  369. #endif
  370. static void dynapi_warn(const char *msg)
  371. {
  372. const char *caption = "SDL Dynamic API Failure!";
  373. /* SDL_ShowSimpleMessageBox() is a too heavy for here. */
  374. #if (defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  375. MessageBoxA(NULL, msg, caption, MB_OK | MB_ICONERROR);
  376. #elif defined(HAVE_STDIO_H)
  377. fprintf(stderr, "\n\n%s\n%s\n\n", caption, msg);
  378. fflush(stderr);
  379. #endif
  380. }
  381. /* This is not declared in any header, although it is shared between some
  382. parts of SDL, because we don't want anything calling it without an
  383. extremely good reason. */
  384. #ifdef __cplusplus
  385. extern "C" {
  386. #endif
  387. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  388. #if defined(__WATCOMC__)
  389. #pragma aux SDL_ExitProcess aborts;
  390. #endif
  391. #ifdef __cplusplus
  392. }
  393. #endif
  394. static void SDL_InitDynamicAPILocked(void)
  395. {
  396. const char *libname = SDL_getenv_REAL(SDL_DYNAMIC_API_ENVVAR);
  397. SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
  398. SDL_bool use_internal = SDL_TRUE;
  399. if (libname) {
  400. entry = (SDL_DYNAPI_ENTRYFN)get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
  401. if (!entry) {
  402. dynapi_warn("Couldn't load overriding SDL library. Please fix or remove the " SDL_DYNAMIC_API_ENVVAR " environment variable. Using the default SDL.");
  403. /* Just fill in the function pointers from this library, later. */
  404. }
  405. }
  406. if (entry) {
  407. if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof(jump_table)) < 0) {
  408. dynapi_warn("Couldn't override SDL library. Using a newer SDL build might help. Please fix or remove the " SDL_DYNAMIC_API_ENVVAR " environment variable. Using the default SDL.");
  409. /* Just fill in the function pointers from this library, later. */
  410. } else {
  411. use_internal = SDL_FALSE; /* We overrode SDL! Don't use the internal version! */
  412. }
  413. }
  414. /* Just fill in the function pointers from this library. */
  415. if (use_internal) {
  416. if (initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof(jump_table)) < 0) {
  417. /* Now we're screwed. Should definitely abort now. */
  418. dynapi_warn("Failed to initialize internal SDL dynapi. As this would otherwise crash, we have to abort now.");
  419. SDL_ExitProcess(86);
  420. }
  421. }
  422. /* we intentionally never close the newly-loaded lib, of course. */
  423. }
  424. static void SDL_InitDynamicAPI(void)
  425. {
  426. /* So the theory is that every function in the jump table defaults to
  427. * calling this function, and then replaces itself with a version that
  428. * doesn't call this function anymore. But it's possible that, in an
  429. * extreme corner case, you can have a second thread hit this function
  430. * while the jump table is being initialized by the first.
  431. * In this case, a spinlock is really painful compared to what spinlocks
  432. * _should_ be used for, but this would only happen once, and should be
  433. * insanely rare, as you would have to spin a thread outside of SDL (as
  434. * SDL_CreateThread() would also call this function before building the
  435. * new thread).
  436. */
  437. static SDL_bool already_initialized = SDL_FALSE;
  438. /* SDL_AtomicLock calls SDL mutex functions to emulate if
  439. SDL_ATOMIC_DISABLED, which we can't do here, so in such a
  440. configuration, you're on your own. */
  441. #if !SDL_ATOMIC_DISABLED
  442. static SDL_SpinLock lock = 0;
  443. SDL_AtomicLock_REAL(&lock);
  444. #endif
  445. if (!already_initialized) {
  446. SDL_InitDynamicAPILocked();
  447. already_initialized = SDL_TRUE;
  448. }
  449. #if !SDL_ATOMIC_DISABLED
  450. SDL_AtomicUnlock_REAL(&lock);
  451. #endif
  452. }
  453. #endif /* SDL_DYNAMIC_API */