-
Notifications
You must be signed in to change notification settings - Fork 0
/
anal-alsa-printStatus.c
290 lines (245 loc) · 6.19 KB
/
anal-alsa-printStatus.c
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
#include <alsa/asoundlib.h>
#include <ctype.h>
snd_ctl_t *_ctl;
char *_name = "hw:0";
char *_mix_name = "Master";
int setup()
{
snd_ctl_t *ctl;
int err;
err = snd_ctl_open(&ctl, _name, SND_CTL_READONLY);
if (err < 0) {
fprintf(stderr, "Unable to open ctl %s\n", _name);
exit(1);
}
err = snd_ctl_subscribe_events(ctl, 1);
if (err < 0) {
fprintf(stderr, "Unable to subscribe to ctl %s\n", _name);
exit(1);
}
_ctl = ctl;
return 1;
}
int read_alsa_event()
{
snd_ctl_event_t *event;
snd_ctl_event_alloca(&event);
if (snd_ctl_read(_ctl, event) < 0) {
return 0;
}
if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM) {
return 0;
}
unsigned int mask = snd_ctl_event_elem_get_mask(event);
if (mask & SND_CTL_EVENT_MASK_VALUE) {
return 1;
}
return 0;
}
double read_alsa_volume()
{
snd_mixer_t* handle;
snd_mixer_elem_t* elem;
snd_mixer_selem_id_t* sid;
long pmin, pmax;
long get_vol, set_vol;
snd_mixer_selem_id_alloca(&sid);
//sets simple-mixer index and name
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, _mix_name);
if ((snd_mixer_open(&handle, 0)) < 0) {
return 0.0;
}
if ((snd_mixer_attach(handle, _name)) < 0) {
snd_mixer_close(handle);
return 0.0;
}
if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
snd_mixer_close(handle);
return 0.0;
}
int ret = snd_mixer_load(handle);
if (ret < 0) {
snd_mixer_close(handle);
return 0.0;
}
elem = snd_mixer_find_selem(handle, sid);
if (!elem) {
snd_mixer_close(handle);
return 0.0;
}
long minv=0, maxv=0;
snd_mixer_selem_get_playback_volume_range (elem, &minv, &maxv);
long outvol = 0;
if(snd_mixer_selem_get_playback_volume(elem, 0, &outvol) < 0) {
snd_mixer_close(handle);
return 0;
// return -6;
}
snd_mixer_close(handle);
/* make the value bound to 100 */
outvol -= minv;
maxv -= minv;
minv = 0;
double result = (double)outvol;
result /= (double)maxv;
return result;
}
int read_alsa_playback_switch()
{
snd_mixer_t* handle;
snd_mixer_elem_t* elem;
snd_mixer_selem_id_t* sid;
long pmin, pmax;
long get_vol, set_vol;
if ((snd_mixer_open(&handle, 0)) < 0) {
return 0;
}
if ((snd_mixer_attach(handle, _name)) < 0) {
snd_mixer_close(handle);
return 0;
}
if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
snd_mixer_close(handle);
return 0;
}
int ret = snd_mixer_load(handle);
if (ret < 0) {
snd_mixer_close(handle);
return 0;
}
snd_mixer_selem_id_alloca(&sid);
//sets simple-mixer index and name
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, _mix_name);
elem = snd_mixer_find_selem(handle, sid);
if (!elem) {
snd_mixer_close(handle);
return 0;
}
if (snd_mixer_selem_has_playback_switch(elem)) {
int playback = 0;
if(snd_mixer_selem_get_playback_switch(elem, 0, &playback) == 0) {
snd_mixer_close(handle);
return playback;
}
}
snd_mixer_close(handle);
return 0;
}
void print_status()
{
int playback_switch = read_alsa_playback_switch();
double vol = read_alsa_volume();
printf("playbackSwitch:%d volume:%f\n", (playback_switch) ? 1 : 0, vol);
fflush(stdout);
}
void get_first_element()
{
snd_mixer_t* handle;
if ((snd_mixer_open(&handle, 0)) < 0) {
return;
}
if ((snd_mixer_attach(handle, _name)) < 0) {
goto cleanup;
}
if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
goto cleanup;
}
int ret = snd_mixer_load(handle);
if (ret < 0) {
goto cleanup;
}
for (snd_mixer_elem_t *elem = snd_mixer_first_elem(handle); elem; elem = snd_mixer_elem_next(elem)) {
if (snd_mixer_elem_get_type(elem) != SND_MIXER_ELEM_SIMPLE) {
continue;
}
int index = snd_mixer_selem_get_index(elem);
char *elem_name = (char *) snd_mixer_selem_get_name(elem);
static char namebuf[256];
if (elem_name) {
char *p = elem_name;
char *q = namebuf;
char *end = &namebuf[255];
for (;;) {
if (!*p) {
break;
}
if (q == end) {
break;
}
if (isalnum(*p)) {
*q = *p;
q++;
}
p++;
}
*q = 0;
} else {
strcpy(namebuf, "none");
}
if (snd_mixer_selem_has_playback_volume(elem)) {
_mix_name = namebuf;
break;
}
}
cleanup:
snd_mixer_close(handle);
}
void read_asoundrc()
{
char *home = getenv("HOME");
if (!home) {
fprintf(stderr, "Unable to get HOME environment variable\n");
return;
}
char pathbuf[1024];
sprintf(pathbuf, "%.256s/.asoundrc", home);
FILE *fp = fopen(pathbuf, "r");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", pathbuf);
return;
}
char buf[1024];
if (fgets(buf, 1024, fp)) {
if (!strncmp(buf, "#ANaL hw:", 9)) {
char *p = &buf[9];
if (isdigit(*p)) {
unsigned long val = strtoul(p, 0, 10);
static char namebuf[64];
sprintf(namebuf, "hw:%lu", val);
_name = namebuf;
}
}
}
fclose(fp);
}
/*
Usage: anal-alsa-printStatus [card name] [mixer name]
'card name' is 'hw:0' by default
'mixer name' is 'Master' by default
*/
void main(int argc, char **argv)
{
if (argc >= 2) {
_name = argv[1];
} else {
read_asoundrc();
}
if (argc >= 3) {
_mix_name = argv[2];
} else {
get_first_element();
}
if (!setup()) {
exit(1);
}
for(;;) {
print_status();
int result = read_alsa_event();
if (!result) {
fprintf(stderr, "Unable to read alsa event\n");
}
}
exit(0);
}