[pulseaudio-discuss] [PATCH v2 3/5] conf-parser: Add pa_config_parse_volume()

David Henningsson david.henningsson at canonical.com
Mon Apr 20 00:31:15 PDT 2015



On 2015-04-17 22:26, Tanu Kaskinen wrote:
> This will be used to parse the path default volume in the alsa mixer
> configuration files. For now, percentages are the only supported
> format, because I don't have need for anything else, but we can of
> course add more volume formats later as needed.

There is a parse_volume function in pactl.c, how about moving it to 
somewhere both conf-parser and pactl can use it?

> ---
>   src/pulsecore/conf-parser.c | 36 ++++++++++++++++++++++++++++++++++++
>   src/pulsecore/conf-parser.h |  3 ++-
>   2 files changed, 38 insertions(+), 1 deletion(-)
>
> diff --git a/src/pulsecore/conf-parser.c b/src/pulsecore/conf-parser.c
> index 2dcd45a..5d54afe 100644
> --- a/src/pulsecore/conf-parser.c
> +++ b/src/pulsecore/conf-parser.c
> @@ -312,3 +312,39 @@ int pa_config_parse_string(pa_config_parser_state *state) {
>       *s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;
>       return 0;
>   }
> +
> +int pa_config_parse_volume(pa_config_parser_state *state) {
> +    pa_volume_t *vol;
> +    size_t len;
> +    char *d_str;
> +    double d;
> +
> +    pa_assert(state);
> +
> +    vol = state->data;
> +
> +    len = strlen(state->rvalue);
> +    if (len < 2)
> +        goto fail;
> +
> +    if (state->rvalue[len - 1] != '%')
> +        goto fail;
> +
> +    d_str = pa_xstrndup(state->rvalue, len - 1);
> +    if (pa_atod(d_str, &d) < 0)
> +        goto fail;
> +    pa_xfree(d_str);
> +
> +    d /= 100;
> +    d *= PA_VOLUME_NORM;
> +
> +    if (d < 0 || d > PA_VOLUME_MAX)
> +        goto fail;
> +
> +    *vol = d;
> +    return 0;
> +
> +fail:
> +    pa_log("[%s:%u] Failed to parse volume value: %s", state->filename, state->lineno, state->rvalue);
> +    return -1;
> +}
> diff --git a/src/pulsecore/conf-parser.h b/src/pulsecore/conf-parser.h
> index dbb6f5c..62d7046 100644
> --- a/src/pulsecore/conf-parser.h
> +++ b/src/pulsecore/conf-parser.h
> @@ -70,12 +70,13 @@ struct pa_config_parser_state {
>    * are not allowed at all in the configuration file. */
>   int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, pa_proplist *proplist, void *userdata);
>
> -/* Generic parsers for integers, size_t, booleans and strings */
> +/* Generic parsers for integers, size_t, booleans, strings and pa_volume_t */
>   int pa_config_parse_int(pa_config_parser_state *state);
>   int pa_config_parse_unsigned(pa_config_parser_state *state);
>   int pa_config_parse_size(pa_config_parser_state *state);
>   int pa_config_parse_bool(pa_config_parser_state *state);
>   int pa_config_parse_not_bool(pa_config_parser_state *state);
>   int pa_config_parse_string(pa_config_parser_state *state);
> +int pa_config_parse_volume(pa_config_parser_state *state);
>
>   #endif
>

-- 
David Henningsson, Canonical Ltd.
https://launchpad.net/~diwic


More information about the pulseaudio-discuss mailing list