SDL_dynapi.c 11 KB

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