src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 07 Dec 2012 23:26:28 -0500
changeset 6724 6c5ed0c4cc6d
parent 6376 f7ec45572c54
child 6885 700f1b25f77f
permissions -rw-r--r--
X11 msgbox: try to protect the existing setlocale() state.
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1950
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1950
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1950
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1950
    20
*/
slouken@1950
    21
#include "SDL_config.h"
slouken@1950
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@1950
    25
#define DEBUG_DYNAMIC_X11 0
slouken@1950
    26
slouken@1950
    27
#include "SDL_x11dyn.h"
slouken@1950
    28
slouken@1950
    29
#if DEBUG_DYNAMIC_X11
slouken@1950
    30
#include <stdio.h>
slouken@1950
    31
#endif
slouken@1950
    32
slouken@1950
    33
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
    34
slouken@1950
    35
#include "SDL_name.h"
slouken@1950
    36
#include "SDL_loadso.h"
slouken@1950
    37
slouken@1950
    38
typedef struct
slouken@1950
    39
{
slouken@1950
    40
    void *lib;
slouken@1950
    41
    const char *libname;
slouken@1950
    42
} x11dynlib;
slouken@1950
    43
slouken@1950
    44
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
    45
#define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
slouken@1950
    46
#endif
slouken@1950
    47
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
slouken@1950
    48
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
slouken@1950
    49
#endif
slouken@5471
    50
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR
slouken@5471
    51
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR NULL
slouken@5471
    52
#endif
slouken@5408
    53
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA
slouken@5408
    54
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA NULL
slouken@5408
    55
#endif
slouken@6311
    56
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2
slouken@6311
    57
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 NULL
slouken@1950
    58
#endif
slouken@1950
    59
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
slouken@1950
    60
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
slouken@1950
    61
#endif
bob@3036
    62
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS
bob@3036
    63
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS NULL
bob@3036
    64
#endif
slouken@5408
    65
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE
slouken@5408
    66
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE NULL
slouken@5408
    67
#endif
slouken@1950
    68
slouken@1950
    69
static x11dynlib x11libs[] = {
slouken@1950
    70
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
slouken@1950
    71
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
slouken@5471
    72
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR},
slouken@5408
    73
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA},
slouken@6311
    74
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2},
slouken@1950
    75
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
slouken@3025
    76
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS},
slouken@5408
    77
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE}
slouken@1950
    78
};
slouken@1950
    79
icculus@6376
    80
static void *
icculus@6376
    81
X11_GetSym(const char *fnname, int *pHasModule)
slouken@1950
    82
{
slouken@1950
    83
    int i;
icculus@6376
    84
    void *fn = NULL;
slouken@1950
    85
    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
    86
        if (x11libs[i].lib != NULL) {
icculus@6376
    87
            fn = SDL_LoadFunction(x11libs[i].lib, fnname);
icculus@6376
    88
            if (fn != NULL)
slouken@1950
    89
                break;
slouken@1950
    90
        }
slouken@1950
    91
    }
slouken@1950
    92
slouken@1950
    93
#if DEBUG_DYNAMIC_X11
icculus@6376
    94
    if (fn != NULL)
icculus@6376
    95
        printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname, fn);
slouken@1950
    96
    else
slouken@1950
    97
        printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
slouken@1950
    98
#endif
slouken@1950
    99
icculus@6376
   100
    if (fn == NULL)
icculus@6376
   101
        *pHasModule = 0;  /* kill this module. */
icculus@6376
   102
icculus@6376
   103
    return fn;
slouken@1950
   104
}
slouken@1950
   105
slouken@1950
   106
slouken@1950
   107
/* Define all the function pointers and wrappers... */
slouken@1950
   108
#define SDL_X11_MODULE(modname)
slouken@1950
   109
#define SDL_X11_SYM(rc,fn,params,args,ret) \
icculus@6376
   110
	typedef rc (*SDL_DYNX11FN_##fn) params; \
icculus@6376
   111
	static SDL_DYNX11FN_##fn p##fn = NULL; \
slouken@1950
   112
	rc fn params { ret p##fn args ; }
slouken@1950
   113
#include "SDL_x11sym.h"
slouken@1950
   114
#undef SDL_X11_MODULE
slouken@1950
   115
#undef SDL_X11_SYM
slouken@1950
   116
#endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
slouken@1950
   117
slouken@1950
   118
/* Annoying varargs entry point... */
slouken@1950
   119
#ifdef X_HAVE_UTF8_STRING
icculus@6376
   120
typedef XIC(*SDL_DYNX11FN_XCreateIC) (XIM,...);
icculus@6376
   121
SDL_DYNX11FN_XCreateIC pXCreateIC = NULL;
icculus@6376
   122
typedef char *(*SDL_DYNX11FN_XGetICValues) (XIC, ...);
icculus@6376
   123
SDL_DYNX11FN_XGetICValues pXGetICValues = NULL;
slouken@1950
   124
#endif
slouken@1950
   125
slouken@1950
   126
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
icculus@6026
   127
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 0;
slouken@1950
   128
#define SDL_X11_SYM(rc,fn,params,args,ret)
slouken@1950
   129
#include "SDL_x11sym.h"
slouken@1950
   130
#undef SDL_X11_MODULE
slouken@1950
   131
#undef SDL_X11_SYM
slouken@1950
   132
slouken@1950
   133
slouken@1950
   134
static int x11_load_refcount = 0;
slouken@1950
   135
slouken@1950
   136
void
slouken@1950
   137
SDL_X11_UnloadSymbols(void)
slouken@1950
   138
{
slouken@1950
   139
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   140
    /* Don't actually unload if more than one module is using the libs... */
slouken@1950
   141
    if (x11_load_refcount > 0) {
slouken@1950
   142
        if (--x11_load_refcount == 0) {
slouken@1950
   143
            int i;
slouken@1950
   144
slouken@1950
   145
            /* set all the function pointers to NULL. */
icculus@6026
   146
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 0;
slouken@1950
   147
#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
slouken@1950
   148
#include "SDL_x11sym.h"
slouken@1950
   149
#undef SDL_X11_MODULE
slouken@1950
   150
#undef SDL_X11_SYM
slouken@1950
   151
slouken@1950
   152
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   153
            pXCreateIC = NULL;
bob@2325
   154
            pXGetICValues = NULL;
slouken@1950
   155
#endif
slouken@1950
   156
slouken@1950
   157
            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   158
                if (x11libs[i].lib != NULL) {
slouken@1950
   159
                    SDL_UnloadObject(x11libs[i].lib);
slouken@1950
   160
                    x11libs[i].lib = NULL;
slouken@1950
   161
                }
slouken@1950
   162
            }
slouken@1950
   163
        }
slouken@1950
   164
    }
slouken@1950
   165
#endif
slouken@1950
   166
}
slouken@1950
   167
slouken@1950
   168
/* returns non-zero if all needed symbols were loaded. */
slouken@1950
   169
int
slouken@1950
   170
SDL_X11_LoadSymbols(void)
slouken@1950
   171
{
slouken@1950
   172
    int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
slouken@1950
   173
slouken@1950
   174
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   175
    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
slouken@1950
   176
    if (x11_load_refcount++ == 0) {
slouken@1950
   177
        int i;
slouken@1950
   178
        int *thismod = NULL;
slouken@1950
   179
        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   180
            if (x11libs[i].libname != NULL) {
slouken@1950
   181
                x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
slouken@1950
   182
            }
slouken@1950
   183
        }
icculus@6030
   184
icculus@6030
   185
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1; /* default yes */
icculus@6030
   186
#define SDL_X11_SYM(a,fn,x,y,z)
icculus@6030
   187
#include "SDL_x11sym.h"
icculus@6030
   188
#undef SDL_X11_MODULE
icculus@6030
   189
#undef SDL_X11_SYM
icculus@6030
   190
slouken@1950
   191
#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
icculus@6376
   192
#define SDL_X11_SYM(a,fn,x,y,z) p##fn = (SDL_DYNX11FN_##fn) X11_GetSym(#fn,thismod);
slouken@1950
   193
#include "SDL_x11sym.h"
slouken@1950
   194
#undef SDL_X11_MODULE
slouken@1950
   195
#undef SDL_X11_SYM
slouken@1950
   196
slouken@1950
   197
#ifdef X_HAVE_UTF8_STRING
icculus@6376
   198
        pXCreateIC = (SDL_DYNX11FN_XCreateIC)
icculus@6376
   199
                        X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8);
icculus@6376
   200
        pXGetICValues = (SDL_DYNX11FN_XGetICValues)
icculus@6376
   201
                        X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8);
slouken@1950
   202
#endif
slouken@1950
   203
slouken@2120
   204
        if (SDL_X11_HAVE_BASEXLIB) {
slouken@2120
   205
            /* all required symbols loaded. */
icculus@2118
   206
            SDL_ClearError();
icculus@2118
   207
        } else {
slouken@2120
   208
            /* in case something got loaded... */
slouken@2120
   209
            SDL_X11_UnloadSymbols();
slouken@1950
   210
            rc = 0;
slouken@1950
   211
        }
slouken@1950
   212
    }
slouken@1950
   213
#else
slouken@1950
   214
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   215
    pXCreateIC = XCreateIC;
bob@2325
   216
    pXGetICValues = XGetICValues;
slouken@1950
   217
#endif
slouken@1950
   218
#endif
slouken@1950
   219
slouken@1950
   220
    return rc;
slouken@1950
   221
}
slouken@1950
   222
slouken@5481
   223
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   224
slouken@1950
   225
/* vi: set ts=4 sw=4 expandtab: */