1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 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_MIR
24 
25 #define DEBUG_DYNAMIC_MIR 0
26 
27 #include "SDL_mirdyn.h"
28 
29 #if DEBUG_DYNAMIC_MIR
30 #include "SDL_log.h"
31 #endif
32 
33 #ifdef SDL_VIDEO_DRIVER_MIR_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 } mirdynlib;
43 
44 #ifndef SDL_VIDEO_DRIVER_MIR_DYNAMIC
45 #define SDL_VIDEO_DRIVER_MIR_DYNAMIC NULL
46 #endif
47 #ifndef SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON
48 #define SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON NULL
49 #endif
50 
51 static mirdynlib mirlibs[] = {
52     {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC},
53     {NULL, SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON}
54 };
55 
56 static void *
MIR_GetSym(const char * fnname,int * pHasModule)57 MIR_GetSym(const char *fnname, int *pHasModule)
58 {
59     int i;
60     void *fn = NULL;
61     for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
62         if (mirlibs[i].lib != NULL) {
63             fn = SDL_LoadFunction(mirlibs[i].lib, fnname);
64             if (fn != NULL)
65                 break;
66         }
67     }
68 
69 #if DEBUG_DYNAMIC_MIR
70     if (fn != NULL)
71         SDL_Log("MIR: Found '%s' in %s (%p)\n", fnname, mirlibs[i].libname, fn);
72     else
73         SDL_Log("MIR: Symbol '%s' NOT FOUND!\n", fnname);
74 #endif
75 
76     if (fn == NULL)
77         *pHasModule = 0;  /* kill this module. */
78 
79     return fn;
80 }
81 
82 #endif /* SDL_VIDEO_DRIVER_MIR_DYNAMIC */
83 
84 /* Define all the function pointers and wrappers... */
85 #define SDL_MIR_MODULE(modname) int SDL_MIR_HAVE_##modname = 0;
86 #define SDL_MIR_SYM(rc,fn,params) SDL_DYNMIRFN_##fn MIR_##fn = NULL;
87 #define SDL_MIR_SYM_CONST(type,name) SDL_DYMMIRCONST_##name MIR_##name = NULL;
88 #include "SDL_mirsym.h"
89 
90 static int mir_load_refcount = 0;
91 
92 void
SDL_MIR_UnloadSymbols(void)93 SDL_MIR_UnloadSymbols(void)
94 {
95     /* Don't actually unload if more than one module is using the libs... */
96     if (mir_load_refcount > 0) {
97         if (--mir_load_refcount == 0) {
98 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
99             int i;
100 #endif
101 
102             /* set all the function pointers to NULL. */
103 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 0;
104 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = NULL;
105 #define SDL_MIR_SYM_CONST(type,name) MIR_##name = NULL;
106 #include "SDL_mirsym.h"
107 
108 
109 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
110             for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
111                 if (mirlibs[i].lib != NULL) {
112                     SDL_UnloadObject(mirlibs[i].lib);
113                     mirlibs[i].lib = NULL;
114                 }
115             }
116 #endif
117         }
118     }
119 }
120 
121 /* returns non-zero if all needed symbols were loaded. */
122 int
SDL_MIR_LoadSymbols(void)123 SDL_MIR_LoadSymbols(void)
124 {
125     int rc = 1;                 /* always succeed if not using Dynamic MIR stuff. */
126 
127     /* deal with multiple modules (dga, wayland, mir, etc) needing these symbols... */
128     if (mir_load_refcount++ == 0) {
129 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
130         int i;
131         int *thismod = NULL;
132         for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
133             if (mirlibs[i].libname != NULL) {
134                 mirlibs[i].lib = SDL_LoadObject(mirlibs[i].libname);
135             }
136         }
137 
138 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
139 #include "SDL_mirsym.h"
140 
141 #define SDL_MIR_MODULE(modname) thismod = &SDL_MIR_HAVE_##modname;
142 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = (SDL_DYNMIRFN_##fn) MIR_GetSym(#fn,thismod);
143 #define SDL_MIR_SYM_CONST(type,name) MIR_##name = *(SDL_DYMMIRCONST_##name*) MIR_GetSym(#name,thismod);
144 #include "SDL_mirsym.h"
145 
146         if ((SDL_MIR_HAVE_MIR_CLIENT) && (SDL_MIR_HAVE_XKBCOMMON)) {
147             /* all required symbols loaded. */
148             SDL_ClearError();
149         } else {
150             /* in case something got loaded... */
151             SDL_MIR_UnloadSymbols();
152             rc = 0;
153         }
154 
155 #else  /* no dynamic MIR */
156 
157 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
158 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = fn;
159 #define SDL_MIR_SYM_CONST(type,name) MIR_##name = name;
160 #include "SDL_mirsym.h"
161 
162 #endif
163     }
164 
165     return rc;
166 }
167 
168 #endif /* SDL_VIDEO_DRIVER_MIR */
169 
170 /* vi: set ts=4 sw=4 expandtab: */
171