aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Kangas2022-09-26 14:38:25 +0200
committerStefan Kangas2022-09-26 17:41:37 +0200
commitb0289e7f6ddebdd2bdefa63e2b4afcb29733b3b7 (patch)
treedea172d18b22c9a5cb6124a4e9b024311bb5d329
parent35d5ad713ee05f5cd922f66462df41deed95f7e8 (diff)
downloademacs-b0289e7f6ddebdd2bdefa63e2b4afcb29733b3b7.tar.gz
emacs-b0289e7f6ddebdd2bdefa63e2b4afcb29733b3b7.zip
Fix setting the wallpaper in XFCE
* lisp/image/wallpaper.el (wallpaper-command-args) (wallpaper-default-set-function): Support new format specifiers %S for screen, %W for workspace, and %M for monitor. (wallpaper--default-setters): Use above new specifiers for XFCE. (wallpaper--format-arg): New defun broken out from... (wallpaper-default-set-function): ...here. (wallpaper--get-height-or-width): Support noninteractive use. * test/lisp/image/wallpaper-tests.el (wallpaper--format-arg/filename) (wallpaper--format-arg/filename-hex) (wallpaper--format-arg/width, wallpaper--format-arg/screen) (wallpaper--format-arg/monitor, wallpaper--format-arg/workspace): New tests.
-rw-r--r--lisp/image/wallpaper.el96
-rw-r--r--test/lisp/image/wallpaper-tests.el29
2 files changed, 94 insertions, 31 deletions
diff --git a/lisp/image/wallpaper.el b/lisp/image/wallpaper.el
index bdaa148e2b6..31cc2b4eece 100644
--- a/lisp/image/wallpaper.el
+++ b/lisp/image/wallpaper.el
@@ -153,7 +153,7 @@ and returns non-nil if this setter should be used."
153 153
154 ("XFCE" 154 ("XFCE"
155 "xfconf-query" '("-c" "xfce4-desktop" 155 "xfconf-query" '("-c" "xfce4-desktop"
156 "-p" "/backdrop/screen0/monitoreDP/workspace0/last-image" 156 "-p" "/backdrop/screen%S/monitor%M/workspace%W/last-image"
157 "-s" "%f") 157 "-s" "%f")
158 :predicate (lambda () 158 :predicate (lambda ()
159 (or (and (getenv "DESKTOP_SESSION") 159 (or (and (getenv "DESKTOP_SESSION")
@@ -320,15 +320,20 @@ automatically, so there is usually no need to customize this.
320However, if you do need to change this, you might also want to 320However, if you do need to change this, you might also want to
321customize `wallpaper-command' to match. 321customize `wallpaper-command' to match.
322 322
323In each of the command line arguments, \"%f\" will be replaced 323In each command line argument, these specifiers will be replaced:
324with the full file name, \"%F\" with the full file name
325URI-encoded, \"%h\" with the height of the selected frame's
326display (as returned by `display-pixel-height'), and \"%w\" with
327the width of the selected frame's display (as returned by
328`display-pixel-width').
329 324
330If `wallpaper-set' is run from a TTY frame, it will prompt for a 325 %f full file name
331height and width for \"%h\" and \"%w\" instead. 326 %h height of the selected frame's display (as returned
327 by `display-pixel-height')
328 %w the width of the selected frame's display (as returned
329 by `display-pixel-width').
330 %F full file name URI-encoded
331 %S current X screen (e.g. \"0\")
332 %W current workspace (e.g., \"0\")
333 %M name of the monitor (e.g., \"0\" or \"LVDS\")
334
335If `wallpaper-set' is run from a TTY frame, instead prompt for a
336height and width to use for %h and %w.
332 337
333The value of this variable is ignored on MS-Windows and Haiku 338The value of this variable is ignored on MS-Windows and Haiku
334systems, where a native API is used instead." 339systems, where a native API is used instead."
@@ -350,9 +355,9 @@ This is only used when it can't be detected automatically.
350See also `wallpaper-default-width'.") 355See also `wallpaper-default-width'.")
351 356
352(defun wallpaper--get-height-or-width (desc fun default) 357(defun wallpaper--get-height-or-width (desc fun default)
353 (if (display-graphic-p) 358 (cond ((display-graphic-p) (funcall fun))
354 (funcall fun) 359 (noninteractive default)
355 (read-number (format "Wallpaper %s in pixels: " desc) default))) 360 ((read-number (format "Wallpaper %s in pixels: " desc) default))))
356 361
357(autoload 'ffap-file-at-point "ffap") 362(autoload 'ffap-file-at-point "ffap")
358 363
@@ -373,41 +378,70 @@ See also `wallpaper-default-width'.")
373 378
374;;; wallpaper-set 379;;; wallpaper-set
375 380
381(defun wallpaper--format-arg (format file)
382 "Format a `wallpaper-command-args' argument ARG.
383FILE is the image file name."
384 (format-spec
385 format
386 `((?f . ,(expand-file-name file))
387 (?F . ,(mapconcat #'url-hexify-string
388 (file-name-split file)
389 "/"))
390 (?h . ,(wallpaper--get-height-or-width
391 "height"
392 #'display-pixel-height
393 wallpaper-default-height))
394 (?w . ,(wallpaper--get-height-or-width
395 "width"
396 #'display-pixel-width
397 wallpaper-default-width))
398 ;; screen number
399 (?S . ,(let ((display (frame-parameter (selected-frame) 'display)))
400 (if (and display
401 (string-match (rx ":" (+ (in "0-9")) "."
402 (group (+ (in "0-9"))) eos)
403 display))
404 (match-string 1 display)
405 "0")))
406 ;; monitor name
407 (?M . ,(let* ((attrs (car (display-monitor-attributes-list)))
408 (source (cdr (assq 'source attrs)))
409 (monitor (cdr (assq 'name attrs))))
410 (if (and monitor (member source '("XRandr" "XRandr 1.5" "Gdk")))
411 monitor
412 "0")))
413 ;; workspace
414 (?W . ,(or (and (fboundp 'x-window-property)
415 (display-graphic-p)
416 (number-to-string
417 (or (x-window-property "_NET_CURRENT_DESKTOP" nil "CARDINAL" 0 nil t)
418 (x-window-property "WIN_WORKSPACE" nil "CARDINAL" 0 nil t))))
419 "0")))))
420
376(defun wallpaper-default-set-function (file) 421(defun wallpaper-default-set-function (file)
377 "Set the wallpaper to FILE using a command. 422 "Set the wallpaper to FILE using a command.
378This is the default function for `wallpaper-set-function'." 423This is the default function for `wallpaper-set-function'."
379 (unless wallpaper-command 424 (unless wallpaper-command
380 (error "Couldn't find a command to set the wallpaper with")) 425 (error "Couldn't find a command to set the wallpaper with"))
381 (let* ((fmt-spec `((?f . ,(expand-file-name file)) 426 (let* ((real-args (mapcar (lambda (arg) (wallpaper--format-arg arg file))
382 (?F . ,(mapconcat #'url-hexify-string 427 wallpaper-command-args))
383 (file-name-split file)
384 "/"))
385 (?h . ,(wallpaper--get-height-or-width
386 "height"
387 #'display-pixel-height
388 wallpaper-default-height))
389 (?w . ,(wallpaper--get-height-or-width
390 "width"
391 #'display-pixel-width
392 wallpaper-default-width))))
393 (bufname (format " *wallpaper-%s*" (random))) 428 (bufname (format " *wallpaper-%s*" (random)))
394 (process 429 (process
395 (and wallpaper-command 430 (and wallpaper-command
396 (apply #'start-process "set-wallpaper" bufname 431 (apply #'start-process "set-wallpaper" bufname
397 wallpaper-command 432 wallpaper-command real-args))))
398 (mapcar (lambda (arg) (format-spec arg fmt-spec))
399 wallpaper-command-args)))))
400 (unless wallpaper-command 433 (unless wallpaper-command
401 (error "Couldn't find a suitable command for setting the wallpaper")) 434 (error "Couldn't find a suitable command for setting the wallpaper"))
402 (wallpaper-debug 435 (wallpaper-debug "Using command: \"%s %s\""
403 "Using command %S %S" wallpaper-command 436 wallpaper-command (string-join wallpaper-command-args " "))
404 wallpaper-command-args) 437 (wallpaper-debug (wallpaper--format-arg
438 "f=%f w=%w h=%h S=%S M=%M W=%W" file))
405 (setf (process-sentinel process) 439 (setf (process-sentinel process)
406 (lambda (process status) 440 (lambda (process status)
407 (unwind-protect 441 (unwind-protect
408 (unless (and (eq (process-status process) 'exit) 442 (unless (and (eq (process-status process) 'exit)
409 (zerop (process-exit-status process))) 443 (zerop (process-exit-status process)))
410 (message "command %S %s: %S" 444 (message "command \"%s %s\": %S"
411 (string-join (process-command process) " ") 445 (string-join (process-command process) " ")
412 (string-replace "\n" "" status) 446 (string-replace "\n" "" status)
413 (with-current-buffer (process-buffer process) 447 (with-current-buffer (process-buffer process)
diff --git a/test/lisp/image/wallpaper-tests.el b/test/lisp/image/wallpaper-tests.el
index 80d512c9858..c3feab0e206 100644
--- a/test/lisp/image/wallpaper-tests.el
+++ b/test/lisp/image/wallpaper-tests.el
@@ -54,4 +54,33 @@
54 (insert fil) 54 (insert fil)
55 (should (stringp (wallpaper--get-default-file)))))) 55 (should (stringp (wallpaper--get-default-file))))))
56 56
57(ert-deftest wallpaper--format-arg/filename ()
58 (should (file-name-absolute-p (wallpaper--format-arg "%f" "foo.jpg"))))
59
60(ert-deftest wallpaper--format-arg/filename-hex ()
61 (should (equal (wallpaper--format-arg "%F" "foo bar åäö.jpg")
62 "foo%20bar%20%C3%A5%C3%A4%C3%B6.jpg")))
63
64(ert-deftest wallpaper--format-arg/width ()
65 (skip-unless noninteractive)
66 (should (equal (wallpaper--format-arg "%w" "foo.jpg")
67 (number-to-string wallpaper-default-width))))
68
69(ert-deftest wallpaper--format-arg/height ()
70 (skip-unless noninteractive)
71 (should (equal (wallpaper--format-arg "%h" "foo.jpg")
72 (number-to-string wallpaper-default-height))))
73
74(ert-deftest wallpaper--format-arg/screen ()
75 (skip-unless noninteractive)
76 (should (equal (wallpaper--format-arg "%S" "foo.jpg") "0")))
77
78(ert-deftest wallpaper--format-arg/monitor ()
79 (skip-unless noninteractive)
80 (should (equal (wallpaper--format-arg "%M" "foo.jpg") "0")))
81
82(ert-deftest wallpaper--format-arg/workspace ()
83 (skip-unless noninteractive)
84 (should (equal (wallpaper--format-arg "%W" "foo.jpg") "0")))
85
57;;; wallpaper-tests.el ends here 86;;; wallpaper-tests.el ends here