Waybar/src/modules/pulseaudio.cpp

136 lines
5.0 KiB
C++
Raw Normal View History

2018-08-09 21:55:38 +00:00
#include "modules/pulseaudio.hpp"
2019-04-18 15:52:00 +00:00
waybar::modules::Pulseaudio::Pulseaudio(const std::string &id, const Json::Value &config)
: ALabel(config, "pulseaudio", id, "{volume}%") {
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &Pulseaudio::handleScroll));
2018-08-19 11:39:57 +00:00
backend = util::AudioBackend::getInstance([this] { this->dp.emit(); });
backend->setIgnoredSinks(config_["ignored-sinks"]);
2018-08-09 21:55:38 +00:00
}
bool waybar::modules::Pulseaudio::handleScroll(GdkEventScroll *e) {
// change the pulse volume only when no user provided
// events are configured
2019-06-16 13:13:40 +00:00
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
return AModule::handleScroll(e);
}
auto dir = AModule::getScrollDir(e);
2019-06-15 12:57:52 +00:00
if (dir == SCROLL_DIR::NONE) {
return true;
}
int max_volume = 100;
double step = 1;
2019-05-14 07:24:06 +00:00
// isDouble returns true for integers as well, just in case
if (config_["scroll-step"].isDouble()) {
step = config_["scroll-step"].asDouble();
2019-05-14 07:24:06 +00:00
}
if (config_["max-volume"].isInt()) {
max_volume = config_["max-volume"].asInt();
}
auto change_type = (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::RIGHT)
? util::ChangeType::Increase
: util::ChangeType::Decrease;
backend->changeVolume(change_type, step, max_volume);
return true;
2018-08-09 21:55:38 +00:00
}
static const std::array<std::string, 9> ports = {
2022-04-06 06:37:19 +00:00
"headphone", "speaker", "hdmi", "headset", "hands-free", "portable", "car", "hifi", "phone",
};
2021-07-15 06:52:02 +00:00
const std::vector<std::string> waybar::modules::Pulseaudio::getPulseIcon() const {
std::vector<std::string> res = {backend->getCurrentSinkName(), backend->getDefaultSourceName()};
std::string nameLC = backend->getSinkPortName() + backend->getFormFactor();
std::transform(nameLC.begin(), nameLC.end(), nameLC.begin(), ::tolower);
2019-04-18 15:52:00 +00:00
for (auto const &port : ports) {
if (nameLC.find(port) != std::string::npos) {
2021-07-15 06:52:02 +00:00
res.push_back(port);
return res;
2018-08-29 21:50:41 +00:00
}
}
2021-07-15 06:52:02 +00:00
return res;
2018-08-29 21:50:41 +00:00
}
2019-04-18 15:52:00 +00:00
auto waybar::modules::Pulseaudio::update() -> void {
2018-08-26 23:36:25 +00:00
auto format = format_;
2022-04-06 06:37:19 +00:00
std::string tooltip_format;
auto sink_volume = backend->getSinkVolume();
2020-02-16 21:48:22 +00:00
if (!alt_) {
std::string format_name = "format";
if (backend->isBluetooth()) {
2020-02-16 21:48:22 +00:00
format_name = format_name + "-bluetooth";
2022-11-24 11:28:52 +00:00
label_.get_style_context()->add_class("bluetooth");
2020-02-16 21:48:22 +00:00
} else {
2022-11-24 11:28:52 +00:00
label_.get_style_context()->remove_class("bluetooth");
2020-02-16 21:48:22 +00:00
}
if (backend->getSinkMuted()) {
// Check muted bluetooth format exist, otherwise fallback to default muted format
if (format_name != "format" && !config_[format_name + "-muted"].isString()) {
format_name = "format";
}
2020-02-16 21:48:22 +00:00
format_name = format_name + "-muted";
2022-11-24 11:28:52 +00:00
label_.get_style_context()->add_class("muted");
label_.get_style_context()->add_class("sink-muted");
2020-02-16 21:48:22 +00:00
} else {
2022-11-24 11:28:52 +00:00
label_.get_style_context()->remove_class("muted");
label_.get_style_context()->remove_class("sink-muted");
2020-02-16 21:48:22 +00:00
}
auto state = getState(sink_volume, true);
2023-04-07 04:19:45 +00:00
if (!state.empty() && config_[format_name + "-" + state].isString()) {
format = config_[format_name + "-" + state].asString();
} else if (config_[format_name].isString()) {
format = config_[format_name].asString();
}
2020-02-16 21:48:22 +00:00
}
2019-05-21 12:53:31 +00:00
// TODO: find a better way to split source/sink
2019-05-21 12:55:17 +00:00
std::string format_source = "{volume}%";
if (backend->getSourceMuted()) {
2022-11-24 11:28:52 +00:00
label_.get_style_context()->add_class("source-muted");
if (config_["format-source-muted"].isString()) {
format_source = config_["format-source-muted"].asString();
}
} else {
2022-11-24 11:28:52 +00:00
label_.get_style_context()->remove_class("source-muted");
if (config_["format-source-muted"].isString()) {
format_source = config_["format-source"].asString();
}
2019-05-21 12:53:31 +00:00
}
auto source_volume = backend->getSourceVolume();
auto sink_desc = backend->getSinkDesc();
auto source_desc = backend->getSourceDesc();
format_source = fmt::format(fmt::runtime(format_source), fmt::arg("volume", source_volume));
auto text = fmt::format(
fmt::runtime(format), fmt::arg("desc", sink_desc), fmt::arg("volume", sink_volume),
fmt::arg("format_source", format_source), fmt::arg("source_volume", source_volume),
fmt::arg("source_desc", source_desc), fmt::arg("icon", getIcon(sink_volume, getPulseIcon())));
2022-12-02 16:32:03 +00:00
if (text.empty()) {
label_.hide();
} else {
label_.set_markup(text);
label_.show();
}
2019-02-22 10:35:26 +00:00
if (tooltipEnabled()) {
if (tooltip_format.empty() && config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
if (!tooltip_format.empty()) {
2022-11-24 11:28:52 +00:00
label_.set_tooltip_text(fmt::format(
fmt::runtime(tooltip_format), fmt::arg("desc", sink_desc),
fmt::arg("volume", sink_volume), fmt::arg("format_source", format_source),
fmt::arg("source_volume", source_volume), fmt::arg("source_desc", source_desc),
fmt::arg("icon", getIcon(sink_volume, getPulseIcon()))));
} else {
label_.set_tooltip_text(sink_desc);
}
2019-02-22 10:35:26 +00:00
}
2020-04-12 16:30:21 +00:00
// Call parent update
2022-11-24 11:28:52 +00:00
ALabel::update();
2018-08-13 20:33:07 +00:00
}