From: Programmingkid <programmingkidx@gmail.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-devel qemu-devel <qemu-devel@nongnu.org>
Subject: [Qemu-devel] [PATCH 2/3] ui/cocoa.m: Adds device menu items to Machine menu
Date: Mon, 11 May 2015 18:16:10 -0400 [thread overview]
Message-ID: <810C0446-745B-4F71-A769-3F55B58ECA8D@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 6490 bytes --]
Adds all removable devices to the Machine menu as a Change and Eject menu item pair.
ide-cd0 would have a "Change ide-cd0..." and "Eject ide-cd0" menu items.
Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
---
ui/cocoa.m | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 110 insertions(+), 3 deletions(-)
diff --git a/ui/cocoa.m b/ui/cocoa.m
index 7a9c194..5e558ea 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -30,6 +30,7 @@
#include "ui/input.h"
#include "sysemu/sysemu.h"
#include "qmp-commands.h"
+#include "sysemu/blockdev.h"
#ifndef MAC_OS_X_VERSION_10_4
#define MAC_OS_X_VERSION_10_4 1040
@@ -241,7 +242,16 @@ static int cocoa_keycode_to_qemu(int keycode)
return keymap[keycode];
}
-
+/* Handles any errors that happen with a device transaction */
+static void handleAnyDeviceErrors(Error * err)
+{
+ if (err) {
+ NSRunAlertPanel(@"Alert", [NSString stringWithCString: error_get_pretty(err)
+ encoding: NSASCIIStringEncoding],
+ @"OK", nil, nil);
+ error_free(err);
+ }
+}
/*
------------------------------------------------------
@@ -301,6 +311,17 @@ QemuCocoaView *cocoaView;
screen.width = frameRect.size.width;
screen.height = frameRect.size.height;
+ /* Used for displaying pause on the screen */
+ pauseLabel = [NSTextField new];
+ [pauseLabel setBezeled:YES];
+ [pauseLabel setDrawsBackground:YES];
+ [pauseLabel setBackgroundColor: [NSColor whiteColor]];
+ [pauseLabel setEditable:NO];
+ [pauseLabel setSelectable:NO];
+ [pauseLabel setStringValue: @"Paused"];
+ [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
+ [pauseLabel setTextColor: [NSColor blackColor]];
+ [pauseLabel sizeToFit];
}
return self;
}
@@ -788,8 +809,6 @@ QemuCocoaView *cocoaView;
- (QEMUScreen) gscreen {return screen;}
@end
-
-
/*
------------------------------------------------------
QemuCocoaAppController
@@ -807,6 +826,8 @@ QemuCocoaView *cocoaView;
- (void)resumeQemu: (id) sender;
- (void)displayPause;
- (void)removePause;
+- (void)ejectDeviceMedia:(id)sender;
+- (void)changeDeviceMedia:(id)sender;
@end
@implementation QemuCocoaAppController
@@ -998,6 +1019,51 @@ QemuCocoaView *cocoaView;
[pauseLabel removeFromSuperview];
}
+// Ejects the media
+// Uses sender's tag to figure out device to eject
+- (void)ejectDeviceMedia:(id)sender
+{
+ NSString * drive;
+ drive = [sender representedObject];
+ if(drive == nil) {
+ NSBeep();
+ NSRunAlertPanel(@"Alert", @"Failed to find drive to eject!", @"OK", nil, nil);
+ NSLog(@"Error: Failed to find drive to eject!\n");
+ return;
+ }
+
+ Error *err = NULL;
+ qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding], false, false, &err);
+ handleAnyDeviceErrors(err);
+}
+
+/* Displays a dialog box asking the user to select an image file to load.
+ Uses sender's tag value to figure out which drive to use. */
+- (void)changeDeviceMedia:(id)sender
+{
+ /* Find the drive name */
+ NSString * drive;
+ drive = [sender representedObject];
+ if(drive == nil) {
+ NSBeep();
+ NSRunAlertPanel(@"Alert", @"Could not find drive!", @"OK", nil, nil);
+ NSLog(@"Error: Could not find drive!\n");
+ return;
+ }
+
+ /* Display the file open dialog */
+ NSOpenPanel * open_panel;
+ open_panel = [NSOpenPanel openPanel];
+ [open_panel setCanChooseFiles: YES];
+ [open_panel setAllowsMultipleSelection: NO];
+ if([open_panel runModalForDirectory: nil file: nil] == NSOKButton) {
+ NSString * file = [[open_panel filenames] objectAtIndex: 0];
+ Error *err = NULL;
+ qmp_change_blockdev([drive cStringUsingEncoding: NSASCIIStringEncoding], [file cStringUsingEncoding: NSASCIIStringEncoding], "raw", &err);
+ handleAnyDeviceErrors(err);
+ }
+}
+
@end
@@ -1096,6 +1162,46 @@ int main (int argc, const char * argv[]) {
#pragma mark machine menu code
+// Make menu items for all removable devices
+static void addDeviceMenuItems(NSMenu * menu)
+{
+ NSMenuItem * menuItem;
+ BlockInfoList * currentDevice;
+ NSString * deviceName;
+
+ currentDevice = qmp_query_block(false);
+ if(currentDevice == NULL) {
+ NSBeep();
+ NSRunAlertPanel(@"Alert", @"Failed to query for block devices!", @"OK", nil, nil);
+ return;
+ }
+
+ // Add a separator between related groups of menu items
+ [menu addItem:[NSMenuItem separatorItem]];
+
+ /* Loop thru all the block devices in the emulator */
+ while (currentDevice) {
+ deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
+
+ if(currentDevice->value->removable == true) {
+ menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
+ action: @selector(changeDeviceMedia:)
+ keyEquivalent: @""];
+ [menu addItem: menuItem];
+ [menuItem setRepresentedObject: deviceName];
+ [menuItem autorelease];
+
+ menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
+ action: @selector(ejectDeviceMedia:)
+ keyEquivalent: @""];
+ [menu addItem: menuItem];
+ [menuItem setRepresentedObject: deviceName];
+ [menuItem autorelease];
+ }
+ currentDevice = currentDevice->next;
+ }
+}
+
/*
Adds the Machine menu to the menu bar.
Has to be added separately because QEMU needs
@@ -1115,6 +1221,7 @@ static void createMachineMenu()
[menuItem setSubmenu:menu];
[[NSApp mainMenu] insertItem: menuItem atIndex: 2]; // Insert after View menu
[[menu itemWithTitle: @"Resume"] setEnabled: NO]; // Disables the Resume menu item because it isn't needed right now.
+ addDeviceMenuItems(menu);
}
#pragma mark qemu
--
1.7.5.4
[-- Attachment #2: Type: text/html, Size: 34480 bytes --]
next reply other threads:[~2015-05-11 22:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-11 22:16 Programmingkid [this message]
2015-05-16 22:21 ` [Qemu-devel] [PATCH 2/3] ui/cocoa.m: Adds device menu items to Machine menu Peter Maydell
2015-05-18 13:24 ` Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=810C0446-745B-4F71-A769-3F55B58ECA8D@gmail.com \
--to=programmingkidx@gmail.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).