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