1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/* $OpenBSD: mixerctl.c,v 1.4 1998/05/02 22:28:07 millert Exp $ */
/* $NetBSD: mixerctl.c,v 1.11 1998/04/27 16:55:23 augustss Exp $ */
/*
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Author: Lennart Augustsson, with some code and ideas from Chuck Cranor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
char *catstr __P((char *p, char *q));
struct field *findfield __P((char *name));
void prfield __P((struct field *p, char *sep, int prvalset));
int rdfield __P((struct field *p, char *q));
int main(int argc, char **argv);
FILE *out = stdout;
char *prog;
struct field {
char *name;
mixer_ctrl_t *valp;
mixer_devinfo_t *infp;
char changed;
} *fields, *rfields;
mixer_ctrl_t *values;
mixer_devinfo_t *infos;
char *
catstr(p, q)
char *p;
char *q;
{
char *r = malloc(strlen(p) + strlen(q) + 2);
strcpy(r, p);
strcat(r, ".");
strcat(r, q);
return r;
}
struct field *
findfield(name)
char *name;
{
int i;
for(i = 0; fields[i].name; i++)
if (strcmp(fields[i].name, name) == 0)
return &fields[i];
return 0;
}
void
prfield(p, sep, prvalset)
struct field *p;
char *sep;
int prvalset;
{
mixer_ctrl_t *m;
int i, n;
if (sep)
fprintf(out, "%s%s", p->name, sep);
m = p->valp;
switch(m->type) {
case AUDIO_MIXER_ENUM:
for(i = 0; i < p->infp->un.e.num_mem; i++)
if (p->infp->un.e.member[i].ord == m->un.ord)
fprintf(out, "%s",
p->infp->un.e.member[i].label.name);
if (prvalset) {
fprintf(out, " [ ");
for(i = 0; i < p->infp->un.e.num_mem; i++)
fprintf(out, "%s ", p->infp->un.e.member[i].label.name);
fprintf(out, "]");
}
break;
case AUDIO_MIXER_SET:
for(n = i = 0; i < p->infp->un.s.num_mem; i++)
if (m->un.mask & p->infp->un.s.member[i].mask)
fprintf(out, "%s%s", n++ ? "," : "",
p->infp->un.s.member[i].label.name);
if (prvalset) {
fprintf(out, " { ");
for(i = 0; i < p->infp->un.s.num_mem; i++)
fprintf(out, "%s ", p->infp->un.s.member[i].label.name);
fprintf(out, "}");
}
break;
case AUDIO_MIXER_VALUE:
if (m->un.value.num_channels == 1)
fprintf(out, "%d", m->un.value.level[0]);
else
fprintf(out, "%d,%d", m->un.value.level[0],
m->un.value.level[1]);
if (prvalset)
fprintf(out, " %s", p->infp->un.v.units.name);
break;
default:
printf("\n");
errx(1, "Invalid format.");
}
}
int
rdfield(p, q)
struct field *p;
char *q;
{
mixer_ctrl_t *m;
int v, v0, v1, mask;
int i;
char *s;
m = p->valp;
switch(m->type) {
case AUDIO_MIXER_ENUM:
for(i = 0; i < p->infp->un.e.num_mem; i++)
if (strcmp(p->infp->un.e.member[i].label.name, q) == 0)
break;
if (i < p->infp->un.e.num_mem)
m->un.ord = p->infp->un.e.member[i].ord;
else {
warnx("Bad enum value %s", q);
return 0;
}
break;
case AUDIO_MIXER_SET:
mask = 0;
for(v = 0; q && *q; q = s) {
s = strchr(q, ',');
if (s)
*s++ = 0;
for (i = 0; i < p->infp->un.s.num_mem; i++)
if (strcmp(p->infp->un.s.member[i].label.name, q) == 0)
break;
if (i < p->infp->un.s.num_mem) {
mask |= p->infp->un.s.member[i].mask;
} else {
warnx("Bad set value %s", q);
return 0;
}
}
m->un.mask = mask;
break;
case AUDIO_MIXER_VALUE:
if (m->un.value.num_channels == 1) {
if (sscanf(q, "%d", &v) == 1) {
switch (*q) {
case '+':
case '-':
m->un.value.level[0] += v;
break;
default:
m->un.value.level[0] = v;
break;
}
} else {
warnx("Bad number %s", q);
return 0;
}
} else {
if (sscanf(q, "%d,%d", &v0, &v1) == 2) {
switch (*q) {
case '+':
case '-':
m->un.value.level[0] += v0;
break;
default:
m->un.value.level[0] = v0;
break;
}
s = strchr(q, ',') + 1;
switch (*s) {
case '+':
case '-':
m->un.value.level[1] += v1;
break;
default:
m->un.value.level[1] = v1;
break;
}
} else if (sscanf(q, "%d", &v) == 1) {
switch (*q) {
case '+':
case '-':
m->un.value.level[0] += v;
m->un.value.level[1] += v;
break;
default:
m->un.value.level[0] =
m->un.value.level[1] = v;
break;
}
} else {
warnx("Bad numbers %s", q);
return 0;
}
}
break;
default:
errx(1, "Invalid format.");
}
p->changed = 1;
return 1;
}
int
main(argc, argv)
int argc;
char **argv;
{
int fd, i, j, ch, pos;
int aflag = 0, wflag = 0, vflag = 0;
char *file;
char *sep = "=";
mixer_devinfo_t dinfo;
mixer_ctrl_t val;
int ndev;
file = getenv("MIXERDEVICE");
if (file == 0)
file = "/dev/mixer";
prog = *argv;
while ((ch = getopt(argc, argv, "af:nvw")) != -1) {
switch(ch) {
case 'a':
aflag++;
break;
case 'w':
wflag++;
break;
case 'v':
vflag++;
break;
case 'n':
sep = 0;
break;
case 'f':
file = optarg;
break;
case '?':
default:
usage:
fprintf(out, "%s [-f file] [-v] [-n] name ...\n", prog);
fprintf(out, "%s [-f file] [-v] [-n] -w name=value ...\n", prog);
fprintf(out, "%s [-f file] [-v] [-n] -a\n", prog);
exit(0);
}
}
argc -= optind;
argv += optind;
fd = open(file, O_RDWR);
if (fd < 0)
err(1, "%s", file);
for(ndev = 0; ; ndev++) {
dinfo.index = ndev;
if (ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo) < 0)
break;
}
rfields = calloc(ndev, sizeof *rfields);
fields = calloc(ndev, sizeof *fields);
infos = calloc(ndev, sizeof *infos);
values = calloc(ndev, sizeof *values);
for(i = 0; i < ndev; i++) {
infos[i].index = i;
ioctl(fd, AUDIO_MIXER_DEVINFO, &infos[i]);
}
for(i = 0; i < ndev; i++) {
rfields[i].name = infos[i].label.name;
rfields[i].valp = &values[i];
rfields[i].infp = &infos[i];
}
for(i = 0; i < ndev; i++) {
values[i].dev = i;
values[i].type = infos[i].type;
if (infos[i].type != AUDIO_MIXER_CLASS) {
values[i].un.value.num_channels = 2;
if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0) {
values[i].un.value.num_channels = 1;
if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0)
err(1, "AUDIO_MIXER_READ");
}
}
}
for(j = i = 0; i < ndev; i++) {
if (infos[i].type != AUDIO_MIXER_CLASS &&
infos[i].type != -1) {
fields[j++] = rfields[i];
for(pos = infos[i].next; pos != AUDIO_MIXER_LAST;
pos = infos[pos].next) {
fields[j] = rfields[pos];
fields[j].name = catstr(rfields[i].name,
infos[pos].label.name);
infos[pos].type = -1;
j++;
}
}
}
for(i = 0; i < j; i++) {
int cls = fields[i].infp->mixer_class;
if (cls >= 0 && cls < ndev)
fields[i].name = catstr(infos[cls].label.name,
fields[i].name);
}
if (argc == 0 && aflag && !wflag) {
for(i = 0; fields[i].name; i++) {
prfield(&fields[i], sep, vflag);
fprintf(out, "\n");
}
} else if (argc > 0 && !aflag) {
struct field *p;
if (wflag) {
while(argc--) {
char *q;
q = strchr(*argv, '=');
if (q) {
*q++ = 0;
p = findfield(*argv);
if (p == 0)
warnx("field %s does not exist", *argv);
else {
val = *p->valp;
if (rdfield(p, q)) {
if (ioctl(fd, AUDIO_MIXER_WRITE, p->valp) < 0)
warn("AUDIO_MIXER_WRITE");
else if (sep) {
*p->valp = val;
prfield(p, ": ", 0);
ioctl(fd, AUDIO_MIXER_READ, p->valp);
printf(" -> ");
prfield(p, 0, 0);
printf("\n");
}
}
}
} else {
warnx("No `=' in %s", *argv);
}
argv++;
}
} else {
while(argc--) {
p = findfield(*argv);
if (p == 0)
warnx("field %s does not exist", *argv);
else
prfield(p, sep, vflag), fprintf(out, "\n");
argv++;
}
}
} else
goto usage;
exit(0);
}
|