Skip to content

Commit

Permalink
display(qemu): support saving dynamic resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
osy committed Nov 20, 2024
1 parent 367008d commit 6448f8e
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController {
}
}

if isSecondary && isDisplaySizeDynamic, let window = window {
restoreDynamicResolution(for: window)
}

super.windowDidLoad()
}

Expand Down Expand Up @@ -248,10 +252,10 @@ extension VMDisplayQemuMetalWindowController {
return
}
if isDisplaySizeDynamic != supported {
displaySizeDidChange(size: displaySize)
displaySizeDidChange(size: displaySize, shouldSaveResolution: false)
DispatchQueue.main.async {
if supported, let window = self.window {
_ = self.updateGuestResolution(for: window, frameSize: window.frame.size)
self.restoreDynamicResolution(for: window)
}
}
}
Expand All @@ -262,7 +266,7 @@ extension VMDisplayQemuMetalWindowController {

// MARK: - Screen management
extension VMDisplayQemuMetalWindowController {
fileprivate func displaySizeDidChange(size: CGSize) {
fileprivate func displaySizeDidChange(size: CGSize, shouldSaveResolution: Bool = true) {
// cancel any pending resize
cancelResize?.cancel()
cancelResize = nil
Expand All @@ -282,6 +286,9 @@ extension VMDisplayQemuMetalWindowController {
} else {
self.updateHostFrame(forGuestResolution: size)
}
if shouldSaveResolution {
self.saveDynamicResolution()
}
}
}

Expand Down Expand Up @@ -412,13 +419,15 @@ extension VMDisplayQemuMetalWindowController {
if isFullScreenAutoCapture {
captureMouse()
}
saveDynamicResolution()
}

func windowDidExitFullScreen(_ notification: Notification) {
isFullScreen = false
if isFullScreenAutoCapture {
releaseMouse()
}
saveDynamicResolution()
}

func windowDidBecomeMain(_ notification: Notification) {
Expand Down Expand Up @@ -449,6 +458,32 @@ extension VMDisplayQemuMetalWindowController {
}
}

// MARK: - Save and restore resolution
@MainActor extension VMDisplayQemuMetalWindowController {
func saveDynamicResolution() {
guard isDisplaySizeDynamic else {
return
}
var resolution = UTMRegistryEntry.Resolution()
resolution.isFullscreen = isFullScreen
resolution.size = displaySize
vm.registryEntry.resolutionSettings[id] = resolution
}

func restoreDynamicResolution(for window: NSWindow) {
guard let resolution = vm.registryEntry.resolutionSettings[id] else {
return
}
if resolution.isFullscreen && !isFullScreen {
window.toggleFullScreen(self)
} else if resolution.size != .zero {
_ = self.updateGuestResolution(for: window, frameSize: resolution.size)
} else {
_ = self.updateGuestResolution(for: window, frameSize: window.frame.size)
}
}
}

// MARK: - Input events
extension VMDisplayQemuMetalWindowController: VMMetalViewInputDelegate {
var shouldUseCmdOptForCapture: Bool {
Expand Down

0 comments on commit 6448f8e

Please sign in to comment.