-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add config file and implement config file parsing #79
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some config dir information in the client, server, and backends
--help
print informationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good idea.
Though, I can't find a way to add text to
--help
without adding another option withgtk-rs
. 😅I think one way to workaround that would be to add a
--config
option that allows specifying a custom config path, and explain the config stuff in the help text for that option.I'll look at this at some point in the next few days, when I have a bit more time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, true. Sounds like a good idea 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented the
--config
option today. Sorry it took so long. The libinput backend doesn't have any command line parsing at the moment, so I left it out there, but I can add similar parsing to it as with client/server.The help message isn't final yet, I still need to think about how to best communicate the location without hardcoding paths or making the message too long.
Let me know if you like the basic implementation outline or if you'd like it done differently.