diff mbox

ui/cocoa.m: Add real CDROM menu item

Message ID A454BEC8-3B89-4811-A15C-7222BD6603DD@gmail.com
State New
Headers show

Commit Message

Programmingkid Sept. 26, 2015, 3:01 a.m. UTC
Add a menu item to the Machine menu called "Use Real CDROM". It gives the user
the ability to use a real CDROM disc with QEMU by simply selecting a menu item.

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

---
 ui/cocoa.m |   87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)

Comments

Peter Maydell Oct. 13, 2015, 8:44 p.m. UTC | #1
On 26 September 2015 at 04:01, Programmingkid <programmingkidx@gmail.com> wrote:
> Add a menu item to the Machine menu called "Use Real CDROM". It gives the user
> the ability to use a real CDROM disc with QEMU by simply selecting a menu item.
>
> Signed-off-by: John Arbuckle <programmingkidx@gmail.com>

This feature is not present in any of our other UI frontends,
and the implementation makes assumptions about the names of
block devices in the models (ie that if it has "cd" in the name
anywhere it's a CD). I also don't have a mac with a CD drive
so I have no way of testing it.

I'm afraid that I think that this comes under the heading of
"features that should be in the VM management layer, not QEMU
itself", so I would prefer not to take it into QEMU.

thanks
-- PMM
Programmingkid Oct. 13, 2015, 10:16 p.m. UTC | #2
On Oct 13, 2015, at 4:44 PM, Peter Maydell wrote:

> On 26 September 2015 at 04:01, Programmingkid <programmingkidx@gmail.com> wrote:
>> Add a menu item to the Machine menu called "Use Real CDROM". It gives the user
>> the ability to use a real CDROM disc with QEMU by simply selecting a menu item.
>> 
>> Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
> 
> This feature is not present in any of our other UI frontends,
> and the implementation makes assumptions about the names of
> block devices in the models (ie that if it has "cd" in the name
> anywhere it's a CD). I also don't have a mac with a CD drive
> so I have no way of testing it.
> 
> I'm afraid that I think that this comes under the heading of
> "features that should be in the VM management layer, not QEMU
> itself", so I would prefer not to take it into QEMU.

Mac OS X users don't have any VM management layer. That is pretty much
a Linux-only feature. What we do have is the cocoa interface.
diff mbox

Patch

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 334e6f6..7586ba3 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -829,6 +829,7 @@  QemuCocoaView *cocoaView;
 - (void)powerDownQEMU:(id)sender;
 - (void)ejectDeviceMedia:(id)sender;
 - (void)changeDeviceMedia:(id)sender;
+- (void)useRealCDROM:(id)sender;
 @end
 
 @implementation QemuCocoaAppController
@@ -1125,6 +1126,16 @@  QemuCocoaView *cocoaView;
     }
 }
 
+/* Has QEMU use the real CDROM drive */
+- (void)useRealCDROM:(id)sender
+{
+    Error *err = NULL;
+    const char *device = [[sender representedObject] cStringUsingEncoding:
+                                                         NSASCIIStringEncoding];
+    qmp_change_blockdev(device, "/dev/cdrom", "raw", &err);
+    handleAnyDeviceErrors(err);
+}
+
 @end
 
 
@@ -1338,6 +1349,79 @@  static void add_console_menu_entries(void)
     }
 }
 
+/*
+ * Determines if the emulator has a CDROM drive.
+ * Can only be called after the addRemovableDevicesMenuItems() function has been
+ * called.
+ */
+static bool emulatorHasCDROM(void)
+{
+    int index;
+    NSString *title;
+    NSArray *menu_item_array;
+    NSMenu *menu;
+    menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
+    menu_item_array = [menu itemArray];
+    if (!menu_item_array) {
+        NSBeep();
+        QEMU_Alert(@"Failed to obtain Machine menu items!");
+        return false;
+    }
+    /* Start at the first removable device menu item */
+    for (index = 7; index < [menu_item_array count]; index++) {
+        title = [[menu_item_array objectAtIndex: index] title];
+        if ([title rangeOfString: @"cd"].location != NSNotFound) {
+            return true;
+        }
+    }
+    return false;
+}
+
+/* Find the CDROM's name */
+static NSString *getCDName(void)
+{
+    int index, end_of_string_index;
+    NSString *title, *return_value;
+    NSArray *menu_item_array;
+    NSMenu *menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
+    menu_item_array = [menu itemArray];
+    if (!menu_item_array) {
+        NSBeep();
+        QEMU_Alert(@"Failed to obtain Machine menu items!");
+        return nil;
+    }
+    /* Start at the first removable device menu item */
+    for (index = 7; index < [menu_item_array count]; index++) {
+        title = [[menu_item_array objectAtIndex: index] title];
+        if ([title rangeOfString: @"cd"].location != NSNotFound) {
+            return_value = [[title componentsSeparatedByString: @" "]
+                                                              objectAtIndex: 1];
+            end_of_string_index = [return_value rangeOfString: @"."].location;
+            return_value = [return_value substringToIndex: end_of_string_index];
+            return return_value;
+        }
+    }
+    return nil;
+}
+
+/* Add the "Use Real CDROM" menu item */
+static void addRealCDMenuItem(void)
+{
+    NSMenu *machine_menu;
+    NSMenuItem *menu_item;
+    machine_menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
+    if (!machine_menu) {
+        NSBeep();
+        QEMU_Alert(@"Could not find Machine Menu!");
+        return;
+    }
+    menu_item = [[NSMenuItem alloc] initWithTitle: @"Use Real CDROM"
+                                          action: @selector(useRealCDROM:)
+                                   keyEquivalent: @""];
+    [menu_item setRepresentedObject: getCDName()];
+    [machine_menu insertItem: menu_item atIndex: 7];
+}
+
 /* Make menu items for all removable devices.
  * Each device is given an 'Eject' and 'Change' menu item.
  */
@@ -1433,4 +1517,7 @@  void cocoa_display_init(DisplayState *ds, int full_screen)
      * find out what removable devices it has.
      */
     addRemovableDevicesMenuItems();
+    if(emulatorHasCDROM()) {
+        addRealCDMenuItem();
+    }
 }