Skip to contentSkip to conversation

PATCH] ui/cocoa.m: verify with user before quitting QEMU

Qemu-devel Programmingkid 2 participants 4 messages Sep 18, 2015 – Sep 21, 2015

lore
Share & tools
lore
#0 lore raw

This patch prevents the user from accidentally quitting QEMU by pushing Command-Q or by pushing the close button on the main window. When the user does one of these two things, a dialog box appears verifying with the user if he wants to quit QEMU.

---
 ui/cocoa.m |   22 +++++++++++++++++++++-
 1 files changed, 21 insertions(+), 1 deletions(-)
ui/cocoa.m +21 -1
index 334e6f6..ff1a72b 100644
@@ -829,6 +829,7 @@ QemuCocoaView *cocoaView;
 - (void)powerDownQEMU:(id)sender;
 - (void)ejectDeviceMedia:(id)sender;
 - (void)changeDeviceMedia:(id)sender;
+- (void)verifyQuit;
 @end
 
 @implementation QemuCocoaAppController
@@ -862,6 +863,7 @@ QemuCocoaView *cocoaView;
 #endif
         [normalWindow makeKeyAndOrderFront:self];
         [normalWindow center];
+        [normalWindow setDelegate: self];
         stretch_video = false;
 
         /* Used for displaying pause on the screen */
@@ -933,6 +935,13 @@ QemuCocoaView *cocoaView;
     return YES;
 }
 
+/* Called when the user clicks on a window's close button */
+- (BOOL)windowShouldClose:(id)sender
+{
+    [self verifyQuit];
+    return NO;
+}
+
 - (void)startEmulationWithArgc:(int)argc argv:(char**)argv
 {
     COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
@@ -1125,6 +1134,16 @@ QemuCocoaView *cocoaView;
     }
 }
 
+/* Verifies if the user really wants to quit */
+- (void)verifyQuit
+{
+    NSInteger response;
+    response = NSRunAlertPanel(@"Quit?", @"Are you sure you want to quit?",
+                                                       @"Cancel", @"Quit", nil);
+    if(response == NSAlertAlternateReturn)
+        exit(EXIT_SUCCESS);
+}
+
 @end
 
 
@@ -1178,7 +1197,8 @@ int main (int argc, const char * argv[]) {
     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
     [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
     [menu addItem:[NSMenuItem separatorItem]]; //Separator
-    [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
+    [menu addItemWithTitle:@"Quit QEMU" action:@selector(verifyQuit)
+                                                            keyEquivalent:@"q"];
     menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
     [menuItem setSubmenu:menu];
     [[NSApp mainMenu] addItem:menuItem];
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 5 lines · Programmingkid

This patch prevents the user from accidentally quitting QEMU by pushing Command-Q or by pushing the close button on the main window. When the user does
one of these two things, a dialog box appears verifying with the user if he wants to quit QEMU.

"if they want to" -- our users are not exclusively male :-)

Quoted 51 lines
---
 ui/cocoa.m |   22 +++++++++++++++++++++-
 1 files changed, 21 insertions(+), 1 deletions(-)
ui/cocoa.m +1 -0
index 334e6f6..ff1a72b 100644
@@ -829,6 +829,7 @@ QemuCocoaView *cocoaView;
 - (void)powerDownQEMU:(id)sender;
 - (void)ejectDeviceMedia:(id)sender;
 - (void)changeDeviceMedia:(id)sender;
+- (void)verifyQuit;
 @end
 
 

@implementation QemuCocoaAppController

ui/cocoa.m +1 -0
@@ -862,6 +863,7 @@ QemuCocoaView *cocoaView;
 #endif
         [normalWindow makeKeyAndOrderFront:self];
         [normalWindow center];
+        [normalWindow setDelegate: self];
         stretch_video = false;
 
 
         /* Used for displaying pause on the screen */
ui/cocoa.m +7 -0
@@ -933,6 +935,13 @@ QemuCocoaView *cocoaView;
     return YES;
 }
 
 
 
+/* Called when the user clicks on a window's close button */
+- (BOOL)windowShouldClose:(id)sender
+{
+    [self verifyQuit];
+    return NO;
+}
+
 - (void)startEmulationWithArgc:(int)argc argv:(char**)argv
 {
     COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
ui/cocoa.m +0 -0
@@ -1125,6 +1134,16 @@ QemuCocoaView *cocoaView;
     }
 }
 
 

ui/cocoa.m QemuCocoaView *cocoaView; · line 1147

Quoted 10 lines
ui/cocoa.m +8 -0
 
+/* Verifies if the user really wants to quit */
+- (void)verifyQuit
+{
+    NSInteger response;
+    response = NSRunAlertPanel(@"Quit?", @"Are you sure you want to quit?",
+                                                       @"Cancel", @"Quit",
 nil);
+    if(response == NSAlertAlternateReturn)
+        exit(EXIT_SUCCESS);

Missing braces. Checkpatch will warn you about this kind of thing.

Calling exit() here doesn't really seem right to me -- I think we should return the "OK to terminate app/close this window" response to the OSX framework and let it cause us to exit in the normal way.

Quoted 12 lines
ui/cocoa.m +2 -0
+}
+

@end

ui/cocoa.m +0 -0
@@ -1178,7 +1197,8 @@ int main (int argc, const char * argv[]) {
     [menuItem
 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
     [menu addItemWithTitle:@"Show All"
Quoted 5 lines

action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
[menu addItem:[NSMenuItem separatorItem]]; //Separator
- [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:)
keyEquivalent:@"q"];
+ [menu addItemWithTitle:@"Quit QEMU" action:@selector(verifyQuit)

Should we be doing this by implementing NSApplication:applicationShouldTerminate?
That seems to be where the OSX framework expects us to handle "do you really want to do this" as part of the standard terminate:
flow.

Quoted 6 lines

+
keyEquivalent:@"q"];
menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil

keyEquivalent:@""];
     [menuItem setSubmenu:menu];
     [[NSApp mainMenu] addItem:menuItem];

thanks
-- PMM

#2 lore raw
Quoted 8 lines · Peter Maydell
Quoted 5 lines · Programmingkid

This patch prevents the user from accidentally quitting QEMU by pushing Command-Q or by pushing the close button on the main window. When the user does
one of these two things, a dialog box appears verifying with the user if he wants to quit QEMU.

"if they want to" -- our users are not exclusively male :-)

I'm thinking "if he or she wants to" would be more grammatically correct.

Quoted 53 lines
Quoted 53 lines

---
ui/cocoa.m | 22 +++++++++++++++++++++-

1 files changed, 21 insertions(+), 1 deletions(-)
ui/cocoa.m +1 -3
index 334e6f6..ff1a72b 100644
@@ -829,6 +829,7 @@ QemuCocoaView *cocoaView;
- (void)powerDownQEMU:(id)sender;
- (void)ejectDeviceMedia:(id)sender;
- (void)changeDeviceMedia:(id)sender;
+- (void)verifyQuit;

@end

@implementation QemuCocoaAppController

ui/cocoa.m +1 -0
@@ -862,6 +863,7 @@ QemuCocoaView *cocoaView;
 #endif
        [normalWindow makeKeyAndOrderFront:self];
        [normalWindow center];
+        [normalWindow setDelegate: self];
        stretch_video = false;
 
 
        /* Used for displaying pause on the screen */
ui/cocoa.m +7 -1
@@ -933,6 +935,13 @@ QemuCocoaView *cocoaView;
    return YES;
 }
 
 
 
+/* Called when the user clicks on a window's close button */
+- (BOOL)windowShouldClose:(id)sender
+{
+    [self verifyQuit];
+    return NO;
+}
+
- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
{
    COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
ui/cocoa.m +0 -0
@@ -1125,6 +1134,16 @@ QemuCocoaView *cocoaView;
    }
 }
 
 
 
Quoted 11 lines

ui/cocoa.m QemuCocoaView *cocoaView; · line 1144

Quoted 9 lines
ui/cocoa.m +8 -0
+/* Verifies if the user really wants to quit */
+- (void)verifyQuit
+{
+    NSInteger response;
+    response = NSRunAlertPanel(@"Quit?", @"Are you sure you want to quit?",
+                                                       @"Cancel", @"Quit",
 nil);
+    if(response == NSAlertAlternateReturn)
+        exit(EXIT_SUCCESS);

Missing braces. Checkpatch will warn you about this kind of thing.

Surprisingly it didn't work. I have noticed that checkpatch also doesn't detect lines over 80 characters in length. I did use checkpatch on this patch. It said there were no errors or warnings. Maybe it doesn't know objective-c very well?

Quoted 4 lines

Calling exit() here doesn't really seem right to me -- I think we should return the "OK to terminate app/close this window" response to the OSX framework and let it cause us to exit in the normal way.

Ok.

Quoted 9 lines
Quoted 9 lines
ui/cocoa.m +2 -0
+}
+

@end

Quoted 15 lines
Quoted 9 lines
ui/cocoa.m +0 -0
@@ -1178,7 +1197,8 @@ int main (int argc, const char * argv[]) {
    [menuItem
 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
    [menu addItemWithTitle:@"Show All"

action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
[menu addItem:[NSMenuItem separatorItem]]; //Separator
- [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:)
keyEquivalent:@"q"];
+ [menu addItemWithTitle:@"Quit QEMU" action:@selector(verifyQuit)

Should we be doing this by implementing NSApplication:applicationShouldTerminate?
That seems to be where the OSX framework expects us to handle "do you really want to do this" as part of the standard terminate:
flow.

Ok. I can do that if you wish.

Quoted 10 lines
Quoted 7 lines

+
keyEquivalent:@"q"];
menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil

keyEquivalent:@""];
    [menuItem setSubmenu:menu];
    [[NSApp mainMenu] addItem:menuItem];

thanks
-- PMM

#3 lore raw
Quoted 10 lines · Programmingkid
Quoted 7 lines · Peter Maydell
Quoted 5 lines · Programmingkid

This patch prevents the user from accidentally quitting QEMU by pushing Command-Q or by pushing the close button on the main window. When the user does
one of these two things, a dialog box appears verifying with the user if he wants to quit QEMU.

Quoted 3 lines
Quoted 1 line

"if they want to" -- our users are not exclusively male :-)

I'm thinking "if he or she wants to" would be more grammatically correct.

Singular 'they' is totally fine and has been for centuries, and it's less clunky than 'he or she'. But you can use the latter if you prefer it.

thanks
-- PMM