forge
Explore
Log in

Don't you dare go Hollow

Add the `LoadConfigFile` action (#2163)

Browse files at this commit

67361f88fd01974ebee4cf80f0e29c87d805cc39Unverifiedvanderlokken and Ivan MolodetskikhParent: f74d83d
* Add the `LoadConfigFile` action

* fixes

---------
niri-config/src/lib.rsmodified+3 -0
@@ -1924,6 +1924,8 @@ pub enum Action {
1924 SetWindowUrgent(u64),1924 SetWindowUrgent(u64),
1925 #[knuffel(skip)]1925 #[knuffel(skip)]
1926 UnsetWindowUrgent(u64),1926 UnsetWindowUrgent(u64),
1927+ #[knuffel(skip)]
1928+ LoadConfigFile,
1927 }1929 }
1928 1930
1929 impl From<niri_ipc::Action> for Action {1931 impl From<niri_ipc::Action> for Action {
@@ -2199,6 +2201,7 @@ impl From<niri_ipc::Action> for Action {
2199 niri_ipc::Action::ToggleWindowUrgent { id } => Self::ToggleWindowUrgent(id),2201 niri_ipc::Action::ToggleWindowUrgent { id } => Self::ToggleWindowUrgent(id),
2200 niri_ipc::Action::SetWindowUrgent { id } => Self::SetWindowUrgent(id),2202 niri_ipc::Action::SetWindowUrgent { id } => Self::SetWindowUrgent(id),
2201 niri_ipc::Action::UnsetWindowUrgent { id } => Self::UnsetWindowUrgent(id),2203 niri_ipc::Action::UnsetWindowUrgent { id } => Self::UnsetWindowUrgent(id),
2204+ niri_ipc::Action::LoadConfigFile {} => Self::LoadConfigFile,
2202 }2205 }
2203 }2206 }
2204 }2207 }
niri-ipc/src/lib.rsmodified+5 -0
@@ -840,6 +840,11 @@ pub enum Action {
840 #[cfg_attr(feature = "clap", arg(long))]840 #[cfg_attr(feature = "clap", arg(long))]
841 id: u64,841 id: u64,
842 },842 },
843+ /// Reload the config file.
844+ ///
845+ /// Can be useful for scripts changing the config file, to avoid waiting the small duration for
846+ /// niri's config file watcher to notice the changes.
847+ LoadConfigFile {},
843 }848 }
844 849
845 /// Change in window or column size.850 /// Change in window or column size.
src/input/mod.rsmodified+5 -0
@@ -2113,6 +2113,11 @@ impl State {
2113 }2113 }
2114 self.niri.queue_redraw_all();2114 self.niri.queue_redraw_all();
2115 }2115 }
2116+ Action::LoadConfigFile => {
2117+ if let Some(watcher) = &self.niri.config_file_watcher {
2118+ watcher.load_config();
2119+ }
2120+ }
2116 }2121 }
2117 }2122 }
2118 2123
src/main.rsmodified+2 -23
@@ -23,8 +23,7 @@ use niri::utils::spawning::{
23 spawn, store_and_increase_nofile_rlimit, CHILD_DISPLAY, CHILD_ENV, REMOVE_ENV_RUST_BACKTRACE,23 spawn, store_and_increase_nofile_rlimit, CHILD_DISPLAY, CHILD_ENV, REMOVE_ENV_RUST_BACKTRACE,
24 REMOVE_ENV_RUST_LIB_BACKTRACE,24 REMOVE_ENV_RUST_LIB_BACKTRACE,
25 };25 };
26-use niri::utils::watcher::Watcher;26+use niri::utils::{cause_panic, version, watcher, xwayland, IS_SYSTEMD_SERVICE};
27-use niri::utils::{cause_panic, version, xwayland, IS_SYSTEMD_SERVICE};
28 use niri_config::ConfigPath;27 use niri_config::ConfigPath;
29 use niri_ipc::socket::SOCKET_PATH_ENV;28 use niri_ipc::socket::SOCKET_PATH_ENV;
30 use portable_atomic::Ordering;29 use portable_atomic::Ordering;
@@ -230,27 +229,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
230 }229 }
231 }230 }
232 231
233- // Set up config file watcher.232+ watcher::setup(&mut state, &config_path);
234- let _watcher = {
235- // Parsing the config actually takes > 20 ms on my beefy machine, so let's do it on the
236- // watcher thread.
237- let process = |path: &ConfigPath| {
238- path.load().map_err(|err| {
239- warn!("{err:?}");
240- })
241- };
242-
243- let (tx, rx) = calloop::channel::sync_channel(1);
244- let watcher = Watcher::new(config_path.clone(), process, tx);
245- event_loop
246- .handle()
247- .insert_source(rx, |event, _, state| match event {
248- calloop::channel::Event::Msg(config) => state.reload_config(config),
249- calloop::channel::Event::Closed => (),
250- })
251- .unwrap();
252- watcher
253- };
254 233
255 // Spawn commands from cli and auto-start.234 // Spawn commands from cli and auto-start.
256 spawn(cli.command, None);235 spawn(cli.command, None);
src/niri.rsmodified+4 -0
@@ -164,6 +164,7 @@ use crate::ui::screen_transition::{self, ScreenTransition};
164 use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRenderElement};164 use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRenderElement};
165 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};165 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};
166 use crate::utils::spawning::{CHILD_DISPLAY, CHILD_ENV};166 use crate::utils::spawning::{CHILD_DISPLAY, CHILD_ENV};
167+use crate::utils::watcher::Watcher;
167 use crate::utils::xwayland::satellite::Satellite;168 use crate::utils::xwayland::satellite::Satellite;
168 use crate::utils::{169 use crate::utils::{
169 center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, is_mapped,170 center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, is_mapped,
@@ -190,6 +191,8 @@ pub struct Niri {
190 /// (and transient changes dropped).191 /// (and transient changes dropped).
191 pub config_file_output_config: niri_config::Outputs,192 pub config_file_output_config: niri_config::Outputs,
192 193
194+ pub config_file_watcher: Option<Watcher>,
195+
193 pub event_loop: LoopHandle<'static, State>,196 pub event_loop: LoopHandle<'static, State>,
194 pub scheduler: Scheduler<()>,197 pub scheduler: Scheduler<()>,
195 pub stop_signal: LoopSignal,198 pub stop_signal: LoopSignal,
@@ -2528,6 +2531,7 @@ impl Niri {
2528 let mut niri = Self {2531 let mut niri = Self {
2529 config,2532 config,
2530 config_file_output_config,2533 config_file_output_config,
2534+ config_file_watcher: None,
2531 2535
2532 event_loop,2536 event_loop,
2533 scheduler,2537 scheduler,
src/utils/watcher.rsmodified+88 -68
@@ -1,22 +1,17 @@
1 //! File modification watcher.1 //! File modification watcher.
2 2
3 use std::path::{Path, PathBuf};3 use std::path::{Path, PathBuf};
4-use std::sync::atomic::{AtomicBool, Ordering};4+use std::sync::mpsc;
5-use std::sync::{mpsc, Arc};
6 use std::time::{Duration, SystemTime};5 use std::time::{Duration, SystemTime};
7 use std::{io, thread};6 use std::{io, thread};
8 7
9 use niri_config::ConfigPath;8 use niri_config::ConfigPath;
10 use smithay::reexports::calloop::channel::SyncSender;9 use smithay::reexports::calloop::channel::SyncSender;
11 10
12-pub struct Watcher {11+use crate::niri::State;
13- should_stop: Arc<AtomicBool>,
14-}
15 12
16-impl Drop for Watcher {13+pub struct Watcher {
17- fn drop(&mut self) {14+ load_config: mpsc::Sender<()>,
18- self.should_stop.store(true, Ordering::SeqCst);
19- }
20 }15 }
21 16
22 impl Watcher {17 impl Watcher {
@@ -36,81 +31,106 @@ impl Watcher {
36 started: Option<mpsc::SyncSender<()>>,31 started: Option<mpsc::SyncSender<()>>,
37 polling_interval: Duration,32 polling_interval: Duration,
38 ) -> Self {33 ) -> Self {
39- let should_stop = Arc::new(AtomicBool::new(false));34+ let (load_config, load_config_rx) = mpsc::channel();
40-35+
41- {36+ thread::Builder::new()
42- let should_stop = should_stop.clone();37+ .name(format!("Filesystem Watcher for {config_path:?}"))
43- thread::Builder::new()38+ .spawn(move || {
44- .name(format!("Filesystem Watcher for {config_path:?}"))39+ // this "should" be as simple as storing the last seen mtime,
45- .spawn(move || {40+ // and if the contents change without updating mtime, we ignore it.
46- // this "should" be as simple as storing the last seen mtime,41+ //
47- // and if the contents change without updating mtime, we ignore it.42+ // but that breaks if the config is a symlink, and its target
48- //43+ // changes but the new target and old target have identical mtimes.
49- // but that breaks if the config is a symlink, and its target44+ // in which case we should *not* ignore it; this is an entirely different file.
50- // changes but the new target and old target have identical mtimes.45+ //
51- // in which case we should *not* ignore it; this is an entirely different file.46+ // in practice, this edge case does not occur on systems other than nix.
52- //47+ // because, on nix, everything is a symlink to /nix/store
53- // in practice, this edge case does not occur on systems other than nix.48+ // and /nix/store keeps no mtime (= 1970-01-01)
54- // because, on nix, everything is a symlink to /nix/store49+ // so, symlink targets change frequently when mtime doesn't.
55- // and /nix/store keeps no mtime (= 1970-01-01)50+ //
56- // so, symlink targets change frequently when mtime doesn't.51+ // therefore, we must also store the canonical path, along with its mtime
57- //52+
58- // therefore, we must also store the canonical path, along with its mtime53+ fn see_path(path: &Path) -> io::Result<(SystemTime, PathBuf)> {
59-54+ let canon = path.canonicalize()?;
60- fn see_path(path: &Path) -> io::Result<(SystemTime, PathBuf)> {55+ let mtime = canon.metadata()?.modified()?;
61- let canon = path.canonicalize()?;56+ Ok((mtime, canon))
62- let mtime = canon.metadata()?.modified()?;57+ }
63- Ok((mtime, canon))
64- }
65 58
66- fn see(config_path: &ConfigPath) -> io::Result<(SystemTime, PathBuf)> {59+ fn see(config_path: &ConfigPath) -> io::Result<(SystemTime, PathBuf)> {
67- match config_path {60+ match config_path {
68- ConfigPath::Explicit(path) => see_path(path),61+ ConfigPath::Explicit(path) => see_path(path),
69- ConfigPath::Regular {62+ ConfigPath::Regular {
70- user_path,63+ user_path,
71- system_path,64+ system_path,
72- } => see_path(user_path).or_else(|_| see_path(system_path)),65+ } => see_path(user_path).or_else(|_| see_path(system_path)),
73- }
74 }66 }
67+ }
75 68
76- let mut last_props = see(&config_path).ok();69+ let mut last_props = see(&config_path).ok();
77-
78- if let Some(started) = started {
79- let _ = started.send(());
80- }
81 70
82- loop {71+ if let Some(started) = started {
83- thread::sleep(polling_interval);72+ let _ = started.send(());
73+ }
84 74
85- if should_stop.load(Ordering::SeqCst) {75+ loop {
86- break;76+ let mut should_load = match load_config_rx.recv_timeout(polling_interval) {
77+ Ok(()) => true,
78+ Err(mpsc::RecvTimeoutError::Disconnected) => break,
79+ Err(mpsc::RecvTimeoutError::Timeout) => false,
80+ };
81+
82+ if let Ok(new_props) = see(&config_path) {
83+ if last_props.as_ref() != Some(&new_props) {
84+ last_props = Some(new_props);
85+ trace!("config file changed");
86+ should_load = true;
87 }87 }
88 88
89- if let Ok(new_props) = see(&config_path) {89+ if should_load {
90- if last_props.as_ref() != Some(&new_props) {90+ let rv = process(&config_path);
91- trace!("config file changed");
92-
93- let rv = process(&config_path);
94 91
95- if let Err(err) = changed.send(rv) {92+ if let Err(err) = changed.send(rv) {
96- warn!("error sending change notification: {err:?}");93+ warn!("error sending change notification: {err:?}");
97- break;94+ break;
98- }
99-
100- last_props = Some(new_props);
101 }95 }
102 }96 }
103 }97 }
98+ }
104 99
105- debug!("exiting watcher thread for {config_path:?}");100+ debug!("exiting watcher thread for {config_path:?}");
106- })101+ })
107- .unwrap();102+ .unwrap();
108- }103+
104+ Self { load_config }
105+ }
109 106
110- Self { should_stop }107+ pub fn load_config(&self) {
108+ let _ = self.load_config.send(());
111 }109 }
112 }110 }
113 111
112+pub fn setup(state: &mut State, config_path: &ConfigPath) {
113+ // Parsing the config actually takes > 20 ms on my beefy machine, so let's do it on the
114+ // watcher thread.
115+ let process = |path: &ConfigPath| {
116+ path.load().map_err(|err| {
117+ warn!("{err:?}");
118+ })
119+ };
120+
121+ let (tx, rx) = calloop::channel::sync_channel(1);
122+ state
123+ .niri
124+ .event_loop
125+ .insert_source(rx, |event, _, state| match event {
126+ calloop::channel::Event::Msg(config) => state.reload_config(config),
127+ calloop::channel::Event::Closed => (),
128+ })
129+ .unwrap();
130+
131+ state.niri.config_file_watcher = Some(Watcher::new(config_path.clone(), process, tx));
132+}
133+
114 #[cfg(test)]134 #[cfg(test)]
115 mod tests {135 mod tests {
116 use std::error::Error;136 use std::error::Error;