Mixer patches

Craig Dooley craig at xlnx-x.net
Mon Jan 5 10:01:47 PST 2004


This pretty much syncs us up with the FreeBSD5 mixer, but cleans up some
messy code, gets rid of bar, baz, and foo as variable names, adds an -h
options, and documents it.  Most important feature is that volumes can
be specified as relative values now, so mixer vol +5 works with my
hotkeys now.

-Craig
-- 
------------------------------------------------------------------------
Craig Dooley                                            craig at xxxxxxxxxx
------------------------------------------------------------------------
Index: usr.sbin/mixer/Makefile
===================================================================
RCS file: /src/dcvs/src/usr.sbin/mixer/Makefile,v
retrieving revision 1.2
diff -u -r1.2 Makefile
--- usr.sbin/mixer/Makefile	17 Jun 2003 04:29:57 -0000	1.2
+++ usr.sbin/mixer/Makefile	29 Dec 2003 21:47:10 -0000
@@ -4,5 +4,5 @@
 PROG=	mixer
 WARNS?=	2
 MAN=	mixer.8
-
+CFLAGS+= -g
 .include <bsd.prog.mk>
Index: usr.sbin/mixer/mixer.8
===================================================================
RCS file: /src/dcvs/src/usr.sbin/mixer/mixer.8,v
retrieving revision 1.2
diff -u -r1.2 mixer.8
--- usr.sbin/mixer/mixer.8	17 Jun 2003 04:29:57 -0000	1.2
+++ usr.sbin/mixer/mixer.8	5 Jan 2004 17:51:54 -0000
@@ -38,19 +38,33 @@
 .Nm
 .Op Fl f Ar device
 .Op Fl s
+.Op Fl h
 .Oo
-.Oo Ar dev
-.Op Ar lvol Ns Op : Ns Ar rvol
-.Cm | recsrc |
+.Ar dev
 .Sm off
-.Eo \&{
-.Cm ^ | + | - | =
-.Sm on
-.Ec \&} Ns Cm rec
-.Ar rdev
+.Oo
+.Op Cm + | -
+.Ar lvol
+.Op : Oo Cm + | - Oc Ar rvol
 .Oc
-.Ar ...
 .Oc
+.Sm on
+.Ar ...
+.Nm
+.Op Fl f Ar device
+.Op Fl s
+.Cm recsrc
+.Ar ...
+.Nm
+.Op Fl f Ar device
+.Op Fl s
+.Sm off
+.Bro
+.Cm ^ | + | - | =
+.Brc
+.Cm rec
+.Sm on
+.Ar rdev ...
 .Sh DESCRIPTION
 The
 .Nm
@@ -66,15 +80,16 @@
 .Pp
 Not all mixer devices are available.
 .Pp
-When
+Without any arguments,
 .Nm
-is run without any arguments, all supported devices are displayed
-along with current values.
+displays the current settings for all supported devices, followed by information
+about the current recording input devices.
 If the
 .Ar dev
-argument is specified, the value for the
-.Ar dev
-device will be displayed.
+argument is specified, 
+.Nm
+displays only the value for that
+.Ar dev .
 .Pp
 To modify the mixer value
 .Ar dev ,
@@ -88,6 +103,13 @@
 .Ar dev
 and including only the channel settings will change the main volume level.
 .Pp
+If the left or right channel settings are prefixed with
+.Cm +
+or
+.Cm - ,
+the value following will be used as a relative adjustment, modifying the
+current settings by the amount specified.
+.Pp
 If the
 .Fl s
 flag is used, the current mixer values will be displayed in a format suitable
@@ -127,6 +149,11 @@
 will open
 .Ar device
 as the mixer device.
+.Pp
+The option
+.Fl h
+will print a help and exit
+.Pp
 .Sh FILES
 .Bl -tag -width /dev/mixer -compact
 .It Pa /dev/mixer
Index: usr.sbin/mixer/mixer.c
===================================================================
RCS file: /src/dcvs/src/usr.sbin/mixer/mixer.c,v
retrieving revision 1.2
diff -u -r1.2 mixer.c
--- usr.sbin/mixer/mixer.c	17 Jun 2003 04:29:57 -0000	1.2
+++ usr.sbin/mixer/mixer.c	5 Jan 2004 17:44:02 -0000
@@ -22,7 +22,11 @@
 #include <unistd.h>
 #include <sys/soundcard.h>
 
+#define LEFT(vol) (vol & 0x7f)
+#define RIGHT(vol) ((vol >> 8) & 0x7f)
+
 const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
+const char *defaultdev = "/dev/mixer";
 
 void usage(int devmask, int recmask);
 int res_name(const char *name, int mask);
@@ -33,7 +37,10 @@
 {
 	int i, n;
 
-	printf("usage: mixer [-f device] [-s] [[dev [voll[:volr]] | recsrc | {^|+|-|=}rec recdev] ... ]\n");
+	printf("usage: mixer [-f device] [-s] [dev [+|-][voll[:[+|-]volr]] ...\n"
+	       "       mixer [-f device] [-s] recsrc ...\n"
+	       "       mixer [-f device] [-s] {^|+|-|=}rec recdev ...\n"
+	       "       mixer -h\n");
 	printf(" devices: ");
 	for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++)
 		if ((1 << i) & devmask)  {
@@ -57,13 +64,13 @@
 int
 res_name(const char *name, int mask)
 {
-	int foo;
+	int i;
 
-	for (foo = 0; foo < SOUND_MIXER_NRDEVICES; foo++)
-		if ((1 << foo) & mask && !strcmp(names[foo], name))
+	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
+		if ((1 << i) & mask && !strcmp(names[i], name))
 			break;
 
-	return foo == SOUND_MIXER_NRDEVICES ? -1 : foo;
+	return i == SOUND_MIXER_NRDEVICES ? -1 : i;
 }
 
 void
@@ -85,65 +92,72 @@
 int
 main(int argc, char *argv[])
 {
-	int foo, bar, baz, dev;
+	int i, mset, fd, dev;
 	int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
 	int dusage = 0, drecsrc = 0, shortflag = 0;
 	int l = 0, r = 0, t = 0;
+	int n = 0, lrel = 0, rrel = 0;
+	char lstr[8], rstr[8];
 	char ch;
 
-	char *name;
-
-	name = strdup("/dev/mixer");
+	const char *name = defaultdev;
 
-	if (!strcmp(argv[0], "mixer2"))
-		name = strdup("/dev/mixer1");
-	else if (!strcmp(argv[0], "mixer3"))
-		name = strdup("/dev/mixer2");
-
-	while ((ch = getopt(argc, argv, "f:s")) != -1)
+	while ((ch = getopt(argc, argv, "f:sh")) != -1)
 		switch (ch) {
 			case 'f':
-				name = strdup(optarg);
+				name = optarg;
 				break;
 			case 's':
 				shortflag = 1;
 				break;
+			case 'h': /* Fall through */
 			default:
 				dusage = 1;
 		}
-	argc -= (optind - 1);
-	argv += (optind - 1);
+	argc -= optind;
+	argv += optind;
 
-	if ((baz = open(name, O_RDWR)) < 0)
+	if ((fd = open(name, O_RDWR)) < 0)
 		err(1, "%s", name);
-	free(name);
-	if (ioctl(baz, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
+	if (ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
 		err(1, "SOUND_MIXER_READ_DEVMASK");
-	if (ioctl(baz, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
+	if (ioctl(fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
 		err(1, "SOUND_MIXER_READ_RECMASK");
-	if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
+	if (ioctl(fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
 		err(1, "SOUND_MIXER_READ_RECSRC");
 	orecsrc = recsrc;
 
-	if ((argc == 1) && (dusage == 0)) {
-		for (foo = 0; foo < SOUND_MIXER_NRDEVICES; foo++) {
-			if (!((1 << foo) & devmask)) 
+	if (dusage) {
+		close(fd);
+		usage(devmask, recmask); /* Does not return */
+	}
+
+	if (argc == 0) {
+		for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+			if (!((1 << i) & devmask)) 
 				continue;
-			if (ioctl(baz, MIXER_READ(foo),&bar)== -1) {
+			if (ioctl(fd, MIXER_READ(i),&mset)== -1) {
 			   	warn("MIXER_READ");
 				continue;
 			}
 			if (shortflag)
-				printf("%s %d:%d ", names[foo], bar & 0x7f, (bar >> 8) & 0x7f);
+				printf("%s %d:%d ", names[i], LEFT(mset), 
+						RIGHT(mset));
 			else
-				printf("Mixer %-8s is currently set to %3d:%d\n", names[foo], bar & 0x7f, (bar >> 8) & 0x7f);
+				printf("Mixer %-8s is currently set to %3d:%d\n", 
+						names[i], LEFT(mset), RIGHT(mset));
 		}
-		return(0);
+		if(shortflag && isatty(STDOUT_FILENO))
+			printf("\n");
+		if (ioctl(fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
+			err(1, "SOUND_MIXER_READ_RECSRC");
+		print_recsrc(recsrc);
+		exit(0);
 	}
 
-	argc--; argv++;
 
-	while ((argc > 0) && (dusage == 0)) {
+
+	while (argc > 0) {
 		if (!strcmp("recsrc", *argv)) {
 			drecsrc = 1;
 			argc--; argv++;
@@ -188,24 +202,52 @@
 			break;
 		}
 
-		switch(argc > 1 ? sscanf(argv[1], "%d:%d", &l, &r) : t) {
+#define	issign(c)	(((c) == '+') || ((c) == '-'))
+
+		if (argc > 1) {
+			n = sscanf(argv[1], "%7[^:]:%7s", lstr, rstr);
+			if (n > 0) {
+				if (issign(lstr[0]))
+					lrel = rrel = 1;
+				l = atoi(lstr);
+			}
+			if (n > 1) {
+				rrel = 0;
+				if (issign(rstr[0]))
+					rrel = 1;
+				r = atoi(rstr);
+			}
+		}
+
+		switch(argc > 1 ? n : t) {
 		case 0:
-			if (ioctl(baz, MIXER_READ(dev),&bar)== -1) {
+			if (ioctl(fd, MIXER_READ(dev),&mset)== -1) {
 				warn("MIXER_READ");
 				argc--; argv++;
 				continue;
 			}
 			if (shortflag)
-				printf("%s %d:%d ", names[dev], bar & 0x7f, (bar >> 8) & 0x7f);
+				printf("%s %d:%d ", names[dev], LEFT(mset), RIGHT(mset));
 			else
 				printf("Mixer %-8s is currently set to %3d:%d\n",
-				  names[dev], bar & 0x7f, (bar >> 8) & 0x7f);
+				  names[dev], LEFT(mset), RIGHT(mset));
 
 			argc--; argv++;
 			break;
 		case 1:
 			r = l;
 		case 2:
+			if (ioctl(fd, MIXER_READ(dev),&mset)== -1) {
+				warn("MIXER_READ");
+				argc--; argv++;
+				continue;
+			}
+
+			if (lrel)
+				l += LEFT(mset);
+			if (rrel)
+				r += RIGHT(mset);
+
 			if (l < 0)
 				l = 0;
 			else if (l > 100)
@@ -219,7 +261,7 @@
 			    l, r);
 
 			l |= r << 8;
-			if (ioctl(baz, MIXER_WRITE(dev), &l) == -1)
+			if (ioctl(fd, MIXER_WRITE(dev), &l) == -1)
 				warn("WRITE_MIXER");
 
 			argc -= 2; argv += 2;
@@ -227,23 +269,17 @@
 		}
 	}
 
-	if (dusage) {
-		close(baz);
-		usage(devmask, recmask);
-		/* Not reached */
-	}
-
 	if (orecsrc != recsrc)
-		if (ioctl(baz, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1)
+		if (ioctl(fd, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1)
 			err(1, "SOUND_MIXER_WRITE_RECSRC");
  
 	if (drecsrc) {
-		if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
+		if (ioctl(fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
 			err(1, "SOUND_MIXER_READ_RECSRC");
 		print_recsrc(recsrc);
 	}
 
-	close(baz);
+	close(fd);
 
 	exit(0);
 }




More information about the Submit mailing list