SDL  2.0
SDL_kmsdrmdyn.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4  Atomic KMSDRM backend by Manuel Alfayate Corchete <redwindwanderer@gmail.com>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
22 
23 #include "../../SDL_internal.h"
24 
25 #if SDL_VIDEO_DRIVER_KMSDRM
26 
27 #define DEBUG_DYNAMIC_KMSDRM 0
28 
29 #include "SDL_kmsdrmdyn.h"
30 
31 
32 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
33 
34 #include "SDL_name.h"
35 #include "SDL_loadso.h"
36 
37 typedef struct
38 {
39  void *lib;
40  const char *libname;
41 } kmsdrmdynlib;
42 
43 #ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
44 #define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC NULL
45 #endif
46 #ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM
47 #define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM NULL
48 #endif
49 
50 static kmsdrmdynlib kmsdrmlibs[] = {
51  {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM},
52  {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC}
53 };
54 
55 static void *
56 KMSDRM_GetSym(const char *fnname, int *pHasModule)
57 {
58  int i;
59  void *fn = NULL;
60  for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
61  if (kmsdrmlibs[i].lib != NULL) {
62  fn = SDL_LoadFunction(kmsdrmlibs[i].lib, fnname);
63  if (fn != NULL)
64  break;
65  }
66  }
67 
68 #if DEBUG_DYNAMIC_KMSDRM
69  if (fn != NULL)
70  SDL_Log("KMSDRM: Found '%s' in %s (%p)\n", fnname, kmsdrmlibs[i].libname, fn);
71  else
72  SDL_Log("KMSDRM: Symbol '%s' NOT FOUND!\n", fnname);
73 #endif
74 
75  if (fn == NULL)
76  *pHasModule = 0; /* kill this module. */
77 
78  return fn;
79 }
80 
81 #endif /* SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC */
82 
83 /* Define all the function pointers and wrappers... */
84 #define SDL_KMSDRM_MODULE(modname) int SDL_KMSDRM_HAVE_##modname = 0;
85 #define SDL_KMSDRM_SYM(rc,fn,params) SDL_DYNKMSDRMFN_##fn KMSDRM_##fn = NULL;
86 #define SDL_KMSDRM_SYM_CONST(type,name) SDL_DYNKMSDRMCONST_##name KMSDRM_##name = NULL;
87 #include "SDL_kmsdrmsym.h"
88 
89 static int kmsdrm_load_refcount = 0;
90 
91 void
93 {
94  /* Don't actually unload if more than one module is using the libs... */
95  if (kmsdrm_load_refcount > 0) {
96  if (--kmsdrm_load_refcount == 0) {
97 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
98  int i;
99 #endif
100 
101  /* set all the function pointers to NULL. */
102 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 0;
103 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = NULL;
104 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = NULL;
105 #include "SDL_kmsdrmsym.h"
106 
107 
108 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
109  for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
110  if (kmsdrmlibs[i].lib != NULL) {
111  SDL_UnloadObject(kmsdrmlibs[i].lib);
112  kmsdrmlibs[i].lib = NULL;
113  }
114  }
115 #endif
116  }
117  }
118 }
119 
120 /* returns non-zero if all needed symbols were loaded. */
121 int
123 {
124  int rc = 1; /* always succeed if not using Dynamic KMSDRM stuff. */
125 
126  /* deal with multiple modules needing these symbols... */
127  if (kmsdrm_load_refcount++ == 0) {
128 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
129  int i;
130  int *thismod = NULL;
131  for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
132  if (kmsdrmlibs[i].libname != NULL) {
133  kmsdrmlibs[i].lib = SDL_LoadObject(kmsdrmlibs[i].libname);
134  }
135  }
136 
137 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
138 #include "SDL_kmsdrmsym.h"
139 
140 #define SDL_KMSDRM_MODULE(modname) thismod = &SDL_KMSDRM_HAVE_##modname;
141 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = (SDL_DYNKMSDRMFN_##fn) KMSDRM_GetSym(#fn,thismod);
142 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = *(SDL_DYNKMSDRMCONST_##name*) KMSDRM_GetSym(#name,thismod);
143 #include "SDL_kmsdrmsym.h"
144 
145  if ((SDL_KMSDRM_HAVE_LIBDRM) && (SDL_KMSDRM_HAVE_GBM)) {
146  /* all required symbols loaded. */
147  SDL_ClearError();
148  } else {
149  /* in case something got loaded... */
151  rc = 0;
152  }
153 
154 #else /* no dynamic KMSDRM */
155 
156 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
157 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = fn;
158 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = name;
159 #include "SDL_kmsdrmsym.h"
160 
161 #endif
162  }
163 
164  return rc;
165 }
166 
167 #endif /* SDL_VIDEO_DRIVER_KMSDRM */
168 
169 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_LoadObject
#define SDL_UnloadObject
#define SDL_ClearError
#define SDL_Log
int SDL_KMSDRM_LoadSymbols(void)
void SDL_KMSDRM_UnloadSymbols(void)
void * SDL_LoadFunction(void *handle, const char *name)
#define SDL_TABLESIZE(table)
Definition: SDL_stdinc.h:122
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:50
#define NULL
Definition: begin_code.h:163