diff --git a/Cargo.lock b/Cargo.lock index 025dfdf..b3ca42c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2205,7 +2205,7 @@ dependencies = [ [[package]] name = "maolan-editor" -version = "0.1.5" +version = "0.1.6" dependencies = [ "alsa", "maolan-engine", @@ -2221,6 +2221,7 @@ dependencies = [ "serde_json", "symphonia", "tokio", + "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 2ad65d9..bc7f74a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "maolan-editor" -version = "0.1.5" +version = "0.1.6" edition = "2024" description = "Simple audio editor for Maolan audio files" license = "BSD-2-Clause" @@ -29,7 +29,7 @@ standalone = [ "dep:oxideav-ogg", "dep:symphonia", ] -embedded = ["dep:maolan-engine", "maolan-engine?/audio"] +embedded = [] [dependencies] maolan-engine = { version = "0.4.24", optional = true } @@ -44,6 +44,7 @@ rfd = "0.18.0" serde = { version = "1.0", features = ["derive"] } rubato = "5.1.0" serde_json = "1.0.154" +toml = "1.2.1" tokio = { version = "1.54.1", features = ["sync", "time"] } [target.'cfg(target_os = "linux")'.dependencies] diff --git a/src/preferences.rs b/src/preferences.rs index a080e37..6891730 100644 --- a/src/preferences.rs +++ b/src/preferences.rs @@ -1,6 +1,93 @@ use std::path::PathBuf; -pub(crate) use maolan_engine::preferences::AudioDevicePreferences as EditorPreferences; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default)] +pub(crate) struct EditorPreferences { + default_output_device_id: Option, + default_input_device_id: Option, +} + +impl EditorPreferences { + pub(crate) fn from_toml_str(contents: &str) -> Result { + toml::from_str(contents) + } + + pub(crate) fn default_output_device_id(&self) -> Option<&str> { + self.default_output_device_id.as_deref() + } + + pub(crate) fn default_input_device_id(&self) -> Option<&str> { + self.default_input_device_id.as_deref() + } + + #[cfg(target_os = "freebsd")] + pub(crate) fn set_default_output_device_id(&mut self, id: Option) { + self.default_output_device_id = id; + } + + #[cfg(target_os = "freebsd")] + pub(crate) fn set_default_input_device_id(&mut self, id: Option) { + self.default_input_device_id = id; + } + + #[cfg(target_os = "freebsd")] + fn update_toml_str(&self, contents: &str) -> Result { + let mut value = if contents.trim().is_empty() { + toml::Value::Table(toml::map::Map::new()) + } else { + toml::from_str::(contents).map_err(|error| error.to_string())? + }; + let table = value + .as_table_mut() + .ok_or_else(|| String::from("Audio device settings require a TOML table."))?; + update_id( + table, + "default_output_device_id", + &self.default_output_device_id, + ); + update_id( + table, + "default_input_device_id", + &self.default_input_device_id, + ); + toml::to_string_pretty(&value).map_err(|error| error.to_string()) + } + + fn load_from_path(path: &std::path::Path) -> Self { + std::fs::read_to_string(path) + .ok() + .and_then(|contents| Self::from_toml_str(&contents).ok()) + .unwrap_or_default() + } + + #[cfg(target_os = "freebsd")] + fn save_to_path(&self, path: &std::path::Path) -> Result<(), String> { + let contents = match std::fs::read_to_string(path) { + Ok(contents) => contents, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => String::new(), + Err(error) => return Err(error.to_string()), + }; + let updated = self.update_toml_str(&contents)?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|error| error.to_string())?; + } + std::fs::write(path, updated).map_err(|error| error.to_string()) + } +} + +#[cfg(target_os = "freebsd")] +fn update_id(table: &mut toml::map::Map, key: &str, id: &Option) { + if let Some(id) = id + .as_deref() + .filter(|id| !id.is_empty() && *id != "__auto__") + { + table.insert(key.to_string(), toml::Value::String(id.to_string())); + } else { + table.remove(key); + } +} pub(crate) fn load() -> EditorPreferences { let Some(config_path) = edit_config_path() else {