aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher R. Nelson <christopher.nelson@languidnights.com>2026-11-01 00:27:57 -0400
committerChristopher R. Nelson <christopher.nelson@languidnights.com>2026-11-01 00:27:57 -0400
commitf1933ef4b012de715e637f489bbcb89d944496f8 (patch)
tree8a00179529584f6376dfa1a40d3713c30795bd41
parent7fb53e418a48b8baa7bcab89967a0755f2230162 (diff)
Save the chosen wallpaper and restore it at loginHEADmain
Menu items now call the program back with --set FILE, which runs the configured command and, if it exits 0, saves the path to $XDG_STATE_HOME/wallpaper-switcher/current. --restore sets the saved wallpaper again, for use in labwc/openbox autostart. Items use the binary's absolute path so the menu works when it is not on PATH. Paths from the command line are parsed as native namestrings so file names containing * or [ are not treated as wildcards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--README.md21
-rw-r--r--src/main.lisp63
-rw-r--r--src/menu.lisp37
-rw-r--r--src/package.lisp10
-rw-r--r--src/state.lisp41
-rw-r--r--t/tests.lisp89
-rw-r--r--wallpaper-switcher.asd1
7 files changed, 240 insertions, 22 deletions
diff --git a/README.md b/README.md
index 0ee66af..e43c21c 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,21 @@ In `menu.xml`:
Then reference `wallpapers` from your root menu and reconfigure Openbox.
+Picking an item runs `wallpaper-switcher --set FILE`, which runs the
+configured command and saves the path to
+`$XDG_STATE_HOME/wallpaper-switcher/current` (usually
+`~/.local/state/wallpaper-switcher/current`). Items call the program by its
+absolute path, so it does not need to be on `PATH`.
+
+## Restoring at login
+
+Add this to `~/.config/labwc/autostart` (or `~/.config/openbox/autostart`):
+
+ wallpaper-switcher --restore &
+
+It sets the saved wallpaper again with the current `command` setting. If you
+pass `--config` or `--command` to the menu, pass the same options here.
+
## Configuration
Optional. Put it in `$XDG_CONFIG_HOME/wallpaper-switcher/config` (usually
@@ -32,8 +47,10 @@ Optional. Put it in `$XDG_CONFIG_HOME/wallpaper-switcher/config` (usually
# hidden ones (starting with .) are skipped.
directory = ~/.wallpapers
-# Shell command run when an item is picked. %f is the file (already quoted
-# for the shell), %% is a literal %.
+# Shell command run when an item is picked or restored. %f is the file
+# (already quoted for the shell), %% is a literal %. It is run with sh -c and
+# waited for, so anything long-running (like swaybg) must end with &. The
+# wallpaper is only saved if the command exits with status 0.
command = pkill -x swaybg; swaybg -m fill -i %f &
# File extensions to include, matched without regard to case.
diff --git a/src/main.lisp b/src/main.lisp
index 27a7dbb..3a2fb35 100644
--- a/src/main.lisp
+++ b/src/main.lisp
@@ -2,8 +2,15 @@
(defparameter *usage*
"Usage: wallpaper-switcher [options]
+ wallpaper-switcher [options] --set FILE
+ wallpaper-switcher [options] --restore
-Print an Openbox pipe menu listing the wallpapers in a directory.
+With no mode, print an Openbox pipe menu listing the wallpapers in a
+directory. Each menu item calls back with --set.
+
+Modes:
+ -s, --set FILE Set FILE as the wallpaper and save it as current
+ -r, --restore Set the saved wallpaper again (for autostart)
Options:
-c, --config FILE Read settings from FILE
@@ -29,13 +36,21 @@ Options:
(setf (getf opts :directory) (value)))
((member arg '("-e" "--command") :test #'string=)
(setf (getf opts :command) (value)))
+ ((member arg '("-s" "--set") :test #'string=)
+ (setf (getf opts :set) (value)))
+ ((member arg '("-r" "--restore") :test #'string=)
+ (setf (getf opts :restore) t))
(t (error "unknown argument ~S" arg))))))
+ (when (and (getf opts :set) (getf opts :restore))
+ (error "--set and --restore cannot be used together"))
opts))
(defun resolve-config (opts)
"Defaults, then the config file, then command-line options."
(let* ((explicit (getf opts :config))
- (path (if explicit (expand-path explicit) (default-config-path))))
+ (path (if explicit
+ (uiop:parse-native-namestring (expand-path explicit))
+ (default-config-path))))
(when (and explicit (not (probe-file path)))
(error "config file ~A does not exist" path))
(let ((config (load-config path)))
@@ -45,6 +60,41 @@ Options:
(setf (config-command config) (getf opts :command)))
config)))
+(defun set-wallpaper (config file)
+ "Run the configured command for FILE and, if it succeeds, save FILE as
+the current wallpaper."
+ ;; Parse as a native path: file names may contain * or [, which a Lisp
+ ;; namestring would treat as wildcards.
+ (let ((path (probe-file (uiop:parse-native-namestring (expand-path file)))))
+ (unless (and path (not (uiop:directory-pathname-p path)))
+ (error "no such file: ~A" file))
+ (let* ((native (uiop:native-namestring path))
+ (code (run-wallpaper-command (config-command config) native)))
+ (unless (zerop code)
+ (error "wallpaper command exited with status ~D; not saving ~A" code native))
+ (write-state path))))
+
+(defun restore-wallpaper (config)
+ "Set the saved wallpaper again."
+ (let ((saved (read-state)))
+ (unless saved
+ (error "no saved wallpaper in ~A" (uiop:native-namestring (state-file-path))))
+ (set-wallpaper config saved)))
+
+(defun self-command (opts)
+ "The program path plus the options a menu item should pass on to --set."
+ (append (list (or #+sbcl (and sb-ext:*runtime-pathname*
+ (uiop:native-namestring sb-ext:*runtime-pathname*))
+ (uiop:argv0)
+ "wallpaper-switcher"))
+ (when (getf opts :config)
+ (list "--config" (uiop:native-namestring
+ (merge-pathnames (uiop:parse-native-namestring
+ (expand-path (getf opts :config)))
+ (uiop:getcwd)))))
+ (when (getf opts :command)
+ (list "--command" (getf opts :command)))))
+
(defun run (args)
"Do the work for ARGS and return an exit code. Safe to call from a REPL."
(handler-case
@@ -55,7 +105,10 @@ Options:
(handler-bind ((warning (lambda (w)
(format *error-output* "wallpaper-switcher: ~A~%" w)
(muffle-warning w))))
- (render-menu (resolve-config opts))))
+ (let ((config (resolve-config opts)))
+ (cond ((getf opts :set) (set-wallpaper config (getf opts :set)))
+ ((getf opts :restore) (restore-wallpaper config))
+ (t (render-menu config))))))
(finish-output)
0)
(error (e)
@@ -64,4 +117,6 @@ Options:
(defun main ()
"Entry point for the built executable."
- (uiop:quit (run (uiop:command-line-arguments))))
+ (let ((args (uiop:command-line-arguments)))
+ (uiop:quit (let ((*self-command* (self-command (ignore-errors (parse-args args)))))
+ (run args)))))
diff --git a/src/menu.lisp b/src/menu.lisp
index da99ab0..b65a8c6 100644
--- a/src/menu.lisp
+++ b/src/menu.lisp
@@ -90,24 +90,43 @@ directory's last component."
(t (write-char c out) (incf i))))
(progn (write-char c out) (incf i)))))))
-(defun build-command (template pathname)
- "The command line for one menu item. Openbox splits Execute commands
-like a shell does but does not run them through one, so the expanded
-template is passed to `sh -c` as a single quoted argument."
- (format nil "sh -c ~A"
- (shell-quote (expand-template template (uiop:native-namestring pathname)))))
+(defvar *self-command* '("wallpaper-switcher")
+ "How a menu item calls this program back: the executable followed by any
+options (--config, --command) that should carry over to --set. MAIN sets
+it to the executable's absolute path, so the menu still works when the
+program is not on PATH.")
+
+(defun build-command (pathname)
+ "The Execute command for one menu item: call this program back with
+--set, which sets the wallpaper and saves it for --restore. Openbox
+splits the command into words like a shell would, so each word is
+shell-quoted."
+ (format nil "~{~A~^ ~}"
+ (mapcar #'shell-quote
+ (append *self-command*
+ (list "--set" (uiop:native-namestring pathname))))))
+
+(defun run-wallpaper-command (template file)
+ "Run TEMPLATE for FILE under `sh -c` and return its exit code. This waits
+for the shell, which is why the default command ends with &: swaybg keeps
+running, so it has to go in the background."
+ (nth-value 2 (uiop:run-program (list "/bin/sh" "-c" (expand-template template file))
+ :input nil
+ :output :interactive
+ :error-output :interactive
+ :ignore-error-status t)))
;;; Rendering
(defun indent (stream depth)
(loop repeat (* 2 depth) do (write-char #\Space stream)))
-(defun render-item (stream depth pathname command)
+(defun render-item (stream depth pathname)
(indent stream depth)
(format stream "<item label=\"~A\">~%" (xml-escape (menu-label pathname)))
(indent stream (1+ depth))
(format stream "<action name=\"Execute\"><command>~A</command></action>~%"
- (xml-escape (build-command command pathname)))
+ (xml-escape (build-command pathname)))
(indent stream depth)
(format stream "</item>~%"))
@@ -141,7 +160,7 @@ rather than recursed into forever."
(indent stream depth)
(format stream "</menu>~%"))
(dolist (f files)
- (render-item stream depth f (config-command config)))))
+ (render-item stream depth f))))
(defun render-menu (config &optional (stream *standard-output*))
"Write the Openbox pipe menu XML for CONFIG to STREAM."
diff --git a/src/package.lisp b/src/package.lisp
index dcf297e..99773cc 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -19,4 +19,14 @@
#:xml-escape
#:menu-label
#:build-command
+ #:expand-template
+ #:run-wallpaper-command
+ #:*self-command*
+ ;; state
+ #:*state-file*
+ #:state-file-path
+ #:write-state
+ #:read-state
+ #:set-wallpaper
+ #:restore-wallpaper
#:render-menu))
diff --git a/src/state.lisp b/src/state.lisp
new file mode 100644
index 0000000..43b643e
--- /dev/null
+++ b/src/state.lisp
@@ -0,0 +1,41 @@
+(in-package #:wallpaper-switcher)
+
+;;; Remembering the current wallpaper
+;;;
+;;; The path of the last wallpaper set is kept in
+;;; $XDG_STATE_HOME/wallpaper-switcher/current (by default
+;;; ~/.local/state/wallpaper-switcher/current) as a single line, so
+;;; `--restore` can set it again at login.
+
+(defvar *state-file* nil
+ "Overrides the state file location when non-NIL (used by the tests).")
+
+(defun state-file-path ()
+ (or *state-file*
+ (let ((base (uiop:getenv-absolute-directory "XDG_STATE_HOME")))
+ (merge-pathnames "wallpaper-switcher/current"
+ (or base (merge-pathnames ".local/state/"
+ (user-homedir-pathname)))))))
+
+(defun write-state (pathname)
+ "Save PATHNAME as the current wallpaper. Writes a temporary file and
+renames it over the old one, so a crash never leaves a half-written file.
+The temporary file differs by name, not extension: RENAME-FILE fills a
+missing extension in from the source, so renaming current.tmp to current
+would land back on current.tmp."
+ (let* ((path (state-file-path))
+ (tmp (make-pathname :name (format nil "~A-tmp" (pathname-name path))
+ :defaults path)))
+ (ensure-directories-exist path)
+ (with-open-file (out tmp :direction :output :if-exists :supersede)
+ (write-line (uiop:native-namestring pathname) out))
+ (uiop:rename-file-overwriting-target tmp path)
+ path))
+
+(defun read-state ()
+ "The saved wallpaper path as a string, or NIL if none has been saved."
+ (let ((path (state-file-path)))
+ (when (probe-file path)
+ (let ((line (string-trim '(#\Space #\Tab #\Newline #\Return)
+ (uiop:read-file-string path))))
+ (unless (string= line "") line)))))
diff --git a/t/tests.lisp b/t/tests.lisp
index bdb109f..4fc0665 100644
--- a/t/tests.lisp
+++ b/t/tests.lisp
@@ -52,7 +52,7 @@
`(call-with-temp-dir (lambda (,var) ,@body)))
(defun touch (dir name)
- (with-open-file (s (merge-pathnames name dir) :direction :output
+ (with-open-file (s (merge-pathnames (uiop:parse-native-namestring name) dir) :direction :output
:if-does-not-exist :create)
(declare (ignorable s))))
@@ -102,9 +102,14 @@ extensions = .PNG, jpg webp")))
(check-equal '("png" "webp") (config-extensions c))
(check (search "swaybg" (config-command c)))))
+(deftest test-expand-template
+ (check-equal "swaybg -i '/w/it'\\''s.png' 100%"
+ (expand-template "swaybg -i %f 100%%" "/w/it's.png")))
+
(deftest test-build-command
- (check-equal "sh -c 'swaybg -i '\\''/w/a b.png'\\'' 100%'"
- (build-command "swaybg -i %f 100%%" #p"/w/a b.png")))
+ (let ((*self-command* '("/opt/wall switcher" "--command" "feh %f")))
+ (check-equal "'/opt/wall switcher' '--command' 'feh %f' '--set' '/w/a b.png'"
+ (build-command #p"/w/a b.png"))))
(deftest test-find-wallpapers
(with-temp-dir (dir)
@@ -123,13 +128,13 @@ extensions = .PNG, jpg webp")))
(with-temp-dir (dir)
(touch dir "tom & jerry.png")
(let ((xml (with-output-to-string (s)
- (render-menu (make-config :directory (namestring dir)
- :command "swaybg -i %f")
- s))))
+ (let ((*self-command* '("/bin/ws")))
+ (render-menu (make-config :directory (namestring dir)) s)))))
(check (search "<openbox_pipe_menu>" xml))
(check (search "label=\"tom &amp; jerry\"" xml))
- (check (search "<action name=\"Execute\"><command>sh -c &apos;swaybg -i &apos;\\&apos;&apos;"
+ (check (search "<action name=\"Execute\"><command>&apos;/bin/ws&apos; &apos;--set&apos; &apos;"
xml))
+ (check (search "tom &amp; jerry.png&apos;</command>" xml))
(check (search "</openbox_pipe_menu>" xml)))))
(deftest test-render-menu-empty
@@ -207,3 +212,73 @@ extensions = .PNG, jpg webp")))
(let ((xml (render-to-string dir)))
(check (search "label=\"a\"" xml))
(check (not (search "label=\"loop\"" xml)))))))
+
+;;; Saving and restoring
+
+(defmacro with-state-in ((dir) &body body)
+ `(let ((*state-file* (merge-pathnames "state/current" ,dir)))
+ ,@body))
+
+(defun recording-config (dir)
+ "A config whose command appends the chosen file to DIR/log."
+ (make-config :command (format nil "printf '%s\\n' %f >> ~A"
+ (shell-quote (uiop:native-namestring
+ (merge-pathnames "log" dir))))))
+
+(defun log-lines (dir)
+ (let ((log (merge-pathnames "log" dir)))
+ (when (probe-file log) (uiop:read-file-lines log))))
+
+(deftest test-state-round-trip
+ (with-temp-dir (dir)
+ (with-state-in (dir)
+ (check-equal nil (read-state))
+ (write-state #p"/w/first.png")
+ (write-state #p"/w/second one.png")
+ (check-equal "/w/second one.png" (read-state))
+ (check (not (probe-file (merge-pathnames "state/current-tmp" dir)))))))
+
+(deftest test-state-file-default-location
+ (let ((*state-file* nil))
+ (check (search "wallpaper-switcher/current"
+ (uiop:native-namestring (state-file-path))))))
+
+(deftest test-set-wallpaper-runs-command-and-saves
+ (with-temp-dir (dir)
+ (with-state-in (dir)
+ (touch dir "it's [odd]*.png")
+ (let ((file (uiop:native-namestring (merge-pathnames (uiop:parse-native-namestring "it's [odd]*.png") dir))))
+ (wallpaper-switcher::set-wallpaper (recording-config dir) file)
+ (check-equal (list file) (log-lines dir))
+ (check-equal file (read-state))))))
+
+(deftest test-set-wallpaper-failure-does-not-save
+ (with-temp-dir (dir)
+ (with-state-in (dir)
+ (touch dir "a.png")
+ (let ((errored nil))
+ (handler-case
+ (wallpaper-switcher::set-wallpaper (make-config :command "exit 3")
+ (namestring (merge-pathnames "a.png" dir)))
+ (error () (setf errored t)))
+ (check errored)
+ (check-equal nil (read-state))))))
+
+(deftest test-set-wallpaper-missing-file
+ (with-temp-dir (dir)
+ (with-state-in (dir)
+ (check (nth-value 1 (ignore-errors
+ (wallpaper-switcher::set-wallpaper
+ (recording-config dir) "/nonexistent/x.png"))))
+ (check-equal nil (log-lines dir)))))
+
+(deftest test-restore-wallpaper
+ (with-temp-dir (dir)
+ (with-state-in (dir)
+ (check (nth-value 1 (ignore-errors
+ (wallpaper-switcher::restore-wallpaper (recording-config dir)))))
+ (touch dir "b.webp")
+ (let ((file (uiop:native-namestring (merge-pathnames "b.webp" dir))))
+ (write-state (merge-pathnames "b.webp" dir))
+ (wallpaper-switcher::restore-wallpaper (recording-config dir))
+ (check-equal (list file) (log-lines dir))))))
diff --git a/wallpaper-switcher.asd b/wallpaper-switcher.asd
index 6df70b7..22aa511 100644
--- a/wallpaper-switcher.asd
+++ b/wallpaper-switcher.asd
@@ -8,6 +8,7 @@
:serial t
:components ((:file "package")
(:file "config")
+ (:file "state")
(:file "menu")
(:file "main"))
:build-operation "program-op"