From 1f35b9dc89892bf4a8d390baf599ccb29257aa17 Mon Sep 17 00:00:00 2001 From: NBonaparte Date: Sat, 28 Oct 2017 22:50:51 -0700 Subject: [PATCH] fix(pulseaudio): Add logging --- include/adapters/pulseaudio.hpp | 6 ++++-- src/adapters/pulseaudio.cpp | 19 +++++++++++++------ src/modules/pulseaudio.cpp | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/adapters/pulseaudio.hpp b/include/adapters/pulseaudio.hpp index c7ad5f70..33115f5f 100644 --- a/include/adapters/pulseaudio.hpp +++ b/include/adapters/pulseaudio.hpp @@ -16,6 +16,7 @@ typedef struct pa_context pa_context; typedef struct pa_threaded_mainloop pa_threaded_mainloop; POLYBAR_NS +class logger; DEFINE_ERROR(pulseaudio_error); @@ -25,7 +26,7 @@ class pulseaudio { using queue = std::queue; public: - explicit pulseaudio(string&& sink_name); + explicit pulseaudio(const logger& logger, string&& sink_name); ~pulseaudio(); pulseaudio(const pulseaudio& o) = delete; @@ -54,11 +55,12 @@ class pulseaudio { inline void wait_loop(pa_operation *op, pa_threaded_mainloop *loop); + const logger& m_log; + // used for temporary callback results int success{0}; pa_cvolume cv; bool muted{false}; - bool exists{false}; // default sink name string def_s_name; diff --git a/src/adapters/pulseaudio.cpp b/src/adapters/pulseaudio.cpp index dd098ca5..563f337b 100644 --- a/src/adapters/pulseaudio.cpp +++ b/src/adapters/pulseaudio.cpp @@ -1,11 +1,12 @@ #include "adapters/pulseaudio.hpp" +#include "components/logger.hpp" POLYBAR_NS /** * Construct pulseaudio object */ -pulseaudio::pulseaudio(string&& sink_name) : spec_s_name(sink_name) { +pulseaudio::pulseaudio(const logger& logger, string&& sink_name) : m_log(logger), spec_s_name(sink_name) { m_mainloop = pa_threaded_mainloop_new(); if (!m_mainloop) { throw pulseaudio_error("Could not create pulseaudio threaded mainloop."); @@ -37,6 +38,8 @@ pulseaudio::pulseaudio(string&& sink_name) : spec_s_name(sink_name) { throw pulseaudio_error("Could not start pulseaudio mainloop."); } + m_log.trace("pulseaudio: started mainloop"); + pa_threaded_mainloop_wait(m_mainloop); if (pa_context_get_state(m_context) != PA_CONTEXT_READY) { pa_threaded_mainloop_unlock(m_mainloop); @@ -52,7 +55,8 @@ pulseaudio::pulseaudio(string&& sink_name) : spec_s_name(sink_name) { op = pa_context_get_sink_info_by_name(m_context, sink_name.c_str(), sink_info_callback, this); wait_loop(op, m_mainloop); } - if (!exists) { + if (s_name.empty()) { + printf("not found\n"); op = pa_context_get_server_info(m_context, get_default_sink_callback, this); if (!op) { throw pulseaudio_error("Failed to get pulseaudio server info."); @@ -63,6 +67,9 @@ pulseaudio::pulseaudio(string&& sink_name) : spec_s_name(sink_name) { // get the sink index op = pa_context_get_sink_info_by_name(m_context, def_s_name.c_str(), sink_info_callback, this); wait_loop(op, m_mainloop); + m_log.warn("pulseaudio: using default sink %s", s_name); + } else { + m_log.trace("pulseaudio: using sink %s", s_name); } op = pa_context_subscribe(m_context, PA_SUBSCRIPTION_MASK_SINK, simple_callback, this); @@ -132,6 +139,7 @@ int pulseaudio::process_events() { throw pulseaudio_error("Failed to get default sink."); o = pa_context_get_sink_info_by_name(m_context, def_s_name.c_str(), sink_info_callback, this); wait_loop(o, m_mainloop); + m_log.warn("pulseaudio: using default sink %s", s_name); break; } m_events.pop(); @@ -179,6 +187,8 @@ void pulseaudio::inc_volume(int delta_perc) { if (delta_perc > 0) { if (pa_cvolume_max(&cv) + vol <= PA_VOLUME_UI_MAX) { pa_cvolume_inc(&cv, vol); + } else { + m_log.warn("pulseaudio: maximum volume reached"); } } else pa_cvolume_dec(&cv, vol); @@ -293,10 +303,7 @@ void pulseaudio::get_default_sink_callback(pa_context *, const pa_server_info *i */ void pulseaudio::sink_info_callback(pa_context *, const pa_sink_info *info, int eol, void *userdata) { pulseaudio *This = static_cast(userdata); - if (eol || !info) - This->exists = false; - else { - This->exists = true; + if (!eol && info) { This->m_index = info->index; This->s_name = info->name; } diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index ab007949..12592fda 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -18,7 +18,7 @@ namespace modules { // Load configuration values auto sink_name = m_conf.get(name(), "sink", ""s); try { - m_pulseaudio = factory_util::unique(move(sink_name)); + m_pulseaudio = factory_util::unique(m_log, move(sink_name)); } catch (const pulseaudio_error& err) { throw module_error(err.what()); }