From d3b5574d7aac0a7f2d4eb785facfad9f37c15dd5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 6 Jul 2009 21:52:10 +0200 Subject: volume: moved range check to handle_setvol() Converted the range checks in volume_level_change() to assertions. Changed all volume types to "unsigned", expect for those which must be able to indicate error (-1). --- src/command.c | 5 +++++ src/mixer_all.c | 12 +++++------- src/mixer_all.h | 2 +- src/volume.c | 19 +++++++++---------- src/volume.h | 2 +- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/command.c b/src/command.c index 9f725edf1..dda75a1b3 100644 --- a/src/command.c +++ b/src/command.c @@ -1061,6 +1061,11 @@ handle_setvol(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) if (!check_int(client, &level, argv[1], need_integer)) return COMMAND_RETURN_ERROR; + if (level < 0 || level > 100) { + command_error(client, ACK_ERROR_ARG, "Invalid volume value"); + return COMMAND_RETURN_ERROR; + } + success = volume_level_change(level); if (!success) { command_error(client, ACK_ERROR_SYSTEM, diff --git a/src/mixer_all.c b/src/mixer_all.c index 21ff44c35..980f854a2 100644 --- a/src/mixer_all.c +++ b/src/mixer_all.c @@ -70,12 +70,13 @@ mixer_all_get_volume(void) } static bool -output_mixer_set_volume(unsigned i, int volume) +output_mixer_set_volume(unsigned i, unsigned volume) { struct audio_output *output; struct mixer *mixer; assert(i < audio_output_count()); + assert(volume <= 100); output = audio_output_get(i); if (!output->enabled) @@ -85,20 +86,17 @@ output_mixer_set_volume(unsigned i, int volume) if (mixer == NULL) return false; - if (volume > 100) - volume = 100; - else if (volume < 0) - volume = 0; - return mixer_set_volume(mixer, volume); } bool -mixer_all_set_volume(int volume) +mixer_all_set_volume(unsigned volume) { bool success = false; unsigned count = audio_output_count(); + assert(volume <= 100); + for (unsigned i = 0; i < count; i++) success = output_mixer_set_volume(i, volume) || success; diff --git a/src/mixer_all.h b/src/mixer_all.h index 1f6e016df..e7c9eecc8 100644 --- a/src/mixer_all.h +++ b/src/mixer_all.h @@ -41,6 +41,6 @@ mixer_all_get_volume(void); * @return true on success, false on failure */ bool -mixer_all_set_volume(int volume); +mixer_all_set_volume(unsigned volume); #endif diff --git a/src/volume.c b/src/volume.c index 146c6b13a..3d240f4e4 100644 --- a/src/volume.c +++ b/src/volume.c @@ -42,7 +42,7 @@ static enum mixer_type volume_mixer_type = MIXER_TYPE_HARDWARE; -static int volume_software_set = 100; +static unsigned volume_software_set = 100; /** the cached hardware mixer value; invalid if negative */ static int last_hardware_volume = -1; @@ -117,12 +117,9 @@ int volume_level_get(void) return -1; } -static bool software_volume_change(int volume) +static bool software_volume_change(unsigned volume) { - if (volume > 100) - volume = 100; - else if (volume < 0) - volume = 0; + assert(volume <= 100); volume_software_set = volume; @@ -139,7 +136,7 @@ static bool software_volume_change(int volume) return true; } -static bool hardware_volume_change(int volume) +static bool hardware_volume_change(unsigned volume) { /* reset the cache */ last_hardware_volume = -1; @@ -147,8 +144,10 @@ static bool hardware_volume_change(int volume) return mixer_all_set_volume(volume); } -bool volume_level_change(int volume) +bool volume_level_change(unsigned volume) { + assert(volume <= 100); + idle_add(IDLE_MIXER); switch (volume_mixer_type) { @@ -175,7 +174,7 @@ void read_sw_volume_state(FILE *fp) g_strchomp(buf); sv = strtol(buf + strlen(SW_VOLUME_STATE), &end, 10); - if (G_LIKELY(!*end)) + if (G_LIKELY(!*end) && sv >= 0 && sv <= 100) software_volume_change(sv); else g_warning("Can't parse software volume: %s\n", buf); @@ -186,5 +185,5 @@ void read_sw_volume_state(FILE *fp) void save_sw_volume_state(FILE *fp) { if (volume_mixer_type == MIXER_TYPE_SOFTWARE) - fprintf(fp, SW_VOLUME_STATE "%d\n", volume_software_set); + fprintf(fp, SW_VOLUME_STATE "%u\n", volume_software_set); } diff --git a/src/volume.h b/src/volume.h index 035d7215b..352d36168 100644 --- a/src/volume.h +++ b/src/volume.h @@ -29,7 +29,7 @@ void volume_finish(void); int volume_level_get(void); -bool volume_level_change(int volume); +bool volume_level_change(unsigned volume); void read_sw_volume_state(FILE *fp); -- cgit v1.2.3