Merge branch 'refactor/pw-cli-parse-seam': pure parse_pw_nodes seam
This commit is contained in:
+43
-36
@@ -14,52 +14,59 @@ impl std::fmt::Display for AudioDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn enumerate_audio_devices() -> Vec<AudioDevice> {
|
pub fn enumerate_audio_devices() -> Vec<AudioDevice> {
|
||||||
let mut devices = Vec::new();
|
|
||||||
let output = Command::new("pw-cli")
|
let output = Command::new("pw-cli")
|
||||||
.arg("list-objects")
|
.arg("list-objects")
|
||||||
.arg("Node")
|
.arg("Node")
|
||||||
.output();
|
.output();
|
||||||
|
|
||||||
if let Ok(out) = output {
|
match output {
|
||||||
let text = String::from_utf8_lossy(&out.stdout);
|
Ok(out) => parse_pw_nodes(&String::from_utf8_lossy(&out.stdout)),
|
||||||
let mut current_name = String::new();
|
Err(_) => Vec::new(),
|
||||||
let mut current_desc = String::new();
|
}
|
||||||
let mut current_class = String::new();
|
}
|
||||||
|
|
||||||
for line in text.lines() {
|
/// Emits the in-progress node as an `AudioDevice` if it's a complete Audio/*
|
||||||
let line = line.trim();
|
/// node, then resets the accumulators for the next block. Non-audio or
|
||||||
if line.starts_with("id ") {
|
/// incomplete blocks are dropped (but still reset).
|
||||||
// Save previous if valid
|
fn push_device(name: &mut String, desc: &mut String, class: &mut String, out: &mut Vec<AudioDevice>) {
|
||||||
if !current_name.is_empty() && current_class.starts_with("Audio/") {
|
if !name.is_empty() && class.starts_with("Audio/") {
|
||||||
devices.push(AudioDevice {
|
out.push(AudioDevice {
|
||||||
name: current_name.clone(),
|
name: name.clone(),
|
||||||
description: if current_desc.is_empty() { current_name.clone() } else { current_desc.clone() },
|
description: if desc.is_empty() { name.clone() } else { desc.clone() },
|
||||||
is_input: current_class == "Audio/Source",
|
is_input: class == "Audio/Source",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
current_name.clear();
|
name.clear();
|
||||||
current_desc.clear();
|
desc.clear();
|
||||||
current_class.clear();
|
class.clear();
|
||||||
} else if let Some(val) = line.strip_prefix("node.name = \"") {
|
}
|
||||||
current_name = val.trim_end_matches('"').to_string();
|
|
||||||
} else if let Some(val) = line.strip_prefix("node.description = \"") {
|
|
||||||
current_desc = val.trim_end_matches('"').to_string();
|
|
||||||
} else if let Some(val) = line.strip_prefix("media.class = \"") {
|
|
||||||
current_class = val.trim_end_matches('"').to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final one
|
/// Parses the text of `pw-cli list-objects Node` into the audio devices we care
|
||||||
if !current_name.is_empty() && current_class.starts_with("Audio/") {
|
/// about. Each object is a block introduced by an `id N, ...` line; within a
|
||||||
devices.push(AudioDevice {
|
/// block we collect `node.name` / `node.description` / `media.class`, and a
|
||||||
name: current_name.clone(),
|
/// device is emitted at the next `id` (and at EOF) when the class is `Audio/*`
|
||||||
description: if current_desc.is_empty() { current_name } else { current_desc },
|
/// (`Audio/Source` => input). Description falls back to the node name when
|
||||||
is_input: current_class == "Audio/Source",
|
/// absent. Returned sorted by description for stable UI display.
|
||||||
});
|
fn parse_pw_nodes(text: &str) -> Vec<AudioDevice> {
|
||||||
|
let mut devices = Vec::new();
|
||||||
|
let mut current_name = String::new();
|
||||||
|
let mut current_desc = String::new();
|
||||||
|
let mut current_class = String::new();
|
||||||
|
|
||||||
|
for line in text.lines() {
|
||||||
|
let line = line.trim();
|
||||||
|
if line.starts_with("id ") {
|
||||||
|
push_device(&mut current_name, &mut current_desc, &mut current_class, &mut devices);
|
||||||
|
} else if let Some(val) = line.strip_prefix("node.name = \"") {
|
||||||
|
current_name = val.trim_end_matches('"').to_string();
|
||||||
|
} else if let Some(val) = line.strip_prefix("node.description = \"") {
|
||||||
|
current_desc = val.trim_end_matches('"').to_string();
|
||||||
|
} else if let Some(val) = line.strip_prefix("media.class = \"") {
|
||||||
|
current_class = val.trim_end_matches('"').to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
push_device(&mut current_name, &mut current_desc, &mut current_class, &mut devices);
|
||||||
|
|
||||||
// Sort devices for consistent display
|
|
||||||
devices.sort_by(|a, b| a.description.cmp(&b.description));
|
devices.sort_by(|a, b| a.description.cmp(&b.description));
|
||||||
devices
|
devices
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user