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/state.lisp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/state.lisp (limited to 'src/state.lisp') 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))))) -- cgit v1.2.3