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,18 +212,28 @@
;; 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)
(setq default-output (match-string 1)) (match-string 1)))
(default-output (with-temp-buffer
(funcall find-first-output)))
(second-output (with-temp-buffer
(funcall find-first-output)
(forward-line) (forward-line)
(if (not (re-search-forward xrandr-output-regexp nil 'noerror)) (when (re-search-forward xrandr-output-regexp nil 'noerror)
(match-string 1)))))
(if (not second-output)
(progn (progn
(call-process "xrandr" nil nil nil "--output" default-output "--auto" "--primary") (call-process "xrandr" nil nil nil "--output" default-output "--auto" "--primary")
(with-temp-buffer (with-temp-buffer
@ -235,16 +245,34 @@
(when (and (re-search-forward xrandr-monitor-regexp nil 'noerror) (when (and (re-search-forward xrandr-monitor-regexp nil 'noerror)
(not (string= (match-string 1) default-output))) (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) "--auto")))))
(call-process (apply #'call-process
"xrandr" nil nil nil "xrandr" nil nil nil
"--output" (match-string 1) "--primary" "--auto" "--output" second-output
"--output" default-output "--off") "--auto"
(setq exwm-randr-workspace-monitor-plist (list 0 (match-string 1))))))) (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))