From f1933ef4b012de715e637f489bbcb89d944496f8 Mon Sep 17 00:00:00 2001 From: "Christopher R. Nelson" Date: Sun, 1 Nov 2026 00:27:57 -0400 Subject: Save the chosen wallpaper and restore it at login 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 --- src/menu.lisp | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'src/menu.lisp') 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 "~%" (xml-escape (menu-label pathname))) (indent stream (1+ depth)) (format stream "~A~%" - (xml-escape (build-command command pathname))) + (xml-escape (build-command pathname))) (indent stream depth) (format stream "~%")) @@ -141,7 +160,7 @@ rather than recursed into forever." (indent stream depth) (format stream "~%")) (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." -- cgit v1.2.3