SDL  2.0
SDL_dynapi.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
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 
50 static 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
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! */
121 typedef 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
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. */
134  #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) fn##_DEFAULT,
135  #include "SDL_dynapi_procs.h"
136  #undef SDL_DYNAPI_PROC
137 };
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
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
165 #else
166 /* !!! FIXME: need the jump magic. */
167 #error Write me.
168 #endif
169 
171 
172 /* Here's the exported entry point that fills in the jump table. */
173 /* Use specific types when an "int" might suffice to keep this sane. */
174 typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
177 Sint32
178 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
179 {
180  SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
181 
182  if (apiver != SDL_DYNAPI_VERSION) {
183  /* !!! FIXME: can maybe handle older versions? */
184  return -1; /* not compatible. */
185  } else if (tablesize > sizeof (jump_table)) {
186  return -1; /* newer version of SDL with functions we can't provide. */
187  }
189  /* Init our jump table first. */
190  #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) jump_table.fn = fn##_REAL;
191  #include "SDL_dynapi_procs.h"
192  #undef SDL_DYNAPI_PROC
193 
194  /* Then the external table... */
195  if (output_jump_table != &jump_table) {
196  jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
197  }
199  /* Safe to call SDL functions now; jump table is initialized! */
201  return 0; /* success! */
202 }
203 
205 /* Obviously we can't use SDL_LoadObject() to load SDL. :) */
206 /* Also obviously, we never close the loaded library. */
207 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
208 #ifndef WIN32_LEAN_AND_MEAN
209 #define WIN32_LEAN_AND_MEAN 1
210 #endif
211 #include <windows.h>
212 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
213 {
214  HANDLE lib = LoadLibraryA(fname);
215  void *retval = NULL;
216  if (lib) {
217  retval = GetProcAddress(lib, sym);
218  if (retval == NULL) {
219  FreeLibrary(lib);
220  }
221  }
222  return retval;
223 }
225 #elif defined(unix) || defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__) || defined(__QNX__)
226 #include <dlfcn.h>
227 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
228 {
229  void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
230  void *retval = NULL;
231  if (lib != NULL) {
232  retval = dlsym(lib, sym);
233  if (retval == NULL) {
234  dlclose(lib);
235  }
236  }
237  return retval;
238 }
239 
240 #elif defined(__OS2__)
241 static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
242 {
243  HMODULE hmodule;
244  PFN retval = NULL;
245  char error[256];
246  if (DosLoadModule(&error, sizeof(error), fname, &hmodule) == NO_ERROR) {
247  if (DosQueryProcAddr(hmodule, 0, sym, &retval) != NO_ERROR) {
248  DosFreeModule(hmodule);
249  }
250  }
251  return (void *) retval;
252 }
253 
254 #else
255 #error Please define your platform.
256 #endif
257 
259 static void
261 {
262  const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
263  SDL_DYNAPI_ENTRYFN entry = SDL_DYNAPI_entry; /* funcs from here by default. */
265  if (libname) {
266  entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
267  if (!entry) {
268  /* !!! FIXME: fail to startup here instead? */
269  /* !!! FIXME: definitely warn user. */
270  /* Just fill in the function pointers from this library. */
271  entry = SDL_DYNAPI_entry;
272  }
273  }
275  if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
276  /* !!! FIXME: fail to startup here instead? */
277  /* !!! FIXME: definitely warn user. */
278  /* Just fill in the function pointers from this library. */
279  if (entry != SDL_DYNAPI_entry) {
280  if (!SDL_DYNAPI_entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table))) {
281  /* !!! FIXME: now we're screwed. Should definitely abort now. */
282  }
283  }
284  }
285 
286  /* we intentionally never close the newly-loaded lib, of course. */
287 }
289 static void
291 {
292  /* So the theory is that every function in the jump table defaults to
293  * calling this function, and then replaces itself with a version that
294  * doesn't call this function anymore. But it's possible that, in an
295  * extreme corner case, you can have a second thread hit this function
296  * while the jump table is being initialized by the first.
297  * In this case, a spinlock is really painful compared to what spinlocks
298  * _should_ be used for, but this would only happen once, and should be
299  * insanely rare, as you would have to spin a thread outside of SDL (as
300  * SDL_CreateThread() would also call this function before building the
301  * new thread).
302  */
303  static SDL_bool already_initialized = SDL_FALSE;
305  /* SDL_AtomicLock calls SDL mutex functions to emulate if
306  SDL_ATOMIC_DISABLED, which we can't do here, so in such a
307  configuration, you're on your own. */
308  #if !SDL_ATOMIC_DISABLED
309  static SDL_SpinLock lock = 0;
310  SDL_AtomicLock_REAL(&lock);
311  #endif
313  if (!already_initialized) {
315  already_initialized = SDL_TRUE;
316  }
317 
318  #if !SDL_ATOMIC_DISABLED
319  SDL_AtomicUnlock_REAL(&lock);
320  #endif
321 }
323 #endif /* SDL_DYNAMIC_API */
325 /* vi: set ts=4 sw=4 expandtab: */
GLenum GLsizei GLenum GLenum const void * table
static SDL_INLINE void * get_sdlapi_entry(const char *fname, const char *sym)
Definition: SDL_dynapi.c:212
uint32_t Uint32
Definition: SDL_stdinc.h:181
Sint32(* SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize)
Definition: SDL_dynapi.c:174
#define SDL_DYNAPI_VERSION
Definition: SDL_dynapi.c:48
SDL_bool retval
#define DECLSPEC
Definition: SDL_internal.h:44
static void SDL_InitDynamicAPI(void)
Definition: SDL_dynapi.c:290
static SDL_DYNAPI_jump_table jump_table
Definition: SDL_dynapi.c:133
static void SDL_InitDynamicAPILocked(void)
Definition: SDL_dynapi.c:260
int32_t Sint32
Definition: SDL_stdinc.h:175
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
SDL_mutex * lock
Definition: SDL_events.c:77
Sint32 SDL_DYNAPI_entry(Uint32, void *, Uint32)
Definition: SDL_dynapi.c:178
#define SDL_INLINE
Definition: begin_code.h:131
int SDL_SpinLock
Definition: SDL_atomic.h:89
#define SDL_DYNAPI_VARARGS(_static, name, initcall)
Definition: SDL_dynapi.c:71
#define SDLCALL
Definition: SDL_internal.h:45