ctl.c revision 1.5 1 /* $NetBSD: ctl.c,v 1.5 1997/07/16 06:55:27 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Lennart Augustsson
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
38 #include <stdio.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/stat.h>
45 #include <sys/ioctl.h>
46 #include <sys/audioio.h>
47
48 FILE *out = stdout;
49
50 char *prog;
51
52 audio_device_t adev;
53
54 audio_info_t info;
55
56 char encbuf[1000];
57
58 int fullduplex, rerror;
59
60 struct field {
61 char *name;
62 void *valp;
63 int format;
64 #define STRING 1
65 #define INT 2
66 #define UINT 3
67 #define P_R 4
68 #define ULONG 5
69 #define UCHAR 6
70 #define ENC 7
71 char flags;
72 #define READONLY 1
73 #define ALIAS 2
74 #define SET 4
75 } fields[] = {
76 { "name", &adev.name, STRING, READONLY },
77 { "version", &adev.version, STRING, READONLY },
78 { "config", &adev.config, STRING, READONLY },
79 { "encodings", encbuf, STRING, READONLY },
80 { "full_duplex", &fullduplex, INT, 0 },
81 { "blocksize", &info.blocksize, UINT, 0 },
82 { "hiwat", &info.hiwat, UINT, 0 },
83 { "lowat", &info.lowat, UINT, 0 },
84 { "backlog", &info.backlog, UINT, 0 },
85 { "mode", &info.mode, P_R, READONLY },
86 { "play.rate", &info.play.sample_rate, UINT, 0 },
87 { "play.sample_rate", &info.play.sample_rate, UINT, ALIAS },
88 { "play.channels", &info.play.channels, UINT, 0 },
89 { "play.precision", &info.play.precision, UINT, 0 },
90 { "play.encoding", &info.play.encoding, ENC, 0 },
91 { "play.gain", &info.play.gain, UINT, 0 },
92 { "play.port", &info.play.port, UINT, 0 },
93 { "play.seek", &info.play.seek, ULONG, READONLY },
94 { "play.samples", &info.play.samples, UINT, READONLY },
95 { "play.eof", &info.play.eof, UINT, READONLY },
96 { "play.pause", &info.play.pause, UCHAR, 0 },
97 { "play.error", &info.play.error, UCHAR, READONLY },
98 { "play.waiting", &info.play.waiting, UCHAR, READONLY },
99 { "play.open", &info.play.open, UCHAR, READONLY },
100 { "play.active", &info.play.active, UCHAR, READONLY },
101 { "record.rate", &info.record.sample_rate,UINT, 0 },
102 { "record.sample_rate", &info.record.sample_rate,UINT, ALIAS },
103 { "record.channels", &info.record.channels, UINT, 0 },
104 { "record.precision", &info.record.precision, UINT, 0 },
105 { "record.encoding", &info.record.encoding, ENC, 0 },
106 { "record.gain", &info.record.gain, UINT, 0 },
107 { "record.port", &info.record.port, UINT, 0 },
108 { "record.seek", &info.record.seek, ULONG, READONLY },
109 { "record.samples", &info.record.samples, UINT, READONLY },
110 { "record.eof", &info.record.eof, UINT, READONLY },
111 { "record.pause", &info.record.pause, UCHAR, 0 },
112 { "record.error", &info.record.error, UCHAR, READONLY },
113 { "record.waiting", &info.record.waiting, UCHAR, READONLY },
114 { "record.open", &info.record.open, UCHAR, READONLY },
115 { "record.active", &info.record.active, UCHAR, READONLY },
116 { "record.errors", &rerror, INT, READONLY },
117 { 0 }
118 };
119
120 struct {
121 char *ename;
122 int eno;
123 } encs[] = {
124 { "ulaw", AUDIO_ENCODING_ULAW },
125 { "mulaw", AUDIO_ENCODING_ULAW },
126 { "alaw", AUDIO_ENCODING_ALAW },
127 { "slinear", AUDIO_ENCODING_SLINEAR },
128 { "linear", AUDIO_ENCODING_SLINEAR },
129 { "ulinear", AUDIO_ENCODING_ULINEAR },
130 { "adpcm", AUDIO_ENCODING_ADPCM },
131 { "ADPCM", AUDIO_ENCODING_ADPCM },
132 { "slinear_le", AUDIO_ENCODING_SLINEAR_LE },
133 { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
134 { "ulinear_le", AUDIO_ENCODING_ULINEAR_LE },
135 { "slinear_be", AUDIO_ENCODING_SLINEAR_BE },
136 { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
137 { "ulinear_be", AUDIO_ENCODING_ULINEAR_BE },
138 { 0 }
139 };
140
141 struct field *
142 findfield(char *name)
143 {
144 int i;
145 for(i = 0; fields[i].name; i++)
146 if (strcmp(fields[i].name, name) == 0)
147 return &fields[i];
148 return 0;
149 }
150
151 void
152 prfield(struct field *p, char *sep)
153 {
154 u_int v;
155 char *cm;
156 int i;
157
158 if (sep)
159 fprintf(out, "%s%s", p->name, sep);
160 switch(p->format) {
161 case STRING:
162 fprintf(out, "%s", (char*)p->valp);
163 break;
164 case INT:
165 fprintf(out, "%d", *(int*)p->valp);
166 break;
167 case UINT:
168 fprintf(out, "%u", *(u_int*)p->valp);
169 break;
170 case UCHAR:
171 fprintf(out, "%u", *(u_char*)p->valp);
172 break;
173 case ULONG:
174 fprintf(out, "%lu", *(u_long*)p->valp);
175 break;
176 case P_R:
177 v = *(u_int*)p->valp;
178 cm = "";
179 if (v & AUMODE_PLAY) {
180 if (v & AUMODE_PLAY_ALL)
181 fprintf(out, "play");
182 else
183 fprintf(out, "playsync");
184 cm = ",";
185 }
186 if (v & AUMODE_RECORD)
187 fprintf(out, "%srecord", cm);
188 break;
189 case ENC:
190 v = *(u_int*)p->valp;
191 for(i = 0; encs[i].ename; i++)
192 if (encs[i].eno == v)
193 break;
194 if (encs[i].ename)
195 fprintf(out, "%s", encs[i].ename);
196 else
197 fprintf(out, "%u", v);
198 break;
199 default:
200 errx(1, "Invalid format.");
201 }
202 }
203
204 void
205 rdfield(struct field *p, char *q)
206 {
207 int i;
208
209 switch(p->format) {
210 case UINT:
211 if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
212 warnx("Bad number %s", q);
213 break;
214 case ENC:
215 for(i = 0; encs[i].ename; i++)
216 if (strcmp(encs[i].ename, q) == 0)
217 break;
218 if (encs[i].ename)
219 *(u_int*)p->valp = encs[i].eno;
220 else
221 warnx("Unknown encoding: %s", q);
222 break;
223 default:
224 errx(1, "Invalid format.");
225 }
226 p->flags |= SET;
227 }
228
229 void
230 getinfo(int fd)
231 {
232 int pos, i;
233
234 if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
235 err(1, NULL);
236 for(pos = 0, i = 0; ; i++) {
237 audio_encoding_t enc;
238 enc.index = i;
239 if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
240 break;
241 if (pos)
242 encbuf[pos++] = ',';
243 sprintf(encbuf+pos, "%s:%d%s", enc.name,
244 enc.precision,
245 enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
246 pos += strlen(encbuf+pos);
247 }
248 if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
249 err(1, NULL);
250 if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
251 err(1, NULL);
252 if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
253 err(1, NULL);
254 }
255
256 void
257 usage(void)
258 {
259 fprintf(out, "%s [-f file] [-n] name ...\n", prog);
260 fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
261 fprintf(out, "%s [-f file] [-n] -a\n", prog);
262 exit(1);
263 }
264
265 void
266 main(int argc, char **argv)
267 {
268 int fd, i, ch;
269 int aflag = 0, wflag = 0;
270 struct stat dstat, ostat;
271 char *file = "/dev/sound";
272 char *sep = "=";
273
274 prog = *argv;
275
276 while ((ch = getopt(argc, argv, "af:nw")) != -1) {
277 switch(ch) {
278 case 'a':
279 aflag++;
280 break;
281 case 'w':
282 wflag++;
283 break;
284 case 'n':
285 sep = 0;
286 break;
287 case 'f':
288 file = optarg;
289 break;
290 case '?':
291 default:
292 usage();
293 }
294 }
295 argc -= optind;
296 argv += optind;
297
298 fd = open(file, O_RDWR);
299 if (fd < 0)
300 fd = open(file, O_WRONLY);
301 if (fd < 0)
302 fd = open(file, O_RDONLY);
303 if (fd < 0)
304 err(1, "%s", file);
305
306 /* Check is stdout is the same device as the audio device. */
307 if (fstat(fd, &dstat) < 0)
308 err(1, NULL);
309 if (fstat(STDOUT_FILENO, &ostat) < 0)
310 err(1, NULL);
311 if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
312 major(dstat.st_dev) == major(ostat.st_dev) &&
313 minor(dstat.st_dev) == minor(ostat.st_dev))
314 /* We can't write to stdout so use stderr */
315 out = stderr;
316
317 if (!wflag)
318 getinfo(fd);
319
320 if (argc == 0 && aflag && !wflag) {
321 for(i = 0; fields[i].name; i++) {
322 if (!(fields[i].flags & ALIAS)) {
323 prfield(&fields[i], sep);
324 fprintf(out, "\n");
325 }
326 }
327 } else if (argc > 0 && !aflag) {
328 struct field *p;
329 if (wflag) {
330 AUDIO_INITINFO(&info);
331 while(argc--) {
332 char *q;
333
334 q = strchr(*argv, '=');
335 if (q) {
336 *q++ = 0;
337 p = findfield(*argv);
338 if (p == 0)
339 warnx("field %s does not exist", *argv);
340 else {
341 if (p->flags & READONLY)
342 warnx("%s is read only", *argv);
343 else {
344 rdfield(p, q);
345 if (p->valp == &fullduplex)
346 if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
347 err(1, "set failed");
348 }
349 }
350 } else
351 warnx("No `=' in %s", *argv);
352 argv++;
353 }
354 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
355 err(1, "set failed");
356 if (sep) {
357 getinfo(fd);
358 for(i = 0; fields[i].name; i++) {
359 if (fields[i].flags & SET) {
360 fprintf(out, "%s: -> ", fields[i].name);
361 prfield(&fields[i], 0);
362 fprintf(out, "\n");
363 }
364 }
365 }
366 } else {
367 while(argc--) {
368 p = findfield(*argv);
369 if (p == 0) {
370 if (strchr(*argv, '='))
371 warnx("field %s does not exist (use -w to set a variable)", *argv);
372 else
373 warnx("field %s does not exist", *argv);
374 } else {
375 prfield(p, sep);
376 fprintf(out, "\n");
377 }
378 argv++;
379 }
380 }
381 } else
382 usage();
383 exit(0);
384 }
385