src/video/mir/SDL_mirdyn.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 18 Feb 2016 23:27:58 -0500
changeset 10079 6eaf52c8d398
parent 9998 f67cf37e9cd4
child 10089 25fda20d0173
permissions -rw-r--r--
x11/wayland/mir: Make the dynamic loading macro salsa a little less messy.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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 
    38 typedef 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 
    51 static mirdynlib mirlibs[] = {
    52     {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC},
    53     {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON}
    54 };
    55 
    56 static void *
    57 MIR_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 
    89 static int mir_load_refcount = 0;
    90 
    91 void
    92 SDL_MIR_UnloadSymbols(void)
    93 {
    94     /* Don't actually unload if more than one module is using the libs... */
    95     if (mir_load_refcount > 0) {
    96         if (--mir_load_refcount == 0) {
    97 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC            
    98             int i;
    99 #endif
   100             
   101             /* set all the function pointers to NULL. */
   102 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 0;
   103 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = NULL;
   104 #include "SDL_mirsym.h"
   105 
   106 
   107 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
   108             for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
   109                 if (mirlibs[i].lib != NULL) {
   110                     SDL_UnloadObject(mirlibs[i].lib);
   111                     mirlibs[i].lib = NULL;
   112                 }
   113             }
   114 #endif
   115         }
   116     }
   117 }
   118 
   119 /* returns non-zero if all needed symbols were loaded. */
   120 int
   121 SDL_MIR_LoadSymbols(void)
   122 {
   123     int rc = 1;                 /* always succeed if not using Dynamic MIR stuff. */
   124 
   125     /* deal with multiple modules (dga, wayland, mir, etc) needing these symbols... */
   126     if (mir_load_refcount++ == 0) {
   127 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
   128         int i;
   129         int *thismod = NULL;
   130         for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
   131             if (mirlibs[i].libname != NULL) {
   132                 mirlibs[i].lib = SDL_LoadObject(mirlibs[i].libname);
   133             }
   134         }
   135 
   136 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
   137 #include "SDL_mirsym.h"
   138 
   139 #define SDL_MIR_MODULE(modname) thismod = &SDL_MIR_HAVE_##modname;
   140 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = (SDL_DYNMIRFN_##fn) MIR_GetSym(#fn,thismod);
   141 #include "SDL_mirsym.h"
   142 
   143         if ((SDL_MIR_HAVE_MIR_CLIENT) && (SDL_MIR_HAVE_XKBCOMMON)) {
   144             /* all required symbols loaded. */
   145             SDL_ClearError();
   146         } else {
   147             /* in case something got loaded... */
   148             SDL_MIR_UnloadSymbols();
   149             rc = 0;
   150         }
   151 
   152 #else  /* no dynamic MIR */
   153 
   154 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
   155 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = fn;
   156 #include "SDL_mirsym.h"
   157 
   158 #endif
   159     }
   160 
   161     return rc;
   162 }
   163 
   164 #endif /* SDL_VIDEO_DRIVER_MIR */
   165 
   166 /* vi: set ts=4 sw=4 expandtab: */