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:
		
							parent
							
								
									34ec3104f7
								
							
						
					
					
						commit
						831dba20bf
					
				
					 2 changed files with 74 additions and 67 deletions
				
			
		|  | @ -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 | ||||||
|  |                                     size | ||||||
|  |                                     rate | ||||||
|  |                                     dpi | ||||||
|  |                                     rotate) | ||||||
|  |   "Macro to define a constant and two functions for {en,dis}abling a display. | ||||||
|  | 
 | ||||||
|  | NAME - the human-readable identifier for the display | ||||||
|  | OUTPUT - the xrandr identifier for the display | ||||||
|  | PRIMARY - if true, send --primary flag to xrandr | ||||||
|  | POSITION - one of {left-of,right-of,above,below,same-as} | ||||||
|  | SIZE - the pixel resolution of the display | ||||||
|  | RATE - the refresh rate | ||||||
|  | DPI - the pixel density in dots per square inch | ||||||
|  | rotate - one of {normal,left,right,inverted} | ||||||
|  | 
 | ||||||
|  | See the man-page for xrandr for more details." | ||||||
|  |   `(progn | ||||||
|  |      (defconst ,(intern (format "display-%s" name)) ,output | ||||||
|  |        ,(format "The xrandr identifier for %s" name)) | ||||||
|  |      (defun ,(intern (format "display-enable-%s" name)) () | ||||||
|  |          ,(format "Attempt to enable my %s monitor" name) | ||||||
|          (interactive) |          (interactive) | ||||||
|        (prelude-start-process |        (prelude-start-process | ||||||
|    :name "display-enable-4k" |         :name ,(format "display-enable-%s" name) | ||||||
|    :command (string-format |         :command ,(format | ||||||
|              "xrandr --output %s --above %s --primary --auto --size 3840x2160 --rate 30.00 --dpi 144" |                    "xrandr --output %s --%s --%s %s --auto --size %dx%d --rate %0.2f --dpi %d --rotate %s" | ||||||
|              display-4k-monitor |                    output | ||||||
|              display-laptop-monitor))) |                    (if primary "primary" "noprimary") | ||||||
| 
 |                    (car position) (eval (cadr position)) | ||||||
| (defun display-disable-4k () |                    (car size) (cadr size) | ||||||
|   "Disconnect from the 4K monitor." |                    rate | ||||||
|  |                    dpi | ||||||
|  |                    rotate))) | ||||||
|  |      (defun ,(intern (format "display-disable-%s" name)) () | ||||||
|  |          ,(format "Attempt to disable my %s monitor." name) | ||||||
|          (interactive) |          (interactive) | ||||||
|          (prelude-start-process |          (prelude-start-process | ||||||
|    :name "display-disable-4k" |           :name ,(format "display-disable-%s" name) | ||||||
|    :command (string-format "xrandr --output %s --off" |           :command ,(format | ||||||
|                            display-4k-monitor))) |                      "xrandr --output %s --off" | ||||||
|  |                      output))))) | ||||||
| 
 | 
 | ||||||
| (defun display-enable-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 --auto" |                   :rotate normal) | ||||||
|                            display-laptop-monitor))) |  | ||||||
| 
 | 
 | ||||||
| (defun display-disable-laptop () | (display-register 4k-horizontal | ||||||
|   "Turn the laptop monitor off. |                   :output "HDMI1" | ||||||
| Sometimes this is useful when I'm sharing my screen in a Google Hangout and I |                   :primary t | ||||||
|   only want to present one of my monitors." |                   :position (above display-laptop) | ||||||
|   (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-vertical | ||||||
|   "Cycle through `display-display-states' enabling and disabling displays." |                   :output "DP2" | ||||||
|   (interactive) |                   :primary nil | ||||||
|   (let ((state (cycle-next display-display-states))) |                   :position (right-of display-4k-horizontal) | ||||||
|     (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 right) | ||||||
| 
 | 
 | ||||||
| (provide 'display) | (provide 'display) | ||||||
| ;;; display.el ends here | ;;; display.el ends here | ||||||
|  |  | ||||||
|  | @ -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 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue