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