src/video/wayland/SDL_waylanddyn.c
author Ozkan Sezer <sezeroz@gmail.com>
Wed, 20 Nov 2019 02:47:40 +0300
changeset 13260 688fd43988a5
parent 13198 3965c9d391e3
permissions -rw-r--r--
SDL.c (SDL_ExitProcess): unconstify its param to match its declaration.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2019 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 #if SDL_VIDEO_DRIVER_WAYLAND
    24 
    25 #define DEBUG_DYNAMIC_WAYLAND 0
    26 
    27 #include "SDL_waylanddyn.h"
    28 
    29 #if DEBUG_DYNAMIC_WAYLAND
    30 #include "SDL_log.h"
    31 #endif
    32 
    33 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
    34 
    35 #include "SDL_name.h"
    36 #include "SDL_loadso.h"
    37 
    38 typedef struct
    39 {
    40     void *lib;
    41     const char *libname;
    42 } waylanddynlib;
    43 
    44 #ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL
    45 #define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL NULL
    46 #endif
    47 #ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR
    48 #define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR NULL
    49 #endif
    50 #ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON
    51 #define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON NULL
    52 #endif
    53 
    54 static waylanddynlib waylandlibs[] = {
    55     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC},
    56     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL},
    57     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR},
    58     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON}
    59 };
    60 
    61 static void *
    62 WAYLAND_GetSym(const char *fnname, int *pHasModule)
    63 {
    64     int i;
    65     void *fn = NULL;
    66     for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
    67         if (waylandlibs[i].lib != NULL) {
    68             fn = SDL_LoadFunction(waylandlibs[i].lib, fnname);
    69             if (fn != NULL)
    70                 break;
    71         }
    72     }
    73 
    74 #if DEBUG_DYNAMIC_WAYLAND
    75     if (fn != NULL)
    76         SDL_Log("WAYLAND: Found '%s' in %s (%p)\n", fnname, waylandlibs[i].libname, fn);
    77     else
    78         SDL_Log("WAYLAND: Symbol '%s' NOT FOUND!\n", fnname);
    79 #endif
    80 
    81     if (fn == NULL)
    82         *pHasModule = 0;  /* kill this module. */
    83 
    84     return fn;
    85 }
    86 
    87 #endif /* SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC */
    88 
    89 /* Define all the function pointers and wrappers... */
    90 #define SDL_WAYLAND_MODULE(modname) int SDL_WAYLAND_HAVE_##modname = 0;
    91 #define SDL_WAYLAND_SYM(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
    98 SDL_WAYLAND_UnloadSymbols(void)
    99 {
   100     /* Don't actually unload if more than one module is using the libs... */
   101     if (wayland_load_refcount > 0) {
   102         if (--wayland_load_refcount == 0) {
   103 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC            
   104             int i;
   105 #endif
   106             
   107             /* set all the function pointers to NULL. */
   108 #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 0;
   109 #define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = NULL;
   110 #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = NULL;
   111 #include "SDL_waylandsym.h"
   112 
   113 
   114 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
   115             for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
   116                 if (waylandlibs[i].lib != NULL) {
   117                     SDL_UnloadObject(waylandlibs[i].lib);
   118                     waylandlibs[i].lib = NULL;
   119                 }
   120             }
   121 #endif
   122         }
   123     }
   124 }
   125 
   126 /* returns non-zero if all needed symbols were loaded. */
   127 int
   128 SDL_WAYLAND_LoadSymbols(void)
   129 {
   130     int rc = 1;                 /* always succeed if not using Dynamic WAYLAND stuff. */
   131 
   132     /* deal with multiple modules (dga, wayland, etc) needing these symbols... */
   133     if (wayland_load_refcount++ == 0) {
   134 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
   135         int i;
   136         int *thismod = NULL;
   137         for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
   138             if (waylandlibs[i].libname != NULL) {
   139                 waylandlibs[i].lib = SDL_LoadObject(waylandlibs[i].libname);
   140             }
   141         }
   142 
   143 #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; /* default yes */
   144 #include "SDL_waylandsym.h"
   145 
   146 #define SDL_WAYLAND_MODULE(modname) thismod = &SDL_WAYLAND_HAVE_##modname;
   147 #define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = (SDL_DYNWAYLANDFN_##fn) WAYLAND_GetSym(#fn,thismod);
   148 #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = (struct wl_interface *) WAYLAND_GetSym(#iface,thismod);
   149 #include "SDL_waylandsym.h"
   150 
   151         if (SDL_WAYLAND_HAVE_WAYLAND_CLIENT) {
   152             /* all required symbols loaded. */
   153             SDL_ClearError();
   154         } else {
   155             /* in case something got loaded... */
   156             SDL_WAYLAND_UnloadSymbols();
   157             rc = 0;
   158         }
   159 
   160 #else  /* no dynamic WAYLAND */
   161 
   162 #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; /* default yes */
   163 #define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = fn;
   164 #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = &iface;
   165 #include "SDL_waylandsym.h"
   166 
   167 #endif
   168     }
   169 
   170     return rc;
   171 }
   172 
   173 #endif /* SDL_VIDEO_DRIVER_WAYLAND */
   174 
   175 /* vi: set ts=4 sw=4 expandtab: */