| 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_WAYLAND |
| 24 | |
| 25 | #define DEBUG_DYNAMIC_WAYLAND 0 |
| 26 | |
| 27 | #include "SDL_waylanddyn.h" |
| 28 | |
| 29 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC |
| 30 | |
| 31 | typedef struct |
| 32 | { |
| 33 | SDL_SharedObject *lib; |
| 34 | const char *libname; |
| 35 | } waylanddynlib; |
| 36 | |
| 37 | static waylanddynlib waylandlibs[] = { |
| 38 | { NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC }, |
| 39 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL |
| 40 | { NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL }, |
| 41 | #endif |
| 42 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR |
| 43 | { NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR }, |
| 44 | #endif |
| 45 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON |
| 46 | { NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON }, |
| 47 | #endif |
| 48 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_LIBDECOR |
| 49 | { NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_LIBDECOR }, |
| 50 | #endif |
| 51 | { NULL, NULL } |
| 52 | }; |
| 53 | |
| 54 | static void *WAYLAND_GetSym(const char *fnname, int *pHasModule, bool required) |
| 55 | { |
| 56 | void *fn = NULL; |
| 57 | waylanddynlib *dynlib; |
| 58 | for (dynlib = waylandlibs; dynlib->libname; dynlib++) { |
| 59 | if (dynlib->lib) { |
| 60 | fn = SDL_LoadFunction(dynlib->lib, fnname); |
| 61 | if (fn) { |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #if DEBUG_DYNAMIC_WAYLAND |
| 68 | if (fn) { |
| 69 | SDL_Log("WAYLAND: Found '%s' in %s (%p)" , fnname, dynlib->libname, fn); |
| 70 | } else { |
| 71 | SDL_Log("WAYLAND: Symbol '%s' NOT FOUND!" , fnname); |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | if (!fn && required) { |
| 76 | *pHasModule = 0; // kill this module. |
| 77 | } |
| 78 | |
| 79 | return fn; |
| 80 | } |
| 81 | |
| 82 | #else |
| 83 | |
| 84 | #include <wayland-egl.h> |
| 85 | |
| 86 | #endif // SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC |
| 87 | |
| 88 | // Define all the function pointers and wrappers... |
| 89 | #define SDL_WAYLAND_MODULE(modname) int SDL_WAYLAND_HAVE_##modname = 0; |
| 90 | #define SDL_WAYLAND_SYM(rc, fn, params) SDL_DYNWAYLANDFN_##fn WAYLAND_##fn = NULL; |
| 91 | #define SDL_WAYLAND_SYM_OPT(rc, fn, params) SDL_DYNWAYLANDFN_##fn WAYLAND_##fn = NULL; |
| 92 | #define SDL_WAYLAND_INTERFACE(iface) const struct wl_interface *WAYLAND_##iface = NULL; |
| 93 | #include "SDL_waylandsym.h" |
| 94 | |
| 95 | static int wayland_load_refcount = 0; |
| 96 | |
| 97 | void SDL_WAYLAND_UnloadSymbols(void) |
| 98 | { |
| 99 | // Don't actually unload if more than one module is using the libs... |
| 100 | if (wayland_load_refcount > 0) { |
| 101 | if (--wayland_load_refcount == 0) { |
| 102 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC |
| 103 | int i; |
| 104 | #endif |
| 105 | |
| 106 | // set all the function pointers to NULL. |
| 107 | #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 0; |
| 108 | #define SDL_WAYLAND_SYM(rc, fn, params) WAYLAND_##fn = NULL; |
| 109 | #define SDL_WAYLAND_SYM_OPT(rc, fn, params) WAYLAND_##fn = NULL; |
| 110 | #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = NULL; |
| 111 | #include "SDL_waylandsym.h" |
| 112 | |
| 113 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC |
| 114 | for (i = 0; i < SDL_arraysize(waylandlibs); i++) { |
| 115 | if (waylandlibs[i].lib) { |
| 116 | SDL_UnloadObject(waylandlibs[i].lib); |
| 117 | waylandlibs[i].lib = NULL; |
| 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // returns non-zero if all needed symbols were loaded. |
| 126 | bool SDL_WAYLAND_LoadSymbols(void) |
| 127 | { |
| 128 | bool result = true; // always succeed if not using Dynamic WAYLAND stuff. |
| 129 | |
| 130 | // deal with multiple modules (dga, wayland, etc) needing these symbols... |
| 131 | if (wayland_load_refcount++ == 0) { |
| 132 | #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC |
| 133 | int i; |
| 134 | int *thismod = NULL; |
| 135 | for (i = 0; i < SDL_arraysize(waylandlibs); i++) { |
| 136 | if (waylandlibs[i].libname) { |
| 137 | waylandlibs[i].lib = SDL_LoadObject(waylandlibs[i].libname); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; // default yes |
| 142 | #include "SDL_waylandsym.h" |
| 143 | |
| 144 | #define SDL_WAYLAND_MODULE(modname) thismod = &SDL_WAYLAND_HAVE_##modname; |
| 145 | #define SDL_WAYLAND_SYM(rc, fn, params) WAYLAND_##fn = (SDL_DYNWAYLANDFN_##fn)WAYLAND_GetSym(#fn, thismod, true); |
| 146 | #define SDL_WAYLAND_SYM_OPT(rc, fn, params) WAYLAND_##fn = (SDL_DYNWAYLANDFN_##fn)WAYLAND_GetSym(#fn, thismod, false); |
| 147 | #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = (struct wl_interface *)WAYLAND_GetSym(#iface, thismod, true); |
| 148 | #include "SDL_waylandsym.h" |
| 149 | |
| 150 | if (SDL_WAYLAND_HAVE_WAYLAND_CLIENT && |
| 151 | SDL_WAYLAND_HAVE_WAYLAND_CURSOR && |
| 152 | SDL_WAYLAND_HAVE_WAYLAND_EGL && |
| 153 | SDL_WAYLAND_HAVE_WAYLAND_XKB) { |
| 154 | // All required symbols loaded, only libdecor is optional. |
| 155 | SDL_ClearError(); |
| 156 | } else { |
| 157 | // in case something got loaded... |
| 158 | SDL_WAYLAND_UnloadSymbols(); |
| 159 | result = false; |
| 160 | } |
| 161 | |
| 162 | #else // no dynamic WAYLAND |
| 163 | |
| 164 | #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; // default yes |
| 165 | #define SDL_WAYLAND_SYM(rc, fn, params) WAYLAND_##fn = fn; |
| 166 | #define SDL_WAYLAND_SYM_OPT(rc, fn, params) WAYLAND_##fn = fn; |
| 167 | #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = &iface; |
| 168 | #include "SDL_waylandsym.h" |
| 169 | |
| 170 | #endif |
| 171 | } |
| 172 | |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | #endif // SDL_VIDEO_DRIVER_WAYLAND |
| 177 | |