c79f372bf9d0959cbb137b5076ed27ab03cbcac0
[platform/upstream/SDL.git] / src / video / kmsdrm / SDL_kmsdrmdyn.c
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2018 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
22 #include "../../SDL_internal.h"
23
24 #if SDL_VIDEO_DRIVER_KMSDRM
25
26 #define DEBUG_DYNAMIC_KMSDRM 0
27
28 #include "SDL_kmsdrmdyn.h"
29
30 #if DEBUG_DYNAMIC_KMSDRM
31 #include "SDL_log.h"
32 #endif
33
34 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
35
36 #include "SDL_name.h"
37 #include "SDL_loadso.h"
38
39 typedef struct
40 {
41     void *lib;
42     const char *libname;
43 } kmsdrmdynlib;
44
45 #ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
46 #define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC NULL
47 #endif
48 #ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM
49 #define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM NULL
50 #endif
51
52 static kmsdrmdynlib kmsdrmlibs[] = {
53     {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC},
54     {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM}
55 };
56
57 static void *
58 KMSDRM_GetSym(const char *fnname, int *pHasModule)
59 {
60     int i;
61     void *fn = NULL;
62     for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
63         if (kmsdrmlibs[i].lib != NULL) {
64             fn = SDL_LoadFunction(kmsdrmlibs[i].lib, fnname);
65             if (fn != NULL)
66                 break;
67         }
68     }
69
70 #if DEBUG_DYNAMIC_KMSDRM
71     if (fn != NULL)
72         SDL_Log("KMSDRM: Found '%s' in %s (%p)\n", fnname, kmsdrmlibs[i].libname, fn);
73     else
74         SDL_Log("KMSDRM: Symbol '%s' NOT FOUND!\n", fnname);
75 #endif
76
77     if (fn == NULL)
78         *pHasModule = 0;  /* kill this module. */
79
80     return fn;
81 }
82
83 #endif /* SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC */
84
85 /* Define all the function pointers and wrappers... */
86 #define SDL_KMSDRM_MODULE(modname) int SDL_KMSDRM_HAVE_##modname = 0;
87 #define SDL_KMSDRM_SYM(rc,fn,params) SDL_DYNKMSDRMFN_##fn KMSDRM_##fn = NULL;
88 #define SDL_KMSDRM_SYM_CONST(type,name) SDL_DYNKMSDRMCONST_##name KMSDRM_##name = NULL;
89 #include "SDL_kmsdrmsym.h"
90
91 static int kmsdrm_load_refcount = 0;
92
93 void
94 SDL_KMSDRM_UnloadSymbols(void)
95 {
96     /* Don't actually unload if more than one module is using the libs... */
97     if (kmsdrm_load_refcount > 0) {
98         if (--kmsdrm_load_refcount == 0) {
99 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
100             int i;
101 #endif
102
103             /* set all the function pointers to NULL. */
104 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 0;
105 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = NULL;
106 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = NULL;
107 #include "SDL_kmsdrmsym.h"
108
109
110 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
111             for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
112                 if (kmsdrmlibs[i].lib != NULL) {
113                     SDL_UnloadObject(kmsdrmlibs[i].lib);
114                     kmsdrmlibs[i].lib = NULL;
115                 }
116             }
117 #endif
118         }
119     }
120 }
121
122 /* returns non-zero if all needed symbols were loaded. */
123 int
124 SDL_KMSDRM_LoadSymbols(void)
125 {
126     int rc = 1;                 /* always succeed if not using Dynamic KMSDRM stuff. */
127
128     /* deal with multiple modules needing these symbols... */
129     if (kmsdrm_load_refcount++ == 0) {
130 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
131         int i;
132         int *thismod = NULL;
133         for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
134             if (kmsdrmlibs[i].libname != NULL) {
135                 kmsdrmlibs[i].lib = SDL_LoadObject(kmsdrmlibs[i].libname);
136             }
137         }
138
139 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
140 #include "SDL_kmsdrmsym.h"
141
142 #define SDL_KMSDRM_MODULE(modname) thismod = &SDL_KMSDRM_HAVE_##modname;
143 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = (SDL_DYNKMSDRMFN_##fn) KMSDRM_GetSym(#fn,thismod);
144 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = *(SDL_DYNKMSDRMCONST_##name*) KMSDRM_GetSym(#name,thismod);
145 #include "SDL_kmsdrmsym.h"
146
147         if ((SDL_KMSDRM_HAVE_LIBDRM) && (SDL_KMSDRM_HAVE_GBM)) {
148             /* all required symbols loaded. */
149             SDL_ClearError();
150         } else {
151             /* in case something got loaded... */
152             SDL_KMSDRM_UnloadSymbols();
153             rc = 0;
154         }
155
156 #else  /* no dynamic KMSDRM */
157
158 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
159 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = fn;
160 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = name;
161 #include "SDL_kmsdrmsym.h"
162
163 #endif
164     }
165
166     return rc;
167 }
168
169 #endif /* SDL_VIDEO_DRIVER_KMSDRM */
170
171 /* vi: set ts=4 sw=4 expandtab: */