aboutsummaryrefslogtreecommitdiff
path: root/src/main.lisp
blob: 3a2fb3532d82e4b3f870f2a136a1f6ead476753c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(in-package #:wallpaper-switcher)

(defparameter *usage*
  "Usage: wallpaper-switcher [options]
       wallpaper-switcher [options] --set FILE
       wallpaper-switcher [options] --restore

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
                         (default: $XDG_CONFIG_HOME/wallpaper-switcher/config)
  -d, --directory DIR    Wallpaper directory (default: ~/.wallpapers)
  -e, --command CMD      Shell command to set a wallpaper; %f is the file
  -h, --help             Show this help
")

(defun parse-args (args)
  "Return a plist of the options in ARGS.  Signals an error on bad input."
  (let ((opts '()))
    (loop while args
          do (let ((arg (pop args)))
               (flet ((value ()
                        (or (pop args)
                            (error "option ~A needs a value" arg))))
                 (cond ((member arg '("-h" "--help") :test #'string=)
                        (setf (getf opts :help) t))
                       ((member arg '("-c" "--config") :test #'string=)
                        (setf (getf opts :config) (value)))
                       ((member arg '("-d" "--directory") :test #'string=)
                        (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
                   (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)))
      (when (getf opts :directory)
        (setf (config-directory config) (getf opts :directory)))
      (when (getf opts :command)
        (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
      (let ((opts (parse-args args)))
        (if (getf opts :help)
            (write-string *usage*)
            ;; Warnings go to stderr; stdout must contain only the menu XML.
            (handler-bind ((warning (lambda (w)
                                      (format *error-output* "wallpaper-switcher: ~A~%" w)
                                      (muffle-warning w))))
              (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)
      (format *error-output* "wallpaper-switcher: ~A~%" e)
      1)))

(defun main ()
  "Entry point for the built executable."
  (let ((args (uiop:command-line-arguments)))
    (uiop:quit (let ((*self-command* (self-command (ignore-errors (parse-args args)))))
                 (run args)))))