xref: /netbsd-src/usr.bin/mixerctl/mixerctl.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: mixerctl.c,v 1.4 1997/05/23 17:55:29 augustss Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Author: Lennart Augustsson, with some code and ideas from Chuck Cranor.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <err.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 #include <sys/audioio.h>
46 
47 FILE *out = stdout;
48 
49 char *prog;
50 
51 struct field {
52 	char *name;
53 	mixer_ctrl_t *valp;
54 	mixer_devinfo_t *infp;
55 	char changed;
56 } *fields, *rfields;
57 
58 mixer_ctrl_t *values;
59 mixer_devinfo_t *infos;
60 
61 char *
62 catstr(char *p, char *q)
63 {
64 	char *r = malloc(strlen(p) + strlen(q) + 2);
65 	strcpy(r, p);
66 	strcat(r, ".");
67 	strcat(r, q);
68 	return r;
69 }
70 
71 struct field *
72 findfield(char *name)
73 {
74 	int i;
75 	for(i = 0; fields[i].name; i++)
76 		if (strcmp(fields[i].name, name) == 0)
77 			return &fields[i];
78 	return 0;
79 }
80 
81 void
82 prfield(struct field *p, char *sep, int prvalset)
83 {
84 	mixer_ctrl_t *m;
85 	int i, n;
86 
87 	if (sep)
88 		fprintf(out, "%s%s", p->name, sep);
89 	m = p->valp;
90 	switch(m->type) {
91 	case AUDIO_MIXER_ENUM:
92 		for(i = 0; i < p->infp->un.e.num_mem; i++)
93 			if (p->infp->un.e.member[i].ord == m->un.ord)
94 				fprintf(out, "%s",
95 					p->infp->un.e.member[i].label.name);
96 		if (prvalset) {
97 			fprintf(out, "  [ ");
98 			for(i = 0; i < p->infp->un.e.num_mem; i++)
99 				fprintf(out, "%s ", p->infp->un.e.member[i].label.name);
100 			fprintf(out, "]");
101 		}
102 		break;
103 	case AUDIO_MIXER_SET:
104 		for(n = i = 0; i < p->infp->un.s.num_mem; i++)
105 			if (m->un.mask & p->infp->un.s.member[i].mask)
106 				fprintf(out, "%s%s", n++ ? "," : "",
107 					p->infp->un.s.member[i].label.name);
108 		if (prvalset) {
109 			fprintf(out, "  { ");
110 			for(i = 0; i < p->infp->un.s.num_mem; i++)
111 				fprintf(out, "%s ", p->infp->un.s.member[i].label.name);
112 			fprintf(out, "}");
113 		}
114 		break;
115 	case AUDIO_MIXER_VALUE:
116 		if (m->un.value.num_channels == 1)
117 			fprintf(out, "%d", m->un.value.level[0]);
118 		else
119 			fprintf(out, "%d,%d", m->un.value.level[0],
120 			       m->un.value.level[1]);
121 		break;
122 	default:
123 		errx(1, "Invalid format.");
124 	}
125 }
126 
127 int
128 rdfield(struct field *p, char *q)
129 {
130 	mixer_ctrl_t *m;
131 	int v, v0, v1, mask;
132 	int i;
133 	char *s;
134 
135 	m = p->valp;
136 	switch(m->type) {
137 	case AUDIO_MIXER_ENUM:
138 		for(i = 0; i < p->infp->un.e.num_mem; i++)
139 			if (strcmp(p->infp->un.e.member[i].label.name, q) == 0)
140 				break;
141 		if (i < p->infp->un.e.num_mem)
142 			m->un.ord = p->infp->un.e.member[i].ord;
143 		else {
144 			warnx("Bad enum value %s", q);
145 			return 0;
146 		}
147 		break;
148 	case AUDIO_MIXER_SET:
149 		mask = 0;
150 		for(v = 0; q && *q; q = s) {
151 			s = strchr(q, ',');
152 			if (s)
153 				*s++ = 0;
154 			for(i = 0; i < p->infp->un.s.num_mem; i++)
155 				if (strcmp(p->infp->un.s.member[i].label.name, q) == 0)
156 					break;
157 			if (i < p->infp->un.s.num_mem) {
158 				mask |= p->infp->un.s.member[i].mask;
159 			} else {
160 				warnx("Bad set value %s", q);
161 				return 0;
162 			}
163 		}
164 		m->un.mask = mask;
165 		break;
166 	case AUDIO_MIXER_VALUE:
167 		if (m->un.value.num_channels == 1) {
168 			if (sscanf(q, "%d", &v) == 1) {
169 				m->un.value.level[0] = v;
170 			} else {
171 				warnx("Bad number %s", q);
172 				return 0;
173 			}
174 		} else {
175 			if (sscanf(q, "%d,%d", &v0, &v1) == 2) {
176 				m->un.value.level[0] = v0;
177 				m->un.value.level[1] = v1;
178 			} else if (sscanf(q, "%d", &v) == 1) {
179 				m->un.value.level[0] = m->un.value.level[1] = v;
180 			} else {
181 				warnx("Bad numbers %s", q);
182 				return 0;
183 			}
184 		}
185 		break;
186 	default:
187 		errx(1, "Invalid format.");
188 	}
189 	p->changed = 1;
190 	return 1;
191 }
192 
193 void
194 main(int argc, char **argv)
195 {
196 	int fd, r, i, j, ch, pos;
197 	int aflag = 0, wflag = 0, vflag = 0;
198 	char *file = "/dev/mixer";
199 	char *sep = "=";
200 	mixer_devinfo_t dinfo;
201 	mixer_ctrl_t val;
202 	int ndev;
203 
204 	prog = *argv;
205 
206 	while ((ch = getopt(argc, argv, "af:nvw")) != -1) {
207 		switch(ch) {
208 		case 'a':
209 			aflag++;
210 			break;
211 		case 'w':
212 			wflag++;
213 			break;
214 		case 'v':
215 			vflag++;
216 			break;
217 		case 'n':
218 			sep = 0;
219 			break;
220 		case 'f':
221 			file = optarg;
222 			break;
223 		case '?':
224 		default:
225 		usage:
226 		fprintf(out, "%s [-f file] [-v] [-n] name ...\n", prog);
227 		fprintf(out, "%s [-f file] [-v] [-n] -w name=value ...\n", prog);
228 		fprintf(out, "%s [-f file] [-v] [-n] -a\n", prog);
229 		exit(0);
230 		}
231 	}
232 	argc -= optind;
233 	argv += optind;
234 
235 	fd = open(file, O_RDWR);
236 	if (fd < 0)
237 		err(1, "%s", file);
238 
239 	for(ndev = 0; ; ndev++) {
240 		dinfo.index = ndev;
241 		if (ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo) < 0)
242 			break;
243 	}
244 	rfields = calloc(ndev, sizeof *rfields);
245 	fields = calloc(ndev, sizeof *fields);
246 	infos = calloc(ndev, sizeof *infos);
247 	values = calloc(ndev, sizeof *values);
248 
249 	for(i = 0; i < ndev; i++) {
250 		infos[i].index = i;
251 		ioctl(fd, AUDIO_MIXER_DEVINFO, &infos[i]);
252 	}
253 
254 	for(i = 0; i < ndev; i++) {
255 		if (infos[i].mixer_class >= 0 && infos[i].mixer_class < ndev)
256 			rfields[i].name = catstr(infos[infos[i].mixer_class].label.name,
257 						 infos[i].label.name);
258 		else
259 			rfields[i].name = infos[i].label.name;
260 		rfields[i].valp = &values[i];
261 		rfields[i].infp = &infos[i];
262 	}
263 
264 	for(i = 0; i < ndev; i++) {
265 		values[i].dev = i;
266 		values[i].type = infos[i].type;
267 		if (infos[i].type != AUDIO_MIXER_CLASS) {
268 			values[i].un.value.num_channels = 2;
269 			if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0) {
270 				values[i].un.value.num_channels = 1;
271 				if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0)
272 					err(1, NULL);
273 			}
274 		}
275 	}
276 
277 	for(j = i = 0; i < ndev; i++) {
278 		if (infos[i].type != AUDIO_MIXER_CLASS &&
279 		    infos[i].type != -1) {
280 			fields[j++] = rfields[i];
281 			for(pos = infos[i].next; pos != AUDIO_MIXER_LAST;
282 			    pos = infos[pos].next) {
283 				fields[j] = rfields[pos];
284 				fields[j].name = catstr(rfields[i].name,
285 							infos[pos].label.name);
286 				infos[pos].type = -1;
287 				j++;
288 			}
289 		}
290 	}
291 
292 	if (argc == 0 && aflag && !wflag) {
293 		for(i = 0; fields[i].name; i++) {
294 			prfield(&fields[i], sep, vflag);
295 			fprintf(out, "\n");
296 		}
297 	} else if (argc > 0 && !aflag) {
298 		struct field *p;
299 		if (wflag) {
300 			while(argc--) {
301 				char *q;
302 
303 				q = strchr(*argv, '=');
304 				if (q) {
305 					*q++ = 0;
306 					p = findfield(*argv);
307 					if (p == 0)
308 						warnx("field %s does not exist", *argv);
309 					else {
310 						val = *p->valp;
311 						if (rdfield(p, q)) {
312 							if (ioctl(fd, AUDIO_MIXER_WRITE, p->valp) < 0)
313 								warn(NULL);
314 							else if (sep) {
315 								*p->valp = val;
316 								prfield(p, ": ", 0);
317 								ioctl(fd, AUDIO_MIXER_READ, p->valp);
318 								printf(" -> ");
319 								prfield(p, 0, 0);
320 								printf("\n");
321 							}
322 						}
323 					}
324 				} else {
325 					warnx("No `=' in %s", *argv);
326 				}
327 				argv++;
328 			}
329 		} else {
330 			while(argc--) {
331 				p = findfield(*argv);
332 				if (p == 0)
333 					warnx("field %s does not exist", *argv);
334 				else
335 					prfield(p, sep, vflag), fprintf(out, "\n");
336 				argv++;
337 			}
338 		}
339 	} else
340 		goto usage;
341 	exit(0);
342 }
343