-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gui: Watch vm state to terminate when it's stopped #154
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,11 +165,30 @@ - (instancetype)init | |
|
||
@end | ||
|
||
API_AVAILABLE(macos(12.0)) | ||
@interface VMStateObserver : NSObject | ||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; | ||
@end | ||
|
||
@implementation VMStateObserver | ||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; | ||
{ | ||
if ([keyPath isEqualToString:@"state"]) { | ||
int newState = (int)[change[NSKeyValueChangeNewKey] integerValue]; | ||
if (newState == VZVirtualMachineStateStopped || newState == VZVirtualMachineStateError) { | ||
[NSApp performSelectorOnMainThread:@selector(terminate:) withObject:context waitUntilDone:NO]; | ||
[object removeObserver:self forKeyPath:@"state"]; | ||
} | ||
} | ||
} | ||
@end | ||
|
||
@implementation AppDelegate { | ||
VZVirtualMachine *_virtualMachine; | ||
VZVirtualMachineView *_virtualMachineView; | ||
CGFloat _windowWidth; | ||
CGFloat _windowHeight; | ||
VMStateObserver *_observer; | ||
} | ||
|
||
- (instancetype)initWithVirtualMachine:(VZVirtualMachine *)virtualMachine | ||
|
@@ -179,6 +198,11 @@ - (instancetype)initWithVirtualMachine:(VZVirtualMachine *)virtualMachine | |
self = [super init]; | ||
_virtualMachine = virtualMachine; | ||
[_virtualMachine setDelegate:self]; | ||
_observer = [[VMStateObserver alloc] init]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm, when we have chance to release this object...? |
||
[virtualMachine addObserver:_observer | ||
forKeyPath:@"state" | ||
options:NSKeyValueObservingOptionNew | ||
context:(void *)self]; | ||
|
||
// Setup virtual machine view configs | ||
VZVirtualMachineView *view = [[[VZVirtualMachineView alloc] init] autorelease]; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why is not enough code lines at 228 and 234?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, maybe We don't need state for error...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this API is better than observing state by self
https://developer.apple.com/documentation/virtualization/vzvirtualmachinedelegate/3656731-virtualmachine?language=objc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API is already used in Code-Hex/vz
vz/virtualization_view.m
Lines 207 to 212 in 4f8ae6d
but the log is never printed/the method is never called. The VM did not stop because of an error, maybe it is normal this is not called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you try this code with Xcode?
NSLog shows only on the Xcode consoleI'm sorry my mistake. It uses Stderr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So... current code is racing with this API and new observing code that you added.
I think better fix:
And I'm not sure call remove observer in observe handler. (I think this is anti pattern) So it's good calling together with method to release observer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestions, I'll take a look!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(started typing this before your last round of comments, posting it as it adds a bit of explanations)
If we call
Stop
from the host:guestDidStopVirtualMachine
is not calleddidStopWithError
is not called