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