Skip to content

Commit

Permalink
display(iOS): fix scale/origin saving
Browse files Browse the repository at this point in the history
This was broken in the re-factor. We fix the functionality and also
clean up the code and make it so the persistent value is always up to
date.

Fixes #3669
  • Loading branch information
osy committed Feb 25, 2022
1 parent cfceb5d commit 8aae037
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 78 deletions.
2 changes: 0 additions & 2 deletions Configuration/UTMViewState.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) double displayScale;
@property (nonatomic, assign) double displayOriginX;
@property (nonatomic, assign) double displayOriginY;
@property (nonatomic, assign) double displaySizeWidth;
@property (nonatomic, assign) double displaySizeHeight;
@property (nonatomic, assign) BOOL hasSaveState;
@property (nonatomic, copy, nullable) NSData *sharedDirectory;
@property (nonatomic, copy, nullable) NSString *sharedDirectoryPath;
Expand Down
22 changes: 0 additions & 22 deletions Configuration/UTMViewState.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
const NSString *const kUTMViewStateDisplayScaleKey = @"DisplayScale";
const NSString *const kUTMViewStateDisplayOriginXKey = @"DisplayOriginX";
const NSString *const kUTMViewStateDisplayOriginYKey = @"DisplayOriginY";
const NSString *const kUTMViewStateDisplaySizeWidthKey = @"DisplaySizeWidth";
const NSString *const kUTMViewStateDisplaySizeHeightKey = @"DisplaySizeHeight";
const NSString *const kUTMViewStateShowToolbarKey = @"ShowToolbar";
const NSString *const kUTMViewStateShowKeyboardKey = @"ShowKeyboard";
const NSString *const kUTMViewStateSuspendedKey = @"Suspended";
Expand Down Expand Up @@ -79,24 +77,6 @@ - (void)setDisplayOriginY:(double)displayOriginY {
_rootDict[kUTMViewStateDisplayOriginYKey] = @(displayOriginY);
}

- (double)displaySizeWidth {
return [_rootDict[kUTMViewStateDisplaySizeWidthKey] doubleValue];
}

- (void)setDisplaySizeWidth:(double)displaySizeWidth {
[self propertyWillChange];
_rootDict[kUTMViewStateDisplaySizeWidthKey] = @(displaySizeWidth);
}

- (double)displaySizeHeight {
return [_rootDict[kUTMViewStateDisplaySizeHeightKey] doubleValue];
}

- (void)setDisplaySizeHeight:(double)displaySizeHeight {
[self propertyWillChange];
_rootDict[kUTMViewStateDisplaySizeHeightKey] = @(displaySizeHeight);
}

- (BOOL)hasSaveState {
return [_rootDict[kUTMViewStateSuspendedKey] boolValue];
}
Expand Down Expand Up @@ -216,8 +196,6 @@ - (instancetype)init {
self.displayScale = 1.0;
self.displayOriginX = 0;
self.displayOriginY = 0;
self.displaySizeWidth = 0;
self.displaySizeHeight = 0;
}
return self;
}
Expand Down
2 changes: 0 additions & 2 deletions Managers/UTMInputOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)connectWithCompletion:(ioConnectCompletionHandler_t)block;
- (void)disconnect;
- (CSScreenshot* _Nullable)screenshot;
- (void)syncViewState:(UTMViewState *)viewState;
- (void)restoreViewState:(UTMViewState *)viewState;

@end

Expand Down
20 changes: 0 additions & 20 deletions Managers/UTMQemuVirtualMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ - (UTMQemuConfiguration *)qemuConfig {
return (UTMQemuConfiguration *)self.config;
}

- (void)setDelegate:(id<UTMVirtualMachineDelegate>)delegate {
[super setDelegate:delegate];
[self.ioService restoreViewState:self.viewState];
}

- (id)ioDelegate {
if ([self.ioService isKindOfClass:[UTMSpiceIO class]]) {
return ((UTMSpiceIO *)self.ioService).delegate;
Expand Down Expand Up @@ -403,9 +398,6 @@ - (void)_vmStartWithCompletion:(void (^)(NSError * _Nullable))completion {
assert(self.qemu.isConnected);
assert(self.ioService.isConnected);
[self changeState:kVMStarted];
dispatch_async(dispatch_get_main_queue(), ^{
[self.ioService restoreViewState:self.viewState];
});
if (self.viewState.hasSaveState) {
[self _vmDeleteStateWithCompletion:^(NSError *error){
// ignore error
Expand Down Expand Up @@ -439,9 +431,6 @@ - (void)_vmStopForce:(BOOL)force completion:(void (^)(NSError * _Nullable))compl
completion([self errorGeneric]);
return;
}
dispatch_sync(dispatch_get_main_queue(), ^{
[self.ioService syncViewState:self.viewState];
});
if (!force) {
[self changeState:kVMStopping];
}
Expand Down Expand Up @@ -484,9 +473,6 @@ - (void)_vmResetWithCompletion:(void (^)(NSError * _Nullable))completion {
completion([self errorGeneric]);
return;
}
dispatch_sync(dispatch_get_main_queue(), ^{
[self.ioService syncViewState:self.viewState];
});
[self changeState:kVMStopping];
if (self.viewState.hasSaveState) {
[self _vmDeleteStateWithCompletion:^(NSError *error) {}];
Expand Down Expand Up @@ -525,9 +511,6 @@ - (void)_vmPauseWithCompletion:(void (^)(NSError * _Nullable))completion {
completion([self errorGeneric]);
return;
}
dispatch_sync(dispatch_get_main_queue(), ^{
[self.ioService syncViewState:self.viewState];
});
[self changeState:kVMPausing];
dispatch_sync(dispatch_get_main_queue(), ^{
[self updateScreenshot];
Expand Down Expand Up @@ -657,9 +640,6 @@ - (void)_vmResumeWithCompletion:(void (^)(NSError * _Nullable))completion {
}
if (!resumeError) {
[self changeState:kVMStarted];
dispatch_async(dispatch_get_main_queue(), ^{
[self.ioService restoreViewState:self.viewState];
});
} else {
[self changeState:kVMStopped];
}
Expand Down
18 changes: 0 additions & 18 deletions Managers/UTMSpiceIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,6 @@ - (CSScreenshot *)screenshot {
return [self.primaryDisplay screenshot];
}

- (void)syncViewState:(UTMViewState *)viewState {
viewState.displayOriginX = self.primaryDisplay.viewportOrigin.x;
viewState.displayOriginY = self.primaryDisplay.viewportOrigin.y;
viewState.displaySizeWidth = self.primaryDisplay.displaySize.width;
viewState.displaySizeHeight = self.primaryDisplay.displaySize.height;
viewState.displayScale = self.primaryDisplay.viewportScale;
}

- (void)restoreViewState:(UTMViewState *)viewState {
self.primaryDisplay.viewportOrigin = CGPointMake(viewState.displayOriginX, viewState.displayOriginY);
self.primaryDisplay.displaySize = CGSizeMake(viewState.displaySizeWidth, viewState.displaySizeHeight);
if (viewState.displayScale) { // cannot be zero
self.primaryDisplay.viewportScale = viewState.displayScale;
} else {
self.primaryDisplay.viewportScale = 1.0; // default value
}
}

#pragma mark - CSConnectionDelegate

- (void)spiceConnected:(CSConnection *)connection {
Expand Down
6 changes: 0 additions & 6 deletions Managers/UTMTerminalIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,4 @@ - (CSScreenshot *)screenshot {
return nil;
}

- (void)syncViewState:(UTMViewState *)viewState {
}

- (void)restoreViewState:(UTMViewState *)viewState {
}

@end
5 changes: 5 additions & 0 deletions Platform/iOS/Display/VMDisplayMetalViewController+Touch.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ - (void)moveScreen:(UIPanGestureRecognizer *)sender {
viewport.x = CGPointToPixel(translation.x) + _lastTwoPanOrigin.x;
viewport.y = CGPointToPixel(translation.y) + _lastTwoPanOrigin.y;
self.vmDisplay.viewportOrigin = [self clipDisplayToView:viewport];
// persist this change in viewState
self.vm.viewState.displayOriginX = self.vmDisplay.viewportOrigin.x;
self.vm.viewState.displayOriginY = self.vmDisplay.viewportOrigin.y;
}
if (sender.state == UIGestureRecognizerStateEnded) {
// TODO: decelerate
Expand Down Expand Up @@ -433,6 +436,8 @@ - (IBAction)gesturePinch:(UIPinchGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded && (self.twoFingerPanType == VMGestureTypeMoveScreen || self.threeFingerPanType == VMGestureTypeMoveScreen)) {
NSAssert(sender.scale > 0, @"sender.scale cannot be 0");
self.vmDisplay.viewportScale *= sender.scale;
// persist this change in viewState
self.vm.viewState.displayScale = self.vmDisplay.viewportScale;
sender.scale = 1.0;
}
}
Expand Down
14 changes: 14 additions & 0 deletions Platform/iOS/Display/VMDisplayMetalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,19 @@ - (void)resizeDisplayToFit {
CGSize scaled = CGSizeMake(viewSize.width / displaySize.width, viewSize.height / displaySize.height);
self.vmDisplay.viewportScale = MIN(scaled.width, scaled.height);
self.vmDisplay.viewportOrigin = CGPointMake(0, 0);
// persist this change in viewState
self.vm.viewState.displayScale = self.vmDisplay.viewportScale;
self.vm.viewState.displayOriginX = 0;
self.vm.viewState.displayOriginY = 0;
}

- (void)resetDisplay {
self.vmDisplay.viewportScale = 1.0;
self.vmDisplay.viewportOrigin = CGPointMake(0, 0);
// persist this change in viewState
self.vm.viewState.displayScale = 1.0;
self.vm.viewState.displayOriginX = 0;
self.vm.viewState.displayOriginY = 0;
}

#pragma mark - Resizing
Expand All @@ -205,6 +213,12 @@ - (void)spiceDidCreateDisplay:(CSDisplayMetal *)display {
if (display.isPrimaryDisplay) {
self.vmDisplay = display;
_renderer.source = display;
// restore last size
display.viewportOrigin = CGPointMake(self.vm.viewState.displayOriginX, self.vm.viewState.displayOriginY);
double displayScale = self.vm.viewState.displayScale;
if (displayScale) { // cannot be zero
display.viewportScale = displayScale;
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions Platform/iOS/Display/VMToolbarActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ import SwiftUI
}
if viewController.vm.state == .vmStarted {
let yes = UIAlertAction(title: NSLocalizedString("Yes", comment: "VMDisplayViewController"), style: .destructive) { action in
DispatchQueue.global(qos: .background).async {
viewController.vm.requestVmDeleteState()
viewController.vm.requestVmStop()
viewController.vm.requestVmDeleteState()
viewController.vm.vmStop { _ in
viewController.terminateApplication()
}
}
Expand Down
11 changes: 6 additions & 5 deletions Platform/macOS/UTMDataExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ extension UTMData {
if vm.viewState.hasSaveState {
vm.requestVmDeleteState()
}
vm.requestVmStop(force: true)
if let window = vmWindows[vm] {
DispatchQueue.main.async {
window.close()
vm.vmStop(force: true, completion: { _ in
if let window = self.vmWindows[vm] {
DispatchQueue.main.async {
window.close()
}
}
}
})
}

func trySendTextSpice(vm: UTMQemuVirtualMachine, text: String) {
Expand Down

0 comments on commit 8aae037

Please sign in to comment.