diff options
Diffstat (limited to 'src/menu.lisp')
| -rw-r--r-- | src/menu.lisp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/menu.lisp b/src/menu.lisp new file mode 100644 index 0000000..4bae123 --- /dev/null +++ b/src/menu.lisp @@ -0,0 +1,94 @@ +(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)))) + +;;; 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 menu-label (pathname) + "Filename without its extension. Openbox reads _ in a label as a keyboard +shortcut marker, so each _ is doubled to show it as-is." + (let ((name (or (pathname-name pathname) (file-namestring pathname)))) + (with-output-to-string (out) + (loop for c across name + do (write-char c out) + (when (char= c #\_) (write-char c out)))))) + +;;; 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))))))) + +(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))))) + +;;; Rendering + +(defun render-item (stream pathname command) + (format stream " <item label=\"~A\">~% <action name=\"Execute\"><command>~A</command></action>~% </item>~%" + (xml-escape (menu-label pathname)) + (xml-escape (build-command command pathname)))) + +(defun render-menu (config &optional (stream *standard-output*)) + "Write the Openbox pipe menu XML for CONFIG to STREAM." + (let ((files (find-wallpapers (config-directory config) + (config-extensions config)))) + (format stream "<?xml version=\"1.0\" encoding=\"UTF-8\"?>~%<openbox_pipe_menu>~%") + (if files + (dolist (f files) + (render-item stream f (config-command config))) + (format stream " <separator label=\"~A\"/>~%" + (xml-escape (format nil "No wallpapers in ~A" + (expand-path (config-directory config)))))) + (format stream "</openbox_pipe_menu>~%"))) |
