Skip to content

Instantly share code, notes, and snippets.

@stapelberg
Created March 21, 2015 17:29
Show Gist options
  • Save stapelberg/507b400483e2f8efdebd to your computer and use it in GitHub Desktop.
Save stapelberg/507b400483e2f8efdebd to your computer and use it in GitHub Desktop.
// vim:ts=4:sw=4:et
#include <stdio.h>
#include <err.h>
#include <pulse/pulseaudio.h>
void sink_info_cb(pa_context *c, const pa_sink_info *info, int eol, void *userdata) {
printf("got sink info %p, eol %d\n", info, eol);
// Why do we get called with sink_info being NULL?
if (info != NULL) {
printf("sink name = %s, index %d, description %s\n", info->name, info->index, info->description);
char vol[4096];
pa_volume_t avg = pa_cvolume_avg(&(info->volume));
pa_cvolume_snprint(vol, sizeof(vol), &(info->volume));
printf("volume: %u\n", (avg*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM );
printf("(pretty: %s)\n", vol);
}
pa_threaded_mainloop *loop = userdata;
pa_threaded_mainloop_signal(loop, 0);
}
static void context_state_cb(pa_context *c, void *userdata) {
pa_threaded_mainloop *loop = userdata;
switch (pa_context_get_state(c)) {
case PA_CONTEXT_READY:
case PA_CONTEXT_TERMINATED:
case PA_CONTEXT_FAILED:
pa_threaded_mainloop_signal(loop, 0);
break;
case PA_CONTEXT_UNCONNECTED:
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
case PA_CONTEXT_SETTING_NAME:
break;
}
}
int main() {
int error = PA_ERR_INTERNAL;
pa_threaded_mainloop *loop = pa_threaded_mainloop_new();
pa_context* pulsectx = pa_context_new(pa_threaded_mainloop_get_api(loop), "i3status");
// TODO: NULL
//
pa_context_set_state_callback(pulsectx, context_state_cb, loop);
if (pa_context_connect(pulsectx, NULL, 0, NULL) < 0) {
error = pa_context_errno(pulsectx);
errx(1, "pa_context_connect: %s", error);
}
pa_threaded_mainloop_lock(loop);
if (pa_threaded_mainloop_start(loop) < 0)
errx(1, "pa_threaded_mainloop_start()");
// TODO: why is this block necessary? could we not just _wait and then see what the status is?
for (;;) {
pa_context_state_t state;
state = pa_context_get_state(pulsectx);
if (state == PA_CONTEXT_READY)
break;
if (!PA_CONTEXT_IS_GOOD(state)) {
error = pa_context_errno(pulsectx);
errx(1, "pa_context_connect: %s", error);
}
/* Wait until the context is ready */
pa_threaded_mainloop_wait(loop);
}
printf("requesting sink info\n");
pa_operation *o = pa_context_get_sink_info_by_name(
pulsectx, "@DEFAULT_SINK@", sink_info_cb, loop);
while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
printf("wait\n");
pa_threaded_mainloop_wait(loop);
printf("unlocked\n");
}
pa_operation_unref(o);
pa_threaded_mainloop_unlock(loop);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment