VNOX-Client/src/ui/devices.rs
loki5512344 3b95df9e9c
fix(client): list only real audio devices and persist the choice by id
The device pickers showed every ALSA PCM plugin (Discard all samples, Rate
Converter, JACK, Open Sound System, surround/dmix aliases). Enumeration now
goes through a pure filter that keeps the system default, the PipeWire and
PulseAudio servers and one entry per hardware endpoint, with technical tails
removed and a stable sort.

Selections are stored by device id instead of list position, so the filter
and hotplug cannot move a saved choice. Indexes saved by older versions are
migrated by device name; a missing device falls back to the default.

The same preferences change records whether the voice mode was chosen by
the person: a profile that only carries the old push-to-talk default now
starts in voice activation, explicit choices are kept.
2026-10-25 10:03:20 +01:00

69 lines
2.3 KiB
Rust

//! Bridges the persisted device ids and the positions of the settings pickers.
use vnox_client::audio::devices::{self, filter};
use super::state::UiState;
/// Recomputes the picker positions from the persisted ids and the last enumeration.
pub fn sync_indexes(ui: &mut UiState) {
ui.mic_device = filter::index_for_id(&ui.device_lists.input, &ui.mic_device_id);
ui.output_device = filter::index_for_id(&ui.device_lists.output, &ui.output_device_id);
}
/// Enumerates devices again (hardware may have changed) and keeps the saved choices.
pub fn refresh(ui: &mut UiState) {
match devices::enumerate() {
Ok(lists) => ui.device_lists = lists,
Err(error) => tracing::warn!(%error, "cannot enumerate audio devices"),
}
sync_indexes(ui);
}
/// Applies picker positions chosen by the person, storing the matching stable ids.
pub fn select(ui: &mut UiState, input_index: usize, output_index: usize) {
ui.mic_device_id = filter::id_for_index(&ui.device_lists.input, input_index);
ui.output_device_id = filter::id_for_index(&ui.device_lists.output, output_index);
sync_indexes(ui);
}
#[cfg(test)]
mod tests {
use super::*;
use vnox_client::audio::devices::{DeviceEntry, DeviceLists};
fn entry(id: &str, label: &str) -> DeviceEntry {
DeviceEntry {
id: id.into(),
label: label.into(),
}
}
fn state() -> UiState {
UiState {
device_lists: DeviceLists {
input: vec![entry("a", "Mic A"), entry("b", "Mic B")],
output: vec![entry("x", "Out X")],
},
..Default::default()
}
}
#[test]
fn selection_is_stored_by_id_and_survives_list_changes() {
let mut ui = state();
select(&mut ui, 2, 1);
assert_eq!((ui.mic_device_id.as_str(), ui.mic_device), ("b", 2));
assert_eq!((ui.output_device_id.as_str(), ui.output_device), ("x", 1));
// A new device appears before the saved one; the choice follows the id.
ui.device_lists.input.insert(0, entry("new", "Aaa"));
sync_indexes(&mut ui);
assert_eq!(ui.mic_device, 3);
}
#[test]
fn missing_device_falls_back_to_default_position() {
let mut ui = state();
ui.mic_device_id = "unplugged".into();
sync_indexes(&mut ui);
assert_eq!(ui.mic_device, 0);
}
}