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