diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2023-09-26 19:24:50 +0400 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2023-09-26 19:24:50 +0400 |
| commit | 4a585a3293286742d96689e52b1a314cf8c3854e (patch) | |
| tree | 61d6b5872c655303b50b11a2a3f6f04f579cf309 /src/watcher.rs | |
| parent | 3b83b2fb16b7ee55582594c2065167ad626a1b8c (diff) | |
| download | niri-4a585a3293286742d96689e52b1a314cf8c3854e.tar.gz niri-4a585a3293286742d96689e52b1a314cf8c3854e.tar.bz2 niri-4a585a3293286742d96689e52b1a314cf8c3854e.zip | |
Add initial config hot reloading
Diffstat (limited to 'src/watcher.rs')
| -rw-r--r-- | src/watcher.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/watcher.rs b/src/watcher.rs new file mode 100644 index 00000000..16e65b4d --- /dev/null +++ b/src/watcher.rs @@ -0,0 +1,60 @@ +//! File modification watcher. + +use std::path::PathBuf; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; +use std::thread; +use std::time::Duration; + +use smithay::reexports::calloop::channel::SyncSender; + +pub struct Watcher { + should_stop: Arc<AtomicBool>, +} + +impl Drop for Watcher { + fn drop(&mut self) { + self.should_stop.store(true, Ordering::SeqCst); + } +} + +impl Watcher { + pub fn new(path: PathBuf, changed: SyncSender<()>) -> Self { + let should_stop = Arc::new(AtomicBool::new(false)); + + { + let should_stop = should_stop.clone(); + thread::Builder::new() + .name(format!("Filesystem Watcher for {}", path.to_string_lossy())) + .spawn(move || { + let mut last_mtime = path.metadata().and_then(|meta| meta.modified()).ok(); + + loop { + thread::sleep(Duration::from_millis(500)); + + if should_stop.load(Ordering::SeqCst) { + break; + } + + if let Ok(mtime) = path.metadata().and_then(|meta| meta.modified()) { + if last_mtime != Some(mtime) { + trace!("file changed: {}", path.to_string_lossy()); + + if let Err(err) = changed.send(()) { + warn!("error sending change notification: {err:?}"); + break; + } + + last_mtime = Some(mtime); + } + } + } + + debug!("exiting watcher thread for {}", path.to_string_lossy()); + }) + .unwrap(); + } + + Self { should_stop } + } +} |
