chore: Add basic config creation and reading

This commit is contained in:
Tera << 8 2025-06-22 12:28:00 -04:00
parent a88b75a4bc
commit d0a4d26082
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
9 changed files with 263 additions and 2 deletions

2
.gitignore vendored
View file

@ -28,3 +28,5 @@ go.work.sum
# ---> UnrealXR # ---> UnrealXR
# development dirs # development dirs
data data
# artifacts
app/app

80
app/config.go Normal file
View file

@ -0,0 +1,80 @@
package main
import _ "embed"
//go:embed default_config.yml
var InitialConfig []byte
type DisplayConfig struct {
Angle *int `yaml:"angle"`
FOV *int `yaml:"fov"`
Spacing *int `yaml:"spacing"`
Count *int `yaml:"count"`
}
type AppOverrides struct {
AllowUnsupportedDevices *bool `yaml:"allow_unsupported_devices"`
OverrideWidth *int `yaml:"width"`
OverrideHeight *int `yaml:"height"`
OverrideRefreshRate *int `yaml:"refresh_rate"`
}
type Config struct {
DisplayConfig DisplayConfig `yaml:"display"`
Overrides AppOverrides `yaml:"overrides"`
}
func getPtrToInt(int int) *int {
return &int
}
func getPtrToBool(bool bool) *bool {
return &bool
}
var DefaultConfig = &Config{
DisplayConfig: DisplayConfig{
Angle: getPtrToInt(45),
FOV: getPtrToInt(45),
Spacing: getPtrToInt(1),
Count: getPtrToInt(3),
},
Overrides: AppOverrides{
AllowUnsupportedDevices: getPtrToBool(false),
},
}
func InitializePotentiallyMissingConfigValues(config *Config) {
// TODO: is there a better way to do this?
if config.DisplayConfig.Angle == nil {
config.DisplayConfig.Angle = DefaultConfig.DisplayConfig.Angle
}
if config.DisplayConfig.FOV == nil {
config.DisplayConfig.FOV = DefaultConfig.DisplayConfig.FOV
}
if config.DisplayConfig.Spacing == nil {
config.DisplayConfig.Spacing = DefaultConfig.DisplayConfig.Spacing
}
if config.DisplayConfig.Count == nil {
config.DisplayConfig.Count = DefaultConfig.DisplayConfig.Count
}
if config.Overrides.AllowUnsupportedDevices == nil {
config.Overrides.AllowUnsupportedDevices = DefaultConfig.Overrides.AllowUnsupportedDevices
}
if config.Overrides.OverrideWidth == nil {
config.Overrides.OverrideWidth = DefaultConfig.Overrides.OverrideWidth
}
if config.Overrides.OverrideHeight == nil {
config.Overrides.OverrideHeight = DefaultConfig.Overrides.OverrideHeight
}
if config.Overrides.OverrideRefreshRate == nil {
config.Overrides.OverrideRefreshRate = DefaultConfig.Overrides.OverrideRefreshRate
}
}

18
app/default_config.yml Normal file
View file

@ -0,0 +1,18 @@
# __ __ ___ __ ____
# / / / /___ ________ ____ _/ / |/ // __ \
# / / / / __ \/ ___/ _ \/ __ `/ /| // /_/ /
# / /_/ / / / / / / __/ /_/ / // |/ _, _/
# \____/_/ /_/_/ \___/\__,_/_//_/|_/_/ |_|
#
# Welcome to UnrealXR! This is the configuration file to configure various UnrealXR settings.
display:
angle: 45 # Angle of the virtual displays
fov: 45 # FOV of the 3D camera
spacing: 1 # Spacing between virtual displays
count: 3 # Count of virtual displays
overrides:
allow_unsupported_devices: false # If true, allows unsupported devices to be used as long as they're a compatible vendor (Xreal)
# width: 1920 # If set, overrides the width of the screen and virtual displays
# height: 1080 # If set, overrides the height of the screen and virtual displays
# refresh_rate: 120 # If set, overrides the refresh rate of the screen and the maximum refresh rate of the virtual displays

95
app/main.go Normal file
View file

@ -0,0 +1,95 @@
package main
import (
"context"
"fmt"
"os"
"path"
"github.com/charmbracelet/log"
"github.com/goccy/go-yaml"
"github.com/kirsle/configdir"
"github.com/urfave/cli/v3"
)
func mainEntrypoint(context.Context, *cli.Command) error {
if os.Geteuid() != 0 {
return fmt.Errorf("this program must be run as root")
}
log.Info("Initializing UnrealXR")
// Allow for overriding the config directory
configDir := os.Getenv("UNREALXR_CONFIG_PATH")
if configDir == "" {
configDir = configdir.LocalConfig("unrealxr")
err := configdir.MakePath(configDir)
if err != nil {
return fmt.Errorf("failed to ensure config directory exists: %w", err)
}
}
_, err := os.Stat(path.Join(configDir, "config.yml"))
if err != nil {
log.Debug("Creating default config file")
err := os.WriteFile(path.Join(configDir, "config.yml"), InitialConfig, 0644)
if err != nil {
return fmt.Errorf("failed to create initial config file: %w", err)
}
}
// Read and parse the config file
configBytes, err := os.ReadFile(path.Join(configDir, "config.yml"))
if err != nil {
return fmt.Errorf("failed to read config file: %w", err)
}
config := &Config{}
err = yaml.Unmarshal(configBytes, config)
if err != nil {
return fmt.Errorf("failed to parse config file: %w", err)
}
InitializePotentiallyMissingConfigValues(config)
return nil
}
func main() {
logLevel := os.Getenv("UNREALXR_LOG_LEVEL")
if logLevel != "" {
switch logLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
}
}
// Initialize the CLI
cmd := &cli.Command{
Name: "unrealxr",
Usage: "A spatial multi-display renderer for XR devices",
Action: mainEntrypoint,
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}

1
ardriver/ardriver.go Normal file
View file

@ -0,0 +1 @@
package ardriver

25
go.mod Normal file
View file

@ -0,0 +1,25 @@
module git.terah.dev/UnrealXR/unrealxr
go 1.24.3
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
github.com/charmbracelet/lipgloss v1.1.0 // indirect
github.com/charmbracelet/log v0.4.2 // indirect
github.com/charmbracelet/x/ansi v0.8.0 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/goccy/go-yaml v1.18.0 // indirect
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/urfave/cli/v3 v3.3.8 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.30.0 // indirect
)

40
go.sum Normal file
View file

@ -0,0 +1,40 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
github.com/charmbracelet/log v0.4.2 h1:hYt8Qj6a8yLnvR+h7MwsJv/XvmBJXiueUcI3cIxsyig=
github.com/charmbracelet/log v0.4.2/go.mod h1:qifHGX/tc7eluv2R6pWIpyHDDrrb/AG71Pf2ysQu5nw=
github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE=
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/urfave/cli/v3 v3.3.8 h1:BzolUExliMdet9NlJ/u4m5vHSotJ3PzEqSAZ1oPMa/E=
github.com/urfave/cli/v3 v3.3.8/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

View file

@ -1 +1 @@
sudo LD_LIBRARY_PATH="$LD_LIBRARY_PATH" UNREALXR_CONFIG_PATH="$UNREALXR_CONFIG_PATH" UNREALXR_DATA_PATH="$UNREALXR_DATA_PATH" WAYLAND_DISPLAY="$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" XDG_RUNTIME_DIR="/user/run/0" python3 main.py sudo UNREALXR_LOG_LEVEL="$UNREALXR_LOG_LEVEL" LD_LIBRARY_PATH="$LD_LIBRARY_PATH" UNREALXR_CONFIG_PATH="$UNREALXR_CONFIG_PATH" UNREALXR_DATA_PATH="$UNREALXR_DATA_PATH" WAYLAND_DISPLAY="$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" XDG_RUNTIME_DIR="/user/run/0" ./app/app