cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_mirdyn.c (4893B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      4
      5  This software is provided 'as-is', without any express or implied
      6  warranty.  In no event will the authors be held liable for any damages
      7  arising from the use of this software.
      8
      9  Permission is granted to anyone to use this software for any purpose,
     10  including commercial applications, and to alter it and redistribute it
     11  freely, subject to the following restrictions:
     12
     13  1. The origin of this software must not be misrepresented; you must not
     14     claim that you wrote the original software. If you use this software
     15     in a product, an acknowledgment in the product documentation would be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21#include "../../SDL_internal.h"
     22
     23#if SDL_VIDEO_DRIVER_MIR
     24
     25#define DEBUG_DYNAMIC_MIR 0
     26
     27#include "SDL_mirdyn.h"
     28
     29#if DEBUG_DYNAMIC_MIR
     30#include "SDL_log.h"
     31#endif
     32
     33#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
     34
     35#include "SDL_name.h"
     36#include "SDL_loadso.h"
     37
     38typedef struct
     39{
     40    void *lib;
     41    const char *libname;
     42} mirdynlib;
     43
     44#ifndef SDL_VIDEO_DRIVER_MIR_DYNAMIC
     45#define SDL_VIDEO_DRIVER_MIR_DYNAMIC NULL
     46#endif
     47#ifndef SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON
     48#define SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON NULL
     49#endif
     50
     51static mirdynlib mirlibs[] = {
     52    {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC},
     53    {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON}
     54};
     55
     56static void *
     57MIR_GetSym(const char *fnname, int *pHasModule)
     58{
     59    int i;
     60    void *fn = NULL;
     61    for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
     62        if (mirlibs[i].lib != NULL) {
     63            fn = SDL_LoadFunction(mirlibs[i].lib, fnname);
     64            if (fn != NULL)
     65                break;
     66        }
     67    }
     68
     69#if DEBUG_DYNAMIC_MIR
     70    if (fn != NULL)
     71        SDL_Log("MIR: Found '%s' in %s (%p)\n", fnname, mirlibs[i].libname, fn);
     72    else
     73        SDL_Log("MIR: Symbol '%s' NOT FOUND!\n", fnname);
     74#endif
     75
     76    if (fn == NULL)
     77        *pHasModule = 0;  /* kill this module. */
     78
     79    return fn;
     80}
     81
     82#endif /* SDL_VIDEO_DRIVER_MIR_DYNAMIC */
     83
     84/* Define all the function pointers and wrappers... */
     85#define SDL_MIR_MODULE(modname) int SDL_MIR_HAVE_##modname = 0;
     86#define SDL_MIR_SYM(rc,fn,params) SDL_DYNMIRFN_##fn MIR_##fn = NULL;
     87#include "SDL_mirsym.h"
     88#undef SDL_MIR_MODULE
     89#undef SDL_MIR_SYM
     90
     91static int mir_load_refcount = 0;
     92
     93void
     94SDL_MIR_UnloadSymbols(void)
     95{
     96    /* Don't actually unload if more than one module is using the libs... */
     97    if (mir_load_refcount > 0) {
     98        if (--mir_load_refcount == 0) {
     99#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC            
    100            int i;
    101#endif
    102            
    103            /* set all the function pointers to NULL. */
    104#define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 0;
    105#define SDL_MIR_SYM(rc,fn,params) MIR_##fn = NULL;
    106#include "SDL_mirsym.h"
    107#undef SDL_MIR_MODULE
    108#undef SDL_MIR_SYM
    109
    110
    111#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
    112            for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
    113                if (mirlibs[i].lib != NULL) {
    114                    SDL_UnloadObject(mirlibs[i].lib);
    115                    mirlibs[i].lib = NULL;
    116                }
    117            }
    118#endif
    119        }
    120    }
    121}
    122
    123/* returns non-zero if all needed symbols were loaded. */
    124int
    125SDL_MIR_LoadSymbols(void)
    126{
    127    int rc = 1;                 /* always succeed if not using Dynamic MIR stuff. */
    128
    129    /* deal with multiple modules (dga, wayland, mir, etc) needing these symbols... */
    130    if (mir_load_refcount++ == 0) {
    131#ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
    132        int i;
    133        int *thismod = NULL;
    134        for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
    135            if (mirlibs[i].libname != NULL) {
    136                mirlibs[i].lib = SDL_LoadObject(mirlibs[i].libname);
    137            }
    138        }
    139
    140#define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
    141#define SDL_MIR_SYM(rc,fn,params)
    142#include "SDL_mirsym.h"
    143#undef SDL_MIR_MODULE
    144#undef SDL_MIR_SYM
    145
    146#define SDL_MIR_MODULE(modname) thismod = &SDL_MIR_HAVE_##modname;
    147#define SDL_MIR_SYM(rc,fn,params) MIR_##fn = (SDL_DYNMIRFN_##fn) MIR_GetSym(#fn,thismod);
    148#include "SDL_mirsym.h"
    149#undef SDL_MIR_MODULE
    150#undef SDL_MIR_SYM
    151
    152        if ((SDL_MIR_HAVE_MIR_CLIENT) && (SDL_MIR_HAVE_XKBCOMMON)) {
    153            /* all required symbols loaded. */
    154            SDL_ClearError();
    155        } else {
    156            /* in case something got loaded... */
    157            SDL_MIR_UnloadSymbols();
    158            rc = 0;
    159        }
    160
    161#else  /* no dynamic MIR */
    162
    163#define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
    164#define SDL_MIR_SYM(rc,fn,params) MIR_##fn = fn;
    165#include "SDL_mirsym.h"
    166#undef SDL_MIR_MODULE
    167#undef SDL_MIR_SYM
    168
    169#endif
    170    }
    171
    172    return rc;
    173}
    174
    175#endif /* SDL_VIDEO_DRIVER_MIR */
    176
    177/* vi: set ts=4 sw=4 expandtab: */