Repository navigation
fix: preserve display settings across lid transitions #1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ package display1 | |
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/linuxdeepin/dde-daemon/common/scale" | ||
| configManager "github.com/linuxdeepin/go-dbus-factory/org.desktopspec.ConfigManager" | ||
| syspower "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.power1" | ||
| ) | ||
|
|
@@ -78,12 +79,20 @@ func (m *Manager) updateLidState(closed bool) bool { | |
| break | ||
| } | ||
| } | ||
| // 扩展模式和仅内屏模式合盖都需要初始化外屏;仅外屏模式保持原值。 | ||
| initializeExternal := closed && hasExternal && m.shouldInitializeLidExternal(builtin) | ||
| m.lidScaleMu.Lock() | ||
| defer m.lidScaleMu.Unlock() | ||
| builtin.PropsMu.Lock() | ||
| defer builtin.PropsMu.Unlock() | ||
| previous := builtin.lidClosed | ||
| builtin.lidClosed = closed && hasExternal | ||
| changed := previous != builtin.lidClosed | ||
| logger.Infof("sync lid closed: %v", builtin.lidClosed) | ||
| return previous != builtin.lidClosed | ||
| builtin.PropsMu.Unlock() | ||
| if changed { | ||
| m.lidScalePending = initializeExternal | ||
| } | ||
| return changed | ||
| } | ||
|
|
||
| func (m *Manager) handleLidSwitch(sysPower syspower.Power, handleFunc func(bool)) { | ||
|
|
@@ -147,3 +156,114 @@ func (m *Manager) setLidClosed(closed bool) error { | |
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *Manager) shouldInitializeLidExternal(builtin *Monitor) bool { | ||
| m.sysConfig.mu.Lock() | ||
| displayMode := m.sysConfig.Config.DisplayMode | ||
| m.sysConfig.mu.Unlock() | ||
| if displayMode == DisplayModeExtend { | ||
| return true | ||
| } | ||
| if displayMode != DisplayModeOnlyOne { | ||
| return false | ||
| } | ||
|
|
||
| var physicalMonitors Monitors | ||
| for _, monitor := range m.cloneMonitorMap() { | ||
| if monitor.realConnected { | ||
| physicalMonitors = append(physicalMonitors, monitor) | ||
| } | ||
| } | ||
| screenCfg := m.getSysScreenConfig(physicalMonitors.getMonitorsId()) | ||
| return screenCfg.OnlyOneUuid == "" || screenCfg.OnlyOneUuid == builtin.uuid | ||
| } | ||
|
|
||
| func (m *Manager) lidDisplayResetPending() bool { | ||
| m.lidScaleMu.Lock() | ||
| defer m.lidScaleMu.Unlock() | ||
| return m.lidScalePending | ||
| } | ||
|
|
||
| // 显示配置成功应用后再调整缩放,避免失败或中间状态污染当前配置。 | ||
| func (m *Manager) applyLidScale(monitorMap map[uint32]*Monitor) { | ||
| if m.xsManager == nil { | ||
| return | ||
| } | ||
| builtin := m.getBuiltinMonitor() | ||
| if builtin == nil { | ||
| return | ||
| } | ||
| builtin = monitorMap[builtin.ID] | ||
| if builtin == nil { | ||
| return | ||
| } | ||
| m.lidScaleMu.Lock() | ||
| defer m.lidScaleMu.Unlock() | ||
| m.userCfgMu.Lock() | ||
| defer m.userCfgMu.Unlock() | ||
| state := m.userConfig.LidScale | ||
| if !builtin.lidClosed && builtin.Enabled && state != nil && state.ExternalUUID != "" { | ||
| // 切回内屏前保存外屏最后使用的值,不能下次合盖又重置为默认值。 | ||
| factors, err := m.xsManager.GetScreenScaleFactors(0) | ||
| if err != nil { | ||
| logger.Warning("failed to read external scale before lid open:", err) | ||
| return | ||
| } | ||
| state.Factors[state.ExternalUUID] = factors | ||
| if err := m.xsManager.SetScreenScaleFactors(0, state.Factors[builtin.uuid]); err != nil { | ||
| logger.Warning("failed to restore builtin scale after lid open:", err) | ||
| return | ||
| } | ||
| state.ExternalUUID = "" | ||
| if err := m.saveUserConfigNoLock(); err != nil { | ||
|
Comment on lines
+217
to
+218
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Saved external scale gets overwritten When saving user configuration fails after restoring builtin scaling on lid open, Handle a failed config save so later applies cannot treat the restored builtin scale as the external scale. Prompt for AI agentsIn `display1/manager_lid.go` at lines 217-218:
**Saved external scale gets overwritten**
When saving user configuration fails after restoring builtin scaling on lid open, `applyLidScale` clears `ExternalUUID` in memory, but the saved config retains the old external UUID. A later lid-open apply treats the builtin factors as the external scale and overwrites the saved external factors, so the next lid close restores the wrong scale.
Handle a failed config save so later applies cannot treat the restored builtin scale as the external scale. |
||
| logger.Warning("failed to save lid scale config:", err) | ||
| } | ||
| } | ||
| if !m.lidScalePending || !builtin.lidClosed || builtin.Enabled { | ||
| return | ||
| } | ||
| var external *Monitor | ||
| for _, monitor := range monitorMap { | ||
| if monitor.ID != builtin.ID && monitor.realConnected && monitor.Enabled { | ||
| if external != nil { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Automatic scaling stops for multiple displays When the lid closes with multiple enabled external displays and a resolution-change event occurs before it opens, Clear Also at Prompt for AI agentsIn `display1/manager_lid.go` at line 228:
**Automatic scaling stops for multiple displays**
When the lid closes with multiple enabled external displays and a resolution-change event occurs before it opens, `applyLidScale` returns without clearing `lidScalePending`, so `tryToChangeScaleFactor` skips automatic scale adjustments while the lid remains closed. External displays keep an unsuitable scale.
Clear `lidScalePending` before returning for multiple enabled external displays.
Also at `display1/manager_lid.go:229-230`, `display1/manager.go:3891-3893`. |
||
| return // 多外屏组合沿用原有逻辑,不擅自套用单外屏的缩放。 | ||
| } | ||
| external = monitor | ||
| } | ||
| } | ||
| if external == nil { | ||
| return | ||
| } | ||
| if state != nil && state.ExternalUUID == external.uuid { | ||
| // 合盖期间重启服务时,当前值已经属于外屏,不能拿它覆盖内屏记录。 | ||
| m.lidScalePending = false | ||
| return | ||
| } | ||
| factors, err := m.xsManager.GetScreenScaleFactors(0) | ||
| if err != nil { | ||
| logger.Warning("failed to read builtin scale before lid close:", err) | ||
| return | ||
| } | ||
| if state == nil { | ||
| state = &LidScaleConfig{Factors: make(map[string]map[string]float64)} | ||
| m.userConfig.LidScale = state | ||
| } | ||
| state.Factors[builtin.uuid] = factors | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Lid transition crashes the daemon When the user config contains a Initialize Also at Prompt for AI agentsIn `display1/manager_lid.go` at line 251:
**Lid transition crashes the daemon**
When the user config contains a `LidScale` object with `Factors` omitted or null, `applyLidScale` assigns into `state.Factors` while the map is nil, causing a panic during the lid transition and terminating the daemon.
Initialize `state.Factors` when loading or creating a `LidScaleConfig` if it is nil.
Also at `display1/manager_lid.go:212`. |
||
| target := state.Factors[external.uuid] | ||
| if len(target) == 0 { | ||
| // 仅首次使用该外屏时计算默认值,之后优先恢复用户保存的值。 | ||
| target = map[string]float64{"ALL": scale.GetRecommendedScaleFactor(m.xConn)} | ||
| } | ||
| state.ExternalUUID = external.uuid | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · External display keeps wrong scale When the daemon stops after saving Only treat Also at Prompt for AI agentsIn `display1/manager_lid.go` at line 257:
**External display keeps wrong scale**
When the daemon stops after saving `ExternalUUID` but before applying the external scale, or `SetScreenScaleFactors` fails, `applyLidScale` saves `ExternalUUID` before the scale change succeeds, so a later call takes the matching-UUID branch, clears `lidScalePending`, and skips the retry. The external display keeps the wrong scale.
Only treat `ExternalUUID` as proof that the external scale is active after the scale change succeeds.
Also at `display1/manager_lid.go:212`, `display1/manager_lid.go:237-240`, `display1/manager_lid.go:251`, `display1/manager_lid.go:263`. |
||
| if err := m.saveUserConfigNoLock(); err != nil { | ||
| state.ExternalUUID = "" | ||
| logger.Warning("failed to save builtin scale before lid close:", err) | ||
| return | ||
| } | ||
| if err := m.xsManager.SetScreenScaleFactors(0, target); err != nil { | ||
| logger.Warning("failed to initialize external scale after lid close:", err) | ||
| return | ||
| } | ||
| m.lidScalePending = false | ||
| logger.Infof("applied external scale after lid close: %v", target) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Wrong display scale overwrites saved settings
When the external display named by
state.ExternalUUIDis disconnected while the lid is closed before the lid is reopened,applyLidScalestores the current screen factors understate.ExternalUUIDwithout checking that this display is still connected and active. If the external display is unplugged while the lid is closed, reopening captures the builtin display’s factors and overwrites the unplugged display’s saved scale; reconnecting it later restores the wrong scale.Before saving current factors under
ExternalUUID, verify that the corresponding external display is active; otherwise preserve its saved factors and only restore the builtin scale.Prompt for AI agents