Skip to content

Latest commit

 

History

History
332 lines (283 loc) · 11.6 KB

SDL_dynapi.c

File metadata and controls

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