From d6dd7a793878948255546eee412e0793ba438fe7 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 9 Jan 2010 13:27:05 -0800 Subject: [PATCH] Make fm report when the radio is muted and tell the user how to unmute it. --- fm.c | 23 ++++++++++++++++++----- fmlib.c | 28 +++++++++++++++++++++++----- fmlib.h | 1 + test-fm.at | 17 ++++++++++++++--- 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/fm.c b/fm.c index 1fabbf6..61aaaec 100644 --- a/fm.c +++ b/fm.c @@ -208,9 +208,16 @@ static void print_volume(const struct tuner *tuner) { if (tuner_has_volume_control(tuner)) - printf(" at %.2f%% volume\n", tuner_get_volume(tuner)); + printf(" at %.2f%% volume", tuner_get_volume(tuner)); else - printf(" (radio does not support volume control)\n"); + printf(" (radio does not support volume control)"); +} + +static void +print_mute(const struct tuner *tuner) +{ + if (tuner_is_muted(tuner)) + printf(" (radio is muted, use \"fm on\" to unmute)"); } int main(int argc, char **argv) @@ -286,6 +293,7 @@ int main(int argc, char **argv) if (!quiet) { printf("Radio on"); print_volume(&tuner); + putchar('\n'); } } else if (!strcmp(argv[0], "+") || !strcmp(argv[0], "-")) { double new_volume; @@ -299,10 +307,13 @@ int main(int argc, char **argv) new_volume -= increment; new_volume = clamp(new_volume); - if (!quiet) - printf("Setting volume to %.2f%%\n", new_volume); - tuner_set_volume(&tuner, new_volume); + + if (!quiet) { + printf("Setting volume to %.2f%%", new_volume); + print_mute(&tuner); + putchar('\n'); + } } else if (atof(argv[0])) { double frequency = atof(argv[0]); double volume = argc > 1 ? clamp(atof(argv[1])) : defaultvol; @@ -311,6 +322,8 @@ int main(int argc, char **argv) if (!quiet) { printf("Radio tuned to %2.2f MHz", frequency); print_volume(&tuner); + print_mute(&tuner); + putchar('\n'); } } else { fatal(0, "unrecognized command syntax; use --help for help"); diff --git a/fmlib.c b/fmlib.c index 1d0922a..60bb9d1 100644 --- a/fmlib.c +++ b/fmlib.c @@ -30,6 +30,7 @@ struct tuner_test { int freq; /* In 1/16 MHz. */ int volume; /* Between 1000 and 2000. */ + bool mute; /* Muted? */ }; char *program_name; @@ -76,13 +77,19 @@ tuner_open(struct tuner *tuner, const char *device, int index) device = "/dev/radio0"; else if (!strcmp(device, "test") || !strncmp(device, "test ", 5)) { double volume; + int mute; + int n; - if (sscanf(device, "test %lf", &volume) != 1) + n = sscanf(device, "test %lf %d", &volume, &mute); + if (n < 1) volume = 50; + if (n < 2) + mute = 0; tuner->test = xmalloc(sizeof *tuner->test); tuner->test->freq = 90 * 16; tuner->test->volume = volume >= 0 ? volume * 10 + 1000.5 : 0; + tuner->test->mute = mute != 0; device = "/dev/null"; } @@ -102,6 +109,12 @@ tuner_close(struct tuner *tuner) close(tuner->fd); } +bool +tuner_is_muted(const struct tuner *tuner) +{ + return get_control(tuner, V4L2_CID_AUDIO_MUTE); +} + void tuner_set_mute(struct tuner *tuner, bool mute) { @@ -235,8 +248,12 @@ get_control(const struct tuner *tuner, uint32_t id) memset(&control, 0, sizeof control); control.id = id; if (tuner->test) { - assert(id == V4L2_CID_AUDIO_VOLUME); - control.value = tuner->test->volume; + if (id == V4L2_CID_AUDIO_VOLUME) + control.value = tuner->test->volume; + else if (id == V4L2_CID_AUDIO_MUTE) + control.value = tuner->test->mute; + else + abort(); } else if (ioctl(tuner->fd, VIDIOC_G_CTRL, &control) == -1) fatal(errno, "VIDIOC_G_CTRL"); return control.value; @@ -251,9 +268,10 @@ set_control(const struct tuner *tuner, uint32_t id, int32_t value) control.id = id; control.value = value; if (tuner->test) { - if (id == V4L2_CID_AUDIO_MUTE) + if (id == V4L2_CID_AUDIO_MUTE) { assert(value == 0 || value == 1); - else if (id == V4L2_CID_AUDIO_VOLUME) { + tuner->test->mute = value; + } else if (id == V4L2_CID_AUDIO_VOLUME) { assert(value >= 1000 && value <= 2000); tuner->test->volume = value; } else { diff --git a/fmlib.h b/fmlib.h index 9a65474..b0c7de6 100644 --- a/fmlib.h +++ b/fmlib.h @@ -39,6 +39,7 @@ void fatal(int error, const char *msg, ...) void tuner_open(struct tuner *, const char *device, int index); void tuner_close(struct tuner *); +bool tuner_is_muted(const struct tuner *); void tuner_set_mute(struct tuner *, bool mute); bool tuner_has_volume_control(const struct tuner *); diff --git a/test-fm.at b/test-fm.at index 86d000c..4a70f10 100644 --- a/test-fm.at +++ b/test-fm.at @@ -35,6 +35,12 @@ AT_CHECK([RUN_FM +], [0], [Setting volume to 60.00% ]) AT_CLEANUP +AT_SETUP(["+" command, in-range (muted)]) +AT_CHECK([RUN_FM([50 1]) +], [0], + [Setting volume to 60.00% (radio is muted, use "fm on" to unmute) +]) +AT_CLEANUP + AT_SETUP(["+" command, in-range (quiet)]) AT_CHECK([RUN_FM -q +]) AT_CLEANUP @@ -82,9 +88,14 @@ AT_CHECK([RUN_FM 90], [0], [Radio tuned to 90.00 MHz at 12.50% volume ]) AT_CLEANUP -AT_SETUP([tune valid freq, no volume control]) -AT_CHECK([RUN_FM([-1]) 90], [0], - [Radio tuned to 90.00 MHz (radio does not support volume control) +AT_SETUP([tune valid freq, default volume]) +AT_CHECK([RUN_FM 90], [0], [Radio tuned to 90.00 MHz at 12.50% volume +]) +AT_CLEANUP + +AT_SETUP([tune valid freq, muted]) +AT_CHECK([RUN_FM([50 1]) 90], [0], + [Radio tuned to 90.00 MHz at 12.50% volume (radio is muted, use "fm on" to unmute) ]) AT_CLEANUP -- 2.11.4.GIT