ctl.c revision 1.11 1 1.11 augustss /* $NetBSD: ctl.c,v 1.11 1997/10/11 13:40:26 augustss Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * Author: Lennart Augustsson
8 1.1 augustss *
9 1.1 augustss * Redistribution and use in source and binary forms, with or without
10 1.1 augustss * modification, are permitted provided that the following conditions
11 1.1 augustss * are met:
12 1.1 augustss * 1. Redistributions of source code must retain the above copyright
13 1.1 augustss * notice, this list of conditions and the following disclaimer.
14 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer in the
16 1.1 augustss * documentation and/or other materials provided with the distribution.
17 1.1 augustss * 3. All advertising materials mentioning features or use of this software
18 1.1 augustss * must display the following acknowledgement:
19 1.1 augustss * This product includes software developed by the NetBSD
20 1.1 augustss * Foundation, Inc. and its contributors.
21 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 augustss * contributors may be used to endorse or promote products derived
23 1.1 augustss * from this software without specific prior written permission.
24 1.1 augustss *
25 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.10 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.10 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
36 1.1 augustss */
37 1.1 augustss
38 1.1 augustss #include <stdio.h>
39 1.1 augustss #include <fcntl.h>
40 1.1 augustss #include <err.h>
41 1.1 augustss #include <unistd.h>
42 1.1 augustss #include <string.h>
43 1.1 augustss #include <sys/types.h>
44 1.2 augustss #include <sys/stat.h>
45 1.1 augustss #include <sys/ioctl.h>
46 1.1 augustss #include <sys/audioio.h>
47 1.1 augustss
48 1.9 augustss struct field *findfield __P((char *name));
49 1.9 augustss void prfield __P((struct field *p, char *sep));
50 1.9 augustss void rdfield __P((struct field *p, char *q));
51 1.9 augustss void getinfo __P((int fd));
52 1.9 augustss void usage __P((void));
53 1.9 augustss int main __P((int argc, char **argv));
54 1.9 augustss
55 1.1 augustss FILE *out = stdout;
56 1.1 augustss
57 1.1 augustss char *prog;
58 1.1 augustss
59 1.1 augustss audio_device_t adev;
60 1.1 augustss
61 1.1 augustss audio_info_t info;
62 1.1 augustss
63 1.1 augustss char encbuf[1000];
64 1.1 augustss
65 1.6 augustss int properties, fullduplex, rerror;
66 1.1 augustss
67 1.1 augustss struct field {
68 1.1 augustss char *name;
69 1.1 augustss void *valp;
70 1.1 augustss int format;
71 1.1 augustss #define STRING 1
72 1.1 augustss #define INT 2
73 1.1 augustss #define UINT 3
74 1.1 augustss #define P_R 4
75 1.1 augustss #define ULONG 5
76 1.1 augustss #define UCHAR 6
77 1.1 augustss #define ENC 7
78 1.6 augustss #define PROPS 8
79 1.2 augustss char flags;
80 1.2 augustss #define READONLY 1
81 1.2 augustss #define ALIAS 2
82 1.2 augustss #define SET 4
83 1.1 augustss } fields[] = {
84 1.11 augustss { "name", &adev.name, STRING, READONLY },
85 1.11 augustss { "version", &adev.version, STRING, READONLY },
86 1.11 augustss { "config", &adev.config, STRING, READONLY },
87 1.11 augustss { "encodings", encbuf, STRING, READONLY },
88 1.11 augustss { "properties", &properties, PROPS, READONLY },
89 1.11 augustss { "full_duplex", &fullduplex, INT, 0 },
90 1.11 augustss { "buffersize", &info.buffersize, UINT, 0 },
91 1.11 augustss { "blocksize", &info.blocksize, UINT, 0 },
92 1.11 augustss { "hiwat", &info.hiwat, UINT, 0 },
93 1.11 augustss { "lowat", &info.lowat, UINT, 0 },
94 1.11 augustss { "backlog", &info.backlog, UINT, 0 },
95 1.11 augustss { "mode", &info.mode, P_R, READONLY },
96 1.11 augustss { "play.rate", &info.play.sample_rate, UINT, 0 },
97 1.11 augustss { "play.sample_rate", &info.play.sample_rate, UINT, ALIAS },
98 1.11 augustss { "play.channels", &info.play.channels, UINT, 0 },
99 1.11 augustss { "play.precision", &info.play.precision, UINT, 0 },
100 1.11 augustss { "play.encoding", &info.play.encoding, ENC, 0 },
101 1.11 augustss { "play.gain", &info.play.gain, UINT, 0 },
102 1.11 augustss { "play.port", &info.play.port, UINT, 0 },
103 1.11 augustss { "play.seek", &info.play.seek, ULONG, READONLY },
104 1.11 augustss { "play.samples", &info.play.samples, UINT, READONLY },
105 1.11 augustss { "play.eof", &info.play.eof, UINT, READONLY },
106 1.11 augustss { "play.pause", &info.play.pause, UCHAR, 0 },
107 1.11 augustss { "play.error", &info.play.error, UCHAR, READONLY },
108 1.11 augustss { "play.waiting", &info.play.waiting, UCHAR, READONLY },
109 1.11 augustss { "play.open", &info.play.open, UCHAR, READONLY },
110 1.11 augustss { "play.active", &info.play.active, UCHAR, READONLY },
111 1.11 augustss { "record.rate", &info.record.sample_rate,UINT, 0 },
112 1.11 augustss { "record.sample_rate", &info.record.sample_rate,UINT, ALIAS },
113 1.11 augustss { "record.channels", &info.record.channels, UINT, 0 },
114 1.11 augustss { "record.precision", &info.record.precision, UINT, 0 },
115 1.11 augustss { "record.encoding", &info.record.encoding, ENC, 0 },
116 1.11 augustss { "record.gain", &info.record.gain, UINT, 0 },
117 1.11 augustss { "record.port", &info.record.port, UINT, 0 },
118 1.11 augustss { "record.seek", &info.record.seek, ULONG, READONLY },
119 1.11 augustss { "record.samples", &info.record.samples, UINT, READONLY },
120 1.11 augustss { "record.eof", &info.record.eof, UINT, READONLY },
121 1.11 augustss { "record.pause", &info.record.pause, UCHAR, 0 },
122 1.11 augustss { "record.error", &info.record.error, UCHAR, READONLY },
123 1.11 augustss { "record.waiting", &info.record.waiting, UCHAR, READONLY },
124 1.11 augustss { "record.open", &info.record.open, UCHAR, READONLY },
125 1.11 augustss { "record.active", &info.record.active, UCHAR, READONLY },
126 1.11 augustss { "record.errors", &rerror, INT, READONLY },
127 1.11 augustss { 0 }
128 1.1 augustss };
129 1.1 augustss
130 1.1 augustss struct {
131 1.1 augustss char *ename;
132 1.1 augustss int eno;
133 1.1 augustss } encs[] = {
134 1.11 augustss { "ulaw", AUDIO_ENCODING_ULAW },
135 1.11 augustss { "mulaw", AUDIO_ENCODING_ULAW },
136 1.11 augustss { "alaw", AUDIO_ENCODING_ALAW },
137 1.11 augustss { "slinear", AUDIO_ENCODING_SLINEAR },
138 1.11 augustss { "linear", AUDIO_ENCODING_SLINEAR },
139 1.11 augustss { "ulinear", AUDIO_ENCODING_ULINEAR },
140 1.11 augustss { "adpcm", AUDIO_ENCODING_ADPCM },
141 1.11 augustss { "ADPCM", AUDIO_ENCODING_ADPCM },
142 1.11 augustss { "slinear_le", AUDIO_ENCODING_SLINEAR_LE },
143 1.11 augustss { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
144 1.11 augustss { "ulinear_le", AUDIO_ENCODING_ULINEAR_LE },
145 1.11 augustss { "slinear_be", AUDIO_ENCODING_SLINEAR_BE },
146 1.11 augustss { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
147 1.11 augustss { "ulinear_be", AUDIO_ENCODING_ULINEAR_BE },
148 1.11 augustss { 0 }
149 1.11 augustss };
150 1.11 augustss
151 1.11 augustss static struct {
152 1.11 augustss char *name;
153 1.11 augustss u_int prop;
154 1.11 augustss } props[] = {
155 1.11 augustss { "full_duplex", AUDIO_PROP_FULLDUPLEX },
156 1.11 augustss { "mmap", AUDIO_PROP_MMAP },
157 1.11 augustss { "independent", AUDIO_PROP_INDEPENDENT },
158 1.11 augustss { 0 }
159 1.1 augustss };
160 1.1 augustss
161 1.1 augustss struct field *
162 1.9 augustss findfield(name)
163 1.11 augustss char *name;
164 1.1 augustss {
165 1.11 augustss int i;
166 1.11 augustss for(i = 0; fields[i].name; i++)
167 1.11 augustss if (strcmp(fields[i].name, name) == 0)
168 1.11 augustss return &fields[i];
169 1.11 augustss return 0;
170 1.1 augustss }
171 1.1 augustss
172 1.1 augustss void
173 1.9 augustss prfield(p, sep)
174 1.11 augustss struct field *p;
175 1.11 augustss char *sep;
176 1.1 augustss {
177 1.11 augustss u_int v;
178 1.11 augustss char *cm;
179 1.11 augustss int i;
180 1.11 augustss
181 1.11 augustss if (sep)
182 1.11 augustss fprintf(out, "%s%s", p->name, sep);
183 1.11 augustss switch(p->format) {
184 1.11 augustss case STRING:
185 1.11 augustss fprintf(out, "%s", (char*)p->valp);
186 1.11 augustss break;
187 1.11 augustss case INT:
188 1.11 augustss fprintf(out, "%d", *(int*)p->valp);
189 1.11 augustss break;
190 1.11 augustss case UINT:
191 1.11 augustss fprintf(out, "%u", *(u_int*)p->valp);
192 1.11 augustss break;
193 1.11 augustss case UCHAR:
194 1.11 augustss fprintf(out, "%u", *(u_char*)p->valp);
195 1.11 augustss break;
196 1.11 augustss case ULONG:
197 1.11 augustss fprintf(out, "%lu", *(u_long*)p->valp);
198 1.11 augustss break;
199 1.11 augustss case P_R:
200 1.11 augustss v = *(u_int*)p->valp;
201 1.11 augustss cm = "";
202 1.11 augustss if (v & AUMODE_PLAY) {
203 1.11 augustss if (v & AUMODE_PLAY_ALL)
204 1.11 augustss fprintf(out, "play");
205 1.11 augustss else
206 1.11 augustss fprintf(out, "playsync");
207 1.11 augustss cm = ",";
208 1.11 augustss }
209 1.11 augustss if (v & AUMODE_RECORD)
210 1.11 augustss fprintf(out, "%srecord", cm);
211 1.11 augustss break;
212 1.11 augustss case ENC:
213 1.11 augustss v = *(u_int*)p->valp;
214 1.11 augustss for(i = 0; encs[i].ename; i++)
215 1.11 augustss if (encs[i].eno == v)
216 1.11 augustss break;
217 1.11 augustss if (encs[i].ename)
218 1.11 augustss fprintf(out, "%s", encs[i].ename);
219 1.11 augustss else
220 1.11 augustss fprintf(out, "%u", v);
221 1.11 augustss break;
222 1.11 augustss case PROPS:
223 1.11 augustss v = *(u_int*)p->valp;
224 1.11 augustss for (cm = "", i = 0; props[i].name; i++) {
225 1.11 augustss if (v & props[i].prop) {
226 1.11 augustss fprintf(out, "%s%s", cm, props[i].name);
227 1.11 augustss cm = ",";
228 1.11 augustss }
229 1.11 augustss }
230 1.1 augustss break;
231 1.11 augustss default:
232 1.11 augustss errx(1, "Invalid format.");
233 1.6 augustss }
234 1.2 augustss }
235 1.2 augustss
236 1.2 augustss void
237 1.9 augustss rdfield(p, q)
238 1.11 augustss struct field *p;
239 1.11 augustss char *q;
240 1.2 augustss {
241 1.11 augustss int i;
242 1.2 augustss
243 1.11 augustss switch(p->format) {
244 1.11 augustss case UINT:
245 1.11 augustss if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
246 1.11 augustss warnx("Bad number %s", q);
247 1.1 augustss break;
248 1.11 augustss case ENC:
249 1.11 augustss for(i = 0; encs[i].ename; i++)
250 1.11 augustss if (strcmp(encs[i].ename, q) == 0)
251 1.11 augustss break;
252 1.11 augustss if (encs[i].ename)
253 1.11 augustss *(u_int*)p->valp = encs[i].eno;
254 1.11 augustss else
255 1.11 augustss warnx("Unknown encoding: %s", q);
256 1.11 augustss break;
257 1.11 augustss default:
258 1.11 augustss errx(1, "Invalid format.");
259 1.11 augustss }
260 1.11 augustss p->flags |= SET;
261 1.1 augustss }
262 1.1 augustss
263 1.1 augustss void
264 1.9 augustss getinfo(fd)
265 1.11 augustss int fd;
266 1.1 augustss {
267 1.11 augustss int pos, i;
268 1.1 augustss
269 1.11 augustss if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
270 1.11 augustss err(1, "AUDIO_GETDEV");
271 1.11 augustss for(pos = 0, i = 0; ; i++) {
272 1.11 augustss audio_encoding_t enc;
273 1.11 augustss enc.index = i;
274 1.11 augustss if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
275 1.11 augustss break;
276 1.11 augustss if (pos)
277 1.11 augustss encbuf[pos++] = ',';
278 1.11 augustss sprintf(encbuf+pos, "%s:%d%s", enc.name,
279 1.11 augustss enc.precision,
280 1.11 augustss enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
281 1.11 augustss pos += strlen(encbuf+pos);
282 1.11 augustss }
283 1.11 augustss if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
284 1.11 augustss err(1, "AUDIO_GETFD");
285 1.11 augustss if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
286 1.11 augustss err(1, "AUDIO_GETPROPS");
287 1.11 augustss if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
288 1.11 augustss err(1, "AUDIO_RERROR");
289 1.11 augustss if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
290 1.11 augustss err(1, "AUDIO_GETINFO");
291 1.2 augustss }
292 1.2 augustss
293 1.2 augustss void
294 1.9 augustss usage()
295 1.2 augustss {
296 1.11 augustss fprintf(out, "%s [-f file] [-n] name ...\n", prog);
297 1.11 augustss fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
298 1.11 augustss fprintf(out, "%s [-f file] [-n] -a\n", prog);
299 1.11 augustss exit(1);
300 1.1 augustss }
301 1.1 augustss
302 1.9 augustss int
303 1.9 augustss main(argc, argv)
304 1.11 augustss int argc;
305 1.11 augustss char **argv;
306 1.1 augustss {
307 1.11 augustss int fd, i, ch;
308 1.11 augustss int aflag = 0, wflag = 0;
309 1.11 augustss struct stat dstat, ostat;
310 1.11 augustss char *file = "/dev/audioctl";
311 1.11 augustss char *sep = "=";
312 1.2 augustss
313 1.11 augustss prog = *argv;
314 1.2 augustss
315 1.11 augustss while ((ch = getopt(argc, argv, "af:nw")) != -1) {
316 1.11 augustss switch(ch) {
317 1.11 augustss case 'a':
318 1.11 augustss aflag++;
319 1.11 augustss break;
320 1.11 augustss case 'w':
321 1.11 augustss wflag++;
322 1.11 augustss break;
323 1.11 augustss case 'n':
324 1.11 augustss sep = 0;
325 1.11 augustss break;
326 1.11 augustss case 'f':
327 1.11 augustss file = optarg;
328 1.11 augustss break;
329 1.11 augustss case '?':
330 1.11 augustss default:
331 1.11 augustss usage();
332 1.11 augustss }
333 1.1 augustss }
334 1.11 augustss argc -= optind;
335 1.11 augustss argv += optind;
336 1.1 augustss
337 1.11 augustss fd = open(file, O_WRONLY);
338 1.11 augustss if (fd < 0)
339 1.11 augustss fd = open(file, O_RDONLY);
340 1.11 augustss if (fd < 0)
341 1.11 augustss err(1, "%s", file);
342 1.2 augustss
343 1.11 augustss /* Check if stdout is the same device as the audio device. */
344 1.11 augustss if (fstat(fd, &dstat) < 0)
345 1.11 augustss err(1, "fstat au");
346 1.11 augustss if (fstat(STDOUT_FILENO, &ostat) < 0)
347 1.9 augustss err(1, "fstat stdout");
348 1.11 augustss if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
349 1.11 augustss major(dstat.st_dev) == major(ostat.st_dev) &&
350 1.11 augustss minor(dstat.st_dev) == minor(ostat.st_dev))
351 1.11 augustss /* We can't write to stdout so use stderr */
352 1.11 augustss out = stderr;
353 1.11 augustss
354 1.11 augustss if (!wflag)
355 1.2 augustss getinfo(fd);
356 1.11 augustss
357 1.11 augustss if (argc == 0 && aflag && !wflag) {
358 1.1 augustss for(i = 0; fields[i].name; i++) {
359 1.11 augustss if (!(fields[i].flags & ALIAS)) {
360 1.11 augustss prfield(&fields[i], sep);
361 1.11 augustss fprintf(out, "\n");
362 1.11 augustss }
363 1.1 augustss }
364 1.11 augustss } else if (argc > 0 && !aflag) {
365 1.11 augustss struct field *p;
366 1.11 augustss if (wflag) {
367 1.11 augustss AUDIO_INITINFO(&info);
368 1.11 augustss while(argc--) {
369 1.11 augustss char *q;
370 1.11 augustss
371 1.11 augustss q = strchr(*argv, '=');
372 1.11 augustss if (q) {
373 1.11 augustss *q++ = 0;
374 1.11 augustss p = findfield(*argv);
375 1.11 augustss if (p == 0)
376 1.11 augustss warnx("field `%s' does not exist", *argv);
377 1.11 augustss else {
378 1.11 augustss if (p->flags & READONLY)
379 1.11 augustss warnx("`%s' is read only", *argv);
380 1.11 augustss else {
381 1.11 augustss rdfield(p, q);
382 1.11 augustss if (p->valp == &fullduplex)
383 1.11 augustss if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
384 1.11 augustss err(1, "set failed");
385 1.11 augustss }
386 1.11 augustss }
387 1.11 augustss } else
388 1.11 augustss warnx("No `=' in %s", *argv);
389 1.11 augustss argv++;
390 1.11 augustss }
391 1.11 augustss if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
392 1.11 augustss err(1, "set failed");
393 1.11 augustss if (sep) {
394 1.11 augustss getinfo(fd);
395 1.11 augustss for(i = 0; fields[i].name; i++) {
396 1.11 augustss if (fields[i].flags & SET) {
397 1.11 augustss fprintf(out, "%s: -> ", fields[i].name);
398 1.11 augustss prfield(&fields[i], 0);
399 1.11 augustss fprintf(out, "\n");
400 1.11 augustss }
401 1.11 augustss }
402 1.11 augustss }
403 1.1 augustss } else {
404 1.11 augustss while(argc--) {
405 1.11 augustss p = findfield(*argv);
406 1.11 augustss if (p == 0) {
407 1.11 augustss if (strchr(*argv, '='))
408 1.11 augustss warnx("field %s does not exist (use -w to set a variable)", *argv);
409 1.11 augustss else
410 1.11 augustss warnx("field %s does not exist", *argv);
411 1.11 augustss } else {
412 1.11 augustss prfield(p, sep);
413 1.11 augustss fprintf(out, "\n");
414 1.11 augustss }
415 1.11 augustss argv++;
416 1.11 augustss }
417 1.1 augustss }
418 1.11 augustss } else
419 1.11 augustss usage();
420 1.11 augustss exit(0);
421 1.1 augustss }
422