Load config file

- Read image file path from config file.
- "mode" left untouched for now.
This commit is contained in:
Candifloss 2025-12-17 00:41:19 +05:30
parent 432ddc5ca6
commit c02d53464d
3 changed files with 43 additions and 4 deletions

View File

@ -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"
panic = "abort"

36
src/config.rs Normal file
View File

@ -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<String>, // Unused now, deal with it later
}
impl Config {
/// Load config from config file.
pub fn load() -> Result<Self> {
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<PathBuf> {
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)
}

View File

@ -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)?;