aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLiam Girdwood <lrg@ti.com>2011-10-05 22:01:47 +0100
committerSimon Wilson <simonwilson@google.com>2012-11-09 14:02:15 -0800
commit4ef9a57355a6bede99e3626d92034382cbdfaebb (patch)
tree12d4f8f00be0fcf92d1c3b0d017f53f7ecc670c2
parent9bb806603cb687e54b4376f00e679f17c6be0049 (diff)
tinyplay: add clean shutdown handler for ctrl-c
Shutdown gracefully on ctrl-c signals
-rw-r--r--tinyplay.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/tinyplay.c b/tinyplay.c
index cc35170..d7e7d46 100644
--- a/tinyplay.c
+++ b/tinyplay.c
@@ -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);