19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to an ESD network stream mixing buffer */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <esd.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_esdaudio.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_name.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_loadso.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SDL_NAME(X)	X
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by ESD audio */
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ESD_DRIVER_NAME		"esd"
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ESD_OpenAudio(_THIS, SDL_AudioSpec *spec);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ESD_WaitAudio(_THIS);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ESD_PlayAudio(_THIS);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *ESD_GetAudioBuf(_THIS);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ESD_CloseAudio(_THIS);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void *esd_handle = NULL;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int esd_loaded = 0;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(esd_open_sound))( const char *host );
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(esd_close))( int esd );
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(esd_play_stream))( esd_format_t format, int rate,
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                         const char *host, const char *name );
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct {
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *name;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void **func;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} esd_functions[] = {
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "esd_open_sound",	(void **)&SDL_NAME(esd_open_sound)	},
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "esd_close",		(void **)&SDL_NAME(esd_close)		},
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "esd_play_stream",	(void **)&SDL_NAME(esd_play_stream)	},
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UnloadESDLibrary()
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( esd_loaded ) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnloadObject(esd_handle);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		esd_handle = NULL;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		esd_loaded = 0;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int LoadESDLibrary(void)
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, retval = -1;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	esd_handle = SDL_LoadObject(esd_library);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( esd_handle ) {
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		esd_loaded = 1;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 0;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<SDL_arraysize(esd_functions); ++i ) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*esd_functions[i].func = SDL_LoadFunction(esd_handle, esd_functions[i].name);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( !*esd_functions[i].func ) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				retval = -1;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				UnloadESDLibrary();
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UnloadESDLibrary()
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int LoadESDLibrary(void)
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int connection;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int available;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	available = 0;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( LoadESDLibrary() < 0 ) {
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return available;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	connection = SDL_NAME(esd_open_sound)(NULL);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( connection >= 0 ) {
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		available = 1;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(esd_close)(connection);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnloadESDLibrary();
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(available);
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnloadESDLibrary();
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LoadESDLibrary();
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fd = -1;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = ESD_OpenAudio;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = ESD_WaitAudio;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = ESD_PlayAudio;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = ESD_GetAudioBuf;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = ESD_CloseAudio;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = Audio_DeleteDevice;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap ESD_bootstrap = {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ESD_DRIVER_NAME, "Enlightened Sound Daemon",
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ESD_WaitAudio(_THIS)
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 ticks;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if the thread-parent process is still alive */
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ static int cnt = 0;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Note that this only works with thread implementations
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   that use a different process id for each thread.
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( kill(parent, 0) < 0 ) {
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				this->enabled = 0;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Use timer for general audio synchronization */
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ticks > 0 ) {
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Delay(ticks);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ESD_PlayAudio(_THIS)
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int written;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Write the audio data, checking for EAGAIN on broken audio drivers */
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		written = write(audio_fd, mixbuf, mixlen);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Delay(1);	/* Let a little CPU time go by */
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} while ( (written < 0) &&
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the next write frame */
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_frame += frame_ticks;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we couldn't write, assume fatal error for now */
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( written < 0 ) {
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->enabled = 0;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *ESD_GetAudioBuf(_THIS)
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(mixbuf);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ESD_CloseAudio(_THIS)
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf != NULL ) {
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeAudioMem(mixbuf);
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mixbuf = NULL;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( audio_fd >= 0 ) {
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(esd_close)(audio_fd);
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		audio_fd = -1;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Try to get the name of the program */
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *get_progname(void)
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *progname = NULL;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __LINUX__
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FILE *fp;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static char temp[BUFSIZ];
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fp = fopen(temp, "r");
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fp != NULL ) {
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( fgets(temp, sizeof(temp)-1, fp) ) {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			progname = SDL_strrchr(temp, '/');
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( progname == NULL ) {
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				progname = temp;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				progname = progname+1;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fclose(fp);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(progname);
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ESD_OpenAudio(_THIS, SDL_AudioSpec *spec)
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	esd_format_t format;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Convert audio spec to the ESD audio format */
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format = (ESD_STREAM | ESD_PLAY);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch ( spec->format & 0xFF ) {
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 8:
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format |= ESD_BITS8;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 16:
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format |= ESD_BITS16;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unsupported ESD audio format");
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( spec->channels == 1 ) {
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format |= ESD_MONO;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format |= ESD_STEREO;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->samples = ESD_BUF_SIZE;	/* Darn, no way to change this yet */
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Open a connection to the ESD audio server */
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fd = SDL_NAME(esd_play_stream)(format, spec->freq, NULL, get_progname());
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( audio_fd < 0 ) {
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't open ESD connection");
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Calculate the final parameters for this audio specification */
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	frame_ticks = (float)(spec->samples*1000)/spec->freq;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_frame = SDL_GetTicks()+frame_ticks;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mixing buffer */
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixlen = spec->size;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf == NULL ) {
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(mixbuf, spec->silence, spec->size);
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the parent process id (we're the parent of the audio thread) */
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	parent = getpid();
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to rock and roll. :-) */
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
324