Define display-4k-vertical

I recently acquired a new monitor, which I'm orienting vertically for logs,
chats, etc. As such I needed to add more functions, KBDs to wrangle the
setup. To DRY up my code, I define a macro, `display-register`, as a DSL for
supporting new monitors. This:
- defines two functions for enabling and disabling the displays
- defines a constant, `display-<name>`

It's basically just a wrapper around `xrandr`, and that's good enough for now.
This commit is contained in:
William Carroll 2020-09-28 13:41:24 +01:00
parent 34ec3104f7
commit 831dba20bf
2 changed files with 74 additions and 67 deletions

View file

@ -8,8 +8,6 @@
;;; Commentary: ;;; Commentary:
;; Mostly wrappers around xrandr. ;; Mostly wrappers around xrandr.
;; ;;
;; TODO: Look into autorandr to see if it could be useful.
;;
;; Troubleshooting: ;; Troubleshooting:
;; The following commands help me when I (infrequently) interact with xrandr. ;; The following commands help me when I (infrequently) interact with xrandr.
;; - xrandr --listmonitors ;; - xrandr --listmonitors
@ -22,75 +20,83 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'prelude) (require 'prelude)
(require 'cycle)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TODO: Consider if this logic should be conditioned by `device-work-laptop?'.
(defconst display-laptop-monitor "eDP1"
"The xrandr identifier for my primary screen (on work laptop).")
;; TODO: Why is HDMI-1, eDP-1 sometimes and HDMI1, eDP1 other times.
(defconst display-4k-monitor "HDMI1"
"The xrandr identifer for my 4K monitor.")
(defconst display-display-states (cycle-from-list '((t . nil) (nil . t)))
"A list of cons cells modelling enabled and disabled states for my displays.
The car models the enabled state of my laptop display; the cdr models the
enabled state of my external monitor.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library ;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TODO: Debug why something this scales to 4k appropriately and other times it (cl-defmacro display-register (name &key
;; doesn't. output
(defun display-enable-4k () primary
"Attempt to connect to my 4K monitor." position
(interactive) size
(prelude-start-process rate
:name "display-enable-4k" dpi
:command (string-format rotate)
"xrandr --output %s --above %s --primary --auto --size 3840x2160 --rate 30.00 --dpi 144" "Macro to define a constant and two functions for {en,dis}abling a display.
display-4k-monitor
display-laptop-monitor)))
(defun display-disable-4k () NAME - the human-readable identifier for the display
"Disconnect from the 4K monitor." OUTPUT - the xrandr identifier for the display
(interactive) PRIMARY - if true, send --primary flag to xrandr
(prelude-start-process POSITION - one of {left-of,right-of,above,below,same-as}
:name "display-disable-4k" SIZE - the pixel resolution of the display
:command (string-format "xrandr --output %s --off" RATE - the refresh rate
display-4k-monitor))) DPI - the pixel density in dots per square inch
rotate - one of {normal,left,right,inverted}
(defun display-enable-laptop () See the man-page for xrandr for more details."
"Turn the laptop monitor off. `(progn
Sometimes this is useful when I'm sharing my screen in a Google Hangout and I (defconst ,(intern (format "display-%s" name)) ,output
only want to present one of my monitors." ,(format "The xrandr identifier for %s" name))
(interactive) (defun ,(intern (format "display-enable-%s" name)) ()
(prelude-start-process ,(format "Attempt to enable my %s monitor" name)
:name "display-disable-laptop" (interactive)
:command (string-format "xrandr --output %s --auto" (prelude-start-process
display-laptop-monitor))) :name ,(format "display-enable-%s" name)
:command ,(format
"xrandr --output %s --%s --%s %s --auto --size %dx%d --rate %0.2f --dpi %d --rotate %s"
output
(if primary "primary" "noprimary")
(car position) (eval (cadr position))
(car size) (cadr size)
rate
dpi
rotate)))
(defun ,(intern (format "display-disable-%s" name)) ()
,(format "Attempt to disable my %s monitor." name)
(interactive)
(prelude-start-process
:name ,(format "display-disable-%s" name)
:command ,(format
"xrandr --output %s --off"
output)))))
(defun display-disable-laptop () (display-register laptop
"Turn the laptop monitor off. :output "eDP1"
Sometimes this is useful when I'm sharing my screen in a Google Hangout and I :primary nil
only want to present one of my monitors." :position (below display-4k-horizontal)
(interactive) :size (3840 2160)
(prelude-start-process :rate 30.0
:name "display-disable-laptop" :dpi 144
:command (string-format "xrandr --output %s --off" :rotate normal)
display-laptop-monitor)))
(defun display-cycle-display-states () (display-register 4k-horizontal
"Cycle through `display-display-states' enabling and disabling displays." :output "HDMI1"
(interactive) :primary t
(let ((state (cycle-next display-display-states))) :position (above display-laptop)
(if (car state) (display-enable-laptop) (display-disable-laptop)) :size (3840 2160)
(if (cdr state) (display-enable-4k) (display-disable-4k)))) :rate 30.0
:dpi 144
:rotate normal)
(display-register 4k-vertical
:output "DP2"
:primary nil
:position (right-of display-4k-horizontal)
:size (3840 2160)
:rate 30.0
:dpi 144
:rotate right)
(provide 'display) (provide 'display)
;;; display.el ends here ;;; display.el ends here

View file

@ -224,14 +224,15 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when (device-work-laptop?) (when (device-work-laptop?)
(keybindings-exwm "<XF86Display>" #'display-cycle-display-states)
(general-define-key (general-define-key
:prefix "<SPC>" :prefix "<SPC>"
:states '(normal) :states '(normal)
"d0" #'display-disable-laptop "d0" #'display-enable-laptop
"d1" #'display-enable-laptop "D0" #'display-disable-laptop
"D0" #'display-disable-4k "d1" #'display-enable-4k-horizontal
"D1" #'display-enable-4k)) "D1" #'display-disable-4k-horizontal
"d2" #'display-enable-4k-vertical
"D2" #'display-disable-4k-vertical))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; notmuch ;; notmuch