(in-package #:wallpaper-switcher)
;;; Finding wallpapers
(defun wallpaper-p (pathname extensions)
(let ((type (pathname-type pathname)))
(and type (member type extensions :test #'string-equal))))
(defun find-wallpapers (directory extensions)
"Image files directly inside DIRECTORY (not subdirectories), sorted by
name. Returns NIL if DIRECTORY does not exist."
(let ((dir (uiop:ensure-directory-pathname (expand-path directory))))
(when (uiop:directory-exists-p dir)
(sort (remove-if-not (lambda (p) (wallpaper-p p extensions))
(uiop:directory-files dir))
#'string-lessp
:key #'file-namestring))))
(defun directory-name (directory)
"The last component of DIRECTORY, e.g. \"nature\" for /walls/nature/."
(let ((last (car (last (pathname-directory directory)))))
(if (stringp last) last (namestring directory))))
(defun hidden-p (name)
(and (plusp (length name)) (char= (char name 0) #\.)))
(defun find-subdirectories (directory)
"Non-hidden subdirectories directly inside DIRECTORY, sorted by name."
(let ((dir (uiop:ensure-directory-pathname (expand-path directory))))
(when (uiop:directory-exists-p dir)
(sort (remove-if (lambda (d) (hidden-p (directory-name d)))
(uiop:subdirectories dir))
#'string-lessp
:key #'directory-name))))
;;; Escaping
(defun shell-quote (string)
"Quote STRING as one POSIX shell word: wrap it in single quotes and write
each embedded ' as '\\''."
(with-output-to-string (out)
(write-char #\' out)
(loop for c across string
do (if (char= c #\')
(write-string "'\\''" out)
(write-char c out)))
(write-char #\' out)))
(defun xml-escape (string)
(with-output-to-string (out)
(loop for c across string
do (case c
(#\& (write-string "&" out))
(#\< (write-string "<" out))
(#\> (write-string ">" out))
(#\" (write-string """ out))
(#\' (write-string "'" out))
(t (write-char c out))))))
(defun escape-mnemonic (name)
"Openbox reads _ in a label as a keyboard shortcut marker, so each _ is
doubled to show it as-is."
(with-output-to-string (out)
(loop for c across name
do (write-char c out)
(when (char= c #\_) (write-char c out)))))
(defun menu-label (pathname)
"Menu label for PATHNAME: a file's name without its extension, or a
directory's last component."
(escape-mnemonic
(if (uiop:directory-pathname-p pathname)
(directory-name pathname)
(or (pathname-name pathname) (file-namestring pathname)))))
;;; Building the Execute command
(defun expand-template (template file)
"Replace %f in TEMPLATE with FILE shell-quoted, and %% with %."
(with-output-to-string (out)
(loop with i = 0
with n = (length template)
while (< i n)
do (let ((c (char template i)))
(if (and (char= c #\%) (< (1+ i) n))
(let ((next (char template (1+ i))))
(case next
(#\f (write-string (shell-quote file) out) (incf i 2))
(#\% (write-char #\% out) (incf i 2))
(t (write-char c out) (incf i))))
(progn (write-char c out) (incf i)))))))
(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)
(indent stream depth)
(format stream "- ~%" (xml-escape (menu-label pathname)))
(indent stream (1+ depth))
(format stream "~A~%"
(xml-escape (build-command pathname)))
(indent stream depth)
(format stream "
~%"))
(defun render-empty (stream depth directory)
(indent stream depth)
(format stream "~%"
(xml-escape (format nil "No wallpapers in ~A"
(uiop:native-namestring directory)))))
(defun submenu-id (directory)
"Openbox needs a unique id for every inline submenu; the path is one."
(format nil "wallpaper-switcher:~A" (uiop:native-namestring directory)))
(defun render-directory (stream depth directory config visited)
"Write DIRECTORY's contents at DEPTH: a submenu for each subdirectory,
then an item for each wallpaper. A directory with neither gets a
\"No wallpapers in ...\" header. VISITED holds the truenames already
rendered on this branch, so a symlink pointing back up the tree is skipped
rather than recursed into forever."
(let ((subdirs (remove-if (lambda (d) (member (truename d) visited :test #'equal))
(find-subdirectories (namestring directory))))
(files (find-wallpapers (namestring directory) (config-extensions config))))
(when (and (null subdirs) (null files))
(render-empty stream depth directory))
(dolist (d subdirs)
(indent stream depth)
(format stream "~%"))
(dolist (f files)
(render-item stream depth f))))
(defun render-menu (config &optional (stream *standard-output*))
"Write the Openbox pipe menu XML for CONFIG to STREAM."
(let ((root (uiop:ensure-directory-pathname
(expand-path (config-directory config)))))
(format stream "~%~%")
(if (uiop:directory-exists-p root)
(render-directory stream 1 root config (list (truename root)))
(render-empty stream 1 root))
(format stream "~%")))