-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathkwayland_dbus.go
384 lines (339 loc) · 9.35 KB
/
kwayland_dbus.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package display
import (
"context"
"encoding/json"
"errors"
"fmt"
"os/exec"
"sort"
"strings"
"sync"
"time"
"github.com/linuxdeepin/go-x11-client/ext/randr"
"github.com/linuxdeepin/startdde/wl_display/ddewloutput"
)
type monitorIdGenerator struct {
nextId uint32
uuidIdMap map[string]uint32
mu sync.Mutex
}
func newMonitorIdGenerator() *monitorIdGenerator {
return &monitorIdGenerator{
nextId: 1,
uuidIdMap: make(map[string]uint32),
}
}
func (mig *monitorIdGenerator) getId(uuid string) uint32 {
mig.mu.Lock()
defer mig.mu.Unlock()
id, ok := mig.uuidIdMap[uuid]
if ok {
return id
}
id = mig.nextId
mig.nextId++
mig.uuidIdMap[uuid] = id
return id
}
type KOutputInfo struct {
Uuid string `json:"uuid"`
Edid string `json:"edid_base64"`
Enabled int32 `json:"enabled"`
X int32 `json:"x"`
Y int32 `json:"y"`
Width int32 `json:"width"`
Height int32 `json:"height"`
RefreshRate int32 `json:"refresh_rate"`
Manufacturer string `json:"manufacturer"`
Model string `json:"model"`
ModeInfos []KModeInfo `json:"ModeInfo"`
PhysHeight int32 `json:"phys_height"`
PhysWidth int32 `json:"phys_width"`
Transform int32 `json:"transform"`
Scale float64 `json:"scale"`
}
func newKOutputInfoByUUID(uuid string) (*KOutputInfo, error) {
sinfo, err := ddewloutput.GetScreenInfo()
if err != nil {
return nil, err
}
info := sinfo.Outputs.Get(uuid)
if info == nil {
return nil, fmt.Errorf("not found output by %s", uuid)
}
var kinfo = KOutputInfo{
Uuid: info.UUID,
Model: info.Name,
Manufacturer: info.Manufacturer,
X: info.X,
Y: info.Y,
Width: info.Width,
Height: info.Height,
RefreshRate: int32(info.Refresh * 1000),
PhysWidth: info.PhysWidth,
PhysHeight: info.PhysHeight,
Transform: info.Transform,
Scale: info.ScaleF,
ModeInfos: []KModeInfo{},
}
if needSwapWidthHeight(kinfo.rotation()) {
kinfo.Width, kinfo.Height = kinfo.Height, kinfo.Width
}
if info.Enabled {
kinfo.Enabled = 1
} else {
kinfo.Enabled = 0
}
for _, m := range info.Modes {
kinfo.ModeInfos = append(kinfo.ModeInfos, KModeInfo{
Id: m.ID,
Width: m.Width,
Height: m.Height,
RefreshRate: int32(m.Refresh * 1000),
Flags: int32(m.Flag),
})
}
return &kinfo, nil
}
func (m *Manager) applyByWLOutput() error {
var disabledMonitors []*Monitor
args := make([]string, 0)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
for _, monitor := range m.monitorMap {
trans := int32(randrRotationToTransform(int(monitor.Rotation)))
if !monitor.Enabled {
disabledMonitors = append(disabledMonitors, monitor)
continue
}
logger.Debug("---------Will apply:", monitor.Name, monitor.uuid, monitor.Enabled, monitor.X, monitor.Y, monitor.CurrentMode, trans)
args = append(args, monitor.uuid, fmt.Sprint("1"),
fmt.Sprintf("%d", monitor.X), fmt.Sprintf("%d", monitor.Y), fmt.Sprintf("%d", monitor.CurrentMode.Width),
fmt.Sprintf("%d", monitor.CurrentMode.Height), fmt.Sprintf("%d", int32(monitor.CurrentMode.Rate*1000)),
fmt.Sprintf("%d", trans))
}
if len(args) > 0 {
cmdline := exec.CommandContext(ctx, "dde_wloutput", "set")
cmdline.Args = append(cmdline.Args, args...)
logger.Info("cmd line args:", cmdline.Args)
data, err := cmdline.CombinedOutput()
cancel()
// ignore timeout signal
if err != nil && !strings.Contains(err.Error(), "killed") {
logger.Warningf("%s(%s)", string(data), err)
return err
}
// wait request done
//time.Sleep(time.Millisecond * 500)
}
for _, monitor := range disabledMonitors {
logger.Debug("-----------Will disable output:", monitor.Name)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
data, err := exec.CommandContext(ctx, "dde_wloutput", "set", monitor.uuid, "0", "0", "0", "0", "0", "0", "0").CombinedOutput()
cancel()
// ignore timeout signal
if err != nil && !strings.Contains(err.Error(), "killed") {
logger.Warningf("%s(%s)", string(data), err)
return err
}
// wait request done
//time.Sleep(time.Millisecond * 500)
}
return nil
}
func (oi *KOutputInfo) getModes() (result []ModeInfo) {
for _, mi := range oi.ModeInfos {
result = append(result, mi.toModeInfo())
}
sort.Sort(sort.Reverse(ModeInfos(result)))
return
}
const (
OutputDeviceTransformNormal = 0
OutputDeviceTransform90 = 1
OutputDeviceTransform180 = 2
OutputDeviceTransform270 = 3
OutputDeviceTransformFlipped = 4
OutputDeviceTransformFlipped90 = 5
OutputDeviceTransformFlipped180 = 6
OutputDeviceTransformFlipped270 = 7
)
const (
OutputDeviceModeCurrent = 1 << 0
OutputDeviceModePreferred = 1 << 1
)
func (oi *KOutputInfo) getBestMode() ModeInfo {
var preferredMode *KModeInfo
for _, info := range oi.ModeInfos {
if info.Flags&OutputDeviceModePreferred != 0 {
preferredMode = &info
break
}
}
if preferredMode == nil {
// not found preferred mode
return getMaxAreaOutputDeviceMode(oi.ModeInfos).toModeInfo()
}
return preferredMode.toModeInfo()
}
func (oi *KOutputInfo) getCurrentMode() ModeInfo {
for _, info := range oi.ModeInfos {
if info.Flags&OutputDeviceModeCurrent != 0 {
return info.toModeInfo()
}
}
return ModeInfo{}
}
func (oi *KOutputInfo) rotation() uint16 {
switch oi.Transform {
case OutputDeviceTransformNormal:
return randr.RotationRotate0
case OutputDeviceTransform90:
return randr.RotationRotate90
case OutputDeviceTransform180:
return randr.RotationRotate180
case OutputDeviceTransform270:
return randr.RotationRotate270
case OutputDeviceTransformFlipped:
return randr.RotationRotate0
case OutputDeviceTransformFlipped90:
return randr.RotationRotate90
case OutputDeviceTransformFlipped180:
return randr.RotationRotate180
case OutputDeviceTransformFlipped270:
return randr.RotationRotate270
}
return 0
}
func randrRotationToTransform(rotation int) int {
switch rotation {
case randr.RotationRotate0:
return OutputDeviceTransformNormal
case randr.RotationRotate90:
return OutputDeviceTransform90
case randr.RotationRotate180:
return OutputDeviceTransform180
case randr.RotationRotate270:
return OutputDeviceTransform270
}
return 0
}
func getMaxAreaOutputDeviceMode(modes []KModeInfo) KModeInfo {
if len(modes) == 0 {
return KModeInfo{}
}
maxAreaMode := modes[0]
for _, mode := range modes[1:] {
if int(maxAreaMode.Width)*int(maxAreaMode.Height) < int(mode.Width)*int(mode.Height) {
maxAreaMode = mode
}
}
return maxAreaMode
}
func (oi *KOutputInfo) getEnabled() bool {
return int32ToBool(oi.Enabled)
}
func (oi *KOutputInfo) getName() string {
return getOutputDeviceName(oi.Model, oi.Manufacturer)
}
type KModeInfo struct {
Id int32 `json:"id"`
Width int32 `json:"width"`
Height int32 `json:"height"`
RefreshRate int32 `json:"refresh_rate"`
Flags int32 `json:"flags"`
}
func (mi KModeInfo) toModeInfo() ModeInfo {
return ModeInfo{
Id: uint32(mi.Id),
name: mi.name(),
Width: uint16(mi.Width),
Height: uint16(mi.Height),
Rate: mi.rate(),
}
}
func (mi KModeInfo) name() string {
return fmt.Sprintf("%dx%d", mi.Width, mi.Height)
}
func (mi KModeInfo) rate() float64 {
return float64(mi.RefreshRate) / 1000.0
}
func unmarshalOutputInfos(str string) ([]*KOutputInfo, error) {
var v outputInfoWrap
err := json.Unmarshal([]byte(str), &v)
if err != nil {
return nil, err
}
return v.OutputInfo, nil
}
func unmarshalOutputInfo(str string) (*KOutputInfo, error) {
var v outputInfoWrap
err := json.Unmarshal([]byte(str), &v)
if err != nil {
return nil, err
}
if len(v.OutputInfo) == 0 {
return nil, errors.New("length of slice v.OutputInfo is 0")
}
return v.OutputInfo[0], nil
}
type outputInfoWrap struct {
OutputInfo []*KOutputInfo
}
func (m *Manager) listOutput() ([]*KOutputInfo, error) {
var outputJ string
var duration = time.Millisecond * 500
// sometimes got the output list will return nil, this is the output service not inited yet.
// so try got 3 times.
for i := 0; i < 3; i++ {
data, err := m.management.ListOutput(0)
if len(data) != 0 {
outputJ = data
break
}
if err != nil || len(data) == 0 {
logger.Warning("Failed to get output list:", err)
}
time.Sleep(duration)
duration += 100
}
logger.Debug("outputJ:", outputJ)
return unmarshalOutputInfos(outputJ)
}
// such as: make('dell'), model('eDP-1-dell'), so name is 'eDP-1'
func getOutputDeviceName(model, make string) string {
logger.Debugf("[DEBUG] get name: '%s', '%s'", model, make)
name := getNameFromModelAndMake(model, make)
if name != model {
return name
}
names := strings.Split(model, "-")
if len(names) <= 2 {
return getNameFromModel(model)
}
idx := len(names) - 1
for ; idx > 1; idx-- {
if len(names[idx]) > 1 {
continue
}
break
}
return strings.Join(names[:idx+1], "-")
}
func getNameFromModel(model string) string {
idx := strings.IndexByte(model, ' ')
if idx == -1 {
return model
}
return model[:idx]
}
func getNameFromModelAndMake(model, make string) string {
preMake := strings.Split(make, " ")[0]
name := strings.Split(model, preMake)[0]
return strings.TrimRight(name, "-")
}
func int32ToBool(v int32) bool {
return v != 0
}