src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 13 Jun 2007 08:02:43 +0000
changeset 2118 238db749017a
parent 1950 a344e42bce3b
child 2120 2c835d58faad
permissions -rw-r--r--
Merged r3062:3063 from branches/SDL-1.2: clear inaccurate x11dyn error strings.
slouken@1950
     1
/*
slouken@1950
     2
    SDL - Simple DirectMedia Layer
slouken@1950
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@1950
     4
slouken@1950
     5
    This library is free software; you can redistribute it and/or
slouken@1950
     6
    modify it under the terms of the GNU Library General Public
slouken@1950
     7
    License as published by the Free Software Foundation; either
slouken@1950
     8
    version 2 of the License, or (at your option) any later version.
slouken@1950
     9
slouken@1950
    10
    This library is distributed in the hope that it will be useful,
slouken@1950
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1950
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1950
    13
    Library General Public License for more details.
slouken@1950
    14
slouken@1950
    15
    You should have received a copy of the GNU Library General Public
slouken@1950
    16
    License along with this library; if not, write to the Free
slouken@1950
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@1950
    18
slouken@1950
    19
    Sam Lantinga
slouken@1950
    20
    slouken@libsdl.org
slouken@1950
    21
*/
slouken@1950
    22
#include "SDL_config.h"
slouken@1950
    23
slouken@1950
    24
#define DEBUG_DYNAMIC_X11 0
slouken@1950
    25
slouken@1950
    26
#include "SDL_x11dyn.h"
slouken@1950
    27
slouken@1950
    28
#if DEBUG_DYNAMIC_X11
slouken@1950
    29
#include <stdio.h>
slouken@1950
    30
#endif
slouken@1950
    31
slouken@1950
    32
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
    33
slouken@1950
    34
#include "SDL_name.h"
slouken@1950
    35
#include "SDL_loadso.h"
slouken@1950
    36
slouken@1950
    37
typedef struct
slouken@1950
    38
{
slouken@1950
    39
    void *lib;
slouken@1950
    40
    const char *libname;
slouken@1950
    41
} x11dynlib;
slouken@1950
    42
slouken@1950
    43
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
    44
#define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
slouken@1950
    45
#endif
slouken@1950
    46
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
slouken@1950
    47
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
slouken@1950
    48
#endif
slouken@1950
    49
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER
slouken@1950
    50
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER NULL
slouken@1950
    51
#endif
slouken@1950
    52
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
slouken@1950
    53
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
slouken@1950
    54
#endif
slouken@1950
    55
slouken@1950
    56
static x11dynlib x11libs[] = {
slouken@1950
    57
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
slouken@1950
    58
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
slouken@1950
    59
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER},
slouken@1950
    60
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
slouken@1950
    61
};
slouken@1950
    62
slouken@1950
    63
static void
slouken@1950
    64
X11_GetSym(const char *fnname, int *rc, void **fn)
slouken@1950
    65
{
slouken@1950
    66
    int i;
slouken@1950
    67
    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
    68
        if (x11libs[i].lib != NULL) {
slouken@1950
    69
            *fn = SDL_LoadFunction(x11libs[i].lib, fnname);
slouken@1950
    70
            if (*fn != NULL)
slouken@1950
    71
                break;
slouken@1950
    72
        }
slouken@1950
    73
    }
slouken@1950
    74
slouken@1950
    75
#if DEBUG_DYNAMIC_X11
slouken@1950
    76
    if (*fn != NULL)
slouken@1950
    77
        printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname,
slouken@1950
    78
               *fn);
slouken@1950
    79
    else
slouken@1950
    80
        printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
slouken@1950
    81
#endif
slouken@1950
    82
slouken@1950
    83
    if (*fn == NULL)
slouken@1950
    84
        *rc = 0;                /* kill this module. */
slouken@1950
    85
}
slouken@1950
    86
slouken@1950
    87
slouken@1950
    88
/* Define all the function pointers and wrappers... */
slouken@1950
    89
#define SDL_X11_MODULE(modname)
slouken@1950
    90
#define SDL_X11_SYM(rc,fn,params,args,ret) \
slouken@1950
    91
	static rc (*p##fn) params = NULL; \
slouken@1950
    92
	rc fn params { ret p##fn args ; }
slouken@1950
    93
#include "SDL_x11sym.h"
slouken@1950
    94
#undef SDL_X11_MODULE
slouken@1950
    95
#undef SDL_X11_SYM
slouken@1950
    96
#endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
slouken@1950
    97
slouken@1950
    98
/* Annoying varargs entry point... */
slouken@1950
    99
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   100
XIC(*pXCreateIC) (XIM,...) = NULL;
slouken@1950
   101
#endif
slouken@1950
   102
slouken@1950
   103
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
slouken@1950
   104
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
slouken@1950
   105
#define SDL_X11_SYM(rc,fn,params,args,ret)
slouken@1950
   106
#include "SDL_x11sym.h"
slouken@1950
   107
#undef SDL_X11_MODULE
slouken@1950
   108
#undef SDL_X11_SYM
slouken@1950
   109
slouken@1950
   110
slouken@1950
   111
static int x11_load_refcount = 0;
slouken@1950
   112
slouken@1950
   113
void
slouken@1950
   114
SDL_X11_UnloadSymbols(void)
slouken@1950
   115
{
slouken@1950
   116
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   117
    /* Don't actually unload if more than one module is using the libs... */
slouken@1950
   118
    if (x11_load_refcount > 0) {
slouken@1950
   119
        if (--x11_load_refcount == 0) {
slouken@1950
   120
            int i;
slouken@1950
   121
slouken@1950
   122
            /* set all the function pointers to NULL. */
slouken@1950
   123
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
slouken@1950
   124
#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
slouken@1950
   125
#include "SDL_x11sym.h"
slouken@1950
   126
#undef SDL_X11_MODULE
slouken@1950
   127
#undef SDL_X11_SYM
slouken@1950
   128
slouken@1950
   129
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   130
            pXCreateIC = NULL;
slouken@1950
   131
#endif
slouken@1950
   132
slouken@1950
   133
            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   134
                if (x11libs[i].lib != NULL) {
slouken@1950
   135
                    SDL_UnloadObject(x11libs[i].lib);
slouken@1950
   136
                    x11libs[i].lib = NULL;
slouken@1950
   137
                }
slouken@1950
   138
            }
slouken@1950
   139
        }
slouken@1950
   140
    }
slouken@1950
   141
#endif
slouken@1950
   142
}
slouken@1950
   143
slouken@1950
   144
/* returns non-zero if all needed symbols were loaded. */
slouken@1950
   145
int
slouken@1950
   146
SDL_X11_LoadSymbols(void)
slouken@1950
   147
{
slouken@1950
   148
    int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
slouken@1950
   149
slouken@1950
   150
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   151
    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
slouken@1950
   152
    if (x11_load_refcount++ == 0) {
slouken@1950
   153
        int i;
slouken@1950
   154
        int *thismod = NULL;
slouken@1950
   155
        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   156
            if (x11libs[i].libname != NULL) {
slouken@1950
   157
                x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
slouken@1950
   158
            }
slouken@1950
   159
        }
slouken@1950
   160
#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
slouken@1950
   161
#define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
slouken@1950
   162
#include "SDL_x11sym.h"
slouken@1950
   163
#undef SDL_X11_MODULE
slouken@1950
   164
#undef SDL_X11_SYM
slouken@1950
   165
slouken@1950
   166
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   167
        X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8, (void **) &pXCreateIC);
slouken@1950
   168
#endif
slouken@1950
   169
icculus@2118
   170
        if (SDL_X11_HAVE_BASEXLIB) {  /* all required symbols loaded. */
icculus@2118
   171
            SDL_ClearError();
icculus@2118
   172
        } else {
icculus@2118
   173
            SDL_X11_UnloadSymbols();  /* in case something got loaded... */
slouken@1950
   174
            rc = 0;
slouken@1950
   175
        }
slouken@1950
   176
    }
slouken@1950
   177
#else
slouken@1950
   178
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   179
    pXCreateIC = XCreateIC;
slouken@1950
   180
#endif
slouken@1950
   181
#endif
slouken@1950
   182
slouken@1950
   183
    return rc;
slouken@1950
   184
}
slouken@1950
   185
slouken@1950
   186
/* vi: set ts=4 sw=4 expandtab: */