change SDL 1.2 to SDL 2.0
[platform/upstream/SDL.git] / src / video / wayland / SDL_waylanddyn.c
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_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
45 #define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC NULL
46 #endif
47 #ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL
48 #define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL NULL
49 #endif
50 #ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR
51 #define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR NULL
52 #endif
53 #ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON
54 #define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON NULL
55 #endif
56
57 static waylanddynlib waylandlibs[] = {
58     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC},
59     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL},
60     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR},
61     {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON}
62 };
63
64 static void *
65 WAYLAND_GetSym(const char *fnname, int *pHasModule)
66 {
67     int i;
68     void *fn = NULL;
69     for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
70         if (waylandlibs[i].lib != NULL) {
71             fn = SDL_LoadFunction(waylandlibs[i].lib, fnname);
72             if (fn != NULL)
73                 break;
74         }
75     }
76
77 #if DEBUG_DYNAMIC_WAYLAND
78     if (fn != NULL)
79         SDL_Log("WAYLAND: Found '%s' in %s (%p)\n", fnname, waylandlibs[i].libname, fn);
80     else
81         SDL_Log("WAYLAND: Symbol '%s' NOT FOUND!\n", fnname);
82 #endif
83
84     if (fn == NULL)
85         *pHasModule = 0;  /* kill this module. */
86
87     return fn;
88 }
89
90 #endif /* SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC */
91
92 /* Define all the function pointers and wrappers... */
93 #define SDL_WAYLAND_MODULE(modname) int SDL_WAYLAND_HAVE_##modname = 0;
94 #define SDL_WAYLAND_SYM(rc,fn,params) SDL_DYNWAYLANDFN_##fn WAYLAND_##fn = NULL;
95 #define SDL_WAYLAND_INTERFACE(iface) const struct wl_interface *WAYLAND_##iface = NULL;
96 #include "SDL_waylandsym.h"
97 #undef SDL_WAYLAND_MODULE
98 #undef SDL_WAYLAND_SYM
99 #undef SDL_WAYLAND_INTERFACE
100
101 static int wayland_load_refcount = 0;
102
103 void
104 SDL_WAYLAND_UnloadSymbols(void)
105 {
106     /* Don't actually unload if more than one module is using the libs... */
107     if (wayland_load_refcount > 0) {
108         if (--wayland_load_refcount == 0) {
109 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC            
110             int i;
111 #endif
112             
113             /* set all the function pointers to NULL. */
114 #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 0;
115 #define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = NULL;
116 #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = NULL;
117 #include "SDL_waylandsym.h"
118 #undef SDL_WAYLAND_MODULE
119 #undef SDL_WAYLAND_SYM
120 #undef SDL_WAYLAND_INTERFACE
121
122
123 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
124             for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
125                 if (waylandlibs[i].lib != NULL) {
126                     SDL_UnloadObject(waylandlibs[i].lib);
127                     waylandlibs[i].lib = NULL;
128                 }
129             }
130 #endif
131         }
132     }
133 }
134
135 /* returns non-zero if all needed symbols were loaded. */
136 int
137 SDL_WAYLAND_LoadSymbols(void)
138 {
139     int rc = 1;                 /* always succeed if not using Dynamic WAYLAND stuff. */
140
141     /* deal with multiple modules (dga, wayland, etc) needing these symbols... */
142     if (wayland_load_refcount++ == 0) {
143 #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
144         int i;
145         int *thismod = NULL;
146         for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
147             if (waylandlibs[i].libname != NULL) {
148                 waylandlibs[i].lib = SDL_LoadObject(waylandlibs[i].libname);
149             }
150         }
151
152 #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; /* default yes */
153 #define SDL_WAYLAND_SYM(rc,fn,params)
154 #define SDL_WAYLAND_INTERFACE(iface)
155 #include "SDL_waylandsym.h"
156 #undef SDL_WAYLAND_MODULE
157 #undef SDL_WAYLAND_SYM
158 #undef SDL_WAYLAND_INTERFACE
159
160 #define SDL_WAYLAND_MODULE(modname) thismod = &SDL_WAYLAND_HAVE_##modname;
161 #define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = (SDL_DYNWAYLANDFN_##fn) WAYLAND_GetSym(#fn,thismod);
162 #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = (struct wl_interface *) WAYLAND_GetSym(#iface,thismod);
163 #include "SDL_waylandsym.h"
164 #undef SDL_WAYLAND_MODULE
165 #undef SDL_WAYLAND_SYM
166 #undef SDL_WAYLAND_INTERFACE
167
168         if (SDL_WAYLAND_HAVE_WAYLAND_CLIENT) {
169             /* all required symbols loaded. */
170             SDL_ClearError();
171         } else {
172             /* in case something got loaded... */
173             SDL_WAYLAND_UnloadSymbols();
174             rc = 0;
175         }
176
177 #else  /* no dynamic WAYLAND */
178
179 #define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; /* default yes */
180 #define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = fn;
181 #define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = &iface;
182 #include "SDL_waylandsym.h"
183 #undef SDL_WAYLAND_MODULE
184 #undef SDL_WAYLAND_SYM
185 #undef SDL_WAYLAND_INTERFACE
186
187 #endif
188     }
189
190     return rc;
191 }
192
193 #endif /* SDL_VIDEO_DRIVER_WAYLAND */
194
195 /* vi: set ts=4 sw=4 expandtab: */