cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_dynapi.c (11118B)


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