Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sunrise / sunset support #6

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyprlux"
version = "0.1.2"
version = "0.1.3"
edition = "2021"

[dependencies]
Expand All @@ -14,5 +14,6 @@ notify = "6.1.1"
regex = "1.10.6"
serde = { version = "1.0", features = ["derive"] }
strfmt = "0.2.4"
sunrise = "1.0.1"
toml = "0.8.19"
xdg = "2.5.2"
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ And configure it:
Run `cargo build`

## TODO
- [ ] Toggle night light based on location and time of day
- [x] Toggle night light based on location and time of day
- [x] Allow config reload
- [ ] Allow stop and resume
- [ ] Publish to aur and crate
- [x] Add nix module systemd service support
6 changes: 6 additions & 0 deletions examples/location_based.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
night_light = { enabled = true, latitude = 46.056946, longitude = 14.505751, temperature = 3500 }

vibrance_configs = [
{ window_class = "firefox", window_title = "", strength = 100 },
{ window_class = "mplayer2", window_title = "Video Player", strength = 100 },
]
File renamed without changes.
20 changes: 15 additions & 5 deletions nix/hm-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ hyprlux: {
type = lib.types.bool;
default = false;
};
latitude = lib.mkOption {
description = "Your latitude";
type = with lib.types; nullOr (oneOf [int float]);
default = null;
};
longitude = lib.mkOption {
description = "Your longitude";
type = with lib.types; nullOr (oneOf [int float]);
default = null;
};
start_time = lib.mkOption {
description = "When to start night light";
type = time;
type = lib.types.nullOr time;
default = "20:00";
};
end_time = lib.mkOption {
description = "When to end night light";
type = time;
type = lib.types.nullOr time;
default = "06:00";
};
temperature = lib.mkOption {
Expand Down Expand Up @@ -91,8 +101,8 @@ in {
};
example = {
enabled = true;
start_time = "20:00";
end_time = "06:00";
latitude = 46.056946;
longitude = 14.505751;
temperature = 3500;
};
};
Expand Down Expand Up @@ -122,7 +132,7 @@ in {

xdg.configFile."hypr/hyprlux.toml" = {
source = cfgFormat.generate "hyprlux.toml" {
night_light = cfg.night_light;
night_light = lib.attrsets.filterAttrs (n: v: v != null) cfg.night_light;
vibrance_configs = cfg.vibrance_configs;
};
};
Expand Down
20 changes: 15 additions & 5 deletions nix/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ hyprlux: {
type = lib.types.bool;
default = false;
};
latitude = lib.mkOption {
description = "Your latitude";
type = with lib.types; nullOr (oneOf [int float]);
default = null;
};
longitude = lib.mkOption {
description = "Your longitude";
type = with lib.types; nullOr (oneOf [int float]);
default = null;
};
start_time = lib.mkOption {
description = "When to start night light";
type = time;
type = lib.types.nullOr time;
default = "20:00";
};
end_time = lib.mkOption {
description = "When to end night light";
type = time;
type = lib.types.nullOr time;
default = "06:00";
};
temperature = lib.mkOption {
Expand Down Expand Up @@ -76,8 +86,8 @@ in {
};
example = {
enabled = true;
start_time = "20:00";
end_time = "06:00";
latitude = 46.056946;
longitude = 14.505751;
temperature = 3500;
};
};
Expand Down Expand Up @@ -108,7 +118,7 @@ in {

environment.etc."hyprlux/config.toml" = {
source = cfgFormat.generate "config.toml" {
night_light = cfg.night_light;
night_light = lib.attrsets.filterAttrs (n: v: v != null) cfg.night_light;
vibrance_configs = cfg.vibrance_configs;
};
};
Expand Down
12 changes: 8 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ impl Default for Config {
#[derive(Debug, Deserialize)]
pub struct NightLightConfig {
pub enabled: bool,
pub start_time: String,
pub end_time: String,
pub start_time: Option<String>,
pub end_time: Option<String>,
pub temperature: i32,
pub latitude: Option<f64>,
pub longitude: Option<f64>,
}

impl Default for NightLightConfig {
fn default() -> Self {
Self {
enabled: false,
start_time: "00:00".to_string(),
end_time: "00:00".to_string(),
start_time: None,
end_time: None,
temperature: 3500,
latitude: None,
longitude: None,
}
}
}
Expand Down
30 changes: 22 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod utils;
use hyprland::event_listener::EventListener;
use log::{debug, error, info};
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use shaders::night_light::NightLightShader;
use shaders::shader::{self, Shader};
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
Expand Down Expand Up @@ -131,13 +132,26 @@ fn load_config_and_shaders(config_path: &str) -> ConfigData {
let cfg = cfg.unwrap();
info!("Config loaded: {:?}", cfg);

let night_light_shader = shaders::night_light::new(
cfg.night_light.enabled,
cfg.night_light.start_time,
cfg.night_light.end_time,
cfg.night_light.temperature,
None,
);
let night_light_shader: Option<NightLightShader>;
if cfg.night_light.latitude.is_some() && cfg.night_light.longitude.is_some() {
night_light_shader = Some(shaders::night_light::new_from_location(
cfg.night_light.enabled,
cfg.night_light.latitude.unwrap(),
cfg.night_light.longitude.unwrap(),
cfg.night_light.temperature,
None,
));
} else if cfg.night_light.start_time.is_some() && cfg.night_light.end_time.is_some() {
night_light_shader = Some(shaders::night_light::new(
cfg.night_light.enabled,
cfg.night_light.start_time.unwrap(),
cfg.night_light.end_time.unwrap(),
cfg.night_light.temperature,
None,
));
} else {
night_light_shader = None;
}

let vibrance_shaders: Vec<shaders::vibrance::VibranceShader> = cfg
.vibrance_configs
Expand All @@ -152,7 +166,7 @@ fn load_config_and_shaders(config_path: &str) -> ConfigData {
.collect();

ConfigData {
night_light_shader: Some(night_light_shader),
night_light_shader,
vibrance_shaders,
}
}
Expand Down
26 changes: 25 additions & 1 deletion src/shaders/night_light.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::time::{Duration, UNIX_EPOCH};

use chrono::NaiveTime;
use chrono::{DateTime, Datelike, Local, NaiveTime};
use strfmt::Format;

use super::super::utils::Time;
Expand Down Expand Up @@ -78,6 +79,29 @@ pub fn new(
}
}

pub fn new_from_location(
enabled: bool,
latitude: f64,
longitude: f64,
temperature: i32,
mock_time: Option<String>,
) -> NightLightShader {
let now = Local::now();
let (sunrise, sunset) =
sunrise::sunrise_sunset(latitude, longitude, now.year(), now.month(), now.day());

let start_time = unix_to_string(sunset.try_into().unwrap());
let end_time = unix_to_string(sunrise.try_into().unwrap());

new(enabled, start_time, end_time, temperature, mock_time)
}

fn unix_to_string(ts: u64) -> String {
DateTime::<Local>::from(UNIX_EPOCH + Duration::from_secs(ts))
.format(TIME_FMT)
.to_string()
}

impl Shader for NightLightShader {
fn should_apply(&self, _: Option<String>, _: Option<String>) -> bool {
let now = self.time_impl.now();
Expand Down
Loading