Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/input/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ pub struct Manager {
/// Mapping of target devices to their respective handles
/// E.g. {"/org/shadowblip/InputPlumber/devices/target/dbus0": <Handle>}
target_devices: HashMap<String, TargetDeviceClient>,
/// 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<String, Vec<String>>,
/// List of composite device dbus paths with gamepad devices in player order.
/// E.g. ["/org/shadowblip/InputPlumber/CompositeDevice0"]
target_gamepad_order: Vec<String>,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:?}");
}
Expand Down Expand Up @@ -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<dyn Error>> {
let dev_path = device.devpath();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
25 changes: 25 additions & 0 deletions src/input/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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.
Expand Down Expand Up @@ -490,6 +498,14 @@ impl<T: TargetInputDevice + TargetOutputDevice + Send + 'static> TargetDriver<T>
self.tx.clone().into()
}

/// Returns the serials of any vhci devices the wrapped device generates.
pub fn vhci_device_serials(&self) -> Vec<String> {
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<dyn Error>> {
log::debug!("Started running target device: {}", self.dbus.path());
Expand Down Expand Up @@ -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<String> {
match self {
TargetDevice::SteamDeck(device) => device.vhci_device_serials(),
_ => vec![],
}
}

/// Run the target device
pub async fn run(self) -> Result<(), Box<dyn Error>> {
match self {
Expand Down
5 changes: 5 additions & 0 deletions src/input/target/steam_deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
vec![self.serial_number.clone()]
}

fn get_capabilities(&self) -> Result<Vec<Capability>, InputError> {
Ok(vec![
Capability::Accelerometer(Source::Center),
Expand Down
2 changes: 1 addition & 1 deletion src/udev/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down