Skip to content

Commit 439e32a

Browse files
committed
fix: skip screen off if already in dpms off state
1. Add a check at the beginning of the systemTurnOffScreen function to skip processing if the display is already in a DPMS off state 2. Introduce a new isDpmsOff helper function to read the state from / tmp/dpms-state and determine if the screen is already off 3. Adjust the ordering of suspend state constants for logical consistency, moving suspendStateLidClose before suspendStateFinish Log: Optimized power button logic to avoid redundant screen-off commands Influence: 1. Test pressing the power button when the screen is already off via DPMS; verify the screen state does not change and no redundant actions occur 2. Test pressing the power button when the screen is on; verify the screen turns off correctly 3. Test the behavior after waking the system from suspend (lid open); ensure the screen state is correctly reported and handled 4. Verify that the /tmp/dpms-state file is created and updated correctly on screen state changes 5. Test the interaction between the power button and other screen off mechanisms (like idle timeout) to ensure no conflicts fix: 如果屏幕已处于DPMS关闭状态则跳过屏幕关闭处理 1. 在systemTurnOffScreen函数开头添加检查,如果显示器已处于DPMS off状态则 跳过处理 2. 新增isDpmsOff辅助函数,通过读取/tmp/dpms-state文件来判断屏幕是否已 关闭 3. 调整挂起状态常量的顺序以保持逻辑一致性,将suspendStateLidClose移到 suspendStateFinish之前 Log: 优化电源键逻辑,避免重复发送屏幕关闭命令 Influence: 1. 测试在屏幕已通过DPMS关闭时按下电源键,验证屏幕状态不会改变且没有多余 操作 2. 测试在屏幕开启时按下电源键,验证屏幕能正确关闭 3. 测试从挂起状态唤醒(开盖)后的行为,确保屏幕状态能正确报告和处理 4. 验证/tmp/dpms-state文件在屏幕状态变化时能被正确创建和更新 5. 测试电源键与其他屏幕关闭机制(如空闲超时)的交互,确保没有冲突 PMS: BUG-364835
1 parent 40b43f9 commit 439e32a

4 files changed

Lines changed: 88 additions & 7 deletions

File tree

common/fileutil/fileutil.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
package fileutil
6+
7+
import (
8+
"fmt"
9+
"io"
10+
"os"
11+
12+
"golang.org/x/sys/unix"
13+
)
14+
15+
// SafeReadFile 安全读取文件,拒绝符号链接
16+
func SafeReadFile(filename string) ([]byte, error) {
17+
fi, err := os.Lstat(filename)
18+
if err != nil {
19+
return nil, err
20+
}
21+
if fi.Mode()&os.ModeSymlink != 0 {
22+
return nil, fmt.Errorf("file %q is a symlink, refusing to follow", filename)
23+
}
24+
if !fi.Mode().IsRegular() {
25+
return nil, fmt.Errorf("file %q is not a regular file", filename)
26+
}
27+
28+
f, err := os.OpenFile(filename, unix.O_RDONLY|unix.O_NOFOLLOW, 0)
29+
if err != nil {
30+
return nil, err
31+
}
32+
defer f.Close()
33+
return io.ReadAll(f)
34+
}
35+
36+
// SafeWriteFile 安全写入文件,拒绝符号链接
37+
func SafeWriteFile(filename string, content []byte, perm os.FileMode) error {
38+
fi, err := os.Lstat(filename)
39+
if err != nil && !os.IsNotExist(err) {
40+
return err
41+
}
42+
if err == nil {
43+
// 文件已存在,必须是普通文件
44+
if fi.Mode()&os.ModeSymlink != 0 {
45+
return fmt.Errorf("file %q is a symlink, refusing to write", filename)
46+
}
47+
if !fi.Mode().IsRegular() {
48+
return fmt.Errorf("file %q is not a regular file", filename)
49+
}
50+
f, err := os.OpenFile(filename, unix.O_WRONLY|unix.O_TRUNC|unix.O_NOFOLLOW, perm)
51+
if err != nil {
52+
return err
53+
}
54+
defer f.Close()
55+
_, err = f.Write(content)
56+
return err
57+
}
58+
// 文件不存在,安全创建
59+
f, err := os.OpenFile(filename, unix.O_WRONLY|unix.O_CREAT|unix.O_EXCL, perm)
60+
if err != nil {
61+
return err
62+
}
63+
defer f.Close()
64+
_, err = f.Write(content)
65+
return err
66+
}

keybinding1/utils.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
dbus "github.com/godbus/dbus/v5"
2121
"github.com/linuxdeepin/dde-daemon/keybinding1/constants"
2222
"github.com/linuxdeepin/dde-daemon/keybinding1/util"
23+
"github.com/linuxdeepin/dde-daemon/common/fileutil"
2324
wm "github.com/linuxdeepin/go-dbus-factory/session/com.deepin.wm"
2425

2526
gio "github.com/linuxdeepin/go-gir/gio-2.0"
@@ -31,10 +32,10 @@ import (
3132
const (
3233
suspendStateUnknown = iota + 1
3334
suspendStateLidOpen
35+
suspendStateLidClose
3436
suspendStateFinish
3537
suspendStateWakeup
3638
suspendStatePrepare
37-
suspendStateLidClose
3839
suspendStateButtonClick
3940
)
4041

@@ -287,6 +288,9 @@ func (m *Manager) systemShutdown() {
287288
}
288289

289290
func (m *Manager) systemTurnOffScreen() {
291+
if isDpmsOff() {
292+
return
293+
}
290294
logger.Info("DPMS Off")
291295
var err error
292296
var useWayland bool
@@ -327,7 +331,16 @@ func (m *Manager) systemTurnOffScreen() {
327331
m.setWmBlackScreenActive(false)
328332
}
329333
undoPrepareSuspend()
330-
os.WriteFile("/tmp/dpms-state", []byte("1"), 0644)
334+
fileutil.SafeWriteFile("/tmp/dpms-state", []byte("1"), 0600)
335+
}
336+
337+
func isDpmsOff() bool {
338+
content, err := fileutil.SafeReadFile("/tmp/dpms-state")
339+
if err != nil {
340+
logger.Debug("read dpms state error:", err)
341+
return false
342+
}
343+
return bytes.Equal(bytes.TrimSpace(content), []byte("1"))
331344
}
332345

333346
func (m *Manager) systemLogout() {

session/power1/power_save_plan.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"os/exec"
1414
"strings"
1515
"sync"
16+
17+
"github.com/linuxdeepin/dde-daemon/common/fileutil"
1618
"time"
1719

1820
"github.com/godbus/dbus/v5"
@@ -1059,15 +1061,15 @@ func (psp *powerSavePlan) startIdleTasksLocked() {
10591061
}
10601062

10611063
func (ps *powerSavePlan) restoreDpmsStateFile() {
1062-
v, err := os.ReadFile("/tmp/dpms-state")
1064+
v, err := fileutil.SafeReadFile("/tmp/dpms-state")
10631065
if err != nil {
10641066
return
10651067
}
10661068

10671069
if string(v) == "1" {
1068-
err = os.WriteFile("/tmp/dpms-state", []byte("0"), 0644)
1070+
err = fileutil.SafeWriteFile("/tmp/dpms-state", []byte("0"), 0600)
10691071
if err != nil {
1070-
logger.Warning("WriteFile /tmp/dpms-state:", err)
1072+
logger.Warning("write dpms state:", err)
10711073
}
10721074
}
10731075
}

session/power1/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package power
66

77
import (
88
"math"
9-
"os"
109
"os/exec"
1110
"strings"
1211
"time"
1312

1413
dbus "github.com/godbus/dbus/v5"
1514
"github.com/linuxdeepin/dde-api/soundutils"
15+
"github.com/linuxdeepin/dde-daemon/common/fileutil"
1616
. "github.com/linuxdeepin/go-lib/gettext"
1717
"github.com/linuxdeepin/go-lib/pulse"
1818
"github.com/linuxdeepin/go-x11-client/ext/dpms"
@@ -210,7 +210,7 @@ func (m *Manager) setDPMSModeOff() {
210210
} else {
211211
callSetScreenState(true)
212212
}
213-
os.WriteFile("/tmp/dpms-state", []byte("1"), 0644)
213+
fileutil.SafeWriteFile("/tmp/dpms-state", []byte("1"), 0600)
214214
}
215215

216216
const (

0 commit comments

Comments
 (0)