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