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; }