1998-08-13 15:42:56 +00:00
|
|
|
/*
|
|
|
|
|
* Control Interface - main file
|
2000-03-01 18:12:30 +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>
|
1998-08-27 20:47:51 +00:00
|
|
|
#include <string.h>
|
1998-08-13 15:42:56 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
2000-06-21 15:00:24 +00:00
|
|
|
#include <assert.h>
|
1998-08-30 21:08:44 +00:00
|
|
|
#include "asoundlib.h"
|
2000-07-28 20:21:12 +00:00
|
|
|
#include "control_local.h"
|
1998-08-13 15:42:56 +00:00
|
|
|
|
1999-02-01 17:57:08 +00:00
|
|
|
#define SND_FILE_CONTROL "/dev/snd/controlC%i"
|
2000-07-15 10:20:32 +00:00
|
|
|
#define SND_CTL_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
|
1999-06-02 00:40:30 +00:00
|
|
|
int snd_ctl_open(snd_ctl_t **handle, int card)
|
1998-11-27 14:57:39 +00:00
|
|
|
{
|
|
|
|
|
int fd, ver;
|
|
|
|
|
char filename[32];
|
|
|
|
|
snd_ctl_t *ctl;
|
1998-08-27 20:47:51 +00:00
|
|
|
|
1999-01-30 18:35:52 +00:00
|
|
|
*handle = NULL;
|
|
|
|
|
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(card >= 0 && card < SND_CARDS);
|
1998-11-27 14:57:39 +00:00
|
|
|
sprintf(filename, SND_FILE_CONTROL, card);
|
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_CTL_IOCTL_PVERSION, &ver) < 0) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
1999-07-22 10:49:39 +00:00
|
|
|
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_CTL_VERSION_MAX)) {
|
1998-11-27 14:57:39 +00:00
|
|
|
close(fd);
|
1999-07-22 10:49:39 +00:00
|
|
|
return -SND_ERROR_INCOMPATIBLE_VERSION;
|
1998-11-27 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
ctl = (snd_ctl_t *) calloc(1, sizeof(snd_ctl_t));
|
|
|
|
|
if (ctl == NULL) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
ctl->card = card;
|
|
|
|
|
ctl->fd = fd;
|
|
|
|
|
*handle = ctl;
|
|
|
|
|
return 0;
|
1998-08-27 20:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
1999-06-02 00:40:30 +00:00
|
|
|
int snd_ctl_close(snd_ctl_t *handle)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:57:39 +00:00
|
|
|
int res;
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle);
|
|
|
|
|
res = close(handle->fd) < 0 ? -errno : 0;
|
|
|
|
|
free(handle);
|
1998-11-27 14:57:39 +00:00
|
|
|
return res;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1999-06-02 00:40:30 +00:00
|
|
|
int snd_ctl_file_descriptor(snd_ctl_t *handle)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle);
|
|
|
|
|
return handle->fd;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
1999-06-02 00:40:30 +00:00
|
|
|
int snd_ctl_hw_info(snd_ctl_t *handle, struct snd_ctl_hw_info *info)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle && info);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_HW_INFO, info) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
int snd_ctl_clist(snd_ctl_t *handle, snd_control_list_t *list)
|
1998-09-08 14:37:31 +00:00
|
|
|
{
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle && list);
|
2000-07-15 10:20:32 +00:00
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_LIST, list) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
1999-03-27 19:44:51 +00:00
|
|
|
return 0;
|
1998-09-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
int snd_ctl_cinfo(snd_ctl_t *handle, snd_control_info_t *info)
|
1998-11-27 14:57:39 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
assert(handle && info && info->id.name[0]);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_INFO, info) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-09-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2000-07-15 10:20:32 +00:00
|
|
|
int snd_ctl_cread(snd_ctl_t *handle, snd_control_t *control)
|
1998-09-08 14:37:31 +00:00
|
|
|
{
|
2000-07-15 10:20:32 +00:00
|
|
|
assert(handle && control && control->id.name[0]);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_READ, control) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_ctl_cwrite(snd_ctl_t *handle, snd_control_t *control)
|
|
|
|
|
{
|
|
|
|
|
assert(handle && control && control->id.name[0]);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_WRITE, control) < 0)
|
1999-06-22 13:18:24 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 15:00:24 +00:00
|
|
|
int snd_ctl_hwdep_info(snd_ctl_t *handle, snd_hwdep_info_t * info)
|
1998-09-08 14:37:31 +00:00
|
|
|
{
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle && info);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_HWDEP_INFO, info) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-09-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 15:00:24 +00:00
|
|
|
int snd_ctl_pcm_info(snd_ctl_t *handle, snd_pcm_info_t * info)
|
1998-09-08 14:37:31 +00:00
|
|
|
{
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle && info);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_PCM_INFO, info) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-09-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 15:00:24 +00:00
|
|
|
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *handle, int subdev)
|
1998-09-08 14:37:31 +00:00
|
|
|
{
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_PCM_PREFER_SUBDEVICE, &subdev) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 15:00:24 +00:00
|
|
|
int snd_ctl_rawmidi_info(snd_ctl_t *handle, snd_rawmidi_info_t * info)
|
1998-11-27 14:57:39 +00:00
|
|
|
{
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle && info);
|
|
|
|
|
if (ioctl(handle->fd, SND_CTL_IOCTL_RAWMIDI_INFO, info) < 0)
|
1998-11-27 14:57:39 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
1998-09-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2000-01-24 10:26:04 +00:00
|
|
|
int snd_ctl_read(snd_ctl_t *handle, snd_ctl_callbacks_t * callbacks)
|
1999-05-04 19:21:28 +00:00
|
|
|
{
|
|
|
|
|
int result, count;
|
2000-07-15 10:20:32 +00:00
|
|
|
snd_ctl_event_t r;
|
|
|
|
|
|
2000-06-21 15:00:24 +00:00
|
|
|
assert(handle);
|
1999-05-04 19:21:28 +00:00
|
|
|
count = 0;
|
2000-06-21 15:00:24 +00:00
|
|
|
while ((result = read(handle->fd, &r, sizeof(r))) > 0) {
|
1999-05-04 19:21:28 +00:00
|
|
|
if (result != sizeof(r))
|
|
|
|
|
return -EIO;
|
|
|
|
|
if (!callbacks)
|
|
|
|
|
continue;
|
2000-07-15 10:20:32 +00:00
|
|
|
switch (r.type) {
|
|
|
|
|
case SND_CTL_EVENT_REBUILD:
|
1999-05-04 19:21:28 +00:00
|
|
|
if (callbacks->rebuild)
|
2000-07-15 10:20:32 +00:00
|
|
|
callbacks->rebuild(handle, callbacks->private_data);
|
|
|
|
|
break;
|
|
|
|
|
case SND_CTL_EVENT_VALUE:
|
|
|
|
|
if (callbacks->value)
|
|
|
|
|
callbacks->value(handle, callbacks->private_data, &r.data.id);
|
|
|
|
|
break;
|
|
|
|
|
case SND_CTL_EVENT_CHANGE:
|
|
|
|
|
if (callbacks->change)
|
|
|
|
|
callbacks->change(handle, callbacks->private_data, &r.data.id);
|
|
|
|
|
break;
|
|
|
|
|
case SND_CTL_EVENT_ADD:
|
|
|
|
|
if (callbacks->add)
|
|
|
|
|
callbacks->add(handle, callbacks->private_data, &r.data.id);
|
1999-05-04 19:21:28 +00:00
|
|
|
break;
|
2000-07-15 10:20:32 +00:00
|
|
|
case SND_CTL_EVENT_REMOVE:
|
|
|
|
|
if (callbacks->remove)
|
|
|
|
|
callbacks->remove(handle, callbacks->private_data, &r.data.id);
|
1999-05-04 19:21:28 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
return result >= 0 ? count : -errno;
|
|
|
|
|
}
|