about summary refs log tree commit diff
path: root/emacs/.emacs.d/wpc/display.el
blob: 56f16e3e0c475c7cc9ed496cc75f3da3ee34ecd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
;;; display.el --- Working with single or multiple displays -*- lexical-binding: t -*-

;; Author: William Carroll <wpcarro@gmail.com>
;; Version: 0.0.1
;; URL: https://git.wpcarro.dev/wpcarro/briefcase
;; Package-Requires: ((emacs "24"))

;;; Commentary:
;; Mostly wrappers around xrandr.
;;
;; Troubleshooting:
;; The following commands help me when I (infrequently) interact with xrandr.
;; - xrandr --listmonitors
;; - xrandr --query

;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'prelude)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(cl-defmacro display-register (name &key
                                    output
                                    primary
                                    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)
       (prelude-start-process
        :name ,(format "display-enable-%s" name)
        :command ,(format
                   "xrandr --output %s --%s %s --auto --size %dx%d --rate %0.2f --dpi %d --rotate %s"
                   output
                   (if primary "primary" "noprimary")
                   (if position
                       (format "--%s %s"
                               (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)))))

;; I'm omitting the position argument to avoid a circular dependency between
;; laptop and 4k-horizontal.
(display-register laptop
                  :output "eDP1"
                  :primary nil
                  :size (3840 2160)
                  :rate 30.0
                  :dpi 144
                  :rotate normal)

(display-register 4k-horizontal
                  :output "HDMI1"
                  :primary t
                  :position (above display-laptop)
                  :size (3840 2160)
                  :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)
;;; display.el ends here