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)
|
2023-10-15 13:16:49 +00:00
|
|
|
: ALabel(config, "pulseaudio", id, "{volume}%") {
|
2019-07-06 13:29:35 +00:00
|
|
|
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
|
|
|
|
2023-10-15 13:16:49 +00:00
|
|
|
backend = util::AudioBackend::getInstance([this] { this->dp.emit(); });
|
|
|
|
backend->setIgnoredSinks(config_["ignored-sinks"]);
|
2018-08-09 21:55:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 13:07:50 +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()) {
|
2019-06-16 13:07:50 +00:00
|
|
|
return AModule::handleScroll(e);
|
|
|
|
}
|
|
|
|
auto dir = AModule::getScrollDir(e);
|
2019-06-15 12:57:52 +00:00
|
|
|
if (dir == SCROLL_DIR::NONE) {
|
|
|
|
return true;
|
2018-10-29 16:18:35 +00:00
|
|
|
}
|
2022-08-20 20:21:57 +00:00
|
|
|
int max_volume = 100;
|
2022-09-30 21:25:12 +00:00
|
|
|
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()) {
|
2022-09-30 21:25:12 +00:00
|
|
|
step = config_["scroll-step"].asDouble();
|
2019-05-14 07:24:06 +00:00
|
|
|
}
|
2022-08-20 20:21:57 +00:00
|
|
|
if (config_["max-volume"].isInt()) {
|
2023-10-15 13:16:49 +00:00
|
|
|
max_volume = config_["max-volume"].asInt();
|
2022-07-27 00:12:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 13:16:49 +00:00
|
|
|
auto change_type = (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::RIGHT)
|
|
|
|
? util::ChangeType::Increase
|
|
|
|
: util::ChangeType::Decrease;
|
2021-07-20 08:11:55 +00:00
|
|
|
|
2023-10-15 13:16:49 +00:00
|
|
|
backend->changeVolume(change_type, step, max_volume);
|
|
|
|
return true;
|
2018-08-09 21:55:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:35:16 +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",
|
2019-03-15 01:35:16 +00:00
|
|
|
};
|
|
|
|
|
2021-07-15 06:52:02 +00:00
|
|
|
const std::vector<std::string> waybar::modules::Pulseaudio::getPulseIcon() const {
|
2023-10-15 13:16:49 +00:00
|
|
|
std::vector<std::string> res = {backend->getCurrentSinkName(), backend->getDefaultSourceName()};
|
|
|
|
std::string nameLC = backend->getSinkPortName() + backend->getFormFactor();
|
2019-03-15 01:08:12 +00:00
|
|
|
std::transform(nameLC.begin(), nameLC.end(), nameLC.begin(), ::tolower);
|
2019-04-18 15:52:00 +00:00
|
|
|
for (auto const &port : ports) {
|
2019-03-15 01:08:12 +00:00
|
|
|
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;
|
2023-10-15 13:16:49 +00:00
|
|
|
auto sink_volume = backend->getSinkVolume();
|
2020-02-16 21:48:22 +00:00
|
|
|
if (!alt_) {
|
|
|
|
std::string format_name = "format";
|
2023-10-15 13:16:49 +00:00
|
|
|
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
|
|
|
}
|
2023-10-15 13:16:49 +00:00
|
|
|
if (backend->getSinkMuted()) {
|
2020-03-25 21:53:09 +00:00
|
|
|
// 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
|
|
|
}
|
2023-10-15 13:16:49 +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}%";
|
2023-10-15 13:16:49 +00:00
|
|
|
if (backend->getSourceMuted()) {
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.get_style_context()->add_class("source-muted");
|
2021-01-12 21:51:44 +00:00
|
|
|
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");
|
2021-01-12 21:51:44 +00:00
|
|
|
if (config_["format-source-muted"].isString()) {
|
|
|
|
format_source = config_["format-source"].asString();
|
|
|
|
}
|
2019-05-21 12:53:31 +00:00
|
|
|
}
|
2023-10-15 13:16:49 +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));
|
2022-12-02 15:08:56 +00:00
|
|
|
auto text = fmt::format(
|
2023-10-15 13:16:49 +00:00
|
|
|
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()) {
|
2022-12-02 15:08:56 +00:00
|
|
|
label_.hide();
|
|
|
|
} else {
|
|
|
|
label_.set_markup(text);
|
|
|
|
label_.show();
|
|
|
|
}
|
2022-02-09 13:20:09 +00:00
|
|
|
|
2019-02-22 10:35:26 +00:00
|
|
|
if (tooltipEnabled()) {
|
2021-01-12 21:51:44 +00:00
|
|
|
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(
|
2023-10-15 13:16:49 +00:00
|
|
|
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()))));
|
2021-01-12 21:51:44 +00:00
|
|
|
} else {
|
2023-10-15 13:16:49 +00:00
|
|
|
label_.set_tooltip_text(sink_desc);
|
2021-01-12 21:51:44 +00:00
|
|
|
}
|
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
|
|
|
}
|