2017-09-26 18:21:23 -04:00
|
|
|
#include "modules/pulseaudio.hpp"
|
2019-11-28 11:36:14 -05:00
|
|
|
|
2017-09-26 18:21:23 -04:00
|
|
|
#include "adapters/pulseaudio.hpp"
|
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/progressbar.hpp"
|
|
|
|
#include "drawtypes/ramp.hpp"
|
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
#include "settings.hpp"
|
2019-11-28 11:36:14 -05:00
|
|
|
#include "utils/math.hpp"
|
2017-09-26 18:21:23 -04:00
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
namespace modules {
|
|
|
|
template class module<pulseaudio_module>;
|
|
|
|
|
2019-11-28 11:36:14 -05:00
|
|
|
pulseaudio_module::pulseaudio_module(const bar_settings& bar, string name_)
|
|
|
|
: event_module<pulseaudio_module>(bar, move(name_)) {
|
2021-01-04 04:25:52 -05:00
|
|
|
if (m_handle_events) {
|
|
|
|
m_router->register_action(EVENT_DEC, &pulseaudio_module::action_dec);
|
|
|
|
m_router->register_action(EVENT_INC, &pulseaudio_module::action_inc);
|
|
|
|
m_router->register_action(EVENT_TOGGLE, &pulseaudio_module::action_toggle);
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:21:23 -04:00
|
|
|
// Load configuration values
|
2018-08-06 16:47:12 -04:00
|
|
|
m_interval = m_conf.get(name(), "interval", m_interval);
|
|
|
|
|
2017-09-26 18:21:23 -04:00
|
|
|
auto sink_name = m_conf.get(name(), "sink", ""s);
|
2018-04-09 19:13:04 -04:00
|
|
|
bool m_max_volume = m_conf.get(name(), "use-ui-max", true);
|
|
|
|
|
2017-09-26 18:21:23 -04:00
|
|
|
try {
|
2018-04-09 19:13:04 -04:00
|
|
|
m_pulseaudio = factory_util::unique<pulseaudio>(m_log, move(sink_name), m_max_volume);
|
2017-09-26 18:21:23 -04:00
|
|
|
} catch (const pulseaudio_error& err) {
|
|
|
|
throw module_error(err.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add formats and elements
|
|
|
|
m_formatter->add(FORMAT_VOLUME, TAG_LABEL_VOLUME, {TAG_RAMP_VOLUME, TAG_LABEL_VOLUME, TAG_BAR_VOLUME});
|
|
|
|
m_formatter->add(FORMAT_MUTED, TAG_LABEL_MUTED, {TAG_RAMP_VOLUME, TAG_LABEL_MUTED, TAG_BAR_VOLUME});
|
|
|
|
|
|
|
|
if (m_formatter->has(TAG_BAR_VOLUME)) {
|
|
|
|
m_bar_volume = load_progressbar(m_bar, m_conf, name(), TAG_BAR_VOLUME);
|
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL_VOLUME, FORMAT_VOLUME)) {
|
|
|
|
m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%%");
|
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL_MUTED, FORMAT_MUTED)) {
|
|
|
|
m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%%");
|
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_RAMP_VOLUME)) {
|
|
|
|
m_ramp_volume = load_ramp(m_conf, name(), TAG_RAMP_VOLUME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pulseaudio_module::teardown() {
|
|
|
|
m_pulseaudio.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pulseaudio_module::has_event() {
|
|
|
|
// Poll for mixer and control events
|
|
|
|
try {
|
2017-10-07 23:19:06 -04:00
|
|
|
if (m_pulseaudio->wait())
|
2017-09-26 18:21:23 -04:00
|
|
|
return true;
|
|
|
|
} catch (const pulseaudio_error& e) {
|
|
|
|
m_log.err("%s: %s", name(), e.what());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pulseaudio_module::update() {
|
|
|
|
// Consume pending events
|
|
|
|
m_pulseaudio->process_events();
|
|
|
|
|
2017-10-14 14:27:43 -04:00
|
|
|
// Get volume and mute state
|
2017-09-26 18:21:23 -04:00
|
|
|
m_volume = 100;
|
2019-10-08 20:13:58 -04:00
|
|
|
m_decibels = PA_DECIBEL_MININFTY;
|
2017-09-26 18:21:23 -04:00
|
|
|
m_muted = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (m_pulseaudio) {
|
|
|
|
m_volume = m_volume * m_pulseaudio->get_volume() / 100.0f;
|
2019-10-08 20:13:58 -04:00
|
|
|
m_decibels = m_pulseaudio->get_decibels();
|
2017-09-26 18:21:23 -04:00
|
|
|
m_muted = m_muted || m_pulseaudio->is_muted();
|
|
|
|
}
|
|
|
|
} catch (const pulseaudio_error& err) {
|
|
|
|
m_log.err("%s: Failed to query pulseaudio sink (%s)", name(), err.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace label tokens
|
|
|
|
if (m_label_volume) {
|
|
|
|
m_label_volume->reset_tokens();
|
|
|
|
m_label_volume->replace_token("%percentage%", to_string(m_volume));
|
2019-10-08 20:13:58 -04:00
|
|
|
m_label_volume->replace_token("%decibels%", string_util::floating_point(m_decibels, 2, true));
|
2017-09-26 18:21:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_label_muted) {
|
|
|
|
m_label_muted->reset_tokens();
|
|
|
|
m_label_muted->replace_token("%percentage%", to_string(m_volume));
|
2019-10-08 20:13:58 -04:00
|
|
|
m_label_muted->replace_token("%decibels%", string_util::floating_point(m_decibels, 2, true));
|
2017-09-26 18:21:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string pulseaudio_module::get_format() const {
|
|
|
|
return m_muted ? FORMAT_MUTED : FORMAT_VOLUME;
|
|
|
|
}
|
|
|
|
|
|
|
|
string pulseaudio_module::get_output() {
|
|
|
|
// Get the module output early so that
|
|
|
|
// the format prefix/suffix also gets wrapper
|
|
|
|
// with the cmd handlers
|
|
|
|
string output{module::get_output()};
|
|
|
|
|
|
|
|
if (m_handle_events) {
|
2019-11-28 11:36:14 -05:00
|
|
|
auto click_middle = m_conf.get(name(), "click-middle", ""s);
|
|
|
|
auto click_right = m_conf.get(name(), "click-right", ""s);
|
|
|
|
|
|
|
|
if (!click_middle.empty()) {
|
2020-05-24 12:35:12 -04:00
|
|
|
m_builder->action(mousebtn::MIDDLE, click_middle);
|
2019-11-28 11:36:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!click_right.empty()) {
|
2020-05-24 12:35:12 -04:00
|
|
|
m_builder->action(mousebtn::RIGHT, click_right);
|
2019-11-28 11:36:14 -05:00
|
|
|
}
|
|
|
|
|
2020-05-23 18:36:16 -04:00
|
|
|
m_builder->action(mousebtn::LEFT, *this, EVENT_TOGGLE, "");
|
|
|
|
m_builder->action(mousebtn::SCROLL_UP, *this, EVENT_INC, "");
|
|
|
|
m_builder->action(mousebtn::SCROLL_DOWN, *this, EVENT_DEC, "");
|
2017-09-26 18:21:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_builder->append(output);
|
|
|
|
|
|
|
|
return m_builder->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pulseaudio_module::build(builder* builder, const string& tag) const {
|
|
|
|
if (tag == TAG_BAR_VOLUME) {
|
|
|
|
builder->node(m_bar_volume->output(m_volume));
|
2017-10-14 14:27:43 -04:00
|
|
|
} else if (tag == TAG_RAMP_VOLUME) {
|
2017-09-26 18:21:23 -04:00
|
|
|
builder->node(m_ramp_volume->get_by_percentage(m_volume));
|
|
|
|
} else if (tag == TAG_LABEL_VOLUME) {
|
|
|
|
builder->node(m_label_volume);
|
|
|
|
} else if (tag == TAG_LABEL_MUTED) {
|
|
|
|
builder->node(m_label_muted);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
void pulseaudio_module::action_inc() {
|
|
|
|
m_pulseaudio->inc_volume(m_interval);
|
|
|
|
}
|
2017-09-26 18:21:23 -04:00
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
void pulseaudio_module::action_dec() {
|
|
|
|
m_pulseaudio->inc_volume(-m_interval);
|
|
|
|
}
|
2017-09-26 18:21:23 -04:00
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
void pulseaudio_module::action_toggle() {
|
|
|
|
m_pulseaudio->toggle_mute();
|
2017-09-26 18:21:23 -04:00
|
|
|
}
|
2019-11-28 11:36:14 -05:00
|
|
|
} // namespace modules
|
2017-09-26 18:21:23 -04:00
|
|
|
|
|
|
|
POLYBAR_NS_END
|