Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pulseaudio: Add 'sink-mapping' config option #3421

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/util/audio_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AudioBackend {
std::string default_source_name_;

std::vector<std::string> ignored_sinks_;
std::map<std::string, std::string> sink_mapping_;

std::function<void()> on_updated_cb_ = NOOP;

Expand All @@ -69,6 +70,7 @@ class AudioBackend {
void changeVolume(ChangeType change_type, double step = 1, uint16_t max_volume = 100);

void setIgnoredSinks(const Json::Value& config);
void setSinkMapping(const Json::Value& config);

std::string getSinkPortName() const { return port_name_; }
std::string getFormFactor() const { return form_factor_; }
Expand All @@ -93,4 +95,4 @@ class AudioBackend {
bool isBluetooth();
};

} // namespace waybar::util
} // namespace waybar::util
4 changes: 4 additions & 0 deletions man/waybar-pulseaudio.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Additionally, you can control the volume by scrolling *up* or *down* while the c
typeof: array ++
Sinks in this list will not be shown as active sink by Waybar. Entries should be the sink's description field.

*sink-mapping*: ++
typeof: object ++
Sinks named by the values of this mapping will be considered to be the current sink instead of the sinks named by the respective keys.

*menu*: ++
typeof: string ++
Action that popups the menu.
Expand Down
1 change: 1 addition & 0 deletions src/modules/pulseaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ waybar::modules::Pulseaudio::Pulseaudio(const std::string &id, const Json::Value

backend = util::AudioBackend::getInstance([this] { this->dp.emit(); });
backend->setIgnoredSinks(config_["ignored-sinks"]);
backend->setSinkMapping(config_["sink-mapping"]);
}

bool waybar::modules::Pulseaudio::handleScroll(GdkEventScroll *e) {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/pulseaudio_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PulseaudioSlider::PulseaudioSlider(const std::string& id, const Json::Value& con
: ASlider(config, "pulseaudio-slider", id) {
backend = util::AudioBackend::getInstance([this] { this->dp.emit(); });
backend->setIgnoredSinks(config_["ignored-sinks"]);
backend->setSinkMapping(config_["sink-mapping"]);

if (config_["target"].isString()) {
std::string target = config_["target"].asString();
Expand Down Expand Up @@ -79,4 +80,4 @@ void PulseaudioSlider::onValueChanged() {
backend->changeVolume(volume, min_, max_);
}

} // namespace waybar::modules
} // namespace waybar::modules
17 changes: 17 additions & 0 deletions src/util/audio_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ void AudioBackend::sinkInfoCb(pa_context * /*context*/, const pa_sink_info *i, i
}
}

if (const auto mapping = backend->sink_mapping_.find(backend->current_sink_name_);
mapping != backend->sink_mapping_.end()) {
if (i->name == mapping->second) {
backend->current_sink_name_ = i->name;
}
}

if (backend->current_sink_name_ == i->name) {
backend->current_sink_running_ = i->state == PA_SINK_RUNNING;
}
Expand Down Expand Up @@ -292,4 +299,14 @@ void AudioBackend::setIgnoredSinks(const Json::Value &config) {
}
}

void AudioBackend::setSinkMapping(const Json::Value &config) {
if (config.isObject()) {
for (auto it = config.begin(); it != config.end(); ++it) {
if (it.key().isString() && it->isString()) {
sink_mapping_.emplace(it.key().asString(), it->asString());
}
}
}
}

} // namespace waybar::util
Loading