Skip to content

Commit

Permalink
Add --config option for server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdi265 committed May 6, 2024
1 parent 78d9910 commit e62e950
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
28 changes: 27 additions & 1 deletion src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -30,8 +32,22 @@ pub fn get_proxy() -> zbus::Result<ServerProxyBlocking<'static>> {
}

fn main() -> Result<(), glib::Error> {
// Get config path from command line
let mut config_path: Option<PathBuf> = 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;

Expand All @@ -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("<CONFIG FILE PATH>"),
);

// Capslock cmdline arg
app.add_main_option(
"caps-lock",
Expand Down
5 changes: 3 additions & 2 deletions src/config/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -41,8 +42,8 @@ fn find_user_config() -> Option<PathBuf> {
None
}

pub fn read_user_config() -> Result<UserConfig, Box<dyn Error>> {
let path = match find_user_config() {
pub fn read_user_config(path: Option<&Path>) -> Result<UserConfig, Box<dyn Error>> {
let path = match path.map(Path::to_owned).or_else(find_user_config) {
Some(path) => path,
None => return Ok(Default::default()),
};
Expand Down
1 change: 1 addition & 0 deletions src/global_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub(crate) fn handle_application_args(
}
}
"style" => continue,
"config" => continue,
e => {
eprintln!("Unknown Variant Key: \"{}\"!...", e);
return (HandleLocalStatus::FAILURE, actions);
Expand Down
9 changes: 9 additions & 0 deletions src/server/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<CONFIG FILE PATH>"),
);

app.add_main_option(
"style",
glib::Char::from('s' as u8),
Expand Down
36 changes: 25 additions & 11 deletions src/server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..]);
Expand Down Expand Up @@ -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<PathBuf> = server_config.style.clone();
// Get config path and CSS theme path from command line
let mut config_path: Option<PathBuf> = None;
let mut custom_user_css: Option<PathBuf> = 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
Expand Down

0 comments on commit e62e950

Please sign in to comment.