package host import ( "fmt" "math" "strings" ) // DisplayInfo describes a connected physical monitor on the host. type DisplayInfo struct { Name string `json:"name,omitempty"` FriendlyName string `json:"friendly_name,omitempty"` Width int `json:"width"` Height int `json:"height"` ScalePct int `json:"scale_pct"` // Windows DPI scaling percentage, e.g. 100, 125, 150, 200 RefreshHz int `json:"refresh_hz,omitempty"` Primary bool `json:"primary"` Left int `json:"left,omitempty"` Top int `json:"top,omitempty"` // WorkWidth and WorkHeight are the work area (the screen less the taskbar) in // pixels; 0 where unknown. WorkWidth int `json:"work_width,omitempty"` WorkHeight int `json:"work_height,omitempty"` } // DisplayPreset is a suggested macOS VM resolution option tailored to the host monitor. type DisplayPreset struct { Resolution string `json:"resolution"` Label string `json:"label"` // HiDPI: macOS starts this window size HiDPI on the primary screen (GuestStartsHiDPI). HiDPI bool `json:"hidpi"` Recommended bool `json:"recommended,omitempty"` } // PrimaryDisplayOf picks the primary monitor (or the first detected monitor). func PrimaryDisplayOf(displays []DisplayInfo) *DisplayInfo { for i := range displays { if displays[i].Primary { cp := displays[i] return &cp } } if len(displays) > 0 { cp := displays[0] return &cp } return nil } // ConnectedDisplays enumerates the current host monitors. func ConnectedDisplays() []DisplayInfo { return probeDisplays() } // FindDisplayByName matches a monitor device name or selects the primary display. func FindDisplayByName(displays []DisplayInfo, name string) (*DisplayInfo, bool) { if name == "" || strings.EqualFold(name, "auto") || strings.EqualFold(name, "primary") { return PrimaryDisplayOf(displays), true } for _, display := range displays { if strings.EqualFold(display.Name, name) { return &display, true } } return nil, false } // When an x86-64 macOS guest on reims-vgpu starts its desktop HiDPI. Upstream // steelbrain/reims-vgpu 3731afd, guest/kext/src/RgfxDisplayIdentity.hpp:33-41 // and 68-79, crates/reims-vgpu-window/features.md:138-150: // - the display has 110 pixels per inch a point at the host screen's scale; // - it starts HiDPI from a window of 2048 x 1536 pixels above 160 pixels per // inch, which is a screen at 150% or more (145% gives exactly 160); // - and from 3840 x 2160 pixels at any scale. // // On Windows the window's points are the screen's pixels // (crates/reims-vgpu-window/src/geometry.rs:68-73), so --resolution is the // window's size in pixels and a borderless window is the screen's. const ( guestPointsPerInch = 110 guestHiDPIMinPPI = 160 guestHiDPIMinWidth = 2048 guestHiDPIMinHeight = 1536 guestHiDPIAnyWidth = 3840 guestHiDPIAnyHeight = 2160 windowTitleBarPoints = 32 // a window's title bar and frame, in Windows points defaultTaskbarPoints = 48 // used where the work area is unknown maxDisplayPresetCount = 4 ) // guestPixelsPerInch mirrors rgfxDisplayPixelsPerInch for a screen at scalePct. func guestPixelsPerInch(scalePct int) int { if scalePct <= 0 { return guestPointsPerInch } scale120ths := (scalePct*120 + 50) / 100 return (guestPointsPerInch*scale120ths + 60) / 120 } // GuestStartsHiDPI reports whether macOS starts HiDPI in a reims-vgpu window of // w x h pixels on a host screen at scalePct. func GuestStartsHiDPI(w, h, scalePct int) bool { if w >= guestHiDPIAnyWidth && h >= guestHiDPIAnyHeight { return true } return guestPixelsPerInch(scalePct) > guestHiDPIMinPPI && w >= guestHiDPIMinWidth && h >= guestHiDPIMinHeight } // FullscreenStartsHiDPI reports whether a borderless window, which fills the // screen, starts macOS HiDPI on d. func FullscreenStartsHiDPI(d *DisplayInfo) bool { if d == nil || d.Width <= 0 || d.Height <= 0 { return false } return GuestStartsHiDPI(d.Width, d.Height, d.ScalePct) } // What HiDPIAdvice returns. const ( HiDPIAdviceFullscreen = "fullscreen" // a borderless window is HiDPI HiDPIAdviceScale = "scale" // the screen would be at 150% Windows scaling HiDPIAdviceUnavailable = "unavailable" // too few pixels at any scale ) // HiDPIAdvice says how to get a HiDPI desktop on d when none of presets is // one, and "" when one is or nothing is known about d. func HiDPIAdvice(d *DisplayInfo, presets []DisplayPreset) string { if d == nil || d.Width <= 0 || d.Height <= 0 { return "" } for _, p := range presets { if p.HiDPI { return "" } } switch { case FullscreenStartsHiDPI(d): return HiDPIAdviceFullscreen case d.ScalePct < 150 && GuestStartsHiDPI(d.Width, d.Height, 150): return HiDPIAdviceScale default: return HiDPIAdviceUnavailable } } var ( presetSizes16x9 = [][2]int{{1280, 720}, {1600, 900}, {1920, 1080}, {2560, 1440}, {2880, 1620}, {3200, 1800}, {3840, 2160}} presetSizes16x10 = [][2]int{{1280, 800}, {1680, 1050}, {1920, 1200}, {2560, 1600}, {2880, 1800}, {3840, 2400}} ) // RecommendDisplayPresets lists window sizes that fit the primary screen's // work area below a title bar, largest first, each marked HiDPI where macOS // starts it so there. The recommended one is the largest HiDPI size, else the // largest size: text on a 1x macOS desktop is visibly rough, as macOS has no // subpixel antialiasing. func RecommendDisplayPresets(primary *DisplayInfo) ([]DisplayPreset, string) { if primary == nil || primary.Width <= 0 || primary.Height <= 0 { // Nothing known about the screen: no size can be called HiDPI. return []DisplayPreset{ {Resolution: "1920x1080", Label: "1920 × 1080", Recommended: true}, {Resolution: "2560x1440", Label: "2560 × 1440"}, {Resolution: "2880x1800", Label: "2880 × 1800"}, }, "1920x1080" } scale := primary.ScalePct if scale <= 0 { scale = 100 } usableW, usableH := primary.WorkWidth, primary.WorkHeight if usableW <= 0 || usableH <= 0 { usableW = primary.Width usableH = primary.Height - defaultTaskbarPoints*scale/100 } usableH -= windowTitleBarPoints * scale / 100 sizes := presetSizes16x9 if math.Abs(float64(primary.Width)/float64(primary.Height)-1.6) < 0.08 { sizes = presetSizes16x10 } var fit [][2]int for i := len(sizes) - 1; i >= 0; i-- { if sizes[i][0] <= usableW && sizes[i][1] <= usableH { fit = append(fit, sizes[i]) } } if len(fit) == 0 { fit = [][2]int{sizes[0]} // a screen smaller than every size: the window shrinks to fit } if len(fit) > maxDisplayPresetCount { fit = fit[:maxDisplayPresetCount] } out := make([]DisplayPreset, 0, len(fit)) rec := -1 for i, sz := range fit { hidpi := GuestStartsHiDPI(sz[0], sz[1], scale) if hidpi && rec < 0 { rec = i } out = append(out, DisplayPreset{ Resolution: fmt.Sprintf("%dx%d", sz[0], sz[1]), Label: fmt.Sprintf("%d × %d", sz[0], sz[1]), HiDPI: hidpi, }) } if rec < 0 { rec = 0 } out[rec].Recommended = true return out, out[rec].Resolution } func RecommendAutoResolution(target *DisplayInfo, borderless, fixedModeSupported bool) (resolution string, hidpi bool) { if !borderless { presets, recommended := RecommendDisplayPresets(target) for _, preset := range presets { if preset.Resolution == recommended { return recommended, preset.HiDPI } } } if target == nil || target.Width <= 0 || target.Height <= 0 { return "1920x1080", false } if fixedModeSupported && target.Width >= 3200 && target.ScalePct >= 125 && target.ScalePct < 175 { width := max(16, (target.Width*4/3)/16*16) height := max(2, (target.Height*4/3)/2*2) if width <= 5120 && height <= 3200 && (width != target.Width || height != target.Height) { return fmt.Sprintf("%dx%d", width, height), true } } return fmt.Sprintf("%dx%d", target.Width, target.Height), FullscreenStartsHiDPI(target) } // ParseResolution parses "WxH" or "W×H" (ignoring surrounding spaces). func ParseResolution(raw string) (w, h int, ok bool) { var x rune for _, r := range raw { if r == 'x' || r == 'X' || r == '×' { x = r break } } if x == 0 { return 0, 0, false } var rw, rh int n, err := fmt.Sscanf(raw, "%d"+string(x)+"%d", &rw, &rh) if err != nil || n != 2 || rw <= 0 || rh <= 0 { return 0, 0, false } return rw, rh, true } // ShouldUseFixedGuestMode reports whether reims-vgpu should decouple the // guest's display mode from the window's physical pixels // (REIMS_VGPU_WINDOW_FIXED_MODE=1). // // - In borderless fullscreen: enabled whenever the configured resolution // differs from the target monitor's native pixels (e.g. 5120x2880 HiDPI // 1.5x supersampled onto a 3840x2160 monitor, or 2560x1440 1x upscaled). // Matching the native monitor stays on the zero-copy 1:1 path. // - In windowed mode: enabled when the configured resolution exceeds the // monitor's usable work area (e.g. 5120x2880 supersampled in a window). func ShouldUseFixedGuestMode(borderless bool, resolution string, target *DisplayInfo) bool { w, h, ok := ParseResolution(resolution) if !ok { return false } if borderless { if target == nil || target.Width <= 0 || target.Height <= 0 { return true } return w != target.Width || h != target.Height } if target == nil || target.Width <= 0 || target.Height <= 0 { return false } scale := target.ScalePct if scale <= 0 { scale = 100 } usableW, usableH := target.WorkWidth, target.WorkHeight if usableW <= 0 || usableH <= 0 { usableW = target.Width usableH = target.Height - defaultTaskbarPoints*scale/100 } usableH -= windowTitleBarPoints * scale / 100 return w > usableW || h > usableH }