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 ;; https://github.com/ch11ng/exwm/issues/287
(add-to-list 'exwm-manage-configurations '((string= exwm-title "Kingdom Come: Deliverance") managed t)) (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. ;; Function to automatically toggle between internal/external screens.
(defun ambrevar/exwm-change-screen-hook () (defun ambrevar/exwm-change-screen-hook ()
(let ((xrandr-output-regexp "\n\\([^ ]+\\) connected ") (let* ((xrandr-output-regexp "\n\\([^ ]+\\) connected ")
(xrandr-monitor-regexp "\n .* \\([^ \n]+\\)") (xrandr-monitor-regexp "\n .* \\([^ \n]+\\)")
default-output) (find-first-output (lambda ()
(with-temp-buffer (call-process "xrandr" nil t nil)
(call-process "xrandr" nil t nil) (goto-char (point-min))
(goto-char (point-min)) (re-search-forward xrandr-output-regexp nil 'noerror)
(re-search-forward xrandr-output-regexp nil 'noerror) (match-string 1)))
(setq default-output (match-string 1)) (default-output (with-temp-buffer
(forward-line) (funcall find-first-output)))
(if (not (re-search-forward xrandr-output-regexp nil 'noerror)) (second-output (with-temp-buffer
(progn (funcall find-first-output)
(call-process "xrandr" nil nil nil "--output" default-output "--auto" "--primary") (forward-line)
(with-temp-buffer (when (re-search-forward xrandr-output-regexp nil 'noerror)
;; Turn off all monitors that are not DEFAULT-OUTPUT. (match-string 1)))))
;; See "--auto" in xrandr(1) and https://github.com/ch11ng/exwm/issues/529. (if (not second-output)
(call-process "xrandr" nil t nil "--listactivemonitors") (progn
(goto-char (point-min)) (call-process "xrandr" nil nil nil "--output" default-output "--auto" "--primary")
(while (not (eobp)) (with-temp-buffer
(when (and (re-search-forward xrandr-monitor-regexp nil 'noerror) ;; Turn off all monitors that are not DEFAULT-OUTPUT.
(not (string= (match-string 1) default-output))) ;; See "--auto" in xrandr(1) and https://github.com/ch11ng/exwm/issues/529.
(call-process "xrandr" nil nil nil "--output" (match-string 1) "--auto"))))) (call-process "xrandr" nil t nil "--listactivemonitors")
(call-process (goto-char (point-min))
"xrandr" nil nil nil (while (not (eobp))
"--output" (match-string 1) "--primary" "--auto" (when (and (re-search-forward xrandr-monitor-regexp nil 'noerror)
"--output" default-output "--off") (not (string= (match-string 1) default-output)))
(setq exwm-randr-workspace-monitor-plist (list 0 (match-string 1))))))) (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) (require 'exwm-randr)
(add-hook 'exwm-randr-screen-change-hook 'ambrevar/exwm-change-screen-hook)
(exwm-randr-enable) (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) (setq exwm-edit-bind-default-keys nil)
(when (require 'exwm-edit nil 'noerror) (when (require 'exwm-edit nil 'noerror)
(require 'patch-exwm-edit)) (require 'patch-exwm-edit))