src/video/x11/SDL_x11dyn.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5481 22dfc3958dc3
child 6026 a7142857e885
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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@5408
    56
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT
slouken@5408
    57
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT 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@5408
    74
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT},
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
slouken@1950
    80
static void
slouken@1950
    81
X11_GetSym(const char *fnname, int *rc, void **fn)
slouken@1950
    82
{
slouken@1950
    83
    int i;
slouken@1950
    84
    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
    85
        if (x11libs[i].lib != NULL) {
slouken@1950
    86
            *fn = SDL_LoadFunction(x11libs[i].lib, fnname);
slouken@1950
    87
            if (*fn != NULL)
slouken@1950
    88
                break;
slouken@1950
    89
        }
slouken@1950
    90
    }
slouken@1950
    91
slouken@1950
    92
#if DEBUG_DYNAMIC_X11
slouken@1950
    93
    if (*fn != NULL)
slouken@1950
    94
        printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname,
slouken@1950
    95
               *fn);
slouken@1950
    96
    else
slouken@1950
    97
        printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
slouken@1950
    98
#endif
slouken@1950
    99
slouken@1950
   100
    if (*fn == NULL)
slouken@1950
   101
        *rc = 0;                /* kill this module. */
slouken@1950
   102
}
slouken@1950
   103
slouken@1950
   104
slouken@1950
   105
/* Define all the function pointers and wrappers... */
slouken@1950
   106
#define SDL_X11_MODULE(modname)
slouken@1950
   107
#define SDL_X11_SYM(rc,fn,params,args,ret) \
slouken@1950
   108
	static rc (*p##fn) params = NULL; \
slouken@1950
   109
	rc fn params { ret p##fn args ; }
slouken@1950
   110
#include "SDL_x11sym.h"
slouken@1950
   111
#undef SDL_X11_MODULE
slouken@1950
   112
#undef SDL_X11_SYM
slouken@1950
   113
#endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
slouken@1950
   114
slouken@1950
   115
/* Annoying varargs entry point... */
slouken@1950
   116
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   117
XIC(*pXCreateIC) (XIM,...) = NULL;
bob@2325
   118
char *(*pXGetICValues) (XIC, ...) = NULL;
slouken@1950
   119
#endif
slouken@1950
   120
slouken@1950
   121
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
slouken@1950
   122
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
slouken@1950
   123
#define SDL_X11_SYM(rc,fn,params,args,ret)
slouken@1950
   124
#include "SDL_x11sym.h"
slouken@1950
   125
#undef SDL_X11_MODULE
slouken@1950
   126
#undef SDL_X11_SYM
slouken@1950
   127
slouken@1950
   128
slouken@1950
   129
static int x11_load_refcount = 0;
slouken@1950
   130
slouken@1950
   131
void
slouken@1950
   132
SDL_X11_UnloadSymbols(void)
slouken@1950
   133
{
slouken@1950
   134
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   135
    /* Don't actually unload if more than one module is using the libs... */
slouken@1950
   136
    if (x11_load_refcount > 0) {
slouken@1950
   137
        if (--x11_load_refcount == 0) {
slouken@1950
   138
            int i;
slouken@1950
   139
slouken@1950
   140
            /* set all the function pointers to NULL. */
slouken@1950
   141
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
slouken@1950
   142
#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
slouken@1950
   143
#include "SDL_x11sym.h"
slouken@1950
   144
#undef SDL_X11_MODULE
slouken@1950
   145
#undef SDL_X11_SYM
slouken@1950
   146
slouken@1950
   147
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   148
            pXCreateIC = NULL;
bob@2325
   149
            pXGetICValues = NULL;
slouken@1950
   150
#endif
slouken@1950
   151
slouken@1950
   152
            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   153
                if (x11libs[i].lib != NULL) {
slouken@1950
   154
                    SDL_UnloadObject(x11libs[i].lib);
slouken@1950
   155
                    x11libs[i].lib = NULL;
slouken@1950
   156
                }
slouken@1950
   157
            }
slouken@1950
   158
        }
slouken@1950
   159
    }
slouken@1950
   160
#endif
slouken@1950
   161
}
slouken@1950
   162
slouken@1950
   163
/* returns non-zero if all needed symbols were loaded. */
slouken@1950
   164
int
slouken@1950
   165
SDL_X11_LoadSymbols(void)
slouken@1950
   166
{
slouken@1950
   167
    int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
slouken@1950
   168
slouken@1950
   169
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   170
    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
slouken@1950
   171
    if (x11_load_refcount++ == 0) {
slouken@1950
   172
        int i;
slouken@1950
   173
        int *thismod = NULL;
slouken@1950
   174
        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   175
            if (x11libs[i].libname != NULL) {
slouken@1950
   176
                x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
slouken@1950
   177
            }
slouken@1950
   178
        }
slouken@1950
   179
#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
slouken@1950
   180
#define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
slouken@1950
   181
#include "SDL_x11sym.h"
slouken@1950
   182
#undef SDL_X11_MODULE
slouken@1950
   183
#undef SDL_X11_SYM
slouken@1950
   184
slouken@1950
   185
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   186
        X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8, (void **) &pXCreateIC);
bob@2325
   187
        X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8,
bob@2325
   188
                   (void **) &pXGetICValues);
slouken@1950
   189
#endif
slouken@1950
   190
slouken@2120
   191
        if (SDL_X11_HAVE_BASEXLIB) {
slouken@2120
   192
            /* all required symbols loaded. */
icculus@2118
   193
            SDL_ClearError();
icculus@2118
   194
        } else {
slouken@2120
   195
            /* in case something got loaded... */
slouken@2120
   196
            SDL_X11_UnloadSymbols();
slouken@1950
   197
            rc = 0;
slouken@1950
   198
        }
slouken@1950
   199
    }
slouken@1950
   200
#else
slouken@1950
   201
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   202
    pXCreateIC = XCreateIC;
bob@2325
   203
    pXGetICValues = XGetICValues;
slouken@1950
   204
#endif
slouken@1950
   205
#endif
slouken@1950
   206
slouken@1950
   207
    return rc;
slouken@1950
   208
}
slouken@1950
   209
slouken@5481
   210
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   211
slouken@1950
   212
/* vi: set ts=4 sw=4 expandtab: */