src/video/x11/SDL_x11dyn.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 17 Sep 2008 08:20:57 +0000
changeset 2763 6fc50bdd88c0
parent 2325 c7bcf84ba1b9
child 2859 99210400e8b9
permissions -rw-r--r--
Some cleanups on the new XInput code.

One or two things got moved around, but largely this is hooked up correctly
in the Unix configure system now: it can be dynamically loaded and fallback
gracefully if not available, or libXi can be directly linked to libSDL.

XInput support can be --disable'd from the configure script, too (defaults to
enabled).

Please note that while the framework is in place to gracefully fallback, the
current state of the source requires XInput. We'll need to adjust a few
things still to correct this.
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
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
slouken@1950
    58
slouken@1950
    59
static x11dynlib x11libs[] = {
slouken@1950
    60
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
slouken@1950
    61
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
slouken@1950
    62
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER},
slouken@1950
    63
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
icculus@2763
    64
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT},
slouken@1950
    65
};
slouken@1950
    66
slouken@1950
    67
static void
slouken@1950
    68
X11_GetSym(const char *fnname, int *rc, void **fn)
slouken@1950
    69
{
slouken@1950
    70
    int i;
slouken@1950
    71
    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
    72
        if (x11libs[i].lib != NULL) {
slouken@1950
    73
            *fn = SDL_LoadFunction(x11libs[i].lib, fnname);
slouken@1950
    74
            if (*fn != NULL)
slouken@1950
    75
                break;
slouken@1950
    76
        }
slouken@1950
    77
    }
slouken@1950
    78
slouken@1950
    79
#if DEBUG_DYNAMIC_X11
slouken@1950
    80
    if (*fn != NULL)
slouken@1950
    81
        printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname,
slouken@1950
    82
               *fn);
slouken@1950
    83
    else
slouken@1950
    84
        printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
slouken@1950
    85
#endif
slouken@1950
    86
slouken@1950
    87
    if (*fn == NULL)
slouken@1950
    88
        *rc = 0;                /* kill this module. */
slouken@1950
    89
}
slouken@1950
    90
slouken@1950
    91
slouken@1950
    92
/* Define all the function pointers and wrappers... */
slouken@1950
    93
#define SDL_X11_MODULE(modname)
slouken@1950
    94
#define SDL_X11_SYM(rc,fn,params,args,ret) \
slouken@1950
    95
	static rc (*p##fn) params = NULL; \
slouken@1950
    96
	rc fn params { ret p##fn args ; }
slouken@1950
    97
#include "SDL_x11sym.h"
slouken@1950
    98
#undef SDL_X11_MODULE
slouken@1950
    99
#undef SDL_X11_SYM
slouken@1950
   100
#endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
slouken@1950
   101
slouken@1950
   102
/* Annoying varargs entry point... */
slouken@1950
   103
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   104
XIC(*pXCreateIC) (XIM,...) = NULL;
bob@2325
   105
char *(*pXGetICValues) (XIC, ...) = NULL;
slouken@1950
   106
#endif
slouken@1950
   107
slouken@1950
   108
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
slouken@1950
   109
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
slouken@1950
   110
#define SDL_X11_SYM(rc,fn,params,args,ret)
slouken@1950
   111
#include "SDL_x11sym.h"
slouken@1950
   112
#undef SDL_X11_MODULE
slouken@1950
   113
#undef SDL_X11_SYM
slouken@1950
   114
slouken@1950
   115
slouken@1950
   116
static int x11_load_refcount = 0;
slouken@1950
   117
slouken@1950
   118
void
slouken@1950
   119
SDL_X11_UnloadSymbols(void)
slouken@1950
   120
{
slouken@1950
   121
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   122
    /* Don't actually unload if more than one module is using the libs... */
slouken@1950
   123
    if (x11_load_refcount > 0) {
slouken@1950
   124
        if (--x11_load_refcount == 0) {
slouken@1950
   125
            int i;
slouken@1950
   126
slouken@1950
   127
            /* set all the function pointers to NULL. */
slouken@1950
   128
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
slouken@1950
   129
#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
slouken@1950
   130
#include "SDL_x11sym.h"
slouken@1950
   131
#undef SDL_X11_MODULE
slouken@1950
   132
#undef SDL_X11_SYM
slouken@1950
   133
slouken@1950
   134
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   135
            pXCreateIC = NULL;
bob@2325
   136
            pXGetICValues = NULL;
slouken@1950
   137
#endif
slouken@1950
   138
slouken@1950
   139
            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   140
                if (x11libs[i].lib != NULL) {
slouken@1950
   141
                    SDL_UnloadObject(x11libs[i].lib);
slouken@1950
   142
                    x11libs[i].lib = NULL;
slouken@1950
   143
                }
slouken@1950
   144
            }
slouken@1950
   145
        }
slouken@1950
   146
    }
slouken@1950
   147
#endif
slouken@1950
   148
}
slouken@1950
   149
slouken@1950
   150
/* returns non-zero if all needed symbols were loaded. */
slouken@1950
   151
int
slouken@1950
   152
SDL_X11_LoadSymbols(void)
slouken@1950
   153
{
slouken@1950
   154
    int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
slouken@1950
   155
slouken@1950
   156
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   157
    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
slouken@1950
   158
    if (x11_load_refcount++ == 0) {
slouken@1950
   159
        int i;
slouken@1950
   160
        int *thismod = NULL;
slouken@1950
   161
        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   162
            if (x11libs[i].libname != NULL) {
slouken@1950
   163
                x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
slouken@1950
   164
            }
slouken@1950
   165
        }
slouken@1950
   166
#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
slouken@1950
   167
#define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
slouken@1950
   168
#include "SDL_x11sym.h"
slouken@1950
   169
#undef SDL_X11_MODULE
slouken@1950
   170
#undef SDL_X11_SYM
slouken@1950
   171
slouken@1950
   172
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   173
        X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8, (void **) &pXCreateIC);
bob@2325
   174
        X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8,
bob@2325
   175
                   (void **) &pXGetICValues);
slouken@1950
   176
#endif
slouken@1950
   177
slouken@2120
   178
        if (SDL_X11_HAVE_BASEXLIB) {
slouken@2120
   179
            /* all required symbols loaded. */
icculus@2118
   180
            SDL_ClearError();
icculus@2118
   181
        } else {
slouken@2120
   182
            /* in case something got loaded... */
slouken@2120
   183
            SDL_X11_UnloadSymbols();
slouken@1950
   184
            rc = 0;
slouken@1950
   185
        }
slouken@1950
   186
    }
slouken@1950
   187
#else
slouken@1950
   188
#ifdef X_HAVE_UTF8_STRING
slouken@1950
   189
    pXCreateIC = XCreateIC;
bob@2325
   190
    pXGetICValues = XGetICValues;
slouken@1950
   191
#endif
slouken@1950
   192
#endif
slouken@1950
   193
slouken@1950
   194
    return rc;
slouken@1950
   195
}
slouken@1950
   196
slouken@1950
   197
/* vi: set ts=4 sw=4 expandtab: */