Coding style...

This commit is contained in:
Jaroslav Kysela 1998-11-27 14:57:39 +00:00
parent 2605e4d7dd
commit a741225e6e
6 changed files with 1345 additions and 1223 deletions

View file

@ -30,532 +30,568 @@
#define SND_FILE_CONTROL "/dev/snd/control%i" #define SND_FILE_CONTROL "/dev/snd/control%i"
#define SND_CTL_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 ) #define SND_CTL_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 )
typedef struct { typedef struct {
int card; int card;
int fd; int fd;
} snd_ctl_t; } snd_ctl_t;
int snd_ctl_open( void **handle, int card )
{
int fd, ver;
char filename[32];
snd_ctl_t *ctl;
*handle = NULL; int snd_ctl_open(void **handle, int card)
if ( card < 0 || card >= SND_CARDS ) return -EINVAL; {
sprintf( filename, SND_FILE_CONTROL, card ); int fd, ver;
if ( (fd = open( filename, O_RDWR )) < 0 ) return -errno; char filename[32];
if ( ioctl( fd, SND_CTL_IOCTL_PVERSION, &ver ) < 0 ) { snd_ctl_t *ctl;
close( fd );
return -errno; *handle = NULL;
} if (card < 0 || card >= SND_CARDS)
if ( SND_PROTOCOL_UNCOMPATIBLE( ver, SND_CTL_VERSION_MAX ) ) { return -EINVAL;
close( fd ); sprintf(filename, SND_FILE_CONTROL, card);
return -SND_ERROR_UNCOMPATIBLE_VERSION; if ((fd = open(filename, O_RDWR)) < 0)
} return -errno;
ctl = (snd_ctl_t *)calloc( 1, sizeof( snd_ctl_t ) ); if (ioctl(fd, SND_CTL_IOCTL_PVERSION, &ver) < 0) {
if ( ctl == NULL ) { close(fd);
close( fd ); return -errno;
return -ENOMEM; }
} if (SND_PROTOCOL_UNCOMPATIBLE(ver, SND_CTL_VERSION_MAX)) {
ctl -> card = card; close(fd);
ctl -> fd = fd; return -SND_ERROR_UNCOMPATIBLE_VERSION;
*handle = ctl; }
return 0; 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;
} }
int snd_ctl_close( void *handle ) int snd_ctl_close(void *handle)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
int res; int res;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
res = close( ctl -> fd ) < 0 ? -errno : 0; return -EINVAL;
free( ctl ); res = close(ctl->fd) < 0 ? -errno : 0;
return res; free(ctl);
return res;
} }
int snd_ctl_file_descriptor( void *handle ) int snd_ctl_file_descriptor(void *handle)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
return ctl -> fd; return -EINVAL;
return ctl->fd;
} }
int snd_ctl_hw_info( void *handle, struct snd_ctl_hw_info *info ) int snd_ctl_hw_info(void *handle, struct snd_ctl_hw_info *info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_HW_INFO, info ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_HW_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_switches( void *handle ) int snd_ctl_switches(void *handle)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
int result; int result;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_SWITCHES, &result ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_SWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_ctl_switch( void *handle, const char *switch_id ) int snd_ctl_switch(void *handle, const char *switch_id)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
struct snd_ctl_switch uswitch; struct snd_ctl_switch uswitch;
int idx, switches, err; int idx, switches, err;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_ctl_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_ctl_switches( handle )) < 0 ) /* info about switches should be cached in the snd_ctl_t structure */
return switches; if ((switches = snd_ctl_switches(handle)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_ctl_switch_read( handle, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_ctl_switch_read(handle, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_ctl_switch_read( void *handle, int switchn, struct snd_ctl_switch *data ) int snd_ctl_switch_read(void *handle, int switchn, struct snd_ctl_switch *data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_SWITCH_READ, data ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_SWITCH_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_switch_write( void *handle, int switchn, struct snd_ctl_switch *data ) int snd_ctl_switch_write(void *handle, int switchn, struct snd_ctl_switch *data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_SWITCH_WRITE, data ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_SWITCH_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_pcm_info( void *handle, int dev, snd_pcm_info_t *info ) int snd_ctl_pcm_info(void *handle, int dev, snd_pcm_info_t * info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_INFO, info ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_pcm_playback_info( void *handle, int dev, snd_pcm_playback_info_t *info ) int snd_ctl_pcm_playback_info(void *handle, int dev, snd_pcm_playback_info_t * info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_PLAYBACK_INFO, info ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_PLAYBACK_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_pcm_record_info( void *handle, int dev, snd_pcm_record_info_t *info ) int snd_ctl_pcm_record_info(void *handle, int dev, snd_pcm_record_info_t * info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_RECORD_INFO, info ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_RECORD_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_pcm_playback_switches( void *handle, int dev ) int snd_ctl_pcm_playback_switches(void *handle, int dev)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
int result; int result;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_PSWITCHES, &result ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_PSWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_ctl_pcm_playback_switch( void *handle, int dev, const char *switch_id ) int snd_ctl_pcm_playback_switch(void *handle, int dev, const char *switch_id)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
snd_pcm_switch_t uswitch; snd_pcm_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_ctl_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_ctl_pcm_playback_switches( handle, dev )) < 0 ) /* info about switches should be cached in the snd_ctl_t structure */
return switches; if ((switches = snd_ctl_pcm_playback_switches(handle, dev)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_ctl_pcm_playback_switch_read( handle, dev, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_ctl_pcm_playback_switch_read(handle, dev, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_ctl_pcm_playback_switch_read( void *handle, int dev, int switchn, snd_pcm_switch_t *data ) int snd_ctl_pcm_playback_switch_read(void *handle, int dev, int switchn, snd_pcm_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_PSWITCH_READ, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_PSWITCH_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_pcm_playback_switch_write( void *handle, int dev, int switchn, snd_pcm_switch_t *data ) int snd_ctl_pcm_playback_switch_write(void *handle, int dev, int switchn, snd_pcm_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_PSWITCH_WRITE, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_PSWITCH_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_pcm_record_switches( void *handle, int dev ) int snd_ctl_pcm_record_switches(void *handle, int dev)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
int result; int result;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_RSWITCHES, &result ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_RSWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_ctl_pcm_record_switch( void *handle, int dev, const char *switch_id ) int snd_ctl_pcm_record_switch(void *handle, int dev, const char *switch_id)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
snd_pcm_switch_t uswitch; snd_pcm_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_ctl_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_ctl_pcm_record_switches( handle, dev )) < 0 ) /* info about switches should be cached in the snd_ctl_t structure */
return switches; if ((switches = snd_ctl_pcm_record_switches(handle, dev)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_ctl_pcm_record_switch_read( handle, dev, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_ctl_pcm_record_switch_read(handle, dev, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_ctl_pcm_record_switch_read( void *handle, int dev, int switchn, snd_pcm_switch_t *data ) int snd_ctl_pcm_record_switch_read(void *handle, int dev, int switchn, snd_pcm_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_RSWITCH_READ, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_RSWITCH_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_pcm_record_switch_write( void *handle, int dev, int switchn, snd_pcm_switch_t *data ) int snd_ctl_pcm_record_switch_write(void *handle, int dev, int switchn, snd_pcm_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_PCM_RSWITCH_WRITE, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_RSWITCH_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_mixer_info( void *handle, int dev, snd_mixer_info_t *info ) int snd_ctl_mixer_info(void *handle, int dev, snd_mixer_info_t * info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_INFO, info ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_mixer_switches( void *handle, int dev ) int snd_ctl_mixer_switches(void *handle, int dev)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
int result; int result;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_SWITCHES, &result ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_SWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_ctl_mixer_switch( void *handle, int dev, const char *switch_id ) int snd_ctl_mixer_switch(void *handle, int dev, const char *switch_id)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
snd_mixer_switch_t uswitch; snd_mixer_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_ctl_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_ctl_mixer_switches( handle, dev )) < 0 ) /* info about switches should be cached in the snd_ctl_t structure */
return switches; if ((switches = snd_ctl_mixer_switches(handle, dev)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_ctl_mixer_switch_read( handle, dev, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_ctl_mixer_switch_read(handle, dev, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_ctl_mixer_switch_read( void *handle, int dev, int switchn, snd_mixer_switch_t *data ) int snd_ctl_mixer_switch_read(void *handle, int dev, int switchn, snd_mixer_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_SWITCH_READ, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_SWITCH_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_mixer_switch_write( void *handle, int dev, int switchn, snd_mixer_switch_t *data ) int snd_ctl_mixer_switch_write(void *handle, int dev, int switchn, snd_mixer_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_MIXER_SWITCH_WRITE, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_MIXER_SWITCH_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_rawmidi_info( void *handle, int dev, snd_rawmidi_info_t *info ) int snd_ctl_rawmidi_info(void *handle, int dev, snd_rawmidi_info_t * info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_INFO, info ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_rawmidi_output_info( void *handle, int dev, snd_rawmidi_output_info_t *info ) int snd_ctl_rawmidi_output_info(void *handle, int dev, snd_rawmidi_output_info_t * info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_OUTPUT_INFO, info ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_OUTPUT_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_rawmidi_input_info( void *handle, int dev, snd_rawmidi_input_info_t *info ) int snd_ctl_rawmidi_input_info(void *handle, int dev, snd_rawmidi_input_info_t * info)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_INPUT_INFO, info ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_INPUT_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_rawmidi_output_switches( void *handle, int dev ) int snd_ctl_rawmidi_output_switches(void *handle, int dev)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
int result; int result;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_OSWITCHES, &result ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_OSWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_ctl_rawmidi_output_switch( void *handle, int dev, const char *switch_id ) int snd_ctl_rawmidi_output_switch(void *handle, int dev, const char *switch_id)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
snd_rawmidi_switch_t uswitch; snd_rawmidi_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_ctl_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_ctl_rawmidi_output_switches( handle, dev )) < 0 ) /* info about switches should be cached in the snd_ctl_t structure */
return switches; if ((switches = snd_ctl_rawmidi_output_switches(handle, dev)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_ctl_rawmidi_output_switch_read( handle, dev, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_ctl_rawmidi_output_switch_read(handle, dev, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_ctl_rawmidi_output_switch_read( void *handle, int dev, int switchn, snd_rawmidi_switch_t *data ) int snd_ctl_rawmidi_output_switch_read(void *handle, int dev, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_OSWITCH_READ, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_OSWITCH_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_rawmidi_output_switch_write( void *handle, int dev, int switchn, snd_rawmidi_switch_t *data ) int snd_ctl_rawmidi_output_switch_write(void *handle, int dev, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_OSWITCH_WRITE, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_OSWITCH_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_rawmidi_input_switches( void *handle, int dev ) int snd_ctl_rawmidi_input_switches(void *handle, int dev)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
int result; int result;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) return -EINVAL;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_ISWITCHES, &result ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_ISWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_ctl_rawmidi_input_switch( void *handle, int dev, const char *switch_id ) int snd_ctl_rawmidi_input_switch(void *handle, int dev, const char *switch_id)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
snd_rawmidi_switch_t uswitch; snd_rawmidi_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_ctl_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_ctl_rawmidi_input_switches( handle, dev )) < 0 ) /* info about switches should be cached in the snd_ctl_t structure */
return switches; if ((switches = snd_ctl_rawmidi_input_switches(handle, dev)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_ctl_rawmidi_input_switch_read( handle, dev, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_ctl_rawmidi_input_switch_read(handle, dev, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_ctl_rawmidi_input_switch_read( void *handle, int dev, int switchn, snd_rawmidi_switch_t *data ) int snd_ctl_rawmidi_input_switch_read(void *handle, int dev, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_ISWITCH_READ, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_ISWITCH_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_ctl_rawmidi_input_switch_write( void *handle, int dev, int switchn, snd_rawmidi_switch_t *data ) int snd_ctl_rawmidi_input_switch_write(void *handle, int dev, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_ctl_t *ctl; snd_ctl_t *ctl;
ctl = (snd_ctl_t *)handle; ctl = (snd_ctl_t *) handle;
if ( !ctl ) return -EINVAL; if (!ctl)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_DEVICE, &dev) < 0)
if ( ioctl( ctl -> fd, SND_CTL_IOCTL_RAWMIDI_ISWITCH_WRITE, data ) < 0 ) return -errno;
return -errno; if (ioctl(ctl->fd, SND_CTL_IOCTL_RAWMIDI_ISWITCH_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }

View file

@ -26,17 +26,19 @@
#include <string.h> #include <string.h>
#include "asoundlib.h" #include "asoundlib.h"
static const char *snd_error_codes[] = { static const char *snd_error_codes[] =
"Sound protocol isn't compatible" {
"Sound protocol isn't compatible"
}; };
const char *snd_strerror( int errnum ) const char *snd_strerror(int errnum)
{ {
if ( errnum < 0 ) errnum = -errnum; if (errnum < 0)
if ( errnum < SND_ERROR_BEGIN ) errnum = -errnum;
return (const char *)strerror( errnum ); if (errnum < SND_ERROR_BEGIN)
errnum -= SND_ERROR_BEGIN; return (const char *) strerror(errnum);
if ( errnum >= sizeof( snd_error_codes ) / sizeof( const char * ) ) errnum -= SND_ERROR_BEGIN;
return "Unknown error"; if (errnum >= sizeof(snd_error_codes) / sizeof(const char *))
return snd_error_codes[ errnum ]; return "Unknown error";
return snd_error_codes[errnum];
} }

View file

@ -30,236 +30,254 @@
#define SND_FILE_MIXER "/dev/snd/mixer%i%i" #define SND_FILE_MIXER "/dev/snd/mixer%i%i"
#define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION( 1, 1, 0 ) #define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION( 1, 1, 0 )
typedef struct { typedef struct {
int card; int card;
int device; int device;
int fd; int fd;
} snd_mixer_t; } 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; int snd_mixer_open(void **handle, int card, int device)
if ( card < 0 || card >= SND_CARDS ) return -EINVAL; {
sprintf( filename, SND_FILE_MIXER, card, device ); int fd, ver;
if ( (fd = open( filename, O_RDWR )) < 0 ) return -errno; char filename[32];
if ( ioctl( fd, SND_MIXER_IOCTL_PVERSION, &ver ) < 0 ) { snd_mixer_t *mixer;
close( fd );
return -errno; *handle = NULL;
} if (card < 0 || card >= SND_CARDS)
if ( SND_PROTOCOL_UNCOMPATIBLE( ver, SND_MIXER_VERSION_MAX ) ) { return -EINVAL;
close( fd ); sprintf(filename, SND_FILE_MIXER, card, device);
return -SND_ERROR_UNCOMPATIBLE_VERSION; if ((fd = open(filename, O_RDWR)) < 0)
} return -errno;
mixer = (snd_mixer_t *)calloc( 1, sizeof( snd_mixer_t ) ); if (ioctl(fd, SND_MIXER_IOCTL_PVERSION, &ver) < 0) {
if ( mixer == NULL ) { close(fd);
close( fd ); return -errno;
return -ENOMEM; }
} if (SND_PROTOCOL_UNCOMPATIBLE(ver, SND_MIXER_VERSION_MAX)) {
mixer -> card = card; close(fd);
mixer -> device = device; return -SND_ERROR_UNCOMPATIBLE_VERSION;
mixer -> fd = fd; }
*handle = mixer; mixer = (snd_mixer_t *) calloc(1, sizeof(snd_mixer_t));
return 0; 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 ) int snd_mixer_close(void *handle)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
int res; int res;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
res = close( mixer -> fd ) < 0 ? -errno : 0; return -EINVAL;
free( mixer ); res = close(mixer->fd) < 0 ? -errno : 0;
return res; free(mixer);
return res;
} }
int snd_mixer_file_descriptor( void *handle ) int snd_mixer_file_descriptor(void *handle)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
return mixer -> fd; return -EINVAL;
return mixer->fd;
} }
int snd_mixer_channels( void *handle ) int snd_mixer_channels(void *handle)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
int result; int result;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_CHANNELS, &result ) < 0 ) return -EINVAL;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNELS, &result) < 0)
return result; return -errno;
return result;
} }
int snd_mixer_info( void *handle, snd_mixer_info_t *info ) int snd_mixer_info(void *handle, snd_mixer_info_t * info)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_INFO, info ) < 0 ) return -EINVAL;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_mixer_exact_mode( void *handle, int enable ) int snd_mixer_exact_mode(void *handle, int enable)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_EXACT, &enable ) < 0 ) return -EINVAL;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_EXACT, &enable) < 0)
return 0; return -errno;
return 0;
} }
int snd_mixer_channel( void *handle, const char *channel_id ) int snd_mixer_channel(void *handle, const char *channel_id)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
snd_mixer_channel_info_t info; snd_mixer_channel_info_t info;
int idx, channels, err; int idx, channels, err;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about channels should be cached in the snd_mixer_t structure */ /* bellow implementation isn't optimized for speed */
if ( (channels = snd_mixer_channels( handle )) < 0 ) /* info about channels should be cached in the snd_mixer_t structure */
return channels; if ((channels = snd_mixer_channels(handle)) < 0)
for ( idx = 0; idx < channels; idx++ ) { return channels;
if ( (err = snd_mixer_channel_info( handle, idx, &info )) < 0 ) for (idx = 0; idx < channels; idx++) {
return err; if ((err = snd_mixer_channel_info(handle, idx, &info)) < 0)
if ( !strncmp( channel_id, info.name, sizeof( info.name ) ) ) return err;
return idx; if (!strncmp(channel_id, info.name, sizeof(info.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_mixer_channel_info( void *handle, int channel, snd_mixer_channel_info_t *info ) int snd_mixer_channel_info(void *handle, int channel, snd_mixer_channel_info_t * info)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
info -> channel = channel; return -EINVAL;
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_CHANNEL_INFO, info ) < 0 ) info->channel = channel;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_mixer_channel_read( void *handle, int channel, snd_mixer_channel_t *data ) int snd_mixer_channel_read(void *handle, int channel, snd_mixer_channel_t * data)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
data -> channel = channel; return -EINVAL;
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_CHANNEL_READ, data ) < 0 ) data->channel = channel;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_mixer_channel_write( void *handle, int channel, snd_mixer_channel_t *data ) int snd_mixer_channel_write(void *handle, int channel, snd_mixer_channel_t * data)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
data -> channel = channel; return -EINVAL;
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_CHANNEL_WRITE, data ) < 0 ) data->channel = channel;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_CHANNEL_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_mixer_switches( void *handle ) int snd_mixer_switches(void *handle)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
int result; int result;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_SWITCHES, &result ) < 0 ) return -EINVAL;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_mixer_switch( void *handle, const char *switch_id ) int snd_mixer_switch(void *handle, const char *switch_id)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
snd_mixer_switch_t uswitch; snd_mixer_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_mixer_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_mixer_switches( handle )) < 0 ) /* info about switches should be cached in the snd_mixer_t structure */
return switches; if ((switches = snd_mixer_switches(handle)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_mixer_switch_read( handle, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_mixer_switch_read(handle, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_mixer_switch_read( void *handle, int switchn, snd_mixer_switch_t *data ) int snd_mixer_switch_read(void *handle, int switchn, snd_mixer_switch_t * data)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_SWITCH_READ, data ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_READ, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_mixer_switch_write( void *handle, int switchn, snd_mixer_switch_t *data ) int snd_mixer_switch_write(void *handle, int switchn, snd_mixer_switch_t * data)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( mixer -> fd, SND_MIXER_IOCTL_SWITCH_WRITE, data ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(mixer->fd, SND_MIXER_IOCTL_SWITCH_WRITE, data) < 0)
return 0; return -errno;
return 0;
} }
int snd_mixer_read( void *handle, snd_mixer_callbacks_t *callbacks ) int snd_mixer_read(void *handle, snd_mixer_callbacks_t * callbacks)
{ {
snd_mixer_t *mixer; snd_mixer_t *mixer;
int idx, result, count; int idx, result, count;
unsigned int cmd, tmp; unsigned int cmd, tmp;
unsigned char buffer[ 64 ]; unsigned char buffer[64];
mixer = (snd_mixer_t *)handle; mixer = (snd_mixer_t *) handle;
if ( !mixer ) return -EINVAL; if (!mixer)
count = 0; return -EINVAL;
while ( (result = read( mixer -> fd, &buffer, sizeof( buffer ))) > 0 ) { count = 0;
if ( result & 7 ) return -EIO; while ((result = read(mixer->fd, &buffer, sizeof(buffer))) > 0) {
if ( !callbacks ) continue; if (result & 7)
for ( idx = 0; idx < result; idx += 8 ) { return -EIO;
cmd = *(unsigned int *)&buffer[ idx ]; if (!callbacks)
tmp = *(unsigned int *)&buffer[ idx + 4 ]; continue;
if ( cmd == 0 && callbacks -> channel_was_changed ) { for (idx = 0; idx < result; idx += 8) {
callbacks -> channel_was_changed( callbacks -> private_data, (int)tmp ); cmd = *(unsigned int *) &buffer[idx];
} tmp = *(unsigned int *) &buffer[idx + 4];
if ( cmd == 1 && callbacks -> switch_was_changed ) { if (cmd == 0 && callbacks->channel_was_changed) {
callbacks -> switch_was_changed( callbacks -> private_data, (int)tmp ); callbacks->channel_was_changed(callbacks->private_data, (int) tmp);
} }
} if (cmd == 1 && callbacks->switch_was_changed) {
count += result >> 3; /* return only number of changes */ callbacks->switch_was_changed(callbacks->private_data, (int) tmp);
} }
return result >= 0 ? count : -errno; }
count += result >> 3; /* return only number of changes */
}
return result >= 0 ? count : -errno;
} }

View file

@ -30,381 +30,413 @@
#define SND_FILE_PCM "/dev/snd/pcm%i%i" #define SND_FILE_PCM "/dev/snd/pcm%i%i"
#define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 1 ) #define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 1 )
typedef struct { typedef struct {
int card; int card;
int device; int device;
int fd; int fd;
} snd_pcm_t; } snd_pcm_t;
int snd_pcm_open( void **handle, int card, int device, int mode )
{
int fd, ver;
char filename[32];
snd_pcm_t *pcm;
*handle = NULL; int snd_pcm_open(void **handle, int card, int device, int mode)
if ( card < 0 || card >= SND_CARDS ) return -EINVAL; {
sprintf( filename, SND_FILE_PCM, card, device ); int fd, ver;
if ( (fd = open( filename, mode )) < 0 ) return -errno; char filename[32];
if ( ioctl( fd, SND_PCM_IOCTL_PVERSION, &ver ) < 0 ) { snd_pcm_t *pcm;
close( fd );
return -errno; *handle = NULL;
} if (card < 0 || card >= SND_CARDS)
if ( SND_PROTOCOL_UNCOMPATIBLE( ver, SND_PCM_VERSION_MAX ) ) { return -EINVAL;
close( fd ); sprintf(filename, SND_FILE_PCM, card, device);
return -SND_ERROR_UNCOMPATIBLE_VERSION; if ((fd = open(filename, mode)) < 0)
} return -errno;
pcm = (snd_pcm_t *)calloc( 1, sizeof( snd_pcm_t ) ); if (ioctl(fd, SND_PCM_IOCTL_PVERSION, &ver) < 0) {
if ( pcm == NULL ) { close(fd);
close( fd ); return -errno;
return -ENOMEM; }
} if (SND_PROTOCOL_UNCOMPATIBLE(ver, SND_PCM_VERSION_MAX)) {
pcm -> card = card; close(fd);
pcm -> device = device; return -SND_ERROR_UNCOMPATIBLE_VERSION;
pcm -> fd = fd; }
*handle = pcm; pcm = (snd_pcm_t *) calloc(1, sizeof(snd_pcm_t));
return 0; if (pcm == NULL) {
close(fd);
return -ENOMEM;
}
pcm->card = card;
pcm->device = device;
pcm->fd = fd;
*handle = pcm;
return 0;
} }
int snd_pcm_close( void *handle ) int snd_pcm_close(void *handle)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
int res; int res;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
res = close( pcm -> fd ) < 0 ? -errno : 0; return -EINVAL;
free( pcm ); res = close(pcm->fd) < 0 ? -errno : 0;
return res; free(pcm);
return res;
} }
int snd_pcm_file_descriptor( void *handle ) int snd_pcm_file_descriptor(void *handle)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
return pcm -> fd; return -EINVAL;
return pcm->fd;
} }
int snd_pcm_block_mode( void *handle, int enable ) int snd_pcm_block_mode(void *handle, int enable)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
long flags; long flags;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( fcntl( pcm -> fd, F_GETFL, &flags ) < 0 ) return -EINVAL;
return -errno; if (fcntl(pcm->fd, F_GETFL, &flags) < 0)
if ( enable ) return -errno;
flags |= O_NONBLOCK; if (enable)
else flags |= O_NONBLOCK;
flags &= ~O_NONBLOCK; else
if ( fcntl( pcm -> fd, F_SETFL, &flags ) < 0 ) flags &= ~O_NONBLOCK;
return -errno; if (fcntl(pcm->fd, F_SETFL, &flags) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_info( void *handle, snd_pcm_info_t *info ) int snd_pcm_info(void *handle, snd_pcm_info_t * info)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_INFO, info ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_playback_info( void *handle, snd_pcm_playback_info_t *info ) int snd_pcm_playback_info(void *handle, snd_pcm_playback_info_t * info)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PLAYBACK_INFO, info ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_record_info( void *handle, snd_pcm_record_info_t *info ) int snd_pcm_record_info(void *handle, snd_pcm_record_info_t * info)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RECORD_INFO, info ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_RECORD_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_playback_switches( void *handle ) int snd_pcm_playback_switches(void *handle)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
int result; int result;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PSWITCHES, &result ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_PSWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_pcm_playback_switch( void *handle, const char *switch_id ) int snd_pcm_playback_switch(void *handle, const char *switch_id)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
snd_pcm_switch_t uswitch; snd_pcm_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_mixer_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_pcm_playback_switches( handle )) < 0 ) /* info about switches should be cached in the snd_mixer_t structure */
return switches; if ((switches = snd_pcm_playback_switches(handle)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_pcm_playback_switch_read( handle, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_pcm_playback_switch_read(handle, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_pcm_playback_switch_read( void *handle, int switchn, snd_pcm_switch_t *data ) int snd_pcm_playback_switch_read(void *handle, int switchn, snd_pcm_switch_t * data)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PSWITCH_READ, data ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_PSWITCH_READ, data) < 0)
return 0; return -errno;
} return 0;
int snd_pcm_playback_switch_write( void *handle, int switchn, snd_pcm_switch_t *data )
{
snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle;
if ( !pcm ) return -EINVAL;
data -> switchn = switchn;
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PSWITCH_WRITE, data ) < 0 )
return -errno;
return 0;
}
int snd_pcm_record_switches( void *handle )
{
snd_pcm_t *pcm;
int result;
pcm = (snd_pcm_t *)handle;
if ( !pcm ) return -EINVAL;
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RSWITCHES, &result ) < 0 )
return -errno;
return result;
} }
int snd_pcm_record_switch( void *handle, const char *switch_id ) int snd_pcm_playback_switch_write(void *handle, int switchn, snd_pcm_switch_t * data)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
snd_pcm_switch_t uswitch;
int idx, switches, err;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_mixer_t structure */ data->switchn = switchn;
if ( (switches = snd_pcm_record_switches( handle )) < 0 ) if (ioctl(pcm->fd, SND_PCM_IOCTL_PSWITCH_WRITE, data) < 0)
return switches; return -errno;
for ( idx = 0; idx < switches; idx++ ) { return 0;
if ( (err = snd_pcm_record_switch_read( handle, idx, &uswitch )) < 0 )
return err;
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) )
return idx;
}
return -EINVAL;
} }
int snd_pcm_record_switch_read( void *handle, int switchn, snd_pcm_switch_t *data ) int snd_pcm_record_switches(void *handle)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
int result;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RSWITCH_READ, data ) < 0 ) if (ioctl(pcm->fd, SND_PCM_IOCTL_RSWITCHES, &result) < 0)
return -errno; return -errno;
return 0; return result;
}
int snd_pcm_record_switch_write( void *handle, int switchn, snd_pcm_switch_t *data )
{
snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle;
if ( !pcm ) return -EINVAL;
data -> switchn = switchn;
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RSWITCH_WRITE, data ) < 0 )
return -errno;
return 0;
}
int snd_pcm_playback_format( void *handle, snd_pcm_format_t *format )
{
snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle;
if ( !pcm ) return -EINVAL;
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PLAYBACK_FORMAT, format ) < 0 )
return -errno;
return 0;
} }
int snd_pcm_record_format( void *handle, snd_pcm_format_t *format ) int snd_pcm_record_switch(void *handle, const char *switch_id)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
snd_pcm_switch_t uswitch;
pcm = (snd_pcm_t *)handle; int idx, switches, err;
if ( !pcm ) return -EINVAL;
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RECORD_FORMAT, format ) < 0 ) pcm = (snd_pcm_t *) handle;
return -errno; if (!pcm)
return 0; return -EINVAL;
/* bellow implementation isn't optimized for speed */
/* info about switches should be cached in the snd_mixer_t structure */
if ((switches = snd_pcm_record_switches(handle)) < 0)
return switches;
for (idx = 0; idx < switches; idx++) {
if ((err = snd_pcm_record_switch_read(handle, idx, &uswitch)) < 0)
return err;
if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
return idx;
}
return -EINVAL;
} }
int snd_pcm_playback_params( void *handle, snd_pcm_playback_params_t *params ) int snd_pcm_record_switch_read(void *handle, int switchn, snd_pcm_switch_t * data)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PLAYBACK_PARAMS, params ) < 0 ) return -EINVAL;
return -errno; data->switchn = switchn;
return 0; if (ioctl(pcm->fd, SND_PCM_IOCTL_RSWITCH_READ, data) < 0)
return -errno;
return 0;
} }
int snd_pcm_record_params( void *handle, snd_pcm_record_params_t *params ) int snd_pcm_record_switch_write(void *handle, int switchn, snd_pcm_switch_t * data)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RECORD_PARAMS, params ) < 0 ) return -EINVAL;
return -errno; data->switchn = switchn;
return 0; if (ioctl(pcm->fd, SND_PCM_IOCTL_RSWITCH_WRITE, data) < 0)
return -errno;
return 0;
} }
int snd_pcm_playback_status( void *handle, snd_pcm_playback_status_t *status ) int snd_pcm_playback_format(void *handle, snd_pcm_format_t * format)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PLAYBACK_STATUS, status ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_FORMAT, format) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_record_status( void *handle, snd_pcm_record_status_t *status ) int snd_pcm_record_format(void *handle, snd_pcm_format_t * format)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RECORD_STATUS, status ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_RECORD_FORMAT, format) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_drain_playback( void *handle ) int snd_pcm_playback_params(void *handle, snd_pcm_playback_params_t * params)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_DRAIN_PLAYBACK ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_PARAMS, params) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_flush_playback( void *handle ) int snd_pcm_record_params(void *handle, snd_pcm_record_params_t * params)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_FLUSH_PLAYBACK ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_RECORD_PARAMS, params) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_flush_record( void *handle ) int snd_pcm_playback_status(void *handle, snd_pcm_playback_status_t * status)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_FLUSH_RECORD ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_STATUS, status) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_playback_pause( void *handle, int enable ) int snd_pcm_record_status(void *handle, snd_pcm_record_status_t * status)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PLAYBACK_PAUSE, &enable ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_RECORD_STATUS, status) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_playback_time( void *handle, int enable ) int snd_pcm_drain_playback(void *handle)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_PLAYBACK_TIME, &enable ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_DRAIN_PLAYBACK) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_record_time( void *handle, int enable ) int snd_pcm_flush_playback(void *handle)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
pcm = (snd_pcm_t *)handle; pcm = (snd_pcm_t *) handle;
if ( !pcm ) return -EINVAL; if (!pcm)
if ( ioctl( pcm -> fd, SND_PCM_IOCTL_RECORD_TIME, &enable ) < 0 ) return -EINVAL;
return -errno; if (ioctl(pcm->fd, SND_PCM_IOCTL_FLUSH_PLAYBACK) < 0)
return 0; return -errno;
return 0;
} }
ssize_t snd_pcm_write( void *handle, const void *buffer, size_t size ) int snd_pcm_flush_record(void *handle)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
ssize_t result;
pcm = (snd_pcm_t *) handle;
pcm = (snd_pcm_t *)handle; if (!pcm)
if ( !pcm ) return -EINVAL; return -EINVAL;
result = write( pcm -> fd, buffer, size ); if (ioctl(pcm->fd, SND_PCM_IOCTL_FLUSH_RECORD) < 0)
if ( result < 0 ) return -errno; return -errno;
return result; return 0;
} }
ssize_t snd_pcm_read( void *handle, void *buffer, size_t size ) int snd_pcm_playback_pause(void *handle, int enable)
{ {
snd_pcm_t *pcm; snd_pcm_t *pcm;
ssize_t result;
pcm = (snd_pcm_t *) handle;
pcm = (snd_pcm_t *)handle; if (!pcm)
if ( !pcm ) return -EINVAL; return -EINVAL;
result = read( pcm -> fd, buffer, size ); if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_PAUSE, &enable) < 0)
if ( result < 0 ) return -errno; return -errno;
return result; return 0;
}
int snd_pcm_playback_time(void *handle, int enable)
{
snd_pcm_t *pcm;
pcm = (snd_pcm_t *) handle;
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_TIME, &enable) < 0)
return -errno;
return 0;
}
int snd_pcm_record_time(void *handle, int enable)
{
snd_pcm_t *pcm;
pcm = (snd_pcm_t *) handle;
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_RECORD_TIME, &enable) < 0)
return -errno;
return 0;
}
ssize_t snd_pcm_write(void *handle, const void *buffer, size_t size)
{
snd_pcm_t *pcm;
ssize_t result;
pcm = (snd_pcm_t *) handle;
if (!pcm)
return -EINVAL;
result = write(pcm->fd, buffer, size);
if (result < 0)
return -errno;
return result;
}
ssize_t snd_pcm_read(void *handle, void *buffer, size_t size)
{
snd_pcm_t *pcm;
ssize_t result;
pcm = (snd_pcm_t *) handle;
if (!pcm)
return -EINVAL;
result = read(pcm->fd, buffer, size);
if (result < 0)
return -errno;
return result;
} }

View file

@ -30,114 +30,123 @@
#define SND_FILE_PCM_LB "/proc/asound/%i/pcm%i%s" #define SND_FILE_PCM_LB "/proc/asound/%i/pcm%i%s"
#define SND_PCM_LB_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 ) #define SND_PCM_LB_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 )
typedef struct { typedef struct {
int card; int card;
int device; int device;
int fd; int fd;
} snd_pcm_loopback_t; } snd_pcm_loopback_t;
int snd_pcm_loopback_open( void **handle, int card, int device, int mode )
{
int fd, ver;
char filename[32];
snd_pcm_loopback_t *lb;
*handle = NULL; int snd_pcm_loopback_open(void **handle, int card, int device, int mode)
if ( card < 0 || card >= SND_CARDS ) return -EINVAL; {
sprintf( filename, SND_FILE_PCM_LB, card, device, int fd, ver;
mode == SND_PCM_LB_OPEN_RECORD ? "r" : "p" ); char filename[32];
if ( (fd = open( filename, mode )) < 0 ) return -errno; snd_pcm_loopback_t *lb;
if ( ioctl( fd, SND_PCM_IOCTL_PVERSION, &ver ) < 0 ) {
close( fd ); *handle = NULL;
return -errno; if (card < 0 || card >= SND_CARDS)
} return -EINVAL;
if ( SND_PROTOCOL_UNCOMPATIBLE( ver, SND_PCM_LB_VERSION_MAX ) ) { sprintf(filename, SND_FILE_PCM_LB, card, device,
close( fd ); mode == SND_PCM_LB_OPEN_RECORD ? "r" : "p");
return -SND_ERROR_UNCOMPATIBLE_VERSION; if ((fd = open(filename, mode)) < 0)
} return -errno;
lb = (snd_pcm_loopback_t *)calloc( 1, sizeof( snd_pcm_loopback_t ) ); if (ioctl(fd, SND_PCM_IOCTL_PVERSION, &ver) < 0) {
if ( lb == NULL ) { close(fd);
close( fd ); return -errno;
return -ENOMEM; }
} if (SND_PROTOCOL_UNCOMPATIBLE(ver, SND_PCM_LB_VERSION_MAX)) {
lb -> card = card; close(fd);
lb -> device = device; return -SND_ERROR_UNCOMPATIBLE_VERSION;
lb -> fd = fd; }
*handle = lb; lb = (snd_pcm_loopback_t *) calloc(1, sizeof(snd_pcm_loopback_t));
return 0; if (lb == NULL) {
close(fd);
return -ENOMEM;
}
lb->card = card;
lb->device = device;
lb->fd = fd;
*handle = lb;
return 0;
} }
int snd_pcm_loopback_close( void *handle ) int snd_pcm_loopback_close(void *handle)
{ {
snd_pcm_loopback_t *lb; snd_pcm_loopback_t *lb;
int res; int res;
lb = (snd_pcm_loopback_t *)handle; lb = (snd_pcm_loopback_t *) handle;
if ( !lb ) return -EINVAL; if (!lb)
res = close( lb -> fd ) < 0 ? -errno : 0; return -EINVAL;
free( lb ); res = close(lb->fd) < 0 ? -errno : 0;
return res; free(lb);
return res;
} }
int snd_pcm_loopback_file_descriptor( void *handle ) int snd_pcm_loopback_file_descriptor(void *handle)
{ {
snd_pcm_loopback_t *lb; snd_pcm_loopback_t *lb;
lb = (snd_pcm_loopback_t *)handle; lb = (snd_pcm_loopback_t *) handle;
if ( !lb ) return -EINVAL; if (!lb)
return lb -> fd; return -EINVAL;
return lb->fd;
} }
int snd_pcm_loopback_block_mode( void *handle, int enable ) int snd_pcm_loopback_block_mode(void *handle, int enable)
{ {
snd_pcm_loopback_t *lb; snd_pcm_loopback_t *lb;
long flags; long flags;
lb = (snd_pcm_loopback_t *)handle; lb = (snd_pcm_loopback_t *) handle;
if ( !lb ) return -EINVAL; if (!lb)
if ( fcntl( lb -> fd, F_GETFL, &flags ) < 0 ) return -EINVAL;
return -errno; if (fcntl(lb->fd, F_GETFL, &flags) < 0)
if ( enable ) return -errno;
flags |= O_NONBLOCK; if (enable)
else flags |= O_NONBLOCK;
flags &= ~O_NONBLOCK; else
if ( fcntl( lb -> fd, F_SETFL, &flags ) < 0 ) flags &= ~O_NONBLOCK;
return -errno; if (fcntl(lb->fd, F_SETFL, &flags) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_loopback_stream_mode( void *handle, int mode ) int snd_pcm_loopback_stream_mode(void *handle, int mode)
{ {
snd_pcm_loopback_t *lb; snd_pcm_loopback_t *lb;
long lmode = mode; long lmode = mode;
lb = (snd_pcm_loopback_t *)handle; lb = (snd_pcm_loopback_t *) handle;
if ( !lb ) return -EINVAL; if (!lb)
if ( ioctl( lb -> fd, SND_PCM_LB_IOCTL_STREAM_MODE, &lmode ) < 0 ) return -EINVAL;
return -errno; if (ioctl(lb->fd, SND_PCM_LB_IOCTL_STREAM_MODE, &lmode) < 0)
return 0; return -errno;
return 0;
} }
int snd_pcm_loopback_format( void *handle, snd_pcm_format_t *format ) int snd_pcm_loopback_format(void *handle, snd_pcm_format_t * format)
{ {
snd_pcm_loopback_t *lb; snd_pcm_loopback_t *lb;
lb = (snd_pcm_loopback_t *)handle; lb = (snd_pcm_loopback_t *) handle;
if ( !lb ) return -EINVAL; if (!lb)
if ( ioctl( lb -> fd, SND_PCM_LB_IOCTL_FORMAT, format ) < 0 ) return -EINVAL;
return -errno; if (ioctl(lb->fd, SND_PCM_LB_IOCTL_FORMAT, format) < 0)
return 0; return -errno;
return 0;
} }
ssize_t snd_pcm_loopback_read( void *handle, void *buffer, size_t size ) ssize_t snd_pcm_loopback_read(void *handle, void *buffer, size_t size)
{ {
snd_pcm_loopback_t *lb; snd_pcm_loopback_t *lb;
ssize_t result; ssize_t result;
lb = (snd_pcm_loopback_t *)handle; lb = (snd_pcm_loopback_t *) handle;
if ( !lb ) return -EINVAL; if (!lb)
result = read( lb -> fd, buffer, size ); return -EINVAL;
if ( result < 0 ) return -errno; result = read(lb->fd, buffer, size);
return result; if (result < 0)
return -errno;
return result;
} }

View file

@ -30,304 +30,329 @@
#define SND_FILE_RAWMIDI "/dev/snd/midi%i%i" #define SND_FILE_RAWMIDI "/dev/snd/midi%i%i"
#define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 ) #define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 )
typedef struct { typedef struct {
int card; int card;
int device; int device;
int fd; int fd;
} snd_rawmidi_t; } snd_rawmidi_t;
int snd_rawmidi_open( void **handle, int card, int device, int mode )
{
int fd, ver;
char filename[32];
snd_rawmidi_t *rmidi;
*handle = NULL; int snd_rawmidi_open(void **handle, int card, int device, int mode)
if ( card < 0 || card >= SND_CARDS ) return -EINVAL; {
sprintf( filename, SND_FILE_RAWMIDI, card, device ); int fd, ver;
if ( (fd = open( filename, mode )) < 0 ) return -errno; char filename[32];
if ( ioctl( fd, SND_RAWMIDI_IOCTL_PVERSION, &ver ) < 0 ) { snd_rawmidi_t *rmidi;
close( fd );
return -errno; *handle = NULL;
} if (card < 0 || card >= SND_CARDS)
if ( SND_PROTOCOL_UNCOMPATIBLE( ver, SND_PCM_VERSION_MAX ) ) { return -EINVAL;
close( fd ); sprintf(filename, SND_FILE_RAWMIDI, card, device);
return -SND_ERROR_UNCOMPATIBLE_VERSION; if ((fd = open(filename, mode)) < 0)
} return -errno;
rmidi = (snd_rawmidi_t *)calloc( 1, sizeof( snd_rawmidi_t ) ); if (ioctl(fd, SND_RAWMIDI_IOCTL_PVERSION, &ver) < 0) {
if ( rmidi == NULL ) { close(fd);
close( fd ); return -errno;
return -ENOMEM; }
} if (SND_PROTOCOL_UNCOMPATIBLE(ver, SND_PCM_VERSION_MAX)) {
rmidi -> card = card; close(fd);
rmidi -> device = device; return -SND_ERROR_UNCOMPATIBLE_VERSION;
rmidi -> fd = fd; }
*handle = rmidi; rmidi = (snd_rawmidi_t *) calloc(1, sizeof(snd_rawmidi_t));
return 0; if (rmidi == NULL) {
close(fd);
return -ENOMEM;
}
rmidi->card = card;
rmidi->device = device;
rmidi->fd = fd;
*handle = rmidi;
return 0;
} }
int snd_rawmidi_close( void *handle ) int snd_rawmidi_close(void *handle)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
int res; int res;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
res = close( rmidi -> fd ) < 0 ? -errno : 0; return -EINVAL;
free( rmidi ); res = close(rmidi->fd) < 0 ? -errno : 0;
return res; free(rmidi);
return res;
} }
int snd_rawmidi_file_descriptor( void *handle ) int snd_rawmidi_file_descriptor(void *handle)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
return rmidi -> fd; return -EINVAL;
return rmidi->fd;
} }
int snd_rawmidi_block_mode( void *handle, int enable ) int snd_rawmidi_block_mode(void *handle, int enable)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
long flags; long flags;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( fcntl( rmidi -> fd, F_GETFL, &flags ) < 0 ) return -EINVAL;
return -errno; if (fcntl(rmidi->fd, F_GETFL, &flags) < 0)
if ( enable ) return -errno;
flags |= O_NONBLOCK; if (enable)
else flags |= O_NONBLOCK;
flags &= ~O_NONBLOCK; else
if ( fcntl( rmidi -> fd, F_SETFL, &flags ) < 0 ) flags &= ~O_NONBLOCK;
return -errno; if (fcntl(rmidi->fd, F_SETFL, &flags) < 0)
return 0; return -errno;
return 0;
} }
int snd_rawmidi_info( void *handle, snd_rawmidi_info_t *info ) int snd_rawmidi_info(void *handle, snd_rawmidi_info_t * info)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_INFO, info ) < 0 ) return -EINVAL;
return -errno; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_INFO, info) < 0)
return 0; return -errno;
return 0;
} }
int snd_rawmidi_output_switches( void *handle ) int snd_rawmidi_output_switches(void *handle)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
int result; int result;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_OSWITCHES, &result ) < 0 ) return -EINVAL;
return -errno; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_OSWITCHES, &result) < 0)
return result; return -errno;
return result;
} }
int snd_rawmidi_output_switch( void *handle, const char *switch_id ) int snd_rawmidi_output_switch(void *handle, const char *switch_id)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
snd_rawmidi_switch_t uswitch; snd_rawmidi_switch_t uswitch;
int idx, switches, err; int idx, switches, err;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_mixer_t structure */ /* bellow implementation isn't optimized for speed */
if ( (switches = snd_rawmidi_output_switches( handle )) < 0 ) /* info about switches should be cached in the snd_mixer_t structure */
return switches; if ((switches = snd_rawmidi_output_switches(handle)) < 0)
for ( idx = 0; idx < switches; idx++ ) { return switches;
if ( (err = snd_rawmidi_output_switch_read( handle, idx, &uswitch )) < 0 ) for (idx = 0; idx < switches; idx++) {
return err; if ((err = snd_rawmidi_output_switch_read(handle, idx, &uswitch)) < 0)
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) ) return err;
return idx; if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
} return idx;
return -EINVAL; }
return -EINVAL;
} }
int snd_rawmidi_output_switch_read( void *handle, int switchn, snd_rawmidi_switch_t *data ) int snd_rawmidi_output_switch_read(void *handle, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_OSWITCH_READ, data ) < 0 ) data->switchn = switchn;
return -errno; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_OSWITCH_READ, data) < 0)
return 0; return -errno;
} return 0;
int snd_rawmidi_output_switch_write( void *handle, int switchn, snd_rawmidi_switch_t *data )
{
snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle;
if ( !rmidi ) return -EINVAL;
data -> switchn = switchn;
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_OSWITCH_WRITE, data ) < 0 )
return -errno;
return 0;
}
int snd_rawmidi_input_switches( void *handle )
{
snd_rawmidi_t *rmidi;
int result;
rmidi = (snd_rawmidi_t *)handle;
if ( !rmidi ) return -EINVAL;
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_ISWITCHES, &result ) < 0 )
return -errno;
return result;
} }
int snd_rawmidi_input_switch( void *handle, const char *switch_id ) int snd_rawmidi_output_switch_write(void *handle, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
snd_rawmidi_switch_t uswitch;
int idx, switches, err;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
/* bellow implementation isn't optimized for speed */ return -EINVAL;
/* info about switches should be cached in the snd_mixer_t structure */ data->switchn = switchn;
if ( (switches = snd_rawmidi_input_switches( handle )) < 0 ) if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_OSWITCH_WRITE, data) < 0)
return switches; return -errno;
for ( idx = 0; idx < switches; idx++ ) { return 0;
if ( (err = snd_rawmidi_input_switch_read( handle, idx, &uswitch )) < 0 )
return err;
if ( !strncmp( switch_id, uswitch.name, sizeof( uswitch.name ) ) )
return idx;
}
return -EINVAL;
} }
int snd_rawmidi_input_switch_read( void *handle, int switchn, snd_rawmidi_switch_t *data ) int snd_rawmidi_input_switches(void *handle)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
int result;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
data -> switchn = switchn; return -EINVAL;
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_ISWITCH_READ, data ) < 0 ) if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_ISWITCHES, &result) < 0)
return -errno; return -errno;
return 0; return result;
}
int snd_rawmidi_input_switch_write( void *handle, int switchn, snd_rawmidi_switch_t *data )
{
snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle;
if ( !rmidi ) return -EINVAL;
data -> switchn = switchn;
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_ISWITCH_WRITE, data ) < 0 )
return -errno;
return 0;
}
int snd_rawmidi_output_params( void *handle, snd_rawmidi_output_params_t *params )
{
snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle;
if ( !rmidi ) return -EINVAL;
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_OUTPUT_PARAMS, params ) < 0 )
return -errno;
return 0;
} }
int snd_rawmidi_input_params( void *handle, snd_rawmidi_input_params_t *params ) int snd_rawmidi_input_switch(void *handle, const char *switch_id)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
snd_rawmidi_switch_t uswitch;
rmidi = (snd_rawmidi_t *)handle; int idx, switches, err;
if ( !rmidi ) return -EINVAL;
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_INPUT_PARAMS, params ) < 0 ) rmidi = (snd_rawmidi_t *) handle;
return -errno; if (!rmidi)
return 0; return -EINVAL;
/* bellow implementation isn't optimized for speed */
/* info about switches should be cached in the snd_mixer_t structure */
if ((switches = snd_rawmidi_input_switches(handle)) < 0)
return switches;
for (idx = 0; idx < switches; idx++) {
if ((err = snd_rawmidi_input_switch_read(handle, idx, &uswitch)) < 0)
return err;
if (!strncmp(switch_id, uswitch.name, sizeof(uswitch.name)))
return idx;
}
return -EINVAL;
} }
int snd_rawmidi_output_status( void *handle, snd_rawmidi_output_status_t *status ) int snd_rawmidi_input_switch_read(void *handle, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_OUTPUT_STATUS, status ) < 0 ) return -EINVAL;
return -errno; data->switchn = switchn;
return 0; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_ISWITCH_READ, data) < 0)
return -errno;
return 0;
} }
int snd_rawmidi_input_status( void *handle, snd_rawmidi_input_status_t *status ) int snd_rawmidi_input_switch_write(void *handle, int switchn, snd_rawmidi_switch_t * data)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_INPUT_STATUS, status ) < 0 ) return -EINVAL;
return -errno; data->switchn = switchn;
return 0; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_ISWITCH_WRITE, data) < 0)
return -errno;
return 0;
} }
int snd_rawmidi_drain_output( void *handle ) int snd_rawmidi_output_params(void *handle, snd_rawmidi_output_params_t * params)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_DRAIN_OUTPUT ) < 0 ) return -EINVAL;
return -errno; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_OUTPUT_PARAMS, params) < 0)
return 0; return -errno;
return 0;
} }
int snd_rawmidi_flush_output( void *handle ) int snd_rawmidi_input_params(void *handle, snd_rawmidi_input_params_t * params)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_FLUSH_OUTPUT ) < 0 ) return -EINVAL;
return -errno; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_INPUT_PARAMS, params) < 0)
return 0; return -errno;
return 0;
} }
int snd_rawmidi_flush_input( void *handle ) int snd_rawmidi_output_status(void *handle, snd_rawmidi_output_status_t * status)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *)handle; rmidi = (snd_rawmidi_t *) handle;
if ( !rmidi ) return -EINVAL; if (!rmidi)
if ( ioctl( rmidi -> fd, SND_RAWMIDI_IOCTL_FLUSH_INPUT ) < 0 ) return -EINVAL;
return -errno; if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_OUTPUT_STATUS, status) < 0)
return 0; return -errno;
return 0;
} }
ssize_t snd_rawmidi_write( void *handle, const void *buffer, size_t size ) int snd_rawmidi_input_status(void *handle, snd_rawmidi_input_status_t * status)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
ssize_t result;
rmidi = (snd_rawmidi_t *) handle;
rmidi = (snd_rawmidi_t *)handle; if (!rmidi)
if ( !rmidi ) return -EINVAL; return -EINVAL;
result = write( rmidi -> fd, buffer, size ); if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_INPUT_STATUS, status) < 0)
if ( result < 0 ) return -errno; return -errno;
return result; return 0;
} }
ssize_t snd_rawmidi_read( void *handle, void *buffer, size_t size ) int snd_rawmidi_drain_output(void *handle)
{ {
snd_rawmidi_t *rmidi; snd_rawmidi_t *rmidi;
ssize_t result;
rmidi = (snd_rawmidi_t *) handle;
rmidi = (snd_rawmidi_t *)handle; if (!rmidi)
if ( !rmidi ) return -EINVAL; return -EINVAL;
result = read( rmidi -> fd, buffer, size ); if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_DRAIN_OUTPUT) < 0)
if ( result < 0 ) return -errno; return -errno;
return result; return 0;
}
int snd_rawmidi_flush_output(void *handle)
{
snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *) handle;
if (!rmidi)
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_FLUSH_OUTPUT) < 0)
return -errno;
return 0;
}
int snd_rawmidi_flush_input(void *handle)
{
snd_rawmidi_t *rmidi;
rmidi = (snd_rawmidi_t *) handle;
if (!rmidi)
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_FLUSH_INPUT) < 0)
return -errno;
return 0;
}
ssize_t snd_rawmidi_write(void *handle, const void *buffer, size_t size)
{
snd_rawmidi_t *rmidi;
ssize_t result;
rmidi = (snd_rawmidi_t *) handle;
if (!rmidi)
return -EINVAL;
result = write(rmidi->fd, buffer, size);
if (result < 0)
return -errno;
return result;
}
ssize_t snd_rawmidi_read(void *handle, void *buffer, size_t size)
{
snd_rawmidi_t *rmidi;
ssize_t result;
rmidi = (snd_rawmidi_t *) handle;
if (!rmidi)
return -EINVAL;
result = read(rmidi->fd, buffer, size);
if (result < 0)
return -errno;
return result;
} }