/* * Mixer Interface - main file * Copyright (c) 1998 by Jaroslav Kysela * * * 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 #include #include #include #include #include #include #include "asoundlib.h" #define SND_FILE_MIXER "/dev/snd/mixerC%iD%i" #define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION( 1, 1, 0 ) typedef struct { int card; int device; int fd; } snd_mixer_t; int snd_mixer_open(void **handle, int card, int device) { int fd, ver; char filename[32]; snd_mixer_t *mixer; *handle = NULL; if (card < 0 || card >= SND_CARDS) 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_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; } int snd_mixer_close(void *handle) { 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; } int snd_mixer_file_descriptor(void *handle) { snd_mixer_t *mixer; mixer = (snd_mixer_t *) handle; if (!mixer) return -EINVAL; return mixer->fd; } int snd_mixer_channels(void *handle) { 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; } int snd_mixer_info(void *handle, snd_mixer_info_t * info) { snd_mixer_t *mixer; mixer = (snd_mixer_t *) handle; if (!mixer || !info) return -EINVAL; if (ioctl(mixer->fd, SND_MIXER_IOCTL_INFO, info) < 0) return -errno; return 0; } int snd_mixer_exact_mode(void *handle, int enable) { 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; } int snd_mixer_channel(void *handle, const char *channel_id) { snd_mixer_t *mixer; snd_mixer_channel_info_t info; int idx, channels, err; mixer = (snd_mixer_t *) handle; if (!mixer || !channel_id) 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; } int snd_mixer_channel_info(void *handle, int channel, snd_mixer_channel_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_INFO, info) < 0) return -errno; return 0; } 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; } int snd_mixer_channel_read(void *handle, int channel, snd_mixer_channel_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_READ, data) < 0) return -errno; return 0; } int snd_mixer_channel_write(void *handle, int channel, snd_mixer_channel_t * data) { snd_mixer_t *mixer; mixer = (snd_mixer_t *) handle; if (!mixer || !data) return -EINVAL; data->channel = channel; if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_WRITE, data) < 0) return -errno; return 0; } int snd_mixer_channel_output_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_OREAD, data) < 0) return -errno; return 0; } int snd_mixer_channel_output_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_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) return -errno; return 0; } int snd_mixer_switch_list(void *handle, snd_switch_list_t * list) { snd_mixer_t *mixer; mixer = (snd_mixer_t *) handle; if (!mixer || !list) return -EINVAL; if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_LIST, &list) < 0) return -errno; return 0; } int snd_mixer_switch_read(void *handle, snd_switch_t * sw) { snd_mixer_t *mixer; mixer = (snd_mixer_t *) handle; if (!mixer || !sw || sw->name[0] == '\0') return -EINVAL; if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_READ, sw) < 0) return -errno; return 0; } int snd_mixer_switch_write(void *handle, snd_switch_t * sw) { snd_mixer_t *mixer; mixer = (snd_mixer_t *) handle; if (!mixer || !sw || sw->name[0] == '\0') return -EINVAL; if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_WRITE, sw) < 0) return -errno; return 0; } int snd_mixer_read(void *handle, snd_mixer_callbacks_t * callbacks) { 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]; if (cmd == SND_MIXER_CHANGED && callbacks->channel_was_changed) { callbacks->channel_was_changed(callbacks->private_data, (int) tmp); } 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) { callbacks->switch_was_changed(callbacks->private_data, (int) tmp); } } count += result >> 3; /* return only number of changes */ } return result >= 0 ? count : -errno; }