/* $Header: /u/cvsroot/mixerctl/mixerctl.c,v 1.2 2001/03/15 08:34:22 mayoff Exp $ */

#include <fcntl.h>
#include <unistd.h>
#include <sys/soundcard.h>

#define USAGE "Usage: mixerctl {r|w} /dev/sound/mixer"

int volumes[SOUND_MIXER_NRDEVICES];

void
usage(void)
{
    write(2, USAGE, sizeof(USAGE) - 1);
    exit(1);
}

int
main(int argc, char **argv)
{
    char mode;
    int fd;
    int channel;

    if (argc != 3) usage();

    mode = argv[1][0];
    if ((mode != 'r' && mode != 'w') || argv[1][1] != 0) usage();

    fd = open(argv[2], mode == 'r' ? O_RDONLY : O_WRONLY);
    if (fd < 0) exit(2);

    if (mode == 'r') {
	for (channel = 0; channel < SOUND_MIXER_NRDEVICES; channel++) {
	    volumes[channel] = 0;
	    ioctl(fd, MIXER_READ(channel), volumes + channel);
	}

	if (write(1, volumes, sizeof volumes) != sizeof volumes) {
	    exit(3);
	}
    }

    else {
	if (read(0, volumes, sizeof volumes) != sizeof volumes) {
	    exit(4);
	}

	for (channel = 0; channel < SOUND_MIXER_NRDEVICES; channel++) {
	    ioctl(fd, MIXER_WRITE(channel), volumes + channel);
	}
    }

    exit(0);
}

