hinoirisetr 1.6.2

A daemon to dim the screen at night
Documentation
pub mod gamma;
pub(crate) mod interpolation;
pub mod temp;
#[cfg(feature = "wayland-backend")]
pub(crate) mod wayland;

#[cfg(feature = "wayland-backend")]
use std::sync::{Arc, LazyLock, RwLock};

#[cfg(feature = "wayland-backend")]
use crate::backend::wayland::WlrGammaBackend;
#[cfg(feature = "wayland-backend")]
static WAYLAND_THREAD: LazyLock<Arc<RwLock<Option<WlrGammaBackend>>>> =
    LazyLock::new(|| Arc::new(RwLock::new(None)));

/// this function needs te be called after the config has changed, to start/stop the wayland client thread
#[cfg(feature = "wayland-backend")]
pub fn update_wayland_thread(config: &crate::Config) {
    use crate::debug;

    if config.gamma_backend.contains(&gamma::GammaBackend::Wayland)
        || config.temp_backend.contains(&temp::TempBackend::Wayland)
    {
        let need_init = {
            let thread = WAYLAND_THREAD.read().unwrap();
            thread.is_none()
        };

        if need_init {
            debug!("Starting wayland backend");
            let mut thread = WAYLAND_THREAD.write().unwrap();
            if thread.is_none() {
                thread.replace(wayland::WlrGammaBackend::new());
            }
        }
    } else {
        let need_stop = {
            let thread = WAYLAND_THREAD.read().unwrap();
            thread.is_some()
        };

        if need_stop {
            debug!("Stopping wayland backend");
            let mut thread = WAYLAND_THREAD.write().unwrap();
            thread.take().unwrap().stop();
        }
    }
}

#[cfg(feature = "wayland-backend")]
pub fn kill_wayland_thread() {
    let mut thread = WAYLAND_THREAD.write().unwrap();
    thread.take().unwrap().stop();
}