diff --git a/src/input/manager.rs b/src/input/manager.rs index 2f15628b..3c221714 100644 --- a/src/input/manager.rs +++ b/src/input/manager.rs @@ -180,6 +180,11 @@ pub struct Manager { /// Mapping of target devices to their respective handles /// E.g. {"/org/shadowblip/InputPlumber/devices/target/dbus0": } target_devices: HashMap, + /// Serials of vhci devices generated by target devices, keyed by target + /// device path. These devices are denied as source devices to avoid a + /// feedback loop, while external vhci devices (e.g. USB/IP) are managed. + /// E.g. {"/org/shadowblip/InputPlumber/devices/target/deck0": ["0ABC123DEF"]} + target_vhci_serials: HashMap>, /// List of composite device dbus paths with gamepad devices in player order. /// E.g. ["/org/shadowblip/InputPlumber/CompositeDevice0"] target_gamepad_order: Vec, @@ -226,6 +231,7 @@ impl Manager { source_device_dbus_paths: HashMap::new(), source_devices_used: HashMap::new(), target_devices: HashMap::new(), + target_vhci_serials: HashMap::new(), used_configs: HashMap::new(), composite_device_sources: HashMap::new(), composite_device_targets: HashMap::new(), @@ -379,6 +385,7 @@ impl Manager { ManagerCommand::TargetDeviceStopped { path } => { log::debug!("Target device stopped: {path}"); self.target_devices.remove(&path); + self.target_vhci_serials.remove(&path); // Lookup the compoiste device and see if it is suspended? // TODO: Use a different hashmap to map target device to composite device @@ -779,6 +786,14 @@ impl Manager { target_devices.insert(path.clone(), client.clone()); self.target_devices.insert(path.clone(), client.clone()); + // Track any vhci devices this target generates so the udev monitor + // can deny them as source devices (avoiding a feedback loop). This + // must happen before the target runs and creates the device. + let serials = target.vhci_device_serials(); + if !serials.is_empty() { + self.target_vhci_serials.insert(path.clone(), serials); + } + // Run the target device tokio::spawn(async move { if let Err(e) = target.run().await { @@ -1071,6 +1086,7 @@ impl Manager { ); continue; }; + self.target_vhci_serials.remove(target_device_path); if let Err(e) = client.stop().await { log::warn!("Failed to stop target device {target_device_path}: {e:?}"); } @@ -1334,6 +1350,23 @@ impl Manager { } } + /// Returns true if the given device is a vhci device that InputPlumber + /// generated (e.g. a Steam Deck target). These are denied as source + /// devices to avoid a feedback loop, while external vhci devices (e.g. + /// USB/IP) are not in the tracked set and are still managed. + fn is_generated_vhci_device(&self, device: &UdevDevice) -> bool { + let serial = device.serial_number(); + if serial.is_empty() { + return false; + } + if !device.syspath().contains("vhci_hcd") { + return false; + } + self.target_vhci_serials + .values() + .any(|serials| serials.contains(&serial)) + } + /// Called when a new device is detected by udev async fn on_udev_device_added(&mut self, device: UdevDevice) -> Result<(), Box> { let dev_path = device.devpath(); @@ -1417,7 +1450,15 @@ impl Manager { // Check to see if the device should be managed or not 'check_manage: { if !device.is_virtual() { - log::trace!("{dev_name} ({dev_sysname}) is a real device - {dev_path}"); + // Check if the vhci device is generate by inputplumber + if self.is_generated_vhci_device(&device) { + log::debug!( + "{dev_name} ({dev_sysname}) is a generated vhci device, skipping consideration for {dev_path}" + ); + notify_device_added = false; + } else { + log::trace!("{dev_name} ({dev_sysname}) is a real device - {dev_path}"); + } break 'check_manage; } @@ -1467,7 +1508,15 @@ impl Manager { // Check to see if the device should be managed or not 'check_manage: { if !device.is_virtual() { - log::trace!("{dev_name} ({dev_sysname}) is a real device -{dev_path}"); + // Check if the vhci device is generate by inputplumber + if self.is_generated_vhci_device(&device) { + log::debug!( + "{dev_name} ({dev_sysname}) is a generated vhci device, skipping consideration for {dev_path}" + ); + notify_device_added = false; + } else { + log::trace!("{dev_name} ({dev_sysname}) is a real device -{dev_path}"); + } break 'check_manage; } diff --git a/src/input/target/mod.rs b/src/input/target/mod.rs index 5f04c15a..116fddbf 100644 --- a/src/input/target/mod.rs +++ b/src/input/target/mod.rs @@ -362,6 +362,14 @@ pub trait TargetInputDevice { Ok(vec![]) } + /// Returns the serials of any vhci (virtual USB host controller) devices + /// that this target generates. The input manager uses these to deny the + /// generated devices as source devices, avoiding a feedback loop, while + /// still allowing external vhci devices (e.g. USB/IP) to be managed. + fn vhci_device_serials(&self) -> Vec { + vec![] + } + /// Returns scheduled events that should be written later. This function will /// be called every poll iteration by the [TargetDriver] and schedule the /// events to be written at the specified time. @@ -490,6 +498,14 @@ impl TargetDriver self.tx.clone().into() } + /// Returns the serials of any vhci devices the wrapped device generates. + pub fn vhci_device_serials(&self) -> Vec { + match self.implementation.lock() { + Ok(implementation) => implementation.vhci_device_serials(), + Err(_) => vec![], + } + } + /// Run the target device, consuming the device. pub async fn run(mut self) -> Result<(), Box> { log::debug!("Started running target device: {}", self.dbus.path()); @@ -994,6 +1010,15 @@ impl TargetDevice { } } + /// Returns the serials of any vhci devices this target generates. Only + /// vhci-based targets (e.g. the Steam Deck) return a non-empty list. + pub fn vhci_device_serials(&self) -> Vec { + match self { + TargetDevice::SteamDeck(device) => device.vhci_device_serials(), + _ => vec![], + } + } + /// Run the target device pub async fn run(self) -> Result<(), Box> { match self { diff --git a/src/input/target/steam_deck.rs b/src/input/target/steam_deck.rs index 64e24480..3104b570 100644 --- a/src/input/target/steam_deck.rs +++ b/src/input/target/steam_deck.rs @@ -923,6 +923,11 @@ impl TargetInputDevice for SteamDeckDevice { Ok(()) } + /// The Deck target generates a single vhci device identified by its serial. + fn vhci_device_serials(&self) -> Vec { + vec![self.serial_number.clone()] + } + fn get_capabilities(&self) -> Result, InputError> { Ok(vec![ Capability::Accelerometer(Source::Center), diff --git a/src/udev/device.rs b/src/udev/device.rs index 37f6093b..305c94ad 100644 --- a/src/udev/device.rs +++ b/src/udev/device.rs @@ -477,7 +477,7 @@ impl UdevDevice { /// Returns true if this device is virtual pub fn is_virtual(&self) -> bool { - self.syspath().contains("/devices/virtual") || self.syspath().contains("vhci_hcd") + self.syspath().contains("/devices/virtual") } /// Returns the devnode of the device. The devnode is the full path to the