src/video/x11/SDL_x11dyn.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5481 22dfc3958dc3
child 6026 a7142857e885
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 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_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_X11
    24 
    25 #define DEBUG_DYNAMIC_X11 0
    26 
    27 #include "SDL_x11dyn.h"
    28 
    29 #if DEBUG_DYNAMIC_X11
    30 #include <stdio.h>
    31 #endif
    32 
    33 #ifdef SDL_VIDEO_DRIVER_X11_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 } x11dynlib;
    43 
    44 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
    45 #define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
    46 #endif
    47 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
    48 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
    49 #endif
    50 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR
    51 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR NULL
    52 #endif
    53 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA
    54 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA NULL
    55 #endif
    56 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT
    57 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT NULL
    58 #endif
    59 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
    60 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
    61 #endif
    62 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS
    63 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS NULL
    64 #endif
    65 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE
    66 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE NULL
    67 #endif
    68 
    69 static x11dynlib x11libs[] = {
    70     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
    71     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
    72     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR},
    73     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA},
    74     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT},
    75     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
    76     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS},
    77     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE}
    78 };
    79 
    80 static void
    81 X11_GetSym(const char *fnname, int *rc, void **fn)
    82 {
    83     int i;
    84     for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
    85         if (x11libs[i].lib != NULL) {
    86             *fn = SDL_LoadFunction(x11libs[i].lib, fnname);
    87             if (*fn != NULL)
    88                 break;
    89         }
    90     }
    91 
    92 #if DEBUG_DYNAMIC_X11
    93     if (*fn != NULL)
    94         printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname,
    95                *fn);
    96     else
    97         printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    98 #endif
    99 
   100     if (*fn == NULL)
   101         *rc = 0;                /* kill this module. */
   102 }
   103 
   104 
   105 /* Define all the function pointers and wrappers... */
   106 #define SDL_X11_MODULE(modname)
   107 #define SDL_X11_SYM(rc,fn,params,args,ret) \
   108 	static rc (*p##fn) params = NULL; \
   109 	rc fn params { ret p##fn args ; }
   110 #include "SDL_x11sym.h"
   111 #undef SDL_X11_MODULE
   112 #undef SDL_X11_SYM
   113 #endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
   114 
   115 /* Annoying varargs entry point... */
   116 #ifdef X_HAVE_UTF8_STRING
   117 XIC(*pXCreateIC) (XIM,...) = NULL;
   118 char *(*pXGetICValues) (XIC, ...) = NULL;
   119 #endif
   120 
   121 /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
   122 #define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
   123 #define SDL_X11_SYM(rc,fn,params,args,ret)
   124 #include "SDL_x11sym.h"
   125 #undef SDL_X11_MODULE
   126 #undef SDL_X11_SYM
   127 
   128 
   129 static int x11_load_refcount = 0;
   130 
   131 void
   132 SDL_X11_UnloadSymbols(void)
   133 {
   134 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   135     /* Don't actually unload if more than one module is using the libs... */
   136     if (x11_load_refcount > 0) {
   137         if (--x11_load_refcount == 0) {
   138             int i;
   139 
   140             /* set all the function pointers to NULL. */
   141 #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
   142 #define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
   143 #include "SDL_x11sym.h"
   144 #undef SDL_X11_MODULE
   145 #undef SDL_X11_SYM
   146 
   147 #ifdef X_HAVE_UTF8_STRING
   148             pXCreateIC = NULL;
   149             pXGetICValues = NULL;
   150 #endif
   151 
   152             for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   153                 if (x11libs[i].lib != NULL) {
   154                     SDL_UnloadObject(x11libs[i].lib);
   155                     x11libs[i].lib = NULL;
   156                 }
   157             }
   158         }
   159     }
   160 #endif
   161 }
   162 
   163 /* returns non-zero if all needed symbols were loaded. */
   164 int
   165 SDL_X11_LoadSymbols(void)
   166 {
   167     int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
   168 
   169 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   170     /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   171     if (x11_load_refcount++ == 0) {
   172         int i;
   173         int *thismod = NULL;
   174         for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   175             if (x11libs[i].libname != NULL) {
   176                 x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
   177             }
   178         }
   179 #define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
   180 #define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
   181 #include "SDL_x11sym.h"
   182 #undef SDL_X11_MODULE
   183 #undef SDL_X11_SYM
   184 
   185 #ifdef X_HAVE_UTF8_STRING
   186         X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8, (void **) &pXCreateIC);
   187         X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8,
   188                    (void **) &pXGetICValues);
   189 #endif
   190 
   191         if (SDL_X11_HAVE_BASEXLIB) {
   192             /* all required symbols loaded. */
   193             SDL_ClearError();
   194         } else {
   195             /* in case something got loaded... */
   196             SDL_X11_UnloadSymbols();
   197             rc = 0;
   198         }
   199     }
   200 #else
   201 #ifdef X_HAVE_UTF8_STRING
   202     pXCreateIC = XCreateIC;
   203     pXGetICValues = XGetICValues;
   204 #endif
   205 #endif
   206 
   207     return rc;
   208 }
   209 
   210 #endif /* SDL_VIDEO_DRIVER_X11 */
   211 
   212 /* vi: set ts=4 sw=4 expandtab: */