diff options
Diffstat (limited to 'src/main.lisp')
| -rw-r--r-- | src/main.lisp | 63 |
1 files changed, 59 insertions, 4 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))))) |
