From 6a755ad0682b6a3d9f8c3a6fc9df2d9995a823a6 Mon Sep 17 00:00:00 2001 From: arabian Date: Sun, 5 Oct 2025 23:26:29 +0300 Subject: [PATCH] use device name instead of node id to get audio device --- src/bin/cli.rs | 4 ++-- src/gui/draw.rs | 6 +++--- src/gui/mod.rs | 6 +++--- src/types/audio_player.rs | 8 ++++---- src/types/commands.rs | 13 ++++++++----- src/types/config.rs | 2 +- src/types/gui.rs | 4 ++-- src/types/socket.rs | 4 ++-- src/utils/commands.rs | 9 ++------- src/utils/gui.rs | 17 ++++++----------- src/utils/pipewire.rs | 4 ++-- 11 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/bin/cli.rs b/src/bin/cli.rs index 14db786..adbae5b 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -72,7 +72,7 @@ enum SetCommands { /// Playback position Position { position: f32 }, /// Input - Input { id: u32 }, + Input { name: String }, } #[tokio::main] @@ -102,7 +102,7 @@ async fn main() -> Result<(), Box> { Commands::Set { parameter } => match parameter { SetCommands::Volume { volume } => Request::set_volume(volume), SetCommands::Position { position } => Request::seek(position), - SetCommands::Input { id } => Request::set_input(id), + SetCommands::Input { name } => Request::set_input(&name), }, }; diff --git a/src/gui/draw.rs b/src/gui/draw.rs index d34a781..8062e9e 100644 --- a/src/gui/draw.rs +++ b/src/gui/draw.rs @@ -287,7 +287,7 @@ impl SoundpadGui { ui.add_space(5.0); ui.horizontal_top(|ui| { // ---------- Microphone selection ---------- - let mut mics: Vec<(&u32, &String)> = + let mut mics: Vec<(&String, &String)> = self.audio_player_state.all_inputs.iter().collect(); mics.sort_by_key(|(k, _)| *k); @@ -301,8 +301,8 @@ impl SoundpadGui { .unwrap_or(&String::new()), ) .show_ui(ui, |ui| { - for (index, device) in mics { - ui.selectable_value(&mut selected_input, index.to_owned(), device); + for (name, nick) in mics { + ui.selectable_value(&mut selected_input, name.to_owned(), nick); } }); diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 1ce2711..f018aee 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -92,12 +92,12 @@ impl SoundpadGui { make_request_sync(Request::play(path.to_str().unwrap())).ok(); } - pub fn set_input(&mut self, id: u32) { - make_request_sync(Request::set_input(id)).ok(); + pub fn set_input(&mut self, name: String) { + make_request_sync(Request::set_input(&name)).ok(); if self.config.save_input { let mut daemon_config = get_daemon_config(); - daemon_config.default_input_id = Some(id); + daemon_config.default_input_name = Some(name); daemon_config.save_to_file().ok(); } } diff --git a/src/types/audio_player.rs b/src/types/audio_player.rs index 9738a18..b00a9ef 100644 --- a/src/types/audio_player.rs +++ b/src/types/audio_player.rs @@ -40,8 +40,8 @@ impl AudioPlayer { let daemon_config = get_daemon_config(); let default_volume = daemon_config.default_volume.unwrap_or(1.0); let mut default_input_device: Option = None; - if let Some(id) = daemon_config.default_input_id - && let Ok(device) = get_device(id).await + if let Some(name) = daemon_config.default_input_name + && let Ok(device) = get_device(&name).await && device.device_type == DeviceType::Input { default_input_device = Some(device); @@ -224,8 +224,8 @@ impl AudioPlayer { &self.current_file_path } - pub async fn set_current_input_device(&mut self, id: u32) -> Result<(), Box> { - let input_device = get_device(id).await?; + pub async fn set_current_input_device(&mut self, name: &str) -> Result<(), Box> { + let input_device = get_device(name).await?; if input_device.device_type != DeviceType::Input { return Err("Selected device is not an input device".into()); diff --git a/src/types/commands.rs b/src/types/commands.rs index 4e90645..3ac6801 100644 --- a/src/types/commands.rs +++ b/src/types/commands.rs @@ -47,7 +47,7 @@ pub struct GetCurrentInputCommand {} pub struct GetAllInputsCommand {} pub struct SetCurrentInputCommand { - pub id: Option, + pub name: Option, } #[async_trait] @@ -192,7 +192,10 @@ impl Executable for GetCurrentInputCommand { async fn execute(&self) -> Response { let audio_player = get_audio_player().await.lock().await; if let Some(input_device) = &audio_player.current_input_device { - Response::new(true, format!("{} - {}", input_device.id, input_device.nick)) + Response::new( + true, + format!("{} - {}", input_device.name, input_device.nick), + ) } else { Response::new(false, "No input device selected") } @@ -209,7 +212,7 @@ impl Executable for GetAllInputsCommand { continue; } - let string = format!("{} - {}", device.id, device.nick); + let string = format!("{} - {}", device.name, device.nick); input_devices_strings.push(string); } let response_message = input_devices_strings.join("; "); @@ -221,9 +224,9 @@ impl Executable for GetAllInputsCommand { #[async_trait] impl Executable for SetCurrentInputCommand { async fn execute(&self) -> Response { - if let Some(id) = self.id { + if let Some(name) = &self.name { let mut audio_player = get_audio_player().await.lock().await; - match audio_player.set_current_input_device(id).await { + match audio_player.set_current_input_device(name).await { Ok(_) => Response::new(true, "Input device was set"), Err(err) => Response::new(false, err.to_string()), } diff --git a/src/types/config.rs b/src/types/config.rs index 9eead72..e2bdb8a 100644 --- a/src/types/config.rs +++ b/src/types/config.rs @@ -4,7 +4,7 @@ use std::{collections::HashSet, error::Error, fs, path::PathBuf}; #[derive(Default, Clone, Serialize, Deserialize)] pub struct DaemonConfig { - pub default_input_id: Option, + pub default_input_name: Option, pub default_volume: Option, } diff --git a/src/types/gui.rs b/src/types/gui.rs index 9197608..354099d 100644 --- a/src/types/gui.rs +++ b/src/types/gui.rs @@ -34,6 +34,6 @@ pub struct AudioPlayerState { pub new_position: Option, pub duration: f32, - pub current_input: u32, - pub all_inputs: HashMap, + pub current_input: String, + pub all_inputs: HashMap, } diff --git a/src/types/socket.rs b/src/types/socket.rs index ab2f8d9..b3bb072 100644 --- a/src/types/socket.rs +++ b/src/types/socket.rs @@ -80,8 +80,8 @@ impl Request { Request::new("seek", vec![("position", &position.to_string())]) } - pub fn set_input(id: u32) -> Self { - Request::new("set_input", vec![("input_id", &id.to_string())]) + pub fn set_input(name: &str) -> Self { + Request::new("set_input", vec![("input_name", name)]) } } diff --git a/src/utils/commands.rs b/src/utils/commands.rs index 32e6cba..7552fb5 100644 --- a/src/utils/commands.rs +++ b/src/utils/commands.rs @@ -44,13 +44,8 @@ pub fn parse_command(request: &Request) -> Option> { "get_input" => Some(Box::new(GetCurrentInputCommand {})), "get_inputs" => Some(Box::new(GetAllInputsCommand {})), "set_input" => { - let id = request - .args - .get("input_id") - .unwrap_or(&String::new()) - .parse::() - .ok(); - Some(Box::new(SetCurrentInputCommand { id })) + let name = Some(request.args.get("input_name").unwrap_or(&String::new())).cloned(); + Some(Box::new(SetCurrentInputCommand { name })) } _ => None, } diff --git a/src/utils/gui.rs b/src/utils/gui.rs index 1f836fc..060abd4 100644 --- a/src/utils/gui.rs +++ b/src/utils/gui.rs @@ -96,10 +96,8 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc>() .first() .unwrap() - .to_string() - .parse::() - .unwrap_or_default(), - false => 0, + .to_string(), + false => String::new(), }; let all_inputs = match all_inputs_res.status { true => all_inputs_res @@ -111,14 +109,11 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc() - .ok() - .map(|key| (key, v.trim().to_string())) - }) + entry + .split_once(" - ") + .map(|(k, v)| (k.trim().to_string(), v.trim().to_string())) }) - .collect(), + .collect::>(), false => HashMap::new(), }; diff --git a/src/utils/pipewire.rs b/src/utils/pipewire.rs index d5807a6..2c82c09 100644 --- a/src/utils/pipewire.rs +++ b/src/utils/pipewire.rs @@ -199,12 +199,12 @@ pub async fn get_all_devices() -> Result<(Vec, Vec), B } } -pub async fn get_device(node_id: u32) -> Result> { +pub async fn get_device(device_name: &str) -> Result> { let (mut input_devices, output_devices) = get_all_devices().await?; input_devices.extend(output_devices); for device in input_devices { - if device.id == node_id { + if device.name == device_name { return Ok(device); } }