Made NAS target dynamic, so you can safely include it in shipping binaries. SDL-1.2
authorRyan C. Gordon <icculus@icculus.org>
Sun, 13 Sep 2009 22:19:56 +0000
branchSDL-1.2
changeset 41922f5e884f0a9d
parent 4191 caaa3cbf7b13
child 4193 430e6690c4cd
Made NAS target dynamic, so you can safely include it in shipping binaries.
configure.in
include/SDL_config.h.in
src/audio/nas/SDL_nasaudio.c
     1.1 --- a/configure.in	Sat Sep 12 19:41:54 2009 +0000
     1.2 +++ b/configure.in	Sun Sep 13 22:19:56 2009 +0000
     1.3 @@ -591,30 +591,62 @@
     1.4  AC_HELP_STRING([--enable-nas], [support the NAS audio API [[default=yes]]]),
     1.5                    , enable_nas=yes)
     1.6      if test x$enable_audio = xyes -a x$enable_nas = xyes; then
     1.7 -        save_LDFLAGS="$LDFLAGS"
     1.8 -        LDFLAGS="$LDFLAGS -lXt -lm"
     1.9 -        AC_CHECK_HEADER(audio/audiolib.h, have_audio_hdr=yes)
    1.10 -        AC_CHECK_LIB(audio, AuOpenServer, have_audio_lib=yes)
    1.11 -        AC_CHECK_HEADER(nas/audiolib.h, have_nas_hdr=yes)
    1.12 -        AC_CHECK_LIB(nas, AuOpenServer, have_nas_lib=yes)
    1.13 -        LDFLAGS="$save_LDFLAGS"
    1.14 +        AC_CHECK_HEADER(audio/audiolib.h, have_nas_hdr=yes)
    1.15 +        AC_CHECK_LIB(audio, AuOpenServer, have_nas_lib=yes)
    1.16  
    1.17          AC_MSG_CHECKING(for NAS audio support)
    1.18          have_nas=no
    1.19 -        if test x$have_audio_hdr = xyes -a x$have_audio_lib = xyes; then
    1.20 +
    1.21 +        if test x$have_nas_hdr = xyes -a x$have_nas_lib = xyes; then
    1.22              have_nas=yes
    1.23 -            NAS_LIBS="-laudio -lXt"
    1.24 -        elif test x$have_nas_hdr = xyes -a x$have_nas_lib = xyes; then
    1.25 +            NAS_LIBS="-laudio"
    1.26 +
    1.27 +        elif test -r /usr/X11R6/include/audio/audiolib.h; then
    1.28 +            have_nas=yes
    1.29 +            NAS_CFLAGS="-I/usr/X11R6/include/"
    1.30 +            NAS_LIBS="-L/usr/X11R6/lib -laudio -lXt"
    1.31 +
    1.32 +        dnl On IRIX, the NAS includes are in a different directory,
    1.33 +        dnl and libnas must be explicitly linked in
    1.34 +
    1.35 +        elif test -r /usr/freeware/include/nas/audiolib.h; then
    1.36              have_nas=yes
    1.37              NAS_LIBS="-lnas -lXt"
    1.38 -	fi
    1.39 +        fi
    1.40 +
    1.41          AC_MSG_RESULT($have_nas)
    1.42  
    1.43          if test x$have_nas = xyes; then
    1.44 +            AC_ARG_ENABLE(nas-shared,
    1.45 +AC_HELP_STRING([--enable-nas-shared], [dynamically load NAS audio support [[default=yes]]]),
    1.46 +                          , enable_nas_shared=yes)
    1.47 +            if test "x`echo $NAS_LIBS | grep -- -L`" = "x"; then
    1.48 +                if test "x`ls /lib/libaudio.so.* 2> /dev/null`" != "x"; then
    1.49 +                    NAS_LIBS="-L/lib $NAS_LIBS"
    1.50 +                elif test "x`ls /usr/lib/libaudio.so.* 2> /dev/null`" != "x"; then
    1.51 +                    NAS_LIBS="-L/usr/lib $NAS_LIBS"
    1.52 +                elif test "x`ls /usr/local/lib/libaudio.so.* 2> /dev/null`" != "x"; then
    1.53 +                    NAS_LIBS="-L/usr/local/lib $NAS_LIBS"
    1.54 +                fi
    1.55 +            fi
    1.56 +            nas_lib_spec=`echo $NAS_LIBS | sed 's/.*-L\([[^ ]]*\).*/\1\/libaudio.so.*/'`
    1.57 +            nas_lib=`ls -- $nas_lib_spec | sed 's/.*\/\(.*\)/\1/; q'`
    1.58 +            echo "-- $nas_lib_spec -> $nas_lib"
    1.59 +
    1.60 +            if test x$have_loadso != xyes && \
    1.61 +               test x$enable_nas_shared = xyes; then
    1.62 +                AC_MSG_WARN([You must have SDL_LoadObject() support for dynamic NAS loading])
    1.63 +            fi
    1.64 +            if test x$have_loadso = xyes && \
    1.65 +               test x$enable_nas_shared = xyes && test x$nas_lib != x; then
    1.66 +                AC_DEFINE_UNQUOTED(SDL_AUDIO_DRIVER_NAS_DYNAMIC, "$nas_lib")
    1.67 +            else
    1.68 +                EXTRA_LDFLAGS="$EXTRA_LDFLAGS $NAS_LIBS"
    1.69 +            fi
    1.70 +
    1.71              AC_DEFINE(SDL_AUDIO_DRIVER_NAS)
    1.72              SOURCES="$SOURCES $srcdir/src/audio/nas/*.c"
    1.73              EXTRA_CFLAGS="$EXTRA_CFLAGS $NAS_CFLAGS"
    1.74 -            EXTRA_LDFLAGS="$EXTRA_LDFLAGS $NAS_LIBS"
    1.75              have_audio=yes
    1.76          fi
    1.77      fi
     2.1 --- a/include/SDL_config.h.in	Sat Sep 12 19:41:54 2009 +0000
     2.2 +++ b/include/SDL_config.h.in	Sun Sep 13 22:19:56 2009 +0000
     2.3 @@ -176,6 +176,7 @@
     2.4  #undef SDL_AUDIO_DRIVER_MINT
     2.5  #undef SDL_AUDIO_DRIVER_MMEAUDIO
     2.6  #undef SDL_AUDIO_DRIVER_NAS
     2.7 +#undef SDL_AUDIO_DRIVER_NAS_DYNAMIC
     2.8  #undef SDL_AUDIO_DRIVER_OSS
     2.9  #undef SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H
    2.10  #undef SDL_AUDIO_DRIVER_PAUD
     3.1 --- a/src/audio/nas/SDL_nasaudio.c	Sat Sep 12 19:41:54 2009 +0000
     3.2 +++ b/src/audio/nas/SDL_nasaudio.c	Sun Sep 13 22:19:56 2009 +0000
     3.3 @@ -37,11 +37,122 @@
     3.4  #include "../SDL_audiodev_c.h"
     3.5  #include "SDL_nasaudio.h"
     3.6  
     3.7 +#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
     3.8 +#include "SDL_loadso.h"
     3.9 +#endif
    3.10 +
    3.11  /* The tag name used by artsc audio */
    3.12  #define NAS_DRIVER_NAME         "nas"
    3.13  
    3.14  static struct SDL_PrivateAudioData *this2 = NULL;
    3.15  
    3.16 +static void (*NAS_AuCloseServer) (AuServer *);
    3.17 +static void (*NAS_AuNextEvent) (AuServer *, AuBool, AuEvent *);
    3.18 +static AuBool(*NAS_AuDispatchEvent) (AuServer *, AuEvent *);
    3.19 +static AuFlowID(*NAS_AuCreateFlow) (AuServer *, AuStatus *);
    3.20 +static void (*NAS_AuStartFlow) (AuServer *, AuFlowID, AuStatus *);
    3.21 +static void (*NAS_AuSetElements)
    3.22 +  (AuServer *, AuFlowID, AuBool, int, AuElement *, AuStatus *);
    3.23 +static void (*NAS_AuWriteElement)
    3.24 +  (AuServer *, AuFlowID, int, AuUint32, AuPointer, AuBool, AuStatus *);
    3.25 +static AuServer *(*NAS_AuOpenServer)
    3.26 +  (_AuConst char *, int, _AuConst char *, int, _AuConst char *, char **);
    3.27 +static AuEventHandlerRec *(*NAS_AuRegisterEventHandler)
    3.28 +  (AuServer *, AuMask, int, AuID, AuEventHandlerCallback, AuPointer);
    3.29 +
    3.30 +
    3.31 +#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
    3.32 +
    3.33 +static const char *nas_library = SDL_AUDIO_DRIVER_NAS_DYNAMIC;
    3.34 +static void *nas_handle = NULL;
    3.35 +
    3.36 +static int
    3.37 +load_nas_sym(const char *fn, void **addr)
    3.38 +{
    3.39 +    *addr = SDL_LoadFunction(nas_handle, fn);
    3.40 +    if (*addr == NULL) {
    3.41 +        return 0;
    3.42 +    }
    3.43 +    return 1;
    3.44 +}
    3.45 +
    3.46 +/* cast funcs to char* first, to please GCC's strict aliasing rules. */
    3.47 +#define SDL_NAS_SYM(x) \
    3.48 +    if (!load_nas_sym(#x, (void **) (char *) &NAS_##x)) return -1
    3.49 +#else
    3.50 +#define SDL_NAS_SYM(x) NAS_##x = x
    3.51 +#endif
    3.52 +
    3.53 +static int
    3.54 +load_nas_syms(void)
    3.55 +{
    3.56 +    SDL_NAS_SYM(AuCloseServer);
    3.57 +    SDL_NAS_SYM(AuNextEvent);
    3.58 +    SDL_NAS_SYM(AuDispatchEvent);
    3.59 +    SDL_NAS_SYM(AuCreateFlow);
    3.60 +    SDL_NAS_SYM(AuStartFlow);
    3.61 +    SDL_NAS_SYM(AuSetElements);
    3.62 +    SDL_NAS_SYM(AuWriteElement);
    3.63 +    SDL_NAS_SYM(AuOpenServer);
    3.64 +    SDL_NAS_SYM(AuRegisterEventHandler);
    3.65 +    return 0;
    3.66 +}
    3.67 +
    3.68 +#undef SDL_NAS_SYM
    3.69 +
    3.70 +#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
    3.71 +
    3.72 +static void
    3.73 +UnloadNASLibrary(void)
    3.74 +{
    3.75 +    if (nas_handle != NULL) {
    3.76 +        SDL_UnloadObject(nas_handle);
    3.77 +        nas_handle = NULL;
    3.78 +    }
    3.79 +}
    3.80 +
    3.81 +static int
    3.82 +LoadNASLibrary(void)
    3.83 +{
    3.84 +    int retval = 0;
    3.85 +    if (nas_handle == NULL) {
    3.86 +        nas_handle = SDL_LoadObject(nas_library);
    3.87 +        if (nas_handle == NULL) {
    3.88 +            /* Copy error string so we can use it in a new SDL_SetError(). */
    3.89 +            char *origerr = SDL_GetError();
    3.90 +            size_t len = SDL_strlen(origerr) + 1;
    3.91 +            char *err = (char *) alloca(len);
    3.92 +            SDL_strlcpy(err, origerr, len);
    3.93 +            retval = -1;
    3.94 +            SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s\n",
    3.95 +                         nas_library, err);
    3.96 +        } else {
    3.97 +            retval = load_nas_syms();
    3.98 +            if (retval < 0) {
    3.99 +                UnloadNASLibrary();
   3.100 +            }
   3.101 +        }
   3.102 +    }
   3.103 +    return retval;
   3.104 +}
   3.105 +
   3.106 +#else
   3.107 +
   3.108 +static void
   3.109 +UnloadNASLibrary(void)
   3.110 +{
   3.111 +}
   3.112 +
   3.113 +static int
   3.114 +LoadNASLibrary(void)
   3.115 +{
   3.116 +    load_nas_syms();
   3.117 +    return 0;
   3.118 +}
   3.119 +
   3.120 +#endif /* SDL_AUDIO_DRIVER_NAS_DYNAMIC */
   3.121 +
   3.122 +
   3.123  /* Audio driver functions */
   3.124  static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec);
   3.125  static void NAS_WaitAudio(_THIS);
   3.126 @@ -53,15 +164,22 @@
   3.127  
   3.128  static int Audio_Available(void)
   3.129  {
   3.130 -	AuServer *aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
   3.131 -	if (!aud) return 0;
   3.132 -
   3.133 -	AuCloseServer(aud);
   3.134 -	return 1;
   3.135 +	if (LoadNASLibrary() == 0) {
   3.136 +		AuServer *aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
   3.137 +		if (!aud) {
   3.138 +			UnloadNASLibrary();
   3.139 +			return 0;
   3.140 +		}
   3.141 +		NAS_AuCloseServer(aud);
   3.142 +		UnloadNASLibrary();
   3.143 +		return 1;
   3.144 +	}
   3.145 +	return 0;
   3.146  }
   3.147  
   3.148  static void Audio_DeleteDevice(SDL_AudioDevice *device)
   3.149  {
   3.150 +	UnloadNASLibrary();
   3.151  	SDL_free(device->hidden);
   3.152  	SDL_free(device);
   3.153  }
   3.154 @@ -70,6 +188,10 @@
   3.155  {
   3.156  	SDL_AudioDevice *this;
   3.157  
   3.158 +	if (LoadNASLibrary() < 0) {
   3.159 +		return NULL;
   3.160 +	}
   3.161 +
   3.162  	/* Initialize all variables that we clean on shutdown */
   3.163  	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
   3.164  	if ( this ) {
   3.165 @@ -82,7 +204,7 @@
   3.166  		if ( this ) {
   3.167  			SDL_free(this);
   3.168  		}
   3.169 -		return(0);
   3.170 +		return NULL;
   3.171  	}
   3.172  	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   3.173  
   3.174 @@ -108,8 +230,8 @@
   3.175  {
   3.176  	while ( this->hidden->buf_free < this->hidden->mixlen ) {
   3.177  		AuEvent ev;
   3.178 -		AuNextEvent(this->hidden->aud, AuTrue, &ev);
   3.179 -		AuDispatchEvent(this->hidden->aud, &ev);
   3.180 +		NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
   3.181 +		NAS_AuDispatchEvent(this->hidden->aud, &ev);
   3.182  	}
   3.183  }
   3.184  
   3.185 @@ -119,13 +241,13 @@
   3.186  				    in the hope that some of them is LowWater events telling us more
   3.187  				    of the buffer is free now than what we think. */
   3.188  		AuEvent ev;
   3.189 -		AuNextEvent(this->hidden->aud, AuTrue, &ev);
   3.190 -		AuDispatchEvent(this->hidden->aud, &ev);
   3.191 +		NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
   3.192 +		NAS_AuDispatchEvent(this->hidden->aud, &ev);
   3.193  	}
   3.194  	this->hidden->buf_free -= this->hidden->mixlen;
   3.195  
   3.196  	/* Write the audio data */
   3.197 -	AuWriteElement(this->hidden->aud, this->hidden->flow, 0, this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
   3.198 +	NAS_AuWriteElement(this->hidden->aud, this->hidden->flow, 0, this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
   3.199  
   3.200  	this->hidden->written += this->hidden->mixlen;
   3.201  	
   3.202 @@ -146,7 +268,7 @@
   3.203  		this->hidden->mixbuf = NULL;
   3.204  	}
   3.205  	if ( this->hidden->aud ) {
   3.206 -		AuCloseServer(this->hidden->aud);
   3.207 +		NAS_AuCloseServer(this->hidden->aud);
   3.208  		this->hidden->aud = 0;
   3.209  	}
   3.210  }
   3.211 @@ -211,6 +333,7 @@
   3.212  static AuDeviceID
   3.213  find_device(_THIS, int nch)
   3.214  {
   3.215 +    /* These "Au" things are all macros, not functions... */
   3.216  	int i;
   3.217  	for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
   3.218  		if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
   3.219 @@ -246,7 +369,7 @@
   3.220  	}
   3.221  	spec->format = test_format;
   3.222  
   3.223 -	this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
   3.224 +	this->hidden->aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
   3.225  	if (this->hidden->aud == 0)
   3.226  	{
   3.227  		SDL_SetError("Couldn't open connection to NAS server");
   3.228 @@ -254,8 +377,8 @@
   3.229  	}
   3.230  	
   3.231  	this->hidden->dev = find_device(this, spec->channels);
   3.232 -	if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) {
   3.233 -		AuCloseServer(this->hidden->aud);
   3.234 +	if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = NAS_AuCreateFlow(this->hidden->aud, NULL)))) {
   3.235 +		NAS_AuCloseServer(this->hidden->aud);
   3.236  		this->hidden->aud = 0;
   3.237  		SDL_SetError("Couldn't find a fitting playback device on NAS server");
   3.238  		return (-1);
   3.239 @@ -273,15 +396,16 @@
   3.240  
   3.241  	this2 = this->hidden;
   3.242  
   3.243 +    /* These "Au" things without a NAS_ prefix are macros, not functions... */
   3.244  	AuMakeElementImportClient(elms, spec->freq, format, spec->channels, AuTrue,
   3.245  				buffer_size, buffer_size / 4, 0, NULL);
   3.246  	AuMakeElementExportDevice(elms+1, 0, this->hidden->dev, spec->freq,
   3.247  				AuUnlimitedSamples, 0, NULL);
   3.248 -	AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
   3.249 -	AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow,
   3.250 +	NAS_AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
   3.251 +	NAS_AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow,
   3.252  				event_handler, (AuPointer) NULL);
   3.253  
   3.254 -	AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
   3.255 +	NAS_AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
   3.256  
   3.257  	/* Allocate mixing buffer */
   3.258  	this->hidden->mixlen = spec->size;