138 lines
3.9 KiB
Rust
138 lines
3.9 KiB
Rust
/// USB metadata copied from udev for durable controller matching.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct UsbDeviceIdentity {
|
|
pub vendor_id: String,
|
|
pub product_id: String,
|
|
pub serial: Option<String>,
|
|
pub path: Option<String>,
|
|
}
|
|
|
|
/// One currently visible MIDI port and any durable metadata available for it.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PortIdentity {
|
|
pub backend_id: String,
|
|
pub name: String,
|
|
pub usb: Option<UsbDeviceIdentity>,
|
|
}
|
|
|
|
/// Persisted hints used to find a configured surface without relying on order.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct DeviceMatch {
|
|
pub port_name: String,
|
|
pub vendor_id: Option<String>,
|
|
pub product_id: Option<String>,
|
|
pub serial: Option<String>,
|
|
pub path: Option<String>,
|
|
}
|
|
|
|
/// Evidence that made one current port unambiguous.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum MatchBasis {
|
|
Serial,
|
|
PhysicalPath,
|
|
UniqueModel,
|
|
Manual,
|
|
}
|
|
|
|
/// Result of matching one configured surface against current ports.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub enum PortResolution {
|
|
Matched { index: usize, basis: MatchBasis },
|
|
Missing,
|
|
Ambiguous { candidate_indices: Vec<usize> },
|
|
}
|
|
|
|
/// Resolves durable hints, optionally accepting an explicit current backend ID.
|
|
#[must_use]
|
|
pub fn resolve_port(
|
|
expected: &DeviceMatch,
|
|
ports: &[PortIdentity],
|
|
manual_backend_id: Option<&str>,
|
|
) -> PortResolution {
|
|
let candidates: Vec<usize> = ports
|
|
.iter()
|
|
.enumerate()
|
|
.filter_map(|(index, port)| model_matches(expected, port).then_some(index))
|
|
.collect();
|
|
|
|
if let Some(serial) = expected.serial.as_deref() {
|
|
let serial_matches = candidates_matching(&candidates, ports, |usb| {
|
|
usb.serial.as_deref() == Some(serial)
|
|
});
|
|
return finish(serial_matches, ports, manual_backend_id, MatchBasis::Serial);
|
|
}
|
|
if let Some(path) = expected.path.as_deref() {
|
|
let path_matches =
|
|
candidates_matching(&candidates, ports, |usb| usb.path.as_deref() == Some(path));
|
|
return finish(
|
|
path_matches,
|
|
ports,
|
|
manual_backend_id,
|
|
MatchBasis::PhysicalPath,
|
|
);
|
|
}
|
|
finish(
|
|
candidates,
|
|
ports,
|
|
manual_backend_id,
|
|
MatchBasis::UniqueModel,
|
|
)
|
|
}
|
|
|
|
fn model_matches(expected: &DeviceMatch, port: &PortIdentity) -> bool {
|
|
if port.name != expected.port_name {
|
|
return false;
|
|
}
|
|
match (&expected.vendor_id, &expected.product_id) {
|
|
(Some(vendor), Some(product)) => port.usb.as_ref().is_some_and(|usb| {
|
|
usb.vendor_id.eq_ignore_ascii_case(vendor)
|
|
&& usb.product_id.eq_ignore_ascii_case(product)
|
|
}),
|
|
(None, None) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
fn candidates_matching(
|
|
candidates: &[usize],
|
|
ports: &[PortIdentity],
|
|
predicate: impl Fn(&UsbDeviceIdentity) -> bool,
|
|
) -> Vec<usize> {
|
|
candidates
|
|
.iter()
|
|
.copied()
|
|
.filter(|index| ports[*index].usb.as_ref().is_some_and(&predicate))
|
|
.collect()
|
|
}
|
|
|
|
fn finish(
|
|
candidates: Vec<usize>,
|
|
ports: &[PortIdentity],
|
|
manual_backend_id: Option<&str>,
|
|
automatic_basis: MatchBasis,
|
|
) -> PortResolution {
|
|
match candidates.as_slice() {
|
|
[] => PortResolution::Missing,
|
|
[index] => PortResolution::Matched {
|
|
index: *index,
|
|
basis: automatic_basis,
|
|
},
|
|
_ => manual_backend_id
|
|
.and_then(|selected| {
|
|
candidates
|
|
.iter()
|
|
.copied()
|
|
.find(|index| ports[*index].backend_id == selected)
|
|
})
|
|
.map_or_else(
|
|
|| PortResolution::Ambiguous {
|
|
candidate_indices: candidates,
|
|
},
|
|
|index| PortResolution::Matched {
|
|
index,
|
|
basis: MatchBasis::Manual,
|
|
},
|
|
),
|
|
}
|
|
}
|