src/video/x11/SDL_x11dyn.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Mar 2011 13:21:57 -0800
changeset 5481 22dfc3958dc3
parent 5471 179adad3ae6f
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed so code will compile with SDL_config_minimal.h
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #if SDL_VIDEO_DRIVER_X11
    25 
    26 #define DEBUG_DYNAMIC_X11 0
    27 
    28 #include "SDL_x11dyn.h"
    29 
    30 #if DEBUG_DYNAMIC_X11
    31 #include <stdio.h>
    32 #endif
    33 
    34 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
    35 
    36 #include "SDL_name.h"
    37 #include "SDL_loadso.h"
    38 
    39 typedef struct
    40 {
    41     void *lib;
    42     const char *libname;
    43 } x11dynlib;
    44 
    45 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
    46 #define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
    47 #endif
    48 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
    49 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
    50 #endif
    51 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR
    52 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR NULL
    53 #endif
    54 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA
    55 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA NULL
    56 #endif
    57 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT
    58 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT NULL
    59 #endif
    60 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
    61 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
    62 #endif
    63 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS
    64 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS NULL
    65 #endif
    66 #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE
    67 #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE NULL
    68 #endif
    69 
    70 static x11dynlib x11libs[] = {
    71     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
    72     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
    73     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR},
    74     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA},
    75     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT},
    76     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
    77     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS},
    78     {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE}
    79 };
    80 
    81 static void
    82 X11_GetSym(const char *fnname, int *rc, void **fn)
    83 {
    84     int i;
    85     for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
    86         if (x11libs[i].lib != NULL) {
    87             *fn = SDL_LoadFunction(x11libs[i].lib, fnname);
    88             if (*fn != NULL)
    89                 break;
    90         }
    91     }
    92 
    93 #if DEBUG_DYNAMIC_X11
    94     if (*fn != NULL)
    95         printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname,
    96                *fn);
    97     else
    98         printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
    99 #endif
   100 
   101     if (*fn == NULL)
   102         *rc = 0;                /* kill this module. */
   103 }
   104 
   105 
   106 /* Define all the function pointers and wrappers... */
   107 #define SDL_X11_MODULE(modname)
   108 #define SDL_X11_SYM(rc,fn,params,args,ret) \
   109 	static rc (*p##fn) params = NULL; \
   110 	rc fn params { ret p##fn args ; }
   111 #include "SDL_x11sym.h"
   112 #undef SDL_X11_MODULE
   113 #undef SDL_X11_SYM
   114 #endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
   115 
   116 /* Annoying varargs entry point... */
   117 #ifdef X_HAVE_UTF8_STRING
   118 XIC(*pXCreateIC) (XIM,...) = NULL;
   119 char *(*pXGetICValues) (XIC, ...) = NULL;
   120 #endif
   121 
   122 /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
   123 #define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 1;
   124 #define SDL_X11_SYM(rc,fn,params,args,ret)
   125 #include "SDL_x11sym.h"
   126 #undef SDL_X11_MODULE
   127 #undef SDL_X11_SYM
   128 
   129 
   130 static int x11_load_refcount = 0;
   131 
   132 void
   133 SDL_X11_UnloadSymbols(void)
   134 {
   135 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   136     /* Don't actually unload if more than one module is using the libs... */
   137     if (x11_load_refcount > 0) {
   138         if (--x11_load_refcount == 0) {
   139             int i;
   140 
   141             /* set all the function pointers to NULL. */
   142 #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1;
   143 #define SDL_X11_SYM(rc,fn,params,args,ret) p##fn = NULL;
   144 #include "SDL_x11sym.h"
   145 #undef SDL_X11_MODULE
   146 #undef SDL_X11_SYM
   147 
   148 #ifdef X_HAVE_UTF8_STRING
   149             pXCreateIC = NULL;
   150             pXGetICValues = NULL;
   151 #endif
   152 
   153             for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   154                 if (x11libs[i].lib != NULL) {
   155                     SDL_UnloadObject(x11libs[i].lib);
   156                     x11libs[i].lib = NULL;
   157                 }
   158             }
   159         }
   160     }
   161 #endif
   162 }
   163 
   164 /* returns non-zero if all needed symbols were loaded. */
   165 int
   166 SDL_X11_LoadSymbols(void)
   167 {
   168     int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
   169 
   170 #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
   171     /* deal with multiple modules (dga, x11, etc) needing these symbols... */
   172     if (x11_load_refcount++ == 0) {
   173         int i;
   174         int *thismod = NULL;
   175         for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
   176             if (x11libs[i].libname != NULL) {
   177                 x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
   178             }
   179         }
   180 #define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
   181 #define SDL_X11_SYM(a,fn,x,y,z) X11_GetSym(#fn,thismod,(void**)&p##fn);
   182 #include "SDL_x11sym.h"
   183 #undef SDL_X11_MODULE
   184 #undef SDL_X11_SYM
   185 
   186 #ifdef X_HAVE_UTF8_STRING
   187         X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8, (void **) &pXCreateIC);
   188         X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8,
   189                    (void **) &pXGetICValues);
   190 #endif
   191 
   192         if (SDL_X11_HAVE_BASEXLIB) {
   193             /* all required symbols loaded. */
   194             SDL_ClearError();
   195         } else {
   196             /* in case something got loaded... */
   197             SDL_X11_UnloadSymbols();
   198             rc = 0;
   199         }
   200     }
   201 #else
   202 #ifdef X_HAVE_UTF8_STRING
   203     pXCreateIC = XCreateIC;
   204     pXGetICValues = XGetICValues;
   205 #endif
   206 #endif
   207 
   208     return rc;
   209 }
   210 
   211 #endif /* SDL_VIDEO_DRIVER_X11 */
   212 
   213 /* vi: set ts=4 sw=4 expandtab: */