Home | History | Annotate | Line # | Download | only in audiocfg
main.c revision 1.3
      1  1.3       wiz /* $NetBSD: main.c,v 1.3 2010/09/01 09:18:03 wiz Exp $ */
      2  1.1       mrg 
      3  1.1       mrg /*
      4  1.1       mrg  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1       mrg  * All rights reserved.
      6  1.1       mrg  *
      7  1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8  1.1       mrg  * modification, are permitted provided that the following conditions
      9  1.1       mrg  * are met:
     10  1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11  1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12  1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15  1.1       mrg  *
     16  1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1       mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1       mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1       mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1       mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1       mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1       mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1       mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1       mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1       mrg  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1       mrg  */
     28  1.1       mrg 
     29  1.1       mrg #include <errno.h>
     30  1.1       mrg #include <fcntl.h>
     31  1.1       mrg #include <limits.h>
     32  1.1       mrg #include <stdio.h>
     33  1.1       mrg #include <stdlib.h>
     34  1.1       mrg #include <unistd.h>
     35  1.1       mrg 
     36  1.1       mrg #include "audiodev.h"
     37  1.1       mrg #include "drvctl.h"
     38  1.1       mrg 
     39  1.1       mrg static void
     40  1.1       mrg usage(const char *p)
     41  1.1       mrg {
     42  1.2  jmcneill 	fprintf(stderr, "usage: %s list\n", p);
     43  1.3       wiz 	fprintf(stderr, "       %s default <index>\n", p);
     44  1.3       wiz 	fprintf(stderr, "       %s test <index>\n", p);
     45  1.1       mrg 	exit(EXIT_FAILURE);
     46  1.1       mrg }
     47  1.1       mrg 
     48  1.1       mrg int
     49  1.1       mrg main(int argc, char *argv[])
     50  1.1       mrg {
     51  1.1       mrg 	struct audiodev *adev;
     52  1.1       mrg 	unsigned int n, i;
     53  1.1       mrg 
     54  1.1       mrg 	if (audiodev_refresh() == -1)
     55  1.1       mrg 		return EXIT_FAILURE;
     56  1.1       mrg 
     57  1.2  jmcneill 	if (argc < 2)
     58  1.2  jmcneill 		usage(argv[0]);
     59  1.2  jmcneill 		/* NOTREACHED */
     60  1.2  jmcneill 
     61  1.2  jmcneill 	if (strcmp(argv[1], "list") == 0) {
     62  1.1       mrg 		n = audiodev_count();
     63  1.1       mrg 		for (i = 0; i < n; i++) {
     64  1.1       mrg 			adev = audiodev_get(i);
     65  1.2  jmcneill 			printf("%u: [%c] %s: %s (%u playback channel%s)\n",
     66  1.1       mrg 			    i, adev->defaultdev ? '*' : ' ',
     67  1.2  jmcneill 			    adev->xname, adev->audio_device.name,
     68  1.2  jmcneill 			    adev->pchan, adev->pchan == 1 ? "" : "s");
     69  1.1       mrg 		}
     70  1.2  jmcneill 	} else if (strcmp(argv[1], "default") == 0 && argc == 3) {
     71  1.2  jmcneill 		if (*argv[2] < '0' || *argv[2] > '9')
     72  1.1       mrg 			usage(argv[0]);
     73  1.1       mrg 			/* NOTREACHED */
     74  1.1       mrg 		errno = 0;
     75  1.2  jmcneill 		i = strtoul(argv[2], NULL, 10);
     76  1.1       mrg 		if (errno)
     77  1.1       mrg 			usage(argv[0]);
     78  1.1       mrg 			/* NOTREACHED */
     79  1.1       mrg 		adev = audiodev_get(i);
     80  1.1       mrg 		if (adev == NULL) {
     81  1.1       mrg 			fprintf(stderr, "no such device\n");
     82  1.1       mrg 			return EXIT_FAILURE;
     83  1.1       mrg 		}
     84  1.1       mrg 		printf("setting default audio device to %s\n", adev->xname);
     85  1.1       mrg 		if (audiodev_set_default(adev) == -1) {
     86  1.1       mrg 			perror("couldn't set default device");
     87  1.1       mrg 			return EXIT_FAILURE;
     88  1.1       mrg 		}
     89  1.2  jmcneill 	} else if (strcmp(argv[1], "test") == 0 && argc == 3) {
     90  1.2  jmcneill 		if (*argv[2] < '0' || *argv[2] > '9')
     91  1.2  jmcneill 			usage(argv[0]);
     92  1.2  jmcneill 			/* NOTREACHED */
     93  1.2  jmcneill 		errno = 0;
     94  1.2  jmcneill 		i = strtoul(argv[2], NULL, 10);
     95  1.2  jmcneill 		if (errno)
     96  1.2  jmcneill 			usage(argv[0]);
     97  1.2  jmcneill 			/* NOTREACHED */
     98  1.2  jmcneill 		adev = audiodev_get(i);
     99  1.2  jmcneill 		if (adev == NULL) {
    100  1.2  jmcneill 			fprintf(stderr, "no such device\n");
    101  1.2  jmcneill 			return EXIT_FAILURE;
    102  1.2  jmcneill 		}
    103  1.2  jmcneill 		for (i = 0; i < adev->pchan; i++) {
    104  1.2  jmcneill 			printf("testing channel %d...\n", i);
    105  1.2  jmcneill 			audiodev_test(adev, 1 << i);
    106  1.2  jmcneill 		}
    107  1.2  jmcneill 	} else
    108  1.1       mrg 		usage(argv[0]);
    109  1.1       mrg 		/* NOTREACHED */
    110  1.1       mrg 
    111  1.1       mrg 	return EXIT_SUCCESS;
    112  1.1       mrg }
    113