src/video/mir/SDL_mirdyn.c
changeset 8159 427e8fdf6f69
child 9619 b94b6d0bff0f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/mir/SDL_mirdyn.c	Mon Feb 03 11:52:54 2014 -0500
     1.3 @@ -0,0 +1,177 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#if SDL_VIDEO_DRIVER_MIR
    1.27 +
    1.28 +#define DEBUG_DYNAMIC_MIR 0
    1.29 +
    1.30 +#include "SDL_mirdyn.h"
    1.31 +
    1.32 +#if DEBUG_DYNAMIC_MIR
    1.33 +#include "SDL_log.h"
    1.34 +#endif
    1.35 +
    1.36 +#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
    1.37 +
    1.38 +#include "SDL_name.h"
    1.39 +#include "SDL_loadso.h"
    1.40 +
    1.41 +typedef struct
    1.42 +{
    1.43 +    void *lib;
    1.44 +    const char *libname;
    1.45 +} mirdynlib;
    1.46 +
    1.47 +#ifndef SDL_VIDEO_DRIVER_MIR_DYNAMIC
    1.48 +#define SDL_VIDEO_DRIVER_MIR_DYNAMIC NULL
    1.49 +#endif
    1.50 +#ifndef SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON
    1.51 +#define SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON NULL
    1.52 +#endif
    1.53 +
    1.54 +static mirdynlib mirlibs[] = {
    1.55 +    {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC},
    1.56 +    {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON}
    1.57 +};
    1.58 +
    1.59 +static void *
    1.60 +MIR_GetSym(const char *fnname, int *pHasModule)
    1.61 +{
    1.62 +    int i;
    1.63 +    void *fn = NULL;
    1.64 +    for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
    1.65 +        if (mirlibs[i].lib != NULL) {
    1.66 +            fn = SDL_LoadFunction(mirlibs[i].lib, fnname);
    1.67 +            if (fn != NULL)
    1.68 +                break;
    1.69 +        }
    1.70 +    }
    1.71 +
    1.72 +#if DEBUG_DYNAMIC_MIR
    1.73 +    if (fn != NULL)
    1.74 +        SDL_Log("MIR: Found '%s' in %s (%p)\n", fnname, mirlibs[i].libname, fn);
    1.75 +    else
    1.76 +        SDL_Log("MIR: Symbol '%s' NOT FOUND!\n", fnname);
    1.77 +#endif
    1.78 +
    1.79 +    if (fn == NULL)
    1.80 +        *pHasModule = 0;  /* kill this module. */
    1.81 +
    1.82 +    return fn;
    1.83 +}
    1.84 +
    1.85 +#endif /* SDL_VIDEO_DRIVER_MIR_DYNAMIC */
    1.86 +
    1.87 +/* Define all the function pointers and wrappers... */
    1.88 +#define SDL_MIR_MODULE(modname) int SDL_MIR_HAVE_##modname = 0;
    1.89 +#define SDL_MIR_SYM(rc,fn,params) SDL_DYNMIRFN_##fn MIR_##fn = NULL;
    1.90 +#include "SDL_mirsym.h"
    1.91 +#undef SDL_MIR_MODULE
    1.92 +#undef SDL_MIR_SYM
    1.93 +
    1.94 +static int mir_load_refcount = 0;
    1.95 +
    1.96 +void
    1.97 +SDL_MIR_UnloadSymbols(void)
    1.98 +{
    1.99 +    /* Don't actually unload if more than one module is using the libs... */
   1.100 +    if (mir_load_refcount > 0) {
   1.101 +        if (--mir_load_refcount == 0) {
   1.102 +#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC            
   1.103 +            int i;
   1.104 +#endif
   1.105 +            
   1.106 +            /* set all the function pointers to NULL. */
   1.107 +#define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 0;
   1.108 +#define SDL_MIR_SYM(rc,fn,params) MIR_##fn = NULL;
   1.109 +#include "SDL_mirsym.h"
   1.110 +#undef SDL_MIR_MODULE
   1.111 +#undef SDL_MIR_SYM
   1.112 +
   1.113 +
   1.114 +#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
   1.115 +            for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
   1.116 +                if (mirlibs[i].lib != NULL) {
   1.117 +                    SDL_UnloadObject(mirlibs[i].lib);
   1.118 +                    mirlibs[i].lib = NULL;
   1.119 +                }
   1.120 +            }
   1.121 +#endif
   1.122 +        }
   1.123 +    }
   1.124 +}
   1.125 +
   1.126 +/* returns non-zero if all needed symbols were loaded. */
   1.127 +int
   1.128 +SDL_MIR_LoadSymbols(void)
   1.129 +{
   1.130 +    int rc = 1;                 /* always succeed if not using Dynamic MIR stuff. */
   1.131 +
   1.132 +    /* deal with multiple modules (dga, wayland, mir, etc) needing these symbols... */
   1.133 +    if (mir_load_refcount++ == 0) {
   1.134 +#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
   1.135 +        int i;
   1.136 +        int *thismod = NULL;
   1.137 +        for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
   1.138 +            if (mirlibs[i].libname != NULL) {
   1.139 +                mirlibs[i].lib = SDL_LoadObject(mirlibs[i].libname);
   1.140 +            }
   1.141 +        }
   1.142 +
   1.143 +#define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
   1.144 +#define SDL_MIR_SYM(rc,fn,params)
   1.145 +#include "SDL_mirsym.h"
   1.146 +#undef SDL_MIR_MODULE
   1.147 +#undef SDL_MIR_SYM
   1.148 +
   1.149 +#define SDL_MIR_MODULE(modname) thismod = &SDL_MIR_HAVE_##modname;
   1.150 +#define SDL_MIR_SYM(rc,fn,params) MIR_##fn = (SDL_DYNMIRFN_##fn) MIR_GetSym(#fn,thismod);
   1.151 +#include "SDL_mirsym.h"
   1.152 +#undef SDL_MIR_MODULE
   1.153 +#undef SDL_MIR_SYM
   1.154 +
   1.155 +        if ((SDL_MIR_HAVE_MIR_CLIENT) && (SDL_MIR_HAVE_XKBCOMMON)) {
   1.156 +            /* all required symbols loaded. */
   1.157 +            SDL_ClearError();
   1.158 +        } else {
   1.159 +            /* in case something got loaded... */
   1.160 +            SDL_MIR_UnloadSymbols();
   1.161 +            rc = 0;
   1.162 +        }
   1.163 +
   1.164 +#else  /* no dynamic MIR */
   1.165 +
   1.166 +#define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
   1.167 +#define SDL_MIR_SYM(rc,fn,params) MIR_##fn = fn;
   1.168 +#include "SDL_mirsym.h"
   1.169 +#undef SDL_MIR_MODULE
   1.170 +#undef SDL_MIR_SYM
   1.171 +
   1.172 +#endif
   1.173 +    }
   1.174 +
   1.175 +    return rc;
   1.176 +}
   1.177 +
   1.178 +#endif /* SDL_VIDEO_DRIVER_MIR */
   1.179 +
   1.180 +/* vi: set ts=4 sw=4 expandtab: */