aboutsummaryrefslogtreecommitdiff
path: root/src/mixer.c
diff options
context:
space:
mode:
authorTaylor Holberton <taylorcholberton@gmail.com>2016-10-03 21:07:39 -0400
committerTaylor Holberton <taylorcholberton@gmail.com>2016-10-03 21:07:39 -0400
commit6a38d5fa49fa08e4bf8d20c6fb4741eaedee9dc1 (patch)
tree4088b30d18e40ca723768b5a817cbd2ed9a63e06 /src/mixer.c
parent2bdbdb1fd7b6f49b8c9564ce4da127f83e6b1923 (diff)
parent9b42396aa0713be3435e30362bcb6ca52e544bf0 (diff)
Merge branch 'defer-enum-inspection' of https://github.com/dawagner/tinyalsa into dawagner-defer-enum-inspection
Diffstat (limited to 'src/mixer.c')
-rw-r--r--src/mixer.c63
1 files changed, 43 insertions, 20 deletions
diff --git a/src/mixer.c b/src/mixer.c
index cb1d5cb..cf728e7 100644
--- a/src/mixer.c
+++ b/src/mixer.c
@@ -109,10 +109,9 @@ void mixer_close(struct mixer *mixer)
struct mixer *mixer_open(unsigned int card)
{
struct snd_ctl_elem_list elist;
- struct snd_ctl_elem_info tmp;
struct snd_ctl_elem_id *eid = NULL;
struct mixer *mixer = NULL;
- unsigned int n, m;
+ unsigned int n;
int fd;
char fn[256];
@@ -153,22 +152,6 @@ struct mixer *mixer_open(unsigned int card)
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
goto fail;
mixer->ctl[n].mixer = mixer;
- if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
- char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
- if (!enames)
- goto fail;
- mixer->ctl[n].ename = enames;
- for (m = 0; m < ei->value.enumerated.items; m++) {
- memset(&tmp, 0, sizeof(tmp));
- tmp.id.numid = ei->id.numid;
- tmp.value.enumerated.item = m;
- if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
- goto fail;
- enames[m] = strdup(tmp.value.enumerated.name);
- if (!enames[m])
- goto fail;
- }
- }
}
free(eid);
@@ -547,11 +530,50 @@ unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
return ctl->info.value.enumerated.items;
}
+int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
+{
+ struct snd_ctl_elem_info tmp;
+ unsigned int m;
+ char **enames;
+
+ if (ctl->ename) {
+ return 0;
+ }
+
+ enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
+ if (!enames)
+ goto fail;
+ for (m = 0; m < ctl->info.value.enumerated.items; m++) {
+ memset(&tmp, 0, sizeof(tmp));
+ tmp.id.numid = ctl->info.id.numid;
+ tmp.value.enumerated.item = m;
+ if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
+ goto fail;
+ enames[m] = strdup(tmp.value.enumerated.name);
+ if (!enames[m])
+ goto fail;
+ }
+ ctl->ename = enames;
+ return 0;
+
+fail:
+ if (enames) {
+ for (m = 0; m < ctl->info.value.enumerated.items; m++) {
+ if (enames[m]) {
+ free(enames[m]);
+ }
+ }
+ free(enames);
+ }
+ return -1;
+}
+
const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
unsigned int enum_id)
{
if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
- (enum_id >= ctl->info.value.enumerated.items))
+ (enum_id >= ctl->info.value.enumerated.items) ||
+ mixer_ctl_fill_enum_string(ctl) != 0)
return NULL;
return (const char *)ctl->ename[enum_id];
@@ -563,7 +585,8 @@ int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
struct snd_ctl_elem_value ev;
int ret;
- if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
+ if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
+ mixer_ctl_fill_enum_string(ctl) != 0)
return -EINVAL;
num_enums = ctl->info.value.enumerated.items;