From b29ac1ab6b1f4d78017d652e850a96893347fc29 Mon Sep 17 00:00:00 2001 From: Simon Wilson Date: Thu, 8 Mar 2012 10:15:08 -0800 Subject: mixer: simplify string get APIs Just like the pcm_get_error() API, simplify the mixer API functions to return pointers to constant strings instead of making copies of strings. --- include/tinyalsa/asoundlib.h | 6 +++--- mixer.c | 25 ++++++++----------------- tinymix.c | 19 ++++++++----------- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/include/tinyalsa/asoundlib.h b/include/tinyalsa/asoundlib.h index cac2786..704354e 100644 --- a/include/tinyalsa/asoundlib.h +++ b/include/tinyalsa/asoundlib.h @@ -166,13 +166,13 @@ struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id); struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name); /* Get info about mixer controls */ -int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size); +const char *mixer_ctl_get_name(struct mixer_ctl *ctl); enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl); const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl); unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl); unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl); -int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id, - char *string, unsigned int size); +const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, + unsigned int enum_id); /* Set and get mixer controls */ int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id); diff --git a/mixer.c b/mixer.c index a7f5d21..4742e6e 100644 --- a/mixer.c +++ b/mixer.c @@ -193,13 +193,12 @@ struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) return NULL; } -int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size) +const char *mixer_ctl_get_name(struct mixer_ctl *ctl) { - if (!ctl || !name || (size == 0)) - return -EINVAL; + if (!ctl) + return NULL; - strncpy(name, (char *)ctl->info->id.name, size); - return 0; + return (const char *)ctl->info->id.name; } enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl) @@ -392,24 +391,16 @@ unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl) return ctl->info->value.enumerated.items; } -int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id, - char *string, unsigned int size) +const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, + unsigned int enum_id) { - struct snd_ctl_elem_value ev; int ret; if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || (enum_id >= ctl->info->value.enumerated.items)) - return -EINVAL; - - memset(&ev, 0, sizeof(ev)); - ev.id.numid = ctl->info->id.numid; - ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); - if (ret < 0) - return ret; - strncpy(string, (char *)ctl->ename[enum_id], size); + return NULL; - return 0; + return (const char *)ctl->ename[enum_id]; } int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) diff --git a/tinymix.c b/tinymix.c index e7bd276..6427a02 100644 --- a/tinymix.c +++ b/tinymix.c @@ -77,9 +77,8 @@ int main(int argc, char **argv) static void tinymix_list_controls(struct mixer *mixer) { struct mixer_ctl *ctl; - const char *type; + const char *name, *type; unsigned int num_ctls, num_values; - char buffer[256]; unsigned int i; num_ctls = mixer_get_num_ctls(mixer); @@ -90,10 +89,10 @@ static void tinymix_list_controls(struct mixer *mixer) for (i = 0; i < num_ctls; i++) { ctl = mixer_get_ctl(mixer, i); - mixer_ctl_get_name(ctl, buffer, sizeof(buffer)); + name = mixer_ctl_get_name(ctl); type = mixer_ctl_get_type_string(ctl); num_values = mixer_ctl_get_num_values(ctl); - printf("%d\t%s\t%d\t%-40s", i, type, num_values, buffer); + printf("%d\t%s\t%d\t%-40s", i, type, num_values, name); tinymix_detail_control(mixer, i, 0); } } @@ -101,18 +100,18 @@ static void tinymix_list_controls(struct mixer *mixer) static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all) { unsigned int num_enums; - char buffer[256]; unsigned int i; + const char *string; num_enums = mixer_ctl_get_num_enums(ctl); for (i = 0; i < num_enums; i++) { - mixer_ctl_get_enum_string(ctl, i, buffer, sizeof(buffer)); + string = mixer_ctl_get_enum_string(ctl, i); if (print_all) printf("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "", - buffer); + string); else if (mixer_ctl_get_value(ctl, 0) == (int)i) - printf(" %-s", buffer); + printf(" %-s", string); } } @@ -122,7 +121,6 @@ static void tinymix_detail_control(struct mixer *mixer, unsigned int id, struct mixer_ctl *ctl; enum mixer_ctl_type type; unsigned int num_values; - char buffer[256]; unsigned int i; int min, max; @@ -133,12 +131,11 @@ static void tinymix_detail_control(struct mixer *mixer, unsigned int id, ctl = mixer_get_ctl(mixer, id); - mixer_ctl_get_name(ctl, buffer, sizeof(buffer)); type = mixer_ctl_get_type(ctl); num_values = mixer_ctl_get_num_values(ctl); if (print_all) - printf("%s:", buffer); + printf("%s:", mixer_ctl_get_name(ctl)); for (i = 0; i < num_values; i++) { switch (type) -- cgit v1.2.3