142 lines
4.1 KiB
Go
142 lines
4.1 KiB
Go
package vmstart
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/atomicfile"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
)
|
|
|
|
const vmDisplayConfigFile = ".reims-go-display.json"
|
|
|
|
type VMDisplayConfig struct {
|
|
TargetDisplay string `json:"target_display,omitempty"`
|
|
AutoResolution bool `json:"auto_resolution,omitempty"`
|
|
}
|
|
|
|
type primaryDisplayOnlyKey struct{}
|
|
|
|
func WithPrimaryDisplayOnly(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, primaryDisplayOnlyKey{}, true)
|
|
}
|
|
|
|
func PrimaryDisplayOnly(ctx context.Context) bool {
|
|
only, _ := ctx.Value(primaryDisplayOnlyKey{}).(bool)
|
|
return only
|
|
}
|
|
|
|
func SupportsAutoResolution(graphics string) bool {
|
|
return !strings.EqualFold(strings.TrimSpace(graphics), "vmware-svga")
|
|
}
|
|
|
|
func NormalizeTargetDisplay(raw string) string {
|
|
target := strings.TrimSpace(raw)
|
|
if strings.EqualFold(target, "auto") || strings.EqualFold(target, "primary") {
|
|
return ""
|
|
}
|
|
return target
|
|
}
|
|
|
|
func ReadVMDisplay(vmDir string) VMDisplayConfig {
|
|
if strings.TrimSpace(vmDir) == "" {
|
|
return VMDisplayConfig{}
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(vmDir, vmDisplayConfigFile))
|
|
var cfg VMDisplayConfig
|
|
if err != nil || json.Unmarshal(data, &cfg) != nil {
|
|
return VMDisplayConfig{}
|
|
}
|
|
cfg.TargetDisplay = NormalizeTargetDisplay(cfg.TargetDisplay)
|
|
return cfg
|
|
}
|
|
|
|
func SaveVMDisplay(vmDir string, cfg VMDisplayConfig) (VMDisplayConfig, error) {
|
|
if strings.TrimSpace(vmDir) == "" {
|
|
return VMDisplayConfig{}, errors.New("VM directory is empty")
|
|
}
|
|
cfg.TargetDisplay = NormalizeTargetDisplay(cfg.TargetDisplay)
|
|
dst := filepath.Join(vmDir, vmDisplayConfigFile)
|
|
if cfg.TargetDisplay == "" && !cfg.AutoResolution {
|
|
if err := os.Remove(dst); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return VMDisplayConfig{}, err
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
if err := atomicfile.WriteJSON(dst, cfg, 0o600); err != nil {
|
|
return VMDisplayConfig{}, err
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func SaveVMAutoResolution(vmDir string, enabled bool) (VMDisplayConfig, error) {
|
|
cfg := ReadVMDisplay(vmDir)
|
|
cfg.AutoResolution = enabled
|
|
return SaveVMDisplay(vmDir, cfg)
|
|
}
|
|
|
|
func ResolveAutoResolution(vmDir string, displays []host.DisplayInfo, borderless, fixedModeSupported bool) (resolution string, hidpi, enabled bool) {
|
|
cfg := ReadVMDisplay(vmDir)
|
|
if !cfg.AutoResolution {
|
|
return "", false, false
|
|
}
|
|
target, _ := ResolveTargetDisplay(cfg, displays)
|
|
resolution, hidpi = host.RecommendAutoResolution(target, borderless, fixedModeSupported)
|
|
return resolution, hidpi, true
|
|
}
|
|
|
|
func ResolveTargetDisplay(cfg VMDisplayConfig, displays []host.DisplayInfo) (target *host.DisplayInfo, fallback bool) {
|
|
if display, ok := host.FindDisplayByName(displays, NormalizeTargetDisplay(cfg.TargetDisplay)); ok {
|
|
return display, false
|
|
}
|
|
return host.PrimaryDisplayOf(displays), true
|
|
}
|
|
|
|
func DirectStartDisplays(displays []host.DisplayInfo, screens string) []host.DisplayInfo {
|
|
if target := directStartTargetDisplay(displays, screens); target != nil {
|
|
return []host.DisplayInfo{*target}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func directStartTargetDisplay(displays []host.DisplayInfo, screens string) *host.DisplayInfo {
|
|
var first uint64
|
|
for i, entry := range strings.Split(screens, ",") {
|
|
index, err := strconv.ParseUint(strings.TrimPrefix(strings.TrimSpace(entry), "+"), 10, strconv.IntSize)
|
|
if err != nil {
|
|
return host.PrimaryDisplayOf(displays)
|
|
}
|
|
if i == 0 {
|
|
first = index
|
|
}
|
|
}
|
|
if first < uint64(len(displays)) {
|
|
ordered := slices.Clone(displays)
|
|
slices.SortStableFunc(ordered, func(a, b host.DisplayInfo) int {
|
|
if a.Primary != b.Primary {
|
|
if a.Primary {
|
|
return -1
|
|
}
|
|
return 1
|
|
}
|
|
return cmp.Or(cmp.Compare(a.Top, b.Top), cmp.Compare(a.Left, b.Left))
|
|
})
|
|
return &ordered[first]
|
|
}
|
|
return host.PrimaryDisplayOf(displays)
|
|
}
|
|
|
|
// UsesFixedGuestMode keeps start and resume on the same display geometry.
|
|
func UsesFixedGuestMode(vmDir string, displays []host.DisplayInfo, borderless bool, resolution string) bool {
|
|
target, _ := ResolveTargetDisplay(ReadVMDisplay(vmDir), displays)
|
|
return host.ShouldUseFixedGuestMode(borderless, resolution, target)
|
|
}
|