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