SDL  2.0
SDL_mirdyn.c
Go to the documentation of this file.
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[] = {
54 };
55 
56 static void *
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 #undef SDL_MIR_MODULE
90 #undef SDL_MIR_SYM
91 #undef SDL_MIR_SYM_CONST
92 
93 static int mir_load_refcount = 0;
94 
95 void
97 {
98  /* Don't actually unload if more than one module is using the libs... */
99  if (mir_load_refcount > 0) {
100  if (--mir_load_refcount == 0) {
101 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
102  int i;
103 #endif
104 
105  /* set all the function pointers to NULL. */
106 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 0;
107 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = NULL;
108 #define SDL_MIR_SYM_CONST(type,name) MIR_##name = NULL;
109 #include "SDL_mirsym.h"
110 #undef SDL_MIR_MODULE
111 #undef SDL_MIR_SYM
112 #undef SDL_MIR_SYM_CONST
113 
114 
115 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
116  for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
117  if (mirlibs[i].lib != NULL) {
118  SDL_UnloadObject(mirlibs[i].lib);
119  mirlibs[i].lib = NULL;
120  }
121  }
122 #endif
123  }
124  }
125 }
126 
127 /* returns non-zero if all needed symbols were loaded. */
128 int
130 {
131  int rc = 1; /* always succeed if not using Dynamic MIR stuff. */
132 
133  /* deal with multiple modules (dga, wayland, mir, etc) needing these symbols... */
134  if (mir_load_refcount++ == 0) {
135 #ifdef SDL_VIDEO_DRIVER_MIR_DYNAMIC
136  int i;
137  int *thismod = NULL;
138  for (i = 0; i < SDL_TABLESIZE(mirlibs); i++) {
139  if (mirlibs[i].libname != NULL) {
140  mirlibs[i].lib = SDL_LoadObject(mirlibs[i].libname);
141  }
142  }
143 
144 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
145 #define SDL_MIR_SYM(rc,fn,params)
146 #define SDL_MIR_SYM_CONST(type,name)
147 #include "SDL_mirsym.h"
148 #undef SDL_MIR_MODULE
149 #undef SDL_MIR_SYM
150 #undef SDL_MIR_SYM_CONST
151 
152 #define SDL_MIR_MODULE(modname) thismod = &SDL_MIR_HAVE_##modname;
153 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = (SDL_DYNMIRFN_##fn) MIR_GetSym(#fn,thismod);
154 #define SDL_MIR_SYM_CONST(type,name) MIR_##name = *(SDL_DYMMIRCONST_##name*) MIR_GetSym(#name,thismod);
155 #include "SDL_mirsym.h"
156 #undef SDL_MIR_MODULE
157 #undef SDL_MIR_SYM
158 #undef SDL_MIR_SYM_CONST
159 
160  if ((SDL_MIR_HAVE_MIR_CLIENT) && (SDL_MIR_HAVE_XKBCOMMON)) {
161  /* all required symbols loaded. */
162  SDL_ClearError();
163  } else {
164  /* in case something got loaded... */
166  rc = 0;
167  }
168 
169 #else /* no dynamic MIR */
170 
171 #define SDL_MIR_MODULE(modname) SDL_MIR_HAVE_##modname = 1; /* default yes */
172 #define SDL_MIR_SYM(rc,fn,params) MIR_##fn = fn;
173 #define SDL_MIR_SYM_CONST(type,name) MIR_##name = name;
174 #include "SDL_mirsym.h"
175 #undef SDL_MIR_MODULE
176 #undef SDL_MIR_SYM
177 #undef SDL_MIR_SYM_CONST
178 
179 #endif
180  }
181 
182  return rc;
183 }
184 
185 #endif /* SDL_VIDEO_DRIVER_MIR */
186 
187 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_ClearError
#define SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON
Definition: SDL_config.h:289
#define SDL_LoadObject
#define SDL_UnloadObject
void SDL_MIR_UnloadSymbols(void)
#define SDL_Log
#define SDL_VIDEO_DRIVER_MIR_DYNAMIC
Definition: SDL_config.h:288
#define SDL_TABLESIZE(table)
Definition: SDL_stdinc.h:94
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
#define NULL
Definition: begin_code.h:143
void * SDL_LoadFunction(void *handle, const char *name)
int SDL_MIR_LoadSymbols(void)