Debian Codegraph @

android/app/jni/extern/weakjack/weak_libjack.c

raw
1 /* runtime/weak dynamic JACK linking
2 *
3 * (C) 2014 Robin Gareus <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include "weak_libjack.h"
21
22 #define NDEBUG
23
24 #ifndef USE_WEAK_JACK
25
26 int have_libjack (void) {
27 return 0;
28 }
29
30 #else
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #ifdef _WIN32
37 #include <windows.h>
38 #else
39 #include <dlfcn.h>
40 #endif
41
42 static void* lib_open(const char* const so) {
43 #ifdef _WIN32
44 return (void*) LoadLibraryA(so);
45 #else
46 return dlopen(so, RTLD_NOW|RTLD_LOCAL);
47 #endif
48 }
49
50 static void* lib_symbol(void* const lib, const char* const sym) {
51 #ifdef _WIN32
52 return (void*) GetProcAddress((HMODULE)lib, sym);
53 #else
54 return dlsym(lib, sym);
55 #endif
56 }
57
58 #if defined _MSC_VER && ! defined __INTEL_COMPILER
59 typedef void * pvoid_t;
60 #define MAPSYM(SYM, FAIL) _j._ ## SYM = (func_t)lib_symbol(lib, "jack_" # SYM); \
61 if (!_j._ ## SYM) err |= FAIL;
62 #elif defined NDEBUG
63 typedef void * __attribute__ ((__may_alias__)) pvoid_t;
64 #define MAPSYM(SYM, FAIL) *(pvoid_t *)(&_j._ ## SYM) = lib_symbol(lib, "jack_" # SYM); \
65 if (!_j._ ## SYM) err |= FAIL;
66 #else
67 typedef void * __attribute__ ((__may_alias__)) pvoid_t;
68 #define MAPSYM(SYM, FAIL) *(pvoid_t *)(&_j._ ## SYM) = lib_symbol(lib, "jack_" # SYM); \
69 if (!_j._ ## SYM) { \
70 if (FAIL) { \
71 fprintf(stderr, "*** WEAK-JACK: required symbol 'jack_%s' was not found\n", "" # SYM); \
72 } \
73 err |= FAIL; \
74 }
75 #endif
76
77 typedef void (* func_t) (void);
78
79 /* function pointers to the real jack API */
80 static struct WeakJack {
81 func_t _client_open; // special case due to varargs
82
83 #define JCFUN(ERR, RTYPE, NAME, RVAL) func_t _ ## NAME ;
84 #define JPFUN(ERR, RTYPE, NAME, DEF, ARGS, RVAL) func_t _ ## NAME ;
85 #define JXFUN(ERR, RTYPE, NAME, DEF, ARGS, CODE) func_t _ ## NAME ;
86 #define JVFUN(ERR, NAME, DEF, ARGS, CODE) func_t _ ## NAME ;
87
88 #include "weak_libjack.def"
89
90 #undef JCFUN
91 #undef JPFUN
92 #undef JXFUN
93 #undef JVFUN
94 } _j;
95
96 static int _status = -1;
97 #if !defined _MSC_VER || defined __INTEL_COMPILER
98 __attribute__((constructor))
99 #endif
100 static void init_weak_jack(void)
101 {
102 void* lib;
103 int err = 0;
104 #ifndef NDEBUG
105 fprintf(stderr, "*** WEAK-JACK: initializing\n");
106 #endif
107
108 memset(&_j, 0, sizeof(_j));
109
110 #ifdef __APPLE__
111 lib = lib_open("libjack.dylib");
112 if (!lib) {
113 lib = lib_open("/usr/local/lib/libjack.dylib");
114 }
115 if (!lib) {
116 /* New Homebrew location */
117 lib = lib_open("/opt/homebrew/lib/libjack.dylib");
118 if (lib) {
119 fprintf(stderr, "*** WEAK-JACK: using Homebrew\n");
120 }
121 }
122 if (!lib) {
123 /* MacPorts location */
124 lib = lib_open("/opt/local/lib/libjack.dylib");
125 if (lib) {
126 fprintf(stderr, "*** WEAK-JACK: using MacPorts\n");
127 }
128 }
129
130 #elif (defined _WIN32)
131 # if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__)
132 lib = lib_open("libjack64.dll");
133 # else
134 lib = lib_open("libjack.dll");
135 # endif
136 #else
137 lib = lib_open("libjack.so.0");
138 #endif
139 if (!lib) {
140 #ifndef NDEBUG
141 fprintf(stderr, "*** WEAK-JACK: libjack was not found\n");
142 #endif
143 _status = -2;
144 return;
145 }
146
147 /* found library, now lookup functions */
148 MAPSYM(client_open, 2)
149
150 #define JCFUN(ERR, RTYPE, NAME, RVAL) MAPSYM(NAME, ERR)
151 #define JPFUN(ERR, RTYPE, NAME, DEF, ARGS, RVAL) MAPSYM(NAME, ERR)
152 #define JXFUN(ERR, RTYPE, NAME, DEF, ARGS, CODE) MAPSYM(NAME, ERR)
153 #define JVFUN(ERR, NAME, DEF, ARGS, CODE) MAPSYM(NAME, ERR)
154
155 #include "weak_libjack.def"
156
157 #undef JCFUN
158 #undef JPFUN
159 #undef JXFUN
160 #undef JVFUN
161
162 /* if a required symbol is not found, disable JACK completly */
163 if (err) {
164 _j._client_open = NULL;
165 }
166 _status = err;
167 #ifndef NDEBUG
168 fprintf(stderr, "*** WEAK-JACK: %s. (%d)\n", err ? "jack is not available" : "OK", _status);
169 #endif
170 }
171
172 int have_libjack (void) {
173 if (_status == -1) {
174 init_weak_jack();
175 }
176 return _status;
177 }
178
179 /*******************************************************************************
180 * helper macros
181 */
182
183 #if defined(__GNUC__) && (__GNUC__ > 2) && !defined(NDEBUG)
184 #define likely(expr) (__builtin_expect (!!(expr), 1))
185 #else
186 #define likely(expr) (expr)
187 #endif
188
189 #ifndef NDEBUG
190 # define WJACK_WARNING(NAME) \
191 fprintf(stderr, "*** WEAK-JACK: function 'jack_%s' ignored\n", "" # NAME);
192 #else
193 # define WJACK_WARNING(NAME) ;
194 #endif
195
196 /******************************************************************************
197 * JACK API wrapper functions.
198 *
199 * if a function pointer is set in the static struct WeakJack _j,
200 * the function is called directly.
201 * Otherwise a dummy NOOP implementation is provided.
202 * The latter is mainly for compile-time warnings.
203 *
204 * If libjack is not found, jack_client_open() will fail.
205 * In that case the application should not call any other libjack
206 * functions. Hence a real implementation is not needed.
207 * (jack ringbuffer may be an exception for some apps)
208 */
209
210 /* dedicated support for jack_client_open(,..) variable arg function macro */
211 func_t WJACK_get_client_open(void) {
212 if (_status == -1) {
213 init_weak_jack();
214 }
215 return _j._client_open;
216 }
217
218 /* callback to set status */
219 jack_client_t * WJACK_no_client_open (const char *client_name, jack_options_t options, jack_status_t *status, ...) {
220 WJACK_WARNING(client_open);
221 if (status) { *status = JackFailure; }
222 return NULL;
223 }
224
225 /*******************************************************************************
226 * Macros to wrap jack API
227 */
228
229 /* abstraction for jack_client functions
230 * rtype jack_function_name (jack_client_t *client) { return rval; }
231 */
232 #define JCFUN(ERR, RTYPE, NAME, RVAL) \
233 RTYPE WJACK_ ## NAME (jack_client_t *client) { \
234 if likely(_j._ ## NAME) { \
235 return ((RTYPE (*)(jack_client_t *client)) _j._ ## NAME)(client); \
236 } else { \
237 WJACK_WARNING(NAME) \
238 return RVAL; \
239 } \
240 }
241
242 /* abstraction for NOOP functions with return value
243 * rtype jack_function_name (ARGS) { return rval; }
244 */
245 #define JPFUN(ERR, RTYPE, NAME, DEF, ARGS, RVAL) \
246 RTYPE WJACK_ ## NAME DEF { \
247 if likely(_j._ ## NAME) { \
248 return ((RTYPE (*)DEF) _j._ ## NAME) ARGS; \
249 } else { \
250 WJACK_WARNING(NAME) \
251 return RVAL; \
252 } \
253 }
254
255 /* abstraction for functions that need custom code.
256 * e.g. functions with return-value-pointer args,
257 * use CODE to initialize value
258 *
259 * rtype jack_function_name (ARGS) { CODE }
260 */
261 #define JXFUN(ERR, RTYPE, NAME, DEF, ARGS, CODE) \
262 RTYPE WJACK_ ## NAME DEF { \
263 if likely(_j._ ## NAME) { \
264 return ((RTYPE (*)DEF) _j._ ## NAME) ARGS; \
265 } else { \
266 WJACK_WARNING(NAME) \
267 CODE \
268 } \
269 }
270
271 /* abstraction for void functions with return-value-pointer args
272 * void jack_function_name (ARGS) { CODE }
273 */
274 #define JVFUN(ERR, NAME, DEF, ARGS, CODE) \
275 void WJACK_ ## NAME DEF { \
276 if likely(_j._ ## NAME) { \
277 ((void (*)DEF) _j._ ## NAME) ARGS; \
278 } else { \
279 WJACK_WARNING(NAME) \
280 CODE \
281 } \
282 }
283
284 #include "weak_libjack.def"
285
286 #undef JCFUN
287 #undef JPFUN
288 #undef JXFUN
289 #undef JVFUN
290
291 #endif // end USE_WEAK_JACK