alsa-lib/src/mixer/mixer.c

249 lines
5.4 KiB
C
Raw Normal View History

1998-08-13 15:42:56 +00:00
/*
* Mixer Interface - main file
* Copyright (c) 1998 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 <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
#define SND_FILE_MIXER "/dev/snd/mixerC%iD%i"
#define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
1998-11-27 14:57:39 +00:00
struct snd_mixer {
1998-11-27 14:57:39 +00:00
int card;
int device;
int fd;
} ;
1998-11-27 14:57:39 +00:00
int snd_mixer_open(snd_mixer_t **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
}
int snd_mixer_close(snd_mixer_t *handle)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_mixer_t *mixer;
int res;
mixer = handle;
1998-11-27 14:57:39 +00:00
if (!mixer)
return -EINVAL;
res = close(mixer->fd) < 0 ? -errno : 0;
free(mixer);
return res;
1998-08-13 15:42:56 +00:00
}
int snd_mixer_file_descriptor(snd_mixer_t *handle)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_mixer_t *mixer;
mixer = handle;
1998-11-27 14:57:39 +00:00
if (!mixer)
return -EINVAL;
return mixer->fd;
1998-08-13 15:42:56 +00:00
}
int snd_mixer_info(snd_mixer_t *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 = 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
}
int snd_mixer_elements(snd_mixer_t *handle, snd_mixer_elements_t * elements)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_mixer_t *mixer;
mixer = handle;
1998-11-27 14:57:39 +00:00
if (!mixer)
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENTS, elements) < 0)
1999-03-08 16:51:36 +00:00
return -errno;
return 0;
}
int snd_mixer_routes(snd_mixer_t *handle, snd_mixer_routes_t * routes)
1999-03-08 16:51:36 +00:00
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer)
1999-03-08 16:51:36 +00:00
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ROUTES, routes) < 0)
1999-03-08 16:51:36 +00:00
return -errno;
return 0;
}
int snd_mixer_groups(snd_mixer_t *handle, snd_mixer_groups_t * groups)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_mixer_t *mixer;
mixer = handle;
if (!mixer)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_GROUPS, groups) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_mixer_group(snd_mixer_t *handle, snd_mixer_group_t * group)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_mixer_t *mixer;
mixer = handle;
if (!mixer)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_GROUP, group) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_mixer_element_info(snd_mixer_t *handle, snd_mixer_element_info_t * info)
1998-11-29 16:30:35 +00:00
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer)
1998-11-29 16:30:35 +00:00
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENT_INFO, info) < 0)
1998-11-29 16:30:35 +00:00
return -errno;
return 0;
}
1999-03-08 16:51:36 +00:00
int snd_mixer_element_read(snd_mixer_t *handle, snd_mixer_element_t * element)
1999-03-08 16:51:36 +00:00
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer)
1999-03-08 16:51:36 +00:00
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENT_READ, element) < 0)
1999-03-08 16:51:36 +00:00
return -errno;
return 0;
}
int snd_mixer_element_write(snd_mixer_t *handle, snd_mixer_element_t * element)
1999-03-08 16:51:36 +00:00
{
snd_mixer_t *mixer;
mixer = handle;
if (!mixer)
1999-03-08 16:51:36 +00:00
return -EINVAL;
if (ioctl(mixer->fd, SND_MIXER_IOCTL_ELEMENT_WRITE, element) < 0)
1998-11-29 16:30:35 +00:00
return -errno;
return 0;
}
int snd_mixer_read(snd_mixer_t *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 result, count;
snd_mixer_read_t r;
1998-11-27 14:57:39 +00:00
mixer = handle;
1998-11-27 14:57:39 +00:00
if (!mixer)
return -EINVAL;
count = 0;
while ((result = read(mixer->fd, &r, sizeof(r))) > 0) {
if (result != sizeof(r))
1998-11-27 14:57:39 +00:00
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;
1998-11-27 14:57:39 +00:00
}
count++;
1998-11-27 14:57:39 +00:00
}
return result >= 0 ? count : -errno;
1998-08-13 15:42:56 +00:00
}
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;
}