From 2ae04ff91a2cb38486d3f8003d67798d8b6a111e Mon Sep 17 00:00:00 2001 From: "Brian J. Tarricone" Date: Wed, 4 May 2022 00:27:18 -0700 Subject: [PATCH] Add example config --- Makefile | 5 ++++- bscreensaver.toml.example | 20 ++++++++++++++++++++ dialog-gtk3/src/main.rs | 20 +++++++++++++++----- locker/src/main.rs | 39 ++++++++++++++++++--------------------- util/src/lib.rs | 28 +++++++++++++++------------- 5 files changed, 72 insertions(+), 40 deletions(-) create mode 100644 bscreensaver.toml.example diff --git a/Makefile b/Makefile index 553e23a..cd5c6bb 100644 --- a/Makefile +++ b/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 diff --git a/bscreensaver.toml.example b/bscreensaver.toml.example new file mode 100644 index 0000000..07ac040 --- /dev/null +++ b/bscreensaver.toml.example @@ -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" diff --git a/dialog-gtk3/src/main.rs b/dialog-gtk3/src/main.rs index 4bbf1c1..7f2ab13 100644 --- a/dialog-gtk3/src/main.rs +++ b/dialog-gtk3/src/main.rs @@ -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> = 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(); diff --git a/locker/src/main.rs b/locker/src/main.rs index 1acced7..d6100df 100644 --- a/locker/src/main.rs +++ b/locker/src/main.rs @@ -279,29 +279,26 @@ fn parse_config() -> anyhow::Result { 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 { diff --git a/util/src/lib.rs b/util/src/lib.rs index fcaff23..330c97d 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -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> { - 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 { + 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::()?; - Ok(Some(config_toml)) - }, - } + let config_toml = config.parse::()?; + Ok(config_toml) +} + +pub fn load_configuration() -> anyhow::Result> { + 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 {