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