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).
This commit is contained in:
parent
90472526e0
commit
d3b5574d7a
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
19
src/volume.c
19
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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user