Skip to content

Commit

Permalink
Parse config file in server, client, and input-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdi265 committed Feb 18, 2024
1 parent 438fd15 commit 35a2fc9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub fn get_proxy() -> zbus::Result<ServerProxyBlocking<'static>> {
}

fn main() -> Result<(), glib::Error> {
// Parse Config
let _client_config = config::user::read_user_config()
.expect("Failed to parse config file")
.client;

// Make sure that the server is running
let proxy = match get_proxy() {
Ok(proxy) => match proxy.introspect() {
Expand Down
5 changes: 5 additions & 0 deletions src/input-backend/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ impl LibinputInterface for Interface {
}

fn main() -> Result<(), zbus::Error> {
// Parse Config
let _input_config = config::backend::read_backend_config()
.expect("Failed to parse config file")
.input;

// Create DBUS server
let connection = task::block_on(DbusServer.init());
let object_server = connection.object_server();
Expand Down
43 changes: 26 additions & 17 deletions src/server/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

use super::config::user::ServerConfig;

#[derive(Clone, Shrinkwrap)]
pub struct SwayOSDApplication {
#[shrinkwrap(main_field)]
Expand All @@ -21,7 +23,7 @@ pub struct SwayOSDApplication {
}

impl SwayOSDApplication {
pub fn new(action_receiver: Receiver<(ArgTypes, String)>) -> Self {
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(
Expand Down Expand Up @@ -50,6 +52,16 @@ impl SwayOSDApplication {
windows: Rc::new(RefCell::new(Vec::new())),
};

// Apply Server Config
if let Some(margin) = server_config.top_margin {
if (0_f32..1_f32).contains(&margin) {
set_top_margin(margin);
}
}
if let Some(max_volume) = server_config.max_volume {
set_default_max_volume(max_volume);
}

// Parse args
app.connect_handle_local_options(clone!(@strong osd_app => move |_app, args| {
let actions = match handle_application_args(args.to_variant()) {
Expand All @@ -59,24 +71,21 @@ impl SwayOSDApplication {
for (arg_type, data) in actions {
match (arg_type, data) {
(ArgTypes::TopMargin, margin) => {
let margin: Option<f32> = match margin {
Some(margin) => match margin.parse::<f32>() {
Ok(margin) => (0_f32..1_f32).contains(&margin).then_some(margin),
_ => None,
},
_ => None,
};
set_top_margin(margin.unwrap_or(*TOP_MARGIN_DEFAULT))
let margin: Option<f32> = margin
.and_then(|margin| margin.parse().ok())
.and_then(|margin| (0_f32..1_f32).contains(&margin).then_some(margin));

if let Some(margin) = margin {
set_top_margin(margin)
}
},
(ArgTypes::MaxVolume, max) => {
let volume: u8 = match max {
Some(max) => match max.parse() {
Ok(max) => max,
_ => get_default_max_volume(),
}
_ => get_default_max_volume(),
};
set_default_max_volume(volume);
let max: Option<u8> = max
.and_then(|max| max.parse().ok());

if let Some(max) = max {
set_default_max_volume(max);
}
},
(arg_type, data) => Self::action_activated(&osd_app, arg_type, data),
}
Expand Down
9 changes: 7 additions & 2 deletions src/server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ 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 @@ -107,7 +112,7 @@ fn main() {
}

// Try loading the users CSS theme
let mut custom_user_css: Option<PathBuf> = None;
let mut custom_user_css: Option<PathBuf> = server_config.style.clone();
let mut args = args_os().into_iter();
while let Some(arg) = args.next() {
match arg.to_str() {
Expand Down Expand Up @@ -135,5 +140,5 @@ fn main() {
// Start the DBus Server
async_std::task::spawn(DbusServer::new(sender));
// Start the GTK Application
std::process::exit(SwayOSDApplication::new(receiver).start());
std::process::exit(SwayOSDApplication::new(server_config, receiver).start());
}

0 comments on commit 35a2fc9

Please sign in to comment.