src/video/x11/SDL_x11dyn.c
changeset 1950 a344e42bce3b
child 2118 238db749017a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/x11/SDL_x11dyn.c	Wed Jul 26 06:34:54 2006 +0000
     1.3 @@ -0,0 +1,184 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2004 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#define DEBUG_DYNAMIC_X11 0
    1.28 +
    1.29 +#include "SDL_x11dyn.h"
    1.30 +
    1.31 +#if DEBUG_DYNAMIC_X11
    1.32 +#include <stdio.h>
    1.33 +#endif
    1.34 +
    1.35 +#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
    1.36 +
    1.37 +#include "SDL_name.h"
    1.38 +#include "SDL_loadso.h"
    1.39 +
    1.40 +typedef struct
    1.41 +{
    1.42 +    void *lib;
    1.43 +    const char *libname;
    1.44 +} x11dynlib;
    1.45 +
    1.46 +#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
    1.47 +#define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
    1.48 +#endif
    1.49 +#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
    1.50 +#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
    1.51 +#endif
    1.52 +#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER
    1.53 +#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER NULL
    1.54 +#endif
    1.55 +#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
    1.56 +#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
    1.57 +#endif
    1.58 +
    1.59 +static x11dynlib x11libs[] = {
    1.60 +    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
    1.61 +    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
    1.62 +    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER},
    1.63 +    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
    1.64 +};
    1.65 +
    1.66 +static void
    1.67 +X11_GetSym(const char *fnname, int *rc, void **fn)
    1.68 +{
    1.69 +    int i;
    1.70 +    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
    1.71 +        if (x11libs[i].lib != NULL) {
    1.72 +            *fn = SDL_LoadFunction(x11libs[i].lib, fnname);
    1.73 +            if (*fn != NULL)
    1.74 +                break;
    1.75 +        }
    1.76 +    }
    1.77 +
    1.78 +#if DEBUG_DYNAMIC_X11
    1.79 +    if (*fn != NULL)
    1.80 +        printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname,
    1.81 +               *fn);
    1.82 +    else
    1.83 +        printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    1.84 +#endif
    1.85 +
    1.86 +    if (*fn == NULL)
    1.87 +        *rc = 0;                /* kill this module. */
    1.88 +}
    1.89 +
    1.90 +
    1.91 +/* Define all the function pointers and wrappers... */
    1.92 +#define SDL_X11_MODULE(modname)
    1.93 +#define SDL_X11_SYM(rc,fn,params,args,ret) \
    1.94 +	static rc (*p##fn) params = NULL; \
    1.95 +	rc fn params { ret p##fn args ; }
    1.96 +#include "SDL_x11sym.h"
    1.97 +#undef SDL_X11_MODULE
    1.98 +#undef SDL_X11_SYM
    1.99 +#endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
   1.100 +
   1.101 +/* Annoying varargs entry point... */
   1.102 +#ifdef X_HAVE_UTF8_STRING
   1.103 +XIC(*pXCreateIC) (XIM,...) = NULL;
   1.104 +#endif
   1.105 +
   1.106 +/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
   1.107 +#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
   1.108 +#define SDL_X11_SYM(rc,fn,params,args,ret)
   1.109 +#include "SDL_x11sym.h"
   1.110 +#undef SDL_X11_MODULE
   1.111 +#undef SDL_X11_SYM
   1.112 +
   1.113 +
   1.114 +static int x11_load_refcount = 0;
   1.115 +
   1.116 +void
   1.117 +SDL_X11_UnloadSymbols(void)
   1.118 +{
   1.119 +#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   1.120 +    /* Don't actually unload if more than one module is using the libs... */
   1.121 +    if (x11_load_refcount > 0) {
   1.122 +        if (--x11_load_refcount == 0) {
   1.123 +            int i;
   1.124 +
   1.125 +            /* set all the function pointers to NULL. */
   1.126 +#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
   1.127 +#define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
   1.128 +#include "SDL_x11sym.h"
   1.129 +#undef SDL_X11_MODULE
   1.130 +#undef SDL_X11_SYM
   1.131 +
   1.132 +#ifdef X_HAVE_UTF8_STRING
   1.133 +            pXCreateIC = NULL;
   1.134 +#endif
   1.135 +
   1.136 +            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   1.137 +                if (x11libs[i].lib != NULL) {
   1.138 +                    SDL_UnloadObject(x11libs[i].lib);
   1.139 +                    x11libs[i].lib = NULL;
   1.140 +                }
   1.141 +            }
   1.142 +        }
   1.143 +    }
   1.144 +#endif
   1.145 +}
   1.146 +
   1.147 +/* returns non-zero if all needed symbols were loaded. */
   1.148 +int
   1.149 +SDL_X11_LoadSymbols(void)
   1.150 +{
   1.151 +    int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
   1.152 +
   1.153 +#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   1.154 +    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   1.155 +    if (x11_load_refcount++ == 0) {
   1.156 +        int i;
   1.157 +        int *thismod = NULL;
   1.158 +        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   1.159 +            if (x11libs[i].libname != NULL) {
   1.160 +                x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
   1.161 +            }
   1.162 +        }
   1.163 +#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
   1.164 +#define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
   1.165 +#include "SDL_x11sym.h"
   1.166 +#undef SDL_X11_MODULE
   1.167 +#undef SDL_X11_SYM
   1.168 +
   1.169 +#ifdef X_HAVE_UTF8_STRING
   1.170 +        X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8, (void **) &pXCreateIC);
   1.171 +#endif
   1.172 +
   1.173 +        if (!SDL_X11_HAVE_BASEXLIB) {   /* some required symbol didn't load. */
   1.174 +            SDL_X11_UnloadSymbols();    /* in case something got loaded... */
   1.175 +            rc = 0;
   1.176 +        }
   1.177 +    }
   1.178 +#else
   1.179 +#ifdef X_HAVE_UTF8_STRING
   1.180 +    pXCreateIC = XCreateIC;
   1.181 +#endif
   1.182 +#endif
   1.183 +
   1.184 +    return rc;
   1.185 +}
   1.186 +
   1.187 +/* vi: set ts=4 sw=4 expandtab: */