From: Stefan Kangas Date: Thu, 29 Sep 2022 14:10:27 +0000 (+0200) Subject: wallpaper-set: Get monitor name on non-graphical display X-Git-Tag: emacs-29.0.90~1856^2~142 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=2c331dbb4eabadf9f58097591468658e096ba404;p=emacs.git wallpaper-set: Get monitor name on non-graphical display * lisp/image/wallpaper.el (wallpaper--format-arg): Break out function from here... (wallpaper--x-monitor-name): ...to here. Try to get the monitor name non-graphical displays, and prompt if that doesn't work. (wallpaper-default-set-function): Improve debugging output. Ref. https://lists.gnu.org/r/emacs-devel/2022-09/msg01910.html --- diff --git a/lisp/image/wallpaper.el b/lisp/image/wallpaper.el index 3b32c5a4156..80c98629166 100644 --- a/lisp/image/wallpaper.el +++ b/lisp/image/wallpaper.el @@ -396,6 +396,40 @@ See also `wallpaper-default-width'.") ;;; wallpaper-set +(defun wallpaper--x-monitor-name () + "Get the monitor name for `wallpaper-set'. +On a graphical display, try using the same monitor as the current +frame. +On a non-graphical display, try to get the name by connecting to +the display server directly, and run \"xrandr\" if that doesn't +work. Prompt for the monitor name if neither method works." + (if (or (display-graphic-p) + noninteractive) + (let-alist (car (display-monitor-attributes-list)) + (if (and .name (member .source '("XRandr" "XRandR 1.5" "Gdk"))) + .name + "0")) + (if-let ((name + (and (getenv "DISPLAY") + (or + (cdr (assq 'name + (progn + (x-open-connection (getenv "DISPLAY")) + (car (display-monitor-attributes-list + (car (last (terminal-list)))))))) + (and (executable-find "xrandr") + (with-temp-buffer + (call-process "xrandr" nil t nil) + (goto-char (point-min)) + (re-search-forward (rx bol + (group (+ (not (in " \n")))) + " connected") + nil t) + (match-string 1))))))) + ;; Prefer "0" to "default" as that works in XFCE. + (if (equal name "default") "0" name) + (read-string (format-prompt "Monitor name" nil))))) + (defun wallpaper--format-arg (format file) "Format a `wallpaper-command-args' argument ARG. FILE is the image file name." @@ -424,10 +458,7 @@ FILE is the image file name." (match-string 1 display) "0"))) ;; monitor name - (?M . ,(let-alist (car (display-monitor-attributes-list)) - (if (and .name (member .source '("XRandr" "XRandR 1.5" "Gdk"))) - .name - "0"))) + (?M . ,#'wallpaper--x-monitor-name) ;; workspace (?W . ,(or (and (fboundp 'x-window-property) (display-graphic-p) @@ -454,9 +485,7 @@ This is the default function for `wallpaper-set-function'." (unless wallpaper-command (error "Couldn't find a suitable command for setting the wallpaper")) (wallpaper-debug "Using command: \"%s %s\"" - wallpaper-command (string-join args " ")) - (wallpaper-debug (wallpaper--format-arg - "f=%f w=%w h=%h S=%S M=%M W=%W" file)) + wallpaper-command (string-join real-args " ")) (setf (process-sentinel process) (lambda (process status) (unwind-protect