Received: from eggs.gnu.org ([2001:4830:134:3::10]:59555) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWwMU-0004kO-CU for qemu-devel@nongnu.org; Tue, 01 Sep 2015 20:57:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZWwMR-0003mB-1q for qemu-devel@nongnu.org; Tue, 01 Sep 2015 20:57:14 -0400 Received: from mail-qg0-f49.google.com ([209.85.192.49]:35418) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWwMQ-0003Nh-Qx for qemu-devel@nongnu.org; Tue, 01 Sep 2015 20:57:11 -0400 Received: by qgt47 with SMTP id 47so9104468qgt.2 for ; Tue, 01 Sep 2015 17:56:04 -0700 (PDT) From: Programmingkid Content-Type: multipart/alternative; boundary=Apple-Mail-4--1008306615 Date: Tue, 1 Sep 2015 20:56:01 -0400 Message-Id: <65D87A2B-B94F-4236-A9BB-D0F0B7FCF96B@gmail.com> Mime-Version: 1.0 (Apple Message framework v1084) Subject: [Qemu-devel] [PATCH] ui/cocoa.m: Add Mount image file menu item List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell , qemu-devel qemu-devel --Apple-Mail-4--1008306615 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Add "Mount Image File..." and a "Eject Image File" menu items to cocoa interface. This patch makes sharing files between the host and the guest user-friendly. The "Mount Image File..." menu item displays a dialog box having the user pick an image file to use in QEMU. The image file is setup as a USB flash drive. The user can do the equivalent of removing the flash drive by selecting the file in the "Eject Image File" submenu. Signed-off-by: John Arbuckle --- ui/cocoa.m | 212 = +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 210 insertions(+), 1 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 334e6f6..6c0ec18 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -52,6 +52,9 @@ #endif =20 #define cgrect(nsrect) (*(CGRect *)&(nsrect)) +#define USB_DISK_ID "USB_DISK" +#define EJECT_IMAGE_FILE_TAG 2099 =20 typedef struct { int width; @@ -263,6 +266,43 @@ static void handleAnyDeviceErrors(Error * err) } } =20 +/* Sends a command to the monitor console */ +static void sendMonitorCommand(const char * commandString) +{ + int index; + char * consoleName; + static QemuConsole *monitor; + + /* If the monitor console hasn't been found yet */ + if(!monitor) { + index =3D 0; + /* Find the monitor console */ + while (qemu_console_lookup_by_index(index) !=3D NULL) { + consoleName =3D = qemu_console_get_label(qemu_console_lookup_by_index(index)); + if(strstr(consoleName, "monitor")) { + monitor =3D qemu_console_lookup_by_index(index); + break; + } + index++; + } + } + + /* If the monitor console was not found */ + if(!monitor) { + NSBeep(); + QEMU_Alert(@"Failed to find the monitor console!"); + return; + } + + /* send each letter in the commandString to the monitor */ + for (index =3D 0; index < strlen(commandString); index++) { + kbd_put_keysym_console(monitor, commandString[index]); + } + + /* simulate the user pushing the return key */ + kbd_put_keysym_console(monitor, '\n'); +} + /* ------------------------------------------------------ QemuCocoaView @@ -585,6 +625,7 @@ QemuCocoaView *cocoaView; // forward command key combos to the host UI unless the = mouse is grabbed if (!isMouseGrabbed && ([event modifierFlags] & = NSCommandKeyMask)) { [NSApp sendEvent:event]; + printf("Returning\n"); return; } =20 @@ -829,6 +870,9 @@ QemuCocoaView *cocoaView; - (void)powerDownQEMU:(id)sender; - (void)ejectDeviceMedia:(id)sender; - (void)changeDeviceMedia:(id)sender; +- (void)mountImageFile:(id)sender; +- (void)ejectImageFile:(id)sender; +- (void)updateEjectImageMenuItems; @end =20 @implementation QemuCocoaAppController @@ -1125,6 +1169,150 @@ QemuCocoaView *cocoaView; } } =20 +/* Displays a dialog box asking the user for an image file to mount */ +- (void)mountImageFile:(id)sender +{ + /* 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; + } + + static int usbDiskCount; // used for the ID + char *commandBuffer, *fileName, *idString, *fileNameHint; + NSString *buffer; + const int fileNameHintSize =3D 10; + + fileName =3D g_strdup_printf("%s", + [file cStringUsingEncoding: = NSASCIIStringEncoding]); + buffer =3D [file lastPathComponent]; + buffer =3D [buffer stringByDeletingPathExtension]; + if([buffer length] > fileNameHintSize) { + buffer =3D [buffer substringToIndex: fileNameHintSize]; + } + fileNameHint =3D g_strdup_printf("%s", + [buffer cStringUsingEncoding: = NSASCIIStringEncoding]); + idString =3D g_strdup_printf("%s_%s_%d", USB_DISK_ID, = fileNameHint, usbDiskCount); + commandBuffer =3D g_strdup_printf("drive_add 0 = if=3Dnone,id=3D%s,file=3D%s", + idString, = fileName); + sendMonitorCommand(commandBuffer); + commandBuffer =3D g_strdup_printf("device_add usb-storage," + "id=3D%s,drive=3D%s", = idString, idString); + sendMonitorCommand(commandBuffer); + [self updateEjectImageMenuItems]; + usbDiskCount++; + g_free(fileName); + g_free(fileNameHint); + g_free(idString); + g_free(commandBuffer); + } +} + +/* Removes an image file from QEMU */ +- (void)ejectImageFile:(id) sender +{ + char *commandBuffer; + NSString *imageFileID; + + imageFileID =3D [sender representedObject]; + if (imageFileID =3D=3D nil) { + NSBeep(); + QEMU_Alert(@"Could not find image file's ID!"); + return; + } + + commandBuffer =3D g_strdup_printf("drive_del %s", + [imageFileID cStringUsingEncoding: = NSASCIIStringEncoding]); + sendMonitorCommand(commandBuffer); + g_free(commandBuffer); + + commandBuffer =3D g_strdup_printf("device_del %s", + [imageFileID cStringUsingEncoding: = NSASCIIStringEncoding]); + sendMonitorCommand(commandBuffer); + g_free(commandBuffer); + + [self updateEjectImageMenuItems]; +} + +/* Gives each mounted image file an eject menu item */ +- (void) updateEjectImageMenuItems +{ + NSMenu *machineMenu; + machineMenu =3D [[[NSApp mainMenu] itemWithTitle:@"Machine"] = submenu]; + + /* Remove old menu items*/ + NSMenu * ejectSubmenu; + ejectSubmenu =3D [[machineMenu itemWithTag: EJECT_IMAGE_FILE_TAG] = submenu]; + if(!ejectSubmenu) { + NSBeep(); + QEMU_Alert(@"Failed to find eject submenu!"); + return; + } + int index; + for (index =3D 0; index < [ejectSubmenu numberOfItems]; index++) { + [ejectSubmenu removeItemAtIndex: 0]; + } + /* Needed probably because of a bug with cocoa */ + if ([ejectSubmenu numberOfItems] > 0) { + [ejectSubmenu removeItemAtIndex: 0]; + } + + BlockInfoList *currentDevice; + currentDevice =3D qmp_query_block(NULL); + + NSString *fileName, *deviceName; + NSMenuItem *ejectFileMenuItem; /* Used with each mounted image = file */ + + /* Look for mounted image files */ + while(currentDevice) { + if (!currentDevice->value || !currentDevice->value->inserted + || = !currentDevice->value->inserted->file) { + currentDevice =3D currentDevice->next; + continue; + } + + /* if the device's name is the generated ID */ + if (!strstr(currentDevice->value->device, USB_DISK_ID)) { + currentDevice =3D currentDevice->next; + continue; + } + + fileName =3D [NSString stringWithFormat: @"%s", = currentDevice->value->inserted->file]; + fileName =3D [fileName lastPathComponent]; /* To obtain only = the file name */ + + ejectFileMenuItem =3D [[NSMenuItem alloc] initWithTitle: = [NSString stringWithFormat: @"Eject %@", fileName] + action: = @selector(ejectImageFile:) + keyEquivalent: @""]; + [ejectSubmenu addItem: ejectFileMenuItem]; + deviceName =3D [NSString stringWithFormat: @"%s", = currentDevice->value->device]; + [ejectFileMenuItem setRepresentedObject: deviceName]; + [ejectFileMenuItem autorelease]; + currentDevice =3D currentDevice->next; + } + + /* Add default menu item if submenu is empty */ + if([ejectSubmenu numberOfItems] =3D=3D 0) { + + /* Create the default menu item */ + NSMenuItem *emptyMenuItem; + emptyMenuItem =3D [NSMenuItem new]; + [emptyMenuItem setTitle: @"No items available"]; + [emptyMenuItem setEnabled: NO]; + + /* Add the default menu item to the submenu */ + [ejectSubmenu addItem: emptyMenuItem]; + [emptyMenuItem release]; + } +} + @end =20 =20 @@ -1383,7 +1571,6 @@ static void addRemovableDevicesMenuItems() /* 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:) @@ -1402,6 +1589,29 @@ static void addRemovableDevicesMenuItems() currentDevice =3D currentDevice->next; } qapi_free_BlockInfoList(pointerToFree); + [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Mount Image = File..." action: @selector(mountImageFile:) keyEquivalent: @""] = autorelease]]; + + /* Create the eject menu item */ + NSMenuItem *ejectMenuItem; + ejectMenuItem =3D [NSMenuItem new]; + [ejectMenuItem setTitle: @"Eject Image File"]; + [ejectMenuItem setTag: EJECT_IMAGE_FILE_TAG]; + [menu addItem: ejectMenuItem]; + [ejectMenuItem autorelease]; + + /* Create the default menu item for the eject menu item's submenu*/ + NSMenuItem *emptyMenuItem; + emptyMenuItem =3D [NSMenuItem new]; + [emptyMenuItem setTitle: @"No items available"]; + [emptyMenuItem setEnabled: NO]; + [emptyMenuItem autorelease]; + + /* Add the default menu item to the submenu */ + NSMenu *submenu; + submenu =3D [NSMenu new]; + [ejectMenuItem setSubmenu: submenu]; + [submenu addItem: emptyMenuItem]; + [submenu autorelease]; } =20 void cocoa_display_init(DisplayState *ds, int full_screen) --=20 1.7.5.4 --Apple-Mail-4--1008306615 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
Add = "Mount Image File..." and a "Eject Image File" menu items to
cocoa = interface. This patch makes sharing files between the
host = and the guest user-friendly.

The = "Mount Image File..." menu item displays a dialog box having = the
user pick an image file to use in QEMU. The image = file is setup as
a USB flash drive. The user can do the equivalent = of removing the
flash drive by selecting the file in the "Eject = Image File" submenu.

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

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

diff --git a/ui/cocoa.m = b/ui/cocoa.m
index 334e6f6..6c0ec18 100644
--- = a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -52,6 +52,9 @@
 

 #define cgrect(nsrect) = (*(CGRect *)&(nsrect))
+#define USB_DISK_ID = "USB_DISK"
+#define EJECT_IMAGE_FILE_TAG 2099

 

     int = width;
@@ -263,6 +266,43 @@ static void = handleAnyDeviceErrors(Error * err)
     }
 

+/* Sends a command to the monitor = console */
+static void sendMonitorCommand(const char * = commandString)
+{
+    int index;
+    static QemuConsole = *monitor;
+
+    /* If the monitor console hasn't = been found yet */
+    if(!monitor) {
+        = /* Find the monitor console */
+        while = (qemu_console_lookup_by_index(index) !=3D NULL) {
+              =   monitor =3D qemu_console_lookup_by_index(index);
+            = }
+    }
+    /* If the monitor console was not = found */
+    if(!monitor) {
+        = QEMU_Alert(@"Failed to find the monitor console!");
+    }
+    /* send each letter in the = commandString to the monitor */
+    for (index =3D 0; = index < strlen(commandString); index++) {
+    }
+
+    /* simulate the = user pushing the return key */
+    = kbd_put_keysym_console(monitor, '\n');
+}
 /*
  = ------------------------------------------------------
@@ -585,6 +625,7 @@ QemuCocoaView = *cocoaView;
             // = forward command key combos to the host UI unless the mouse is = grabbed
             if = (!isMouseGrabbed && ([event modifierFlags] & = NSCommandKeyMask)) {
             =     [NSApp sendEvent:event];
+        =         printf("Returning\n");
             = }

 

@@ = -829,6 +870,9 @@ QemuCocoaView *cocoaView;
 - = (void)powerDownQEMU:(id)sender;
 - = (void)ejectDeviceMedia:(id)sender;
 - = (void)changeDeviceMedia:(id)sender;
+- = (void)mountImageFile:(id)sender;
+- = (void)ejectImageFile:(id)sender;
+- = (void)updateEjectImageMenuItems;
 @end

 

@@ = -1125,6 +1169,150 @@ QemuCocoaView *cocoaView;
 }

 

+/* = Displays a dialog box asking the user for an image file to mount = */
+{
+    /* Display the = file open dialog */
+    NSOpenPanel * openPanel;
+    if([openPanel = runModal] =3D=3D NSFileHandlingPanelOKButton) {
+        if(file = =3D=3D nil) {
+            = NSBeep();
+            = QEMU_Alert(@"Failed to convert URL to file path!");
+
+        static = int usbDiskCount;  // used for the ID
+        = char *commandBuffer, *fileName, *idString, *fileNameHint;
+
+        fileName =3D = g_strdup_printf("%s",
+              =               [file = cStringUsingEncoding: NSASCIIStringEncoding]);
+        buffer =3D [buffer = stringByDeletingPathExtension];
+        = if([buffer length] > fileNameHintSize) {
+        =     buffer =3D [buffer substringToIndex: = fileNameHintSize];
+        }
+              =           [buffer cStringUsingEncoding: = NSASCIIStringEncoding]);
+        idString = =3D g_strdup_printf("%s_%s_%d", USB_DISK_ID, fileNameHint, = usbDiskCount);
+        commandBuffer =3D = g_strdup_printf("drive_add 0 if=3Dnone,id=3D%s,file=3D%s",
+        = sendMonitorCommand(commandBuffer);
+        = commandBuffer =3D g_strdup_printf("device_add usb-storage,"
+ =                     =                     = "id=3D%s,drive=3D%s", idString, idString);
+        = sendMonitorCommand(commandBuffer);
+        [self = updateEjectImageMenuItems];
+        = usbDiskCount++;
+        = g_free(fileName);
+        = g_free(fileNameHint);
+        = g_free(idString);
+        = g_free(commandBuffer);
+    }
+}
+/* Removes an image file from QEMU */
+- = (void)ejectImageFile:(id) sender
+{
+    char = *commandBuffer;
+    NSString *imageFileID;
+    imageFileID =3D [sender = representedObject];
+    if (imageFileID =3D=3D nil) = {
+        = QEMU_Alert(@"Could not find image file's ID!");
+    }
+    commandBuffer =3D = g_strdup_printf("drive_del %s",
+          =           [imageFileID cStringUsingEncoding: = NSASCIIStringEncoding]);
+    = sendMonitorCommand(commandBuffer);
+    = g_free(commandBuffer);
+
+    commandBuffer =3D = g_strdup_printf("device_del %s",
+          =               [imageFileID = cStringUsingEncoding: NSASCIIStringEncoding]);
+
+    [self = updateEjectImageMenuItems];
+}
+
+/* Gives each mounted image = file an eject menu item */
+- (void) = updateEjectImageMenuItems
+{
+    NSMenu = *machineMenu;
+    machineMenu =3D [[[NSApp mainMenu] = itemWithTitle:@"Machine"] submenu];
+
+    /* Remove old menu = items*/
+    NSMenu * ejectSubmenu;
+    if(!ejectSubmenu) = {
+        = QEMU_Alert(@"Failed to find eject submenu!");
+    }
+    for (index =3D 0; = index < [ejectSubmenu numberOfItems]; index++) {
+    }
+     /* Needed = probably because of a bug with cocoa */
+    if = ([ejectSubmenu numberOfItems] > 0) {
+        = [ejectSubmenu removeItemAtIndex: 0];
+    }
+    BlockInfoList = *currentDevice;
+    currentDevice =3D = qmp_query_block(NULL);
+
+    NSString *fileName, = *deviceName;
+    NSMenuItem *ejectFileMenuItem;  = /* Used with each mounted image file */
+
+    /* Look for = mounted image files */
+    while(currentDevice) {
+        =                     =       || !currentDevice->value->inserted->file) = {
+          =   continue;
+        }
+        /* if the device's = name is the generated ID */
+        if = (!strstr(currentDevice->value->device, USB_DISK_ID)) {
+          =   continue;
+        }
+        fileName =3D [NSString = stringWithFormat: @"%s", = currentDevice->value->inserted->file];
+        ejectFileMenuItem =3D = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject = %@", fileName]
+              =                     =                 action: = @selector(ejectImageFile:)
+           =                     =             keyEquivalent: @""];
+        deviceName =3D = [NSString stringWithFormat: @"%s", = currentDevice->value->device];
+        = [ejectFileMenuItem setRepresentedObject: deviceName];
+        currentDevice =3D = currentDevice->next;
+    }
+    /* Add default menu item if submenu = is empty */
+    if([ejectSubmenu numberOfItems] =3D=3D= 0) {
+
+        /* Create the default = menu item */
+        NSMenuItem = *emptyMenuItem;
+        emptyMenuItem =3D = [NSMenuItem new];
+        [emptyMenuItem = setTitle: @"No items available"];
+        = [emptyMenuItem setEnabled: NO];
+
+        /* Add = the default menu item to the submenu */
+        = [ejectSubmenu addItem: emptyMenuItem];
+        = [emptyMenuItem release];
+    }
+
 @end

 

 

@@ -1383,7 +1571,6 @@ static void = addRemovableDevicesMenuItems()
     /* 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:)
@@ = -1402,6 +1589,29 @@ static void addRemovableDevicesMenuItems()
     }
+
+    /* Create the eject menu item = */
+    ejectMenuItem =3D= [NSMenuItem new];
+    [ejectMenuItem setTitle: @"Eject = Image File"];
+    [ejectMenuItem setTag: = EJECT_IMAGE_FILE_TAG];
+    [menu addItem: = ejectMenuItem];
+    [ejectMenuItem = autorelease];
+
+    /* Create the default menu item for = the eject menu item's submenu*/
+    NSMenuItem = *emptyMenuItem;
+    emptyMenuItem =3D [NSMenuItem = new];
+    [emptyMenuItem setTitle: @"No items = available"];
+    [emptyMenuItem setEnabled: = NO];
+    [emptyMenuItem = autorelease];
+
+    /* Add the default menu item to the = submenu */
+    NSMenu *submenu;
+    [ejectMenuItem = setSubmenu: submenu];
+    [submenu addItem: = emptyMenuItem];
+    [submenu autorelease];
 

 void = cocoa_display_init(DisplayState *ds, int full_screen)
1.7.5.4