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