From 5c9a1b08d9d635066a7315976525d32bc6c75388 Mon Sep 17 00:00:00 2001 From: PickNicko13 Date: Sun, 21 Dec 2025 23:46:15 +0100 Subject: [PATCH 1/2] Implemented "only-physical" parameter in the wireplumber module. It makes it find the first node that has adevice.id linked to the default output if the default output doesn't have it (i.e. it is a virtual output). Useful if you want to control (and see) the volume of the actual output and not the effect sink. May need some polishing and/or style fixing, but it works. --- include/modules/wireplumber.hpp | 6 ++ src/modules/wireplumber.cpp | 176 +++++++++++++++++++++++++++++--- 2 files changed, 169 insertions(+), 13 deletions(-) diff --git a/include/modules/wireplumber.hpp b/include/modules/wireplumber.hpp index 0565704f..264b3ebe 100644 --- a/include/modules/wireplumber.hpp +++ b/include/modules/wireplumber.hpp @@ -36,6 +36,10 @@ class Wireplumber : public ALabel { static std::list modules; + uint32_t resolvePhysicalSink(uint32_t start_id); + uint32_t findPlaybackNodeId(const gchar* description); + static uint32_t get_linked_sink_id(WpObjectManager* om, uint32_t from_node_id, const gchar* media_class); + WpCore* wp_core_; GPtrArray* apis_; WpObjectManager* om_; @@ -54,6 +58,8 @@ class Wireplumber : public ALabel { bool source_muted_; double source_volume_; gchar* default_source_name_; + bool only_physical_; + bool resolved_physical_; }; } // namespace waybar::modules diff --git a/src/modules/wireplumber.cpp b/src/modules/wireplumber.cpp index bb3005bd..70320fd1 100644 --- a/src/modules/wireplumber.cpp +++ b/src/modules/wireplumber.cpp @@ -1,4 +1,5 @@ #include "modules/wireplumber.hpp" +#include #include @@ -25,7 +26,8 @@ waybar::modules::Wireplumber::Wireplumber(const std::string& id, const Json::Val source_node_id_(0), source_muted_(false), source_volume_(0.0), - default_source_name_(nullptr) { + default_source_name_(nullptr), + only_physical_(false) { waybar::modules::Wireplumber::modules.push_back(this); wp_init(WP_INIT_PIPEWIRE); @@ -35,6 +37,7 @@ waybar::modules::Wireplumber::Wireplumber(const std::string& id, const Json::Val type_ = g_strdup(config_["node-type"].isString() ? config_["node-type"].asString().c_str() : "Audio/Sink"); + only_physical_ = config_["only-physical"].isBool() ? config_["only-physical"].asBool() : false; prepare(this); @@ -226,28 +229,34 @@ void waybar::modules::Wireplumber::onDefaultNodesApiChanged(waybar::modules::Wir spdlog::debug("[{}]: (onDefaultNodesApiChanged: {})", self->name_, self->type_); // Handle sink - uint32_t defaultNodeId; + uint32_t defaultNodeId = 0; g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", self->type_, &defaultNodeId); if (isValidNodeId(defaultNodeId)) { + uint32_t effective_id = self->only_physical_ ? self->resolvePhysicalSink(defaultNodeId) : defaultNodeId; + + if (self->only_physical_ && effective_id != defaultNodeId) { + spdlog::info("[{}]: only-physical enabled: using sink {} instead of default {}", self->name_, effective_id, defaultNodeId); + } + g_autoptr(WpNode) node = static_cast( wp_object_manager_lookup(self->om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", - "=u", defaultNodeId, nullptr)); + "=u", effective_id, nullptr)); if (node != nullptr) { - const gchar* defaultNodeName = + const gchar* effectiveNodeName = wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(node), "node.name"); - 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); + if (g_strcmp0(self->default_node_name_, effectiveNodeName) != 0 || + self->node_id_ != effective_id) { + spdlog::debug("[{}]: Default sink resolved to -> Node(name: {}, id: {})", self->name_, + effectiveNodeName, effective_id); g_free(self->default_node_name_); - self->default_node_name_ = g_strdup(defaultNodeName); - self->node_id_ = defaultNodeId; - updateVolume(self, defaultNodeId); - updateNodeName(self, defaultNodeId); + self->default_node_name_ = g_strdup(effectiveNodeName); + self->node_id_ = effective_id; + updateVolume(self, effective_id); + updateNodeName(self, effective_id); } } } @@ -300,7 +309,16 @@ void waybar::modules::Wireplumber::onObjectManagerInstalled(waybar::modules::Wir // Get default sink g_signal_emit_by_name(self->def_nodes_api_, "get-default-configured-node-name", self->type_, &self->default_node_name_); - g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", self->type_, &self->node_id_); + uint32_t initial_sink_id = 0; + g_signal_emit_by_name(self->def_nodes_api_, "get-default-node", self->type_, &initial_sink_id); + + if (self->only_physical_ && isValidNodeId(initial_sink_id)) { + self->node_id_ = self->resolvePhysicalSink(initial_sink_id); + spdlog::info("[{}]: only-physical enabled: initial physical sink {} (default was {})", + self->name_, self->node_id_, initial_sink_id); + } else { + self->node_id_ = initial_sink_id; + } // Get default source g_signal_emit_by_name(self->def_nodes_api_, "get-default-configured-node-name", "Audio/Source", @@ -360,6 +378,10 @@ void waybar::modules::Wireplumber::prepare(waybar::modules::Wireplumber* self) { "=s", self->type_, nullptr); wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", "=s", "Audio/Source", nullptr); + wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", + "=s", "Stream/Output/Audio", nullptr); + wp_object_manager_add_interest(om_, WP_TYPE_LINK, nullptr); + wp_object_manager_request_object_features(om_, WP_TYPE_GLOBAL_PROXY, WP_OBJECT_FEATURES_ALL); } void waybar::modules::Wireplumber::onDefaultNodesApiLoaded(WpObject* p, GAsyncResult* res, @@ -530,3 +552,131 @@ bool waybar::modules::Wireplumber::handleScroll(GdkEventScroll* e) { } return true; } + +uint32_t waybar::modules::Wireplumber::findPlaybackNodeId(const gchar* description) { + if (!description || *description == '\0') { + return 0; + } + + spdlog::debug("[{}]: Searching playback node with node.description = {}", name_, description); + + g_autoptr(WpIterator) it = wp_object_manager_new_filtered_iterator( + om_, WP_TYPE_NODE, + WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.description", "=s", description, + WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", "=s", "Stream/Output/Audio", + nullptr); + + uint32_t playback_id = 0; + + g_auto(GValue) item = G_VALUE_INIT; + if(wp_iterator_next(it, &item)) { + WpNode* output_node = WP_NODE(g_value_get_object(&item)); + playback_id = wp_proxy_get_bound_id(WP_PROXY(output_node)); + + spdlog::debug("[{}]: Found matching playback node id {}", name_, playback_id); + } + g_value_unset(&item); + + if (playback_id == 0) { + spdlog::debug("[{}]: No playback node found with description '{}'", name_, description); + } + + return playback_id; +} + +uint32_t waybar::modules::Wireplumber::get_linked_sink_id(WpObjectManager* om, uint32_t from_node_id, const gchar* media_class) { + spdlog::debug("DEBUG: Searching links connected to node {}", from_node_id); + + g_autoptr(WpIterator) out_it = wp_object_manager_new_filtered_iterator( + om, WP_TYPE_LINK, + WP_CONSTRAINT_TYPE_PW_PROPERTY, "link.output.node", "=u", from_node_id, + nullptr); + + g_auto(GValue) item = G_VALUE_INIT; + if(wp_iterator_next(out_it, &item)) { + WpLink* link = WP_LINK(g_value_get_object(&item)); + guint32 out_node, out_port, in_node, in_port; + wp_link_get_linked_object_ids(link, &out_node, &out_port, &in_node, &in_port); + + spdlog::debug("Found outgoing link {} -> {}", out_node, in_node); + + g_value_unset(&item); + return in_node; + } + g_value_unset(&item); + + spdlog::debug("No link found from node {}", from_node_id); + return 0; +} + +uint32_t waybar::modules::Wireplumber::resolvePhysicalSink(uint32_t start_id) { + if (!isValidNodeId(start_id) || !only_physical_) { + return start_id; + } + + std::unordered_set visited; + uint32_t current_id = start_id; + int depth = 0; + const int max_depth = 10; + + spdlog::debug("[{}]: Starting physical sink resolution from id {}", name_, start_id); + + while (visited.insert(current_id).second && depth++ < max_depth) { + g_autoptr(WpProxy) proxy = static_cast(wp_object_manager_lookup( + om_, WP_TYPE_GLOBAL_PROXY, + WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", current_id, + WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", "=s", type_, + nullptr)); + + if (!proxy || !WP_IS_PIPEWIRE_OBJECT(proxy)) { + spdlog::warn("[{}]: Node {} not found during resolution", name_, current_id); + break; + } + + g_autoptr(WpProperties) props = wp_pipewire_object_get_properties(WP_PIPEWIRE_OBJECT(proxy)); + if (!props) props = wp_properties_new_empty(); + + const gchar* device_id = wp_properties_get(props, "device.id"); + if (device_id != nullptr) { + spdlog::debug("[{}]: Found physical sink {} (device.id = {})", name_, current_id, device_id); + return current_id; + } + + const gchar* description = wp_properties_get(props, "node.description"); + if (!description || *description == '\0') { + description = wp_properties_get(props, "node.nick"); + } + if (!description || *description == '\0') { + spdlog::debug("[{}]: Virtual node {} has no description/nick, stopping", name_, current_id); + break; + } + + spdlog::debug("[{}]: Node {} is virtual (description: {}), searching playback node", name_, current_id, description); + + uint32_t playback_id = findPlaybackNodeId(description); + if (playback_id == 0) { + spdlog::debug("[{}]: No playback node found, cannot resolve further", name_, current_id); + break; + } + + // Follow outgoing link from playback node + uint32_t next_id; + next_id = get_linked_sink_id(om_, playback_id, type_); + if (next_id != 0) { + spdlog::debug("[{}]: Found linked node {} via link traversal", name_, next_id); + } else { + spdlog::debug("[{}]: No links found from/to playback node {}", name_, playback_id); + break; + } + + current_id = next_id; + spdlog::debug("[{}]: Resolved next sink id {}", name_, current_id); + } + + if (depth >= max_depth || !visited.insert(current_id).second) { + spdlog::warn("[{}]: Max depth or cycle reached, stopping at {}", name_, current_id); + } + + spdlog::info("[{}]: Final resolved sink id {}", name_, current_id); + return current_id; +} From a2d597f17fada1edd479f8ade866aaa66514d7d4 Mon Sep 17 00:00:00 2001 From: PickNicko13 Date: Thu, 1 Jan 2026 16:54:54 +0100 Subject: [PATCH 2/2] Added better logic for different kinds of filter nodes for only-physical. Added fallback in case the final node doesn't have the volume property. --- include/modules/wireplumber.hpp | 3 +- src/modules/wireplumber.cpp | 148 ++++++++++++++++++++++++-------- 2 files changed, 114 insertions(+), 37 deletions(-) diff --git a/include/modules/wireplumber.hpp b/include/modules/wireplumber.hpp index 264b3ebe..f227145c 100644 --- a/include/modules/wireplumber.hpp +++ b/include/modules/wireplumber.hpp @@ -38,7 +38,8 @@ class Wireplumber : public ALabel { uint32_t resolvePhysicalSink(uint32_t start_id); uint32_t findPlaybackNodeId(const gchar* description); - static uint32_t get_linked_sink_id(WpObjectManager* om, uint32_t from_node_id, const gchar* media_class); + uint32_t get_linked_sink_id(WpObjectManager* om, uint32_t from_node_id); + uint32_t get_linked_node_from_output_ports(WpObjectManager* om, uint32_t from_node_id); WpCore* wp_core_; GPtrArray* apis_; diff --git a/src/modules/wireplumber.cpp b/src/modules/wireplumber.cpp index 70320fd1..bbce1b53 100644 --- a/src/modules/wireplumber.cpp +++ b/src/modules/wireplumber.cpp @@ -374,14 +374,17 @@ void waybar::modules::Wireplumber::activatePlugins() { void waybar::modules::Wireplumber::prepare(waybar::modules::Wireplumber* self) { spdlog::debug("[{}]: preparing object manager: '{}'", name_, self->type_); - wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", - "=s", self->type_, nullptr); - wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", - "=s", "Audio/Source", nullptr); - wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", - "=s", "Stream/Output/Audio", nullptr); - wp_object_manager_add_interest(om_, WP_TYPE_LINK, nullptr); - wp_object_manager_request_object_features(om_, WP_TYPE_GLOBAL_PROXY, WP_OBJECT_FEATURES_ALL); + if(only_physical_){ + wp_object_manager_add_interest(om_, WP_TYPE_NODE, nullptr); + wp_object_manager_add_interest(om_, WP_TYPE_LINK, nullptr); + wp_object_manager_add_interest(om_, WP_TYPE_PORT, nullptr); + wp_object_manager_request_object_features(om_, WP_TYPE_GLOBAL_PROXY, WP_OBJECT_FEATURES_ALL); + } else { + wp_object_manager_add_interest(om_, WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", + "=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, @@ -553,12 +556,14 @@ bool waybar::modules::Wireplumber::handleScroll(GdkEventScroll* e) { return true; } +// Finds the output node for filter chains defined in pipewire, +// since their input nodes are NOT providing actual outputs uint32_t waybar::modules::Wireplumber::findPlaybackNodeId(const gchar* description) { if (!description || *description == '\0') { return 0; } - spdlog::debug("[{}]: Searching playback node with node.description = {}", name_, description); + spdlog::debug("[{}]: Searching for playback node with node.description = {}", name_, description); g_autoptr(WpIterator) it = wp_object_manager_new_filtered_iterator( om_, WP_TYPE_NODE, @@ -584,8 +589,8 @@ uint32_t waybar::modules::Wireplumber::findPlaybackNodeId(const gchar* descripti return playback_id; } -uint32_t waybar::modules::Wireplumber::get_linked_sink_id(WpObjectManager* om, uint32_t from_node_id, const gchar* media_class) { - spdlog::debug("DEBUG: Searching links connected to node {}", from_node_id); +uint32_t waybar::modules::Wireplumber::get_linked_sink_id(WpObjectManager* om, uint32_t from_node_id) { + spdlog::debug("[{}]: Searching for links connected to node {}", name_, from_node_id); g_autoptr(WpIterator) out_it = wp_object_manager_new_filtered_iterator( om, WP_TYPE_LINK, @@ -598,14 +603,75 @@ uint32_t waybar::modules::Wireplumber::get_linked_sink_id(WpObjectManager* om, u guint32 out_node, out_port, in_node, in_port; wp_link_get_linked_object_ids(link, &out_node, &out_port, &in_node, &in_port); - spdlog::debug("Found outgoing link {} -> {}", out_node, in_node); + spdlog::debug("[{}]: Found outgoing link {} -> {}", name_, out_node, in_node); g_value_unset(&item); return in_node; } g_value_unset(&item); - spdlog::debug("No link found from node {}", from_node_id); + spdlog::debug("[{}]: No links found connected to node {}", name_, from_node_id); + return 0; +} + +// Follow non-monitor output ports to the next node +uint32_t waybar::modules::Wireplumber::get_linked_node_from_output_ports(WpObjectManager* om, uint32_t from_node_id) { + spdlog::debug("[{}]: Searching for non-monitor output ports on node {}", name_, from_node_id); + + g_autoptr(WpIterator) port_it = wp_object_manager_new_filtered_iterator( + om, WP_TYPE_PORT, + WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.id", "=u", from_node_id, + WP_CONSTRAINT_TYPE_PW_PROPERTY, "port.direction", "=s", "out", + nullptr); + + g_auto(GValue) port_item = G_VALUE_INIT; + while (wp_iterator_next(port_it, &port_item)) { + WpPort* port = WP_PORT(g_value_get_object(&port_item)); + + g_autoptr(WpProperties) port_props = wp_pipewire_object_get_properties(WP_PIPEWIRE_OBJECT(port)); + if (!port_props) { + g_value_unset(&port_item); + continue; + } + + const gchar* name = wp_properties_get(port_props, "port.name"); + + // WP_CONSTRAINT_VERB_MATCHES uses GPatternSpec and it is glob-like. Unfortunately, there is no way to + // express "not beginning with a string" in glob-style regex. Or at least I didn't figure out how to do that. + // Therefore, just filter them out with a conditional. Performance difference should be negligible anyway. + if (!name || g_str_has_prefix(name, "monitor_")) { + g_value_unset(&port_item); + continue; + } + + spdlog::debug("[{}]: Found non-monitor output port with name '{}'", name_, name); + + // Find outgoing link from this port + uint32_t port_id = wp_proxy_get_bound_id(WP_PROXY(port)); + + g_autoptr(WpIterator) link_it = wp_object_manager_new_filtered_iterator( + om, WP_TYPE_LINK, + WP_CONSTRAINT_TYPE_PW_PROPERTY, "link.output.port", "=u", port_id, + nullptr); + + g_auto(GValue) link_item = G_VALUE_INIT; + if (wp_iterator_next(link_it, &link_item)) { + WpLink* link = WP_LINK(g_value_get_object(&link_item)); + guint32 out_node, out_port, in_node, in_port; + wp_link_get_linked_object_ids(link, &out_node, &out_port, &in_node, &in_port); + + spdlog::debug("[{}]: Found link from port {} (node {}) -> node {}", name_, port_id, from_node_id, in_node); + g_value_unset(&link_item); + g_value_unset(&port_item); + return in_node; + } + g_value_unset(&link_item); + + g_value_unset(&port_item); + } + g_value_unset(&port_item); + + spdlog::debug("[{}]: No non-monitor output ports with links on node {}", name_, from_node_id); return 0; } @@ -621,11 +687,11 @@ uint32_t waybar::modules::Wireplumber::resolvePhysicalSink(uint32_t start_id) { spdlog::debug("[{}]: Starting physical sink resolution from id {}", name_, start_id); + // Follow the output node chain until a physical device is found while (visited.insert(current_id).second && depth++ < max_depth) { g_autoptr(WpProxy) proxy = static_cast(wp_object_manager_lookup( om_, WP_TYPE_GLOBAL_PROXY, WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", current_id, - WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class", "=s", type_, nullptr)); if (!proxy || !WP_IS_PIPEWIRE_OBJECT(proxy)) { @@ -633,50 +699,60 @@ uint32_t waybar::modules::Wireplumber::resolvePhysicalSink(uint32_t start_id) { break; } + // 1: If it has a device.id, we found the physical sink g_autoptr(WpProperties) props = wp_pipewire_object_get_properties(WP_PIPEWIRE_OBJECT(proxy)); if (!props) props = wp_properties_new_empty(); const gchar* device_id = wp_properties_get(props, "device.id"); if (device_id != nullptr) { spdlog::debug("[{}]: Found physical sink {} (device.id = {})", name_, current_id, device_id); - return current_id; - } - - const gchar* description = wp_properties_get(props, "node.description"); - if (!description || *description == '\0') { - description = wp_properties_get(props, "node.nick"); - } - if (!description || *description == '\0') { - spdlog::debug("[{}]: Virtual node {} has no description/nick, stopping", name_, current_id); break; } - spdlog::debug("[{}]: Node {} is virtual (description: {}), searching playback node", name_, current_id, description); + spdlog::debug("[{}]: Node {} is virtual, trying direct output ports", name_, current_id); + + // 2: Try following non-monitor output ports + uint32_t next_id = get_linked_node_from_output_ports(om_, current_id); + if (next_id != 0) { + spdlog::debug("[{}]: Following direct output port link to node {}", name_, next_id); + current_id = next_id; + continue; + } + + // 3: Search for audio stream node + // (pipewire filter chains create a node for input and a separate node for output) + const gchar* description = wp_properties_get(props, "node.description"); + if (!description || *description == '\0') { + spdlog::warn("[{}]: Virtual node {} has no description/nick - cannot search playback node", name_, current_id); + break; + } + + spdlog::debug("[{}]: No direct output ports, searching playback node for description '{}'", name_, description); uint32_t playback_id = findPlaybackNodeId(description); if (playback_id == 0) { - spdlog::debug("[{}]: No playback node found, cannot resolve further", name_, current_id); + spdlog::warn("[{}]: No playback node found for virtual sink {} - stopping at virtual sink", name_, current_id); break; } - // Follow outgoing link from playback node - uint32_t next_id; - next_id = get_linked_sink_id(om_, playback_id, type_); - if (next_id != 0) { - spdlog::debug("[{}]: Found linked node {} via link traversal", name_, next_id); - } else { - spdlog::debug("[{}]: No links found from/to playback node {}", name_, playback_id); + next_id = get_linked_sink_id(om_, playback_id); + if (next_id == 0) { + spdlog::warn("[{}]: Playback node {} has no outgoing links - stopping at virtual sink {}", name_, playback_id, current_id); break; } + spdlog::debug("[{}]: Following playback node link to node {}", name_, next_id); current_id = next_id; - spdlog::debug("[{}]: Resolved next sink id {}", name_, current_id); } - if (depth >= max_depth || !visited.insert(current_id).second) { - spdlog::warn("[{}]: Max depth or cycle reached, stopping at {}", name_, current_id); - } + GVariant* variant = nullptr; + g_signal_emit_by_name(mixer_api_, "get-volume", current_id, &variant); + + if (variant == nullptr) { + spdlog::warn("[{}]: Node {} does not support volume - fallback to default sink id", name_, current_id); + current_id = start_id; + } spdlog::info("[{}]: Final resolved sink id {}", name_, current_id); return current_id; }