aboutsummaryrefslogtreecommitdiff
path: root/src/watcher.rs
diff options
context:
space:
mode:
authorIvan Molodetskikh <yalterz@gmail.com>2023-09-26 19:24:50 +0400
committerIvan Molodetskikh <yalterz@gmail.com>2023-09-26 19:24:50 +0400
commit4a585a3293286742d96689e52b1a314cf8c3854e (patch)
tree61d6b5872c655303b50b11a2a3f6f04f579cf309 /src/watcher.rs
parent3b83b2fb16b7ee55582594c2065167ad626a1b8c (diff)
downloadniri-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.rs60
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 }
+ }
+}