mixer(8): Sync up with FreeBSD.
authorSascha Wildner <saw@online.de>
Sun, 11 Jan 2015 09:23:03 +0000 (10:23 +0100)
committerSascha Wildner <saw@online.de>
Sun, 11 Jan 2015 09:23:31 +0000 (10:23 +0100)
usr.sbin/mixer/Makefile
usr.sbin/mixer/mixer.8
usr.sbin/mixer/mixer.c

index 65faff8..22b6914 100644 (file)
@@ -1,5 +1,4 @@
-# $FreeBSD: src/usr.sbin/mixer/Makefile,v 1.6.2.2 2001/07/30 10:22:58 dd Exp $
-# $DragonFly: src/usr.sbin/mixer/Makefile,v 1.3 2005/01/17 01:49:24 cpressey Exp $
+# $FreeBSD: head/usr.sbin/mixer/Makefile 201390 2010-01-02 11:07:44Z ed $
 
 PROG=  mixer
 MAN=   mixer.8
index 7c4b760..14e8ae1 100644 (file)
@@ -1,5 +1,5 @@
 .\" Copyright (c) 1997
-.\"    Mike Pritchard <mpp@FreeBSD.ORG>.  All rights reserved.
+.\"    Mike Pritchard <mpp@FreeBSD.org>.  All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
 .\" modification, are permitted provided that the following conditions
@@ -25,9 +25,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: src/usr.sbin/mixer/mixer.8,v 1.10.2.7 2003/03/11 21:13:51 trhodes Exp $
+.\" $FreeBSD: head/usr.sbin/mixer/mixer.8 267668 2014-06-20 09:57:27Z bapt $
 .\"
-.Dd January 9, 1997
+.Dd January 11, 2015
 .Dt MIXER 8
 .Os
 .Sh NAME
@@ -36,8 +36,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl f Ar device
-.Op Fl s
-.Op Fl h
+.Op Fl s | S
 .Oo
 .Ar dev
 .Sm off
 .Ar ...
 .Nm
 .Op Fl f Ar device
-.Op Fl s
+.Op Fl s | S
 .Cm recsrc
 .Ar ...
 .Nm
 .Op Fl f Ar device
-.Op Fl s
+.Op Fl s | S
 .Sm off
 .Bro
 .Cm ^ | + | - | =
@@ -69,11 +68,13 @@ The
 .Nm
 utility is used to set and display soundcard mixer device levels.
 It may
-also be used to start and stop recording from the soundcard.  The list
+also be used to start and stop recording from the soundcard.
+The list
 of mixer devices that may be modified are:
 .Bd -ragged -offset indent
-vol, bass, treble, synth, pcm, speaker, mic, cd, mix,
-pcm2, rec, igain, ogain, line1, line2, and line3.
+vol, bass, treble, synth, pcm, speaker, line, mic, cd, mix,
+pcm2, rec, igain, ogain, line1, line2, line3, dig1, dig2, dig3,
+phin, phout, video, radio, and monitor.
 .Ed
 .Pp
 Not all mixer devices are available.
@@ -93,11 +94,13 @@ To modify the mixer value
 .Ar dev ,
 the optional left and right channel settings of
 .Ar lvol Ns Op : Ns Ar rvol
-may be specified.  The
+may be specified.
+The
 .Ar lvol
 and
 .Ar rvol
-arguments may be from 0 - 100.  Omitting
+arguments may be from 0 - 100.
+Omitting
 .Ar dev
 and including only the channel settings will change the main volume level.
 .Pp
@@ -115,6 +118,10 @@ for use as the command-line arguments to a future invocation of
 .Nm
 (as above).
 .Pp
+The
+.Fl S
+flag provides the above output without mixing field separators.
+.Pp
 To change the recording device you use one of:
 .Bl -tag -width =rec -offset indent
 .It Cm ^rec
@@ -134,7 +141,8 @@ sets the recording device to
 .Ar rdev
 .El
 .Pp
-The above commands work on an internal mask.  After all the options
+The above commands work on an internal mask.
+After all the options
 have been parsed, it will set then read the mask from the sound card.
 This will let you see EXACTLY what the soundcard is using for the
 recording device(s).
@@ -147,17 +155,14 @@ The option
 will open
 .Ar device
 as the mixer device.
-.Pp
-The option
-.Fl h
-will print a help and exit
 .Sh FILES
 .Bl -tag -width /dev/mixer -compact
 .It Pa /dev/mixer
 the default mixer device
 .El
 .Sh SEE ALSO
-.Xr cdcontrol 1
+.Xr cdcontrol 1 ,
+.Xr sound 4
 .Sh HISTORY
 The
 .Nm
index 29aa408..7f1b262 100644 (file)
  *
  * ditto for my modifications (John-Mark Gurney, 1997)
  *
- * $FreeBSD: src/usr.sbin/mixer/mixer.c,v 1.11.2.6 2001/07/30 10:22:58 dd Exp $
- * $DragonFly: src/usr.sbin/mixer/mixer.c,v 1.6 2004/04/15 12:58:12 joerg Exp $
+ * $FreeBSD: head/usr.sbin/mixer/mixer.c 230611 2012-01-27 09:15:55Z mav $
  */
 
 #include <err.h>
 #include <fcntl.h>
+#include <libgen.h>
+#include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/soundcard.h>
 
-#define LEFT(vol) (vol & 0x7f)
-#define RIGHT(vol) ((vol >> 8) & 0x7f)
-
 static const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
-static const char *defaultdev = "/dev/mixer";
 
 static void    usage(int devmask, int recmask);
 static int     res_name(const char *name, int mask);
-static void    print_recsrc(int recsrc);
-static void    print_recsrc_short(int recsrc);
+static void    print_recsrc(int recsrc, int recmask, int sflag);
 
-void
+static void
 usage(int devmask, int recmask)
 {
-       int i, 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)  {
-                       if (n)
-                               printf(", ");
-                       printf("%s", names[i]);
-                       n = 1;
+       int     i, n;
+
+       printf("usage: mixer [-f device] [-s | -S] [dev [+|-][voll[:[+|-]volr]] ...\n"
+           "       mixer [-f device] [-s | -S] recsrc ...\n"
+           "       mixer [-f device] [-s | -S] {^|+|-|=}rec rdev ...\n");
+       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]);
+                               n++;
+                       }
                }
        }
-       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]);
-                       n = 1;
+       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]);
+                               n++;
+                       }
                }
        }
        printf("\n");
        exit(1);
 }
 
-int
+static int
 res_name(const char *name, int mask)
 {
-       int i;
+       int     i;
 
        for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
-               if ((1 << i) & mask && !strcmp(names[i], name))
+               if ((1 << i) & mask && strcmp(names[i], name) == 0)
                        break;
 
        if (i == SOUND_MIXER_NRDEVICES)
-               return(-1);
+               return (-1);
 
-       return(i);
+       return (i);
 }
 
-void
-print_recsrc(int recsrc)
+static void
+print_recsrc(int recsrc, int recmask, int sflag)
 {
-       int i, n = 0;
-       printf("Recording source: ");
+       int     i, n;
+
+       if (recmask == 0)
+               return;
 
-       for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+       if (!sflag)
+               printf("Recording source: ");
+
+       for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++) {
                if ((1 << i) & recsrc) {
-                       if (n)
+                       if (sflag)
+                               printf("%srec ", n ? " +" : "=");
+                       else if (n)
                                printf(", ");
                        printf("%s", names[i]);
-                       n = 1;
+                       n++;
                }
        }
-       printf("\n");
+       if (!sflag)
+               printf("\n");
 }
 
-void
-print_recsrc_short(int recsrc)
+int
+main(int argc, char *argv[])
 {
-       int i, first;
-
-       first = 1;
-
-       for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
-               if ((1 << i) & recsrc) {
-                       if (first) {
-                               printf("=rec ");
-                               first = 0;
-                       }
-                       printf("%s ", names[i]);
-               }
+       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, i, bar, baz, dev, m, n, t;
+
+       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;
 
-int
-main(int argc, char **argv)
-{
-       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;
-
-       const char *name = defaultdev;
-
-       while ((ch = getopt(argc, argv, "f:sh")) != -1)
-               switch (ch) {
-                       case 'f':
-                               name = optarg;
-                               break;
-                       case 's':
-                               shortflag = 1;
-                               break;
-                       case 'h': /* Fall through */
-                       default:
+       n = 1;
+       for (;;) {
+               if (n >= argc || *argv[n] != '-')
+                       break;
+               if (strlen(argv[n]) != 2) {
+                       if (strcmp(argv[n] + 1, "rec") != 0)
                                dusage = 1;
+                       break;
+               }
+               ch = *(argv[n] + 1);
+               if (ch == 'f' && n < argc - 1) {
+                       name = argv[n + 1];
+                       n += 2;
+               } else if (ch == 's') {
+                       sflag = 1;
+                       n++;
+               } else if (ch == 'S') {
+                       Sflag = 1;
+                       n++;
+               } else {
+                       dusage = 1;
+                       break;
                }
-       argc -= optind;
-       argv += optind;
+       }
+       if (sflag && Sflag)
+               dusage = 1;
+
+       argc -= n - 1;
+       argv += n - 1;
 
-       if ((fd = open(name, O_RDWR)) < 0)
+       if ((baz = open(name, O_RDWR)) < 0)
                err(1, "%s", name);
-       if (ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
+       if (ioctl(baz, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
                err(1, "SOUND_MIXER_READ_DEVMASK");
-       if (ioctl(fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
+       if (ioctl(baz, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
                err(1, "SOUND_MIXER_READ_RECMASK");
-       if (ioctl(fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
+       if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
                err(1, "SOUND_MIXER_READ_RECSRC");
        orecsrc = recsrc;
 
-       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)) 
+       if (argc == 1 && dusage == 0) {
+               for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+                       if (!((1 << i) & devmask))
                                continue;
-                       if (ioctl(fd, MIXER_READ(i),&mset)== -1) {
+                       if (ioctl(baz, MIXER_READ(i),&bar) == -1) {
                                warn("MIXER_READ");
                                continue;
                        }
-                       if (shortflag)
-                               printf("%s %d:%d ", names[i], LEFT(mset),
-                                      RIGHT(mset));
-                       else
-                               printf("Mixer %-8s is currently set to %3d:%d\n",
-                                      names[i], LEFT(mset), RIGHT(mset));
+                       if (Sflag || sflag) {
+                               printf("%s%s%c%d:%d", n ? " " : "",
+                                   names[i], Sflag ? ':' : ' ',
+                                   bar & 0x7f, (bar >> 8) & 0x7f);
+                               n++;
+                       } else
+                               printf("Mixer %-8s is currently set to "
+                                   "%3d:%d\n", names[i], bar & 0x7f,
+                                   (bar >> 8) & 0x7f);
                }
-               if (ioctl(fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
-                       err(1, "SOUND_MIXER_READ_RECSRC");
-               if (shortflag) {
-                       print_recsrc_short(recsrc);
-                       if (isatty(STDOUT_FILENO))
-                               printf("\n");
-               } else
-                       print_recsrc(recsrc);
-               exit(0);
+               if (n && recmask)
+                       printf(" ");
+               print_recsrc(recsrc, recmask, Sflag || sflag);
+               return (0);
        }
 
+       argc--;
+       argv++;
 
-
-       while (argc > 0) {
-               if (!strcmp("recsrc", *argv)) {
+       n = 0;
+       while (argc > 0 && dusage == 0) {
+               if (strcmp("recsrc", *argv) == 0) {
                        drecsrc = 1;
-                       argc--; argv++;
+                       argc--;
+                       argv++;
                        continue;
-               } else if (argc > 1 && !strcmp("rec", *argv + 1)) {
+               } else if (strcmp("rec", *argv + 1) == 0) {
                        if (**argv != '+' && **argv != '-' &&
                            **argv != '=' && **argv != '^') {
                                warnx("unknown modifier: %c", **argv);
                                dusage = 1;
                                break;
                        }
+                       if (argc <= 1) {
+                               warnx("no recording device specified");
+                               dusage = 1;
+                               break;
+                       }
                        if ((dev = res_name(argv[1], recmask)) == -1) {
                                warnx("unknown recording device: %s", argv[1]);
                                dusage = 1;
                                break;
                        }
-                       switch(**argv) {
+                       switch (**argv) {
                        case '+':
                                recsrc |= (1 << dev);
                                break;
@@ -217,65 +230,70 @@ main(int argc, char **argv)
                                break;
                        }
                        drecsrc = 1;
-                       argc -= 2; argv += 2;
+                       argc -= 2;
+                       argv += 2;
                        continue;
                }
 
-               if ((t = sscanf(*argv, "%d:%d", &l, &r)) > 0) {
+               if ((t = sscanf(*argv, "%d:%d", &l, &r)) > 0)
                        dev = 0;
-               }
-               else if((dev = res_name(*argv, devmask)) == -1) {
+               else if ((dev = res_name(*argv, devmask)) == -1) {
                        warnx("unknown device: %s", *argv);
                        dusage = 1;
                        break;
                }
 
-#define        issign(c)       (((c) == '+') || ((c) == '-'))
-
+               lrel = rrel = 0;
                if (argc > 1) {
-                       n = sscanf(argv[1], "%7[^:]:%7s", lstr, rstr);
-                       if (n > 0) {
-                               if (issign(lstr[0]))
+                       m = sscanf(argv[1], "%7[^:]:%7s", lstr, rstr);
+                       if (m > 0) {
+                               if (*lstr == '+' || *lstr == '-')
                                        lrel = rrel = 1;
-                               l = atoi(lstr);
+                               l = strtol(lstr, NULL, 10);
                        }
-                       if (n > 1) {
-                               rrel = 0;
-                               if (issign(rstr[0]))
+                       if (m > 1) {
+                               if (*rstr == '+' || *rstr == '-')
                                        rrel = 1;
-                               r = atoi(rstr);
+                               r = strtol(rstr, NULL, 10);
                        }
                }
 
-               switch(argc > 1 ? n : t) {
+               switch (argc > 1 ? m : t) {
                case 0:
-                       if (ioctl(fd, MIXER_READ(dev),&mset)== -1) {
+                       if (ioctl(baz, MIXER_READ(dev), &bar) == -1) {
                                warn("MIXER_READ");
-                               argc--; argv++;
+                               argc--;
+                               argv++;
                                continue;
                        }
-                       if (shortflag)
-                               printf("%s %d:%d ", names[dev], LEFT(mset),
-                                      RIGHT(mset));
-                       else
-                               printf("Mixer %-8s is currently set to %3d:%d\n",
-                                      names[dev], LEFT(mset), RIGHT(mset));
-
-                       argc--; argv++;
+                       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++;
                        break;
                case 1:
                        r = l;
+                       /* FALLTHROUGH */
                case 2:
-                       if (ioctl(fd, MIXER_READ(dev),&mset)== -1) {
+                       if (ioctl(baz, MIXER_READ(dev), &bar) == -1) {
                                warn("MIXER_READ");
-                               argc--; argv++;
+                               argc--;
+                               argv++;
                                continue;
                        }
 
                        if (lrel)
-                               l += LEFT(mset);
+                               l = (bar & 0x7f) + l;
                        if (rrel)
-                               r += RIGHT(mset);
+                               r = ((bar >> 8) & 0x7f) + r;
 
                        if (l < 0)
                                l = 0;
@@ -286,29 +304,38 @@ main(int argc, char **argv)
                        else if (r > 100)
                                r = 100;
 
-                       printf("Setting the mixer %s to %d:%d.\n", names[dev],
-                              l, r);
+                       if (!Sflag)
+                               printf("Setting the mixer %s from %d:%d to "
+                                   "%d:%d.\n", names[dev], bar & 0x7f,
+                                   (bar >> 8) & 0x7f, l, r);
 
                        l |= r << 8;
-                       if (ioctl(fd, MIXER_WRITE(dev), &l) == -1)
+                       if (ioctl(baz, MIXER_WRITE(dev), &l) == -1)
                                warn("WRITE_MIXER");
 
-                       argc -= 2; argv += 2;
+                       argc -= 2;
+                       argv += 2;
                        break;
                }
        }
 
-       if (orecsrc != recsrc)
-               if (ioctl(fd, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1)
+       if (dusage) {
+               close(baz);
+               usage(devmask, recmask);
+               /* NOTREACHED */
+       }
+
+       if (orecsrc != recsrc) {
+               if (ioctl(baz, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1)
                        err(1, "SOUND_MIXER_WRITE_RECSRC");
-       if (drecsrc) {
-               if (ioctl(fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
+               if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
                        err(1, "SOUND_MIXER_READ_RECSRC");
-               print_recsrc(recsrc);
        }
 
-       close(fd);
+       if (drecsrc)
+               print_recsrc(recsrc, recmask, Sflag || sflag);
+
+       close(baz);
 
-       exit(0);
+       return (0);
 }