1998-08-13 15:42:56 +00:00
|
|
|
/*
|
|
|
|
|
* Mixer Interface - main file
|
|
|
|
|
* Copyright (c) 1998 by Jaroslav Kysela <perex@jcu.cz>
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* 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 <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
1998-08-30 21:08:44 +00:00
|
|
|
#include "asoundlib.h"
|
1998-08-13 15:42:56 +00:00
|
|
|
|
1999-02-01 17:57:08 +00:00
|
|
|
#define SND_FILE_MIXER "/dev/snd/mixerC%iD%i"
|
1998-11-01 23:22:41 +00:00
|
|
|
#define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION( 1, 1, 0 )
|
1998-11-27 14:57:39 +00:00
|
|
|
|
1998-08-13 15:42:56 +00:00
|
|
|
typedef struct {
|
1998-11-27 14:57:39 +00:00
|
|
|
int card;
|
|
|
|
|
int device;
|
|
|
|
|
int fd;
|
1998-08-13 15:42:56 +00:00
|
|
|
} snd_mixer_t;
|
1998-11-27 14:57:39 +00:00
|
|
|
|
|
|
|
|
int snd_mixer_open(void **handle, int card, int device)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
int fd, ver;
|
|
|
|
|
char filename[32];
|
|
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
*handle = NULL;
|
1999-01-30 18:35:52 +00:00
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
if (card < 0 || card >= SND_CARDS)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
sprintf(filename, SND_FILE_MIXER, card, device);
|
1999-01-30 18:35:52 +00:00
|
|
|
if ((fd = open(filename, O_RDWR)) < 0) {
|
|
|
|
|
snd_card_load(card);
|
|
|
|
|
if ((fd = open(filename, O_RDWR)) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
1998-11-27 14:57:39 +00:00
|
|
|
if (ioctl(fd, SND_MIXER_IOCTL_PVERSION, &ver) < 0) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
if (SND_PROTOCOL_UNCOMPATIBLE(ver, SND_MIXER_VERSION_MAX)) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return -SND_ERROR_UNCOMPATIBLE_VERSION;
|
|
|
|
|
}
|
|
|
|
|
mixer = (snd_mixer_t *) calloc(1, sizeof(snd_mixer_t));
|
|
|
|
|
if (mixer == NULL) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
mixer->card = card;
|
|
|
|
|
mixer->device = device;
|
|
|
|
|
mixer->fd = fd;
|
|
|
|
|
*handle = mixer;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_close(void *handle)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
res = close(mixer->fd) < 0 ? -errno : 0;
|
|
|
|
|
free(mixer);
|
|
|
|
|
return res;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_file_descriptor(void *handle)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
return mixer->fd;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_channels(void *handle)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNELS, &result) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return result;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_info(void *handle, snd_mixer_info_t * info)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1998-12-27 01:01:47 +00:00
|
|
|
if (!mixer || !info)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_INFO, info) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_exact_mode(void *handle, int enable)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_EXACT, &enable) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_channel(void *handle, const char *channel_id)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
snd_mixer_channel_info_t info;
|
|
|
|
|
int idx, channels, err;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1998-12-27 01:01:47 +00:00
|
|
|
if (!mixer || !channel_id)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
/* bellow implementation isn't optimized for speed */
|
|
|
|
|
/* info about channels should be cached in the snd_mixer_t structure */
|
|
|
|
|
if ((channels = snd_mixer_channels(handle)) < 0)
|
|
|
|
|
return channels;
|
|
|
|
|
for (idx = 0; idx < channels; idx++) {
|
|
|
|
|
if ((err = snd_mixer_channel_info(handle, idx, &info)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
if (!strncmp(channel_id, info.name, sizeof(info.name)))
|
|
|
|
|
return idx;
|
|
|
|
|
}
|
|
|
|
|
return -EINVAL;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_channel_info(void *handle, int channel, snd_mixer_channel_info_t * info)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1998-12-27 01:01:47 +00:00
|
|
|
if (!mixer || !info || channel < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
info->channel = channel;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_INFO, info) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1999-03-08 16:51:36 +00:00
|
|
|
int snd_mixer_channel_output_info(void *handle, int channel, snd_mixer_channel_direction_info_t * info)
|
|
|
|
|
{
|
|
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer || !info || channel < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
info->channel = channel;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_OINFO, info) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_mixer_channel_input_info(void *handle, int channel, snd_mixer_channel_direction_info_t * info)
|
|
|
|
|
{
|
|
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer || !info || channel < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
info->channel = channel;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_IINFO, info) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_channel_read(void *handle, int channel, snd_mixer_channel_t * data)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1998-12-27 01:01:47 +00:00
|
|
|
if (!mixer || !data || channel < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
1998-12-27 01:01:47 +00:00
|
|
|
bzero(data, sizeof(snd_mixer_channel_t));
|
1998-11-27 14:57:39 +00:00
|
|
|
data->channel = channel;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_READ, data) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_channel_write(void *handle, int channel, snd_mixer_channel_t * data)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1998-12-27 01:01:47 +00:00
|
|
|
if (!mixer || !data)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
data->channel = channel;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_WRITE, data) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1999-03-08 16:51:36 +00:00
|
|
|
int snd_mixer_channel_output_read(void *handle, int channel, snd_mixer_channel_direction_t * data)
|
1998-11-29 16:30:35 +00:00
|
|
|
{
|
|
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1998-12-27 01:01:47 +00:00
|
|
|
if (!mixer || !data || channel < 0)
|
1998-11-29 16:30:35 +00:00
|
|
|
return -EINVAL;
|
1998-12-27 01:01:47 +00:00
|
|
|
bzero(data, sizeof(snd_mixer_channel_t));
|
1998-11-29 16:30:35 +00:00
|
|
|
data->channel = channel;
|
1999-03-08 16:51:36 +00:00
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_OREAD, data) < 0)
|
1998-11-29 16:30:35 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-08 16:51:36 +00:00
|
|
|
int snd_mixer_channel_output_write(void *handle, int channel, snd_mixer_channel_direction_t * data)
|
1998-11-29 16:30:35 +00:00
|
|
|
{
|
|
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1998-12-27 01:01:47 +00:00
|
|
|
if (!mixer || !data || channel < 0)
|
1998-11-29 16:30:35 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
data->channel = channel;
|
1999-03-08 16:51:36 +00:00
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_OWRITE, data) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_mixer_channel_input_read(void *handle, int channel, snd_mixer_channel_direction_t * data)
|
|
|
|
|
{
|
|
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer || !data || channel < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
bzero(data, sizeof(snd_mixer_channel_t));
|
|
|
|
|
data->channel = channel;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_IREAD, data) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_mixer_channel_input_write(void *handle, int channel, snd_mixer_channel_direction_t * data)
|
|
|
|
|
{
|
|
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer || !data || channel < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
data->channel = channel;
|
|
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_IWRITE, data) < 0)
|
1998-11-29 16:30:35 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-27 19:44:51 +00:00
|
|
|
int snd_mixer_switch_list(void *handle, snd_switch_list_t * list)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1999-03-27 19:44:51 +00:00
|
|
|
if (!mixer || !list)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
1999-03-27 19:44:51 +00:00
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_LIST, &list) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
1999-03-27 19:44:51 +00:00
|
|
|
return 0;
|
1998-08-24 16:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
1999-03-27 19:44:51 +00:00
|
|
|
int snd_mixer_switch_read(void *handle, snd_switch_t * sw)
|
1998-08-24 16:23:22 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1999-03-27 19:44:51 +00:00
|
|
|
if (!mixer || !sw || sw->name[0] == '\0')
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
1999-03-27 19:44:51 +00:00
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_READ, sw) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1999-03-27 19:44:51 +00:00
|
|
|
int snd_mixer_switch_write(void *handle, snd_switch_t * sw)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
1999-03-27 19:44:51 +00:00
|
|
|
if (!mixer || !sw || sw->name[0] == '\0')
|
1998-11-27 14:57:39 +00:00
|
|
|
return -EINVAL;
|
1999-03-27 19:44:51 +00:00
|
|
|
if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_WRITE, sw) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:57:39 +00:00
|
|
|
int snd_mixer_read(void *handle, snd_mixer_callbacks_t * callbacks)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
snd_mixer_t *mixer;
|
|
|
|
|
int idx, result, count;
|
|
|
|
|
unsigned int cmd, tmp;
|
|
|
|
|
unsigned char buffer[64];
|
|
|
|
|
|
|
|
|
|
mixer = (snd_mixer_t *) handle;
|
|
|
|
|
if (!mixer)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
count = 0;
|
|
|
|
|
while ((result = read(mixer->fd, &buffer, sizeof(buffer))) > 0) {
|
|
|
|
|
if (result & 7)
|
|
|
|
|
return -EIO;
|
|
|
|
|
if (!callbacks)
|
|
|
|
|
continue;
|
|
|
|
|
for (idx = 0; idx < result; idx += 8) {
|
|
|
|
|
cmd = *(unsigned int *) &buffer[idx];
|
|
|
|
|
tmp = *(unsigned int *) &buffer[idx + 4];
|
1999-03-08 16:51:36 +00:00
|
|
|
if (cmd == SND_MIXER_CHANGED &&
|
|
|
|
|
callbacks->channel_was_changed) {
|
1998-11-27 14:57:39 +00:00
|
|
|
callbacks->channel_was_changed(callbacks->private_data, (int) tmp);
|
|
|
|
|
}
|
1999-03-08 16:51:36 +00:00
|
|
|
if (cmd == SND_MIXER_OUTPUT_CHANGED &&
|
|
|
|
|
callbacks->output_channel_was_changed) {
|
|
|
|
|
callbacks->output_channel_was_changed(callbacks->private_data, (int) tmp);
|
|
|
|
|
}
|
|
|
|
|
if (cmd == SND_MIXER_INPUT_CHANGED &&
|
|
|
|
|
callbacks->input_channel_was_changed) {
|
|
|
|
|
callbacks->input_channel_was_changed(callbacks->private_data, (int) tmp);
|
|
|
|
|
}
|
|
|
|
|
if (cmd == SND_MIXER_SWITCH_CHANGED &&
|
|
|
|
|
callbacks->switch_was_changed) {
|
1998-11-27 14:57:39 +00:00
|
|
|
callbacks->switch_was_changed(callbacks->private_data, (int) tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
count += result >> 3; /* return only number of changes */
|
|
|
|
|
}
|
|
|
|
|
return result >= 0 ? count : -errno;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|