qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] ui/cocoa.m: Add real CDROM menu item


From: Programmingkid
Subject: [Qemu-devel] [PATCH] ui/cocoa.m: Add real CDROM menu item
Date: Fri, 25 Sep 2015 23:01:06 -0400

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 <address@hidden>

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

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();
+    }
 }
-- 
1.7.5.4





reply via email to

[Prev in Thread] Current Thread [Next in Thread]