From 4ef9a57355a6bede99e3626d92034382cbdfaebb Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Wed, 5 Oct 2011 22:01:47 +0100 Subject: tinyplay: add clean shutdown handler for ctrl-c Shutdown gracefully on ctrl-c signals --- tinyplay.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'tinyplay.c') diff --git a/tinyplay.c b/tinyplay.c index cc35170..d7e7d46 100644 --- a/tinyplay.c +++ b/tinyplay.c @@ -31,6 +31,7 @@ #include #include #include +#include #define ID_RIFF 0x46464952 #define ID_WAVE 0x45564157 @@ -57,10 +58,19 @@ struct chunk_fmt { uint16_t bits_per_sample; }; +static int close = 0; + void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels, unsigned int rate, unsigned int bits, unsigned int period_size, unsigned int period_count); +void stream_close(int sig) +{ + /* allow the stream to be closed gracefully */ + signal(sig, SIG_IGN); + close = 1; +} + int main(int argc, char **argv) { FILE *file; @@ -190,6 +200,9 @@ void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned in printf("Playing sample: %u ch, %u hz, %u bit\n", channels, rate, bits); + /* catch ctrl-c to shutdown cleanly */ + signal(SIGINT, stream_close); + do { num_read = fread(buffer, 1, size, file); if (num_read > 0) { @@ -198,7 +211,7 @@ void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned in break; } } - } while (num_read > 0); + } while (!close && num_read > 0); free(buffer); pcm_close(pcm); -- cgit v1.2.3