diff options
author | Liam Girdwood <lrg@ti.com> | 2011-10-05 22:01:47 +0100 |
---|---|---|
committer | Simon Wilson <simonwilson@google.com> | 2012-11-09 14:02:15 -0800 |
commit | 4ef9a57355a6bede99e3626d92034382cbdfaebb (patch) | |
tree | 12d4f8f00be0fcf92d1c3b0d017f53f7ecc670c2 | |
parent | 9bb806603cb687e54b4376f00e679f17c6be0049 (diff) |
tinyplay: add clean shutdown handler for ctrl-c
Shutdown gracefully on ctrl-c signals
-rw-r--r-- | tinyplay.c | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -31,6 +31,7 @@ #include <stdlib.h> #include <stdint.h> #include <string.h> +#include <signal.h> #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); |