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
|
/* $NetBSD: parse.c,v 1.3 2021/05/08 13:03:40 nia Exp $ */
/*-
* Copyright (c) 2021 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Nia Alarie.
*
* 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.
*
* 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 <sys/audioio.h>
#include <sys/ioctl.h>
#include <curses.h>
#include <stdlib.h>
#include "app.h"
#include "parse.h"
static struct aiomixer_class *get_class(struct aiomixer *, int);
static int compare_control(const void *, const void *);
static int compare_class(const void *, const void *);
static struct aiomixer_class *
get_class(struct aiomixer *aio, int class)
{
size_t i;
for (i = 0; i < aio->numclasses; ++i) {
if (aio->classes[i].index == class) {
return &aio->classes[i];
}
}
return NULL;
}
static int
compare_control(const void *pa, const void *pb)
{
const struct aiomixer_control *a = (const struct aiomixer_control *)pa;
const struct aiomixer_control *b = (const struct aiomixer_control *)pb;
if (a->info.prev != AUDIO_MIXER_LAST ||
b->info.prev != AUDIO_MIXER_LAST) {
if (b->info.prev == a->info.index)
return -1;
if (a->info.prev == b->info.index)
return 1;
} else {
return strcmp(a->info.label.name, b->info.label.name);
}
return 0;
}
static int
compare_class(const void *pa, const void *pb)
{
const struct aiomixer_class *a = (const struct aiomixer_class *)pa;
const struct aiomixer_class *b = (const struct aiomixer_class *)pb;
if (strcmp(a->name, AudioCoutputs) == 0)
return -1;
if (strcmp(b->name, AudioCoutputs) == 0)
return 1;
return strcmp(a->name, b->name);
}
int
aiomixer_parse(struct aiomixer *aio)
{
size_t i;
struct mixer_devinfo info;
struct aiomixer_class *class;
struct aiomixer_control *control;
for (info.index = 0;
ioctl(aio->fd, AUDIO_MIXER_DEVINFO, &info) != -1; ++info.index) {
if (info.type != AUDIO_MIXER_CLASS)
continue;
if (aio->numclasses >= __arraycount(aio->classes))
break;
class = &aio->classes[aio->numclasses++];
memcpy(class->name, info.label.name, MAX_AUDIO_DEV_LEN);
class->index = info.index;
class->numcontrols = 0;
}
for (info.index = 0;
ioctl(aio->fd, AUDIO_MIXER_DEVINFO, &info) != -1; ++info.index) {
if (info.type == AUDIO_MIXER_CLASS)
continue;
if (info.type == AUDIO_MIXER_VALUE) {
/* XXX workaround for hdaudio(4) bugs */
if (info.un.v.delta > AUDIO_MAX_GAIN)
continue;
if (info.un.v.num_channels == 0)
continue;
}
class = get_class(aio, info.mixer_class);
if (class == NULL)
break;
if (class->numcontrols >= __arraycount(class->controls))
break;
control = &class->controls[class->numcontrols++];
control->info = info;
}
qsort(aio->classes, aio->numclasses,
sizeof(struct aiomixer_class),
compare_class);
for (i = 0; i < aio->numclasses; ++i) {
class = &aio->classes[i];
qsort(class->controls, class->numcontrols,
sizeof(struct aiomixer_control),
compare_control);
}
return 0;
}
|