Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions display1/displayconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ type SysCache struct {

// UserConfig v1
type UserConfig struct {
Version string
Screens map[string]UserScreenConfig
Version string
Screens map[string]UserScreenConfig
LidScale *LidScaleConfig `json:",omitempty"`
}

// 缩放仍由 XSettings 生效,这里只保存开合盖切换时各显示器的配置。
type LidScaleConfig struct {
Factors map[string]map[string]float64 // 显示器 UUID -> 原有缩放映射
ExternalUUID string `json:",omitempty"` // 尚未切回内屏的外屏
}

func (cfg *UserConfig) fix() {
Expand Down Expand Up @@ -382,6 +389,8 @@ type SysScreenConfig struct {
Single *SysMonitorModeConfig `json:",omitempty"`
OnlyOneMap map[string]*SysMonitorModeConfig `json:",omitempty"`
OnlyOneUuid string `json:",omitempty"`
// Single 已保存合盖时用户确认的配置,后续合盖应恢复而非初始化。
LidClosedConfigured bool `json:",omitempty"`
}

func isCurrentVersionUuid(uuid string) bool {
Expand All @@ -393,7 +402,7 @@ func (c *SysScreenConfig) updateUuid(monitors Monitors) (result *SysScreenConfig
return nil, false
}

result = &SysScreenConfig{}
result = &SysScreenConfig{LidClosedConfigured: c.LidClosedConfigured}

if c.Mirror != nil {
changed := false
Expand Down Expand Up @@ -478,10 +487,11 @@ func (c *SysScreenConfig) clone() *SysScreenConfig {
return nil
}
result := &SysScreenConfig{
Mirror: c.Mirror.clone(),
Extend: c.Extend.clone(),
Single: c.Single.clone(),
OnlyOneUuid: c.OnlyOneUuid,
Mirror: c.Mirror.clone(),
Extend: c.Extend.clone(),
Single: c.Single.clone(),
OnlyOneUuid: c.OnlyOneUuid,
LidClosedConfigured: c.LidClosedConfigured,
}
if len(c.OnlyOneMap) > 0 {
result.OnlyOneMap = make(map[string]*SysMonitorModeConfig, len(c.OnlyOneMap))
Expand Down
62 changes: 55 additions & 7 deletions display1/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ type Manager struct {
builtinMonitorMu sync.Mutex
candidateBuiltinMonitors []*Monitor // 候补的

lidScaleMu sync.Mutex
lidScalePending bool

monitorMap map[uint32]*Monitor
monitorMapMu sync.Mutex
mm monitorManager
Expand Down Expand Up @@ -983,8 +986,10 @@ func monitorsRemove(monitors []*Monitor, id uint32) []*Monitor {
}

func (m *Manager) buildConfigForSingle(monitor *Monitor) SysMonitorConfigs {
if cfg := m.getLidClosedSingleConfig(monitor); cfg != nil {
return SysMonitorConfigs{cfg}
if !m.lidDisplayResetPending() {
if cfg := m.getLidClosedSingleConfig(monitor); cfg != nil {
return SysMonitorConfigs{cfg}
}
}
cfg := monitor.toBasicSysConfig()
cfg.Enabled = true
Expand Down Expand Up @@ -1071,13 +1076,30 @@ func (m *Manager) applyDisplayConfig(mode byte, monitorsId monitorsId, monitorMa
// 单屏情况
screenCfg := m.getSysScreenConfig(monitorsId)

needSaveCfg := false

monitorConfigs := screenCfg.getSingleMonitorConfigs()
if len(monitorConfigs) == 0 {
// 没有单屏配置
needSaveCfg = true
needSaveCfg := len(monitorConfigs) == 0
resetForLid := m.lidDisplayResetPending() && (needSaveCfg || !screenCfg.LidClosedConfigured)
if needSaveCfg && resetForLid {
// 显示方案已被清空时,不能继续恢复该外屏上一轮的缩放缓存。
m.userCfgMu.Lock()
if state := m.userConfig.LidScale; state != nil {
delete(state.Factors, monitors[0].uuid)
if state.ExternalUUID == monitors[0].uuid {
state.ExternalUUID = ""
}
}
m.userCfgMu.Unlock()
}
if needSaveCfg || resetForLid {
// 合盖只临时使用默认显示参数,不覆盖已有 Single 方案及亮度。
previous := monitorConfigs.getByUuid(monitors[0].uuid)
monitorConfigs = m.buildConfigForSingle(monitors[0])
if resetForLid {
monitorConfigs[0].Brightness = monitors[0].Brightness
if previous != nil {
monitorConfigs[0].Brightness = previous.Brightness
}
}
}

// 应用配置
Expand All @@ -1088,6 +1110,7 @@ func (m *Manager) applyDisplayConfig(mode byte, monitorsId monitorsId, monitorMa
}
if needSaveCfg {
screenCfg.setSingleMonitorConfigs(monitorConfigs)
screenCfg.LidClosedConfigured = false
m.setSysScreenConfig(monitorsId, screenCfg)
err = m.saveSysConfig("single")
if err != nil {
Expand Down Expand Up @@ -2372,6 +2395,14 @@ func (m *Manager) save() (err error) {
}
if len(monitors) == 1 {
screenCfg.setSingleMonitorConfigs(configs)
if builtin := m.getBuiltinMonitor(); builtin != nil && builtin.ID != monitors[0].ID {
builtin.PropsMu.RLock()
closed := builtin.lidClosed
builtin.PropsMu.RUnlock()
if closed {
screenCfg.LidClosedConfigured = true
}
}
} else {
uuid := getOnlyOneMonitorUuid(m.DisplayMode, monitors)
screenCfg.setMonitorConfigs(m.DisplayMode, uuid, configs)
Expand Down Expand Up @@ -2683,6 +2714,8 @@ func (m *Manager) applySysMonitorConfigs(mode byte, monitorsId monitorsId, monit
logger.Warning(err)
}

m.applyLidScale(monitorMap)

return nil
}

Expand Down Expand Up @@ -3854,6 +3887,21 @@ func (m *Manager) tryToChangeScaleFactor(monitorWidth, monitorHeight uint16) {
if monitorWidth == 0 || monitorHeight == 0 || !m.dsAutoChangeScaleEnabled {
return
}
// 合盖初始化期间由最终显示配置统一设置缩放,避免中间分辨率事件覆盖结果。
if m.lidDisplayResetPending() {
return
}
if builtin := m.getBuiltinMonitor(); builtin != nil {
m.userCfgMu.Lock()
hasExternalScale := m.userConfig.LidScale != nil && m.userConfig.LidScale.ExternalUUID != ""
m.userCfgMu.Unlock()
builtin.PropsMu.RLock()
restoring := hasExternalScale && !builtin.lidClosed
builtin.PropsMu.RUnlock()
if restoring {
return
}
}

if m.xsManager == nil {
return
Expand Down
124 changes: 122 additions & 2 deletions display1/manager_lid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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

Copy link
Copy Markdown

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.ExternalUUID is disconnected while the lid is closed before the lid is reopened, applyLidScale stores the current screen factors under state.ExternalUUID without 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
In `display1/manager_lid.go` at line 212:

**Wrong display scale overwrites saved settings**

When the external display named by `state.ExternalUUID` is disconnected while the lid is closed before the lid is reopened, `applyLidScale` stores the current screen factors under `state.ExternalUUID` without 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.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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, 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.

Prompt for AI agents
In `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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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, 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.

Prompt for AI agents
In `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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · 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.

Prompt for AI agents
In `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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · 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.

Prompt for AI agents
In `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)
}
Loading