ctl.c revision 1.9 1 1.9 augustss /* $NetBSD: ctl.c,v 1.9 1997/10/07 13:55:03 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.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29 1.1 augustss * 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.2 augustss { "name", &adev.name, STRING, READONLY },
85 1.2 augustss { "version", &adev.version, STRING, READONLY },
86 1.2 augustss { "config", &adev.config, STRING, READONLY },
87 1.2 augustss { "encodings", encbuf, STRING, READONLY },
88 1.6 augustss { "properties", &properties, PROPS, READONLY },
89 1.1 augustss { "full_duplex", &fullduplex, INT, 0 },
90 1.6 augustss { "buffersize", &info.buffersize, UINT, 0 },
91 1.1 augustss { "blocksize", &info.blocksize, UINT, 0 },
92 1.1 augustss { "hiwat", &info.hiwat, UINT, 0 },
93 1.1 augustss { "lowat", &info.lowat, UINT, 0 },
94 1.1 augustss { "backlog", &info.backlog, UINT, 0 },
95 1.2 augustss { "mode", &info.mode, P_R, READONLY },
96 1.1 augustss { "play.rate", &info.play.sample_rate, UINT, 0 },
97 1.2 augustss { "play.sample_rate", &info.play.sample_rate, UINT, ALIAS },
98 1.1 augustss { "play.channels", &info.play.channels, UINT, 0 },
99 1.1 augustss { "play.precision", &info.play.precision, UINT, 0 },
100 1.1 augustss { "play.encoding", &info.play.encoding, ENC, 0 },
101 1.1 augustss { "play.gain", &info.play.gain, UINT, 0 },
102 1.1 augustss { "play.port", &info.play.port, UINT, 0 },
103 1.2 augustss { "play.seek", &info.play.seek, ULONG, READONLY },
104 1.2 augustss { "play.samples", &info.play.samples, UINT, READONLY },
105 1.2 augustss { "play.eof", &info.play.eof, UINT, READONLY },
106 1.1 augustss { "play.pause", &info.play.pause, UCHAR, 0 },
107 1.2 augustss { "play.error", &info.play.error, UCHAR, READONLY },
108 1.2 augustss { "play.waiting", &info.play.waiting, UCHAR, READONLY },
109 1.2 augustss { "play.open", &info.play.open, UCHAR, READONLY },
110 1.2 augustss { "play.active", &info.play.active, UCHAR, READONLY },
111 1.1 augustss { "record.rate", &info.record.sample_rate,UINT, 0 },
112 1.2 augustss { "record.sample_rate", &info.record.sample_rate,UINT, ALIAS },
113 1.1 augustss { "record.channels", &info.record.channels, UINT, 0 },
114 1.1 augustss { "record.precision", &info.record.precision, UINT, 0 },
115 1.1 augustss { "record.encoding", &info.record.encoding, ENC, 0 },
116 1.1 augustss { "record.gain", &info.record.gain, UINT, 0 },
117 1.1 augustss { "record.port", &info.record.port, UINT, 0 },
118 1.2 augustss { "record.seek", &info.record.seek, ULONG, READONLY },
119 1.2 augustss { "record.samples", &info.record.samples, UINT, READONLY },
120 1.2 augustss { "record.eof", &info.record.eof, UINT, READONLY },
121 1.1 augustss { "record.pause", &info.record.pause, UCHAR, 0 },
122 1.2 augustss { "record.error", &info.record.error, UCHAR, READONLY },
123 1.2 augustss { "record.waiting", &info.record.waiting, UCHAR, READONLY },
124 1.2 augustss { "record.open", &info.record.open, UCHAR, READONLY },
125 1.2 augustss { "record.active", &info.record.active, UCHAR, READONLY },
126 1.2 augustss { "record.errors", &rerror, INT, READONLY },
127 1.1 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.5 augustss { "ulaw", AUDIO_ENCODING_ULAW },
135 1.5 augustss { "mulaw", AUDIO_ENCODING_ULAW },
136 1.5 augustss { "alaw", AUDIO_ENCODING_ALAW },
137 1.5 augustss { "slinear", AUDIO_ENCODING_SLINEAR },
138 1.5 augustss { "linear", AUDIO_ENCODING_SLINEAR },
139 1.5 augustss { "ulinear", AUDIO_ENCODING_ULINEAR },
140 1.5 augustss { "adpcm", AUDIO_ENCODING_ADPCM },
141 1.5 augustss { "ADPCM", AUDIO_ENCODING_ADPCM },
142 1.5 augustss { "slinear_le", AUDIO_ENCODING_SLINEAR_LE },
143 1.5 augustss { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
144 1.5 augustss { "ulinear_le", AUDIO_ENCODING_ULINEAR_LE },
145 1.5 augustss { "slinear_be", AUDIO_ENCODING_SLINEAR_BE },
146 1.5 augustss { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
147 1.5 augustss { "ulinear_be", AUDIO_ENCODING_ULINEAR_BE },
148 1.2 augustss { 0 }
149 1.1 augustss };
150 1.1 augustss
151 1.1 augustss struct field *
152 1.9 augustss findfield(name)
153 1.9 augustss char *name;
154 1.1 augustss {
155 1.2 augustss int i;
156 1.2 augustss for(i = 0; fields[i].name; i++)
157 1.2 augustss if (strcmp(fields[i].name, name) == 0)
158 1.2 augustss return &fields[i];
159 1.2 augustss return 0;
160 1.1 augustss }
161 1.1 augustss
162 1.1 augustss void
163 1.9 augustss prfield(p, sep)
164 1.9 augustss struct field *p;
165 1.9 augustss char *sep;
166 1.1 augustss {
167 1.2 augustss u_int v;
168 1.2 augustss char *cm;
169 1.2 augustss int i;
170 1.2 augustss
171 1.2 augustss if (sep)
172 1.2 augustss fprintf(out, "%s%s", p->name, sep);
173 1.2 augustss switch(p->format) {
174 1.2 augustss case STRING:
175 1.2 augustss fprintf(out, "%s", (char*)p->valp);
176 1.2 augustss break;
177 1.2 augustss case INT:
178 1.2 augustss fprintf(out, "%d", *(int*)p->valp);
179 1.2 augustss break;
180 1.2 augustss case UINT:
181 1.2 augustss fprintf(out, "%u", *(u_int*)p->valp);
182 1.2 augustss break;
183 1.2 augustss case UCHAR:
184 1.2 augustss fprintf(out, "%u", *(u_char*)p->valp);
185 1.2 augustss break;
186 1.2 augustss case ULONG:
187 1.2 augustss fprintf(out, "%lu", *(u_long*)p->valp);
188 1.2 augustss break;
189 1.2 augustss case P_R:
190 1.2 augustss v = *(u_int*)p->valp;
191 1.2 augustss cm = "";
192 1.2 augustss if (v & AUMODE_PLAY) {
193 1.2 augustss if (v & AUMODE_PLAY_ALL)
194 1.2 augustss fprintf(out, "play");
195 1.2 augustss else
196 1.2 augustss fprintf(out, "playsync");
197 1.2 augustss cm = ",";
198 1.2 augustss }
199 1.2 augustss if (v & AUMODE_RECORD)
200 1.2 augustss fprintf(out, "%srecord", cm);
201 1.2 augustss break;
202 1.2 augustss case ENC:
203 1.2 augustss v = *(u_int*)p->valp;
204 1.2 augustss for(i = 0; encs[i].ename; i++)
205 1.2 augustss if (encs[i].eno == v)
206 1.1 augustss break;
207 1.2 augustss if (encs[i].ename)
208 1.2 augustss fprintf(out, "%s", encs[i].ename);
209 1.2 augustss else
210 1.2 augustss fprintf(out, "%u", v);
211 1.2 augustss break;
212 1.6 augustss case PROPS:
213 1.6 augustss v = *(u_int*)p->valp;
214 1.6 augustss cm = "";
215 1.6 augustss if (v & AUDIO_PROP_FULLDUPLEX) {
216 1.6 augustss fprintf(out, "%sfull_duplex", cm);
217 1.6 augustss cm = ",";
218 1.6 augustss }
219 1.6 augustss if (v & AUDIO_PROP_MMAP) {
220 1.6 augustss fprintf(out, "%smmap", cm);
221 1.6 augustss cm = ",";
222 1.6 augustss }
223 1.6 augustss break;
224 1.2 augustss default:
225 1.2 augustss errx(1, "Invalid format.");
226 1.2 augustss }
227 1.2 augustss }
228 1.2 augustss
229 1.2 augustss void
230 1.9 augustss rdfield(p, q)
231 1.9 augustss struct field *p;
232 1.9 augustss char *q;
233 1.2 augustss {
234 1.2 augustss int i;
235 1.2 augustss
236 1.2 augustss switch(p->format) {
237 1.2 augustss case UINT:
238 1.2 augustss if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
239 1.2 augustss warnx("Bad number %s", q);
240 1.2 augustss break;
241 1.2 augustss case ENC:
242 1.2 augustss for(i = 0; encs[i].ename; i++)
243 1.2 augustss if (strcmp(encs[i].ename, q) == 0)
244 1.1 augustss break;
245 1.2 augustss if (encs[i].ename)
246 1.2 augustss *(u_int*)p->valp = encs[i].eno;
247 1.2 augustss else
248 1.2 augustss warnx("Unknown encoding: %s", q);
249 1.2 augustss break;
250 1.2 augustss default:
251 1.2 augustss errx(1, "Invalid format.");
252 1.2 augustss }
253 1.2 augustss p->flags |= SET;
254 1.1 augustss }
255 1.1 augustss
256 1.1 augustss void
257 1.9 augustss getinfo(fd)
258 1.9 augustss int fd;
259 1.1 augustss {
260 1.2 augustss int pos, i;
261 1.1 augustss
262 1.2 augustss if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
263 1.9 augustss err(1, "AUDIO_GETDEV");
264 1.2 augustss for(pos = 0, i = 0; ; i++) {
265 1.2 augustss audio_encoding_t enc;
266 1.2 augustss enc.index = i;
267 1.2 augustss if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
268 1.2 augustss break;
269 1.2 augustss if (pos)
270 1.2 augustss encbuf[pos++] = ',';
271 1.2 augustss sprintf(encbuf+pos, "%s:%d%s", enc.name,
272 1.2 augustss enc.precision,
273 1.2 augustss enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
274 1.2 augustss pos += strlen(encbuf+pos);
275 1.2 augustss }
276 1.2 augustss if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
277 1.9 augustss err(1, "AUDIO_GETFD");
278 1.6 augustss if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
279 1.9 augustss err(1, "AUDIO_GETPROPS");
280 1.2 augustss if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
281 1.9 augustss err(1, "AUDIO_RERROR");
282 1.2 augustss if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
283 1.9 augustss err(1, "AUDIO_GETINFO");
284 1.2 augustss }
285 1.2 augustss
286 1.2 augustss void
287 1.9 augustss usage()
288 1.2 augustss {
289 1.2 augustss fprintf(out, "%s [-f file] [-n] name ...\n", prog);
290 1.2 augustss fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
291 1.2 augustss fprintf(out, "%s [-f file] [-n] -a\n", prog);
292 1.2 augustss exit(1);
293 1.1 augustss }
294 1.1 augustss
295 1.9 augustss int
296 1.9 augustss main(argc, argv)
297 1.9 augustss int argc;
298 1.9 augustss char **argv;
299 1.1 augustss {
300 1.2 augustss int fd, i, ch;
301 1.2 augustss int aflag = 0, wflag = 0;
302 1.2 augustss struct stat dstat, ostat;
303 1.7 augustss char *file = "/dev/audioctl";
304 1.2 augustss char *sep = "=";
305 1.2 augustss
306 1.2 augustss prog = *argv;
307 1.2 augustss
308 1.2 augustss while ((ch = getopt(argc, argv, "af:nw")) != -1) {
309 1.2 augustss switch(ch) {
310 1.2 augustss case 'a':
311 1.2 augustss aflag++;
312 1.2 augustss break;
313 1.2 augustss case 'w':
314 1.2 augustss wflag++;
315 1.2 augustss break;
316 1.2 augustss case 'n':
317 1.2 augustss sep = 0;
318 1.2 augustss break;
319 1.2 augustss case 'f':
320 1.2 augustss file = optarg;
321 1.2 augustss break;
322 1.2 augustss case '?':
323 1.2 augustss default:
324 1.2 augustss usage();
325 1.1 augustss }
326 1.2 augustss }
327 1.2 augustss argc -= optind;
328 1.2 augustss argv += optind;
329 1.1 augustss
330 1.8 augustss fd = open(file, O_WRONLY);
331 1.2 augustss if (fd < 0)
332 1.2 augustss fd = open(file, O_RDONLY);
333 1.2 augustss if (fd < 0)
334 1.2 augustss err(1, "%s", file);
335 1.2 augustss
336 1.7 augustss /* Check if stdout is the same device as the audio device. */
337 1.2 augustss if (fstat(fd, &dstat) < 0)
338 1.9 augustss err(1, "fstat au");
339 1.2 augustss if (fstat(STDOUT_FILENO, &ostat) < 0)
340 1.9 augustss err(1, "fstat stdout");
341 1.2 augustss if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
342 1.2 augustss major(dstat.st_dev) == major(ostat.st_dev) &&
343 1.2 augustss minor(dstat.st_dev) == minor(ostat.st_dev))
344 1.2 augustss /* We can't write to stdout so use stderr */
345 1.2 augustss out = stderr;
346 1.2 augustss
347 1.2 augustss if (!wflag)
348 1.2 augustss getinfo(fd);
349 1.2 augustss
350 1.2 augustss if (argc == 0 && aflag && !wflag) {
351 1.2 augustss for(i = 0; fields[i].name; i++) {
352 1.2 augustss if (!(fields[i].flags & ALIAS)) {
353 1.2 augustss prfield(&fields[i], sep);
354 1.2 augustss fprintf(out, "\n");
355 1.2 augustss }
356 1.1 augustss }
357 1.2 augustss } else if (argc > 0 && !aflag) {
358 1.2 augustss struct field *p;
359 1.2 augustss if (wflag) {
360 1.2 augustss AUDIO_INITINFO(&info);
361 1.2 augustss while(argc--) {
362 1.2 augustss char *q;
363 1.2 augustss
364 1.2 augustss q = strchr(*argv, '=');
365 1.2 augustss if (q) {
366 1.2 augustss *q++ = 0;
367 1.2 augustss p = findfield(*argv);
368 1.2 augustss if (p == 0)
369 1.6 augustss warnx("field `%s' does not exist", *argv);
370 1.2 augustss else {
371 1.2 augustss if (p->flags & READONLY)
372 1.6 augustss warnx("`%s' is read only", *argv);
373 1.2 augustss else {
374 1.2 augustss rdfield(p, q);
375 1.2 augustss if (p->valp == &fullduplex)
376 1.2 augustss if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
377 1.2 augustss err(1, "set failed");
378 1.2 augustss }
379 1.2 augustss }
380 1.2 augustss } else
381 1.2 augustss warnx("No `=' in %s", *argv);
382 1.2 augustss argv++;
383 1.2 augustss }
384 1.2 augustss if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
385 1.2 augustss err(1, "set failed");
386 1.2 augustss if (sep) {
387 1.2 augustss getinfo(fd);
388 1.1 augustss for(i = 0; fields[i].name; i++) {
389 1.2 augustss if (fields[i].flags & SET) {
390 1.3 augustss fprintf(out, "%s: -> ", fields[i].name);
391 1.3 augustss prfield(&fields[i], 0);
392 1.1 augustss fprintf(out, "\n");
393 1.2 augustss }
394 1.1 augustss }
395 1.2 augustss }
396 1.2 augustss } else {
397 1.2 augustss while(argc--) {
398 1.2 augustss p = findfield(*argv);
399 1.2 augustss if (p == 0) {
400 1.2 augustss if (strchr(*argv, '='))
401 1.2 augustss warnx("field %s does not exist (use -w to set a variable)", *argv);
402 1.2 augustss else
403 1.2 augustss warnx("field %s does not exist", *argv);
404 1.1 augustss } else {
405 1.2 augustss prfield(p, sep);
406 1.2 augustss fprintf(out, "\n");
407 1.1 augustss }
408 1.2 augustss argv++;
409 1.2 augustss }
410 1.2 augustss }
411 1.2 augustss } else
412 1.2 augustss usage();
413 1.2 augustss exit(0);
414 1.1 augustss }
415