Skip to contentSkip to conversation

ui/cocoa.m: Adds console items to the View menu

PATCH In review Programmingkid 2 participants 6 messages May 11, 2015 – May 17, 2015

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 ui/cocoa.m: Adds console items to the View menu

Adds any console that is available to the current emulator as a menu item under the View menu.

---
Removed the initConsoleVariables() function.
Removed global variables.
Rewrote how console menu items are added.

 ui/cocoa.m |   50 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 42 insertions(+), 8 deletions(-)
ui/cocoa.m +42 -8
index d37c29b..b4b7adb 100644
@@ -801,6 +801,7 @@ QemuCocoaView *cocoaView;
 - (void)toggleFullScreen:(id)sender;
 - (void)showQEMUDoc:(id)sender;
 - (void)showQEMUTec:(id)sender;
+- (void)displayConsole:(id)sender;
 @end
 
 @implementation QemuCocoaAppController
@@ -943,8 +944,14 @@ QemuCocoaView *cocoaView;
     [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
         [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
 }
-@end
 
+/* Displays the console on the screen */
+- (void)displayConsole:(id)sender
+{
+    console_select([sender tag]);
+}
+
+@end
 
 
 int main (int argc, const char * argv[]) {
@@ -1003,13 +1010,6 @@ int main (int argc, const char * argv[]) {
     [[NSApp mainMenu] addItem:menuItem];
     [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
 
-    // View menu
-    menu = [[NSMenu alloc] initWithTitle:@"View"];
-    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
-    menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
-    [menuItem setSubmenu:menu];
-    [[NSApp mainMenu] addItem:menuItem];
-
     // Window menu
     menu = [[NSMenu alloc] initWithTitle:@"Window"];
     [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
@@ -1116,6 +1116,37 @@ static const DisplayChangeListenerOps dcl_ops = {
     .dpy_refresh = cocoa_refresh,
 };
 
+// Returns a name for a given console
+static NSString * getConsoleName(QemuConsole * console)
+{
+    return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
+}
+
+// Creates the view menu
+static void create_view_menu()
+{
+    NSMenu * menu;
+    NSMenuItem * menuItem;
+    int index = 0;
+
+    menu = [[NSMenu alloc] initWithTitle:@"View"];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
+    [menu addItem:[NSMenuItem separatorItem]]; //Separator
+
+    // Give each console its own menu item in the View menu
+    while(qemu_console_lookup_by_index(index) != NULL) {
+        menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
+                                               action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
+        [menuItem setTag: index];
+        [menu addItem: menuItem];
+        index++;
+    }
+
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
+    [menuItem setSubmenu:menu];
+    [[NSApp mainMenu] insertItem: menuItem atIndex: 1]; // insert View menu after Application menu
+}
+
 void cocoa_display_init(DisplayState *ds, int full_screen)
 {
     COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
@@ -1128,4 +1159,7 @@ void cocoa_display_init(DisplayState *ds, int full_screen)
 
     // register cleanup function
     atexit(cocoa_cleanup);
+
+    // QEMU needs to be running first before we can create the view menu
+    create_view_menu();
 }
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 3 lines · Programmingkid

Adds any console that is available to the current emulator as a menu item under the View menu.

Thanks; I think this version looks pretty good. A minor improvement:

Quoted 13 lines
ui/cocoa.m +13 -0
+static void create_view_menu()
+{
+    NSMenu * menu;
+    NSMenuItem * menuItem;
+    int index = 0;
+
+    menu = [[NSMenu alloc] initWithTitle:@"View"];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
+    [menu addItem:[NSMenuItem separatorItem]]; //Separator
+
+    // Give each console its own menu item in the View menu
+    while(qemu_console_lookup_by_index(index) != NULL) {
+        menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))

ui/cocoa.m static const DisplayChangeListenerOps dcl_ops = { · line 1148

Quoted 10 lines
ui/cocoa.m +10 -0
+                                               action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
+        [menuItem setTag: index];
+        [menu addItem: menuItem];
+        index++;
+    }
+
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
+    [menuItem setSubmenu:menu];
+    [[NSApp mainMenu] insertItem: menuItem atIndex: 1]; // insert View menu after Application menu
+}

Rather than creating the whole menu here, and then having to use awkward hard-coded indexes to decide where to put it in the menubar, I think it would be nicer to leave the creation of the menu (and its fixed entries) where it is and just do the addition of new items here. You can get the View menu with:

    menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];

and then just [menu addItem ... ] the new items to it.

I also had to tweak the patch a bit to get it to apply on top of the fullscreen patches, so I went ahead and made that change, and applied the resulting patch to my cocoa.next branch in
https://git.linaro.org/people/peter.maydell/qemu-arm.git

Let me know if you'd rather do something else.

thanks
-- PMM

#2 lore raw
Quoted 48 lines · Peter Maydell
Quoted 3 lines · Programmingkid

Adds any console that is available to the current emulator as a menu item under the View menu.

Thanks; I think this version looks pretty good. A minor improvement:

Quoted 13 lines
ui/cocoa.m +13 -0
+static void create_view_menu()
+{
+    NSMenu * menu;
+    NSMenuItem * menuItem;
+    int index = 0;
+
+    menu = [[NSMenu alloc] initWithTitle:@"View"];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
+    [menu addItem:[NSMenuItem separatorItem]]; //Separator
+
+    // Give each console its own menu item in the View menu
+    while(qemu_console_lookup_by_index(index) != NULL) {
+        menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))

ui/cocoa.m static const DisplayChangeListenerOps dcl_ops = { · line 1148

Quoted 10 lines
ui/cocoa.m +10 -0
+                                               action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
+        [menuItem setTag: index];
+        [menu addItem: menuItem];
+        index++;
+    }
+
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
+    [menuItem setSubmenu:menu];
+    [[NSApp mainMenu] insertItem: menuItem atIndex: 1]; // insert View menu after Application menu
+}

Rather than creating the whole menu here, and then having to use awkward hard-coded indexes to decide where to put it in the menubar, I think it would be nicer to leave the creation of the menu (and its fixed entries) where it is and just do the addition of new items here. You can get the View menu with:

   menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];

and then just [menu addItem ... ] the new items to it.

I also had to tweak the patch a bit to get it to apply on top of the fullscreen patches, so I went ahead and made that change, and applied the resulting patch to my cocoa.next branch in
https://git.linaro.org/people/peter.maydell/qemu-arm.git

Quoted 4 lines

Let me know if you'd rather do something else.

thanks
-- PMM

What you did looks good.

You know how there's a "Zoom To Fit" menu item. What if we added a mutually exclusive menu item called "Resize To Fit"? This menu item would instead adjust the host screen's resolution to match that of the guest screen's resolution. It can be implemented in its own patch if you like the idea.

#3 lore raw
Quoted 5 lines · Programmingkid

You know how there's a "Zoom To Fit" menu item. What if we added a mutually exclusive menu item called "Resize To Fit"? This menu item would instead adjust the host screen's resolution to match that of the guest screen's resolution. It can be implemented in its own patch if you like the idea.

Do any other VM/emulator type programs for OSX do that? It sounds rather odd -- wouldn't we run into the same "changing resolution makes OSX move all your other windows around" problems we had with an earlier version of the fullscreen patches?

-- PMM

#4 lore raw
Quoted 8 lines · Peter Maydell
Quoted 5 lines · Programmingkid

You know how there's a "Zoom To Fit" menu item. What if we added a mutually exclusive menu item called "Resize To Fit"? This menu item would instead adjust the host screen's resolution to match that of the guest screen's resolution. It can be implemented in its own patch if you like the idea.

Do any other VM/emulator type programs for OSX do that?

Connectix Virtual PC 3.

Quoted 4 lines

It sounds
rather odd -- wouldn't we run into the same "changing resolution makes OSX move all your other windows around" problems we had with an earlier version of the fullscreen patches?

It would cause those problems, but shouldn't the user decide what is and isn't acceptable? Some users might like the convenience of having QEMU do all the work to make the screen look good.

I think SDL solved this problem some how. I was using a fullscreen program that used SDL and it managed to change the resolution without messing up all the windows. So I think there is a way to solve this problem. Maybe someone out there might be willing to share...

#5 lore raw
Quoted 4 lines · Programmingkid

I think SDL solved this problem some how. I was using a fullscreen program that used SDL and it managed to change the resolution without messing up all the windows. So I think there is a way to solve this problem. Maybe someone out there might be willing to share...

Parallels also manages to let the guest work in non-standard resolutions without messing up the main screen resolution/window placement, so yeah, it ought to be possible. I have no idea how, though.

-- PMM