Add example config

This commit is contained in:
Brian Tarricone 2022-05-04 00:27:18 -07:00
parent 92504d279a
commit 2ae04ff91a
5 changed files with 72 additions and 40 deletions

View File

@ -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
View 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"

View File

@ -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")
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()?.filter(|nlc| !nlc.is_empty());
.transpose()?
{
break Some(nlc);
}
} else {
break None;
}
};
let standalone = std::env::var("BSCREENSAVER_DIALOG_STANDALONE").is_ok();

View File

@ -279,9 +279,7 @@ fn parse_config() -> anyhow::Result<Configuration> {
use humantime::parse_duration;
let mut config = Configuration::default();
match load_configuration()? {
None => Ok(config),
Some(config_toml) => {
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'"))?)?,
@ -296,12 +294,11 @@ fn parse_config() -> anyhow::Result<Configuration> {
};
if config.blank_before_locking >= config.lock_timeout {
Err(anyhow!("'blank-before-locking' cannot be greater than 'lock-timeout'"))
} else {
Err(anyhow!("'blank-before-locking' cannot be greater than 'lock-timeout'"))?
}
}
Ok(config)
}
},
}
}
fn init_signals() -> anyhow::Result<SignalFd> {

View File

@ -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) => {
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))
},
}
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> {