Removed old mixer API functions.

Added simple control layer (experimental).
This commit is contained in:
Jaroslav Kysela 2000-07-15 10:20:32 +00:00
parent 3c9755bb7e
commit 273d115de0
11 changed files with 807 additions and 883 deletions

View file

@ -1,6 +1,6 @@
/*
* Mixer Interface - main file
* Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
* Copyright (c) 1998/1999/2000 by Jaroslav Kysela <perex@suse.cz>
*
*
* This library is free software; you can redistribute it and/or modify
@ -27,265 +27,50 @@
#include <fcntl.h>
#include <sys/ioctl.h>
#include "asoundlib.h"
#include "mixer_local.h"
#define __USE_GNU
#include <search.h>
#define SND_FILE_MIXER "/dev/snd/mixerC%iD%i"
#define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
struct snd_mixer {
int card;
int device;
int fd;
} ;
int snd_mixer_open(snd_mixer_t **handle, int card, int device)
int snd_mixer_open(snd_mixer_t **r_handle, int card)
{
int fd, ver;
char filename[32];
snd_mixer_t *mixer;
snd_mixer_t *handle;
snd_ctl_t *ctl_handle;
int err;
*handle = NULL;
if (card < 0 || card >= SND_CARDS)
if (r_handle == NULL)
return -EINVAL;
sprintf(filename, SND_FILE_MIXER, card, device);
if ((fd = open(filename, O_RDWR)) < 0) {
snd_card_load(card);
if ((fd = open(filename, O_RDWR)) < 0)
return -errno;
}
if (ioctl(fd, SND_MIXER_IOCTL_PVERSION, &ver) < 0) {
close(fd);
return -errno;
}
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_MIXER_VERSION_MAX)) {
close(fd);
return -SND_ERROR_INCOMPATIBLE_VERSION;
}
mixer = (snd_mixer_t *) calloc(1, sizeof(snd_mixer_t));
if (mixer == NULL) {
close(fd);
*r_handle = NULL;
if ((err = snd_ctl_open(&ctl_handle, card)) < 0)
return err;
handle = (snd_mixer_t *) calloc(1, sizeof(snd_mixer_t));
if (handle == NULL) {
snd_ctl_close(ctl_handle);
return -ENOMEM;
}
mixer->card = card;
mixer->device = device;
mixer->fd = fd;
*handle = mixer;
handle->ctl_handle = ctl_handle;
*r_handle = handle;
return 0;
}
int snd_mixer_close(snd_mixer_t *handle)
{
snd_mixer_t *mixer;
int res;
int err = 0;
mixer = handle;
if (!mixer)
if (handle == NULL)
return -EINVAL;
res = close(mixer->fd) < 0 ? -errno : 0;
free(mixer);
return res;
if (handle->simple_valid)
snd_mixer_simple_destroy(handle);
if (handle->ctl_handle)
err = snd_ctl_close(handle->ctl_handle);
return err;
}
int snd_mixer_file_descriptor(snd_mixer_t *handle)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer)
return -EINVAL;
return mixer->fd;
if (handle == NULL || handle->ctl_handle == NULL)
return -EIO;
return snd_ctl_file_descriptor(handle->ctl_handle);
}
int snd_mixer_info(snd_mixer_t *handle, snd_mixer_info_t * info)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !info)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_INFO, info) < 0)
return -errno;
return 0;
}
int snd_mixer_elements(snd_mixer_t *handle, snd_mixer_elements_t * elements)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !elements)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENTS, elements) < 0)
return -errno;
return 0;
}
int snd_mixer_routes(snd_mixer_t *handle, snd_mixer_routes_t * routes)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !routes)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ROUTES, routes) < 0)
return -errno;
return 0;
}
int snd_mixer_groups(snd_mixer_t *handle, snd_mixer_groups_t * groups)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !groups)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_GROUPS, groups) < 0)
return -errno;
return 0;
}
int snd_mixer_group_read(snd_mixer_t *handle, snd_mixer_group_t * group)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !group)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_GROUP_READ, group) < 0)
return -errno;
return 0;
}
int snd_mixer_group_write(snd_mixer_t *handle, snd_mixer_group_t * group)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !group)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_GROUP_WRITE, group) < 0)
return -errno;
return 0;
}
int snd_mixer_element_info(snd_mixer_t *handle, snd_mixer_element_info_t * info)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !info)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENT_INFO, info) < 0)
return -errno;
return 0;
}
int snd_mixer_element_read(snd_mixer_t *handle, snd_mixer_element_t * element)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !element)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENT_READ, element) < 0)
return -errno;
return 0;
}
int snd_mixer_element_write(snd_mixer_t *handle, snd_mixer_element_t * element)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !element)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENT_WRITE, element) < 0)
return -errno;
return 0;
}
int snd_mixer_get_filter(snd_mixer_t *handle, snd_mixer_filter_t * filter)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !filter)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_GET_FILTER, filter) < 0)
return -errno;
return 0;
}
int snd_mixer_put_filter(snd_mixer_t *handle, snd_mixer_filter_t * filter)
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer || !filter)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_PUT_FILTER, filter) < 0)
return -errno;
return 0;
}
int snd_mixer_read(snd_mixer_t *handle, snd_mixer_callbacks_t * callbacks)
{
snd_mixer_t *mixer;
int result, count;
snd_mixer_read_t r;
mixer = handle;
if (!mixer)
return -EINVAL;
count = 0;
while ((result = read(mixer->fd, &r, sizeof(r))) > 0) {
if (result != sizeof(r))
return -EIO;
if (!callbacks)
continue;
switch (r.cmd) {
case SND_MIXER_READ_REBUILD:
if (callbacks->rebuild)
callbacks->rebuild(callbacks->private_data);
break;
case SND_MIXER_READ_ELEMENT_VALUE:
case SND_MIXER_READ_ELEMENT_CHANGE:
case SND_MIXER_READ_ELEMENT_ROUTE:
case SND_MIXER_READ_ELEMENT_ADD:
case SND_MIXER_READ_ELEMENT_REMOVE:
if (callbacks->element)
callbacks->element(callbacks->private_data, r.cmd, &r.data.eid);
break;
case SND_MIXER_READ_GROUP_CHANGE:
case SND_MIXER_READ_GROUP_ADD:
case SND_MIXER_READ_GROUP_REMOVE:
if (callbacks->group)
callbacks->group(callbacks->private_data, r.cmd, &r.data.gid);
break;
}
count++;
}
return result >= 0 ? count : -errno;
}
void snd_mixer_set_bit(unsigned int *bitmap, int bit, int val)
{
if (val) {
bitmap[bit >> 5] |= 1 << (bit & 31);
} else {
bitmap[bit >> 5] &= ~(1 << (bit & 31));
}
}
int snd_mixer_get_bit(unsigned int *bitmap, int bit)
{
return (bitmap[bit >> 5] & (1 << (bit & 31))) ? 1 : 0;
}
const char *snd_mixer_channel_name(int channel)
const char *snd_mixer_simple_channel_name(int channel)
{
static char *array[6] = {
"Front-Left",
@ -301,228 +86,147 @@ const char *snd_mixer_channel_name(int channel)
return array[channel];
}
typedef int (*snd_mixer_compare_gid_func_t)(const snd_mixer_gid_t *a, const snd_mixer_gid_t *b, void* private_data);
void snd_mixer_sort_gid_ptr(snd_mixer_gid_t **list, int count,
void* private_data,
snd_mixer_compare_gid_func_t compare)
int snd_mixer_simple_control_list(snd_mixer_t *handle, snd_mixer_simple_control_list_t *list)
{
int _compare(const void* a, const void* b) {
snd_mixer_gid_t * const *_a = a;
snd_mixer_gid_t * const *_b = b;
return compare(*_a, *_b, private_data);
mixer_simple_t *s;
snd_mixer_sid_t *p;
int err;
unsigned int tmp;
if (handle == NULL || list == NULL)
return -EINVAL;
if (!handle->simple_valid)
if ((err = snd_mixer_simple_build(handle)) < 0)
return err;
list->controls_count = 0;
tmp = list->controls_offset;
for (s = handle->simple_first; s != NULL && tmp > 0; s = s->next);
tmp = list->controls_request;
p = list->pids;
printf("request = %i\n", tmp);
if (tmp > 0 && p == NULL)
return -EINVAL;
for (; s != NULL && tmp > 0; s = s->next, tmp--, p++, list->controls_count++)
memcpy(p, &s->id, sizeof(*p));
list->controls = handle->simple_count;
return 0;
}
static mixer_simple_t *look_for_simple(snd_mixer_t *handle, snd_mixer_sid_t *sid)
{
mixer_simple_t *s;
for (s = handle->simple_first; s != NULL; s = s->next)
if (!strcmp(s->id.name, sid->name) && s->id.index == sid->index)
return s;
return NULL;
}
int snd_mixer_simple_control_read(snd_mixer_t *handle, snd_mixer_simple_control_t *control)
{
mixer_simple_t *s;
if (handle == NULL || control == NULL)
return -EINVAL;
if (!handle->simple_valid)
snd_mixer_simple_build(handle);
s = look_for_simple(handle, &control->sid);
if (s == NULL)
return -ENOENT;
if (s->get == NULL)
return -EIO;
return s->get(handle, s, control);
}
int snd_mixer_simple_control_write(snd_mixer_t *handle, snd_mixer_simple_control_t *control)
{
mixer_simple_t *s;
if (handle == NULL || control == NULL)
return -EINVAL;
if (!handle->simple_valid)
snd_mixer_simple_build(handle);
s = look_for_simple(handle, &control->sid);
if (s == NULL)
return -ENOENT;
if (s->put == NULL)
return -EIO;
return s->put(handle, s, control);
}
static void snd_mixer_simple_read_rebuild(snd_ctl_t *ctl_handle, void *private_data)
{
snd_mixer_t *handle = (snd_mixer_t *)private_data;
if (handle->ctl_handle != ctl_handle)
return;
handle->callbacks->rebuild(handle, handle->callbacks->private_data);
handle->simple_changes++;
}
static void event_for_all_simple_controls(snd_mixer_t *handle, snd_ctl_event_type_t etype, snd_control_id_t *id)
{
mixer_simple_t *s;
for (s = handle->simple_first; s != NULL; s = s->next) {
if (s->event)
s->event(handle, etype, id);
}
qsort(list, count, sizeof(snd_mixer_gid_t *), _compare);
}
void snd_mixer_sort_gid(snd_mixer_gid_t *list, int count,
void* private_data,
snd_mixer_compare_gid_func_t compare)
{
snd_mixer_gid_t *list1 = malloc(sizeof(snd_mixer_gid_t) * count);
snd_mixer_gid_t **ptrs = malloc(sizeof(snd_mixer_gid_t *) * count);
int k;
memcpy(list1, list, count * sizeof(snd_mixer_gid_t));
for (k = 0; k < count; ++k)
ptrs[k] = list1 + k;
snd_mixer_sort_gid_ptr(ptrs, count, private_data, compare);
for (k = 0; k < count; ++k)
memcpy(list + k, ptrs[k], sizeof(snd_mixer_gid_t));
free(list1);
free(ptrs);
}
/* Compare using name and index */
int snd_mixer_compare_gid_name_index(const snd_mixer_gid_t *a,
const snd_mixer_gid_t *b,
void *ignored UNUSED)
static void snd_mixer_simple_read_value(snd_ctl_t *ctl_handle, void *private_data, snd_control_id_t *id)
{
int r = strcmp(a->name, b->name);
if (r != 0)
return r;
return a->index - b->index;
snd_mixer_t *handle = (snd_mixer_t *)private_data;
if (handle->ctl_handle != ctl_handle)
return;
event_for_all_simple_controls(handle, SND_CTL_EVENT_VALUE, id);
}
/* Compare using a table mapping name to weight */
int snd_mixer_compare_gid_table(const snd_mixer_gid_t *a,
const snd_mixer_gid_t *b,
void* private_data)
static void snd_mixer_simple_read_change(snd_ctl_t *ctl_handle, void *private_data, snd_control_id_t *id)
{
struct hsearch_data *htab = private_data;
ENTRY ea, eb;
ENTRY *ra, *rb;
int aw = 0, bw = 0;
int r;
ea.key = (char *) a->name;
if (hsearch_r(ea, FIND, &ra, htab))
aw = *(int *)ra->data;
eb.key = (char *) b->name;
if (hsearch_r(eb, FIND, &rb, htab))
bw = *(int *)rb->data;
r = aw - bw;
if (r != 0)
return r;
r = strcmp(a->name, b->name);
if (r != 0)
return r;
return a->index - b->index;
snd_mixer_t *handle = (snd_mixer_t *)private_data;
if (handle->ctl_handle != ctl_handle)
return;
event_for_all_simple_controls(handle, SND_CTL_EVENT_CHANGE, id);
}
void snd_mixer_sort_gid_name_index(snd_mixer_gid_t *list, int count)
static void snd_mixer_simple_read_add(snd_ctl_t *ctl_handle, void *private_data, snd_control_id_t *id)
{
snd_mixer_sort_gid(list, count, NULL, snd_mixer_compare_gid_name_index);
snd_mixer_t *handle = (snd_mixer_t *)private_data;
if (handle->ctl_handle != ctl_handle)
return;
event_for_all_simple_controls(handle, SND_CTL_EVENT_ADD, id);
}
void snd_mixer_sort_gid_table(snd_mixer_gid_t *list, int count, snd_mixer_weight_entry_t *table)
static void snd_mixer_simple_read_remove(snd_ctl_t *ctl_handle, void *private_data, snd_control_id_t *id)
{
struct hsearch_data htab;
int k;
htab.table = NULL;
for (k = 0; table[k].name; ++k);
hcreate_r(k*2, &htab);
for (k = 0; table[k].name; ++k) {
ENTRY e;
ENTRY *r;
e.key = table[k].name;
e.data = (char *) &table[k].weight;
hsearch_r(e, ENTER, &r, &htab);
snd_mixer_t *handle = (snd_mixer_t *)private_data;
if (handle->ctl_handle != ctl_handle)
return;
event_for_all_simple_controls(handle, SND_CTL_EVENT_REMOVE, id);
}
int snd_mixer_simple_read(snd_mixer_t *handle, snd_mixer_simple_callbacks_t *callbacks)
{
snd_ctl_callbacks_t xcallbacks;
int err;
if (handle == NULL)
return -EINVAL;
if (!handle->simple_valid)
snd_mixer_simple_build(handle);
memset(&xcallbacks, 0, sizeof(xcallbacks));
xcallbacks.private_data = handle;
xcallbacks.rebuild = snd_mixer_simple_read_rebuild;
xcallbacks.value = snd_mixer_simple_read_value;
xcallbacks.change = snd_mixer_simple_read_change;
xcallbacks.add = snd_mixer_simple_read_add;
xcallbacks.remove = snd_mixer_simple_read_remove;
handle->callbacks = callbacks;
handle->simple_changes = 0;
if ((err = snd_ctl_read(handle->ctl_handle, &xcallbacks)) <= 0) {
handle->callbacks = NULL;
return err;
}
snd_mixer_sort_gid(list, count, &htab, snd_mixer_compare_gid_table);
hdestroy_r(&htab);
handle->callbacks = NULL;
return handle->simple_changes;
}
typedef int (*snd_mixer_compare_eid_func_t)(const snd_mixer_eid_t *a, const snd_mixer_eid_t *b, void* private_data);
void snd_mixer_sort_eid_ptr(snd_mixer_eid_t **list, int count,
void* private_data,
snd_mixer_compare_eid_func_t compare)
{
int _compare(const void* a, const void* b) {
snd_mixer_eid_t * const *_a = a;
snd_mixer_eid_t * const *_b = b;
return compare(*_a, *_b, private_data);
}
qsort(list, count, sizeof(snd_mixer_eid_t *), _compare);
}
void snd_mixer_sort_eid(snd_mixer_eid_t *list, int count,
void* private_data,
snd_mixer_compare_eid_func_t compare)
{
snd_mixer_eid_t *list1 = malloc(sizeof(snd_mixer_eid_t) * count);
snd_mixer_eid_t **ptrs = malloc(sizeof(snd_mixer_eid_t *) * count);
int k;
memcpy(list1, list, count * sizeof(snd_mixer_eid_t));
for (k = 0; k < count; ++k)
ptrs[k] = list1 + k;
snd_mixer_sort_eid_ptr(ptrs, count, private_data, compare);
for (k = 0; k < count; ++k)
memcpy(list + k, ptrs[k], sizeof(snd_mixer_eid_t));
free(list1);
free(ptrs);
}
/* Compare using name and index */
int snd_mixer_compare_eid_name_index(const snd_mixer_eid_t *a,
const snd_mixer_eid_t *b,
void *ignored UNUSED)
{
int r = strcmp(a->name, b->name);
if (r != 0)
return r;
return a->index - b->index;
}
/* Compare using a table mapping name to weight */
int snd_mixer_compare_eid_table(const snd_mixer_eid_t *a,
const snd_mixer_eid_t *b,
void* private_data)
{
struct hsearch_data *htab = private_data;
ENTRY ea, eb;
ENTRY *ra, *rb;
int aw = 0, bw = 0;
int r;
ea.key = (char *) a->name;
if (hsearch_r(ea, FIND, &ra, htab))
aw = *(int *)ra->data;
eb.key = (char *) b->name;
if (hsearch_r(eb, FIND, &rb, htab))
bw = *(int *)rb->data;
r = aw - bw;
if (r != 0)
return r;
r = strcmp(a->name, b->name);
if (r != 0)
return r;
return a->index - b->index;
}
void snd_mixer_sort_eid_name_index(snd_mixer_eid_t *list, int count)
{
snd_mixer_sort_eid(list, count, NULL, snd_mixer_compare_eid_name_index);
}
void snd_mixer_sort_eid_table(snd_mixer_eid_t *list, int count, snd_mixer_weight_entry_t *table )
{
struct hsearch_data htab;
int k;
htab.table = NULL;
for (k = 0; table[k].name; ++k);
hcreate_r(k*2, &htab);
for (k = 0; table[k].name; ++k) {
ENTRY e;
ENTRY *r;
e.key = table[k].name;
e.data = (char *) &table[k].weight;
hsearch_r(e, ENTER, &r, &htab);
}
snd_mixer_sort_eid(list, count, &htab, snd_mixer_compare_eid_table);
hdestroy_r(&htab);
}
static snd_mixer_weight_entry_t _snd_mixer_default_weights[] = {
{ SND_MIXER_OUT_MASTER, -1360 },
{ SND_MIXER_OUT_MASTER_DIGITAL, -1350 },
{ SND_MIXER_OUT_MASTER_MONO, -1340 },
{ SND_MIXER_OUT_HEADPHONE, -1330 },
{ SND_MIXER_OUT_PHONE, -1320 },
{ SND_MIXER_GRP_EFFECT_3D, -1310 },
{ SND_MIXER_GRP_BASS, -1300 },
{ SND_MIXER_GRP_TREBLE, -1290 },
{ SND_MIXER_GRP_EQUALIZER, -1280 },
{ SND_MIXER_GRP_FADER, -1270 },
{ SND_MIXER_OUT_CENTER, -1260 },
{ SND_MIXER_IN_CENTER, -1250 },
{ SND_MIXER_OUT_WOOFER, -1240 },
{ SND_MIXER_IN_WOOFER, -1230 },
{ SND_MIXER_OUT_SURROUND, -1220 },
{ SND_MIXER_IN_SURROUND, -1210 },
{ SND_MIXER_IN_SYNTHESIZER, -1200 },
{ SND_MIXER_IN_FM, -1190 },
{ SND_MIXER_GRP_EFFECT, -1180 },
{ SND_MIXER_OUT_DSP, -1170 },
{ SND_MIXER_IN_DSP, -1160 },
{ SND_MIXER_IN_PCM, -1150 },
{ SND_MIXER_IN_DAC, -1140 },
{ SND_MIXER_IN_LINE, -1130 },
{ SND_MIXER_IN_MIC, -1120 },
{ SND_MIXER_IN_CD, -1110 },
{ SND_MIXER_IN_VIDEO, -1100 },
{ SND_MIXER_IN_RADIO, -1090 },
{ SND_MIXER_IN_PHONE, -1080 },
{ SND_MIXER_GRP_MIC_GAIN, -1070 },
{ SND_MIXER_GRP_OGAIN, -1060 },
{ SND_MIXER_GRP_IGAIN, -1050 },
{ SND_MIXER_GRP_ANALOG_LOOPBACK,-1040 },
{ SND_MIXER_GRP_DIGITAL_LOOPBACK,-1030 },
{ SND_MIXER_IN_SPEAKER, -1020 },
{ SND_MIXER_IN_MONO, -1010 },
{ SND_MIXER_IN_AUX, -1000 },
{ NULL, 0 }
};
snd_mixer_weight_entry_t *snd_mixer_default_weights = _snd_mixer_default_weights;