-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hotdog-printALSAStatus.c
196 lines (160 loc) · 3.97 KB
/
hotdog-printALSAStatus.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
#include <alsa/asoundlib.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);
}
/*
Usage: hotdog-printALSAStatus [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];
}
if (argc >= 3) {
_mix_name = argv[2];
}
if (!setup()) {
exit(1);
}
for(;;) {
print_status();
int result = read_alsa_event();
if (!result) {
fprintf(stderr, "Unable to read alsa event\n");
}
}
exit(0);
}