From e62e9509ef80fbba92cf1426a3bd0b850ca880e2 Mon Sep 17 00:00:00 2001 From: Ferdinand Bachmann Date: Mon, 6 May 2024 18:30:54 +0200 Subject: [PATCH] Add --config option for server and client --- src/client/main.rs | 28 +++++++++++++++++++++++++++- src/config/user.rs | 5 +++-- src/global_utils.rs | 1 + src/server/application.rs | 9 +++++++++ src/server/main.rs | 36 +++++++++++++++++++++++++----------- 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/client/main.rs b/src/client/main.rs index 702e3f3..0ca837e 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -13,6 +13,8 @@ use global_utils::{handle_application_args, HandleLocalStatus}; use gtk::glib::{OptionArg, OptionFlags}; use gtk::{gio::ApplicationFlags, Application}; use gtk::{glib, prelude::*}; +use std::env::args_os; +use std::path::PathBuf; use zbus::{blocking::Connection, dbus_proxy}; #[dbus_proxy( @@ -30,8 +32,22 @@ pub fn get_proxy() -> zbus::Result> { } fn main() -> Result<(), glib::Error> { + // Get config path from command line + let mut config_path: Option = None; + let mut args = args_os().into_iter(); + while let Some(arg) = args.next() { + match arg.to_str() { + Some("--config") => { + if let Some(path) = args.next() { + config_path = Some(path.into()); + } + } + _ => (), + } + } + // Parse Config - let _client_config = config::user::read_user_config() + let _client_config = config::user::read_user_config(config_path.as_deref()) .expect("Failed to parse config file") .client; @@ -52,6 +68,16 @@ fn main() -> Result<(), glib::Error> { let app = Application::new(Some(APPLICATION_NAME), ApplicationFlags::FLAGS_NONE); + // Config cmdline arg for documentation + app.add_main_option( + "config", + glib::Char::from(0), + OptionFlags::NONE, + OptionArg::String, + "Use a custom config file instead of looking for one.", + Some(""), + ); + // Capslock cmdline arg app.add_main_option( "caps-lock", diff --git a/src/config/user.rs b/src/config/user.rs index 179ba89..bcd98fc 100644 --- a/src/config/user.rs +++ b/src/config/user.rs @@ -2,6 +2,7 @@ use gtk::glib::system_config_dirs; use gtk::glib::user_config_dir; use serde_derive::Deserialize; use std::error::Error; +use std::path::Path; use std::path::PathBuf; #[derive(Deserialize, Default, Debug)] @@ -41,8 +42,8 @@ fn find_user_config() -> Option { None } -pub fn read_user_config() -> Result> { - let path = match find_user_config() { +pub fn read_user_config(path: Option<&Path>) -> Result> { + let path = match path.map(Path::to_owned).or_else(find_user_config) { Some(path) => path, None => return Ok(Default::default()), }; diff --git a/src/global_utils.rs b/src/global_utils.rs index fa64428..d786474 100644 --- a/src/global_utils.rs +++ b/src/global_utils.rs @@ -118,6 +118,7 @@ pub(crate) fn handle_application_args( } } "style" => continue, + "config" => continue, e => { eprintln!("Unknown Variant Key: \"{}\"!...", e); return (HandleLocalStatus::FAILURE, actions); diff --git a/src/server/application.rs b/src/server/application.rs index 77eb444..8a85fe0 100644 --- a/src/server/application.rs +++ b/src/server/application.rs @@ -26,6 +26,15 @@ impl SwayOSDApplication { pub fn new(server_config: ServerConfig, action_receiver: Receiver<(ArgTypes, String)>) -> Self { let app = Application::new(Some(APPLICATION_NAME), ApplicationFlags::FLAGS_NONE); + app.add_main_option( + "config", + glib::Char::from(0), + OptionFlags::NONE, + OptionArg::String, + "Use a custom config file instead of looking for one.", + Some(""), + ); + app.add_main_option( "style", glib::Char::from('s' as u8), diff --git a/src/server/main.rs b/src/server/main.rs index 21cbf0d..d2f1a97 100644 --- a/src/server/main.rs +++ b/src/server/main.rs @@ -79,11 +79,6 @@ fn main() { std::process::exit(1); } - // Parse Config - let server_config = config::user::read_user_config() - .expect("Failed to parse config file") - .server; - // Load the compiled resource bundle let resources_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/swayosd.gresource")); let resource_data = Bytes::from(&resources_bytes[..]); @@ -111,18 +106,37 @@ fn main() { None => eprintln!("Could not find the system CSS file..."), } - // Try loading the users CSS theme - let mut custom_user_css: Option = server_config.style.clone(); + // Get config path and CSS theme path from command line + let mut config_path: Option = None; + let mut custom_user_css: Option = None; let mut args = args_os().into_iter(); while let Some(arg) = args.next() { match arg.to_str() { - Some("-s") | Some("--style") => match args.next() { - Some(path) => custom_user_css = path.to_str().map(|s| PathBuf::from(s)), - _ => (), - }, + Some("--config") => { + if let Some(path) = args.next() { + config_path = Some(path.into()); + } + } + Some("-s") | Some("--style") => { + if let Some(path) = args.next() { + custom_user_css = Some(path.into()); + } + } _ => (), } } + + // Parse Config + let server_config = config::user::read_user_config(config_path.as_deref()) + .expect("Failed to parse config file") + .server; + + // Load style path from config if none is given on CLI + if custom_user_css.is_none() { + custom_user_css = server_config.style.clone(); + } + + // Try loading the users CSS theme if let Some(user_config_path) = user_style_path(custom_user_css) { let user_provider = CssProvider::new(); user_provider