| 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 | |
| 22 | #include "SDL_internal.h" |
| 23 | |
| 24 | #ifdef SDL_VIDEO_DRIVER_KMSDRM |
| 25 | |
| 26 | #define DEBUG_DYNAMIC_KMSDRM 0 |
| 27 | |
| 28 | #include "SDL_kmsdrmdyn.h" |
| 29 | |
| 30 | #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC |
| 31 | |
| 32 | typedef struct |
| 33 | { |
| 34 | void *lib; |
| 35 | const char *libname; |
| 36 | } kmsdrmdynlib; |
| 37 | |
| 38 | #ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM |
| 39 | #define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM NULL |
| 40 | #endif |
| 41 | |
| 42 | static kmsdrmdynlib kmsdrmlibs[] = { |
| 43 | { NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM }, |
| 44 | { NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC } |
| 45 | }; |
| 46 | |
| 47 | static void *KMSDRM_GetSym(const char *fnname, int *pHasModule, bool required) |
| 48 | { |
| 49 | int i; |
| 50 | void *fn = NULL; |
| 51 | for (i = 0; i < SDL_arraysize(kmsdrmlibs); i++) { |
| 52 | if (kmsdrmlibs[i].lib) { |
| 53 | fn = SDL_LoadFunction(kmsdrmlibs[i].lib, fnname); |
| 54 | if (fn) { |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #if DEBUG_DYNAMIC_KMSDRM |
| 61 | if (fn) |
| 62 | SDL_Log("KMSDRM: Found '%s' in %s (%p)" , fnname, kmsdrmlibs[i].libname, fn); |
| 63 | else |
| 64 | SDL_Log("KMSDRM: Symbol '%s' NOT FOUND!" , fnname); |
| 65 | #endif |
| 66 | |
| 67 | if (!fn && required) { |
| 68 | *pHasModule = 0; // kill this module. |
| 69 | } |
| 70 | |
| 71 | return fn; |
| 72 | } |
| 73 | |
| 74 | #endif // SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC |
| 75 | |
| 76 | // Define all the function pointers and wrappers... |
| 77 | #define SDL_KMSDRM_MODULE(modname) int SDL_KMSDRM_HAVE_##modname = 0; |
| 78 | #define SDL_KMSDRM_SYM(rc, fn, params) SDL_DYNKMSDRMFN_##fn KMSDRM_##fn = NULL; |
| 79 | #define SDL_KMSDRM_SYM_CONST(type, name) SDL_DYNKMSDRMCONST_##name KMSDRM_##name = NULL; |
| 80 | #define SDL_KMSDRM_SYM_OPT(rc, fn, params) SDL_DYNKMSDRMFN_##fn KMSDRM_##fn = NULL; |
| 81 | #include "SDL_kmsdrmsym.h" |
| 82 | |
| 83 | static int kmsdrm_load_refcount = 0; |
| 84 | |
| 85 | void SDL_KMSDRM_UnloadSymbols(void) |
| 86 | { |
| 87 | // Don't actually unload if more than one module is using the libs... |
| 88 | if (kmsdrm_load_refcount > 0) { |
| 89 | if (--kmsdrm_load_refcount == 0) { |
| 90 | #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC |
| 91 | int i; |
| 92 | #endif |
| 93 | |
| 94 | // set all the function pointers to NULL. |
| 95 | #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 0; |
| 96 | #define SDL_KMSDRM_SYM(rc, fn, params) KMSDRM_##fn = NULL; |
| 97 | #define SDL_KMSDRM_SYM_CONST(type, name) KMSDRM_##name = NULL; |
| 98 | #define SDL_KMSDRM_SYM_OPT(rc, fn, params) KMSDRM_##fn = NULL; |
| 99 | #include "SDL_kmsdrmsym.h" |
| 100 | |
| 101 | #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC |
| 102 | for (i = 0; i < SDL_arraysize(kmsdrmlibs); i++) { |
| 103 | if (kmsdrmlibs[i].lib) { |
| 104 | SDL_UnloadObject(kmsdrmlibs[i].lib); |
| 105 | kmsdrmlibs[i].lib = NULL; |
| 106 | } |
| 107 | } |
| 108 | #endif |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // returns non-zero if all needed symbols were loaded. |
| 114 | bool SDL_KMSDRM_LoadSymbols(void) |
| 115 | { |
| 116 | bool result = true; // always succeed if not using Dynamic KMSDRM stuff. |
| 117 | |
| 118 | // deal with multiple modules needing these symbols... |
| 119 | if (kmsdrm_load_refcount++ == 0) { |
| 120 | #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC |
| 121 | int i; |
| 122 | int *thismod = NULL; |
| 123 | for (i = 0; i < SDL_arraysize(kmsdrmlibs); i++) { |
| 124 | if (kmsdrmlibs[i].libname) { |
| 125 | kmsdrmlibs[i].lib = SDL_LoadObject(kmsdrmlibs[i].libname); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; // default yes |
| 130 | #include "SDL_kmsdrmsym.h" |
| 131 | |
| 132 | #define SDL_KMSDRM_MODULE(modname) thismod = &SDL_KMSDRM_HAVE_##modname; |
| 133 | #define SDL_KMSDRM_SYM(rc, fn, params) KMSDRM_##fn = (SDL_DYNKMSDRMFN_##fn)KMSDRM_GetSym(#fn, thismod, true); |
| 134 | #define SDL_KMSDRM_SYM_CONST(type, name) KMSDRM_##name = *(SDL_DYNKMSDRMCONST_##name *)KMSDRM_GetSym(#name, thismod, true); |
| 135 | #define SDL_KMSDRM_SYM_OPT(rc, fn, params) KMSDRM_##fn = (SDL_DYNKMSDRMFN_##fn)KMSDRM_GetSym(#fn, thismod, false); |
| 136 | #include "SDL_kmsdrmsym.h" |
| 137 | |
| 138 | if ((SDL_KMSDRM_HAVE_LIBDRM) && (SDL_KMSDRM_HAVE_GBM)) { |
| 139 | // all required symbols loaded. |
| 140 | SDL_ClearError(); |
| 141 | } else { |
| 142 | // in case something got loaded... |
| 143 | SDL_KMSDRM_UnloadSymbols(); |
| 144 | result = false; |
| 145 | } |
| 146 | |
| 147 | #else // no dynamic KMSDRM |
| 148 | |
| 149 | #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; // default yes |
| 150 | #define SDL_KMSDRM_SYM(rc, fn, params) KMSDRM_##fn = fn; |
| 151 | #define SDL_KMSDRM_SYM_CONST(type, name) KMSDRM_##name = name; |
| 152 | #define SDL_KMSDRM_SYM_OPT(rc, fn, params) KMSDRM_##fn = fn; |
| 153 | #include "SDL_kmsdrmsym.h" |
| 154 | |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | #endif // SDL_VIDEO_DRIVER_KMSDRM |
| 162 | |