Merge pull request #4319 from M0streng0/master

wireplumber & backlight: fix states and support for microphone
This commit is contained in:
Alexis Rouillard
2025-08-08 08:56:07 +02:00
committed by GitHub
3 changed files with 201 additions and 55 deletions

View File

@ -22,6 +22,8 @@ class Wireplumber : public ALabel {
void activatePlugins(); void activatePlugins();
static void updateVolume(waybar::modules::Wireplumber* self, uint32_t id); static void updateVolume(waybar::modules::Wireplumber* self, uint32_t id);
static void updateNodeName(waybar::modules::Wireplumber* self, uint32_t id); static void updateNodeName(waybar::modules::Wireplumber* self, uint32_t id);
static void updateSourceVolume(waybar::modules::Wireplumber* self, uint32_t id);
static void updateSourceName(waybar::modules::Wireplumber* self, uint32_t id); // NEW
static void onPluginActivated(WpObject* p, GAsyncResult* res, waybar::modules::Wireplumber* self); static void onPluginActivated(WpObject* p, GAsyncResult* res, waybar::modules::Wireplumber* self);
static void onDefaultNodesApiLoaded(WpObject* p, GAsyncResult* res, static void onDefaultNodesApiLoaded(WpObject* p, GAsyncResult* res,
waybar::modules::Wireplumber* self); waybar::modules::Wireplumber* self);
@ -46,7 +48,12 @@ class Wireplumber : public ALabel {
double min_step_; double min_step_;
uint32_t node_id_{0}; uint32_t node_id_{0};
std::string node_name_; std::string node_name_;
std::string source_name_;
gchar* type_; gchar* type_;
uint32_t source_node_id_;
bool source_muted_;
double source_volume_;
gchar* default_source_name_;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -38,10 +38,20 @@ auto waybar::modules::Backlight::update() -> void {
event_box_.show(); event_box_.show();
const uint8_t percent = const uint8_t percent =
best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max()); best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max());
std::string desc = fmt::format(fmt::runtime(format_), fmt::arg("percent", percent),
// Get the state and apply state-specific format if available
auto state = getState(percent);
std::string current_format = format_;
if (!state.empty()) {
std::string state_format_name = "format-" + state;
if (config_[state_format_name].isString()) {
current_format = config_[state_format_name].asString();
}
}
std::string desc = fmt::format(fmt::runtime(current_format), fmt::arg("percent", percent),
fmt::arg("icon", getIcon(percent))); fmt::arg("icon", getIcon(percent)));
label_.set_markup(desc); label_.set_markup(desc);
getState(percent);
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_format; std::string tooltip_format;
if (config_["tooltip-format"].isString()) { if (config_["tooltip-format"].isString()) {

View File

@ -14,11 +14,15 @@ waybar::modules::Wireplumber::Wireplumber(const std::string& id, const Json::Val
mixer_api_(nullptr), mixer_api_(nullptr),
def_nodes_api_(nullptr), def_nodes_api_(nullptr),
default_node_name_(nullptr), default_node_name_(nullptr),
default_source_name_(nullptr),
pending_plugins_(0), pending_plugins_(0),
muted_(false), muted_(false),
source_muted_(false),
volume_(0.0), volume_(0.0),
source_volume_(0.0),
min_step_(0.0), min_step_(0.0),
node_id_(0), node_id_(0),
source_node_id_(0),
type_(nullptr) { type_(nullptr) {
waybar::modules::Wireplumber::modules.push_back(this); waybar::modules::Wireplumber::modules.push_back(this);
@ -55,6 +59,7 @@ waybar::modules::Wireplumber::~Wireplumber() {
g_clear_object(&mixer_api_); g_clear_object(&mixer_api_);
g_clear_object(&def_nodes_api_); g_clear_object(&def_nodes_api_);
g_free(default_node_name_); g_free(default_node_name_);
g_free(default_source_name_);
g_free(type_); g_free(type_);
} }
@ -94,6 +99,41 @@ void waybar::modules::Wireplumber::updateNodeName(waybar::modules::Wireplumber*
spdlog::debug("[{}]: Updating '{}' node name to: {}", self->name_, self->type_, self->node_name_); spdlog::debug("[{}]: Updating '{}' node name to: {}", self->name_, self->type_, self->node_name_);
} }
void waybar::modules::Wireplumber::updateSourceName(waybar::modules::Wireplumber* self, uint32_t id) {
spdlog::debug("[{}]: updating source name with node.id {}", self->name_, id);
if (!isValidNodeId(id)) {
spdlog::warn("[{}]: '{}' is not a valid source node ID. Ignoring source name update.", self->name_, id);
return;
}
auto* proxy = static_cast<WpProxy*>(wp_object_manager_lookup(self->om_, WP_TYPE_GLOBAL_PROXY,
WP_CONSTRAINT_TYPE_G_PROPERTY,
"bound-id", "=u", id, nullptr));
if (proxy == nullptr) {
auto err = fmt::format("Source object '{}' not found\n", id);
spdlog::error("[{}]: {}", self->name_, err);
return;
}
g_autoptr(WpProperties) properties =
WP_IS_PIPEWIRE_OBJECT(proxy) != 0
? wp_pipewire_object_get_properties(WP_PIPEWIRE_OBJECT(proxy))
: wp_properties_new_empty();
g_autoptr(WpProperties) globalP = wp_global_proxy_get_global_properties(WP_GLOBAL_PROXY(proxy));
properties = wp_properties_ensure_unique_owner(properties);
wp_properties_add(properties, globalP);
wp_properties_set(properties, "object.id", nullptr);
const auto* nick = wp_properties_get(properties, "node.nick");
const auto* description = wp_properties_get(properties, "node.description");
self->source_name_ = nick != nullptr ? nick
: description != nullptr ? description
: "Unknown source name";
spdlog::debug("[{}]: Updating source name to: {}", self->name_, self->source_name_);
}
void waybar::modules::Wireplumber::updateVolume(waybar::modules::Wireplumber* self, uint32_t id) { void waybar::modules::Wireplumber::updateVolume(waybar::modules::Wireplumber* self, uint32_t id) {
spdlog::debug("[{}]: updating volume", self->name_); spdlog::debug("[{}]: updating volume", self->name_);
GVariant* variant = nullptr; GVariant* variant = nullptr;
@ -120,6 +160,29 @@ void waybar::modules::Wireplumber::updateVolume(waybar::modules::Wireplumber* se
self->dp.emit(); self->dp.emit();
} }
void waybar::modules::Wireplumber::updateSourceVolume(waybar::modules::Wireplumber* self, uint32_t id) {
spdlog::debug("[{}]: updating source volume", self->name_);
GVariant* variant = nullptr;
if (!isValidNodeId(id)) {
spdlog::error("[{}]: '{}' is not a valid source node ID. Ignoring source volume update.", self->name_, id);
return;
}
g_signal_emit_by_name(self->mixer_api_, "get-volume", id, &variant);
if (variant == nullptr) {
spdlog::debug("[{}]: Source node {} does not support volume", self->name_, id);
return;
}
g_variant_lookup(variant, "volume", "d", &self->source_volume_);
g_variant_lookup(variant, "mute", "b", &self->source_muted_);
g_clear_pointer(&variant, g_variant_unref);
self->dp.emit();
}
void waybar::modules::Wireplumber::onMixerChanged(waybar::modules::Wireplumber* self, uint32_t id) { void waybar::modules::Wireplumber::onMixerChanged(waybar::modules::Wireplumber* self, uint32_t id) {
g_autoptr(WpNode) node = static_cast<WpNode*>(wp_object_manager_lookup( g_autoptr(WpNode) node = static_cast<WpNode*>(wp_object_manager_lookup(
self->om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", id, nullptr)); self->om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", id, nullptr));
@ -127,9 +190,9 @@ void waybar::modules::Wireplumber::onMixerChanged(waybar::modules::Wireplumber*
if (node == nullptr) { if (node == nullptr) {
// log a warning only if no other widget is targeting the id. // log a warning only if no other widget is targeting the id.
// this reduces log spam when multiple instances of the module are used on different node types. // this reduces log spam when multiple instances of the module are used on different node types.
if (id != self->node_id_) { if (id != self->node_id_ && id != self->source_node_id_) {
for (auto const& module : waybar::modules::Wireplumber::modules) { for (auto const& module : waybar::modules::Wireplumber::modules) {
if (module->node_id_ == id) { if (module->node_id_ == id || module->source_node_id_ == id) {
return; return;
} }
} }
@ -142,68 +205,73 @@ void waybar::modules::Wireplumber::onMixerChanged(waybar::modules::Wireplumber*
const gchar* name = wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(node), "node.name"); const gchar* name = wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(node), "node.name");
if (self->node_id_ != id) { if (self->node_id_ == id) {
spdlog::debug( spdlog::debug("[{}]: (onMixerChanged: {}) - updating sink volume for node: {}",
"[{}]: (onMixerChanged: {}) - ignoring mixer update for node: id: {}, name: {} as it is " self->name_, self->type_, name);
"not the default node: {} with id: {}", updateVolume(self, id);
self->name_, self->type_, id, name, self->default_node_name_, self->node_id_); } else if (self->source_node_id_ == id) {
return; spdlog::debug("[{}]: (onMixerChanged: {}) - updating source volume for node: {}",
self->name_, self->type_, name);
updateSourceVolume(self, id);
} }
spdlog::debug(
"[{}]: (onMixerChanged: {}) - Need to update volume for node with id {} and name {}",
self->name_, self->type_, id, name);
updateVolume(self, id);
} }
void waybar::modules::Wireplumber::onDefaultNodesApiChanged(waybar::modules::Wireplumber* self) { void waybar::modules::Wireplumber::onDefaultNodesApiChanged(waybar::modules::Wireplumber* self) {
spdlog::debug("[{}]: (onDefaultNodesApiChanged: {})", self->name_, self->type_); spdlog::debug("[{}]: (onDefaultNodesApiChanged: {})", self->name_, self->type_);
// Handle sink
uint32_t defaultNodeId; uint32_t defaultNodeId;
g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", self->type_, &defaultNodeId); g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", self->type_, &defaultNodeId);
if (!isValidNodeId(defaultNodeId)) { if (isValidNodeId(defaultNodeId)) {
spdlog::warn("[{}]: '{}' is not a valid node ID. Ignoring '{}' node change.", self->name_, g_autoptr(WpNode) node = static_cast<WpNode*>(
defaultNodeId, self->type_);
return;
}
g_autoptr(WpNode) node = static_cast<WpNode*>(
wp_object_manager_lookup(self->om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", wp_object_manager_lookup(self->om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id",
"=u", defaultNodeId, nullptr)); "=u", defaultNodeId, nullptr));
if (node == nullptr) { if (node != nullptr) {
spdlog::warn("[{}]: (onDefaultNodesApiChanged: {}) - Object with id {} not found", self->name_, const gchar* defaultNodeName =
self->type_, defaultNodeId); wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(node), "node.name");
return;
if (g_strcmp0(self->default_node_name_, defaultNodeName) != 0 ||
self->node_id_ != defaultNodeId) {
spdlog::debug("[{}]: Default sink changed to -> Node(name: {}, id: {})",
self->name_, defaultNodeName, defaultNodeId);
g_free(self->default_node_name_);
self->default_node_name_ = g_strdup(defaultNodeName);
self->node_id_ = defaultNodeId;
updateVolume(self, defaultNodeId);
updateNodeName(self, defaultNodeId);
}
}
} }
const gchar* defaultNodeName = // Handle source
wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(node), "node.name"); uint32_t defaultSourceId;
g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", "Audio/Source", &defaultSourceId);
spdlog::debug( if (isValidNodeId(defaultSourceId)) {
"[{}]: (onDefaultNodesApiChanged: {}) - got the following default node: Node(name: {}, id: " g_autoptr(WpNode) sourceNode = static_cast<WpNode*>(
"{})", wp_object_manager_lookup(self->om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id",
self->name_, self->type_, defaultNodeName, defaultNodeId); "=u", defaultSourceId, nullptr));
if (g_strcmp0(self->default_node_name_, defaultNodeName) == 0 && if (sourceNode != nullptr) {
self->node_id_ == defaultNodeId) { const gchar* defaultSourceName =
spdlog::debug( wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(sourceNode), "node.name");
"[{}]: (onDefaultNodesApiChanged: {}) - Default node has not changed. Node(name: {}, id: "
"{}). Ignoring.", if (g_strcmp0(self->default_source_name_, defaultSourceName) != 0 ||
self->name_, self->type_, self->default_node_name_, defaultNodeId); self->source_node_id_ != defaultSourceId) {
return; spdlog::debug("[{}]: Default source changed to -> Node(name: {}, id: {})",
self->name_, defaultSourceName, defaultSourceId);
g_free(self->default_source_name_);
self->default_source_name_ = g_strdup(defaultSourceName);
self->source_node_id_ = defaultSourceId;
updateSourceVolume(self, defaultSourceId);
updateSourceName(self, defaultSourceId);
}
}
} }
spdlog::debug(
"[{}]: (onDefaultNodesApiChanged: {}) - Default node changed to -> Node(name: {}, id: {})",
self->name_, self->type_, defaultNodeName, defaultNodeId);
g_free(self->default_node_name_);
self->default_node_name_ = g_strdup(defaultNodeName);
self->node_id_ = defaultNodeId;
updateVolume(self, defaultNodeId);
updateNodeName(self, defaultNodeId);
} }
void waybar::modules::Wireplumber::onObjectManagerInstalled(waybar::modules::Wireplumber* self) { void waybar::modules::Wireplumber::onObjectManagerInstalled(waybar::modules::Wireplumber* self) {
@ -223,18 +291,32 @@ void waybar::modules::Wireplumber::onObjectManagerInstalled(waybar::modules::Wir
throw std::runtime_error("Mixer api is not loaded\n"); throw std::runtime_error("Mixer api is not loaded\n");
} }
// Get default sink
g_signal_emit_by_name(self->def_nodes_api_, "get-default-configured-node-name", self->type_, g_signal_emit_by_name(self->def_nodes_api_, "get-default-configured-node-name", self->type_,
&self->default_node_name_); &self->default_node_name_);
g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", self->type_, &self->node_id_); g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", self->type_, &self->node_id_);
// Get default source
g_signal_emit_by_name(self->def_nodes_api_, "get-default-configured-node-name", "Audio/Source",
&self->default_source_name_);
g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", "Audio/Source", &self->source_node_id_);
if (self->default_node_name_ != nullptr) { if (self->default_node_name_ != nullptr) {
spdlog::debug( spdlog::debug(
"[{}]: (onObjectManagerInstalled: {}) - default configured node name: {} and id: {}", "[{}]: (onObjectManagerInstalled: {}) - default configured node name: {} and id: {}",
self->name_, self->type_, self->default_node_name_, self->node_id_); self->name_, self->type_, self->default_node_name_, self->node_id_);
} }
if (self->default_source_name_ != nullptr) {
spdlog::debug(
"[{}]: default source: {} (id: {})",
self->name_, self->default_source_name_, self->source_node_id_);
}
updateVolume(self, self->node_id_); updateVolume(self, self->node_id_);
updateNodeName(self, self->node_id_); updateNodeName(self, self->node_id_);
updateSourceVolume(self, self->source_node_id_);
updateSourceName(self, self->source_node_id_);
g_signal_connect_swapped(self->mixer_api_, "changed", (GCallback)onMixerChanged, self); g_signal_connect_swapped(self->mixer_api_, "changed", (GCallback)onMixerChanged, self);
g_signal_connect_swapped(self->def_nodes_api_, "changed", (GCallback)onDefaultNodesApiChanged, g_signal_connect_swapped(self->def_nodes_api_, "changed", (GCallback)onDefaultNodesApiChanged,
@ -271,6 +353,8 @@ void waybar::modules::Wireplumber::prepare(waybar::modules::Wireplumber* self) {
spdlog::debug("[{}]: preparing object manager: '{}'", name_, self->type_); spdlog::debug("[{}]: preparing object manager: '{}'", name_, self->type_);
wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class",
"=s", self->type_, nullptr); "=s", self->type_, nullptr);
wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class",
"=s", "Audio/Source", nullptr);
} }
void waybar::modules::Wireplumber::onDefaultNodesApiLoaded(WpObject* p, GAsyncResult* res, void waybar::modules::Wireplumber::onDefaultNodesApiLoaded(WpObject* p, GAsyncResult* res,
@ -332,19 +416,60 @@ auto waybar::modules::Wireplumber::update() -> void {
auto format = format_; auto format = format_;
std::string tooltipFormat; std::string tooltipFormat;
// Handle sink mute state
if (muted_) { if (muted_) {
format = config_["format-muted"].isString() ? config_["format-muted"].asString() : format; format = config_["format-muted"].isString() ? config_["format-muted"].asString() : format;
label_.get_style_context()->add_class("muted"); label_.get_style_context()->add_class("muted");
label_.get_style_context()->add_class("sink-muted");
} else { } else {
label_.get_style_context()->remove_class("muted"); label_.get_style_context()->remove_class("muted");
label_.get_style_context()->remove_class("sink-muted");
}
// Handle source mute state
if (source_muted_) {
label_.get_style_context()->add_class("source-muted");
} else {
label_.get_style_context()->remove_class("source-muted");
} }
int vol = round(volume_ * 100.0); int vol = round(volume_ * 100.0);
std::string markup = fmt::format(fmt::runtime(format), fmt::arg("node_name", node_name_), int source_vol = round(source_volume_ * 100.0);
fmt::arg("volume", vol), fmt::arg("icon", getIcon(vol)));
label_.set_markup(markup);
getState(vol); // Get the state and apply state-specific format if available
auto state = getState(vol);
if (!state.empty()) {
std::string format_name = muted_ ? "format-muted" : "format";
std::string state_format_name = format_name + "-" + state;
if (config_[state_format_name].isString()) {
format = config_[state_format_name].asString();
}
}
// Prepare source format string (similar to PulseAudio)
std::string format_source = "{volume}%";
if (source_muted_) {
if (config_["format-source-muted"].isString()) {
format_source = config_["format-source-muted"].asString();
}
} else {
if (config_["format-source"].isString()) {
format_source = config_["format-source"].asString();
}
}
// Format the source string with actual volume
std::string formatted_source = fmt::format(fmt::runtime(format_source),
fmt::arg("volume", source_vol));
std::string markup = fmt::format(fmt::runtime(format),
fmt::arg("node_name", node_name_),
fmt::arg("volume", vol),
fmt::arg("icon", getIcon(vol)),
fmt::arg("format_source", formatted_source),
fmt::arg("source_volume", source_vol),
fmt::arg("source_desc", source_name_));
label_.set_markup(markup);
if (tooltipEnabled()) { if (tooltipEnabled()) {
if (tooltipFormat.empty() && config_["tooltip-format"].isString()) { if (tooltipFormat.empty() && config_["tooltip-format"].isString()) {
@ -354,7 +479,11 @@ auto waybar::modules::Wireplumber::update() -> void {
if (!tooltipFormat.empty()) { if (!tooltipFormat.empty()) {
label_.set_tooltip_text(fmt::format(fmt::runtime(tooltipFormat), label_.set_tooltip_text(fmt::format(fmt::runtime(tooltipFormat),
fmt::arg("node_name", node_name_), fmt::arg("node_name", node_name_),
fmt::arg("volume", vol), fmt::arg("icon", getIcon(vol)))); fmt::arg("volume", vol),
fmt::arg("icon", getIcon(vol)),
fmt::arg("format_source", formatted_source),
fmt::arg("source_volume", source_vol),
fmt::arg("source_desc", source_name_)));
} else { } else {
label_.set_tooltip_text(node_name_); label_.set_tooltip_text(node_name_);
} }