use device name instead of node id to get audio device

This commit is contained in:
2025-10-05 23:26:29 +03:00
parent 7809a8c9ff
commit 6a755ad068
11 changed files with 35 additions and 42 deletions
+4 -4
View File
@@ -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<AudioDevice> = 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<dyn Error>> {
let input_device = get_device(id).await?;
pub async fn set_current_input_device(&mut self, name: &str) -> Result<(), Box<dyn Error>> {
let input_device = get_device(name).await?;
if input_device.device_type != DeviceType::Input {
return Err("Selected device is not an input device".into());
+8 -5
View File
@@ -47,7 +47,7 @@ pub struct GetCurrentInputCommand {}
pub struct GetAllInputsCommand {}
pub struct SetCurrentInputCommand {
pub id: Option<u32>,
pub name: Option<String>,
}
#[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()),
}
+1 -1
View File
@@ -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<u32>,
pub default_input_name: Option<String>,
pub default_volume: Option<f32>,
}
+2 -2
View File
@@ -34,6 +34,6 @@ pub struct AudioPlayerState {
pub new_position: Option<f32>,
pub duration: f32,
pub current_input: u32,
pub all_inputs: HashMap<u32, String>,
pub current_input: String,
pub all_inputs: HashMap<String, String>,
}
+2 -2
View File
@@ -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)])
}
}