diff --git a/komorebi/src/reaper.rs b/komorebi/src/reaper.rs index c2848733..37f8cd81 100644 --- a/komorebi/src/reaper.rs +++ b/komorebi/src/reaper.rs @@ -134,7 +134,14 @@ fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> } // Save to file - save_known_hwnds(wm.known_hwnds.keys().copied().collect()); + let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); + let file = OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(hwnd_json)?; + + serde_json::to_writer_pretty(&file, &wm.known_hwnds.keys().collect::>())?; } Ok(()) @@ -206,49 +213,3 @@ fn find_orphans() -> color_eyre::Result<()> { } } } - -static PENDING_HWNDS: Mutex>> = Mutex::new(None); -static PENDING_HWNDS_CHANGED: parking_lot::Condvar = parking_lot::Condvar::new(); -static HWNDS_WRITER: std::sync::Once = std::sync::Once::new(); - -/// Saves the list of known handles to `komorebi.hwnd.json`, from a thread of its own: writing the -/// file with the window manager locked made every other thread wait for the disk. Only the latest -/// list is written when several arrive before the writer is done, as the file only ever says what -/// is known now. -pub fn save_known_hwnds(hwnds: Vec) { - *PENDING_HWNDS.lock() = Some(hwnds); - HWNDS_WRITER.call_once(|| { - std::thread::spawn(|| { - loop { - let hwnds = { - let mut pending = PENDING_HWNDS.lock(); - while pending.is_none() { - PENDING_HWNDS_CHANGED.wait(&mut pending); - } - pending.take().unwrap_or_default() - }; - - let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); - match OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(hwnd_json) - { - Ok(file) => { - if let Err(error) = serde_json::to_writer_pretty(&file, &hwnds) { - tracing::error!( - "Failed to save list of known_hwnds on file: {}", - error - ); - } - } - Err(error) => { - tracing::error!("Failed to save list of known_hwnds on file: {}", error); - } - } - } - }); - }); - PENDING_HWNDS_CHANGED.notify_one(); -} diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 402af904..874d0af1 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use std::collections::HashSet; use std::collections::VecDeque; use std::env::temp_dir; +use std::fs::OpenOptions; use std::io::ErrorKind; use std::net::Shutdown; use std::num::NonZeroUsize; @@ -4008,8 +4009,25 @@ impl WindowManager { *reaper_cache = known_hwnds.clone(); } - // Save to file, from the writer thread of the reaper - crate::reaper::save_known_hwnds(known_hwnds.keys().copied().collect()); + // Save to file + let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); + match OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(hwnd_json) + { + Ok(file) => { + if let Err(error) = + serde_json::to_writer_pretty(&file, &known_hwnds.keys().collect::>()) + { + tracing::error!("Failed to save list of known_hwnds on file: {}", error); + } + } + Err(error) => { + tracing::error!("Failed to save list of known_hwnds on file: {}", error); + } + } // Store new hwnds self.known_hwnds = known_hwnds;