src/video/x11/SDL_x11dyn.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 3697 f7b03b6838cb
child 5408 35a63789c3f2
permissions -rw-r--r--
Happy 2011! :)
slouken@1950
     1
/*
slouken@1950
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@1950
     4
slouken@1950
     5
    This library is free software; you can redistribute it and/or
slouken@3697
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1950
     7
    License as published by the Free Software Foundation; either
slouken@3697
     8
    version 2.1 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@3697
    13
    Lesser General Public License for more details.
slouken@1950
    14
slouken@3697
    15
    You should have received a copy of the GNU Lesser General Public
slouken@3697
    16
    License along with this library; if not, write to the Free Software
slouken@3697
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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
icculus@2763
    55
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT
icculus@2763
    56
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT NULL
icculus@2763
    57
#endif
bob@3036
    58
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS
bob@3036
    59
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS NULL
bob@3036
    60
#endif
slouken@1950
    61
slouken@1950
    62
static x11dynlib x11libs[] = {
slouken@1950
    63
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
slouken@1950
    64
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
slouken@1950
    65
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER},
slouken@1950
    66
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
icculus@2763
    67
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT},
slouken@3025
    68
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS},
slouken@1950
    69
};
slouken@1950
    70
slouken@1950
    71
static void
slouken@1950
    72
X11_GetSym(const char *fnname, int *rc, void **fn)
slouken@1950
    73
{
slouken@1950
    74
    int i;
slouken@1950
    75
    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
    76
        if (x11libs[i].lib != NULL) {
slouken@1950
    77
            *fn = SDL_LoadFunction(x11libs[i].lib, fnname);
slouken@1950
    78
            if (*fn != NULL)
slouken@1950
    79
                break;
slouken@1950
    80
        }
slouken@1950
    81
    }
slouken@1950
    82
slouken@1950
    83
#if DEBUG_DYNAMIC_X11
slouken@1950
    84
    if (*fn != NULL)
slouken@1950
    85
        printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname,
slouken@1950
    86
               *fn);
slouken@1950
    87
    else
slouken@1950
    88
        printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
slouken@1950
    89
#endif
slouken@1950
    90
slouken@1950
    91
    if (*fn == NULL)
slouken@1950
    92
        *rc = 0;                /* kill this module. */
slouken@1950
    93
}
slouken@1950
    94
slouken@1950
    95
slouken@1950
    96
/* Define all the function pointers and wrappers... */
slouken@1950
    97
#define SDL_X11_MODULE(modname)
slouken@1950
    98
#define SDL_X11_SYM(rc,fn,params,args,ret) \
slouken@1950
    99
	static rc (*p##fn) params = NULL; \
slouken@1950
   100
	rc fn params { ret p##fn args ; }
slouken@1950
   101
#include "SDL_x11sym.h"
slouken@1950
   102
#undef SDL_X11_MODULE
slouken@1950
   103
#undef SDL_X11_SYM
slouken@1950
   104
#endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
slouken@1950
   105
slouken@1950
   106
/* Annoying varargs entry point... */
slouken@1950
   107
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   108
XIC(*pXCreateIC) (XIM,...) = NULL;
bob@2325
   109
char *(*pXGetICValues) (XIC, ...) = NULL;
slouken@1950
   110
#endif
slouken@1950
   111
slouken@1950
   112
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
slouken@1950
   113
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
slouken@1950
   114
#define SDL_X11_SYM(rc,fn,params,args,ret)
slouken@1950
   115
#include "SDL_x11sym.h"
slouken@1950
   116
#undef SDL_X11_MODULE
slouken@1950
   117
#undef SDL_X11_SYM
slouken@1950
   118
slouken@1950
   119
slouken@1950
   120
static int x11_load_refcount = 0;
slouken@1950
   121
slouken@1950
   122
void
slouken@1950
   123
SDL_X11_UnloadSymbols(void)
slouken@1950
   124
{
slouken@1950
   125
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   126
    /* Don't actually unload if more than one module is using the libs... */
slouken@1950
   127
    if (x11_load_refcount > 0) {
slouken@1950
   128
        if (--x11_load_refcount == 0) {
slouken@1950
   129
            int i;
slouken@1950
   130
slouken@1950
   131
            /* set all the function pointers to NULL. */
slouken@1950
   132
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
slouken@1950
   133
#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
slouken@1950
   134
#include "SDL_x11sym.h"
slouken@1950
   135
#undef SDL_X11_MODULE
slouken@1950
   136
#undef SDL_X11_SYM
slouken@1950
   137
slouken@1950
   138
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   139
            pXCreateIC = NULL;
bob@2325
   140
            pXGetICValues = NULL;
slouken@1950
   141
#endif
slouken@1950
   142
slouken@1950
   143
            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   144
                if (x11libs[i].lib != NULL) {
slouken@1950
   145
                    SDL_UnloadObject(x11libs[i].lib);
slouken@1950
   146
                    x11libs[i].lib = NULL;
slouken@1950
   147
                }
slouken@1950
   148
            }
slouken@1950
   149
        }
slouken@1950
   150
    }
slouken@1950
   151
#endif
slouken@1950
   152
}
slouken@1950
   153
slouken@1950
   154
/* returns non-zero if all needed symbols were loaded. */
slouken@1950
   155
int
slouken@1950
   156
SDL_X11_LoadSymbols(void)
slouken@1950
   157
{
slouken@1950
   158
    int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
slouken@1950
   159
slouken@1950
   160
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   161
    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
slouken@1950
   162
    if (x11_load_refcount++ == 0) {
slouken@1950
   163
        int i;
slouken@1950
   164
        int *thismod = NULL;
slouken@1950
   165
        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   166
            if (x11libs[i].libname != NULL) {
slouken@1950
   167
                x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
slouken@1950
   168
            }
slouken@1950
   169
        }
slouken@1950
   170
#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
slouken@1950
   171
#define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
slouken@1950
   172
#include "SDL_x11sym.h"
slouken@1950
   173
#undef SDL_X11_MODULE
slouken@1950
   174
#undef SDL_X11_SYM
slouken@1950
   175
slouken@1950
   176
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   177
        X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8, (void **) &pXCreateIC);
bob@2325
   178
        X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8,
bob@2325
   179
                   (void **) &pXGetICValues);
slouken@1950
   180
#endif
slouken@1950
   181
slouken@2120
   182
        if (SDL_X11_HAVE_BASEXLIB) {
slouken@2120
   183
            /* all required symbols loaded. */
icculus@2118
   184
            SDL_ClearError();
icculus@2118
   185
        } else {
slouken@2120
   186
            /* in case something got loaded... */
slouken@2120
   187
            SDL_X11_UnloadSymbols();
slouken@1950
   188
            rc = 0;
slouken@1950
   189
        }
slouken@1950
   190
    }
slouken@1950
   191
#else
slouken@1950
   192
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   193
    pXCreateIC = XCreateIC;
bob@2325
   194
    pXGetICValues = XGetICValues;
slouken@1950
   195
#endif
slouken@1950
   196
#endif
slouken@1950
   197
slouken@1950
   198
    return rc;
slouken@1950
   199
}
slouken@1950
   200
slouken@1950
   201
/* vi: set ts=4 sw=4 expandtab: */