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