From 118c1ca28c25a4c92cfacd7063ee4f757a30c662 Mon Sep 17 00:00:00 2001 From: "Tom A. Wagner" Date: Fri, 4 Jun 2021 10:30:31 +0200 Subject: [PATCH] Get node_from, node_to ids from state instead of props when a new link appears. This is more reliable than assuming the link carries the id of its nodes, as there have been cases where a link was created without those properties set. Instead, we can just pull them from the state via the port ids of the link. --- src/application.rs | 44 ++++++++++++++++++++++++++++++++------ src/main.rs | 6 +++++- src/pipewire_connection.rs | 18 ++-------------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/application.rs b/src/application.rs index 222bff0..91dd5b2 100644 --- a/src/application.rs +++ b/src/application.rs @@ -6,7 +6,7 @@ use gtk::{ prelude::*, subclass::prelude::*, }; -use log::{info, warn}; +use log::{error, info, warn}; use pipewire::{channel::Sender, spa::Direction}; use crate::{ @@ -127,7 +127,7 @@ impl Application { name, direction, } => app.add_port(id, name, node_id, direction), - PipewireMessage::LinkAdded { id, link } => app.add_link(id, link), + PipewireMessage::LinkAdded { id, port_from, port_to } => app.add_link(id, port_from, port_to), PipewireMessage::ObjectRemoved { id } => app.remove_global(id), }; Continue(true) @@ -196,23 +196,53 @@ impl Application { } /// Add a new link to the view. - pub fn add_link(&self, id: u32, link: PipewireLink) { + pub fn add_link(&self, id: u32, port_from: u32, port_to: u32) { info!("Adding link to graph: id {}", id); let imp = imp::Application::from_instance(self); + let mut state = imp.state.borrow_mut(); // FIXME: Links should be colored depending on the data they carry (video, audio, midi) like ports are. - imp.state.borrow_mut().insert( + let node_from = *match state.get(port_from) { + Some(Item::Port { node_id }) => node_id, + _ => { + error!( + "Tried to add link (id:{}), but its output port (id:{}) is not known", + id, port_from + ); + return; + } + }; + let node_to = *match state.get(port_to) { + Some(Item::Port { node_id }) => node_id, + _ => { + error!( + "Tried to add link (id:{}), but its input port (id:{}) is not known", + id, port_to + ); + return; + } + }; + + state.insert( id, Item::Link { - output_port: link.port_from, - input_port: link.port_to, + output_port: port_from, + input_port: port_to, }, ); // Update graph to contain the new link. - imp.graphview.add_link(id, link); + imp.graphview.add_link( + id, + PipewireLink { + node_from, + port_from, + node_to, + port_to, + }, + ); } // Toggle a link between the two specified ports on the remote pipewire server. diff --git a/src/main.rs b/src/main.rs index 17fe768..5934f3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,11 @@ enum PipewireMessage { direction: Direction, }, /// A new link has appeared. - LinkAdded { id: u32, link: PipewireLink }, + LinkAdded { + id: u32, + port_from: u32, + port_to: u32, + }, /// An object was removed ObjectRemoved { id: u32 }, } diff --git a/src/pipewire_connection.rs b/src/pipewire_connection.rs index 3251a5f..5fa1db1 100644 --- a/src/pipewire_connection.rs +++ b/src/pipewire_connection.rs @@ -147,21 +147,11 @@ fn handle_link(link: &GlobalObject, sender: &glib::Sender, sender: &glib::Sender