Skip to content

Commit

Permalink
Merge pull request #9 from amadejkastelic/fix-file-change-listener
Browse files Browse the repository at this point in the history
Fix hot reload
  • Loading branch information
amadejkastelic authored Jan 2, 2025
2 parents bbc52e2 + 433bdc0 commit 7d48df6
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 59 deletions.
66 changes: 37 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyprlux"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
authors = ["Amadej Kastelic <[email protected]>"]
description = "Hyprland utility that automates vibrance and night light control"
Expand All @@ -14,7 +14,7 @@ chrono = "0.4.39"
colog = "1.3.0"
hyprland = "0.4.0-beta.2"
log = "0.4.22"
notify = "7.0.0"
notify = "6.1.1"
regex = "1.11.1"
serde = { version = "1.0", features = ["derive"] }
strfmt = "0.2.4"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,13 @@ Hyprlux looks for configs in the following locations (sorted by priority):

Example configurations are available in [examples](examples/).

## Running

Either run it as a systemd service or include it in your hyprland exec-once config:

```toml
exec-once=hyprlux > /tmp/hyprlux.log 2>&1
```

## Building
Run `cargo build`
2 changes: 2 additions & 0 deletions examples/location_based.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ vibrance_configs = [
{ window_class = "firefox", window_title = "", strength = 100 },
{ window_class = "mplayer2", window_title = "Video Player", strength = 100 },
]

hot_reload = true
2 changes: 2 additions & 0 deletions examples/manual.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ vibrance_configs = [
{ window_class = "firefox", window_title = "", strength = 100 },
{ window_class = "mplayer2", window_title = "Video Player", strength = 100 },
]

hot_reload = true
8 changes: 8 additions & 0 deletions nix/hm-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ in {
}
];
};

hot_reload = lib.mkOption {
description = "Listen for config changes";
type = lib.types.bool;
default = false;
example = true;
};
};

config = lib.mkIf cfg.enable (lib.mkMerge [
Expand All @@ -134,6 +141,7 @@ in {
source = cfgFormat.generate "hyprlux.toml" {
night_light = lib.attrsets.filterAttrs (n: v: v != null) cfg.night_light;
vibrance_configs = cfg.vibrance_configs;
hot_reload = cfg.hot_reload;
};
};
}
Expand Down
8 changes: 8 additions & 0 deletions nix/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ in {
}
];
};

hot_reload = lib.mkOption {
description = "Listen for config changes";
type = lib.types.bool;
default = false;
example = true;
};
};

config = lib.mkIf cfg.enable {
Expand All @@ -120,6 +127,7 @@ in {
source = cfgFormat.generate "config.toml" {
night_light = lib.attrsets.filterAttrs (n: v: v != null) cfg.night_light;
vibrance_configs = cfg.vibrance_configs;
hot_reload = cfg.hot_reload;
};
};
};
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ const DEFAULT_CONFIG_PATH: &str = "/etc/hyprlux/config.toml";
pub struct Config {
pub night_light: NightLightConfig,
pub vibrance_configs: Vec<VibranceConfig>,
pub hot_reload: Option<bool>,
}

impl Default for Config {
fn default() -> Self {
Self {
night_light: NightLightConfig::default(),
vibrance_configs: Vec::new(),
hot_reload: Some(false),
}
}
}
Expand Down
60 changes: 32 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn main() -> hyprland::Result<()> {
colog::init();

let config_path = config::path();
let config_data = Arc::new(Mutex::new(load_config_and_shaders(&config_path)));

// Channel for notifying when the config file changes
let (tx, rx) = mpsc::channel();
Expand All @@ -31,39 +32,39 @@ fn main() -> hyprland::Result<()> {
.watch(config_path.as_ref(), RecursiveMode::NonRecursive)
.unwrap();

let config_data = Arc::new(Mutex::new(load_config_and_shaders(&config_path)));

let config_data_clone = Arc::clone(&config_data);

// Spawn a thread to watch for config changes and reload shaders
let debounce_delay = Duration::from_millis(2000);
let mut last_event_time = Instant::now();
thread::spawn(move || loop {
match rx.recv() {
Ok(_) => {
let now = Instant::now();
if now.duration_since(last_event_time) > debounce_delay {
info!("Config file changed. Reloading...");

let new_config = load_config_and_shaders(&config_path);

let mut config_data = config_data_clone.lock().unwrap();

// Only load config if it's not the same and it contains data
if new_config != *config_data
&& new_config.night_light_shader != None
&& new_config.vibrance_shaders.len() > 0
{
*config_data = load_config_and_shaders(&config_path);
last_event_time = now;
if config_data_clone.lock().unwrap().hot_reload {
// Spawn a thread to watch for config changes and reload shaders
let debounce_delay = Duration::from_millis(2000);
let mut last_event_time = Instant::now();
thread::spawn(move || loop {
match rx.recv() {
Ok(_) => {
let now = Instant::now();
if now.duration_since(last_event_time) > debounce_delay {
info!("Config file changed. Reloading...");

let new_config = load_config_and_shaders(&config_path);

let mut config_data = config_data_clone.lock().unwrap();

// Only load config if it's not the same and it contains data
if new_config != *config_data
&& new_config.night_light_shader != None
&& new_config.vibrance_shaders.len() > 0
{
*config_data = load_config_and_shaders(&config_path);
last_event_time = now;
}
} else {
info!("Ignoring duplicate event within debounce period");
}
} else {
info!("Ignoring duplicate event within debounce period");
}
Err(error) => error!("Watch error: {:?}", error),
}
Err(error) => error!("Watch error: {:?}", error),
}
});
});
}

// Setup the event listener
let mut event_listener = EventListener::new();
Expand Down Expand Up @@ -122,6 +123,7 @@ fn load_config_and_shaders(config_path: &str) -> ConfigData {
return ConfigData {
night_light_shader: None,
vibrance_shaders: [].to_vec(),
hot_reload: false,
};
}

Expand Down Expand Up @@ -164,11 +166,13 @@ fn load_config_and_shaders(config_path: &str) -> ConfigData {
ConfigData {
night_light_shader,
vibrance_shaders,
hot_reload: cfg.hot_reload.unwrap_or(false),
}
}

#[derive(PartialEq)]
struct ConfigData {
night_light_shader: Option<shaders::night_light::NightLightShader>,
vibrance_shaders: Vec<shaders::vibrance::VibranceShader>,
hot_reload: bool,
}

0 comments on commit 7d48df6

Please sign in to comment.