refactor(pulseaudio): Move loop to inline function

This commit is contained in:
NBonaparte 2017-09-15 21:10:25 -07:00
parent 4f15f42f2c
commit 318a6d83cf
2 changed files with 20 additions and 39 deletions

View File

@ -51,6 +51,8 @@ class pulseaudio {
static void sink_info_callback(pa_context *context, const pa_sink_info *info, int eol, void *userdata); static void sink_info_callback(pa_context *context, const pa_sink_info *info, int eol, void *userdata);
static void context_state_callback(pa_context *context, void *userdata); static void context_state_callback(pa_context *context, void *userdata);
inline void wait_loop(pa_operation *op, pa_threaded_mainloop *loop);
// used for temporary callback results // used for temporary callback results
pa_cvolume cv; pa_cvolume cv;
bool muted; bool muted;

View File

@ -53,9 +53,7 @@ pulseaudio::pulseaudio(string&& sink_name) : spec_s_name(sink_name) {
} }
pa_operation* op = pa_context_get_sink_info_by_name(m_context, sink_name.c_str(), sink_info_callback, this); pa_operation* op = pa_context_get_sink_info_by_name(m_context, sink_name.c_str(), sink_info_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
if (exists) if (exists)
s_name = sink_name; s_name = sink_name;
else { else {
@ -63,22 +61,16 @@ pulseaudio::pulseaudio(string&& sink_name) : spec_s_name(sink_name) {
if (!op) { if (!op) {
throw pulseaudio_error("Failed to get pulseaudio server info."); throw pulseaudio_error("Failed to get pulseaudio server info.");
} }
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
s_name = def_s_name; s_name = def_s_name;
// get the sink index // get the sink index
op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), sink_info_callback, this); op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), sink_info_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
} }
op = pa_context_subscribe(m_context, static_cast<pa_subscription_mask_t>(PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER), op = pa_context_subscribe(m_context, static_cast<pa_subscription_mask_t>(PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER),
simple_callback, this); simple_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
pa_context_set_subscribe_callback(m_context, subscribe_callback, this); pa_context_set_subscribe_callback(m_context, subscribe_callback, this);
pa_threaded_mainloop_unlock(m_mainloop); pa_threaded_mainloop_unlock(m_mainloop);
@ -125,29 +117,21 @@ int pulseaudio::process_events() {
// try to get specified sink // try to get specified sink
case evtype::NEW: case evtype::NEW:
o = pa_context_get_sink_info_by_name(m_context, spec_s_name.c_str(), sink_info_callback, this); o = pa_context_get_sink_info_by_name(m_context, spec_s_name.c_str(), sink_info_callback, this);
while (pa_operation_get_state(o) != PA_OPERATION_DONE) wait_loop(o, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(o);
if (exists) if (exists)
s_name = spec_s_name; s_name = spec_s_name;
break; break;
// get volume // get volume
case evtype::CHANGE: case evtype::CHANGE:
o = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), get_sink_volume_callback, this); o = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), get_sink_volume_callback, this);
while (pa_operation_get_state(o) != PA_OPERATION_DONE) wait_loop(o, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(o);
break; break;
// get default sink // get default sink
case evtype::REMOVE: case evtype::REMOVE:
o = pa_context_get_server_info(m_context, get_default_sink_callback, this); o = pa_context_get_server_info(m_context, get_default_sink_callback, this);
while (pa_operation_get_state(o) != PA_OPERATION_DONE) wait_loop(o, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(o);
o = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), sink_info_callback, this); o = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), sink_info_callback, this);
while (pa_operation_get_state(o) != PA_OPERATION_DONE) wait_loop(o, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(o);
break; break;
} }
m_events.pop(); m_events.pop();
@ -162,9 +146,7 @@ int pulseaudio::process_events() {
int pulseaudio::get_volume() { int pulseaudio::get_volume() {
pa_threaded_mainloop_lock(m_mainloop); pa_threaded_mainloop_lock(m_mainloop);
pa_operation *op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), get_sink_volume_callback, this); pa_operation *op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), get_sink_volume_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
pa_threaded_mainloop_unlock(m_mainloop); pa_threaded_mainloop_unlock(m_mainloop);
// alternatively, user pa_cvolume_avg_mask() to average selected channels // alternatively, user pa_cvolume_avg_mask() to average selected channels
//return math_util::percentage(pa_cvolume_avg(&cv), PA_VOLUME_MUTED, PA_VOLUME_NORM); //return math_util::percentage(pa_cvolume_avg(&cv), PA_VOLUME_MUTED, PA_VOLUME_NORM);
@ -178,15 +160,11 @@ int pulseaudio::get_volume() {
void pulseaudio::set_volume(float percentage) { void pulseaudio::set_volume(float percentage) {
pa_threaded_mainloop_lock(m_mainloop); pa_threaded_mainloop_lock(m_mainloop);
pa_operation *op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), get_sink_volume_callback, this); pa_operation *op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), get_sink_volume_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
pa_volume_t vol = math_util::percentage_to_value<pa_volume_t>(percentage, PA_VOLUME_MUTED, PA_VOLUME_NORM); pa_volume_t vol = math_util::percentage_to_value<pa_volume_t>(percentage, PA_VOLUME_MUTED, PA_VOLUME_NORM);
pa_cvolume_scale(&cv, vol); pa_cvolume_scale(&cv, vol);
op = pa_context_set_sink_volume_by_name(m_context, s_name.c_str(), &cv, simple_callback, this); op = pa_context_set_sink_volume_by_name(m_context, s_name.c_str(), &cv, simple_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
pa_threaded_mainloop_unlock(m_mainloop); pa_threaded_mainloop_unlock(m_mainloop);
} }
@ -196,9 +174,7 @@ void pulseaudio::set_volume(float percentage) {
void pulseaudio::set_mute(bool mode) { void pulseaudio::set_mute(bool mode) {
pa_threaded_mainloop_lock(m_mainloop); pa_threaded_mainloop_lock(m_mainloop);
pa_operation *op = pa_context_set_sink_mute_by_name(m_context, s_name.c_str(), mode, simple_callback, this); pa_operation *op = pa_context_set_sink_mute_by_name(m_context, s_name.c_str(), mode, simple_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
pa_threaded_mainloop_unlock(m_mainloop); pa_threaded_mainloop_unlock(m_mainloop);
} }
@ -215,9 +191,7 @@ void pulseaudio::toggle_mute() {
bool pulseaudio::is_muted() { bool pulseaudio::is_muted() {
pa_threaded_mainloop_lock(m_mainloop); pa_threaded_mainloop_lock(m_mainloop);
pa_operation *op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), check_mute_callback, this); pa_operation *op = pa_context_get_sink_info_by_name(m_context, s_name.c_str(), check_mute_callback, this);
while (pa_operation_get_state(op) != PA_OPERATION_DONE) wait_loop(op, m_mainloop);
pa_threaded_mainloop_wait(m_mainloop);
pa_operation_unref(op);
pa_threaded_mainloop_unlock(m_mainloop); pa_threaded_mainloop_unlock(m_mainloop);
return muted; return muted;
} }
@ -364,4 +338,9 @@ void pulseaudio::context_state_callback(pa_context *context, void *userdata) {
} }
} }
inline void pulseaudio::wait_loop(pa_operation *op, pa_threaded_mainloop *loop) {
while (pa_operation_get_state(op) != PA_OPERATION_DONE)
pa_threaded_mainloop_wait(loop);
pa_operation_unref(op);
}
POLYBAR_NS_END POLYBAR_NS_END