1998-08-13 15:42:56 +00:00
|
|
|
/*
|
|
|
|
|
* Mixer Interface - main file
|
2000-07-15 10:20:32 +00:00
|
|
|
* Copyright (c) 1998/1999/2000 by Jaroslav Kysela <perex@suse.cz>
|
1998-08-13 15:42:56 +00:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* 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>
|
2000-07-15 10:20:32 +00:00
|
|
|
#include "mixer_local.h"
|
1998-08-13 15:42:56 +00:00
|
|
|
|
2000-08-16 13:35:36 +00:00
|
|
|
static void snd_mixer_simple_read_rebuild(snd_ctl_t *ctl_handle, void *private_data);
|
2001-02-07 15:13:15 +00:00
|
|
|
static void snd_mixer_simple_read_add(snd_ctl_t *ctl_handle, void *private_data, snd_hctl_element_t *helem);
|
2000-08-16 13:35:36 +00:00
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
int snd_mixer_open(snd_mixer_t **r_handle, char *name)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
snd_mixer_t *handle;
|
|
|
|
|
snd_ctl_t *ctl_handle;
|
|
|
|
|
int err;
|
1999-01-30 18:35:52 +00:00
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
if (r_handle == NULL)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
2000-07-15 10:20:32 +00:00
|
|
|
*r_handle = NULL;
|
2000-09-11 15:49:10 +00:00
|
|
|
if ((err = snd_ctl_open(&ctl_handle, name)) < 0)
|
2000-07-15 10:20:32 +00:00
|
|
|
return err;
|
2000-08-17 21:50:34 +00:00
|
|
|
handle = (snd_mixer_t *) calloc(1, sizeof(snd_mixer_t));
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
|
snd_ctl_close(ctl_handle);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
2000-08-16 13:35:36 +00:00
|
|
|
if ((err = snd_ctl_hcallback_rebuild(ctl_handle, snd_mixer_simple_read_rebuild, handle)) < 0) {
|
|
|
|
|
snd_ctl_close(ctl_handle);
|
2000-08-17 21:50:34 +00:00
|
|
|
free(handle);
|
2000-08-16 13:35:36 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if ((err = snd_ctl_hcallback_add(ctl_handle, snd_mixer_simple_read_add, handle)) < 0) {
|
|
|
|
|
snd_ctl_close(ctl_handle);
|
2000-08-17 21:50:34 +00:00
|
|
|
free(handle);
|
2000-08-16 13:35:36 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
2000-07-15 10:20:32 +00:00
|
|
|
handle->ctl_handle = ctl_handle;
|
2000-08-16 13:35:36 +00:00
|
|
|
INIT_LIST_HEAD(&handle->simples);
|
2000-07-15 10:20:32 +00:00
|
|
|
*r_handle = handle;
|
1998-11-27 14:57:39 +00:00
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1999-06-02 00:40:30 +00:00
|
|
|
int snd_mixer_close(snd_mixer_t *handle)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
int err = 0;
|
1998-11-27 14:57:39 +00:00
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
if (handle == NULL)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
2000-07-15 10:20:32 +00:00
|
|
|
if (handle->simple_valid)
|
|
|
|
|
snd_mixer_simple_destroy(handle);
|
|
|
|
|
if (handle->ctl_handle)
|
|
|
|
|
err = snd_ctl_close(handle->ctl_handle);
|
|
|
|
|
return err;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
int snd_mixer_poll_descriptor(snd_mixer_t *handle)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
if (handle == NULL || handle->ctl_handle == NULL)
|
|
|
|
|
return -EIO;
|
2000-09-24 09:57:26 +00:00
|
|
|
return snd_ctl_poll_descriptor(handle->ctl_handle);
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
1999-05-02 16:21:30 +00:00
|
|
|
|
2001-02-05 15:44:42 +00:00
|
|
|
const char *snd_mixer_simple_channel_name(snd_mixer_channel_id_t channel)
|
1999-07-22 18:10:19 +00:00
|
|
|
{
|
2001-02-05 15:44:42 +00:00
|
|
|
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"
|
1999-07-22 18:10:19 +00:00
|
|
|
};
|
2001-02-05 15:44:42 +00:00
|
|
|
char *p;
|
|
|
|
|
assert(channel <= SND_MIXER_CHN_LAST);
|
|
|
|
|
p = array[snd_enum_to_int(channel)];
|
|
|
|
|
if (!p)
|
1999-07-22 18:10:19 +00:00
|
|
|
return "?";
|
2001-02-05 15:44:42 +00:00
|
|
|
return p;
|
1999-07-22 18:10:19 +00:00
|
|
|
}
|
2000-01-18 08:45:02 +00:00
|
|
|
|
2001-02-07 15:13:15 +00:00
|
|
|
int snd_mixer_simple_element_list(snd_mixer_t *handle, snd_mixer_simple_element_list_t *list)
|
2000-01-18 08:45:02 +00:00
|
|
|
{
|
2000-08-16 13:35:36 +00:00
|
|
|
struct list_head *lh;
|
2000-07-15 10:20:32 +00:00
|
|
|
mixer_simple_t *s;
|
|
|
|
|
snd_mixer_sid_t *p;
|
2000-08-17 21:50:34 +00:00
|
|
|
int err;
|
|
|
|
|
unsigned int idx;
|
2000-01-18 08:45:02 +00:00
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
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;
|
2000-08-16 13:35:36 +00:00
|
|
|
if (list->controls_request > 0 && p == NULL)
|
2000-07-15 10:20:32 +00:00
|
|
|
return -EINVAL;
|
2000-08-16 13:35:36 +00:00
|
|
|
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++;
|
|
|
|
|
}
|
2000-07-15 10:20:32 +00:00
|
|
|
list->controls = handle->simple_count;
|
|
|
|
|
return 0;
|
2000-01-18 08:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
static mixer_simple_t *look_for_simple(snd_mixer_t *handle, snd_mixer_sid_t *sid)
|
2000-01-18 08:45:02 +00:00
|
|
|
{
|
2000-08-16 13:35:36 +00:00
|
|
|
struct list_head *list;
|
2000-07-15 10:20:32 +00:00
|
|
|
mixer_simple_t *s;
|
|
|
|
|
|
2000-08-16 13:35:36 +00:00
|
|
|
list_for_each(list, &handle->simples) {
|
|
|
|
|
s = list_entry(list, mixer_simple_t, list);
|
2000-07-28 20:21:12 +00:00
|
|
|
if (!strcmp(s->sid.name, sid->name) && s->sid.index == sid->index)
|
2000-07-15 10:20:32 +00:00
|
|
|
return s;
|
2000-08-16 13:35:36 +00:00
|
|
|
}
|
2000-07-15 10:20:32 +00:00
|
|
|
return NULL;
|
2000-01-18 08:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
2001-02-07 15:13:15 +00:00
|
|
|
int snd_mixer_simple_element_read(snd_mixer_t *handle, snd_mixer_simple_element_t *control)
|
2000-01-18 08:45:02 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
mixer_simple_t *s;
|
2000-01-18 08:45:02 +00:00
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2000-01-18 08:45:02 +00:00
|
|
|
|
2001-02-07 15:13:15 +00:00
|
|
|
int snd_mixer_simple_element_write(snd_mixer_t *handle, snd_mixer_simple_element_t *control)
|
2000-01-18 08:45:02 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
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);
|
2000-01-18 08:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
static void snd_mixer_simple_read_rebuild(snd_ctl_t *ctl_handle, void *private_data)
|
2000-01-18 08:45:02 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
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++;
|
2000-01-18 08:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
2001-02-07 15:13:15 +00:00
|
|
|
static void snd_mixer_simple_read_add(snd_ctl_t *ctl_handle ATTRIBUTE_UNUSED, void *private_data, snd_hctl_element_t *helem)
|
2000-01-18 08:45:02 +00:00
|
|
|
{
|
2000-08-16 13:35:36 +00:00
|
|
|
snd_mixer_t *handle = (snd_mixer_t *)private_data;
|
2000-07-15 10:20:32 +00:00
|
|
|
mixer_simple_t *s;
|
2000-08-16 13:35:36 +00:00
|
|
|
struct list_head *list;
|
2000-07-15 10:20:32 +00:00
|
|
|
|
2000-08-16 13:35:36 +00:00
|
|
|
list_for_each(list, &handle->simples) {
|
|
|
|
|
s = list_entry(list, mixer_simple_t, list);
|
|
|
|
|
if (s->event_add)
|
2001-02-07 15:13:15 +00:00
|
|
|
s->event_add(handle, helem);
|
2000-01-18 08:45:02 +00:00
|
|
|
}
|
2000-07-15 10:20:32 +00:00
|
|
|
}
|
2000-01-18 08:45:02 +00:00
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
int snd_mixer_simple_read(snd_mixer_t *handle, snd_mixer_simple_callbacks_t *callbacks)
|
2000-01-18 08:45:02 +00:00
|
|
|
{
|
2000-08-16 13:35:36 +00:00
|
|
|
mixer_simple_t *s;
|
|
|
|
|
struct list_head *list;
|
2000-07-15 10:20:32 +00:00
|
|
|
int err;
|
|
|
|
|
|
2000-08-16 13:35:36 +00:00
|
|
|
if (handle == NULL || callbacks == NULL)
|
2000-07-15 10:20:32 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
if (!handle->simple_valid)
|
|
|
|
|
snd_mixer_simple_build(handle);
|
|
|
|
|
handle->callbacks = callbacks;
|
|
|
|
|
handle->simple_changes = 0;
|
2000-08-11 19:28:43 +00:00
|
|
|
if ((err = snd_ctl_hevent(handle->ctl_handle)) <= 0) {
|
2000-07-15 10:20:32 +00:00
|
|
|
handle->callbacks = NULL;
|
|
|
|
|
return err;
|
2000-01-18 08:45:02 +00:00
|
|
|
}
|
2000-07-15 10:20:32 +00:00
|
|
|
handle->callbacks = NULL;
|
2000-08-16 13:35:36 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-07-15 10:20:32 +00:00
|
|
|
return handle->simple_changes;
|
2000-01-18 08:45:02 +00:00
|
|
|
}
|