-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm.c
70 lines (61 loc) · 1.89 KB
/
vm.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
#include "util.h"
#include <pulse/context.h>
#include <pulse/def.h>
#include <pulse/introspect.h>
#include <pulse/mainloop.h>
#include <pulse/pulseaudio.h>
#include <pulse/simple.h>
#include <pulse/volume.h>
#include <stdio.h>
static pa_volume_t volume;
static void
get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int is_last,
void *userdata __attribute__((unused)))
{
if (is_last) return;
if (i->mute) {
volume = 0;
} else {
volume = (double) pa_cvolume_avg(&i->volume) / PA_VOLUME_NORM *
100.0;
}
pa_context_disconnect(c);
}
static void
context_state_callback(pa_context *c, void *userdata)
{
switch (pa_context_get_state(c)) {
case PA_CONTEXT_READY:
pa_context_get_sink_info_by_index(
c, 0, get_sink_volume_callback, NULL);
break;
case PA_CONTEXT_UNCONNECTED:
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
case PA_CONTEXT_SETTING_NAME:
case PA_CONTEXT_FAILED:
// initial conn fails a few times before succeeding
break;
case PA_CONTEXT_TERMINATED:
pa_mainloop_quit(userdata, 1);
break;
}
}
void
vm(Args *arg, char *buff, int bufflen)
{
char *fmt = "%s%d%%";
pa_mainloop *m;
pa_mainloop_api *mapi;
pa_context *c;
m = pa_mainloop_new();
mapi = pa_mainloop_get_api(m);
c = pa_context_new(mapi, "statusbar");
pa_context_connect(c, NULL, PA_CONTEXT_NOFLAGS, NULL);
pa_context_set_state_callback(c, context_state_callback, m);
pa_mainloop_run(m, NULL);
pa_context_disconnect(c);
pa_context_unref(c);
pa_mainloop_free(m);
snprintf(buff, bufflen, fmt, getIcon(arg, volume), volume);
}