Received: from eggs.gnu.org ([2001:4830:134:3::10]:54315) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4Jay-0005zy-JK for qemu-devel@nongnu.org; Sun, 14 Jun 2015 21:53:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z4Jav-0007wT-8m for qemu-devel@nongnu.org; Sun, 14 Jun 2015 21:53:52 -0400 Received: from mail-qc0-x22f.google.com ([2607:f8b0:400d:c01::22f]:33620) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4Jau-0007wP-Vn for qemu-devel@nongnu.org; Sun, 14 Jun 2015 21:53:49 -0400 Received: by qcbfz6 with SMTP id fz6so1737803qcb.0 for ; Sun, 14 Jun 2015 18:53:48 -0700 (PDT) From: Programmingkid Content-Type: multipart/alternative; boundary=Apple-Mail-1-759491531 Date: Sun, 14 Jun 2015 21:53:44 -0400 Message-Id: Mime-Version: 1.0 (Apple Message framework v1084) Subject: [Qemu-devel] [PATCH v3] cocoa.m: machine menu device menu items List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell Cc: qemu-devel qemu-devel --Apple-Mail-1-759491531 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. ide-cd0 would have a "Change ide-cd0..." and "Eject ide-cd0" menu items. Signed-off-by: John Arbuckle --- Removed depreciated code from QEMU_Alert(). Unified supported image file extensions into one array. Replaced depreciated filename method for URLs method in NSOpenPanel = code. Replaced free() with qapi_free_BlockInfoList(). ui/cocoa.m | 156 = ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 153 insertions(+), 3 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 559058b..5abbea7 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_5 #define MAC_OS_X_VERSION_10_5 1050 @@ -67,6 +68,7 @@ int gArgc; char **gArgv; bool stretch_video; NSTextField *pauseLabel; +NSArray * supportedImageFileTypes; =20 // keymap conversion int keymap[] =3D @@ -242,7 +244,24 @@ static int cocoa_keycode_to_qemu(int keycode) return keymap[keycode]; } +/* Displays an alert dialog box with the specified message */ +static void QEMU_Alert(NSString *message) +{ + NSAlert *alert; + alert =3D [NSAlert new]; + [alert setMessageText: message]; + [alert runModal]; +} =20 +/* Handles any errors that happen with a device transaction */ +static void handleAnyDeviceErrors(Error * err) +{ + if (err) { + QEMU_Alert([NSString stringWithCString: error_get_pretty(err) + encoding: = NSASCIIStringEncoding]); + error_free(err); + } +} =20 /* ------------------------------------------------------ @@ -808,6 +827,8 @@ QemuCocoaView *cocoaView; - (void)removePause; - (void)restartQEMU:(id)sender; - (void)powerDownQEMU:(id)sender; +- (void)ejectDeviceMedia:(id)sender; +- (void)changeDeviceMedia:(id)sender; @end =20 @implementation QemuCocoaAppController @@ -854,6 +875,10 @@ QemuCocoaView *cocoaView; [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: = 90]]; [pauseLabel setTextColor: [NSColor blackColor]]; [pauseLabel sizeToFit]; + + // set the supported image file types that can be opened + supportedImageFileTypes =3D [NSArray arrayWithObjects: @"img", = @"iso", @"dmg", + @"qcow", @"qcow2", @"cloop", @"vmdk", = nil]; } return self; } @@ -877,10 +902,8 @@ QemuCocoaView *cocoaView; NSOpenPanel *op =3D [[NSOpenPanel alloc] init]; [op setPrompt:@"Boot image"]; [op setMessage:@"Select the disk image you want to boot.\n\nHit = the \"Cancel\" button to quit"]; - NSArray *filetypes =3D [NSArray arrayWithObjects:@"img", = @"iso", @"dmg", - @"qcow", @"qcow2", @"cloop", @"vmdk", = nil]; #if (MAC_OS_X_VERSION_MAX_ALLOWED >=3D MAC_OS_X_VERSION_10_6) - [op setAllowedFileTypes:filetypes]; + [op setAllowedFileTypes:supportedImageFileTypes]; [op beginSheetModalForWindow:normalWindow completionHandler:^(NSInteger returnCode) { [self openPanelDidEnd:op @@ -1047,6 +1070,61 @@ QemuCocoaView *cocoaView; qmp_system_powerdown(NULL); } =20 +/* Ejects the media. + * Uses sender's tag to figure out the device to eject. + */ +- (void)ejectDeviceMedia:(id)sender +{ + NSString * drive; + drive =3D [sender representedObject]; + if(drive =3D=3D nil) { + NSBeep(); + QEMU_Alert(@"Failed to find drive to eject!"); + 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 represented object 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(); + QEMU_Alert(@"Could not find drive!"); + return; + } + + /* Display the file open dialog */ + NSOpenPanel * openPanel; + openPanel =3D [NSOpenPanel openPanel]; + [openPanel setCanChooseFiles: YES]; + [openPanel setAllowsMultipleSelection: NO]; + [openPanel setAllowedFileTypes: supportedImageFileTypes]; + if([openPanel runModal] =3D=3D NSFileHandlingPanelOKButton) { + NSString * file =3D [[[openPanel URLs] objectAtIndex: 0] path]; + if(file =3D=3D nil) { + NSBeep(); + QEMU_Alert(@"Failed to convert URL to file path!"); + return; + } + + Error *err =3D NULL; + qmp_change_blockdev([drive cStringUsingEncoding: = NSASCIIStringEncoding], + [file cStringUsingEncoding: = NSASCIIStringEncoding], + "raw", + &err); + handleAnyDeviceErrors(err); + } +} + @end =20 =20 @@ -1260,6 +1338,72 @@ static void add_console_menu_entries(void) } } =20 +/* Make menu items for all removable devices. + * Each device is given an 'Eject' and 'Change' menu item. + */ +static void addRemovableDevicesMenuItems() +{ + NSMenu *menu; + NSMenuItem *menuItem; + BlockInfoList *currentDevice, *pointerToFree; + NSString *deviceName; + + currentDevice =3D qmp_query_block(NULL); + pointerToFree =3D currentDevice; + if(currentDevice =3D=3D NULL) { + NSBeep(); + QEMU_Alert(@"Failed to query for block devices!"); + return; + } + + menu =3D [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; + + // Add a separator between related groups of menu items + [menu addItem:[NSMenuItem separatorItem]]; + + // Set the attributes to the "Removable Media" menu item + NSString *titleString =3D @"Removable Media"; + NSMutableAttributedString *attString=3D[[NSMutableAttributedString = alloc] initWithString:titleString]; + NSColor *newColor =3D [NSColor blackColor]; + NSFontManager *fontManager =3D [NSFontManager sharedFontManager]; + NSFont *font =3D [fontManager fontWithFamily:@"Helvetica" + = traits:NSBoldFontMask|NSItalicFontMask + weight:0 + size:14]; + [attString addAttribute:NSFontAttributeName value:font = range:NSMakeRange(0, [titleString length])]; + [attString addAttribute:NSForegroundColorAttributeName = value:newColor range:NSMakeRange(0, [titleString length])]; + [attString addAttribute:NSUnderlineStyleAttributeName = value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString = length])]; + + // Add the "Removable Media" menu item + menuItem =3D [NSMenuItem new]; + [menuItem setAttributedTitle: attString]; + [menuItem setEnabled: NO]; + [menu addItem: menuItem]; + + /* Loop thru all the block devices in the emulator */ + while (currentDevice) { + deviceName =3D [[NSString stringWithFormat: @"%s", = currentDevice->value->device] retain]; + + if(currentDevice->value->removable) { + 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; + } + qapi_free_BlockInfoList(pointerToFree); +} + void cocoa_display_init(DisplayState *ds, int full_screen) { COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); @@ -1283,4 +1427,10 @@ void cocoa_display_init(DisplayState *ds, int = full_screen) * menu entries for them. */ add_console_menu_entries(); + + /* Give all removable devices a menu item. + * Has to be called after QEMU has started to + * find out what removable devices it has. + */ + addRemovableDevicesMenuItems(); } --=20 1.7.5.4 --Apple-Mail-1-759491531 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
menu = items.

Signed-off-by: John Arbuckle = <programmingkidx@gmail.com>= ;

---
Removed depreciated code from = QEMU_Alert().
Unified supported image file extensions into one = array.
Replaced depreciated filename method for URLs = method in NSOpenPanel code.
Replaced free() with qapi_free_BlockInfoList().


diff --git a/ui/cocoa.m = b/ui/cocoa.m
index 559058b..5abbea7 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_5
 #define MAC_OS_X_VERSION_10_5 1050
@@ = -67,6 +68,7 @@ int gArgc;
 char **gArgv;
 NSTextField = *pauseLabel;
+NSArray * supportedImageFileTypes;

 

 int keymap[] =3D
@@ = -242,7 +244,24 @@ static int cocoa_keycode_to_qemu(int = keycode)
     return = keymap[keycode];
 }

+/* = Displays an alert dialog box with the specified message */
+    NSAlert *alert;
+    [alert = setMessageText: message];
+    [alert = runModal];
+}

 

+/* Handles any errors that happen = with a device transaction */
+static void = handleAnyDeviceErrors(Error * err)
+{
+    if (err) = {
+              =                     =     encoding: NSASCIIStringEncoding]);
+}

 

 /*
  = ------------------------------------------------------
@@ = -808,6 +827,8 @@ QemuCocoaView *cocoaView;
 - = (void)removePause;
 - (void)restartQEMU:(id)sender;
+- = (void)ejectDeviceMedia:(id)sender;
+- = (void)changeDeviceMedia:(id)sender;
 @end

 

@@ = -854,6 +875,10 @@ QemuCocoaView *cocoaView;
       =   [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: = 90]];
         [pauseLabel = setTextColor: [NSColor blackColor]];
         = [pauseLabel sizeToFit];
+
+        // set = the supported image file types that can be opened
+         =                     =     @"qcow", @"qcow2", @"cloop", @"vmdk", nil];
     return = self;
 }
@@ -877,10 +902,8 @@ QemuCocoaView = *cocoaView;
         NSOpenPanel *op =3D= [[NSOpenPanel alloc] init];
         [op = setPrompt:@"Boot image"];
         [op = setMessage:@"Select the disk image you want to boot.\n\nHit the = \"Cancel\" button to quit"];
-        NSArray = *filetypes =3D [NSArray arrayWithObjects:@"img", @"iso", = @"dmg",
-               =                   @"qcow", = @"qcow2", @"cloop", @"vmdk", nil];
 #if = (MAC_OS_X_VERSION_MAX_ALLOWED >=3D MAC_OS_X_VERSION_10_6)
+        [op = setAllowedFileTypes:supportedImageFileTypes];
       =       completionHandler:^(NSInteger returnCode)
@@ -1047,6 +1070,61 @@ QemuCocoaView = *cocoaView;
     = qmp_system_powerdown(NULL);
 }

 

+/* = Ejects the media.
+ * Uses sender's tag to figure out the device to = eject.
+ */
+- = (void)ejectDeviceMedia:(id)sender
+{
+    NSString * = drive;
+    drive =3D [sender = representedObject];
+    if(drive =3D=3D nil) {
+        = QEMU_Alert(@"Failed to find drive to eject!");
+    }
+    Error *err =3D NULL;
+
+/* Displays a dialog box asking the user to select = an image file to load.
+ * Uses sender's represented object value to = figure out which drive to use.
+ */
+- = (void)changeDeviceMedia:(id)sender
+{
+    /* Find the = drive name */
+    NSString * drive;
+        = NSBeep();
+        QEMU_Alert(@"Could not = find drive!");
+        return;
+
+    /* Display the file open dialog = */
+    openPanel =3D = [NSOpenPanel openPanel];
+    [openPanel = setCanChooseFiles: YES];
+    [openPanel = setAllowsMultipleSelection: NO];
+    [openPanel = setAllowedFileTypes: supportedImageFileTypes];
+        NSString = * file =3D [[[openPanel URLs] objectAtIndex: 0] path];
+          =   return;
+        }
+        Error *err =3D = NULL;
+        = qmp_change_blockdev([drive cStringUsingEncoding: = NSASCIIStringEncoding],
+          =                   [file = cStringUsingEncoding: NSASCIIStringEncoding],
+          =                   = &err);
+        = handleAnyDeviceErrors(err);
+    }
+
 @end

 

 

@@ -1260,6 +1338,72 @@ static void = add_console_menu_entries(void)
     }
 

+/* Make menu items for all removable = devices.
+ * Each device is given an 'Eject' and 'Change' = menu item.
+ */
+static void = addRemovableDevicesMenuItems()
+{
+    NSMenu = *menu;
+    NSMenuItem *menuItem;
+
+    currentDevice =3D= qmp_query_block(NULL);
+    pointerToFree =3D = currentDevice;
+    if(currentDevice =3D=3D NULL) = {
+        = QEMU_Alert(@"Failed to query for block devices!");
+    }
+    menu =3D [[[NSApp mainMenu] = itemWithTitle:@"Machine"] submenu];
+
+    // Add a separator = between related groups of menu items
+    [menu = addItem:[NSMenuItem separatorItem]];
+
+    // Set the attributes = to the "Removable Media" menu item
+    NSString *titleString = =3D @"Removable Media";
+    = NSMutableAttributedString *attString=3D[[NSMutableAttributedString = alloc] initWithString:titleString];
+    NSColor *newColor =3D = [NSColor blackColor];
+    NSFontManager *fontManager =3D = [NSFontManager sharedFontManager];
+    NSFont *font =3D = [fontManager fontWithFamily:@"Helvetica"
+        =                     =               = traits:NSBoldFontMask|NSItalicFontMask
+        =                     =               weight:0
+    [attString = addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, = [titleString length])];
+    [attString = addAttribute:NSForegroundColorAttributeName value:newColor = range:NSMakeRange(0, [titleString length])];
+
+    // Add the "Removable Media" menu = item
+    menuItem =3D [NSMenuItem = new];
+    [menuItem setAttributedTitle: = attString];
+    [menuItem setEnabled: NO];
+
+    /* Loop thru = all the block devices in the emulator */
+    while = (currentDevice) {
+        deviceName =3D = [[NSString stringWithFormat: @"%s", currentDevice->value->device] = retain];
+
+        = if(currentDevice->value->removable) {
+        =     menuItem =3D [[NSMenuItem alloc] initWithTitle: [NSString = stringWithFormat: @"Change %s...", = currentDevice->value->device]
+          =                     =                     = 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];
+        }
+    }
+
 void cocoa_display_init(DisplayState *ds, int = full_screen)
 {
     = COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
@@ = -1283,4 +1427,10 @@ void cocoa_display_init(DisplayState *ds, int = full_screen)
      * menu entries for = them.
      */
+    /* Give all removable devices a menu = item.
+     * Has to be called after QEMU has = started to
+     * find out what removable devices = it has.
+     */
+    = addRemovableDevicesMenuItems();
 }
-- 

= --Apple-Mail-1-759491531--