-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config file and implement config file parsing (#79)
* Add config file format and config file parsing * Parse config file in server, client, and input-backend * Add --config option for server and client
- Loading branch information
Showing
10 changed files
with
238 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use gtk::glib::system_config_dirs; | ||
use serde_derive::Deserialize; | ||
use std::error::Error; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Deserialize, Default, Debug)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct InputBackendConfig {} | ||
|
||
#[derive(Deserialize, Default, Debug)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct BackendConfig { | ||
#[serde(default)] | ||
pub input: InputBackendConfig, | ||
} | ||
|
||
fn find_backend_config() -> Option<PathBuf> { | ||
for path in system_config_dirs() { | ||
let path = path.join("swayosd").join("backend.toml"); | ||
if path.exists() { | ||
return Some(path); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
pub fn read_backend_config() -> Result<BackendConfig, Box<dyn Error>> { | ||
let path = match find_backend_config() { | ||
Some(path) => path, | ||
None => return Ok(Default::default()), | ||
}; | ||
|
||
let config_file = std::fs::read_to_string(path)?; | ||
let config: BackendConfig = toml::from_str(&config_file)?; | ||
Ok(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct ClientConfig {} | ||
|
||
#[derive(Deserialize, Default, Debug)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct ServerConfig { | ||
pub style: Option<PathBuf>, | ||
pub top_margin: Option<f32>, | ||
pub max_volume: Option<u8>, | ||
} | ||
|
||
#[derive(Deserialize, Default, Debug)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct UserConfig { | ||
#[serde(default)] | ||
pub server: ServerConfig, | ||
#[serde(default)] | ||
pub client: ClientConfig, | ||
} | ||
|
||
fn find_user_config() -> Option<PathBuf> { | ||
let path = user_config_dir().join("swayosd").join("config.toml"); | ||
if path.exists() { | ||
return Some(path); | ||
} | ||
|
||
for path in system_config_dirs() { | ||
let path = path.join("swayosd").join("config.toml"); | ||
if path.exists() { | ||
return Some(path); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
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()), | ||
}; | ||
|
||
let config_file = std::fs::read_to_string(path)?; | ||
let config: UserConfig = toml::from_str(&config_file)?; | ||
Ok(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.