src/video/kmsdrm/SDL_kmsdrmdyn.c
changeset 11175 cbc6a8a5b701
child 11342 0b924e560249
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/kmsdrm/SDL_kmsdrmdyn.c	Wed Aug 02 10:22:48 2017 -0700
     1.3 @@ -0,0 +1,170 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2016 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_KMSDRM
    1.27 +
    1.28 +#define DEBUG_DYNAMIC_KMSDRM 0
    1.29 +
    1.30 +#include "SDL_kmsdrmdyn.h"
    1.31 +
    1.32 +#if DEBUG_DYNAMIC_KMSDRM
    1.33 +#include "SDL_log.h"
    1.34 +#endif
    1.35 +
    1.36 +#ifdef SDL_VIDEO_DRIVER_KMSDRM_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 +} kmsdrmdynlib;
    1.46 +
    1.47 +#ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
    1.48 +#define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC NULL
    1.49 +#endif
    1.50 +#ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM
    1.51 +#define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM NULL
    1.52 +#endif
    1.53 +
    1.54 +static kmsdrmdynlib kmsdrmlibs[] = {
    1.55 +    {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC},
    1.56 +    {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM}
    1.57 +};
    1.58 +
    1.59 +static void *
    1.60 +KMSDRM_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(kmsdrmlibs); i++) {
    1.65 +        if (kmsdrmlibs[i].lib != NULL) {
    1.66 +            fn = SDL_LoadFunction(kmsdrmlibs[i].lib, fnname);
    1.67 +            if (fn != NULL)
    1.68 +                break;
    1.69 +        }
    1.70 +    }
    1.71 +
    1.72 +#if DEBUG_DYNAMIC_KMSDRM
    1.73 +    if (fn != NULL)
    1.74 +        SDL_Log("KMSDRM: Found '%s' in %s (%p)\n", fnname, kmsdrmlibs[i].libname, fn);
    1.75 +    else
    1.76 +        SDL_Log("KMSDRM: 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_KMSDRM_DYNAMIC */
    1.86 +
    1.87 +/* Define all the function pointers and wrappers... */
    1.88 +#define SDL_KMSDRM_MODULE(modname) int SDL_KMSDRM_HAVE_##modname = 0;
    1.89 +#define SDL_KMSDRM_SYM(rc,fn,params) SDL_DYNKMSDRMFN_##fn KMSDRM_##fn = NULL;
    1.90 +#define SDL_KMSDRM_SYM_CONST(type,name) SDL_DYNKMSDRMCONST_##name KMSDRM_##name = NULL;
    1.91 +#include "SDL_kmsdrmsym.h"
    1.92 +
    1.93 +static int kmsdrm_load_refcount = 0;
    1.94 +
    1.95 +void
    1.96 +SDL_KMSDRM_UnloadSymbols(void)
    1.97 +{
    1.98 +    /* Don't actually unload if more than one module is using the libs... */
    1.99 +    if (kmsdrm_load_refcount > 0) {
   1.100 +        if (--kmsdrm_load_refcount == 0) {
   1.101 +#ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
   1.102 +            int i;
   1.103 +#endif
   1.104 +
   1.105 +            /* set all the function pointers to NULL. */
   1.106 +#define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 0;
   1.107 +#define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = NULL;
   1.108 +#define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = NULL;
   1.109 +#include "SDL_kmsdrmsym.h"
   1.110 +
   1.111 +
   1.112 +#ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
   1.113 +            for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
   1.114 +                if (kmsdrmlibs[i].lib != NULL) {
   1.115 +                    SDL_UnloadObject(kmsdrmlibs[i].lib);
   1.116 +                    kmsdrmlibs[i].lib = NULL;
   1.117 +                }
   1.118 +            }
   1.119 +#endif
   1.120 +        }
   1.121 +    }
   1.122 +}
   1.123 +
   1.124 +/* returns non-zero if all needed symbols were loaded. */
   1.125 +int
   1.126 +SDL_KMSDRM_LoadSymbols(void)
   1.127 +{
   1.128 +    int rc = 1;                 /* always succeed if not using Dynamic KMSDRM stuff. */
   1.129 +
   1.130 +    /* deal with multiple modules needing these symbols... */
   1.131 +    if (kmsdrm_load_refcount++ == 0) {
   1.132 +#ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
   1.133 +        int i;
   1.134 +        int *thismod = NULL;
   1.135 +        for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
   1.136 +            if (kmsdrmlibs[i].libname != NULL) {
   1.137 +                kmsdrmlibs[i].lib = SDL_LoadObject(kmsdrmlibs[i].libname);
   1.138 +            }
   1.139 +        }
   1.140 +
   1.141 +#define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
   1.142 +#include "SDL_kmsdrmsym.h"
   1.143 +
   1.144 +#define SDL_KMSDRM_MODULE(modname) thismod = &SDL_KMSDRM_HAVE_##modname;
   1.145 +#define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = (SDL_DYNKMSDRMFN_##fn) KMSDRM_GetSym(#fn,thismod);
   1.146 +#define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = *(SDL_DYNKMSDRMCONST_##name*) KMSDRM_GetSym(#name,thismod);
   1.147 +#include "SDL_kmsdrmsym.h"
   1.148 +
   1.149 +        if ((SDL_KMSDRM_HAVE_LIBDRM) && (SDL_KMSDRM_HAVE_GBM)) {
   1.150 +            /* all required symbols loaded. */
   1.151 +            SDL_ClearError();
   1.152 +        } else {
   1.153 +            /* in case something got loaded... */
   1.154 +            SDL_KMSDRM_UnloadSymbols();
   1.155 +            rc = 0;
   1.156 +        }
   1.157 +
   1.158 +#else  /* no dynamic KMSDRM */
   1.159 +
   1.160 +#define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
   1.161 +#define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = fn;
   1.162 +#define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = name;
   1.163 +#include "SDL_kmsdrmsym.h"
   1.164 +
   1.165 +#endif
   1.166 +    }
   1.167 +
   1.168 +    return rc;
   1.169 +}
   1.170 +
   1.171 +#endif /* SDL_VIDEO_DRIVER_KMSDRM */
   1.172 +
   1.173 +/* vi: set ts=4 sw=4 expandtab: */