Add example config
This commit is contained in:
parent
92504d279a
commit
2ae04ff91a
5
Makefile
5
Makefile
@ -3,7 +3,9 @@
|
||||
PREFIX ?= /usr/local
|
||||
BINDIR ?= $(PREFIX)/bin
|
||||
LIBEXECDIR ?= $(PREFIX)/libexec
|
||||
SYSCONFDIR ?= $(PREFIX)/etc
|
||||
|
||||
CONFIG_DIR = $(SYSCONFDIR)/xdg/bscreensaver
|
||||
HELPER_DIR = $(LIBEXECDIR)/bscreensaver
|
||||
HELPERS = \
|
||||
bscreensaver-dbus-service \
|
||||
@ -24,9 +26,10 @@ build-dev:
|
||||
HELPER_DIR=target/debug cargo build
|
||||
|
||||
install: build
|
||||
$(INSTALL) -m 0755 -d $(BINDIR) $(HELPER_DIR)
|
||||
$(INSTALL) -m 0755 -d $(BINDIR) $(HELPER_DIR) $(CONFIG_DIR)
|
||||
$(INSTALL) -m 0755 target/release/bscreensaver $(BINDIR)
|
||||
$(INSTALL) -m 0755 $(addprefix target/release/,$(HELPERS)) $(HELPER_DIR)
|
||||
$(INSTALL) -m 0644 bscreensaver.toml.example $(CONFIG_DIR)
|
||||
|
||||
clean:
|
||||
cargo clean
|
||||
|
20
bscreensaver.toml.example
Normal file
20
bscreensaver.toml.example
Normal file
@ -0,0 +1,20 @@
|
||||
# Config files are searched for in this order:
|
||||
#
|
||||
# $XDG_CONFIG_DIRS/bscreensaver/bscreensaver.toml
|
||||
# $XDG_CONFIG_HOME/bscreensaver/bscreensaver.toml
|
||||
#
|
||||
# Settings specified in files later in the search path override
|
||||
# settings from prior files. $XDG_CONFIG_DIRS (default /etc/xdg/) is a
|
||||
# colon-separated list of directories, while $XDG_CONFIG_HOME (default
|
||||
# $HOME/.config/) is a single directory.
|
||||
|
||||
# Lock the screen after this amount of time of no user activity
|
||||
lock-timeout = "10m"
|
||||
# Blank the screen this amount of time before locking (must be less
|
||||
# than 'lock-timeout')
|
||||
blank-before-locking = "1m"
|
||||
# Backend to use for the unlock dialog. Current options: 'gtk3'
|
||||
dialog-backend = "gtk3"
|
||||
# Adds a 'New Login' button to the unlock dialog that will run the
|
||||
# specified command when clicked
|
||||
new-login-command = "dm-tool switch-to-greeter"
|
@ -14,11 +14,21 @@ const DIALOG_TIMEOUT: Duration = Duration::from_secs(60);
|
||||
fn main() -> anyhow::Result<()> {
|
||||
init_logging("BSCREENSAVER_DIALOG_GTK3_LOG");
|
||||
|
||||
let mut new_login_command = load_configuration()?.and_then(|config_toml| {
|
||||
config_toml.get("new-login-command")
|
||||
.and_then(|nlc| nlc.as_str().map(|s| s.to_string()))
|
||||
.map(|nlc| shell_words::split(&nlc))
|
||||
}).transpose()?.filter(|nlc| !nlc.is_empty());
|
||||
let config_tomls = load_configuration()?;
|
||||
let mut config_tomls_iter = config_tomls.into_iter().rev();
|
||||
let mut new_login_command: Option<Vec<String>> = loop {
|
||||
if let Some(config_toml) = config_tomls_iter.next() {
|
||||
if let Some(nlc) = config_toml.get("new-login-command")
|
||||
.and_then(|nlc| nlc.as_str().map(|s| s.to_string()))
|
||||
.map(|nlc| shell_words::split(&nlc))
|
||||
.transpose()?
|
||||
{
|
||||
break Some(nlc);
|
||||
}
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
};
|
||||
|
||||
let standalone = std::env::var("BSCREENSAVER_DIALOG_STANDALONE").is_ok();
|
||||
|
||||
|
@ -279,29 +279,26 @@ fn parse_config() -> anyhow::Result<Configuration> {
|
||||
use humantime::parse_duration;
|
||||
|
||||
let mut config = Configuration::default();
|
||||
match load_configuration()? {
|
||||
None => Ok(config),
|
||||
Some(config_toml) => {
|
||||
config.lock_timeout = match config_toml.get("lock-timeout") {
|
||||
None => config.lock_timeout,
|
||||
Some(val) => parse_duration(val.as_str().ok_or(anyhow!("'lock-timeout' must be a duration string like '10m' or '90s'"))?)?,
|
||||
};
|
||||
config.blank_before_locking = match config_toml.get("blank-before-locking") {
|
||||
None => config.blank_before_locking,
|
||||
Some(val) => parse_duration(val.as_str().ok_or(anyhow!("'blank-before-locking' must be a duration string like '10m' or '90s'"))?)?,
|
||||
};
|
||||
config.dialog_backend = match config_toml.get("dialog-backend") {
|
||||
None => config.dialog_backend,
|
||||
Some(val) => DialogBackend::try_from(val.as_str().ok_or(anyhow!("'dialog-backend' must be a string"))?)?,
|
||||
};
|
||||
for config_toml in load_configuration()? {
|
||||
config.lock_timeout = match config_toml.get("lock-timeout") {
|
||||
None => config.lock_timeout,
|
||||
Some(val) => parse_duration(val.as_str().ok_or(anyhow!("'lock-timeout' must be a duration string like '10m' or '90s'"))?)?,
|
||||
};
|
||||
config.blank_before_locking = match config_toml.get("blank-before-locking") {
|
||||
None => config.blank_before_locking,
|
||||
Some(val) => parse_duration(val.as_str().ok_or(anyhow!("'blank-before-locking' must be a duration string like '10m' or '90s'"))?)?,
|
||||
};
|
||||
config.dialog_backend = match config_toml.get("dialog-backend") {
|
||||
None => config.dialog_backend,
|
||||
Some(val) => DialogBackend::try_from(val.as_str().ok_or(anyhow!("'dialog-backend' must be a string"))?)?,
|
||||
};
|
||||
|
||||
if config.blank_before_locking >= config.lock_timeout {
|
||||
Err(anyhow!("'blank-before-locking' cannot be greater than 'lock-timeout'"))
|
||||
} else {
|
||||
Ok(config)
|
||||
}
|
||||
},
|
||||
if config.blank_before_locking >= config.lock_timeout {
|
||||
Err(anyhow!("'blank-before-locking' cannot be greater than 'lock-timeout'"))?
|
||||
}
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn init_signals() -> anyhow::Result<SignalFd> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::{ffi::CStr, fs::File, io::{self, Read}};
|
||||
use std::{ffi::CStr, fs::File, io::{self, Read}, path::PathBuf};
|
||||
use toml::Value;
|
||||
use xcb::x;
|
||||
|
||||
@ -11,19 +11,21 @@ pub fn init_logging(env_name: &str) {
|
||||
.init();
|
||||
}
|
||||
|
||||
pub fn load_configuration() -> anyhow::Result<Option<Value>> {
|
||||
match xdg::BaseDirectories::new()?.find_config_file("bscreensaver.toml") {
|
||||
None => Ok(None),
|
||||
Some(config_path) => {
|
||||
let mut f = File::open(config_path)?;
|
||||
let mut config = String::new();
|
||||
f.read_to_string(&mut config)?;
|
||||
drop(f);
|
||||
fn parse_config_toml(config_path: &PathBuf) -> anyhow::Result<Value> {
|
||||
let mut f = File::open(config_path)?;
|
||||
let mut config = String::new();
|
||||
f.read_to_string(&mut config)?;
|
||||
drop(f);
|
||||
|
||||
let config_toml = config.parse::<Value>()?;
|
||||
Ok(Some(config_toml))
|
||||
},
|
||||
}
|
||||
let config_toml = config.parse::<Value>()?;
|
||||
Ok(config_toml)
|
||||
}
|
||||
|
||||
pub fn load_configuration() -> anyhow::Result<Vec<Value>> {
|
||||
xdg::BaseDirectories::new()?
|
||||
.find_config_files("bscreensaver/bscreensaver.toml")
|
||||
.map(|config_path| parse_config_toml(&config_path))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn create_atom(conn: &xcb::Connection, name: &[u8]) -> xcb::Result<x::Atom> {
|
||||
|
Loading…
Reference in New Issue
Block a user