2014-10-23 07:01:45 -04:00
|
|
|
/* runtime/weak dynamic JACK linking
|
|
|
|
*
|
|
|
|
* (C) 2014 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2019-08-02 22:11:41 -04:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-10-23 07:01:45 -04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2019-08-02 22:11:41 -04:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2014-10-23 07:01:45 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "weak_libjack.h"
|
|
|
|
|
|
|
|
#ifndef USE_WEAK_JACK
|
|
|
|
|
|
|
|
int have_libjack (void) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void* lib_open(const char* const so) {
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
return (void*) LoadLibraryA(so);
|
|
|
|
#else
|
|
|
|
return dlopen(so, RTLD_NOW|RTLD_LOCAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* lib_symbol(void* const lib, const char* const sym) {
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
return (void*) GetProcAddress((HMODULE)lib, sym);
|
|
|
|
#else
|
|
|
|
return dlsym(lib, sym);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COMPILER_MSVC
|
|
|
|
typedef void * pvoid_t;
|
2014-10-24 13:34:01 -04:00
|
|
|
#define MAPSYM(SYM, FAIL) _j._ ## SYM = (func_t)lib_symbol(lib, "jack_" # SYM); \
|
2014-10-23 07:01:45 -04:00
|
|
|
if (!_j._ ## SYM) err |= FAIL;
|
2014-11-13 15:28:33 -05:00
|
|
|
#elif defined NDEBUG
|
|
|
|
typedef void * __attribute__ ((__may_alias__)) pvoid_t;
|
|
|
|
#define MAPSYM(SYM, FAIL) *(pvoid_t *)(&_j._ ## SYM) = lib_symbol(lib, "jack_" # SYM); \
|
|
|
|
if (!_j._ ## SYM) err |= FAIL;
|
2014-10-23 07:01:45 -04:00
|
|
|
#else
|
|
|
|
typedef void * __attribute__ ((__may_alias__)) pvoid_t;
|
|
|
|
#define MAPSYM(SYM, FAIL) *(pvoid_t *)(&_j._ ## SYM) = lib_symbol(lib, "jack_" # SYM); \
|
2014-11-13 15:28:33 -05:00
|
|
|
if (!_j._ ## SYM) { \
|
|
|
|
if (FAIL) { \
|
|
|
|
fprintf(stderr, "*** WEAK-JACK: required symbol 'jack_%s' was not found\n", "" # SYM); \
|
|
|
|
} \
|
|
|
|
err |= FAIL; \
|
2014-11-13 16:19:22 -05:00
|
|
|
}
|
2014-10-23 07:01:45 -04:00
|
|
|
#endif
|
2014-10-24 09:18:38 -04:00
|
|
|
|
2014-10-23 07:01:45 -04:00
|
|
|
typedef void (* func_t) (void);
|
|
|
|
|
|
|
|
/* function pointers to the real jack API */
|
|
|
|
static struct WeakJack {
|
2014-10-24 09:18:38 -04:00
|
|
|
func_t _client_open; // special case due to varargs
|
|
|
|
|
|
|
|
#define JCFUN(ERR, RTYPE, NAME, RVAL) func_t _ ## NAME ;
|
|
|
|
#define JPFUN(ERR, RTYPE, NAME, DEF, ARGS, RVAL) func_t _ ## NAME ;
|
|
|
|
#define JXFUN(ERR, RTYPE, NAME, DEF, ARGS, CODE) func_t _ ## NAME ;
|
|
|
|
#define JVFUN(ERR, NAME, DEF, ARGS, CODE) func_t _ ## NAME ;
|
|
|
|
|
|
|
|
#include "weak_libjack.def"
|
|
|
|
|
|
|
|
#undef JCFUN
|
|
|
|
#undef JPFUN
|
|
|
|
#undef JXFUN
|
|
|
|
#undef JVFUN
|
2014-10-23 07:01:45 -04:00
|
|
|
} _j;
|
|
|
|
|
|
|
|
static int _status = -1;
|
|
|
|
|
|
|
|
__attribute__((constructor))
|
|
|
|
static void init_weak_jack(void)
|
|
|
|
{
|
|
|
|
void* lib;
|
|
|
|
int err = 0;
|
2014-10-24 11:15:03 -04:00
|
|
|
#ifndef NDEBUG
|
|
|
|
fprintf(stderr, "*** WEAK-JACK: initializing\n");
|
|
|
|
#endif
|
2014-10-23 07:01:45 -04:00
|
|
|
|
|
|
|
memset(&_j, 0, sizeof(_j));
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
lib = lib_open("libjack.dylib");
|
|
|
|
if (!lib) {
|
|
|
|
lib = lib_open("/usr/local/lib/libjack.dylib");
|
|
|
|
}
|
|
|
|
#elif (defined PLATFORM_WINDOWS)
|
2015-04-30 12:44:28 -04:00
|
|
|
# if ( defined(__x86_64__) || defined(_M_X64) )
|
2014-10-24 08:36:04 -04:00
|
|
|
lib = lib_open("libjack64.dll");
|
|
|
|
# else
|
2014-10-23 07:01:45 -04:00
|
|
|
lib = lib_open("libjack.dll");
|
2014-10-24 08:36:04 -04:00
|
|
|
# endif
|
2014-10-23 07:01:45 -04:00
|
|
|
#else
|
2014-10-24 08:36:04 -04:00
|
|
|
lib = lib_open("libjack.so.0");
|
2014-10-23 07:01:45 -04:00
|
|
|
#endif
|
|
|
|
if (!lib) {
|
2014-10-24 11:15:03 -04:00
|
|
|
#ifndef NDEBUG
|
|
|
|
fprintf(stderr, "*** WEAK-JACK: libjack was not found\n");
|
|
|
|
#endif
|
2014-10-23 07:01:45 -04:00
|
|
|
_status = -2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
/* found library, now lookup functions */
|
2014-10-23 07:01:45 -04:00
|
|
|
MAPSYM(client_open, 2)
|
2014-10-24 09:18:38 -04:00
|
|
|
|
|
|
|
#define JCFUN(ERR, RTYPE, NAME, RVAL) MAPSYM(NAME, ERR)
|
|
|
|
#define JPFUN(ERR, RTYPE, NAME, DEF, ARGS, RVAL) MAPSYM(NAME, ERR)
|
|
|
|
#define JXFUN(ERR, RTYPE, NAME, DEF, ARGS, CODE) MAPSYM(NAME, ERR)
|
|
|
|
#define JVFUN(ERR, NAME, DEF, ARGS, CODE) MAPSYM(NAME, ERR)
|
|
|
|
|
|
|
|
#include "weak_libjack.def"
|
|
|
|
|
|
|
|
#undef JCFUN
|
|
|
|
#undef JPFUN
|
|
|
|
#undef JXFUN
|
|
|
|
#undef JVFUN
|
2014-10-23 07:01:45 -04:00
|
|
|
|
|
|
|
/* if a required symbol is not found, disable JACK completly */
|
|
|
|
if (err) {
|
|
|
|
_j._client_open = NULL;
|
|
|
|
}
|
|
|
|
_status = err;
|
2014-10-24 11:15:03 -04:00
|
|
|
#ifndef NDEBUG
|
|
|
|
fprintf(stderr, "*** WEAK-JACK: %s. (%d)\n", err ? "jack is not available" : "OK", _status);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int have_libjack (void) {
|
|
|
|
if (_status == -1) {
|
|
|
|
init_weak_jack();
|
|
|
|
}
|
|
|
|
return _status;
|
2014-10-23 07:01:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
2014-10-24 09:18:38 -04:00
|
|
|
* helper macros
|
2014-10-23 07:01:45 -04:00
|
|
|
*/
|
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
#if defined(__GNUC__) && (__GNUC__ > 2) && !defined(NDEBUG)
|
|
|
|
#define likely(expr) (__builtin_expect (!!(expr), 1))
|
|
|
|
#else
|
|
|
|
#define likely(expr) (expr)
|
|
|
|
#endif
|
|
|
|
|
2014-10-23 07:01:45 -04:00
|
|
|
#ifndef NDEBUG
|
|
|
|
# define WJACK_WARNING(NAME) \
|
|
|
|
fprintf(stderr, "*** WEAK-JACK: function 'jack_%s' ignored\n", "" # NAME);
|
|
|
|
#else
|
|
|
|
# define WJACK_WARNING(NAME) ;
|
|
|
|
#endif
|
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* JACK API wrapper functions.
|
|
|
|
*
|
|
|
|
* if a function pointer is set in the static struct WeakJack _j,
|
|
|
|
* the function is called directly.
|
|
|
|
* Otherwise a dummy NOOP implementation is provided.
|
|
|
|
* The latter is mainly for compile-time warnings.
|
|
|
|
*
|
|
|
|
* If libjack is not found, jack_client_open() will fail.
|
|
|
|
* In that case the application should not call any other libjack
|
|
|
|
* functions. Hence a real implementation is not needed.
|
|
|
|
* (jack ringbuffer may be an exception for some apps)
|
|
|
|
*/
|
|
|
|
|
2014-10-25 10:12:38 -04:00
|
|
|
/* dedicated support for jack_client_open(,..) variable arg function macro */
|
|
|
|
func_t WJACK_get_client_open(void) {
|
2014-11-13 15:31:09 -05:00
|
|
|
if (_status == -1) {
|
|
|
|
init_weak_jack();
|
|
|
|
}
|
2014-10-25 10:12:38 -04:00
|
|
|
return _j._client_open;
|
2014-10-24 09:18:38 -04:00
|
|
|
}
|
|
|
|
|
2014-10-25 10:12:38 -04:00
|
|
|
/* callback to set status */
|
|
|
|
jack_client_t * WJACK_no_client_open (const char *client_name, jack_options_t options, jack_status_t *status, ...) {
|
|
|
|
WJACK_WARNING(client_open);
|
|
|
|
if (status) { *status = JackFailure; }
|
|
|
|
return NULL;
|
2014-10-24 09:18:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Macros to wrap jack API
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* abstraction for jack_client functions
|
|
|
|
* rtype jack_function_name (jack_client_t *client) { return rval; }
|
|
|
|
*/
|
|
|
|
#define JCFUN(ERR, RTYPE, NAME, RVAL) \
|
2014-10-23 07:01:45 -04:00
|
|
|
RTYPE WJACK_ ## NAME (jack_client_t *client) { \
|
2014-10-24 09:18:38 -04:00
|
|
|
if likely(_j._ ## NAME) { \
|
2014-10-23 07:01:45 -04:00
|
|
|
return ((RTYPE (*)(jack_client_t *client)) _j._ ## NAME)(client); \
|
|
|
|
} else { \
|
|
|
|
WJACK_WARNING(NAME) \
|
|
|
|
return RVAL; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
/* abstraction for NOOP functions with return value
|
|
|
|
* rtype jack_function_name (ARGS) { return rval; }
|
|
|
|
*/
|
|
|
|
#define JPFUN(ERR, RTYPE, NAME, DEF, ARGS, RVAL) \
|
2014-10-23 07:01:45 -04:00
|
|
|
RTYPE WJACK_ ## NAME DEF { \
|
2014-10-24 09:18:38 -04:00
|
|
|
if likely(_j._ ## NAME) { \
|
2014-10-23 07:01:45 -04:00
|
|
|
return ((RTYPE (*)DEF) _j._ ## NAME) ARGS; \
|
|
|
|
} else { \
|
|
|
|
WJACK_WARNING(NAME) \
|
|
|
|
return RVAL; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
/* abstraction for functions that need custom code.
|
|
|
|
* e.g. functions with return-value-pointer args,
|
|
|
|
* use CODE to initialize value
|
|
|
|
*
|
|
|
|
* rtype jack_function_name (ARGS) { CODE }
|
|
|
|
*/
|
|
|
|
#define JXFUN(ERR, RTYPE, NAME, DEF, ARGS, CODE) \
|
2014-10-23 07:01:45 -04:00
|
|
|
RTYPE WJACK_ ## NAME DEF { \
|
2014-10-24 09:18:38 -04:00
|
|
|
if likely(_j._ ## NAME) { \
|
2014-10-23 07:01:45 -04:00
|
|
|
return ((RTYPE (*)DEF) _j._ ## NAME) ARGS; \
|
|
|
|
} else { \
|
|
|
|
WJACK_WARNING(NAME) \
|
|
|
|
CODE \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
/* abstraction for void functions with return-value-pointer args
|
|
|
|
* void jack_function_name (ARGS) { CODE }
|
|
|
|
*/
|
|
|
|
#define JVFUN(ERR, NAME, DEF, ARGS, CODE) \
|
|
|
|
void WJACK_ ## NAME DEF { \
|
|
|
|
if likely(_j._ ## NAME) { \
|
|
|
|
((void (*)DEF) _j._ ## NAME) ARGS; \
|
2014-10-23 07:01:45 -04:00
|
|
|
} else { \
|
|
|
|
WJACK_WARNING(NAME) \
|
|
|
|
CODE \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
#include "weak_libjack.def"
|
2014-10-23 07:01:45 -04:00
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
#undef JCFUN
|
|
|
|
#undef JPFUN
|
|
|
|
#undef JXFUN
|
|
|
|
#undef JVFUN
|
2014-10-23 07:01:45 -04:00
|
|
|
|
2014-10-24 09:18:38 -04:00
|
|
|
#endif // end USE_WEAK_JACK
|