SDL_dynapi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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_build_config.h"
  19. #include "SDL_dynapi.h"
  20. #include "SDL_dynapi_unsupported.h"
  21. #if SDL_DYNAMIC_API
  22. #define SDL_DYNAMIC_API_ENVVAR "SDL3_DYNAMIC_API"
  23. #define SDL_SLOW_MEMCPY
  24. #define SDL_SLOW_MEMMOVE
  25. #define SDL_SLOW_MEMSET
  26. #ifdef HAVE_STDIO_H
  27. #include <stdio.h>
  28. #endif
  29. #ifdef HAVE_STDLIB_H
  30. #include <stdlib.h>
  31. #endif
  32. #include <SDL3/SDL.h>
  33. #define SDL_MAIN_NOIMPL // don't drag in header-only implementation of SDL_main
  34. #include <SDL3/SDL_main.h>
  35. // These headers have system specific definitions, so aren't included above
  36. #include <SDL3/SDL_vulkan.h>
  37. #if defined(WIN32) || defined(_WIN32) || defined(SDL_PLATFORM_CYGWIN)
  38. #ifndef WIN32_LEAN_AND_MEAN
  39. #define WIN32_LEAN_AND_MEAN 1
  40. #endif
  41. #include <windows.h>
  42. #endif
  43. /* This is the version of the dynamic API. This doesn't match the SDL version
  44. and should not change until there's been a major revamp in API/ABI.
  45. So 2.0.5 adds functions over 2.0.4? This number doesn't change;
  46. the sizeof(jump_table) changes instead. But 2.1.0 changes how a function
  47. works in an incompatible way or removes a function? This number changes,
  48. since sizeof(jump_table) isn't sufficient anymore. It's likely
  49. we'll forget to bump every time we add a function, so this is the
  50. failsafe switch for major API change decisions. Respect it and use it
  51. sparingly. */
  52. #define SDL_DYNAPI_VERSION 2
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. static void SDL_InitDynamicAPI(void);
  57. /* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
  58. Even self-contained stuff might call SDL_SetError() and break everything. */
  59. // behold, the macro salsa!
  60. // Can't use the macro for varargs nonsense. This is atrocious.
  61. #define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio) \
  62. _static void SDLCALL SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  63. { \
  64. va_list ap; \
  65. initcall; \
  66. va_start(ap, fmt); \
  67. jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
  68. va_end(ap); \
  69. }
  70. #define SDL_DYNAPI_VARARGS(_static, name, initcall) \
  71. _static bool SDLCALL SDL_SetError##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  72. { \
  73. char buf[128], *str = buf; \
  74. int result; \
  75. va_list ap; \
  76. initcall; \
  77. va_start(ap, fmt); \
  78. result = jump_table.SDL_vsnprintf(buf, sizeof(buf), fmt, ap); \
  79. va_end(ap); \
  80. if (result >= 0 && (size_t)result >= sizeof(buf)) { \
  81. str = NULL; \
  82. va_start(ap, fmt); \
  83. result = jump_table.SDL_vasprintf(&str, fmt, ap); \
  84. va_end(ap); \
  85. } \
  86. if (result >= 0) { \
  87. jump_table.SDL_SetError("%s", str); \
  88. } \
  89. if (str != buf) { \
  90. jump_table.SDL_free(str); \
  91. } \
  92. return false; \
  93. } \
  94. _static int SDLCALL SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) \
  95. { \
  96. int result; \
  97. va_list ap; \
  98. initcall; \
  99. va_start(ap, fmt); \
  100. result = jump_table.SDL_vsscanf(buf, fmt, ap); \
  101. va_end(ap); \
  102. return result; \
  103. } \
  104. _static int SDLCALL SDL_snprintf##name(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  105. { \
  106. int result; \
  107. va_list ap; \
  108. initcall; \
  109. va_start(ap, fmt); \
  110. result = jump_table.SDL_vsnprintf(buf, maxlen, fmt, ap); \
  111. va_end(ap); \
  112. return result; \
  113. } \
  114. _static int SDLCALL SDL_swprintf##name(SDL_OUT_Z_CAP(maxlen) wchar_t *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...) \
  115. { \
  116. int result; \
  117. va_list ap; \
  118. initcall; \
  119. va_start(ap, fmt); \
  120. result = jump_table.SDL_vswprintf(buf, maxlen, fmt, ap); \
  121. va_end(ap); \
  122. return result; \
  123. } \
  124. _static int SDLCALL SDL_asprintf##name(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  125. { \
  126. int result; \
  127. va_list ap; \
  128. initcall; \
  129. va_start(ap, fmt); \
  130. result = jump_table.SDL_vasprintf(strp, fmt, ap); \
  131. va_end(ap); \
  132. return result; \
  133. } \
  134. _static size_t SDLCALL SDL_IOprintf##name(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  135. { \
  136. size_t result; \
  137. va_list ap; \
  138. initcall; \
  139. va_start(ap, fmt); \
  140. result = jump_table.SDL_IOvprintf(context, fmt, ap); \
  141. va_end(ap); \
  142. return result; \
  143. } \
  144. _static bool SDLCALL SDL_RenderDebugTextFormat##name(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  145. { \
  146. char buf[128], *str = buf; \
  147. int result; \
  148. va_list ap; \
  149. initcall; \
  150. va_start(ap, fmt); \
  151. result = jump_table.SDL_vsnprintf(buf, sizeof(buf), fmt, ap); \
  152. va_end(ap); \
  153. if (result >= 0 && (size_t)result >= sizeof(buf)) { \
  154. str = NULL; \
  155. va_start(ap, fmt); \
  156. result = jump_table.SDL_vasprintf(&str, fmt, ap); \
  157. va_end(ap); \
  158. } \
  159. bool retval = false; \
  160. if (result >= 0) { \
  161. retval = jump_table.SDL_RenderDebugTextFormat(renderer, x, y, "%s", str); \
  162. } \
  163. if (str != buf) { \
  164. jump_table.SDL_free(str); \
  165. } \
  166. return retval; \
  167. } \
  168. _static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  169. { \
  170. va_list ap; \
  171. initcall; \
  172. va_start(ap, fmt); \
  173. jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \
  174. va_end(ap); \
  175. } \
  176. _static void SDLCALL SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  177. { \
  178. va_list ap; \
  179. initcall; \
  180. va_start(ap, fmt); \
  181. jump_table.SDL_LogMessageV(category, priority, fmt, ap); \
  182. va_end(ap); \
  183. } \
  184. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Trace, TRACE) \
  185. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Verbose, VERBOSE) \
  186. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Debug, DEBUG) \
  187. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Info, INFO) \
  188. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Warn, WARN) \
  189. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Error, ERROR) \
  190. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Critical, CRITICAL)
  191. // Typedefs for function pointers for jump table, and predeclare funcs
  192. // The DEFAULT funcs will init jump table and then call real function.
  193. // The REAL funcs are the actual functions, name-mangled to not clash.
  194. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  195. typedef rc (SDLCALL *SDL_DYNAPIFN_##fn) params;\
  196. static rc SDLCALL fn##_DEFAULT params; \
  197. extern rc SDLCALL fn##_REAL params;
  198. #include "SDL_dynapi_procs.h"
  199. #undef SDL_DYNAPI_PROC
  200. // The jump table!
  201. typedef struct
  202. {
  203. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) SDL_DYNAPIFN_##fn fn;
  204. #include "SDL_dynapi_procs.h"
  205. #undef SDL_DYNAPI_PROC
  206. } SDL_DYNAPI_jump_table;
  207. // The actual jump table.
  208. static SDL_DYNAPI_jump_table jump_table = {
  209. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) fn##_DEFAULT,
  210. #include "SDL_dynapi_procs.h"
  211. #undef SDL_DYNAPI_PROC
  212. };
  213. // Default functions init the function table then call right thing.
  214. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  215. static rc SDLCALL fn##_DEFAULT params \
  216. { \
  217. SDL_InitDynamicAPI(); \
  218. ret jump_table.fn args; \
  219. }
  220. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  221. #include "SDL_dynapi_procs.h"
  222. #undef SDL_DYNAPI_PROC
  223. #undef SDL_DYNAPI_PROC_NO_VARARGS
  224. SDL_DYNAPI_VARARGS(static, _DEFAULT, SDL_InitDynamicAPI())
  225. // Public API functions to jump into the jump table.
  226. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  227. rc SDLCALL fn params \
  228. { \
  229. ret jump_table.fn args; \
  230. }
  231. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  232. #include "SDL_dynapi_procs.h"
  233. #undef SDL_DYNAPI_PROC
  234. #undef SDL_DYNAPI_PROC_NO_VARARGS
  235. SDL_DYNAPI_VARARGS(, , )
  236. #define ENABLE_SDL_CALL_LOGGING 0
  237. #if ENABLE_SDL_CALL_LOGGING
  238. static bool SDLCALL SDL_SetError_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  239. {
  240. char buf[512]; // !!! FIXME: dynamic allocation
  241. va_list ap;
  242. SDL_Log_REAL("SDL3CALL SDL_SetError");
  243. va_start(ap, fmt);
  244. SDL_vsnprintf_REAL(buf, sizeof(buf), fmt, ap);
  245. va_end(ap);
  246. return SDL_SetError_REAL("%s", buf);
  247. }
  248. static int SDLCALL SDL_sscanf_LOGSDLCALLS(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
  249. {
  250. int result;
  251. va_list ap;
  252. SDL_Log_REAL("SDL3CALL SDL_sscanf");
  253. va_start(ap, fmt);
  254. result = SDL_vsscanf_REAL(buf, fmt, ap);
  255. va_end(ap);
  256. return result;
  257. }
  258. static int SDLCALL SDL_snprintf_LOGSDLCALLS(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  259. {
  260. int result;
  261. va_list ap;
  262. SDL_Log_REAL("SDL3CALL SDL_snprintf");
  263. va_start(ap, fmt);
  264. result = SDL_vsnprintf_REAL(buf, maxlen, fmt, ap);
  265. va_end(ap);
  266. return result;
  267. }
  268. static int SDLCALL SDL_asprintf_LOGSDLCALLS(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  269. {
  270. int result;
  271. va_list ap;
  272. SDL_Log_REAL("SDL3CALL SDL_asprintf");
  273. va_start(ap, fmt);
  274. result = SDL_vasprintf_REAL(strp, fmt, ap);
  275. va_end(ap);
  276. return result;
  277. }
  278. static int SDLCALL SDL_swprintf_LOGSDLCALLS(SDL_OUT_Z_CAP(maxlen) wchar_t *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...)
  279. {
  280. int result;
  281. va_list ap;
  282. SDL_Log_REAL("SDL3CALL SDL_swprintf");
  283. va_start(ap, fmt);
  284. result = SDL_vswprintf_REAL(buf, maxlen, fmt, ap);
  285. va_end(ap);
  286. return result;
  287. }
  288. static size_t SDLCALL SDL_IOprintf_LOGSDLCALLS(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  289. {
  290. size_t result;
  291. va_list ap;
  292. SDL_Log_REAL("SDL3CALL SDL_IOprintf");
  293. va_start(ap, fmt);
  294. result = SDL_IOvprintf_REAL(context, fmt, ap);
  295. va_end(ap);
  296. return result;
  297. }
  298. static bool SDLCALL SDL_RenderDebugTextFormat_LOGSDLCALLS(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  299. {
  300. char buf[128], *str = buf;
  301. int result;
  302. va_list ap;
  303. SDL_Log_REAL("SDL3CALL SDL_RenderDebugTextFormat");
  304. va_start(ap, fmt);
  305. result = jump_table.SDL_vsnprintf(buf, sizeof(buf), fmt, ap);
  306. va_end(ap);
  307. if (result >= 0 && (size_t)result >= sizeof(buf)) {
  308. str = NULL;
  309. va_start(ap, fmt);
  310. result = SDL_vasprintf_REAL(&str, fmt, ap);
  311. va_end(ap);
  312. }
  313. bool retval = false;
  314. if (result >= 0) {
  315. retval = SDL_RenderDebugTextFormat_REAL(renderer, x, y, "%s", str);
  316. }
  317. if (str != buf) {
  318. jump_table.SDL_free(str);
  319. }
  320. return retval;
  321. }
  322. static void SDLCALL SDL_Log_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  323. {
  324. va_list ap;
  325. SDL_Log_REAL("SDL3CALL SDL_Log");
  326. va_start(ap, fmt);
  327. SDL_LogMessageV_REAL(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
  328. va_end(ap);
  329. }
  330. static void SDLCALL SDL_LogMessage_LOGSDLCALLS(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  331. {
  332. va_list ap;
  333. SDL_Log_REAL("SDL3CALL SDL_LogMessage");
  334. va_start(ap, fmt);
  335. SDL_LogMessageV_REAL(category, priority, fmt, ap);
  336. va_end(ap);
  337. }
  338. #define SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(logname, prio) \
  339. static void SDLCALL SDL_Log##logname##_LOGSDLCALLS(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  340. { \
  341. va_list ap; \
  342. va_start(ap, fmt); \
  343. SDL_Log_REAL("SDL3CALL SDL_Log%s", #logname); \
  344. SDL_LogMessageV_REAL(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
  345. va_end(ap); \
  346. }
  347. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Trace, TRACE)
  348. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Verbose, VERBOSE)
  349. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Debug, DEBUG)
  350. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Info, INFO)
  351. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Warn, WARN)
  352. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Error, ERROR)
  353. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Critical, CRITICAL)
  354. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  355. rc SDLCALL fn##_LOGSDLCALLS params \
  356. { \
  357. SDL_Log_REAL("SDL3CALL %s", #fn); \
  358. ret fn##_REAL args; \
  359. }
  360. #define SDL_DYNAPI_PROC_NO_VARARGS 1
  361. #include "SDL_dynapi_procs.h"
  362. #undef SDL_DYNAPI_PROC
  363. #undef SDL_DYNAPI_PROC_NO_VARARGS
  364. #endif
  365. /* we make this a static function so we can call the correct one without the
  366. system's dynamic linker resolving to the wrong version of this. */
  367. static Sint32 initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
  368. {
  369. SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *)table;
  370. if (apiver != SDL_DYNAPI_VERSION) {
  371. // !!! FIXME: can maybe handle older versions?
  372. return -1; // not compatible.
  373. } else if (tablesize > sizeof(jump_table)) {
  374. return -1; // newer version of SDL with functions we can't provide.
  375. }
  376. // Init our jump table first.
  377. #if ENABLE_SDL_CALL_LOGGING
  378. {
  379. const char *env = SDL_getenv_unsafe_REAL("SDL_DYNAPI_LOG_CALLS");
  380. const bool log_calls = (env && SDL_atoi_REAL(env));
  381. if (log_calls) {
  382. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_LOGSDLCALLS;
  383. #include "SDL_dynapi_procs.h"
  384. #undef SDL_DYNAPI_PROC
  385. } else {
  386. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_REAL;
  387. #include "SDL_dynapi_procs.h"
  388. #undef SDL_DYNAPI_PROC
  389. }
  390. }
  391. #else
  392. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_REAL;
  393. #include "SDL_dynapi_procs.h"
  394. #undef SDL_DYNAPI_PROC
  395. #endif
  396. // Then the external table...
  397. if (output_jump_table != &jump_table) {
  398. jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
  399. }
  400. // Safe to call SDL functions now; jump table is initialized!
  401. return 0; // success!
  402. }
  403. // Here's the exported entry point that fills in the jump table.
  404. // Use specific types when an "int" might suffice to keep this sane.
  405. typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
  406. extern SDL_DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
  407. Sint32 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
  408. {
  409. return initialize_jumptable(apiver, table, tablesize);
  410. }
  411. #ifdef __cplusplus
  412. }
  413. #endif
  414. // Obviously we can't use SDL_LoadObject() to load SDL. :)
  415. // Also obviously, we never close the loaded library.
  416. #if defined(WIN32) || defined(_WIN32) || defined(SDL_PLATFORM_CYGWIN)
  417. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  418. {
  419. HMODULE lib = LoadLibraryA(fname);
  420. void *result = NULL;
  421. if (lib) {
  422. result = (void *) GetProcAddress(lib, sym);
  423. if (!result) {
  424. FreeLibrary(lib);
  425. }
  426. }
  427. return result;
  428. }
  429. #elif defined(SDL_PLATFORM_UNIX) || defined(SDL_PLATFORM_APPLE) || defined(SDL_PLATFORM_HAIKU)
  430. #include <dlfcn.h>
  431. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  432. {
  433. void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
  434. void *result = NULL;
  435. if (lib) {
  436. result = dlsym(lib, sym);
  437. if (!result) {
  438. dlclose(lib);
  439. }
  440. }
  441. return result;
  442. }
  443. #else
  444. #error Please define your platform.
  445. #endif
  446. static void dynapi_warn(const char *msg)
  447. {
  448. const char *caption = "SDL Dynamic API Failure!";
  449. (void)caption;
  450. // SDL_ShowSimpleMessageBox() is a too heavy for here.
  451. #if (defined(WIN32) || defined(_WIN32) || defined(SDL_PLATFORM_CYGWIN)) && !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  452. MessageBoxA(NULL, msg, caption, MB_OK | MB_ICONERROR);
  453. #elif defined(HAVE_STDIO_H)
  454. fprintf(stderr, "\n\n%s\n%s\n\n", caption, msg);
  455. fflush(stderr);
  456. #endif
  457. }
  458. /* This is not declared in any header, although it is shared between some
  459. parts of SDL, because we don't want anything calling it without an
  460. extremely good reason. */
  461. #ifdef __cplusplus
  462. extern "C" {
  463. #endif
  464. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  465. #ifdef __WATCOMC__
  466. #pragma aux SDL_ExitProcess aborts;
  467. #endif
  468. #ifdef __cplusplus
  469. }
  470. #endif
  471. static void SDL_InitDynamicAPILocked(void)
  472. {
  473. // this can't use SDL_getenv_unsafe_REAL, because it might allocate memory before the app can set their allocator.
  474. #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
  475. // We've always used LoadLibraryA for this, so this has never worked with Unicode paths on Windows. Sorry.
  476. char envbuf[512]; // overflows will just report as environment variable being unset, but LoadLibraryA has a MAX_PATH of 260 anyhow, apparently.
  477. const DWORD rc = GetEnvironmentVariableA(SDL_DYNAMIC_API_ENVVAR, envbuf, (DWORD) sizeof (envbuf));
  478. char *libname = ((rc != 0) && (rc < sizeof (envbuf))) ? envbuf : NULL;
  479. #else
  480. char *libname = getenv(SDL_DYNAMIC_API_ENVVAR);
  481. #endif
  482. SDL_DYNAPI_ENTRYFN entry = NULL; // funcs from here by default.
  483. bool use_internal = true;
  484. if (libname) {
  485. while (*libname && !entry) {
  486. // This is evil, but we're not making any permanent changes...
  487. char *ptr = (char *)libname;
  488. while (true) {
  489. char ch = *ptr;
  490. if ((ch == ',') || (ch == '\0')) {
  491. *ptr = '\0';
  492. entry = (SDL_DYNAPI_ENTRYFN)get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
  493. *ptr = ch;
  494. libname = (ch == '\0') ? ptr : (ptr + 1);
  495. break;
  496. } else {
  497. ptr++;
  498. }
  499. }
  500. }
  501. if (!entry) {
  502. dynapi_warn("Couldn't load an overriding SDL library. Please fix or remove the " SDL_DYNAMIC_API_ENVVAR " environment variable. Using the default SDL.");
  503. // Just fill in the function pointers from this library, later.
  504. }
  505. }
  506. if (entry) {
  507. if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof(jump_table)) < 0) {
  508. 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.");
  509. // Just fill in the function pointers from this library, later.
  510. } else {
  511. use_internal = false; // We overrode SDL! Don't use the internal version!
  512. }
  513. }
  514. // Just fill in the function pointers from this library.
  515. if (use_internal) {
  516. if (initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof(jump_table)) < 0) {
  517. // Now we're screwed. Should definitely abort now.
  518. dynapi_warn("Failed to initialize internal SDL dynapi. As this would otherwise crash, we have to abort now.");
  519. #ifndef NDEBUG
  520. SDL_TriggerBreakpoint();
  521. #endif
  522. SDL_ExitProcess(86);
  523. }
  524. }
  525. // we intentionally never close the newly-loaded lib, of course.
  526. }
  527. static void SDL_InitDynamicAPI(void)
  528. {
  529. /* So the theory is that every function in the jump table defaults to
  530. * calling this function, and then replaces itself with a version that
  531. * doesn't call this function anymore. But it's possible that, in an
  532. * extreme corner case, you can have a second thread hit this function
  533. * while the jump table is being initialized by the first.
  534. * In this case, a spinlock is really painful compared to what spinlocks
  535. * _should_ be used for, but this would only happen once, and should be
  536. * insanely rare, as you would have to spin a thread outside of SDL (as
  537. * SDL_CreateThread() would also call this function before building the
  538. * new thread).
  539. */
  540. static bool already_initialized = false;
  541. static SDL_SpinLock lock = 0;
  542. SDL_LockSpinlock_REAL(&lock);
  543. if (!already_initialized) {
  544. SDL_InitDynamicAPILocked();
  545. already_initialized = true;
  546. }
  547. SDL_UnlockSpinlock_REAL(&lock);
  548. }
  549. #else // SDL_DYNAMIC_API
  550. #include <SDL3/SDL.h>
  551. Sint32 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize);
  552. Sint32 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
  553. {
  554. (void)apiver;
  555. (void)table;
  556. (void)tablesize;
  557. return -1; // not compatible.
  558. }
  559. #endif // SDL_DYNAMIC_API