emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 2c331dbb4e 3/3: wallpaper-set: Get monitor name on non-graphical


From: Stefan Kangas
Subject: master 2c331dbb4e 3/3: wallpaper-set: Get monitor name on non-graphical display
Date: Thu, 29 Sep 2022 10:10:39 -0400 (EDT)

branch: master
commit 2c331dbb4eabadf9f58097591468658e096ba404
Author: Stefan Kangas <stefankangas@gmail.com>
Commit: Stefan Kangas <stefankangas@gmail.com>

    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
---
 lisp/image/wallpaper.el | 43 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/lisp/image/wallpaper.el b/lisp/image/wallpaper.el
index 3b32c5a415..80c9862916 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]