aboutsummaryrefslogtreecommitdiff
path: root/mixer.c
diff options
context:
space:
mode:
Diffstat (limited to 'mixer.c')
-rw-r--r--mixer.c176
1 files changed, 118 insertions, 58 deletions
diff --git a/mixer.c b/mixer.c
index afe9d7f..ac294ca 100644
--- a/mixer.c
+++ b/mixer.c
@@ -28,11 +28,14 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
+#include <limits.h>
+#include <time.h>
#include <sys/ioctl.h>
@@ -46,14 +49,13 @@
struct mixer_ctl {
struct mixer *mixer;
- struct snd_ctl_elem_info *info;
+ struct snd_ctl_elem_info info;
char **ename;
};
struct mixer {
int fd;
struct snd_ctl_card_info card_info;
- struct snd_ctl_elem_info *elem_info;
struct mixer_ctl *ctl;
unsigned int count;
};
@@ -71,7 +73,7 @@ void mixer_close(struct mixer *mixer)
if (mixer->ctl) {
for (n = 0; n < mixer->count; n++) {
if (mixer->ctl[n].ename) {
- unsigned int max = mixer->ctl[n].info->value.enumerated.items;
+ unsigned int max = mixer->ctl[n].info.value.enumerated.items;
for (m = 0; m < max; m++)
free(mixer->ctl[n].ename[m]);
free(mixer->ctl[n].ename);
@@ -80,9 +82,6 @@ void mixer_close(struct mixer *mixer)
free(mixer->ctl);
}
- if (mixer->elem_info)
- free(mixer->elem_info);
-
free(mixer);
/* TODO: verify frees */
@@ -112,8 +111,7 @@ struct mixer *mixer_open(unsigned int card)
goto fail;
mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
- mixer->elem_info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
- if (!mixer->ctl || !mixer->elem_info)
+ if (!mixer->ctl)
goto fail;
if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
@@ -131,11 +129,10 @@ struct mixer *mixer_open(unsigned int card)
goto fail;
for (n = 0; n < mixer->count; n++) {
- struct snd_ctl_elem_info *ei = mixer->elem_info + n;
+ struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
ei->id.numid = eid[n].numid;
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
goto fail;
- mixer->ctl[n].info = ei;
mixer->ctl[n].mixer = mixer;
if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
@@ -200,12 +197,15 @@ struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
unsigned int index)
{
unsigned int n;
+ struct mixer_ctl *ctl;
if (!mixer)
return NULL;
+ ctl = mixer->ctl;
+
for (n = 0; n < mixer->count; n++)
- if (!strcmp(name, (char*) mixer->elem_info[n].id.name))
+ if (!strcmp(name, (char*) ctl[n].info.id.name))
if (index-- == 0)
return mixer->ctl + n;
@@ -217,12 +217,23 @@ void mixer_ctl_update(struct mixer_ctl *ctl)
ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
}
+unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
+{
+ if (!ctl)
+ return UINT_MAX;
+
+ /* numid values start at 1, return a 0-base value that
+ * can be passed to mixer_get_ctl()
+ */
+ return ctl->info.id.numid - 1;
+}
+
const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
{
if (!ctl)
return NULL;
- return (const char *)ctl->info->id.name;
+ return (const char *)ctl->info.id.name;
}
enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
@@ -230,7 +241,7 @@ enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
if (!ctl)
return MIXER_CTL_TYPE_UNKNOWN;
- switch (ctl->info->type) {
+ switch (ctl->info.type) {
case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
@@ -246,7 +257,7 @@ const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
if (!ctl)
return "";
- switch (ctl->info->type) {
+ switch (ctl->info.type) {
case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
@@ -262,19 +273,16 @@ unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
if (!ctl)
return 0;
- return ctl->info->count;
+ return ctl->info.count;
}
static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
{
- int range;
-
- if (percent > 100)
- percent = 100;
- else if (percent < 0)
- percent = 0;
+ if ((percent > 100) || (percent < 0)) {
+ return -EINVAL;
+ }
- range = (ei->value.integer.max - ei->value.integer.min);
+ int range = (ei->value.integer.max - ei->value.integer.min);
return ei->value.integer.min + (range * percent) / 100;
}
@@ -291,18 +299,18 @@ static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
{
- if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
+ if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
return -EINVAL;
- return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
+ return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
}
int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
{
- if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
+ if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
return -EINVAL;
- return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
+ return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
}
int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
@@ -310,16 +318,16 @@ int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
struct snd_ctl_elem_value ev;
int ret;
- if (!ctl || (id >= ctl->info->count))
+ if (!ctl || (id >= ctl->info.count))
return -EINVAL;
memset(&ev, 0, sizeof(ev));
- ev.id.numid = ctl->info->id.numid;
+ ev.id.numid = ctl->info.id.numid;
ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
if (ret < 0)
return ret;
- switch (ctl->info->type) {
+ switch (ctl->info.type) {
case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
return !!ev.value.integer.value[id];
@@ -342,31 +350,55 @@ int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
{
struct snd_ctl_elem_value ev;
- int ret;
+ int ret = 0;
size_t size;
void *source;
- if (!ctl || (count > ctl->info->count) || !count || !array)
+ if (!ctl || (count > ctl->info.count) || !count || !array)
return -EINVAL;
memset(&ev, 0, sizeof(ev));
- ev.id.numid = ctl->info->id.numid;
+ ev.id.numid = ctl->info.id.numid;
- ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
- if (ret < 0)
- return ret;
-
- switch (ctl->info->type) {
+ switch (ctl->info.type) {
case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
case SNDRV_CTL_ELEM_TYPE_INTEGER:
+ ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
+ if (ret < 0)
+ return ret;
size = sizeof(ev.value.integer.value[0]);
source = ev.value.integer.value;
break;
case SNDRV_CTL_ELEM_TYPE_BYTES:
- size = sizeof(ev.value.bytes.data[0]);
- source = ev.value.bytes.data;
- break;
+ /* check if this is new bytes TLV */
+ if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
+ struct snd_ctl_tlv *tlv;
+ int ret;
+
+ if (count > SIZE_MAX - sizeof(*tlv))
+ return -EINVAL;
+ tlv = calloc(1, sizeof(*tlv) + count);
+ if (!tlv)
+ return -ENOMEM;
+ tlv->numid = ctl->info.id.numid;
+ tlv->length = count;
+ ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
+
+ source = tlv->tlv;
+ memcpy(array, source, count);
+
+ free(tlv);
+
+ return ret;
+ } else {
+ ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
+ if (ret < 0)
+ return ret;
+ size = sizeof(ev.value.bytes.data[0]);
+ source = ev.value.bytes.data;
+ break;
+ }
default:
return -EINVAL;
@@ -382,21 +414,26 @@ int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
struct snd_ctl_elem_value ev;
int ret;
- if (!ctl || (id >= ctl->info->count))
+ if (!ctl || (id >= ctl->info.count))
return -EINVAL;
memset(&ev, 0, sizeof(ev));
- ev.id.numid = ctl->info->id.numid;
+ ev.id.numid = ctl->info.id.numid;
ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
if (ret < 0)
return ret;
- switch (ctl->info->type) {
+ switch (ctl->info.type) {
case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
ev.value.integer.value[id] = !!value;
break;
case SNDRV_CTL_ELEM_TYPE_INTEGER:
+ if ((value < mixer_ctl_get_range_min(ctl)) ||
+ (value > mixer_ctl_get_range_max(ctl))) {
+ return -EINVAL;
+ }
+
ev.value.integer.value[id] = value;
break;
@@ -404,6 +441,10 @@ int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
ev.value.enumerated.item[id] = value;
break;
+ case SNDRV_CTL_ELEM_TYPE_BYTES:
+ ev.value.bytes.data[id] = value;
+ break;
+
default:
return -EINVAL;
}
@@ -417,13 +458,13 @@ int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
size_t size;
void *dest;
- if (!ctl || (count > ctl->info->count) || !count || !array)
+ if (!ctl || (count > ctl->info.count) || !count || !array)
return -EINVAL;
memset(&ev, 0, sizeof(ev));
- ev.id.numid = ctl->info->id.numid;
+ ev.id.numid = ctl->info.id.numid;
- switch (ctl->info->type) {
+ switch (ctl->info.type) {
case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
case SNDRV_CTL_ELEM_TYPE_INTEGER:
size = sizeof(ev.value.integer.value[0]);
@@ -431,8 +472,27 @@ int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
break;
case SNDRV_CTL_ELEM_TYPE_BYTES:
- size = sizeof(ev.value.bytes.data[0]);
- dest = ev.value.bytes.data;
+ /* check if this is new bytes TLV */
+ if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
+ struct snd_ctl_tlv *tlv;
+ int ret = 0;
+ if (count > SIZE_MAX - sizeof(*tlv))
+ return -EINVAL;
+ tlv = calloc(1, sizeof(*tlv) + count);
+ if (!tlv)
+ return -ENOMEM;
+ tlv->numid = ctl->info.id.numid;
+ tlv->length = count;
+ memcpy(tlv->tlv, array, count);
+
+ ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
+ free(tlv);
+
+ return ret;
+ } else {
+ size = sizeof(ev.value.bytes.data[0]);
+ dest = ev.value.bytes.data;
+ }
break;
default:
@@ -446,18 +506,18 @@ int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
{
- if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
+ if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
return -EINVAL;
- return ctl->info->value.integer.min;
+ return ctl->info.value.integer.min;
}
int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
{
- if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
+ if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
return -EINVAL;
- return ctl->info->value.integer.max;
+ return ctl->info.value.integer.max;
}
unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
@@ -465,14 +525,14 @@ unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
if (!ctl)
return 0;
- return ctl->info->value.enumerated.items;
+ return ctl->info.value.enumerated.items;
}
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))
+ if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
+ (enum_id >= ctl->info.value.enumerated.items))
return NULL;
return (const char *)ctl->ename[enum_id];
@@ -484,15 +544,15 @@ 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))
return -EINVAL;
- num_enums = ctl->info->value.enumerated.items;
+ num_enums = ctl->info.value.enumerated.items;
for (i = 0; i < num_enums; i++) {
if (!strcmp(string, ctl->ename[i])) {
memset(&ev, 0, sizeof(ev));
ev.value.enumerated.item[0] = i;
- ev.id.numid = ctl->info->id.numid;
+ ev.id.numid = ctl->info.id.numid;
ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
if (ret < 0)
return ret;