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