129 lines
4.1 KiB
Rust
129 lines
4.1 KiB
Rust
use crate::*;
|
|
use std::path::PathBuf;
|
|
use tokio::sync::{broadcast, watch};
|
|
|
|
pub struct PersistenceManager {
|
|
state: State,
|
|
state_tx: watch::Sender<State>,
|
|
notification_rx: broadcast::Receiver<JackNotification>,
|
|
state_file_path: PathBuf,
|
|
}
|
|
|
|
impl PersistenceManager {
|
|
pub fn new(
|
|
args: &Args,
|
|
notification_rx: broadcast::Receiver<JackNotification>,
|
|
) -> (Self, watch::Receiver<State>) {
|
|
let state_file_path = args.config_file.clone();
|
|
if let Some(parent) = state_file_path.parent() {
|
|
std::fs::create_dir_all(parent).ok(); // Create directory if it doesn't exist
|
|
}
|
|
let initial_state = Self::load_from_disk(&state_file_path).unwrap_or_default();
|
|
let (state_tx, state_rx) = watch::channel(initial_state.clone());
|
|
|
|
let manager = Self {
|
|
state: initial_state,
|
|
state_tx,
|
|
notification_rx,
|
|
state_file_path,
|
|
};
|
|
|
|
(manager, state_rx)
|
|
}
|
|
|
|
pub async fn run(&mut self) -> Result<()> {
|
|
while let Ok(notification) = self.notification_rx.recv().await {
|
|
if let JackNotification::PortConnect {
|
|
port_a,
|
|
port_b,
|
|
connected,
|
|
} = notification
|
|
{
|
|
self.handle_port_connection(port_a, port_b, connected)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn handle_port_connection(
|
|
&mut self,
|
|
port_a: String,
|
|
port_b: String,
|
|
connected: bool,
|
|
) -> Result<()> {
|
|
// Determine which port is ours and which is external
|
|
let (our_port, external_port) = if port_a.starts_with("looper:") {
|
|
(port_a, port_b)
|
|
} else if port_b.starts_with("looper:") {
|
|
(port_b, port_a)
|
|
} else {
|
|
// Neither port is ours, ignore
|
|
return Ok(());
|
|
};
|
|
|
|
// Update the connections state
|
|
let our_port_name = our_port.strip_prefix("looper:").unwrap_or(&our_port);
|
|
let connections = &mut self.state.connections;
|
|
|
|
let port_list = match our_port_name {
|
|
"midi_in" => &mut connections.midi_in,
|
|
"audio_in" => &mut connections.audio_in,
|
|
"audio_out" => &mut connections.audio_out,
|
|
"click_track" => &mut connections.click_track_out,
|
|
_ => {
|
|
log::warn!("Unknown port: {}", our_port_name);
|
|
return Ok(());
|
|
}
|
|
};
|
|
|
|
if connected {
|
|
// Add connection if not already present
|
|
if !port_list.contains(&external_port) {
|
|
port_list.push(external_port.clone());
|
|
log::info!("Added connection: {} -> {}", our_port, external_port);
|
|
}
|
|
} else {
|
|
// Remove connection
|
|
port_list.retain(|p| p != &external_port);
|
|
log::info!("Removed connection: {} -> {}", our_port, external_port);
|
|
}
|
|
|
|
// Broadcast state change
|
|
if let Err(e) = self.state_tx.send(self.state.clone()) {
|
|
log::error!("Failed to broadcast state change: {}", e);
|
|
}
|
|
|
|
// Save to disk
|
|
self.save_to_disk()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn load_from_disk(path: &PathBuf) -> Result<State> {
|
|
match std::fs::read_to_string(path) {
|
|
Ok(content) => {
|
|
let state: State = serde_json::from_str(&content)
|
|
.map_err(|_| LooperError::StateLoad(std::panic::Location::caller()))?;
|
|
log::info!("Loaded state from {:?}", path);
|
|
Ok(state)
|
|
}
|
|
Err(_) => {
|
|
log::info!("Could not load state from {:?}. Using defaults.", path);
|
|
Err(LooperError::StateLoad(std::panic::Location::caller()))
|
|
}
|
|
}
|
|
}
|
|
|
|
fn save_to_disk(&self) -> Result<()> {
|
|
let json = serde_json::to_string_pretty(&self.state)
|
|
.map_err(|_| LooperError::StateSave(std::panic::Location::caller()))?;
|
|
|
|
std::fs::write(&self.state_file_path, json)
|
|
.map_err(|_| LooperError::StateSave(std::panic::Location::caller()))?;
|
|
|
|
log::debug!("Saved state to {:?}", self.state_file_path);
|
|
Ok(())
|
|
}
|
|
}
|