hinoirisetr 0.3.1

A daemon to dim the screen at night
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::env;
use std::path::PathBuf;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::{Duration, UNIX_EPOCH};

use hinoirisetr::notify::InitializedNotificationSystem;
use hinoirisetr::time::Time;
use hinoirisetr::{apply_settings, compute_settings, debug, error, info, trace, warn, Config, GammaBackend};
use smol::Timer;
use smol::channel::{Sender, unbounded};
use smol::io::{AsyncBufReadExt, BufReader};
use smol::lock::{RwLock, RwLockReadGuard};
use smol::net::unix::UnixListener;
use smol::stream::StreamExt;

const SOCKET_PATH: &str = "/tmp/hinoirisetr.sock";

static CONFIG: OnceLock<Arc<RwLock<Config>>> = OnceLock::new();
static LAST_MODIFIED: AtomicU64 = AtomicU64::new(0);

enum NotifyState {
    Enabled(InitializedNotificationSystem),
    Disabled,
}

async fn config_realoader(notify: Arc<Sender<()>>) {
    debug!("config reloader started");
    // Config polling every 5 seconds
    loop {
        trace!("config poll tick");
        let config_path = get_config_path();
        if config_path.exists() {
            if let Ok(current_modified) = std::fs::metadata(&config_path)
                .and_then(|m| m.modified())
                .map(|t| t.duration_since(UNIX_EPOCH).unwrap().as_secs())
            {
                let last: u64 = LAST_MODIFIED.load(Ordering::SeqCst);
                if 0 != last {
                    if current_modified > last {
                        trace!("{current_modified}");
                        trace!("{last}");
                        debug!("Config file modified, reloading...");
                        reload_config(Arc::clone(&notify)).await;
                    }
                } else {
                    // File is new, reload to capture initial state
                    debug!("Config file detected, reloading...");
                    reload_config(Arc::clone(&notify)).await;
                }
            }
        }
        Timer::after(Duration::from_secs(5)).await;
    }
}

async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Sender<()>>) {
    let listener = UnixListener::bind(SOCKET_PATH).expect("Failed to bind socket");
    trace!("socket server bound");
    let notification: NotifyState =
        match hinoirisetr::notify::InitializedNotificationSystem::new("hinoirisetr") {
            Ok(not) => NotifyState::Enabled(not),
            Err(_) => {
                info!("libnotify not found, disabling 'status_notify' command");
                NotifyState::Disabled
            }
        };

    loop {
        let (stream, _) = listener.accept().await.unwrap();
        let reader = BufReader::new(stream);
        let mut lines = reader.lines();

        trace!("socket server accepted connection");

        if let Some(Ok(line)) = lines.next().await {
            match line.trim() {
                "disable" => {
                    trace!("disable dispatched");
                    disabled.store(true, Ordering::SeqCst);
                    let _ = notify.send(()).await;
                    debug!("dimming is disabled");
                }
                "enable" => {
                    trace!("enable dispatched");
                    disabled.store(false, Ordering::SeqCst);
                    let _ = notify.send(()).await;
                    debug!("dimming is enabled");
                }
                "toggle" => {
                    trace!("toggle dispatched");
                    let now = !disabled.load(Ordering::SeqCst);
                    disabled.store(now, Ordering::SeqCst);
                    let _ = notify.send(()).await;
                    debug!("dimming is {}", if now { "enabled" } else { "disabled" });
                }
                "status" => {
                    trace!("status dispatched");
                    // compute current temp/gamma
                    let now = get_time();
                    let (cur_temp, cur_gamma) = compute_settings(now, &*config_guard().await);

                    info!(
                        "dimming is {} — temp: {}K, gamma: {}%",
                        if disabled.load(Ordering::SeqCst) {
                            "disabled"
                        } else {
                            "enabled"
                        },
                        cur_temp,
                        cur_gamma
                    );
                }
                "reload" => {
                    trace!("reload dispatched");
                    reload_config(notify.clone()).await;
                }
                "status_notify" => {
                    trace!("status_notify dispatched");
                    let now = get_time();
                    let (cur_temp, cur_gamma) = compute_settings(now, &*config_guard().await);

                    let body = if disabled.load(Ordering::SeqCst) {
                        "disabled".to_string()
                    } else {
                        format!("temp: {cur_temp}K, gamma: {cur_gamma}%")
                    };

                    match notification {
                        NotifyState::Enabled(ref not) => {
                            trace!("notify notification enabled");
                            let timeout = config_guard().await.notification_timeout;
                            match not.show_notification(
                                "Sunsetting",
                                &body,
                                "notification-icon",
                                timeout as i32,
                            ) {
                                Ok(_) => {}
                                Err(e) => error!("Failed to show notification: {e:?}"),
                            };
                        }
                        NotifyState::Disabled => {
                            trace!("notify notification disabled");
                        }
                    }
                }
                _ => error!("unknown command: {}", line.trim()),
            }
        }
    }
}

fn main() {
    let args: Vec<String> = std::env::args().collect();

    if args.len() > 1 && (args[1] == "--version" || args[1] == "-v") {
        println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
        return;
    }

    smol::block_on(async {
        match env::var("RUST_LOG") {
            Ok(val) => match val.parse::<hinoirisetr::log::LogLevel>() {
                Ok(level) => hinoirisetr::log::set_log_level(level),
                Err(err) => error!("Failed to parse RUST_LOG: {err}"),
            },
            Err(_) => {
                if cfg!(debug_assertions) {
                    hinoirisetr::log::set_log_level(hinoirisetr::log::LogLevel::Debug);
                } else {
                    hinoirisetr::log::set_log_level(hinoirisetr::log::LogLevel::Info);
                }
            }
        }

        info!("starting the daemon");

        if !is_binary_available("hyprctl") {
            error!("hyprctl is not available, exiting.");
            std::process::exit(1);
        }

        let disabled = Arc::new(AtomicBool::new(false));
        let (notify_s, notify_r) = unbounded::<()>();
        let notify = Arc::new(notify_s);

        // load config
        let config_path = get_config_path();
        let cfg: Config = if config_path.exists() {
            debug!("Config file found, loading...");
            LAST_MODIFIED.store(
                std::fs::metadata(&config_path)
                    .and_then(|m| m.modified())
                    .map(|t| t.duration_since(UNIX_EPOCH).unwrap().as_secs())
                    .unwrap_or(0),
                SeqCst,
            );
            match Config::load(&config_path) {
                Ok(cfg) => cfg,
                Err(err) => {
                    error!("Failed to load config: {err:?}");
                    warn!("Using default config.");
                    Config::default()
                }
            }
        } else {
            warn!("Config file not found, using default config.");
            warn!("Config path {}", get_config_path().display());
            Config::default()
        };


        // TODO: add more temp backends and remove this
        if !is_binary_available("hyprctl") {
            error!("hyprctl is not available, exiting.");
            std::process::exit(1);
        }
        
        match cfg.gamma_backend {
            GammaBackend::Hyprctl => {
                if !is_binary_available("hyprctl") {
                    error!("hyprctl is not available, exiting.");
                    std::process::exit(1);
                }
            }
            GammaBackend::Ddcutil => {
                if !is_binary_available("ddcutil") {
                    error!("ddcutil is not available, exiting.");
                    std::process::exit(1);
                }
            }
        }


        CONFIG.set(Arc::new(RwLock::new(cfg))).unwrap();

        if std::path::Path::new(SOCKET_PATH).exists() {
            match std::os::unix::net::UnixStream::connect(SOCKET_PATH) {
                Ok(_) => {
                    error!("Another instance is running.");
                    std::process::exit(1);
                }
                Err(_) => {
                    warn!("Stale socket found, removing.");
                    let _ = std::fs::remove_file(SOCKET_PATH);
                }
            }
        }

        // Spawn control socket server
        {
            let disabled = Arc::clone(&disabled);
            let notify = Arc::clone(&notify);
            smol::spawn(async move {
                socket_server(disabled, notify).await;
            })
            .detach();
        }

        // Spawn config reloader
        {
            let notify = Arc::clone(&notify);
            smol::spawn(async move {
                config_realoader(notify).await;
            })
            .detach();
        }

        // Spawn timer
        {
            let notify = Arc::clone(&notify);
            smol::spawn(async move {
                loop {
                    Timer::after(Duration::from_secs(300)).await;
                    let _ = notify.send(()).await;
                }
            })
            .detach();
        }

        // Signal handling
        // let mut sigint = signal(SignalKind::interrupt()).unwrap();
        // let mut sigterm = signal(SignalKind::terminate()).unwrap();

        // set initial settings
        {
            let now = get_time();
            let (temp, gamma) = compute_settings(now, &*config_guard().await);
            apply_settings(temp, gamma, config_guard().await.gamma_backend);
            trace!("initial settings applied: {temp}K, {gamma}%");
        }

        // Main loop with shutdown support
        loop {
            if disabled.load(Ordering::SeqCst) {
                apply_settings(
                    config_guard().await.temp_day,
                    config_guard().await.gamma_day,
                    config_guard().await.gamma_backend,
                );
            } else {
                let now = get_time();
                let (temp, gamma) = compute_settings(now, &*config_guard().await);
                apply_settings(temp, gamma, config_guard().await.gamma_backend);
            }

            let _ = notify_r.recv().await;
        }
        //     _ = sigint.recv() => {
        //         info!("Received SIGINT, shutting down...");
        //     },
        //     _ = sigterm.recv() => {
        //         info!("Received SIGTERM, shutting down...");
        //     },
        // }

        // Cleanup the socket file on shutdown
        // if std::path::Path::new(SOCKET_PATH).exists() {
        //     match std::fs::remove_file(SOCKET_PATH) {
        //         Ok(_) => info!("Socket file {SOCKET_PATH} removed."),
        //         Err(e) => warn!("Failed to remove socket file {SOCKET_PATH}: {e}"),
        //     }
        // }
    })
}

// Function to handle config reloading
async fn reload_config(notify: Arc<Sender<()>>) {
    trace!("reload_config called");
    let config_handle = config_handle();
    let mut config = config_handle.write().await;
    let config_path = get_config_path();
    match Config::load(&config_path) {
        Ok(cfg) => {
            debug!("Config file reloaded successfully");
            *config = cfg;
            trace!("new config: {:#?}", config);

            let new_modified = std::fs::metadata(&config_path)
                .and_then(|m| m.modified())
                .map(|t| t.duration_since(UNIX_EPOCH).unwrap().as_secs())
                .unwrap_or(0);
            trace!("new_modified: {new_modified:?}");
            LAST_MODIFIED.store(new_modified, SeqCst);
            let _ = notify.send(()).await;
        }
        Err(err) => {
            error!("Failed to reload config: {err:?}");
            warn!("Retaining current config");
        }
    }
}
fn is_binary_available(binary_name: &str) -> bool {
    use std::fs;
    if let Ok(paths) = env::var("PATH") {
        for path in env::split_paths(&paths) {
            let full_path = path.join(binary_name);
            if full_path.exists()
                && fs::metadata(&full_path)
                    .map(|m| m.is_file())
                    .unwrap_or(false)
            {
                return true;
            }
        }
    }
    false
}

#[inline]
fn get_config_path() -> PathBuf {
    if cfg!(target_os = "windows") {
        let username = env::var("USERNAME").unwrap_or_else(|_| "Default".to_string());
        PathBuf::from(format!(
            "C:\\Users\\{username}\\AppData\\Local\\hinoirisetr.toml"
        ))
    } else {
        let xdg_config_home = env::var("XDG_CONFIG_HOME").ok();
        let home = env::var("HOME").ok();
        let user = env::var("USER").unwrap_or_else(|_| "default".to_string());

        xdg_config_home
            .map(|x| PathBuf::from(format!("{x}/hinoirisetr.toml")))
            .or_else(|| home.map(|h| PathBuf::from(format!("{h}/.config/hinoirisetr.toml"))))
            .unwrap_or_else(|| PathBuf::from(format!("/home/{user}/.config/hinoirisetr.toml")))
    }
}

async fn config_guard() -> RwLockReadGuard<'static, Config> {
    CONFIG.get().expect("config not init").read().await
}

fn config_handle() -> Arc<RwLock<Config>> {
    CONFIG.get().expect("config not init").clone()
}

// async fn get_config() -> Config {
//     let lock = CONFIG.get().expect("config not initialized").read().await;
//     lock.clone()
// }

fn get_time() -> Time {
    Time::now().expect("Failed to get local time")
}