-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
262 lines (235 loc) · 5.92 KB
/
manager.go
File metadata and controls
262 lines (235 loc) · 5.92 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"fmt"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/dpms"
"github.com/BurntSushi/xgb/randr"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"pkg.deepin.io/dde/api/drandr"
"pkg.deepin.io/lib/dbus"
"sync"
"time"
)
// TODO:
// 1. block output changed signal [X]
// 2. list connected output details [X]
// 3. no black screen and no suspend [X]
// 4. loop to query output state [X]
// 5. move window to special position [X]
// 6. draw background [X]
// 7. keybinding [X]
type Manager struct {
conn *xgb.Conn
xu *xgbutil.XUtil
root xproto.Window
outputInfos drandr.OutputInfos
modeInfos drandr.ModeInfos
width uint16
height uint16
outputLocker sync.Mutex
eventLocker sync.Mutex
Changed func()
}
type OutputInfo struct {
Name string
X int16
Y int16
Width uint16
Height uint16
}
func newManager() (*Manager, error) {
xu, err := xgbutil.NewConn()
if err != nil {
return nil, err
}
err = randr.Init(xu.Conn())
if err != nil {
logger.Error("Failed to init randr:", err)
}
screenInfo, err := drandr.GetScreenInfo(xu.Conn())
if err != nil {
return nil, err
}
err = dpms.Init(xu.Conn())
if err != nil {
logger.Error("Failed to init dpms:", err)
}
var m = &Manager{
conn: xu.Conn(),
xu: xu,
root: xproto.Setup(xu.Conn()).DefaultScreen(xu.Conn()).Root,
outputInfos: screenInfo.Outputs,
modeInfos: screenInfo.Modes,
}
m.width, m.height = screenInfo.GetScreenSize()
return m, nil
}
func (m *Manager) init() {
m.checkScreenStatus()
m.joinExtendMode()
m.updateOutputInfo()
}
func (m *Manager) destroy() {
if m.conn == nil {
return
}
m.conn.Close()
m.conn = nil
}
func (m *Manager) checkScreenStatus() {
// if all output was invalid, wait until output validity
for {
if len(m.outputInfos) != 0 && len(m.modeInfos) != 0 {
break
}
err := doAction("xrandr --auto")
if err != nil {
logger.Warningf("Try open output failed %v, try again", err)
}
time.Sleep(time.Second * 2)
m.updateOutputInfo()
}
}
func (m *Manager) joinExtendMode() {
connected := m.outputInfos.ListConnectionOutputs()
names := connected.ListNames()
infos, _ := newOutputInfosFromFile(outputConfigFile)
if len(infos) == 0 {
infos, _ = newOutputInfosFromFile(defaultOutputFile)
if len(infos) == 0 {
goto output
}
}
if !checkOutputConfigValidity(names, infos) {
logger.Warning("Output config invalid:", infos, names)
goto output
} else {
m.joinExtendModeFromConfigInfos(infos)
return
}
output:
m.joinExtendModeFromOutputs(connected)
}
func (m *Manager) joinExtendModeFromOutputs(outputs drandr.OutputInfos) {
var cmd = "xrandr "
startx := uint16(0)
for _, output := range outputs {
//if !canReadEDID(output.Name) {
//logger.Warning("Failed to read edid for:", output.Name)
//continue
//}
cmd += " --output " + output.Name
modes := m.getOutputModes(output.Name)
var mode drandr.ModeInfo = modes.Max()
if len(output.PreferredModes) != 0 {
mode = modes.Query(output.PreferredModes[0])
}
if v := modes.QueryBySize(1024, 768); len(v) != 0 &&
v[0].Width != 0 && v[0].Height != 0 {
mode = v[0]
}
cmd += fmt.Sprintf(" --mode %dx%d --pos %dx0 ", mode.Width, mode.Height, startx)
if startx == 0 {
cmd += " --primary "
}
startx += mode.Width
}
logger.Debug("[joinExtendModeFromOutputs] command:", cmd)
err := doAction(cmd)
if err != nil {
logger.Error("[joinExtendModeFromOutputs] failed:", err)
}
}
func (m *Manager) joinExtendModeFromConfigInfos(infos []OutputInfo) {
var cmd = "xrandr "
primary := false
for _, info := range infos {
//if !canReadEDID(info.Name) {
//logger.Warning("Failed to read edid for:", info.Name)
//continue
//}
cmd += " --output " + info.Name
cmd += fmt.Sprintf(" --mode %dx%d --pos %dx%d ", info.Width, info.Height, info.X, info.Y)
if !primary && info.X == 0 {
primary = true
cmd += " --primary "
}
}
logger.Debug("[joinExtendModeFromConfigInfos] command:", cmd)
err := doAction(cmd)
if err != nil {
logger.Error("[joinExtendModeFromConfigInfos] failed:", err)
}
}
func (m *Manager) handleEventChanged() {
err := randr.SelectInputChecked(m.conn, m.root,
randr.NotifyMaskOutputChange|randr.NotifyMaskOutputProperty|
randr.NotifyMaskCrtcChange|randr.NotifyMaskScreenChange).Check()
if err != nil {
logger.Error("Failed to select input event:", err)
return
}
for {
e, err := m.conn.WaitForEvent()
if err != nil {
continue
}
m.eventLocker.Lock()
logger.Debug("[Debug] output event:", e.String())
switch ee := e.(type) {
case randr.NotifyEvent:
switch ee.SubCode {
case randr.NotifyCrtcChange:
case randr.NotifyOutputChange:
m.updateOutputInfo()
case randr.NotifyOutputProperty:
}
case randr.ScreenChangeNotifyEvent:
m.updateOutputInfo()
case xproto.KeyPressEvent:
m.handleKeyPressEvent(ee)
}
m.eventLocker.Unlock()
}
}
func (m *Manager) updateOutputInfo() {
screenInfo, err := drandr.GetScreenInfo(m.conn)
if err != nil {
logger.Error("Failed to get screen info:", err)
return
}
m.outputLocker.Lock()
m.outputInfos, m.modeInfos = screenInfo.Outputs, screenInfo.Modes
m.width, m.height = screenInfo.GetScreenSize()
m.outputLocker.Unlock()
oldLen := len(prevConnected)
now := m.ListConnectedOutput()
if oldLen != len(now) {
dbus.Emit(m, "Changed")
}
}
func (m *Manager) getOutputModes(name string) drandr.ModeInfos {
info := m.outputInfos.QueryByName(name)
var modes drandr.ModeInfos
for _, id := range info.Modes {
modes = append(modes, m.modeInfos.Query(id))
}
return modes
}
// inhibit no blank and no suspend
func (m *Manager) inhibit() {
xproto.SetScreenSaver(m.conn, 0, 0, 0, 0)
// dpms.SetTimeouts(m.conn, 0, 0, 0)
err := dpms.DisableChecked(m.conn).Check()
if err != nil {
logger.Warning("Failed to disable dpms:", err)
}
}
func (*Manager) GetDBusInfo() dbus.DBusInfo {
return dbus.DBusInfo{
Dest: dbusDest,
ObjectPath: dbusPath,
Interface: dbusIFC,
}
}