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))
|
if (!check_int(client, &level, argv[1], need_integer))
|
||||||
return COMMAND_RETURN_ERROR;
|
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);
|
success = volume_level_change(level);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
command_error(client, ACK_ERROR_SYSTEM,
|
command_error(client, ACK_ERROR_SYSTEM,
|
||||||
|
@ -70,12 +70,13 @@ mixer_all_get_volume(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
output_mixer_set_volume(unsigned i, int volume)
|
output_mixer_set_volume(unsigned i, unsigned volume)
|
||||||
{
|
{
|
||||||
struct audio_output *output;
|
struct audio_output *output;
|
||||||
struct mixer *mixer;
|
struct mixer *mixer;
|
||||||
|
|
||||||
assert(i < audio_output_count());
|
assert(i < audio_output_count());
|
||||||
|
assert(volume <= 100);
|
||||||
|
|
||||||
output = audio_output_get(i);
|
output = audio_output_get(i);
|
||||||
if (!output->enabled)
|
if (!output->enabled)
|
||||||
@ -85,20 +86,17 @@ output_mixer_set_volume(unsigned i, int volume)
|
|||||||
if (mixer == NULL)
|
if (mixer == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (volume > 100)
|
|
||||||
volume = 100;
|
|
||||||
else if (volume < 0)
|
|
||||||
volume = 0;
|
|
||||||
|
|
||||||
return mixer_set_volume(mixer, volume);
|
return mixer_set_volume(mixer, volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
mixer_all_set_volume(int volume)
|
mixer_all_set_volume(unsigned volume)
|
||||||
{
|
{
|
||||||
bool success = false;
|
bool success = false;
|
||||||
unsigned count = audio_output_count();
|
unsigned count = audio_output_count();
|
||||||
|
|
||||||
|
assert(volume <= 100);
|
||||||
|
|
||||||
for (unsigned i = 0; i < count; i++)
|
for (unsigned i = 0; i < count; i++)
|
||||||
success = output_mixer_set_volume(i, volume)
|
success = output_mixer_set_volume(i, volume)
|
||||||
|| success;
|
|| success;
|
||||||
|
@ -41,6 +41,6 @@ mixer_all_get_volume(void);
|
|||||||
* @return true on success, false on failure
|
* @return true on success, false on failure
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
mixer_all_set_volume(int volume);
|
mixer_all_set_volume(unsigned volume);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
19
src/volume.c
19
src/volume.c
@ -42,7 +42,7 @@
|
|||||||
|
|
||||||
static enum mixer_type volume_mixer_type = MIXER_TYPE_HARDWARE;
|
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 */
|
/** the cached hardware mixer value; invalid if negative */
|
||||||
static int last_hardware_volume = -1;
|
static int last_hardware_volume = -1;
|
||||||
@ -117,12 +117,9 @@ int volume_level_get(void)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool software_volume_change(int volume)
|
static bool software_volume_change(unsigned volume)
|
||||||
{
|
{
|
||||||
if (volume > 100)
|
assert(volume <= 100);
|
||||||
volume = 100;
|
|
||||||
else if (volume < 0)
|
|
||||||
volume = 0;
|
|
||||||
|
|
||||||
volume_software_set = volume;
|
volume_software_set = volume;
|
||||||
|
|
||||||
@ -139,7 +136,7 @@ static bool software_volume_change(int volume)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool hardware_volume_change(int volume)
|
static bool hardware_volume_change(unsigned volume)
|
||||||
{
|
{
|
||||||
/* reset the cache */
|
/* reset the cache */
|
||||||
last_hardware_volume = -1;
|
last_hardware_volume = -1;
|
||||||
@ -147,8 +144,10 @@ static bool hardware_volume_change(int volume)
|
|||||||
return mixer_all_set_volume(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);
|
idle_add(IDLE_MIXER);
|
||||||
|
|
||||||
switch (volume_mixer_type) {
|
switch (volume_mixer_type) {
|
||||||
@ -175,7 +174,7 @@ void read_sw_volume_state(FILE *fp)
|
|||||||
|
|
||||||
g_strchomp(buf);
|
g_strchomp(buf);
|
||||||
sv = strtol(buf + strlen(SW_VOLUME_STATE), &end, 10);
|
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);
|
software_volume_change(sv);
|
||||||
else
|
else
|
||||||
g_warning("Can't parse software volume: %s\n", buf);
|
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)
|
void save_sw_volume_state(FILE *fp)
|
||||||
{
|
{
|
||||||
if (volume_mixer_type == MIXER_TYPE_SOFTWARE)
|
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);
|
int volume_level_get(void);
|
||||||
|
|
||||||
bool volume_level_change(int volume);
|
bool volume_level_change(unsigned volume);
|
||||||
|
|
||||||
void read_sw_volume_state(FILE *fp);
|
void read_sw_volume_state(FILE *fp);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user