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