From e50fe12e566ea624061245012f903388818209c5 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Wed, 27 May 2020 12:15:27 +0200 Subject: [PATCH] EXWM: Add toggle for automatic screen change configuration. --- .emacs.d/lisp/init-exwm.el | 82 +++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/.emacs.d/lisp/init-exwm.el b/.emacs.d/lisp/init-exwm.el index 081f327a..e457ba84 100644 --- a/.emacs.d/lisp/init-exwm.el +++ b/.emacs.d/lisp/init-exwm.el @@ -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))