aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.lisp63
-rw-r--r--src/menu.lisp37
-rw-r--r--src/package.lisp10
-rw-r--r--src/state.lisp41
4 files changed, 138 insertions, 13 deletions
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)))))