1995-02-15 17:33:04 +00:00
|
|
|
/*
|
|
|
|
* This is an example of a mixer program for Linux
|
|
|
|
*
|
|
|
|
* updated 1/1/93 to add stereo, level query, broken
|
1995-05-30 03:57:47 +00:00
|
|
|
* devmask kludge - cmetz@thor.tjhsst.edu
|
1995-02-15 17:33:04 +00:00
|
|
|
*
|
|
|
|
* (C) Craig Metz and Hannu Savolainen 1993.
|
|
|
|
*
|
|
|
|
* You may do anything you wish with this program.
|
1997-09-19 13:38:51 +00:00
|
|
|
*
|
|
|
|
* ditto for my modifications (John-Mark Gurney, 1997)
|
1995-02-15 17:33:04 +00:00
|
|
|
*/
|
|
|
|
|
2003-05-03 21:06:42 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-09-29 06:38:49 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
1995-02-15 17:33:04 +00:00
|
|
|
#include <fcntl.h>
|
2008-03-15 01:09:47 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <limits.h>
|
1997-09-29 06:38:49 +00:00
|
|
|
#include <stdio.h>
|
1995-02-15 17:33:04 +00:00
|
|
|
#include <string.h>
|
1997-09-19 13:38:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
1995-02-15 17:33:04 +00:00
|
|
|
#include <sys/soundcard.h>
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
|
1995-02-15 17:33:04 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
static void usage(int devmask, int recmask);
|
|
|
|
static int res_name(const char *name, int mask);
|
|
|
|
static void print_recsrc(int recsrc, int recmask, int sflag);
|
1995-02-15 17:33:04 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
static void
|
1997-09-19 13:38:51 +00:00
|
|
|
usage(int devmask, int recmask)
|
1995-02-15 17:33:04 +00:00
|
|
|
{
|
2008-03-15 01:20:05 +00:00
|
|
|
int i, n;
|
1995-02-15 17:33:04 +00:00
|
|
|
|
2005-04-13 07:11:54 +00:00
|
|
|
printf("usage: mixer [-f device] [-s | -S] [dev [+|-][voll[:[+|-]volr]] ...\n"
|
2008-03-15 01:20:05 +00:00
|
|
|
" mixer [-f device] [-s | -S] recsrc ...\n"
|
|
|
|
" mixer [-f device] [-s | -S] {^|+|-|=}rec rdev ...\n");
|
2008-03-15 01:09:47 +00:00
|
|
|
if (devmask != 0) {
|
|
|
|
printf(" devices: ");
|
|
|
|
for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
|
|
|
if ((1 << i) & devmask) {
|
|
|
|
if (n)
|
|
|
|
printf(", ");
|
|
|
|
printf("%s", names[i]);
|
2008-03-15 01:20:05 +00:00
|
|
|
n++;
|
2008-03-15 01:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (recmask != 0) {
|
|
|
|
printf("\n rec devices: ");
|
|
|
|
for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
|
|
|
if ((1 << i) & recmask) {
|
|
|
|
if (n)
|
|
|
|
printf(", ");
|
|
|
|
printf("%s", names[i]);
|
2008-03-15 01:20:05 +00:00
|
|
|
n++;
|
2008-03-15 01:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
1997-09-19 13:38:51 +00:00
|
|
|
printf("\n");
|
1995-02-15 17:33:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
static int
|
1997-09-19 13:38:51 +00:00
|
|
|
res_name(const char *name, int mask)
|
|
|
|
{
|
2008-03-15 01:20:05 +00:00
|
|
|
int foo;
|
1997-09-19 13:38:51 +00:00
|
|
|
|
|
|
|
for (foo = 0; foo < SOUND_MIXER_NRDEVICES; foo++)
|
2008-03-15 01:20:05 +00:00
|
|
|
if ((1 << foo) & mask && strcmp(names[foo], name) == 0)
|
1997-09-19 13:38:51 +00:00
|
|
|
break;
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
return (foo == SOUND_MIXER_NRDEVICES ? -1 : foo);
|
1997-09-19 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
static void
|
|
|
|
print_recsrc(int recsrc, int recmask, int sflag)
|
1995-02-15 17:33:04 +00:00
|
|
|
{
|
2008-03-15 01:20:05 +00:00
|
|
|
int i, n;
|
2006-01-01 20:19:49 +00:00
|
|
|
|
2008-03-15 01:09:47 +00:00
|
|
|
if (recmask == 0)
|
|
|
|
return;
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
if (!sflag)
|
2006-01-01 20:19:49 +00:00
|
|
|
printf("Recording source: ");
|
1995-02-15 17:33:04 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
1995-02-15 17:33:04 +00:00
|
|
|
if ((1 << i) & recsrc) {
|
2008-03-15 01:20:05 +00:00
|
|
|
if (sflag)
|
|
|
|
printf("%srec ", n ? " +" : "=");
|
|
|
|
else if (n)
|
2006-01-01 20:19:49 +00:00
|
|
|
printf(", ");
|
|
|
|
printf("%s", names[i]);
|
2008-03-15 01:20:05 +00:00
|
|
|
n++;
|
1995-02-15 17:33:04 +00:00
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
if (!sflag)
|
2006-01-01 20:19:49 +00:00
|
|
|
printf("\n");
|
1995-02-15 17:33:04 +00:00
|
|
|
}
|
|
|
|
|
1995-05-30 03:57:47 +00:00
|
|
|
int
|
1995-02-15 17:33:04 +00:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2008-03-15 01:20:05 +00:00
|
|
|
char mixer[PATH_MAX] = "/dev/mixer";
|
|
|
|
char lstr[5], rstr[5];
|
|
|
|
char *name, *eptr;
|
|
|
|
int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
|
|
|
|
int dusage = 0, drecsrc = 0, sflag = 0, Sflag = 0;
|
|
|
|
int l, r, lrel, rrel;
|
|
|
|
int ch, foo, bar, baz, dev, m, n, t;
|
2008-03-15 01:09:47 +00:00
|
|
|
|
|
|
|
if ((name = strdup(basename(argv[0]))) == NULL)
|
|
|
|
err(1, "strdup()");
|
|
|
|
if (strncmp(name, "mixer", 5) == 0 && name[5] != '\0') {
|
|
|
|
n = strtol(name + 5, &eptr, 10) - 1;
|
|
|
|
if (n > 0 && *eptr == '\0')
|
|
|
|
snprintf(mixer, PATH_MAX - 1, "/dev/mixer%d", n);
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
name = mixer;
|
1997-09-19 13:38:51 +00:00
|
|
|
|
2008-03-15 01:09:47 +00:00
|
|
|
n = 1;
|
|
|
|
for (;;) {
|
|
|
|
if (n >= argc || *argv[n] != '-')
|
|
|
|
break;
|
|
|
|
if (strlen(argv[n]) != 2) {
|
|
|
|
if (strcmp(argv[n] + 1, "rec") != 0)
|
2000-08-15 05:42:39 +00:00
|
|
|
dusage = 1;
|
2008-03-15 01:09:47 +00:00
|
|
|
break;
|
2000-08-15 05:42:39 +00:00
|
|
|
}
|
2008-03-15 01:09:47 +00:00
|
|
|
ch = *(argv[n] + 1);
|
|
|
|
if (ch == 'f' && n < argc - 1) {
|
|
|
|
name = argv[n + 1];
|
|
|
|
n += 2;
|
|
|
|
} else if (ch == 's') {
|
2008-03-15 01:20:05 +00:00
|
|
|
sflag = 1;
|
2008-03-15 01:09:47 +00:00
|
|
|
n++;
|
|
|
|
} else if (ch == 'S') {
|
2008-03-15 01:20:05 +00:00
|
|
|
Sflag = 1;
|
2008-03-15 01:09:47 +00:00
|
|
|
n++;
|
|
|
|
} else {
|
|
|
|
dusage = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
if (sflag && Sflag)
|
2008-03-15 01:09:47 +00:00
|
|
|
dusage = 1;
|
|
|
|
|
|
|
|
argc -= n - 1;
|
|
|
|
argv += n - 1;
|
1995-02-15 17:33:04 +00:00
|
|
|
|
1997-09-29 06:38:49 +00:00
|
|
|
if ((baz = open(name, O_RDWR)) < 0)
|
|
|
|
err(1, "%s", name);
|
|
|
|
if (ioctl(baz, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
|
1997-10-01 06:34:09 +00:00
|
|
|
err(1, "SOUND_MIXER_READ_DEVMASK");
|
1997-09-29 06:38:49 +00:00
|
|
|
if (ioctl(baz, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
|
1997-10-01 06:34:09 +00:00
|
|
|
err(1, "SOUND_MIXER_READ_RECMASK");
|
1997-09-29 06:38:49 +00:00
|
|
|
if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
|
1997-10-01 06:34:09 +00:00
|
|
|
err(1, "SOUND_MIXER_READ_RECSRC");
|
1997-09-19 13:38:51 +00:00
|
|
|
orecsrc = recsrc;
|
1995-02-15 17:33:04 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
if (argc == 1 && dusage == 0) {
|
|
|
|
for (foo = 0, n = 0; foo < SOUND_MIXER_NRDEVICES; foo++) {
|
|
|
|
if (!((1 << foo) & devmask))
|
1996-01-31 18:01:22 +00:00
|
|
|
continue;
|
2008-03-15 01:20:05 +00:00
|
|
|
if (ioctl(baz, MIXER_READ(foo),&bar) == -1) {
|
1997-09-29 06:38:49 +00:00
|
|
|
warn("MIXER_READ");
|
1996-01-31 18:01:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
if (Sflag || sflag) {
|
|
|
|
printf("%s%s%c%d:%d", n ? " " : "",
|
|
|
|
names[foo], Sflag ? ':' : ' ',
|
|
|
|
bar & 0x7f, (bar >> 8) & 0x7f);
|
|
|
|
n++;
|
|
|
|
} else
|
|
|
|
printf("Mixer %-8s is currently set to "
|
|
|
|
"%3d:%d\n", names[foo], bar & 0x7f,
|
|
|
|
(bar >> 8) & 0x7f);
|
1996-01-31 18:01:22 +00:00
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
if (n && recmask)
|
|
|
|
printf(" ");
|
|
|
|
print_recsrc(recsrc, recmask, Sflag | sflag);
|
|
|
|
return (0);
|
1996-01-31 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1997-09-19 13:38:51 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
n = 0;
|
|
|
|
while (argc > 0 && dusage == 0) {
|
|
|
|
if (strcmp("recsrc", *argv) == 0) {
|
1997-09-19 13:38:51 +00:00
|
|
|
drecsrc = 1;
|
2008-03-15 01:20:05 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1997-09-19 13:38:51 +00:00
|
|
|
continue;
|
2008-03-15 01:20:05 +00:00
|
|
|
} else if (argc > 1 && strcmp("rec", *argv + 1) == 0) {
|
1997-09-19 13:38:51 +00:00
|
|
|
if (**argv != '+' && **argv != '-' &&
|
|
|
|
**argv != '=' && **argv != '^') {
|
2000-08-15 05:42:39 +00:00
|
|
|
warnx("unknown modifier: %c", **argv);
|
1997-09-19 13:38:51 +00:00
|
|
|
dusage = 1;
|
2000-08-15 05:42:39 +00:00
|
|
|
break;
|
1996-01-15 09:11:17 +00:00
|
|
|
}
|
1997-09-19 13:38:51 +00:00
|
|
|
if ((dev = res_name(argv[1], recmask)) == -1) {
|
2000-08-15 05:42:39 +00:00
|
|
|
warnx("unknown recording device: %s", argv[1]);
|
1997-09-19 13:38:51 +00:00
|
|
|
dusage = 1;
|
2000-08-15 05:42:39 +00:00
|
|
|
break;
|
1995-02-15 17:33:04 +00:00
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
switch (**argv) {
|
1997-09-19 13:38:51 +00:00
|
|
|
case '+':
|
1995-02-15 17:33:04 +00:00
|
|
|
recsrc |= (1 << dev);
|
1997-09-19 13:38:51 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
1995-02-15 17:33:04 +00:00
|
|
|
recsrc &= ~(1 << dev);
|
1997-09-19 13:38:51 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
recsrc = (1 << dev);
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
recsrc ^= (1 << dev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
drecsrc = 1;
|
2008-03-15 01:20:05 +00:00
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
1997-09-19 13:38:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
1995-02-15 17:33:04 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
if ((t = sscanf(*argv, "%d:%d", &l, &r)) > 0)
|
2001-04-09 11:10:34 +00:00
|
|
|
dev = 0;
|
2008-03-15 01:20:05 +00:00
|
|
|
else if ((dev = res_name(*argv, devmask)) == -1) {
|
2000-08-15 05:42:39 +00:00
|
|
|
warnx("unknown device: %s", *argv);
|
1997-09-19 13:38:51 +00:00
|
|
|
dusage = 1;
|
2000-08-15 05:42:39 +00:00
|
|
|
break;
|
1997-09-19 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
lrel = rrel = 0;
|
2003-04-09 15:25:52 +00:00
|
|
|
if (argc > 1) {
|
2008-03-15 01:20:05 +00:00
|
|
|
m = sscanf(argv[1], "%7[^:]:%7s", lstr, rstr);
|
|
|
|
if (m > 0) {
|
|
|
|
if (*lstr == '+' || *lstr == '-')
|
2003-04-09 15:25:52 +00:00
|
|
|
lrel = rrel = 1;
|
2008-03-15 01:20:05 +00:00
|
|
|
l = strtol(lstr, NULL, 10);
|
2003-04-09 15:25:52 +00:00
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
if (m > 1) {
|
|
|
|
if (*rstr == '+' || *rstr == '-')
|
2003-04-09 15:25:52 +00:00
|
|
|
rrel = 1;
|
2008-03-15 01:20:05 +00:00
|
|
|
r = strtol(rstr, NULL, 10);
|
2003-04-09 15:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
switch (argc > 1 ? m : t) {
|
1997-09-19 13:38:51 +00:00
|
|
|
case 0:
|
2008-03-15 01:20:05 +00:00
|
|
|
if (ioctl(baz, MIXER_READ(dev), &bar) == -1) {
|
1997-09-29 06:38:49 +00:00
|
|
|
warn("MIXER_READ");
|
2008-03-15 01:20:05 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1997-09-19 13:38:51 +00:00
|
|
|
continue;
|
1995-02-15 17:33:04 +00:00
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
if (Sflag || sflag) {
|
|
|
|
printf("%s%s%c%d:%d", n ? " " : "",
|
|
|
|
names[dev], Sflag ? ':' : ' ',
|
|
|
|
bar & 0x7f, (bar >> 8) & 0x7f);
|
|
|
|
n++;
|
|
|
|
} else
|
|
|
|
printf("Mixer %-8s is currently set to "
|
|
|
|
"%3d:%d\n", names[dev], bar & 0x7f,
|
|
|
|
(bar >> 8) & 0x7f);
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
1997-09-19 13:38:51 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
r = l;
|
2008-03-15 01:20:05 +00:00
|
|
|
/* FALLTHROUGH */
|
1997-09-19 13:38:51 +00:00
|
|
|
case 2:
|
2008-03-15 01:20:05 +00:00
|
|
|
if (ioctl(baz, MIXER_READ(dev), &bar) == -1) {
|
2003-04-09 15:25:52 +00:00
|
|
|
warn("MIXER_READ");
|
2008-03-15 01:20:05 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
2003-04-09 15:25:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lrel)
|
|
|
|
l = (bar & 0x7f) + l;
|
|
|
|
if (rrel)
|
|
|
|
r = ((bar >> 8) & 0x7f) + r;
|
|
|
|
|
1997-09-19 13:38:51 +00:00
|
|
|
if (l < 0)
|
|
|
|
l = 0;
|
|
|
|
else if (l > 100)
|
|
|
|
l = 100;
|
|
|
|
if (r < 0)
|
|
|
|
r = 0;
|
|
|
|
else if (r > 100)
|
|
|
|
r = 100;
|
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
if (!Sflag)
|
|
|
|
printf("Setting the mixer %s from %d:%d to "
|
|
|
|
"%d:%d.\n", names[dev], bar & 0x7f,
|
|
|
|
(bar >> 8) & 0x7f, l, r);
|
1997-09-19 13:38:51 +00:00
|
|
|
|
|
|
|
l |= r << 8;
|
|
|
|
if (ioctl(baz, MIXER_WRITE(dev), &l) == -1)
|
1997-09-29 06:38:49 +00:00
|
|
|
warn("WRITE_MIXER");
|
1997-09-19 13:38:51 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
1997-09-19 13:38:51 +00:00
|
|
|
break;
|
1995-02-15 17:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-15 05:42:39 +00:00
|
|
|
if (dusage) {
|
|
|
|
close(baz);
|
|
|
|
usage(devmask, recmask);
|
2008-03-15 01:20:05 +00:00
|
|
|
/* NOTREACHED */
|
2000-08-15 05:42:39 +00:00
|
|
|
}
|
|
|
|
|
2008-03-15 01:09:47 +00:00
|
|
|
if (orecsrc != recsrc) {
|
1997-09-29 06:38:49 +00:00
|
|
|
if (ioctl(baz, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1)
|
1997-10-01 06:34:09 +00:00
|
|
|
err(1, "SOUND_MIXER_WRITE_RECSRC");
|
1997-09-29 06:38:49 +00:00
|
|
|
if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
|
1997-10-01 06:34:09 +00:00
|
|
|
err(1, "SOUND_MIXER_READ_RECSRC");
|
1997-09-19 13:38:51 +00:00
|
|
|
}
|
2008-03-15 01:20:05 +00:00
|
|
|
|
2008-03-15 01:09:47 +00:00
|
|
|
if (drecsrc)
|
2008-03-15 01:20:05 +00:00
|
|
|
print_recsrc(recsrc, recmask, Sflag || sflag);
|
1997-09-19 13:38:51 +00:00
|
|
|
|
1995-02-15 17:33:04 +00:00
|
|
|
close(baz);
|
1997-09-19 13:38:51 +00:00
|
|
|
|
2008-03-15 01:20:05 +00:00
|
|
|
return (0);
|
1995-02-15 17:33:04 +00:00
|
|
|
}
|