From c02d53464d690df20b03704a12a5e4fcfbfff50a Mon Sep 17 00:00:00 2001 From: Candifloss Date: Wed, 17 Dec 2025 00:41:19 +0530 Subject: [PATCH] Load config file - Read image file path from config file. - "mode" left untouched for now. --- Cargo.toml | 3 ++- src/config.rs | 36 ++++++++++++++++++++++++++++++++++++ src/main.rs | 8 +++++--- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.toml b/Cargo.toml index d479dde..a726fc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ anyhow = "1.0.100" dirs = "6.0.0" image = { version = "0.25.9", default-features = false, features = ["png", "jpeg"] } pico-args = "0.5.0" +serde = { version = "1.0.228", default-features = false, features = ["derive"] } toml = "0.9.8" x11rb = { version = "0.13.2", default-features = false } @@ -16,4 +17,4 @@ lto = "thin" codegen-units = 1 strip = "symbols" opt-level = "z" -panic = "abort" \ No newline at end of file +panic = "abort" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..9850cec --- /dev/null +++ b/src/config.rs @@ -0,0 +1,36 @@ +use anyhow::{Context, Result}; +use serde::Deserialize; +use std::fs; +use std::path::PathBuf; + +/// Runtime configuration for `icing`. +#[derive(Debug, Deserialize)] +pub struct Config { + pub background_image: String, + pub mode: Option, // Unused now, deal with it later +} + +impl Config { + /// Load config from config file. + pub fn load() -> Result { + let path = config_path()?; + + let contents = fs::read_to_string(&path) + .with_context(|| format!("Failed to read config file: {}", path.display()))?; + + let config: Config = toml::from_str(&contents) + .with_context(|| format!("Failed to parse config file: {}", path.display()))?; + + Ok(config) + } +} + +fn config_path() -> Result { + let mut path = dirs::config_dir().context("Could not determine XDG config directory")?; // Usually `~/.config/` + // `~/.config/candywidgets/icing/config.toml` + path.push("candywidgets"); + path.push("icing"); + path.push("config.toml"); + + Ok(path) +} diff --git a/src/main.rs b/src/main.rs index 5eb0564..d9ff495 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,11 +10,13 @@ use x11rb::{ }, rust_connection::RustConnection, }; +mod config; fn main() -> Result<()> { - // Path to wallpaper image. - // This is hardcoded for now and will later be replaced by config loading. - let image_path = "/path/to/image"; + // Load config + let config = config::Config::load()?; + // Path to wallpaper image in config + let image_path = &config.background_image; // Connect to the running graphical session, the X11 server. let (conn, screen_num) = RustConnection::connect(None)?;