152 lines
4.0 KiB
Go
152 lines
4.0 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/hashicorp/hcl/v2/gohcl"
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
|
"github.com/hashicorp/hcl/v2/hclwrite"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Settings are the user-adjustable settings.
|
|
type Settings struct {
|
|
Path string `menu:"-"`
|
|
Display DisplaySettings `hcl:"display,block" icon:"md_monitor_speaker"`
|
|
Player PlayerSettings ` icon:"md_headphones"`
|
|
Spectrum SpectrumSettings `hcl:"spectrum,block" icon:"md_chart_line"`
|
|
System SystemSettings ` icon:"md_raspberry_pi"`
|
|
}
|
|
|
|
type DisplaySettings struct {
|
|
Contrast uint8 `hcl:"contrast" menu:"Display contrast" icon:"md_contrast_box"`
|
|
Sleep string `hcl:"sleep" menu:"Display sleep time" options:"5min,15min,hour,day" icon:"md_power"`
|
|
Brightness uint8 `hcl:"brightness" menu:"Display brightness" icon:"md_brightness_6"`
|
|
}
|
|
|
|
type PlayerSettings struct {
|
|
Pause bool `hcl:"pause" menu:"Pause" icon:"md_pause"`
|
|
ShowArt bool `hcl:"show_art" menu:"Show album art" icon:"md_image"`
|
|
}
|
|
|
|
type SpectrumSettings struct {
|
|
Waterfall bool `hcl:"waterfall" menu:"Use waterfall mode"`
|
|
Mono bool `hcl:"mono" menu:"Use mono"`
|
|
Hold uint8 `hcl:"hold" min:"0" max:"100"`
|
|
MinFrequency int `hcl:"min" min:"50" max:"22050" step:"50" menu:"Low pass filter"`
|
|
MaxFrequency int `hcl:"max" min:"50" max:"22050" step:"50" menu:"High pass filter"`
|
|
}
|
|
|
|
type SystemSettings struct {
|
|
LoadAverage string `label:"Load average"`
|
|
LocalTime string `label:"Local time"`
|
|
}
|
|
|
|
var DefaultSettings = Settings{
|
|
Display: DisplaySettings{
|
|
Contrast: 0x80,
|
|
Sleep: "hour",
|
|
Brightness: 0x7F,
|
|
},
|
|
Player: PlayerSettings{
|
|
ShowArt: true,
|
|
},
|
|
Spectrum: SpectrumSettings{
|
|
Hold: 10,
|
|
MinFrequency: 100,
|
|
MaxFrequency: 15000,
|
|
},
|
|
}
|
|
|
|
var DefaultUserSettingsPath = []string{
|
|
os.ExpandEnv("/etc/beatglow/settings.conf"),
|
|
os.ExpandEnv("${HOME}/.config/beatglow/settings.conf"),
|
|
os.ExpandEnv("${HOME}/.beatglow-settings.conf"),
|
|
}
|
|
|
|
func LoadUserSettings(name string) *Settings {
|
|
if name == "" {
|
|
for _, item := range DefaultUserSettingsPath {
|
|
if _, err := os.Stat(item); err == nil {
|
|
name = item
|
|
break
|
|
}
|
|
}
|
|
if name == "" {
|
|
logrus.Warn("user settings does not exist; using defaults")
|
|
return &DefaultSettings
|
|
}
|
|
}
|
|
|
|
log := logrus.WithField("path", name)
|
|
log.Info("loading user settings")
|
|
|
|
source, err := os.ReadFile(name)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
log.Warn("user settings do not exist; using defaults")
|
|
} else {
|
|
log.WithError(err).Warn("user settings contains errors; using defaults")
|
|
}
|
|
return &DefaultSettings
|
|
}
|
|
|
|
var (
|
|
settings = &Settings{Path: name}
|
|
ctx *hcl.EvalContext
|
|
file *hcl.File
|
|
diag hcl.Diagnostics
|
|
)
|
|
|
|
start := hcl.Pos{Line: 1, Column: 1}
|
|
if file, diag = hclsyntax.ParseConfig(source, name, start); diag.HasErrors() {
|
|
log.WithError(diag).Warn("user settings contains errors; using defaults")
|
|
return &DefaultSettings
|
|
}
|
|
|
|
if diag = gohcl.DecodeBody(file.Body, ctx, settings); diag.HasErrors() {
|
|
log.WithError(diag).Warn("user settings contains errors; using defaults")
|
|
return &DefaultSettings
|
|
}
|
|
|
|
return settings
|
|
}
|
|
|
|
func SaveUserSettings(name string, settings *Settings) error {
|
|
if name == "" {
|
|
name = DefaultUserSettingsPath[len(DefaultUserSettingsPath)-1]
|
|
}
|
|
|
|
logrus.WithField("path", name).Info("saving user settings")
|
|
|
|
file := hclwrite.NewEmptyFile()
|
|
root := file.Body()
|
|
gohcl.EncodeIntoBody(settings, root)
|
|
root.AppendUnstructuredTokens(hclwrite.Tokens{
|
|
&hclwrite.Token{
|
|
Type: hclsyntax.TokenComment,
|
|
Bytes: []byte(fmt.Sprintf("\n# BeatGlow user settings\n# Saved at %s\n", time.Now())),
|
|
SpacesBefore: 0,
|
|
},
|
|
})
|
|
//logrus.Println(string(file.Bytes()[:]))
|
|
|
|
_ = os.Rename(name, name+".bak")
|
|
f, err := os.Create(name)
|
|
if err != nil {
|
|
_ = os.Rename(name+".bak", name)
|
|
return err
|
|
}
|
|
|
|
if _, err = file.WriteTo(f); err != nil {
|
|
_ = f.Close()
|
|
_ = os.Rename(name+".bak", name)
|
|
return err
|
|
}
|
|
|
|
return f.Close()
|
|
}
|