/
heads.go
159 lines (138 loc) · 3.5 KB
/
heads.go
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
package main
import (
"fmt"
"log"
"sort"
"strings"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/randr"
"github.com/BurntSushi/xgb/xproto"
)
type heads struct {
primary *head
heads []head
off []string
disconnected []string
}
func newHeads(X *xgb.Conn) heads {
var primaryHead *head
var primaryOutput randr.Output
root := xproto.Setup(X).DefaultScreen(X).Root
resources, err := randr.GetScreenResourcesCurrent(X, root).Reply()
if err != nil {
log.Fatalf("Could not get screen resources: %s.", err)
}
primaryOutputReply, _ := randr.GetOutputPrimary(X, root).Reply()
if primaryOutputReply != nil {
primaryOutput = primaryOutputReply.Output
}
hds := make([]head, 0, len(resources.Outputs))
off := make([]string, 0)
disconnected := make([]string, 0)
for i, output := range resources.Outputs {
oinfo, err := randr.GetOutputInfo(X, output, 0).Reply()
if err != nil {
log.Fatalf("Could not get output info for screen %d: %s.", i, err)
}
outputName := string(oinfo.Name)
if oinfo.Connection != randr.ConnectionConnected {
disconnected = append(disconnected, outputName)
continue
}
if oinfo.Crtc == 0 {
off = append(off, outputName)
continue
}
crtcinfo, err := randr.GetCrtcInfo(X, oinfo.Crtc, 0).Reply()
if err != nil {
log.Fatalf("Could not get crtc info for screen (%d, %s): %s.",
i, outputName, err)
}
head := newHead(output, outputName, crtcinfo)
if output == primaryOutput {
primaryHead = &head
}
hds = append(hds, head)
}
if primaryHead == nil && len(hds) > 0 {
tmp := hds[0]
primaryHead = &tmp
}
hdsPrim := heads{
primary: primaryHead,
heads: hds,
off: off,
disconnected: disconnected,
}
sort.Sort(hdsPrim)
return hdsPrim
}
func (hs heads) connectedRandrName(config *config, queryName string) string {
if hd := hs.findActive(config, queryName); hd != nil {
return hd.output
}
if hdName := hs.findOff(config, queryName); len(hdName) > 0 {
return hdName
}
return ""
}
func (hs heads) findActive(config *config, queryName string) *head {
if queryName == "primary" {
return hs.primary
}
for _, hd := range hs.heads {
if hd.output == queryName || config.nice(hd.output) == queryName {
return &hd
}
}
return nil
}
func (hs heads) findOff(config *config, queryName string) string {
for _, output := range hs.off {
if output == queryName || config.nice(output) == queryName {
return output
}
}
return ""
}
func (hs heads) findDisconnected(config *config, queryName string) string {
for _, output := range hs.disconnected {
if output == queryName || config.nice(output) == queryName {
return output
}
}
return ""
}
func (hs heads) String() string {
lines := make([]string, len(hs.heads))
for i, head := range hs.heads {
lines[i] = fmt.Sprintf("%d: %s (%d, %d) %dx%d",
i, head.output, head.x, head.y, head.width, head.height)
}
return strings.Join(lines, "\n")
}
func (hs heads) Len() int {
return len(hs.heads)
}
func (hs heads) Less(i, j int) bool {
h1, h2 := hs.heads[i], hs.heads[j]
return h1.x < h2.x || (h1.x == h2.x && h1.y < h2.y)
}
func (hs heads) Swap(i, j int) {
hs.heads[i], hs.heads[j] = hs.heads[j], hs.heads[i]
}
type head struct {
id randr.Output
output string
x, y, width, height int
}
func newHead(id randr.Output, name string, info *randr.GetCrtcInfoReply) head {
return head{
id: id,
output: name,
x: int(info.X),
y: int(info.Y),
width: int(info.Width),
height: int(info.Height),
}
}