Completed control and mixer API

This commit is contained in:
Abramo Bagnara 2001-02-09 11:20:31 +00:00
parent c709b0a627
commit fea0c73cdb
23 changed files with 1988 additions and 1251 deletions

View file

@ -1,6 +1,6 @@
EXTRA_LTLIBRARIES=libmixer.la
libmixer_la_SOURCES = mixer.c simple.c
libmixer_la_SOURCES = bag.c mixer.c mixer_m4.c simple.c
all: libmixer.la

80
src/mixer/bag.c Normal file
View file

@ -0,0 +1,80 @@
/*
* Control Interface - highlevel API - helem bag operations
* Copyright (c) 2000 by Jaroslav Kysela <perex@suse.cz>
* Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define __USE_GNU
#include <search.h>
#include "mixer_local.h"
int snd_hctl_compare_fast(const snd_hctl_elem_t *c1,
const snd_hctl_elem_t *c2);
static void _free(void *ptr ATTRIBUTE_UNUSED) { };
int snd_hctl_bag_destroy(snd_hctl_bag_t *bag)
{
assert(bag != NULL);
tdestroy(bag->root, _free);
bag->root = NULL;
return 0;
}
int snd_hctl_bag_add(snd_hctl_bag_t *bag, snd_hctl_elem_t *helem)
{
void *res;
assert(bag != NULL && helem != NULL);
res = tsearch(helem, &bag->root, (__compar_fn_t)snd_hctl_compare_fast);
if (res == NULL)
return -ENOMEM;
if ((snd_hctl_elem_t *)res == helem)
return -EALREADY;
return 0;
}
int snd_hctl_bag_del(snd_hctl_bag_t *bag, snd_hctl_elem_t *helem)
{
assert(bag != NULL && helem != NULL);
if (tdelete(helem, &bag->root, (__compar_fn_t)snd_hctl_compare_fast) == NULL)
return -ENOENT;
return 0;
}
snd_hctl_elem_t *snd_hctl_bag_find(snd_hctl_bag_t *bag, snd_ctl_elem_id_t *id)
{
void *res;
assert(bag != NULL && id != NULL);
if (bag->root == NULL)
return NULL;
res = tfind(id, &bag->root, (__compar_fn_t)snd_hctl_compare_fast);
return res == NULL ? NULL : *(snd_hctl_elem_t **)res;
}
int snd_hctl_bag_empty(snd_hctl_bag_t *bag)
{
assert(bag != NULL);
return bag->root == NULL;
}

View file

@ -1,6 +1,7 @@
/*
* Mixer Interface - main file
* Copyright (c) 1998/1999/2000 by Jaroslav Kysela <perex@suse.cz>
* Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This library is free software; you can redistribute it and/or modify
@ -27,202 +28,103 @@
#include <sys/ioctl.h>
#include "mixer_local.h"
static void snd_mixer_simple_read_rebuild(snd_ctl_t *ctl_handle, void *private_data);
static void snd_mixer_simple_read_add(snd_ctl_t *ctl_handle, void *private_data, snd_hctl_element_t *helem);
int snd_mixer_open(snd_mixer_t **r_handle, char *name)
int snd_mixer_open(snd_mixer_t **mixerp, char *name)
{
snd_mixer_t *handle;
snd_ctl_t *ctl_handle;
snd_mixer_t *mixer;
snd_ctl_t *ctl;
int err;
if (r_handle == NULL)
return -EINVAL;
*r_handle = NULL;
if ((err = snd_ctl_open(&ctl_handle, name)) < 0)
assert(mixerp);
if ((err = snd_ctl_open(&ctl, name)) < 0)
return err;
handle = (snd_mixer_t *) calloc(1, sizeof(snd_mixer_t));
if (handle == NULL) {
snd_ctl_close(ctl_handle);
mixer = calloc(1, sizeof(snd_mixer_t));
if (mixer == NULL) {
snd_ctl_close(ctl);
return -ENOMEM;
}
if ((err = snd_ctl_hcallback_rebuild(ctl_handle, snd_mixer_simple_read_rebuild, handle)) < 0) {
snd_ctl_close(ctl_handle);
free(handle);
return err;
}
if ((err = snd_ctl_hcallback_add(ctl_handle, snd_mixer_simple_read_add, handle)) < 0) {
snd_ctl_close(ctl_handle);
free(handle);
return err;
}
handle->ctl_handle = ctl_handle;
INIT_LIST_HEAD(&handle->simples);
*r_handle = handle;
mixer->ctl = ctl;
INIT_LIST_HEAD(&mixer->elems);
*mixerp = mixer;
return 0;
}
int snd_mixer_close(snd_mixer_t *handle)
snd_mixer_elem_t *snd_mixer_elem_add(snd_mixer_t *mixer)
{
int err = 0;
if (handle == NULL)
return -EINVAL;
if (handle->simple_valid)
snd_mixer_simple_destroy(handle);
if (handle->ctl_handle)
err = snd_ctl_close(handle->ctl_handle);
return err;
snd_mixer_elem_t *elem;
elem = calloc(1, sizeof(*elem));
if (!elem)
return NULL;
elem->mixer = mixer;
list_add_tail(&elem->list, &mixer->elems);
mixer->count++;
return elem;
}
int snd_mixer_poll_descriptor(snd_mixer_t *handle)
void snd_mixer_elem_remove(snd_mixer_elem_t *elem)
{
if (handle == NULL || handle->ctl_handle == NULL)
snd_mixer_t *mixer = elem->mixer;
if (elem->private_free)
elem->private_free(elem);
if (elem->callback)
elem->callback(elem, SND_CTL_EVENT_REMOVE);
list_del(&elem->list);
free(elem);
mixer->count--;
}
void snd_mixer_free(snd_mixer_t *mixer)
{
while (!list_empty(&mixer->elems))
snd_mixer_elem_remove(list_entry(mixer->elems.next, snd_mixer_elem_t, list));
}
int snd_mixer_close(snd_mixer_t *mixer)
{
assert(mixer);
snd_mixer_free(mixer);
return snd_ctl_close(mixer->ctl);
}
int snd_mixer_poll_descriptor(snd_mixer_t *mixer)
{
if (mixer == NULL || mixer->ctl == NULL)
return -EIO;
return snd_ctl_poll_descriptor(handle->ctl_handle);
return snd_ctl_poll_descriptor(mixer->ctl);
}
const char *snd_mixer_simple_channel_name(snd_mixer_channel_id_t channel)
snd_mixer_elem_t *snd_mixer_first_elem(snd_mixer_t *mixer)
{
static char *array[snd_enum_to_int(SND_MIXER_CHN_LAST) + 1] = {
[SND_MIXER_CHN_FRONT_LEFT] = "Front Left",
[SND_MIXER_CHN_FRONT_RIGHT] = "Front Right",
[SND_MIXER_CHN_FRONT_CENTER] = "Front Center",
[SND_MIXER_CHN_REAR_LEFT] = "Rear Left",
[SND_MIXER_CHN_REAR_RIGHT] = "Rear Right",
[SND_MIXER_CHN_WOOFER] = "Woofer"
};
char *p;
assert(channel <= SND_MIXER_CHN_LAST);
p = array[snd_enum_to_int(channel)];
if (!p)
return "?";
return p;
assert(mixer);
if (list_empty(&mixer->elems))
return NULL;
return list_entry(mixer->elems.next, snd_mixer_elem_t, list);
}
int snd_mixer_simple_element_list(snd_mixer_t *handle, snd_mixer_simple_element_list_t *list)
snd_mixer_elem_t *snd_mixer_last_elem(snd_mixer_t *mixer)
{
struct list_head *lh;
mixer_simple_t *s;
snd_mixer_sid_t *p;
int err;
unsigned int idx;
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;
p = list->pids;
if (list->controls_request > 0 && p == NULL)
return -EINVAL;
idx = 0;
list_for_each(lh, &handle->simples) {
if (idx >= list->controls_offset + list->controls_request)
break;
if (idx >= list->controls_offset) {
s = list_entry(lh, mixer_simple_t, list);
memcpy(p, &s->sid, sizeof(*p)); p++;
list->controls_count++;
}
idx++;
}
list->controls = handle->simple_count;
return 0;
assert(mixer);
if (list_empty(&mixer->elems))
return NULL;
return list_entry(mixer->elems.prev, snd_mixer_elem_t, list);
}
static mixer_simple_t *look_for_simple(snd_mixer_t *handle, snd_mixer_sid_t *sid)
snd_mixer_elem_t *snd_mixer_elem_next(snd_mixer_elem_t *elem)
{
struct list_head *list;
mixer_simple_t *s;
list_for_each(list, &handle->simples) {
s = list_entry(list, mixer_simple_t, list);
if (!strcmp(s->sid.name, sid->name) && s->sid.index == sid->index)
return s;
}
return NULL;
assert(elem);
if (elem->list.next == &elem->mixer->elems)
return NULL;
return list_entry(elem->list.next, snd_mixer_elem_t, list);
}
int snd_mixer_simple_element_read(snd_mixer_t *handle, snd_mixer_simple_element_t *control)
snd_mixer_elem_t *snd_mixer_elem_prev(snd_mixer_elem_t *elem)
{
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);
assert(elem);
if (elem->list.prev == &elem->mixer->elems)
return NULL;
return list_entry(elem->list.prev, snd_mixer_elem_t, list);
}
int snd_mixer_simple_element_write(snd_mixer_t *handle, snd_mixer_simple_element_t *control)
int snd_mixer_events(snd_mixer_t *mixer)
{
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);
return snd_hctl_events(mixer->ctl);
}
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 snd_mixer_simple_read_add(snd_ctl_t *ctl_handle ATTRIBUTE_UNUSED, void *private_data, snd_hctl_element_t *helem)
{
snd_mixer_t *handle = (snd_mixer_t *)private_data;
mixer_simple_t *s;
struct list_head *list;
list_for_each(list, &handle->simples) {
s = list_entry(list, mixer_simple_t, list);
if (s->event_add)
s->event_add(handle, helem);
}
}
int snd_mixer_simple_read(snd_mixer_t *handle, snd_mixer_simple_callbacks_t *callbacks)
{
mixer_simple_t *s;
struct list_head *list;
int err;
if (handle == NULL || callbacks == NULL)
return -EINVAL;
if (!handle->simple_valid)
snd_mixer_simple_build(handle);
handle->callbacks = callbacks;
handle->simple_changes = 0;
if ((err = snd_ctl_hevent(handle->ctl_handle)) <= 0) {
handle->callbacks = NULL;
return err;
}
handle->callbacks = NULL;
list_for_each(list, &handle->simples) {
s = list_entry(list, mixer_simple_t, list);
if (s->change > 0) {
s->change = 0;
if (callbacks->value)
callbacks->value(handle, callbacks->private_data, &s->sid);
}
}
return handle->simple_changes;
}

View file

@ -1,6 +1,7 @@
/*
* Mixer Interface - local header file
* Copyright (c) 2000 by Jaroslav Kysela <perex@suse.cz>
* Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This library is free software; you can redistribute it and/or modify
@ -19,72 +20,62 @@
*
*/
#include "local.h"
//#include "../control/control_local.h"
#include "list.h"
#include "local.h"
typedef struct _mixer_simple mixer_simple_t;
typedef struct _mixer_simple_hctl_element_private mixer_simple_hctl_element_private_t;
typedef struct _snd_hctl_bag {
void *root;
void *private;
} snd_hctl_bag_t;
typedef int (mixer_simple_get_t) (snd_mixer_t *handle, mixer_simple_t *simple, snd_mixer_simple_element_t *control);
typedef int (mixer_simple_put_t) (snd_mixer_t *handle, mixer_simple_t *simple, snd_mixer_simple_element_t *control);
typedef int (mixer_simple_event_add_t) (snd_mixer_t *handle, snd_hctl_element_t *helem);
int snd_hctl_bag_destroy(snd_hctl_bag_t *bag);
int snd_hctl_bag_add(snd_hctl_bag_t *bag, snd_hctl_elem_t *helem);
int snd_hctl_bag_del(snd_hctl_bag_t *bag, snd_hctl_elem_t *helem);
snd_hctl_elem_t *snd_hctl_bag_find(snd_hctl_bag_t *bag, snd_ctl_elem_id_t *id);
int snd_hctl_bag_empty(snd_hctl_bag_t *bag);
#define MIXER_PRESENT_SINGLE_SWITCH (1<<0)
#define MIXER_PRESENT_SINGLE_VOLUME (1<<1)
#define MIXER_PRESENT_GLOBAL_SWITCH (1<<2)
#define MIXER_PRESENT_GLOBAL_VOLUME (1<<3)
#define MIXER_PRESENT_GLOBAL_ROUTE (1<<4)
#define MIXER_PRESENT_PLAYBACK_SWITCH (1<<5)
#define MIXER_PRESENT_PLAYBACK_VOLUME (1<<6)
#define MIXER_PRESENT_PLAYBACK_ROUTE (1<<7)
#define MIXER_PRESENT_CAPTURE_SWITCH (1<<8)
#define MIXER_PRESENT_CAPTURE_VOLUME (1<<9)
#define MIXER_PRESENT_CAPTURE_ROUTE (1<<10)
#define MIXER_PRESENT_CAPTURE_SOURCE (1<<11)
struct _mixer_simple {
/* this may be moved to a private area */
unsigned int present; /* present controls */
unsigned int global_values;
unsigned int gswitch_values;
unsigned int pswitch_values;
unsigned int cswitch_values;
unsigned int gvolume_values;
unsigned int pvolume_values;
unsigned int cvolume_values;
unsigned int groute_values;
unsigned int proute_values;
unsigned int croute_values;
unsigned int ccapture_values;
unsigned int capture_item;
unsigned int caps;
long min;
long max;
int voices;
/* -- */
int refs; /* number of references */
int change; /* simple control was changed */
snd_mixer_sid_t sid;
mixer_simple_get_t *get;
mixer_simple_put_t *put;
mixer_simple_event_add_t *event_add;
struct list_head list;
void *helems; /* bag of associated helems */
unsigned long private_value;
struct _snd_mixer_elem {
snd_mixer_elem_type_t type;
struct list_head list; /* links for list of all elems */
void *private;
void (*private_free)(snd_mixer_elem_t *elem);
snd_mixer_elem_callback_t callback;
void *callback_private;
snd_mixer_t *mixer;
};
struct _mixer_simple_hctl_element_private {
void *simples; /* list of associated helems */
};
struct _snd_mixer {
snd_ctl_t *ctl_handle;
int simple_valid;
int simple_changes; /* total number of changes */
int simple_count;
struct list_head simples; /* list of all simple controls */
snd_mixer_simple_callbacks_t *callbacks;
snd_ctl_t *ctl;
struct list_head elems; /* list of all elemss */
unsigned int count;
snd_mixer_callback_t callback;
void *callback_private;
};
int snd_mixer_simple_build(snd_mixer_t *handle);
int snd_mixer_simple_destroy(snd_mixer_t *handle);
#define SND_MIXER_SCTCAP_VOLUME (1<<0)
#define SND_MIXER_SCTCAP_JOIN_VOLUME (1<<1)
#define SND_MIXER_SCTCAP_MUTE (1<<2)
#define SND_MIXER_SCTCAP_JOIN_MUTE (1<<3)
#define SND_MIXER_SCTCAP_CAPTURE (1<<4)
#define SND_MIXER_SCTCAP_JOIN_CAPTURE (1<<5)
#define SND_MIXER_SCTCAP_EXCL_CAPTURE (1<<6)
struct _snd_mixer_selem_id {
unsigned char name[60];
unsigned int index;
};
struct _snd_mixer_selem {
unsigned int caps; /* RO: capabilities */
unsigned int channels; /* RO: bitmap of active channels */
unsigned int mute; /* RW: bitmap of muted channels */
unsigned int capture; /* RW: bitmap of capture channels */
int capture_group; /* RO: capture group (for exclusive capture) */
long min; /* RO: minimum value */
long max; /* RO: maximum value */
long volume[32];
};
snd_mixer_elem_t *snd_mixer_elem_add(snd_mixer_t *mixer);
void snd_mixer_free(snd_mixer_t *mixer);

205
src/mixer/mixer_m4.c Normal file
View file

@ -0,0 +1,205 @@
/*
* Mixer - Automatically generated functions
* Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "mixer_local.h"
size_t snd_mixer_selem_id_sizeof()
{
return sizeof(snd_mixer_selem_id_t);
}
int snd_mixer_selem_id_malloc(snd_mixer_selem_id_t **ptr)
{
assert(ptr);
*ptr = calloc(1, sizeof(snd_mixer_selem_id_t));
if (!*ptr)
return -ENOMEM;
return 0;
}
void snd_mixer_selem_id_free(snd_mixer_selem_id_t *obj)
{
free(obj);
}
void snd_mixer_selem_id_copy(snd_mixer_selem_id_t *dst, const snd_mixer_selem_id_t *src)
{
assert(dst && src);
*dst = *src;
}
const char *snd_mixer_selem_id_get_name(const snd_mixer_selem_id_t *obj)
{
assert(obj);
return obj->name;
}
unsigned int snd_mixer_selem_id_get_index(const snd_mixer_selem_id_t *obj)
{
assert(obj);
return obj->index;
}
void snd_mixer_selem_id_set_name(snd_mixer_selem_id_t *obj, const char *val)
{
assert(obj);
strncpy(obj->name, val, sizeof(obj->name));
}
void snd_mixer_selem_id_set_index(snd_mixer_selem_id_t *obj, unsigned int val)
{
assert(obj);
obj->index = val;
}
void snd_mixer_set_callback(snd_mixer_t *obj, snd_mixer_callback_t val)
{
assert(obj);
obj->callback = val;
}
void * snd_mixer_get_callback_private(const snd_mixer_t *obj)
{
assert(obj);
return obj->callback_private;
}
void snd_mixer_set_callback_private(snd_mixer_t *obj, void * val)
{
assert(obj);
obj->callback_private = val;
}
unsigned int snd_mixer_get_count(const snd_mixer_t *obj)
{
assert(obj);
return obj->count;
}
void snd_mixer_elem_set_callback(snd_mixer_elem_t *obj, snd_mixer_elem_callback_t val)
{
assert(obj);
obj->callback = val;
}
void * snd_mixer_elem_get_callback_private(const snd_mixer_elem_t *obj)
{
assert(obj);
return obj->callback_private;
}
void snd_mixer_elem_set_callback_private(snd_mixer_elem_t *obj, void * val)
{
assert(obj);
obj->callback_private = val;
}
snd_mixer_elem_type_t snd_mixer_elem_get_type(const snd_mixer_elem_t *obj)
{
assert(obj);
return obj->type;
}
size_t snd_mixer_selem_sizeof()
{
return sizeof(snd_mixer_selem_t);
}
int snd_mixer_selem_malloc(snd_mixer_selem_t **ptr)
{
assert(ptr);
*ptr = calloc(1, sizeof(snd_mixer_selem_t));
if (!*ptr)
return -ENOMEM;
return 0;
}
void snd_mixer_selem_free(snd_mixer_selem_t *obj)
{
free(obj);
}
void snd_mixer_selem_copy(snd_mixer_selem_t *dst, const snd_mixer_selem_t *src)
{
assert(dst && src);
*dst = *src;
}
long snd_mixer_selem_get_min(const snd_mixer_selem_t *obj)
{
assert(obj);
return obj->min;
}
long snd_mixer_selem_get_max(const snd_mixer_selem_t *obj)
{
assert(obj);
return obj->max;
}
int snd_mixer_selem_get_capture_group(const snd_mixer_selem_t *obj)
{
assert(obj);
return obj->capture_group;
}
int snd_mixer_selem_has_volume(const snd_mixer_selem_t *obj)
{
assert(obj);
return !!(obj->caps & SND_MIXER_SCTCAP_VOLUME);
}
int snd_mixer_selem_has_joined_volume(const snd_mixer_selem_t *obj)
{
assert(obj);
return !!(obj->caps & SND_MIXER_SCTCAP_JOIN_VOLUME);
}
int snd_mixer_selem_has_mute(const snd_mixer_selem_t *obj)
{
assert(obj);
return !!(obj->caps & SND_MIXER_SCTCAP_MUTE);
}
int snd_mixer_selem_has_joined_mute(const snd_mixer_selem_t *obj)
{
assert(obj);
return !!(obj->caps & SND_MIXER_SCTCAP_JOIN_MUTE);
}
int snd_mixer_selem_has_capture(const snd_mixer_selem_t *obj)
{
assert(obj);
return !!(obj->caps & SND_MIXER_SCTCAP_CAPTURE);
}
int snd_mixer_selem_has_joined_capture(const snd_mixer_selem_t *obj)
{
assert(obj);
return !!(obj->caps & SND_MIXER_SCTCAP_JOIN_CAPTURE);
}
int snd_mixer_selem_has_exclusive_capture(const snd_mixer_selem_t *obj)
{
assert(obj);
return !!(obj->caps & SND_MIXER_SCTCAP_EXCL_CAPTURE);
}

File diff suppressed because it is too large Load diff