From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56965) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yrvzm-0000jv-PM for qemu-devel@nongnu.org; Mon, 11 May 2015 18:16:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yrvzi-0003Hf-ID for qemu-devel@nongnu.org; Mon, 11 May 2015 18:16:18 -0400 Received: from mail-qg0-x235.google.com ([2607:f8b0:400d:c04::235]:35591) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yrvzi-0003Hb-9q for qemu-devel@nongnu.org; Mon, 11 May 2015 18:16:14 -0400 Received: by qgej70 with SMTP id j70so75682265qge.2 for ; Mon, 11 May 2015 15:16:13 -0700 (PDT) From: Programmingkid Content-Type: multipart/alternative; boundary=Apple-Mail-2--43678663 Date: Mon, 11 May 2015 18:16:10 -0400 Message-Id: <810C0446-745B-4F71-A769-3F55B58ECA8D@gmail.com> Mime-Version: 1.0 (Apple Message framework v1084) Subject: [Qemu-devel] [PATCH 2/3] ui/cocoa.m: Adds device menu items to Machine menu List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell Cc: qemu-devel qemu-devel --Apple-Mail-2--43678663 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Adds all removable devices to the Machine menu as a Change and Eject = menu item pair.=20 ide-cd0 would have a "Change ide-cd0..." and "Eject ide-cd0" menu items.=20= Signed-off-by: John Arbuckle --- 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" =20 #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]; } =20 - +/* 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); + } +} =20 /* ------------------------------------------------------ @@ -301,6 +311,17 @@ QemuCocoaView *cocoaView; screen.width =3D frameRect.size.width; screen.height =3D frameRect.size.height; =20 + /* Used for displaying pause on the screen */ + pauseLabel =3D [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 =20 - - /* ------------------------------------------------------ QemuCocoaAppController @@ -807,6 +826,8 @@ QemuCocoaView *cocoaView; - (void)resumeQemu: (id) sender; - (void)displayPause; - (void)removePause; +- (void)ejectDeviceMedia:(id)sender; +- (void)changeDeviceMedia:(id)sender; @end =20 @implementation QemuCocoaAppController @@ -998,6 +1019,51 @@ QemuCocoaView *cocoaView; [pauseLabel removeFromSuperview]; } =20 +// Ejects the media +// Uses sender's tag to figure out device to eject +- (void)ejectDeviceMedia:(id)sender +{ + NSString * drive; + drive =3D [sender representedObject]; + if(drive =3D=3D 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 =3D 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 =3D [sender representedObject]; + if(drive =3D=3D 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 =3D [NSOpenPanel openPanel]; + [open_panel setCanChooseFiles: YES]; + [open_panel setAllowsMultipleSelection: NO]; + if([open_panel runModalForDirectory: nil file: nil] =3D=3D = NSOKButton) { + NSString * file =3D [[open_panel filenames] objectAtIndex: 0]; + Error *err =3D NULL; + qmp_change_blockdev([drive cStringUsingEncoding: = NSASCIIStringEncoding], [file cStringUsingEncoding: = NSASCIIStringEncoding], "raw", &err); + handleAnyDeviceErrors(err); + } +} + @end =20 =20 @@ -1096,6 +1162,46 @@ int main (int argc, const char * argv[]) { =20 #pragma mark machine menu code =20 +// Make menu items for all removable devices +static void addDeviceMenuItems(NSMenu * menu) +{ + NSMenuItem * menuItem; + BlockInfoList * currentDevice; + NSString * deviceName; + + currentDevice =3D qmp_query_block(false); + if(currentDevice =3D=3D 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 =3D [[NSString stringWithFormat: @"%s", = currentDevice->value->device] retain]; + + if(currentDevice->value->removable =3D=3D true) { + menuItem =3D [[NSMenuItem alloc] initWithTitle: [NSString = stringWithFormat: @"Change %s...", currentDevice->value->device] + action: = @selector(changeDeviceMedia:) + keyEquivalent: @""]; + [menu addItem: menuItem]; + [menuItem setRepresentedObject: deviceName]; + [menuItem autorelease]; + + menuItem =3D [[NSMenuItem alloc] initWithTitle: [NSString = stringWithFormat: @"Eject %s", currentDevice->value->device] + action: = @selector(ejectDeviceMedia:) + keyEquivalent: @""]; + [menu addItem: menuItem]; + [menuItem setRepresentedObject: deviceName]; + [menuItem autorelease]; + } + currentDevice =3D 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); } =20 #pragma mark qemu --=20 1.7.5.4 --Apple-Mail-2--43678663 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
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. 

programmingkidx@gmail.com>= ;

---
 ui/cocoa.m |  113 = ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--

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 = "sysemu/sysemu.h"
 #include "qmp-commands.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) = {
+         =                     =                     =     encoding: NSASCIIStringEncoding],
+ =                     =                     =             @"OK", nil, nil);
+}

 

 /*
  = ------------------------------------------------------
@@ = -301,6 +311,17 @@ QemuCocoaView *cocoaView;
       =   screen.width =3D frameRect.size.width;
 

+        /* Used = for displaying pause on the screen */
+        = pauseLabel =3D [NSTextField new];
+        = [pauseLabel setBezeled:YES];
+        = [pauseLabel setDrawsBackground:YES];
+        = [pauseLabel setBackgroundColor: [NSColor whiteColor]];
+        [pauseLabel setFont: = [NSFont fontWithName: @"Helvetica" size: 90]];
+        [pauseLabel = sizeToFit];
     }
     return = self;
 }
@@ -788,8 +809,6 @@ QemuCocoaView = *cocoaView;
 - (QEMUScreen) gscreen {return = screen;}
 @end

 

-
-
 /*
  = ------------------------------------------------------
@@ = -807,6 +826,8 @@ QemuCocoaView *cocoaView;
 - (void)resumeQemu: (id) = sender;
 - (void)displayPause;
+- = (void)ejectDeviceMedia:(id)sender;
+- = (void)changeDeviceMedia:(id)sender;
 @end

 

@@ = -998,6 +1019,51 @@ QemuCocoaView *cocoaView;
 

+// Ejects the media
+// = Uses sender's tag to figure out device to eject
+- = (void)ejectDeviceMedia:(id)sender
+{
+    NSString * = drive;
+    drive =3D [sender = representedObject];
+    if(drive =3D=3D nil) {
+        = NSRunAlertPanel(@"Alert", @"Failed to find drive to eject!", @"OK", nil, = nil);
+        NSLog(@"Error: Failed = to find drive to eject!\n");
+        = return;
+    }
+
+    Error *err =3D = 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. = */
+{
+    NSString * = drive;
+    drive =3D [sender = representedObject];
+    if(drive =3D=3D nil) {
+        = NSRunAlertPanel(@"Alert", @"Could not find drive!", @"OK", nil, = nil);
+        NSLog(@"Error: Could = not find drive!\n");
+        return;
+
+    /* Display the file open dialog = */
+    open_panel =3D = [NSOpenPanel openPanel];
+    [open_panel = setCanChooseFiles: YES];
+    [open_panel = setAllowsMultipleSelection: NO];
+    if([open_panel = runModalForDirectory: nil file: nil] =3D=3D NSOKButton) {
+        Error = *err =3D 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 =3D = qmp_query_block(false);
+    if(currentDevice =3D=3D = NULL) {
+        NSBeep();
+    }
+    // Add a separator between related = groups of menu items
+    [menu addItem:[NSMenuItem = separatorItem]];
+
+    /* Loop thru all the block devices = in the emulator */
+    while (currentDevice) {
+
+        = if(currentDevice->value->removable =3D=3D true) {
+          =                     =                     = action: @selector(changeDeviceMedia:)
+         =                     =               keyEquivalent: = @""];
+            [menu = addItem: menuItem];
+            = [menuItem setRepresentedObject: deviceName];
+
+            menuItem = =3D [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: = @"Eject %s", currentDevice->value->device]
+           =                     =             keyEquivalent: @""];
+            = [menuItem setRepresentedObject: deviceName];
+        }
+    }
+
 /*
    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
+    addDeviceMenuItems(menu);
 

 #pragma mark qemu
1.7.5.4