2020-11-04 12:27:47 +13:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-03-12 05:44:49 +13:00
|
|
|
"os"
|
2020-11-04 12:27:47 +13:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Desktop struct {
|
2021-03-12 05:44:49 +13:00
|
|
|
Display string
|
|
|
|
|
2020-11-04 12:27:47 +13:00
|
|
|
ScreenWidth int
|
|
|
|
ScreenHeight int
|
2021-01-16 05:30:19 +13:00
|
|
|
ScreenRate int16
|
2020-11-04 12:27:47 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (Desktop) Init(cmd *cobra.Command) error {
|
|
|
|
cmd.PersistentFlags().String("screen", "1280x720@30", "default screen size and framerate")
|
|
|
|
if err := viper.BindPFlag("screen", cmd.PersistentFlags().Lookup("screen")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Desktop) Set() {
|
2021-03-12 05:44:49 +13:00
|
|
|
// Display is provided by env variable
|
|
|
|
s.Display = os.Getenv("DISPLAY")
|
|
|
|
|
2020-11-04 12:27:47 +13:00
|
|
|
s.ScreenWidth = 1280
|
|
|
|
s.ScreenHeight = 720
|
|
|
|
s.ScreenRate = 30
|
|
|
|
|
|
|
|
r := regexp.MustCompile(`([0-9]{1,4})x([0-9]{1,4})@([0-9]{1,3})`)
|
|
|
|
res := r.FindStringSubmatch(viper.GetString("screen"))
|
|
|
|
|
|
|
|
if len(res) > 0 {
|
|
|
|
width, err1 := strconv.ParseInt(res[1], 10, 64)
|
|
|
|
height, err2 := strconv.ParseInt(res[2], 10, 64)
|
|
|
|
rate, err3 := strconv.ParseInt(res[3], 10, 64)
|
|
|
|
|
|
|
|
if err1 == nil && err2 == nil && err3 == nil {
|
|
|
|
s.ScreenWidth = int(width)
|
|
|
|
s.ScreenHeight = int(height)
|
2021-01-16 05:30:19 +13:00
|
|
|
s.ScreenRate = int16(rate)
|
2020-11-04 12:27:47 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|