neko/internal/config/desktop.go

52 lines
1.1 KiB
Go
Raw Normal View History

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 {
2021-03-17 03:24:58 +13:00
cmd.PersistentFlags().String("desktop.screen", "1280x720@30", "default screen size and framerate")
if err := viper.BindPFlag("desktop.screen", cmd.PersistentFlags().Lookup("desktop.screen")); err != nil {
2020-11-04 12:27:47 +13:00
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})`)
2021-03-17 03:24:58 +13:00
res := r.FindStringSubmatch(viper.GetString("desktop.screen"))
2020-11-04 12:27:47 +13:00
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
}
}
}