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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
package main
import (
"regexp"
"strconv"
"strings"
)
// Parsing of `xrandr`'s human-readable output.
//
// These three patterns are a port of the `xrandr-parse` npm package, which this
// tool used to shell out to node for. The whole package was 49 lines, of which
// these regexes were the interesting part, so it is inlined here rather than
// vendored: it removes a GitHub fetch, a node_modules tree assembled by hand out
// of execline and netstrings, and the node runtime itself.
//
// The output of `xrandr` looks like this:
//
// Screen 0: minimum 320 x 200, current 4480 x 1440, maximum 16384 x 16384
// eDP connected 1920x1080+0+360 (normal left inverted ...) 309mm x 173mm
// 1920x1080 60.03*+
// 1680x1050 60.03
// HDMI-A-0 disconnected primary 2560x1440+1920+0 (normal ...) 0mm x 0mm
//
// where `*` marks the currently active mode and `+` the preferred ("native") one.
var (
reConnected = regexp.MustCompile(`^(\S+) connected (?:(\d+)x(\d+))?`)
reDisconnected = regexp.MustCompile(`^(\S+) disconnected`)
// The height may end in `i` for interlaced modes (e.g. `1920x1080i`), so it
// is not simply `\d+`.
reMode = regexp.MustCompile(`^\s+(\d+)x([0-9i]+)\s+((?:\d+\.)?\d+)([* ]?)([+ ]?)`)
)
// Mode is a resolution an output supports.
//
// Width and Height are strings, not numbers, because Height can be something
// like "1080i". Keeping both as they were printed also means the JSON produced
// by `monitor parse` is byte-identical to what the old node parser emitted,
// which is what let the two be diffed against each other during the rewrite.
type Mode struct {
Width string `json:"width"`
Height string `json:"height"`
Rate float64 `json:"rate"`
}
// Output is a single connector (eDP, HDMI-A-0, ...) as xrandr reports it.
type Output struct {
Name string `json:"-"`
Connected bool `json:"connected"`
Modes []Mode `json:"modes"`
Index int `json:"index"`
// Width and Height are the dimensions from the connector line itself
// (`1920x1080+0+360`). Only present when xrandr printed them, which is why
// they are pointers: a disconnected output has none, and emitting `0` would
// be a lie rather than an absence.
Width *int `json:"width,omitempty"`
Height *int `json:"height,omitempty"`
// Native is the mode marked `+` (preferred), Current the one marked `*`
// (active). Either can be absent.
Native *Mode `json:"native,omitempty"`
Current *Mode `json:"current,omitempty"`
}
// Parse turns the output of `xrandr` into a list of outputs, in the order
// xrandr printed them.
//
// Note that `connected` is taken from the literal keyword and nothing else. An
// output can be reported as `disconnected` while still carrying a resolution
// and even `primary` -- this happens on rolery with HDMI-A-0 -- and treating
// "has a resolution" as "is connected" would pick a monitor that is not there.
func Parse(xrandrOutput string) []Output {
var outputs []Output
// Index of the output that mode lines currently attach to, or -1 for none.
last := -1
for _, line := range strings.Split(xrandrOutput, "\n") {
if m := reConnected.FindStringSubmatch(line); m != nil {
out := Output{Name: m[1], Connected: true, Modes: []Mode{}, Index: len(outputs)}
// Groups 2 and 3 are optional: `<name> connected` with no geometry.
if m[2] != "" && m[3] != "" {
w, errW := strconv.Atoi(m[2])
h, errH := strconv.Atoi(m[3])
if errW == nil && errH == nil {
out.Width, out.Height = &w, &h
}
}
outputs = append(outputs, out)
last = len(outputs) - 1
continue
}
if m := reDisconnected.FindStringSubmatch(line); m != nil {
outputs = append(outputs, Output{
Name: m[1], Connected: false, Modes: []Mode{}, Index: len(outputs),
})
last = len(outputs) - 1
continue
}
if last >= 0 {
if m := reMode.FindStringSubmatch(line); m != nil {
// The rate always parses: the regex only matches digits and at
// most one dot.
rate, err := strconv.ParseFloat(m[3], 64)
if err != nil {
continue
}
mode := Mode{Width: m[1], Height: m[2], Rate: rate}
o := &outputs[last]
o.Modes = append(o.Modes, mode)
// `*` and `+` may appear in either order, hence checking both
// groups for each marker.
if m[4] == "+" || m[5] == "+" {
m := mode
o.Native = &m
}
if m[4] == "*" || m[5] == "*" {
m := mode
o.Current = &m
}
continue
}
}
// Any line that is neither a connector nor a mode ends the current
// output. This is load-bearing: it is what stops the indented modeline
// blocks that `xrandr --verbose` prints
//
// 2560x1440 (0x60) 241.500MHz +HSync -VSync
// h: width 2560 start 2608 end 2640 ...
//
// from being attached as modes to whichever output came last.
last = -1
}
return outputs
}
// Connected returns only the outputs xrandr called `connected`.
func Connected(outputs []Output) []Output {
var connected []Output
for _, o := range outputs {
if o.Connected {
connected = append(connected, o)
}
}
return connected
}
// Find returns the output with the given connector name.
func Find(outputs []Output, name string) (Output, bool) {
for _, o := range outputs {
if o.Name == name {
return o, true
}
}
return Output{}, false
}
|