From patchwork Mon May 18 16:23:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Programmingkid X-Patchwork-Id: 473512 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 15E5C1401B5 for ; Tue, 19 May 2015 02:24:37 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=dEAGwCPH; dkim-atps=neutral Received: from localhost ([::1]:41992 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YuNqE-0007Uf-RQ for incoming@patchwork.ozlabs.org; Mon, 18 May 2015 12:24:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50307) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YuNpm-00074I-LE for qemu-devel@nongnu.org; Mon, 18 May 2015 12:24:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YuNpi-0000EX-BO for qemu-devel@nongnu.org; Mon, 18 May 2015 12:24:06 -0400 Received: from mail-qg0-x242.google.com ([2607:f8b0:400d:c04::242]:33935) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YuNpi-0000EO-0o for qemu-devel@nongnu.org; Mon, 18 May 2015 12:24:02 -0400 Received: by qgdq107 with SMTP id q107so355352qgd.1 for ; Mon, 18 May 2015 09:24:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:subject:date:message-id:cc:to:mime-version; bh=AAXpewVbS4GBLJ1lufyBovEmS0ou3wfsKcdAYIjt2F0=; b=dEAGwCPHxv0G0qAgLYNvpi4bNl06K20xwDcAJryAL/T3+AR/zJmwyPGx9KqtfibPpR 2BS9poCZXheRGZusvzKhL+yFR3ca54ulZKoF7Qrl22AYFt0wZ3VFfjagbzgDK9iJgxbl bqrzZRnmafk9TpYloF9snyyzW95DoiN/ONMlJgQxWQrNUwmVY5XxENX5WZ3gQbivDNPz AFEOn36U8D8ymoy1kvshfeUlJI8zKhduVQMGDwymQIOXkiF86hWFqVVwg5fGyGJAeVKC 6Oc52YpKOrMZMSZuUi9JcRTkk8jvhYl2Ukk1hk4cRH80+6XWRdfhVldcTYIYh9lQp/Ii AlYw== X-Received: by 10.55.56.201 with SMTP id f192mr48416048qka.88.1431966241338; Mon, 18 May 2015 09:24:01 -0700 (PDT) Received: from [192.168.0.3] (d199-74-164-53.col.wideopenwest.com. [74.199.53.164]) by mx.google.com with ESMTPSA id w73sm5353976qkw.16.2015.05.18.09.23.59 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 18 May 2015 09:24:00 -0700 (PDT) From: Programmingkid Date: Mon, 18 May 2015 12:23:58 -0400 Message-Id: <5E25ED02-BA7A-4429-94FF-FB806D75AAA6@gmail.com> To: Peter Maydell Mime-Version: 1.0 (Apple Message framework v1084) X-Mailer: Apple Mail (2.1084) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::242 Cc: qemu-devel qemu-devel Subject: [Qemu-devel] [PATCH v2] ui/cocoa.m: Adds device menu items to Machine menu X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- Replace NSRunAlertPanel() with QEMU_Alert(). Free currentDevice variable after finished using it. Add "Removable Media" text to the top of devices menu items for easier identification. Replace depreciated code. ui/cocoa.m | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 140 insertions(+), 0 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 559058b..3acde50 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_5 #define MAC_OS_X_VERSION_10_5 1050 @@ -242,7 +243,27 @@ 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 = [NSAlert alertWithMessageText:message + defaultButton:@"OK" + alternateButton:nil + otherButton:nil + informativeTextWithFormat:@""]; + [alert runModal]; +} +/* 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); + } +} /* ------------------------------------------------------ @@ -808,6 +829,8 @@ QemuCocoaView *cocoaView; - (void)removePause; - (void)restartQEMU:(id)sender; - (void)powerDownQEMU:(id)sender; +- (void)ejectDeviceMedia:(id)sender; +- (void)changeDeviceMedia:(id)sender; @end @implementation QemuCocoaAppController @@ -1047,6 +1070,52 @@ 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 = [sender representedObject]; + if(drive == nil) { + NSBeep(); + QEMU_Alert(@"Failed to find drive to eject!"); + 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(); + QEMU_Alert(@"Could not find drive!"); + return; + } + + /* Display the file open dialog */ + NSOpenPanel * openPanel; + openPanel = [NSOpenPanel openPanel]; + [openPanel setCanChooseFiles: YES]; + [openPanel setAllowsMultipleSelection: NO]; + [openPanel setAllowedFileTypes: [NSArray arrayWithObjects: @"iso", @"cdr", @"img", @"dmg", nil]]; + if([openPanel runModal] == NSFileHandlingPanelOKButton) { + NSString * file = [[openPanel filenames] objectAtIndex: 0]; + Error *err = NULL; + qmp_change_blockdev([drive cStringUsingEncoding: NSASCIIStringEncoding], [file cStringUsingEncoding: NSASCIIStringEncoding], "raw", &err); + handleAnyDeviceErrors(err); + } +} + @end @@ -1260,6 +1329,71 @@ 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; + BlockInfoList *currentDevice; + NSString *deviceName; + + currentDevice = qmp_query_block(NULL); + if(currentDevice == NULL) { + NSBeep(); + QEMU_Alert(@"Failed to query for block devices!"); + return; + } + + menu = [[[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 = @"Removable Media"; + NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString]; + NSColor *newColor = [NSColor blackColor]; + NSFontManager *fontManager = [NSFontManager sharedFontManager]; + NSFont *font = [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 = [NSMenuItem new]; + [menuItem setAttributedTitle: attString]; + [menuItem setEnabled: NO]; + [menu addItem: menuItem]; + + /* Loop thru all the block devices in the emulator */ + while (currentDevice) { + deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain]; + + if(currentDevice->value->removable) { + 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; + } + free(currentDevice); +} + void cocoa_display_init(DisplayState *ds, int full_screen) { COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); @@ -1283,4 +1417,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(); }