-
Notifications
You must be signed in to change notification settings - Fork 50
/
xorg.go
1249 lines (1098 loc) · 34.2 KB
/
xorg.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package display
import (
"errors"
"fmt"
"math"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/linuxdeepin/go-lib/log"
x "github.com/linuxdeepin/go-x11-client"
"github.com/linuxdeepin/go-x11-client/ext/input"
"github.com/linuxdeepin/go-x11-client/ext/randr"
"github.com/linuxdeepin/go-x11-client/ext/xfixes"
)
var _xConn *x.Conn
var _hasRandr1d2 bool // 是否 randr 版本大于等于 1.2
var _useWayland bool
func Init(xConn *x.Conn, useWayland bool) {
_xConn = xConn
_useWayland = useWayland
randrVersion, err := randr.QueryVersion(xConn, randr.MajorVersion, randr.MinorVersion).Reply(xConn)
if err != nil {
logger.Warning(err)
} else {
logger.Debugf("randr version %d.%d", randrVersion.ServerMajorVersion, randrVersion.ServerMinorVersion)
if randrVersion.ServerMajorVersion > 1 ||
(randrVersion.ServerMajorVersion == 1 && randrVersion.ServerMinorVersion >= 2) {
_hasRandr1d2 = true
}
logger.Debug("has randr1.2:", _hasRandr1d2)
}
if _greeterMode {
// 仅 greeter 需要
_, err = xfixes.QueryVersion(xConn, xfixes.MajorVersion, xfixes.MinorVersion).Reply(xConn)
if err != nil {
logger.Warning(err)
}
_, err = input.XIQueryVersion(xConn, input.MajorVersion, input.MinorVersion).Reply(xConn)
if err != nil {
logger.Warning(err)
return
}
}
}
func GetRecommendedScaleFactor() float64 {
if !_hasRandr1d2 {
return 1
}
resources, err := getScreenResources(_xConn)
if err != nil {
logger.Warning(err)
return 1
}
cfgTs := resources.ConfigTimestamp
var monitors []*monitorSizeInfo
for _, output := range resources.Outputs {
outputInfo, err := randr.GetOutputInfo(_xConn, output, cfgTs).Reply(_xConn)
if err != nil {
logger.Warningf("get output %v info failed: %v", output, err)
return 1.0
}
if outputInfo.Connection != randr.ConnectionConnected {
continue
}
if outputInfo.Crtc == 0 {
continue
}
crtcInfo, err := randr.GetCrtcInfo(_xConn, outputInfo.Crtc, cfgTs).Reply(_xConn)
if err != nil {
logger.Warningf("get crtc %v info failed: %v", outputInfo.Crtc, err)
return 1.0
}
monitors = append(monitors, &monitorSizeInfo{
mmWidth: outputInfo.MmWidth,
mmHeight: outputInfo.MmHeight,
width: crtcInfo.Width,
height: crtcInfo.Height,
})
}
if len(monitors) == 0 {
return 1.0
}
minScaleFactor := 3.0
for _, monitor := range monitors {
scaleFactor := calcRecommendedScaleFactor(float64(monitor.width), float64(monitor.height),
float64(monitor.mmWidth), float64(monitor.mmHeight))
if minScaleFactor > scaleFactor {
minScaleFactor = scaleFactor
}
}
return minScaleFactor
}
func getScreenResources(xConn *x.Conn) (*randr.GetScreenResourcesReply, error) {
root := xConn.GetDefaultScreen().Root
resources, err := randr.GetScreenResources(xConn, root).Reply(xConn)
return resources, err
}
const evMaskForHideCursor uint32 = input.XIEventMaskRawMotion | input.XIEventMaskRawTouchBegin
func (m *Manager) listenXEvents() {
if _useWayland {
return
}
eventChan := m.xConn.MakeAndAddEventChan(50)
root := m.xConn.GetDefaultScreen().Root
// 选择监听哪些 randr 事件
err := randr.SelectInputChecked(m.xConn, root,
randr.NotifyMaskOutputChange|randr.NotifyMaskOutputProperty|
randr.NotifyMaskCrtcChange|randr.NotifyMaskScreenChange).Check(m.xConn)
if err != nil {
logger.Warning("failed to select randr event:", err)
return
}
var inputExtData *x.QueryExtensionReply
if _greeterMode {
// 仅 greeter 需要
err = m.doXISelectEvents(evMaskForHideCursor)
if err != nil {
logger.Warning(err)
}
inputExtData = m.xConn.GetExtensionData(input.Ext())
}
rrExtData := m.xConn.GetExtensionData(randr.Ext())
go func() {
for ev := range eventChan {
switch ev.GetEventCode() {
case randr.NotifyEventCode + rrExtData.FirstEvent:
event, _ := randr.NewNotifyEvent(ev)
switch event.SubCode {
case randr.NotifyOutputChange:
e, _ := event.NewOutputChangeNotifyEvent()
m.mm.HandleEvent(e)
case randr.NotifyCrtcChange:
e, _ := event.NewCrtcChangeNotifyEvent()
m.mm.HandleEvent(e)
case randr.NotifyOutputProperty:
e, _ := event.NewOutputPropertyNotifyEvent()
// TODO mm 可能也应该处理这个事件
m.handleOutputPropertyChanged(e)
}
case randr.ScreenChangeNotifyEventCode + rrExtData.FirstEvent:
event, _ := randr.NewScreenChangeNotifyEvent(ev)
cfgTsChanged := m.mm.HandleScreenChanged(event)
m.handleScreenChanged(event, cfgTsChanged)
case x.GeGenericEventCode:
if !_greeterMode {
continue
}
// 仅 greeter 处理这个事件
geEvent, _ := x.NewGeGenericEvent(ev)
if geEvent.Extension == inputExtData.MajorOpcode {
switch geEvent.EventType {
case input.RawMotionEventCode:
m.beginMoveMouse()
case input.RawTouchBeginEventCode:
m.beginTouch()
}
}
}
}
}()
}
type monitorManagerHooks interface {
handleMonitorAdded(monitorInfo *MonitorInfo)
handleMonitorRemoved(monitorId uint32)
handleMonitorChanged(monitorInfo *MonitorInfo)
handlePrimaryRectChanged(pmi primaryMonitorInfo)
getMonitorsId() monitorsId
}
type monitorManager interface {
setHooks(hooks monitorManagerHooks)
getMonitors() []*MonitorInfo
getMonitor(id uint32) *MonitorInfo
apply(monitorsId monitorsId, monitorMap map[uint32]*Monitor, prevScreenSize screenSize, options applyOptions, fillModes map[string]string, primaryMonitorID uint32, displayMode byte) error
setMonitorPrimary(monitorId uint32) error
setMonitorFillMode(monitor *Monitor, fillMode string) error
showCursor(show bool) error
HandleEvent(ev interface{})
HandleScreenChanged(e *randr.ScreenChangeNotifyEvent) (cfgTsChanged bool)
}
type xMonitorManager struct {
hooks monitorManagerHooks
mu sync.Mutex
xConn *x.Conn
hasRandr1d2 bool
cfgTs x.Timestamp
monitorsCache []*MonitorInfo
modes []randr.ModeInfo
crtcs map[randr.Crtc]*CrtcInfo
outputs map[randr.Output]*OutputInfo
primary randr.Output
monitorChangedCbEnabled bool
// 键是 x 的 output 名称,值是标准名。
stdNamesCache map[string]string
}
func newXMonitorManager(xConn *x.Conn, hasRandr1d2 bool) *xMonitorManager {
xmm := &xMonitorManager{
xConn: xConn,
hasRandr1d2: hasRandr1d2,
crtcs: make(map[randr.Crtc]*CrtcInfo),
outputs: make(map[randr.Output]*OutputInfo),
stdNamesCache: make(map[string]string),
}
err := xmm.init()
if err != nil {
logger.Warning("xMonitorManager init failed:", err)
}
return xmm
}
func (mm *xMonitorManager) setHooks(hooks monitorManagerHooks) {
mm.hooks = hooks
}
type CrtcInfo randr.GetCrtcInfoReply
func (ci *CrtcInfo) getRect() x.Rectangle {
rect := x.Rectangle{
X: ci.X,
Y: ci.Y,
Width: ci.Width,
Height: ci.Height,
}
swapWidthHeightWithRotation(ci.Rotation, &rect.Width, &rect.Height)
return rect
}
type OutputInfo randr.GetOutputInfoReply
func (oi *OutputInfo) PreferredMode() randr.Mode {
return (*randr.GetOutputInfoReply)(oi).GetPreferredMode()
}
type primaryMonitorInfo struct {
Name string
Rect x.Rectangle
}
func (pmi primaryMonitorInfo) IsRectEmpty() bool {
return pmi.Rect == x.Rectangle{}
}
func (mm *xMonitorManager) init() error {
if !mm.hasRandr1d2 {
return nil
}
xConn := mm.xConn
resources, err := mm.getScreenResources(xConn)
if err != nil {
return err
}
mm.cfgTs = resources.ConfigTimestamp
mm.modes = resources.Modes
for _, outputId := range resources.Outputs {
reply, err := mm.getOutputInfo(outputId)
if err != nil {
return err
}
mm.outputs[outputId] = (*OutputInfo)(reply)
}
for _, crtcId := range resources.Crtcs {
reply, err := mm.getCrtcInfo(crtcId)
if err != nil {
return err
}
mm.crtcs[crtcId] = (*CrtcInfo)(reply)
}
mm.refreshMonitorsCache()
return nil
}
func (mm *xMonitorManager) getCrtcs() map[randr.Crtc]*CrtcInfo {
mm.mu.Lock()
defer mm.mu.Unlock()
result := make(map[randr.Crtc]*CrtcInfo, len(mm.crtcs))
for crtc, info := range mm.crtcs {
infoCopy := &CrtcInfo{}
*infoCopy = *info
result[crtc] = infoCopy
}
return result
}
func (mm *xMonitorManager) getMonitor(id uint32) *MonitorInfo {
mm.mu.Lock()
defer mm.mu.Unlock()
for _, monitor := range mm.monitorsCache {
if monitor.ID == id {
monitorCp := *monitor
return &monitorCp
}
}
return nil
}
func (mm *xMonitorManager) getMonitors() []*MonitorInfo {
mm.mu.Lock()
defer mm.mu.Unlock()
monitors := make([]*MonitorInfo, len(mm.monitorsCache))
for i, monitorInfo := range mm.monitorsCache {
monitorCp := *monitorInfo
monitors[i] = &monitorCp
}
return monitors
}
func (mm *xMonitorManager) doDiff() {
logger.Debug("mm.doDiff")
// NOTE: 不要加锁
oldMonitors := toMonitorInfoMap(mm.monitorsCache)
mm.refreshMonitorsCache()
newMonitors := mm.monitorsCache
for _, monitor := range newMonitors {
oldMonitor, ok := oldMonitors[monitor.ID]
if ok {
if !monitor.equal(oldMonitor) {
if mm.monitorChangedCbEnabled {
if mm.hooks != nil {
logger.Debug("call manager handleMonitorChanged", monitor.ID)
// NOTE: mm.mu 已经上锁了
mm.mu.Unlock()
mm.hooks.handleMonitorChanged(monitor)
mm.mu.Lock()
}
} else {
logger.Debug("monitorChangedCb disabled")
}
if monitor.outputId() == mm.primary {
// NOTE: mm.mu 已经上锁了
mm.mu.Unlock()
mm.invokePrimaryRectChangedCb(mm.primary)
mm.mu.Lock()
}
}
} else {
logger.Warning("can not handle new monitor")
}
}
}
func (mm *xMonitorManager) wait(crtcCfgs map[randr.Crtc]crtcConfig, disabledOutputs map[randr.Output]bool, monitorsId monitorsId) {
now := time.Now()
defer func() {
logger.Debug("wait cost", time.Since(now))
}()
const (
timeout = 5 * time.Second
interval = 500 * time.Millisecond
count = int(timeout / interval)
)
for i := 0; i < count; i++ {
if mm.compareAll(crtcCfgs, disabledOutputs) {
logger.Debug("mm wait success")
return
}
if mm.hooks != nil && mm.hooks.getMonitorsId() != monitorsId {
logger.Debug("monitorsId changed, wait return")
return
}
time.Sleep(interval)
}
logger.Warning("mm wait time out")
}
func (mm *xMonitorManager) compareAll(crtcCfgs map[randr.Crtc]crtcConfig, disabledOutputs map[randr.Output]bool) bool {
mm.mu.Lock()
defer mm.mu.Unlock()
//logger.Debug("crtcCfgs:", spew.Sdump(crtcCfgs))
//logger.Debug("mm.crtcs:", spew.Sdump(mm.crtcs))
//logger.Debug("mm.outputs:", spew.Sdump(mm.outputs))
for crtc, crtcCfg := range crtcCfgs {
crtcInfo := mm.crtcs[crtc]
if len(crtcCfg.outputs) > 0 {
// 启用 crtc 的情况
if !(crtcCfg.x == crtcInfo.X &&
crtcCfg.y == crtcInfo.Y &&
crtcCfg.mode == crtcInfo.Mode &&
crtcCfg.rotation == crtcInfo.Rotation &&
outputSliceEqual(crtcCfg.outputs, crtcInfo.Outputs)) {
if logger.GetLogLevel() == log.LevelDebug {
logger.Debugf("[compareAll] crtc %d not match, crtcCfg(expect): %v, crtcInfo(actual): %v",
crtcCfg.crtc, spew.Sdump(crtcCfg), spew.Sdump(crtcInfo))
}
return false
}
} else {
// 禁用 crtc 的情况, len(crtcCfg.outputs) == 0 成立。
if !(len(crtcInfo.Outputs) == 0 && crtcInfo.Mode == 0 && crtcInfo.Width == 0 && crtcInfo.Height == 0) {
if logger.GetLogLevel() == log.LevelDebug {
logger.Debugf("[compareAll] crtc %d not disabled, crtcCfg(expect): %v, crtcInfo(actual): %v",
crtcCfg.crtc, spew.Sdump(crtcCfg), spew.Sdump(crtcInfo))
}
return false
}
}
if len(crtcCfg.outputs) > 0 {
outputId := crtcCfg.outputs[0]
outputInfo := mm.outputs[outputId]
if crtcCfg.crtc != outputInfo.Crtc {
logger.Debugf("[compareAll] output %v crtc not match, output crtc expect: %v, actual: %v",
outputId, crtcCfg.crtc, outputInfo.Crtc)
return false
}
}
}
for output := range disabledOutputs {
outputInfo := mm.outputs[output]
if outputInfo.Crtc != 0 {
logger.Debugf("[compareAll] output %v crtc != 0", output)
return false
}
}
return true
}
func toMonitorInfoMap(monitors []*MonitorInfo) map[uint32]*MonitorInfo {
result := make(map[uint32]*MonitorInfo, len(monitors))
for _, monitor := range monitors {
result[monitor.ID] = monitor
}
return result
}
func (mm *xMonitorManager) getStdMonitorName(name string, edid []byte) (string, error) {
// NOTE:不要加锁
stdName := mm.stdNamesCache[name]
if stdName != "" {
return stdName, nil
}
stdName, err := getStdMonitorName(edid)
if err != nil {
return "", err
}
mm.stdNamesCache[name] = stdName
return stdName, nil
}
func (mm *xMonitorManager) refreshMonitorsCache() {
// NOTE: 不要加锁
monitors := make([]*MonitorInfo, 0, len(mm.outputs))
for outputId, outputInfo := range mm.outputs {
monitor := &MonitorInfo{
crtc: outputInfo.Crtc,
ID: uint32(outputId),
Name: outputInfo.Name,
Connected: outputInfo.Connection == randr.ConnectionConnected,
Modes: toModeInfos(mm.modes, outputInfo.Modes),
MmWidth: outputInfo.MmWidth,
MmHeight: outputInfo.MmHeight,
}
monitor.PreferredMode = getPreferredMode(monitor.Modes, uint32(outputInfo.PreferredMode()))
var err error
monitor.EDID, err = mm.getOutputEdid(outputId)
if err != nil {
logger.Warningf("get output %d edid failed: %v", outputId, err)
}
stdName := ""
if monitor.Connected {
stdName, err = mm.getStdMonitorName(monitor.Name, monitor.EDID)
if err != nil {
logger.Warningf("get monitor %v std name failed: %v", monitor.Name, err)
}
}
monitor.UUID = getOutputUuid(monitor.Name, stdName, monitor.EDID)
monitor.UuidV0 = getOutputUuidV0(monitor.Name, monitor.EDID)
monitor.Manufacturer, monitor.Model = parseEdid(monitor.EDID)
availFillModes, err := mm.getOutputAvailableFillModes(outputId)
if err != nil {
logger.Warningf("get output %d available fill modes failed: %v", outputId, err)
}
monitor.AvailableFillModes = availFillModes
// TODO 获取显示器当前的 fill mode
if monitor.crtc != 0 {
crtcInfo := mm.crtcs[monitor.crtc]
if crtcInfo != nil {
monitor.X = crtcInfo.X
monitor.Y = crtcInfo.Y
monitor.Rotation = crtcInfo.Rotation
monitor.Width, monitor.Height = crtcInfo.Width, crtcInfo.Height
swapWidthHeightWithRotation(crtcInfo.Rotation, &monitor.Width, &monitor.Height)
monitor.Rotations = crtcInfo.Rotations
monitor.CurrentMode = findModeInfo(mm.modes, crtcInfo.Mode)
}
}
if monitor.Connected && monitor.Width != 0 && monitor.Height != 0 {
monitor.VirtualConnected = true
monitor.Enabled = true
} else {
monitor.VirtualConnected = false
monitor.Enabled = false
}
monitors = append(monitors, monitor)
}
mm.monitorsCache = monitors
}
func (mm *xMonitorManager) getFreeCrtcMap() map[randr.Crtc]bool {
result := make(map[randr.Crtc]bool)
mm.mu.Lock()
defer mm.mu.Unlock()
for crtc, crtcInfo := range mm.crtcs {
if len(crtcInfo.Outputs) == 0 {
result[crtc] = true
}
}
return result
}
func (mm *xMonitorManager) findFreeCrtc(output randr.Output, freeCrtcs map[randr.Crtc]bool) randr.Crtc {
mm.mu.Lock()
defer mm.mu.Unlock()
for crtc, crtcInfo := range mm.crtcs {
isFree := freeCrtcs[crtc]
if isFree && outputSliceContains(crtcInfo.PossibleOutputs, output) {
freeCrtcs[crtc] = false
return crtc
}
}
return 0
}
type applyOptions map[string]interface{}
const (
optionDisableCrtc = "disableCrtc"
optionOnlyOne = "onlyOne"
)
type crtcConfig struct {
crtc randr.Crtc
outputs []randr.Output
x int16
y int16
rotation uint16
mode randr.Mode
}
func findOutputInCrtcCfgs(crtcCfgs map[randr.Crtc]crtcConfig, crtc randr.Crtc) randr.Output {
for _, crtcCfg := range crtcCfgs {
if crtcCfg.crtc == crtc {
if len(crtcCfg.outputs) > 0 {
return crtcCfg.outputs[0]
}
}
}
return 0
}
func (mm *xMonitorManager) apply(monitorsId monitorsId, monitorMap map[uint32]*Monitor, prevScreenSize screenSize,
options applyOptions, fillModes map[string]string, primaryMonitorID uint32, displayMode byte) error {
logger.Debug("call apply", monitorsId)
optDisableCrtc, _ := options[optionDisableCrtc].(bool)
disabledOutputs := make(map[randr.Output]bool)
freeCrtcs := mm.getFreeCrtcMap()
// 继续找更多的 free crtc
for _, monitor := range monitorMap {
monitor.dumpInfoForDebug()
monitorInfo := mm.getMonitor(monitor.ID)
if monitorInfo == nil {
logger.Warningf("[apply] failed to get monitor %d", monitor.ID)
continue
}
if !monitor.Enabled {
// 禁用显示器
disabledOutputs[randr.Output(monitor.ID)] = true
crtc := monitorInfo.crtc
if crtc != 0 {
freeCrtcs[crtc] = true
}
}
}
// 根据 monitor 的配置,准备 crtc 配置放到 crtcCfgs 中。
crtcCfgs := make(map[randr.Crtc]crtcConfig)
for output, monitor := range monitorMap {
monitorInfo := mm.getMonitor(monitor.ID)
if monitorInfo == nil {
logger.Warningf("[apply] failed to get monitor %d", monitor.ID)
continue
}
crtc := monitorInfo.crtc
if monitor.Enabled {
// 启用显示器
if crtc == 0 {
crtc = mm.findFreeCrtc(randr.Output(output), freeCrtcs)
if crtc == 0 {
return errors.New("failed to find free crtc")
}
}
crtcCfgs[crtc] = crtcConfig{
crtc: crtc,
x: monitor.X,
y: monitor.Y,
mode: randr.Mode(monitor.CurrentMode.Id),
rotation: monitor.Rotation | monitor.Reflect,
outputs: []randr.Output{randr.Output(output)},
}
}
}
for crtc, isFree := range freeCrtcs {
if isFree {
// 禁用此 crtc,把它的 outputs 设置为空。
crtcCfgs[crtc] = crtcConfig{
crtc: crtc,
rotation: randr.RotationRotate0,
}
}
}
logger.Debug("freeCrtcs:", freeCrtcs)
logger.Debug("disableOutputs", disabledOutputs)
if logger.GetLogLevel() == log.LevelDebug {
logger.Debug("crtcCfgs:", spew.Sdump(crtcCfgs))
}
// 未来的,apply 之后的屏幕所需尺寸
screenSize := getScreenSize(monitorMap)
logger.Debugf("screen size after apply: %+v", screenSize)
monitors := getConnectedMonitors(monitorMap)
x.GrabServer(mm.xConn)
logger.Debug("grab server")
ungrabServerDone := false
mm.monitorChangedCbEnabled = false
ungrabServer := func() {
if !ungrabServerDone {
logger.Debug("ungrab server")
err := x.UngrabServerChecked(mm.xConn).Check(mm.xConn)
if err != nil {
logger.Warning(err)
}
ungrabServerDone = true
}
}
defer func() {
ungrabServer()
mm.monitorChangedCbEnabled = true
logger.Debug("apply return", monitorsId)
}()
var shouldDelayDisableCrtc = false
for crtc, crtcInfo := range mm.getCrtcs() {
rect := crtcInfo.getRect()
logger.Debugf("crtc %v, crtcInfo: %+v", crtc, crtcInfo)
// 是否考虑临时禁用 crtc
shouldDisable := false
if optDisableCrtc {
// 可能是切换了显示模式
// NOTE: 如果接入了双屏,断开一个屏幕,让另外的屏幕都暂时禁用,来避免桌面壁纸的闪烁问题(突然黑一下,然后很快恢复),
// 这么做是为了兼顾修复 pms bug 83875 和 94116。
// 但是对于 bug 94116,依然保留问题:先断开再连接显示器,桌面壁纸依然有闪烁问题。
logger.Debugf("should disable crtc %v because of optDisableCrtc is true", crtc)
shouldDisable = true
} else if int(rect.X)+int(rect.Width) > int(screenSize.width) ||
int(rect.Y)+int(rect.Height) > int(screenSize.height) {
// 当前 crtc 的尺寸超过了未来的屏幕尺寸,必须禁用
logger.Debugf("should disable crtc %v because of the size of crtc exceeds the size of future screen", crtc)
shouldDisable = true
} else {
output := findOutputInCrtcCfgs(crtcCfgs, crtc)
if output != 0 {
monitor := monitors.GetById(uint32(output))
// 根据 crtc 找到对应的 monitor
if monitor != nil && monitor.Enabled {
if rect.X != monitor.X || rect.Y != monitor.Y ||
rect.Width != monitor.Width || rect.Height != monitor.Height ||
crtcInfo.Rotation != monitor.Rotation|monitor.Reflect {
// crtc 的参数将发生改变, 这里的 monitor 包含了 crtc 未来的状态。
logger.Debugf("should disable crtc %v because of the parameters of crtc changed", crtc)
shouldDisable = true
}
}
}
}
if shouldDisable && len(crtcInfo.Outputs) > 0 {
logger.Debugf("disable crtc %v, it's outputs: %v", crtc, crtcInfo.Outputs)
shouldDelayDisableCrtc = true
err := mm.disableCrtc(crtc)
if err != nil {
return err
}
}
}
// disableCrtc后等待1秒,防止多个屏幕反复disabled导致屏幕提示 频率超出范围 黑屏现象
// NOTE:此处延时是为了修复BUG 107874 104595 107865
if shouldDelayDisableCrtc {
time.Sleep(1 * time.Second)
}
err := mm.setScreenSize(screenSize)
if err != nil {
return err
}
// 为了规避扩展模式下,A B屏相同低分辨率(X 分辨率)的平铺方式不一致,当切换A B屏为复制模式后
// 此时默认非X分辨率,此时切换到X分辨率,此时需要做规避,以此时主屏的平铺方式去设置所有屏幕
var primaryScreenFillMode = fillModeDefault
if monitorMap[primaryMonitorID] != nil {
primaryScreenFillMode = fillModes[monitorMap[primaryMonitorID].generateFillModeKey()]
}
for id, monitor := range monitorMap {
var monitorFillMode = fillModeDefault
if displayMode == DisplayModeMirror {
monitorFillMode = primaryScreenFillMode
} else {
monitorFillMode = fillModes[monitor.generateFillModeKey()]
}
err = mm.setMonitorFillMode(monitor.m.monitorMap[id], monitorFillMode)
if err != nil {
logger.Warning("set monitor fill mode failed:", monitor, err)
}
}
for _, crtcCfg := range crtcCfgs {
err := mm.setCrtcConfig(crtcCfg)
if err != nil {
logger.Warning("set crtcConfig failed:", crtcCfg, err)
}
}
ungrabServer()
// 等待所有事件结束
mm.wait(crtcCfgs, disabledOutputs, monitorsId)
// 更新一遍所有显示器
mm.monitorChangedCbEnabled = true
logger.Debug("update all monitors")
for _, monitor := range monitorMap {
monitorInfo := mm.getMonitor(monitor.ID)
if monitorInfo != nil {
if mm.hooks != nil {
mm.hooks.handleMonitorChanged(monitorInfo)
}
}
}
logger.Debug("after update all monitors")
// NOTE: 为配合文件管理器修一个 bug:
// 双屏左右摆放,两屏幕有相同最大分辨率,设置左屏为主屏,自定义模式下两屏合并、拆分循环切换,此时如果不发送 PrimaryRect 属性
// 改变信号,将在从合并切换到拆分时,右屏的桌面壁纸没有绘制,是全黑的。可能是所有显示器的分辨率都没有改变,桌面 dde-desktop
// 程序收不到相关信号。
// 此时屏幕尺寸被改变是很好的特征,发送一个 PrimaryRect 属性改变通知桌面 dde-desktop 程序让它重新绘制桌面壁纸,以消除 bug。
// TODO: 这不是一个很好的方案,后续可与桌面程序方面沟通改善方案。
if prevScreenSize.width != screenSize.width || prevScreenSize.height != screenSize.height {
// screen size changed
// NOTE: 不能直接用 prevScreenSize != screenSize 进行比较,因为 screenSize 类型不止 width 和 height 字段。
logger.Debug("[apply] screen size changed, force emit prop changed for PrimaryRect")
// TODO
//m.PropsMu.RLock()
//rect := m.PrimaryRect
//m.PropsMu.RUnlock()
//err := m.emitPropChangedPrimaryRect(rect)
//if err != nil {
// logger.Warning(err)
//}
}
return nil
}
func (mm *xMonitorManager) setMonitorFillMode(monitor *Monitor, fillMode string) error {
if !monitor.Enabled {
return nil
}
if len(monitor.AvailableFillModes) == 0 {
return nil
}
if fillMode == "" {
fillMode = fillModeDefault
}
err := mm.setOutputScalingMode(randr.Output(monitor.ID), fillMode)
if err != nil {
return err
}
// TODO 后续可以根据 output 属性改变来处理
monitor.setPropCurrentFillMode(fillMode)
return nil
}
func getConnectedMonitors(monitorMap map[uint32]*Monitor) Monitors {
var monitors Monitors
for _, monitor := range monitorMap {
monitor.PropsMu.RLock()
connected := monitor.realConnected
monitor.PropsMu.RUnlock()
if connected {
monitors = append(monitors, monitor)
}
}
return monitors
}
// getScreenSize 计算出需要的屏幕尺寸
func getScreenSize(monitorMap map[uint32]*Monitor) screenSize {
width, height := getScreenWidthHeight(monitorMap)
mmWidth := uint32(float64(width) / 3.792)
mmHeight := uint32(float64(height) / 3.792)
return screenSize{
width: width,
height: height,
mmWidth: mmWidth,
mmHeight: mmHeight,
}
}
// getScreenWidthHeight 根据 monitorMap 中显示器的设置,计算出需要的屏幕尺寸。
func getScreenWidthHeight(monitorMap map[uint32]*Monitor) (sw, sh uint16) {
var w, h int
for _, monitor := range monitorMap {
if !monitor.realConnected || !monitor.Enabled {
continue
}
width := monitor.CurrentMode.Width
height := monitor.CurrentMode.Height
swapWidthHeightWithRotation(monitor.Rotation, &width, &height)
w1 := int(monitor.X) + int(width)
h1 := int(monitor.Y) + int(height)
if w < w1 {
w = w1
}
if h < h1 {
h = h1
}
}
if w > math.MaxUint16 {
w = math.MaxUint16
}
if h > math.MaxUint16 {
h = math.MaxUint16
}
sw = uint16(w)
sh = uint16(h)
return
}
func (mm *xMonitorManager) setScreenSize(ss screenSize) error {
root := mm.xConn.GetDefaultScreen().Root
err := randr.SetScreenSizeChecked(mm.xConn, root, ss.width, ss.height, ss.mmWidth,
ss.mmHeight).Check(mm.xConn)
logger.Debugf("set screen size %dx%d, mm: %dx%d",
ss.width, ss.height, ss.mmWidth, ss.mmHeight)
return err
}
func (mm *xMonitorManager) disableCrtc(crtc randr.Crtc) error {
return mm.setCrtcConfig(crtcConfig{
crtc: crtc,
rotation: randr.RotationRotate0,
})
}
func (mm *xMonitorManager) setCrtcConfig(cfg crtcConfig) error {
mm.mu.Lock()
cfgTs := mm.cfgTs
mm.mu.Unlock()
logger.Debugf("setCrtcConfig crtc: %v, cfgTs: %v, x: %v, y: %v,"+
" mode: %v, rotation|reflect: %v, outputs: %v",
cfg.crtc, cfgTs, cfg.x, cfg.y, cfg.mode, cfg.rotation, cfg.outputs)
setCfg, err := randr.SetCrtcConfig(mm.xConn, cfg.crtc, 0, cfgTs,
cfg.x, cfg.y, cfg.mode, cfg.rotation,
cfg.outputs).Reply(mm.xConn)
if err != nil {
return err
}
if setCfg.Status != randr.SetConfigSuccess {
err = fmt.Errorf("failed to configure crtc %v: %v",
cfg.crtc, getRandrStatusStr(setCfg.Status))
return err
}
return nil
}
func (mm *xMonitorManager) getOutputAvailableFillModes(output randr.Output) ([]string, error) {
// 判断是否有该属性
lsPropsReply, err := randr.ListOutputProperties(mm.xConn, output).Reply(mm.xConn)
if err != nil {
return nil, err
}
atomScalingMode, err := mm.xConn.GetAtom("scaling mode")
if err != nil {
return nil, err
}
hasProp := false
for _, atom := range lsPropsReply.Atoms {
if atom == atomScalingMode {
hasProp = true
break
}
}
if !hasProp {
return nil, nil
}
// 获取属性可能的值
outputProp, _ := randr.QueryOutputProperty(mm.xConn, output, atomScalingMode).Reply(mm.xConn)
var result []string
for _, prop := range outputProp.ValidValues {
fillMode, _ := mm.xConn.GetAtomName(x.Atom(prop))
result = append(result, fillMode)
}
return result, nil
}
func (mm *xMonitorManager) setOutputScalingMode(output randr.Output, fillMode string) error {
if fillMode != fillModeFull &&
fillMode != fillModeCenter &&
fillMode != fillModeDefault &&
fillMode != fillModeFullaspect {
logger.Warning("invalid fill mode:", fillMode)
return fmt.Errorf("invalid fill mode %q", fillMode)
}
xConn := mm.xConn
fillModeU32, _ := xConn.GetAtom(fillMode)
name, _ := xConn.GetAtom("scaling mode")
// TODO 改成不用 get ?
outputPropReply, err := randr.GetOutputProperty(xConn, output, name, 0, 0,
100, false, false).Reply(xConn)
if err != nil {
logger.Warning("call GetOutputProperty reply err:", err)
return err
}