EXWM: Add toggle for automatic screen change configuration.

master
Pierre Neidhardt 2020-05-27 12:15:27 +02:00
parent 56b62471db
commit e50fe12e56
1 changed files with 55 additions and 27 deletions

View File

@ -212,39 +212,67 @@
;; https://github.com/ch11ng/exwm/issues/287
(add-to-list 'exwm-manage-configurations '((string= exwm-title "Kingdom Come: Deliverance") managed t))
;; Gens
(add-to-list 'exwm-manage-configurations '((string= exwm-class-name "Gens") floating nil))
(defvar ambrevar/exwm-change-screen-turn-off-primary nil
"Turn off primary display when cable is plugged.")
;; Function to automatically toggle between internal/external screens.
(defun ambrevar/exwm-change-screen-hook ()
(let ((xrandr-output-regexp "\n\\([^ ]+\\) connected ")
(xrandr-monitor-regexp "\n .* \\([^ \n]+\\)")
default-output)
(with-temp-buffer
(call-process "xrandr" nil t nil)
(goto-char (point-min))
(re-search-forward xrandr-output-regexp nil 'noerror)
(setq default-output (match-string 1))
(forward-line)
(if (not (re-search-forward xrandr-output-regexp nil 'noerror))
(progn
(call-process "xrandr" nil nil nil "--output" default-output "--auto" "--primary")
(with-temp-buffer
;; Turn off all monitors that are not DEFAULT-OUTPUT.
;; See "--auto" in xrandr(1) and https://github.com/ch11ng/exwm/issues/529.
(call-process "xrandr" nil t nil "--listactivemonitors")
(goto-char (point-min))
(while (not (eobp))
(when (and (re-search-forward xrandr-monitor-regexp nil 'noerror)
(not (string= (match-string 1) default-output)))
(call-process "xrandr" nil nil nil "--output" (match-string 1) "--auto")))))
(call-process
"xrandr" nil nil nil
"--output" (match-string 1) "--primary" "--auto"
"--output" default-output "--off")
(setq exwm-randr-workspace-monitor-plist (list 0 (match-string 1)))))))
(let* ((xrandr-output-regexp "\n\\([^ ]+\\) connected ")
(xrandr-monitor-regexp "\n .* \\([^ \n]+\\)")
(find-first-output (lambda ()
(call-process "xrandr" nil t nil)
(goto-char (point-min))
(re-search-forward xrandr-output-regexp nil 'noerror)
(match-string 1)))
(default-output (with-temp-buffer
(funcall find-first-output)))
(second-output (with-temp-buffer
(funcall find-first-output)
(forward-line)
(when (re-search-forward xrandr-output-regexp nil 'noerror)
(match-string 1)))))
(if (not second-output)
(progn
(call-process "xrandr" nil nil nil "--output" default-output "--auto" "--primary")
(with-temp-buffer
;; Turn off all monitors that are not DEFAULT-OUTPUT.
;; See "--auto" in xrandr(1) and https://github.com/ch11ng/exwm/issues/529.
(call-process "xrandr" nil t nil "--listactivemonitors")
(goto-char (point-min))
(while (not (eobp))
(when (and (re-search-forward xrandr-monitor-regexp nil 'noerror)
(not (string= (match-string 1) default-output)))
(call-process "xrandr" nil nil nil "--output" (match-string 1) "--auto")))))
(apply #'call-process
"xrandr" nil nil nil
"--output" second-output
"--auto"
(append
(if ambrevar/exwm-change-screen-turn-off-primary '("--primary") '())
(when ambrevar/exwm-change-screen-turn-off-primary
(list "--output" default-output "--off"))))
(setq exwm-randr-workspace-monitor-plist (list 0 second-output)))))
(require 'exwm-randr)
(add-hook 'exwm-randr-screen-change-hook 'ambrevar/exwm-change-screen-hook)
(exwm-randr-enable)
(defun ambrevar/exwm-change-screen-toggle (&optional setting)
"Toggle automatic multiscreen configuration.
Turn off if you want to set the display settings manually.
WIth SETTING to :ENABLE or :DISABLE, set"
(interactive)
(if (member 'ambrevar/exwm-change-screen-hook exwm-randr-screen-change-hook)
(progn
(remove-hook 'exwm-randr-screen-change-hook 'ambrevar/exwm-change-screen-hook)
(message "Manual multiscreen handling."))
(add-hook 'exwm-randr-screen-change-hook 'ambrevar/exwm-change-screen-hook)
(message "Automatic multiscreen handling.")))
(add-hook 'exwm-randr-screen-change-hook 'ambrevar/exwm-change-screen-hook)
;; TODO: Turn the toggle into a global minor mode.
;; Even better: Use autorandr_launcher (https://github.com/phillipberndt/autorandr/issues/210).
(setq exwm-edit-bind-default-keys nil)
(when (require 'exwm-edit nil 'noerror)
(require 'patch-exwm-edit))