| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 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 | #ifdef 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 | typedef struct |
| 36 | { |
| 37 | SDL_SharedObject *lib; |
| 38 | const char *libname; |
| 39 | } x11dynlib; |
| 40 | |
| 41 | #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT |
| 42 | #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL |
| 43 | #endif |
| 44 | #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR |
| 45 | #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR NULL |
| 46 | #endif |
| 47 | #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 |
| 48 | #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 NULL |
| 49 | #endif |
| 50 | #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XFIXES |
| 51 | #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XFIXES NULL |
| 52 | #endif |
| 53 | #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR |
| 54 | #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL |
| 55 | #endif |
| 56 | #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS |
| 57 | #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS NULL |
| 58 | #endif |
| 59 | #ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XTEST |
| 60 | #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XTEST NULL |
| 61 | #endif |
| 62 | |
| 63 | static x11dynlib x11libs[] = { |
| 64 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC }, |
| 65 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT }, |
| 66 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR }, |
| 67 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 }, |
| 68 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XFIXES }, |
| 69 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR }, |
| 70 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS }, |
| 71 | { NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XTEST } |
| 72 | }; |
| 73 | |
| 74 | static void *X11_GetSym(const char *fnname, int *pHasModule) |
| 75 | { |
| 76 | int i; |
| 77 | void *fn = NULL; |
| 78 | for (i = 0; i < SDL_arraysize(x11libs); i++) { |
| 79 | if (x11libs[i].lib) { |
| 80 | fn = SDL_LoadFunction(x11libs[i].lib, fnname); |
| 81 | if (fn) { |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #if DEBUG_DYNAMIC_X11 |
| 88 | if (fn) |
| 89 | printf("X11: Found '%s' in %s (%p)\n" , fnname, x11libs[i].libname, fn); |
| 90 | else |
| 91 | printf("X11: Symbol '%s' NOT FOUND!\n" , fnname); |
| 92 | #endif |
| 93 | |
| 94 | if (!fn) { |
| 95 | *pHasModule = 0; // kill this module. |
| 96 | } |
| 97 | |
| 98 | return fn; |
| 99 | } |
| 100 | |
| 101 | #endif // SDL_VIDEO_DRIVER_X11_DYNAMIC |
| 102 | |
| 103 | // Define all the function pointers and wrappers... |
| 104 | #define SDL_X11_SYM(rc, fn, params, args, ret) SDL_DYNX11FN_##fn X11_##fn = NULL; |
| 105 | #include "SDL_x11sym.h" |
| 106 | |
| 107 | // Annoying varargs entry point... |
| 108 | #ifdef X_HAVE_UTF8_STRING |
| 109 | SDL_DYNX11FN_XCreateIC X11_XCreateIC = NULL; |
| 110 | SDL_DYNX11FN_XGetICValues X11_XGetICValues = NULL; |
| 111 | SDL_DYNX11FN_XSetICValues X11_XSetICValues = NULL; |
| 112 | SDL_DYNX11FN_XVaCreateNestedList X11_XVaCreateNestedList = NULL; |
| 113 | #endif |
| 114 | |
| 115 | /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */ |
| 116 | #define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 0; |
| 117 | #include "SDL_x11sym.h" |
| 118 | |
| 119 | static int x11_load_refcount = 0; |
| 120 | |
| 121 | void SDL_X11_UnloadSymbols(void) |
| 122 | { |
| 123 | // Don't actually unload if more than one module is using the libs... |
| 124 | if (x11_load_refcount > 0) { |
| 125 | if (--x11_load_refcount == 0) { |
| 126 | #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC |
| 127 | int i; |
| 128 | #endif |
| 129 | |
| 130 | // set all the function pointers to NULL. |
| 131 | #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 0; |
| 132 | #define SDL_X11_SYM(rc, fn, params, args, ret) X11_##fn = NULL; |
| 133 | #include "SDL_x11sym.h" |
| 134 | |
| 135 | #ifdef X_HAVE_UTF8_STRING |
| 136 | X11_XCreateIC = NULL; |
| 137 | X11_XGetICValues = NULL; |
| 138 | X11_XSetICValues = NULL; |
| 139 | X11_XVaCreateNestedList = NULL; |
| 140 | #endif |
| 141 | |
| 142 | #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC |
| 143 | for (i = 0; i < SDL_arraysize(x11libs); i++) { |
| 144 | if (x11libs[i].lib) { |
| 145 | SDL_UnloadObject(x11libs[i].lib); |
| 146 | x11libs[i].lib = NULL; |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // returns non-zero if all needed symbols were loaded. |
| 155 | bool SDL_X11_LoadSymbols(void) |
| 156 | { |
| 157 | bool result = true; // always succeed if not using Dynamic X11 stuff. |
| 158 | |
| 159 | // deal with multiple modules (dga, x11, etc) needing these symbols... |
| 160 | if (x11_load_refcount++ == 0) { |
| 161 | #ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC |
| 162 | int i; |
| 163 | int *thismod = NULL; |
| 164 | for (i = 0; i < SDL_arraysize(x11libs); i++) { |
| 165 | if (x11libs[i].libname) { |
| 166 | x11libs[i].lib = SDL_LoadObject(x11libs[i].libname); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1; // default yes |
| 171 | #include "SDL_x11sym.h" |
| 172 | |
| 173 | #define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname; |
| 174 | #define SDL_X11_SYM(a, fn, x, y, z) X11_##fn = (SDL_DYNX11FN_##fn)X11_GetSym(#fn, thismod); |
| 175 | #include "SDL_x11sym.h" |
| 176 | |
| 177 | #ifdef X_HAVE_UTF8_STRING |
| 178 | X11_XCreateIC = (SDL_DYNX11FN_XCreateIC) |
| 179 | X11_GetSym("XCreateIC" , &SDL_X11_HAVE_UTF8); |
| 180 | X11_XGetICValues = (SDL_DYNX11FN_XGetICValues) |
| 181 | X11_GetSym("XGetICValues" , &SDL_X11_HAVE_UTF8); |
| 182 | X11_XSetICValues = (SDL_DYNX11FN_XSetICValues) |
| 183 | X11_GetSym("XSetICValues" , &SDL_X11_HAVE_UTF8); |
| 184 | X11_XVaCreateNestedList = (SDL_DYNX11FN_XVaCreateNestedList) |
| 185 | X11_GetSym("XVaCreateNestedList" , &SDL_X11_HAVE_UTF8); |
| 186 | #endif |
| 187 | |
| 188 | if (SDL_X11_HAVE_BASEXLIB) { |
| 189 | // all required symbols loaded. |
| 190 | SDL_ClearError(); |
| 191 | } else { |
| 192 | // in case something got loaded... |
| 193 | SDL_X11_UnloadSymbols(); |
| 194 | result = false; |
| 195 | } |
| 196 | |
| 197 | #else // no dynamic X11 |
| 198 | |
| 199 | #define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1; // default yes |
| 200 | #define SDL_X11_SYM(a, fn, x, y, z) X11_##fn = (SDL_DYNX11FN_##fn)fn; |
| 201 | #include "SDL_x11sym.h" |
| 202 | |
| 203 | #ifdef X_HAVE_UTF8_STRING |
| 204 | X11_XCreateIC = XCreateIC; |
| 205 | X11_XGetICValues = XGetICValues; |
| 206 | X11_XSetICValues = XSetICValues; |
| 207 | X11_XVaCreateNestedList = XVaCreateNestedList; |
| 208 | #endif |
| 209 | #endif |
| 210 | } |
| 211 | |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | #endif // SDL_VIDEO_DRIVER_X11 |
| 216 | |