Skip to content

Commit d06f5f3

Browse files
authored
feat: 色温调节支持用户自定义 (#285)
色温调节支持用户自定义 Log: 色温调节支持用户自定义
1 parent 97e05e0 commit d06f5f3

File tree

6 files changed

+327
-11
lines changed

6 files changed

+327
-11
lines changed

display/color_temp.go

Lines changed: 181 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"bufio"
99
"bytes"
1010
"errors"
11+
configManager "github.com/linuxdeepin/go-dbus-factory/org.desktopspec.ConfigManager"
1112
"math"
1213
"os"
1314
"os/exec"
15+
"regexp"
1416
"strconv"
1517
"strings"
1618
"sync"
@@ -28,20 +30,28 @@ const (
2830
ColorTemperatureModeAuto
2931
// ColorTemperatureModeManual 手动调整色温
3032
ColorTemperatureModeManual
33+
ColorTemperatureModeCustom
3134
)
3235

3336
const (
3437
timeZoneFile = "/usr/share/zoneinfo/zone1970.tab"
38+
39+
defaultAutoColorTemperatureConf = "6500:3500"
40+
defaultTemperature = 6500
3541
)
3642

3743
func isValidColorTempMode(mode int32) bool {
38-
return mode >= ColorTemperatureModeNone && mode <= ColorTemperatureModeManual
44+
return mode >= ColorTemperatureModeNone && mode <= ColorTemperatureModeCustom
3945
}
4046

4147
// dbus 上导出的方法
4248
func (m *Manager) setColorTempMode(mode int32) error {
4349
if !isValidColorTempMode(mode) {
44-
return errors.New("mode out of range, not 0 or 1 or 2")
50+
return errors.New("mode out of range, not 0 or 1 or 3")
51+
}
52+
53+
if mode != ColorTemperatureModeNone {
54+
m.setCustomColorTempModeOn(mode)
4555
}
4656
m.setPropColorTemperatureMode(mode)
4757
m.setPropColorTemperatureEnabled(mode != 0)
@@ -57,11 +67,15 @@ func (m *Manager) setColorTempModeReal(mode int32) {
5767
switch mode {
5868
case ColorTemperatureModeAuto: // 自动模式调节色温 启动服务
5969
m.redshiftRunner.start()
60-
70+
m.stopCustomColorTempMode()
6171
case ColorTemperatureModeManual, ColorTemperatureModeNone:
6272
// manual 手动调节色温
6373
// none 恢复正常色温
6474
m.redshiftRunner.stop()
75+
m.stopCustomColorTempMode()
76+
case ColorTemperatureModeCustom:
77+
m.redshiftRunner.stop()
78+
m.listenCustomColorTempTime()
6579
}
6680
// 对于自动模式,也要先把色温设置为正常。
6781
m.setColorTempOneShot()
@@ -156,12 +170,19 @@ func (r *redshiftRunner) start() {
156170
return
157171
}
158172
r.state = redshiftStateRunning
159-
173+
var colorConf string
174+
val, err := getGlobalDconfValue(DSettingsAppID, DSettingsDisplayName, "", DSettingsKeyAutoColorTemperature)
175+
if err != nil {
176+
colorConf = defaultAutoColorTemperatureConf
177+
logger.Warning(err)
178+
} else {
179+
colorConf = val.(string)
180+
}
160181
latitude := r.zoneInfoMap[_timeZone].latitude
161182
longitude := r.zoneInfoMap[_timeZone].longitude
162183
geographicalPosition := strconv.FormatFloat(latitude, 'f', -1, 64) + ":" + strconv.FormatFloat(longitude, 'f', -1, 64)
163184
logger.Info("Get geographicalPosition:", geographicalPosition)
164-
cmd := exec.Command("redshift", "-m", "dummy", "-t", "6500:3500", "-r")
185+
cmd := exec.Command("redshift", "-m", "dummy", "-t", colorConf, "-r")
165186
if geographicalPosition != "" {
166187
cmd.Args = append(cmd.Args, "-l", geographicalPosition)
167188
}
@@ -372,6 +393,12 @@ func (m *Manager) getColorTemperatureValue() int {
372393
return int(manual)
373394
case ColorTemperatureModeAuto:
374395
return m.redshiftRunner.getValue()
396+
case ColorTemperatureModeCustom:
397+
value := defaultTemperature
398+
if m.customColorTempFlag {
399+
value = int(manual)
400+
}
401+
return value
375402
}
376403

377404
return defaultTemperatureManual
@@ -484,3 +511,152 @@ func (r *redshiftRunner) registerGeoClueAgent() error {
484511
r.geoAgentRegistered = true
485512
return nil
486513
}
514+
515+
func setGlobalDconfValue(appID string, name string, subPath string, key string, value dbus.Variant) error {
516+
sysBus, err := dbus.SystemBus()
517+
if err != nil {
518+
logger.Warning(err)
519+
return err
520+
}
521+
ds := configManager.NewConfigManager(sysBus)
522+
managerPath, err := ds.AcquireManager(0, appID, name, subPath)
523+
if err != nil {
524+
logger.Warning(err)
525+
return err
526+
}
527+
528+
dsManager, err := configManager.NewManager(sysBus, managerPath)
529+
if err != nil {
530+
logger.Warning(err)
531+
return err
532+
}
533+
534+
err = dsManager.SetValue(0, key, value)
535+
if err != nil {
536+
logger.Warning(err)
537+
return err
538+
}
539+
return nil
540+
}
541+
542+
func (m *Manager) setCustomColorTempTimePeriod(timePeriod string) error {
543+
pattern := `^(?:[01]\d|2[0-3]):[0-5]\d-(?:[01]\d|2[0-3]):[0-5]\d$`
544+
re := regexp.MustCompile(pattern)
545+
var err error
546+
if re.MatchString(timePeriod) {
547+
m.CustomColorTempTimePeriod = timePeriod
548+
err = setGlobalDconfValue(DSettingsAppID, DSettingsDisplayName, "", DSettingsKeyCustomModeTime, dbus.MakeVariant(timePeriod))
549+
} else {
550+
err = errors.New("The timeperiod parameter is invalid")
551+
}
552+
return err
553+
}
554+
555+
func (m *Manager) setCustomColorTempModeOn(modeOn int32) error {
556+
var err error
557+
err = setGlobalDconfValue(DSettingsAppID, DSettingsDisplayName, "", DSettingKeyColorTemperatureModeOn, dbus.MakeVariant(modeOn))
558+
if err != nil {
559+
logger.Warning("try set modeOn failed")
560+
}
561+
562+
return err
563+
}
564+
565+
func (m *Manager) listenCustomColorTempTime() {
566+
if m.customColorTempTimer == nil {
567+
m.customColorTempTimer = time.NewTimer(5 * time.Second)
568+
} else {
569+
m.customColorTempTimer.Reset(5 * time.Second)
570+
}
571+
go func() {
572+
for {
573+
select {
574+
case <-m.customColorTempTimer.C:
575+
if m.ColorTemperatureMode == ColorTemperatureModeCustom {
576+
m.customColorTempFlag = m.checkCustomModeTime()
577+
m.setColorTempOneShot()
578+
}
579+
}
580+
if m.customColorTempTimer == nil {
581+
m.customColorTempTimer = time.NewTimer(5 * time.Second)
582+
} else {
583+
m.customColorTempTimer.Reset(5 * time.Second)
584+
}
585+
}
586+
}()
587+
}
588+
589+
func (m *Manager) checkCustomModeTime() bool {
590+
parts := strings.Split(m.CustomColorTempTimePeriod, "-")
591+
if len(parts) == 2 {
592+
// 获取当前日期
593+
timeLocation, err := time.LoadLocation(_timeZone)
594+
if err != nil {
595+
logger.Warning(err)
596+
return false
597+
}
598+
currentTime := time.Now().In(timeLocation)
599+
year, month, day := currentTime.Date()
600+
601+
// 构建目标时间
602+
targetTimeLayout := "15:04"
603+
targetTimeStart, err := time.Parse(targetTimeLayout, parts[0])
604+
if err != nil {
605+
logger.Warning("Failed to get start time:", err)
606+
return false
607+
}
608+
targetTimeEnd, err := time.Parse(targetTimeLayout, parts[1])
609+
if err != nil {
610+
logger.Warning("Failed to get start time:", err)
611+
return false
612+
}
613+
// 需要考虑凌晨,因为不是同一天
614+
targetTimeStart = time.Date(year, month, day, targetTimeStart.Hour(), targetTimeStart.Minute(), 0, 0, currentTime.Location())
615+
targetTimeEnd = time.Date(year, month, day, targetTimeEnd.Hour(), targetTimeEnd.Minute(), 0, 0, currentTime.Location())
616+
// 计算时间间隔
617+
currentToStart := targetTimeStart.Sub(currentTime)
618+
currentToEnd := targetTimeEnd.Sub(currentTime)
619+
startToEnd := targetTimeEnd.Sub(targetTimeStart)
620+
// 如果设定的时间段小于0,表示结束时间为第二天
621+
if startToEnd < 0 {
622+
return (currentToStart < 0 && currentToEnd < 0) || (currentToStart > 0 && currentToEnd > 0)
623+
} else {
624+
return (currentToStart < 0) && (currentToEnd > 0)
625+
}
626+
}
627+
return false
628+
}
629+
630+
func (m *Manager) stopCustomColorTempMode() {
631+
if m.customColorTempTimer != nil {
632+
m.customColorTempTimer.Stop()
633+
m.customColorTempTimer = nil
634+
}
635+
}
636+
637+
func getGlobalDconfValue(appID string, name string, subPath string, key string) (interface{}, error) {
638+
sysBus, err := dbus.SystemBus()
639+
if err != nil {
640+
logger.Warning(err)
641+
return nil, err
642+
}
643+
ds := configManager.NewConfigManager(sysBus)
644+
managerPath, err := ds.AcquireManager(0, appID, name, subPath)
645+
if err != nil {
646+
logger.Warning(err)
647+
return nil, err
648+
}
649+
650+
dsManager, err := configManager.NewManager(sysBus, managerPath)
651+
if err != nil {
652+
logger.Warning(err)
653+
return nil, err
654+
}
655+
656+
val, err := dsManager.Value(0, key)
657+
if err != nil {
658+
logger.Warning(err)
659+
return nil, err
660+
}
661+
return val.Value(), nil
662+
}

display/display.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ func Start(service *dbusutil.Service) error {
121121
}
122122
mode := ColorTemperatureModeNone
123123
if value {
124-
mode = cfg.ColorTemperatureModeOn
124+
mode = m.colorTemperatureModeOn
125+
} else {
126+
m.colorTemperatureModeOn = m.ColorTemperatureMode
125127
}
126128
err = m.setColorTempMode(mode)
127129
return dbusutil.ToError(err)

display/exported_methods_auto.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)