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    Stéphan Kochen
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    stephan@kochen.nl
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Based on parts of the ALSA and ESounD output drivers.
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to an PulseAudio network stream mixing buffer */
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pulse/pulseaudio.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pulse/simple.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../../include/SDL_video.h"  /* for SDL_WM_GetCaption(). */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_pulseaudio.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_AUDIO_DRIVER_PULSE_DYNAMIC
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_name.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_loadso.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SDL_NAME(X)	X
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by the driver */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define PULSE_DRIVER_NAME	"pulse"
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_WaitAudio(_THIS);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_PlayAudio(_THIS);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *PULSE_GetAudioBuf(_THIS);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_CloseAudio(_THIS);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_WaitDone(_THIS);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_SetCaption(_THIS, const char *str);
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_AUDIO_DRIVER_PULSE_DYNAMIC
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *pulse_library = SDL_AUDIO_DRIVER_PULSE_DYNAMIC;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void *pulse_handle = NULL;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int pulse_loaded = 0;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_simple* (*SDL_NAME(pa_simple_new))(
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *server,
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *name,
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_stream_direction_t dir,
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *dev,
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *stream_name,
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const pa_sample_spec *ss,
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const pa_channel_map *map,
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const pa_buffer_attr *attr,
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int *error
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(pa_simple_free))(pa_simple *s);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_channel_map* (*SDL_NAME(pa_channel_map_init_auto))(
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_channel_map *m,
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned channels,
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_channel_map_def_t def
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall);
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_mainloop * (*SDL_NAME(pa_mainloop_new))(void);
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_mainloop_api * (*SDL_NAME(pa_mainloop_get_api))(pa_mainloop *m);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(pa_mainloop_iterate))(pa_mainloop *m, int block, int *retval);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(pa_mainloop_free))(pa_mainloop *m);
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_operation_state_t (*SDL_NAME(pa_operation_get_state))(pa_operation *o);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(pa_operation_cancel))(pa_operation *o);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(pa_operation_unref))(pa_operation *o);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_context * (*SDL_NAME(pa_context_new))(
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_mainloop_api *m, const char *name);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(pa_context_connect))(
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_context *c, const char *server,
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_context_flags_t flags, const pa_spawn_api *api);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_context_state_t (*SDL_NAME(pa_context_get_state))(pa_context *c);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(pa_context_disconnect))(pa_context *c);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(pa_context_unref))(pa_context *c);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_stream * (*SDL_NAME(pa_stream_new))(pa_context *c,
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *name, const pa_sample_spec *ss, const pa_channel_map *map);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(pa_stream_connect_playback))(pa_stream *s, const char *dev,
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const pa_buffer_attr *attr, pa_stream_flags_t flags,
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_cvolume *volume, pa_stream *sync_stream);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_stream_state_t (*SDL_NAME(pa_stream_get_state))(pa_stream *s);
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t (*SDL_NAME(pa_stream_writable_size))(pa_stream *s);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(pa_stream_write))(pa_stream *s, const void *data, size_t nbytes,
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_free_cb_t free_cb, int64_t offset, pa_seek_mode_t seek);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_operation * (*SDL_NAME(pa_stream_drain))(pa_stream *s,
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_stream_success_cb_t cb, void *userdata);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(pa_stream_disconnect))(pa_stream *s);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(pa_stream_unref))(pa_stream *s);
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pa_operation* (*SDL_NAME(pa_context_set_name))(pa_context *c,
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *name, pa_context_success_cb_t cb, void *userdata);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *name;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void **func;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} pulse_functions[] = {
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_simple_new",
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_simple_new)		},
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_simple_free",
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_simple_free)		},
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_channel_map_init_auto",
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_channel_map_init_auto)	},
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_mainloop_new",
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_mainloop_new)		},
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_mainloop_get_api",
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_mainloop_get_api)		},
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_mainloop_iterate",
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_mainloop_iterate)		},
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_mainloop_free",
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_mainloop_free)		},
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_operation_get_state",
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_operation_get_state)	},
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_operation_cancel",
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_operation_cancel)		},
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_operation_unref",
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_operation_unref)		},
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_context_new",
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_context_new)		},
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_context_connect",
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_context_connect)		},
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_context_get_state",
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_context_get_state)	},
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_context_disconnect",
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_context_disconnect)	},
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_context_unref",
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_context_unref)		},
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_new",
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_new)		},
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_connect_playback",
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_connect_playback)	},
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_get_state",
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_get_state)		},
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_writable_size",
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_writable_size)	},
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_write",
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_write)		},
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_drain",
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_drain)		},
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_disconnect",
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_disconnect)	},
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_stream_unref",
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_stream_unref)		},
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "pa_context_set_name",
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void **)&SDL_NAME(pa_context_set_name)		},
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UnloadPulseLibrary()
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pulse_loaded ) {
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnloadObject(pulse_handle);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pulse_handle = NULL;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pulse_loaded = 0;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int LoadPulseLibrary(void)
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, retval = -1;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pulse_handle = SDL_LoadObject(pulse_library);
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pulse_handle ) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pulse_loaded = 1;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 0;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<SDL_arraysize(pulse_functions); ++i ) {
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*pulse_functions[i].func = SDL_LoadFunction(pulse_handle, pulse_functions[i].name);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( !*pulse_functions[i].func ) {
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				retval = -1;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				UnloadPulseLibrary();
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UnloadPulseLibrary()
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int LoadPulseLibrary(void)
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_AUDIO_DRIVER_PULSE_DYNAMIC */
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_sample_spec paspec;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_simple *connection;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int available;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	available = 0;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( LoadPulseLibrary() < 0 ) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return available;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Connect with a dummy format. */
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paspec.format = PA_SAMPLE_U8;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paspec.rate = 11025;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paspec.channels = 1;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	connection = SDL_NAME(pa_simple_new)(
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NULL,                        /* server */
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		"Test stream",               /* application name */
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PA_STREAM_PLAYBACK,          /* playback mode */
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NULL,                        /* device on the server */
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		"Simple DirectMedia Layer",  /* stream description */
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&paspec,                     /* sample format spec */
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NULL,                        /* channel map */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NULL,                        /* buffering attributes */
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NULL                         /* error code */
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( connection != NULL ) {
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		available = 1;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(pa_simple_free)(connection);
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnloadPulseLibrary();
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(available);
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden->caption);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnloadPulseLibrary();
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LoadPulseLibrary();
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = PULSE_OpenAudio;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = PULSE_WaitAudio;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = PULSE_PlayAudio;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = PULSE_GetAudioBuf;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = PULSE_CloseAudio;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitDone = PULSE_WaitDone;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->SetCaption = PULSE_SetCaption;
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = Audio_DeleteDevice;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap PULSE_bootstrap = {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PULSE_DRIVER_NAME, "PulseAudio",
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_WaitAudio(_THIS)
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int size;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(1) {
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_NAME(pa_context_get_state)(context) != PA_CONTEXT_READY ||
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    SDL_NAME(pa_stream_get_state)(stream) != PA_STREAM_READY ||
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			this->enabled = 0;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		size = SDL_NAME(pa_stream_writable_size)(stream);
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (size >= mixlen)
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_PlayAudio(_THIS)
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Write the audio data */
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_NAME(pa_stream_write)(stream, mixbuf, mixlen, NULL, 0LL, PA_SEEK_RELATIVE) < 0)
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->enabled = 0;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *PULSE_GetAudioBuf(_THIS)
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(mixbuf);
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_CloseAudio(_THIS)
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf != NULL ) {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeAudioMem(mixbuf);
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mixbuf = NULL;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( stream != NULL ) {
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(pa_stream_disconnect)(stream);
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(pa_stream_unref)(stream);
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		stream = NULL;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (context != NULL) {
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(pa_context_disconnect)(context);
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(pa_context_unref)(context);
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		context = NULL;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (mainloop != NULL) {
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(pa_mainloop_free)(mainloop);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mainloop = NULL;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Try to get the name of the program */
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *get_progname(void)
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __LINUX__
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *progname = NULL;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FILE *fp;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static char temp[BUFSIZ];
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fp = fopen(temp, "r");
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fp != NULL ) {
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( fgets(temp, sizeof(temp)-1, fp) ) {
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			progname = SDL_strrchr(temp, '/');
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( progname == NULL ) {
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				progname = temp;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				progname = progname+1;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fclose(fp);
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(progname);
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif defined(__NetBSD__)
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return getprogname();
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return("unknown");
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void caption_set_complete(pa_context *c, int success, void *userdata)
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* no-op. */
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_SetCaption(_THIS, const char *str)
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(this->hidden->caption);
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((str == NULL) || (*str == '\0')) {
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		str = get_progname();  /* set a default so SOMETHING shows up. */
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->caption = SDL_strdup(str);
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (context != NULL) {
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(pa_context_set_name)(context, this->hidden->caption,
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                              caption_set_complete, 0);
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void stream_drain_complete(pa_stream *s, int success, void *userdata)
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* no-op. */
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PULSE_WaitDone(_THIS)
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_operation *o;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	o = SDL_NAME(pa_stream_drain)(stream, stream_drain_complete, NULL);
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!o)
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (SDL_NAME(pa_operation_get_state)(o) != PA_OPERATION_DONE) {
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_NAME(pa_context_get_state)(context) != PA_CONTEXT_READY ||
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    SDL_NAME(pa_stream_get_state)(stream) != PA_STREAM_READY ||
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_NAME(pa_operation_cancel)(o);
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(pa_operation_unref)(o);
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec)
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int             state;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16          test_format;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_sample_spec  paspec;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_buffer_attr  paattr;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_channel_map  pacmap;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pa_stream_flags_t flags = 0;
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paspec.format = PA_SAMPLE_INVALID;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( test_format = SDL_FirstAudioFormat(spec->format); test_format; ) {
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch ( test_format ) {
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_U8:
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				paspec.format = PA_SAMPLE_U8;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_S16LSB:
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				paspec.format = PA_SAMPLE_S16LE;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_S16MSB:
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				paspec.format = PA_SAMPLE_S16BE;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( paspec.format != PA_SAMPLE_INVALID )
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		test_format = SDL_NextAudioFormat();
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (paspec.format == PA_SAMPLE_INVALID ) {
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't find any suitable audio formats");
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->format = test_format;
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paspec.channels = spec->channels;
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paspec.rate = spec->freq;
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Calculate the final parameters for this audio specification */
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef PA_STREAM_ADJUST_LATENCY
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->samples /= 2; /* Mix in smaller chunck to avoid underruns */
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mixing buffer */
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixlen = spec->size;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf == NULL ) {
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(mixbuf, spec->silence, spec->size);
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Reduced prebuffering compared to the defaults. */
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef PA_STREAM_ADJUST_LATENCY
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.tlength = mixlen * 4; /* 2x original requested bufsize */
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.prebuf = -1;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.maxlength = -1;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.minreq = mixlen; /* -1 can lead to pa_stream_writable_size()
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				   >= mixlen never becoming true */
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	flags = PA_STREAM_ADJUST_LATENCY;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.tlength = mixlen*2;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.prebuf = mixlen*2;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.maxlength = mixlen*2;
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paattr.minreq = mixlen;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The SDL ALSA output hints us that we use Windows' channel mapping */
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(pa_channel_map_init_auto)(
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&pacmap, spec->channels, PA_CHANNEL_MAP_WAVEEX);
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up a new main loop */
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!(mainloop = SDL_NAME(pa_mainloop_new)())) {
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PULSE_CloseAudio(this);
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pa_mainloop_new() failed");
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (this->hidden->caption == NULL) {
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *title = NULL;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WM_GetCaption(&title, NULL);
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PULSE_SetCaption(this, title);
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mainloop_api = SDL_NAME(pa_mainloop_get_api)(mainloop);
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!(context = SDL_NAME(pa_context_new)(mainloop_api,
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                         this->hidden->caption))) {
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PULSE_CloseAudio(this);
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("pa_context_new() failed");
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Connect to the PulseAudio server */
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_NAME(pa_context_connect)(context, NULL, 0, NULL) < 0) {
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PULSE_CloseAudio(this);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Could not setup connection to PulseAudio");
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do {
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PULSE_CloseAudio(this);
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("pa_mainloop_iterate() failed");
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		state = SDL_NAME(pa_context_get_state)(context);
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!PA_CONTEXT_IS_GOOD(state)) {
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PULSE_CloseAudio(this);
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Could not connect to PulseAudio");
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} while (state != PA_CONTEXT_READY);
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	stream = SDL_NAME(pa_stream_new)(
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		context,
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		"Simple DirectMedia Layer",  /* stream description */
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&paspec,                     /* sample format spec */
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&pacmap                      /* channel map */
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	);
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( stream == NULL ) {
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PULSE_CloseAudio(this);
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Could not setup PulseAudio stream");
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_NAME(pa_stream_connect_playback)(stream, NULL, &paattr, flags,
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			NULL, NULL) < 0) {
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PULSE_CloseAudio(this);
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Could not connect PulseAudio stream");
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do {
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PULSE_CloseAudio(this);
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("pa_mainloop_iterate() failed");
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		state = SDL_NAME(pa_stream_get_state)(stream);
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!PA_STREAM_IS_GOOD(state)) {
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PULSE_CloseAudio(this);
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Could not create to PulseAudio stream");
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} while (state != PA_STREAM_READY);
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
571