From afa6b7b46cec9e662e3d7cda2fed3b9f4089de3c Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Thu, 26 May 2022 05:23:13 -0400 Subject: [PATCH] Auto-detect screen DPI (#106) * Auto-detect screen DPI Fixes #104 * Added a comment --- Sources/tart/Commands/Set.swift | 8 ++------ Sources/tart/VM.swift | 23 +++++++++++++++++------ Sources/tart/VMConfig.swift | 1 - 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Sources/tart/Commands/Set.swift b/Sources/tart/Commands/Set.swift index 175cfb4..8b6f847 100644 --- a/Sources/tart/Commands/Set.swift +++ b/Sources/tart/Commands/Set.swift @@ -13,7 +13,7 @@ struct Set: AsyncParsableCommand { @Option(help: "VM memory size in megabytes") var memory: UInt16? - @Option(help: "VM display settings in a format of x(x)?. For example, 1200x800 or 1200x800x72") + @Option(help: "VM display resolution in a format of x. For example, 1200x800") var display: VMDisplayConfig? @Option(help: .hidden) @@ -39,9 +39,6 @@ struct Set: AsyncParsableCommand { if (display.height > 0) { vmConfig.display.height = display.height } - if (display.dpi > 0) { - vmConfig.display.dpi = display.dpi - } } try vmConfig.save(toURL: vmDir.configURL) @@ -66,8 +63,7 @@ extension VMDisplayConfig: ExpressibleByArgument { } self = VMDisplayConfig( width: parts[safe: 0] ?? 0, - height: parts[safe: 1] ?? 0, - dpi: parts[safe: 2] ?? 0 + height: parts[safe: 1] ?? 0 ) } } diff --git a/Sources/tart/VM.swift b/Sources/tart/VM.swift index b4630ba..e27abe0 100644 --- a/Sources/tart/VM.swift +++ b/Sources/tart/VM.swift @@ -172,13 +172,24 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject { // Display let graphicsDeviceConfiguration = VZMacGraphicsDeviceConfiguration() - graphicsDeviceConfiguration.displays = [ - VZMacGraphicsDisplayConfiguration( - widthInPixels: vmConfig.display.width, - heightInPixels: vmConfig.display.height, - pixelsPerInch: vmConfig.display.dpi + if let hostMainScreen = NSScreen.main { + let vmScreenSize = NSSize( + width: vmConfig.display.width, + height: vmConfig.display.height ) - ] + graphicsDeviceConfiguration.displays = [ + VZMacGraphicsDisplayConfiguration(for: hostMainScreen, sizeInPoints: vmScreenSize) + ] + } else { + graphicsDeviceConfiguration.displays = [ + VZMacGraphicsDisplayConfiguration( + widthInPixels: vmConfig.display.width, + heightInPixels: vmConfig.display.height, + // Reasonable guess like https://developer.apple.com/documentation/coregraphics/1456599-cgdisplayscreensize + pixelsPerInch: 72 + ) + ] + } configuration.graphicsDevices = [graphicsDeviceConfiguration] // Audio diff --git a/Sources/tart/VMConfig.swift b/Sources/tart/VMConfig.swift index c5c7d73..a6189e2 100644 --- a/Sources/tart/VMConfig.swift +++ b/Sources/tart/VMConfig.swift @@ -29,7 +29,6 @@ enum CodingKeys: String, CodingKey { struct VMDisplayConfig: Codable { var width: Int = 1024 var height: Int = 768 - var dpi: Int = 72 } struct VMConfig: Codable {