Skip to contentSkip to conversation

ui/cocoa: Show QEMU icon in the about window

PATCH In review Akihiko Odaki 3 participants 8 messages Mar 9, 2021 – Mar 11, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ui/cocoa: Show QEMU icon in the about window

---
 ui/cocoa.m | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
ui/cocoa.m +5 -9
index 0ef5fdf3b7a..d8eacea6d22 100644
@@ -39,6 +39,7 @@
 #include "qapi/qapi-commands-misc.h"
 #include "sysemu/blockdev.h"
 #include "qemu-version.h"
+#include "qemu/cutils.h"
 #include "qemu/main-loop.h"
 #include "qemu/module.h"
 #include <Carbon/Carbon.h>
@@ -1401,18 +1402,13 @@ - (void)make_about_window
     y = about_height - picture_height - 10;
     NSRect picture_rect = NSMakeRect(x, y, picture_width, picture_height);
 
-    /* Get the path to the QEMU binary */
-    NSString *binary_name = [NSString stringWithCString: gArgv[0]
-                                      encoding: NSASCIIStringEncoding];
-    binary_name = [binary_name lastPathComponent];
-    NSString *program_path = [[NSString alloc] initWithFormat: @"%@/%@",
-    [[NSBundle mainBundle] bundlePath], binary_name];
-
     /* Make the picture of QEMU */
     NSImageView *picture_view = [[NSImageView alloc] initWithFrame:
                                                      picture_rect];
-    NSImage *qemu_image = [[NSWorkspace sharedWorkspace] iconForFile:
-                                                         program_path];
+    char *qemu_image_path_c = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/512x512/apps/qemu.png");
+    NSString *qemu_image_path = [NSString stringWithUTF8String:qemu_image_path_c];
+    g_free(qemu_image_path_c);
+    NSImage *qemu_image = [[NSImage alloc] initWithContentsOfFile:qemu_image_path];
     [picture_view setImage: qemu_image];
     [picture_view setImageScaling: NSImageScaleProportionallyUpOrDown];
     [superView addSubview: picture_view];
Signature · 1 line
2.24.3 (Apple Git-128)
#1 lore raw

PATCH 2/2 ui/cocoa: Do not rely on the first argument

The first argument of the executable was used to get its path, but it is not reliable because the executer can specify any arbitrary string. Use the interfaces provided by QEMU and the platform to get those paths.

---
 ui/cocoa.m | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
ui/cocoa.m +15 -14
index d8eacea6d22..6e94301c0d6 100644
@@ -1414,20 +1414,21 @@ - (void)make_about_window
     [superView addSubview: picture_view];
 
     /* Make the name label */
-    x = 0;
-    y = y - 25;
-    int name_width = about_width, name_height = 20;
-    NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
-    NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
-    [name_label setEditable: NO];
-    [name_label setBezeled: NO];
-    [name_label setDrawsBackground: NO];
-    [name_label setAlignment: NSTextAlignmentCenter];
-    NSString *qemu_name = [[NSString alloc] initWithCString: gArgv[0]
-                                            encoding: NSASCIIStringEncoding];
-    qemu_name = [qemu_name lastPathComponent];
-    [name_label setStringValue: qemu_name];
-    [superView addSubview: name_label];
+    NSBundle *bundle = [NSBundle mainBundle];
+    if (bundle) {
+        x = 0;
+        y = y - 25;
+        int name_width = about_width, name_height = 20;
+        NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
+        NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
+        [name_label setEditable: NO];
+        [name_label setBezeled: NO];
+        [name_label setDrawsBackground: NO];
+        [name_label setAlignment: NSTextAlignmentCenter];
+        NSString *qemu_name = [[bundle executablePath] lastPathComponent];
+        [name_label setStringValue: qemu_name];
+        [superView addSubview: name_label];
+    }
 
     /* Set the version label's attributes */
     x = 0;
Signature · 1 line
2.24.3 (Apple Git-128)
#2 lore raw

Re: [PATCH 2/2] ui/cocoa: Do not rely on the first argument

Quoted 23 lines · Akihiko Odaki

The first argument of the executable was used to get its path, but it is not reliable because the executer can specify any arbitrary string. Use the interfaces provided by QEMU and the platform to get those paths.

---
ui/cocoa.m | 29 +++++++++++++++--------------

1 file changed, 15 insertions(+), 14 deletions(-)
ui/cocoa.m +0 -6
index d8eacea6d22..6e94301c0d6 100644
@@ -1414,20 +1414,21 @@ - (void)make_about_window
    [superView addSubview: picture_view];
 
    /* Make the name label */
-    x = 0;
-    y = y - 25;
-    int name_width = about_width, name_height = 20;
-    NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
-    NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
-    [name_label setEditable: NO];

ui/cocoa.m - (void)make_about_window · line 1418

Quoted 10 lines
ui/cocoa.m +2 -8
-    [name_label setBezeled: NO];
-    [name_label setDrawsBackground: NO];
-    [name_label setAlignment: NSTextAlignmentCenter];
-    NSString *qemu_name = [[NSString alloc] initWithCString: gArgv[0]
-                                            encoding: NSASCIIStringEncoding];
-    qemu_name = [qemu_name lastPathComponent];
-    [name_label setStringValue: qemu_name];
-    [superView addSubview: name_label];
+    NSBundle *bundle = [NSBundle mainBundle];
+    if (bundle) {

Does this break about window if the executable is not in a bundle (like when run from the command line after compiling)? Shouldn't you only put the qemu_name in this if and have some default name if bundle is not available (or fall back to argv[0] in that case?

Regards,
BALATON Zoltan

Quoted 17 lines
ui/cocoa.m +13 -0
+        x = 0;
+        y = y - 25;
+        int name_width = about_width, name_height = 20;
+        NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
+        NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
+        [name_label setEditable: NO];
+        [name_label setBezeled: NO];
+        [name_label setDrawsBackground: NO];
+        [name_label setAlignment: NSTextAlignmentCenter];
+        NSString *qemu_name = [[bundle executablePath] lastPathComponent];
+        [name_label setStringValue: qemu_name];
+        [superView addSubview: name_label];
+    }
 
    /* Set the version label's attributes */
    x = 0;
#3 lore raw

Re: [PATCH 2/2] ui/cocoa: Do not rely on the first argument

2021年3月9日(火) 22:10 BALATON Zoltan <[email protected]>:

Quoted 41 lines · Akihiko Odaki
Quoted 23 lines

The first argument of the executable was used to get its path, but it is not reliable because the executer can specify any arbitrary string. Use the interfaces provided by QEMU and the platform to get those paths.

---
ui/cocoa.m | 29 +++++++++++++++--------------

1 file changed, 15 insertions(+), 14 deletions(-)
ui/cocoa.m +0 -6
index d8eacea6d22..6e94301c0d6 100644
@@ -1414,20 +1414,21 @@ - (void)make_about_window
    [superView addSubview: picture_view];
 
    /* Make the name label */
-    x = 0;
-    y = y - 25;
-    int name_width = about_width, name_height = 20;
-    NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
-    NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
-    [name_label setEditable: NO];

ui/cocoa.m - (void)make_about_window · line 1418

Quoted 10 lines
ui/cocoa.m +2 -8
-    [name_label setBezeled: NO];
-    [name_label setDrawsBackground: NO];
-    [name_label setAlignment: NSTextAlignmentCenter];
-    NSString *qemu_name = [[NSString alloc] initWithCString: gArgv[0]
-                                            encoding: NSASCIIStringEncoding];
-    qemu_name = [qemu_name lastPathComponent];
-    [name_label setStringValue: qemu_name];
-    [superView addSubview: name_label];
+    NSBundle *bundle = [NSBundle mainBundle];
+    if (bundle) {

Does this break about window if the executable is not in a bundle (like when run from the command line after compiling)? Shouldn't you only put the qemu_name in this if and have some default name if bundle is not available (or fall back to argv[0] in that case?

Quoted 3 lines

Regards,
BALATON Zoltan

No, it just doesn't show the application name. Everything else is fine.

Regards,
Akihiko Odaki

#4 lore raw

Re: [PATCH 2/2] ui/cocoa: Do not rely on the first argument

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

+ if (bundle) {

Does this break about window if the executable is not in a bundle (like when run from the command line after compiling)? Shouldn't you only put the qemu_name in this if and have some default name if bundle is not available (or fall back to argv[0] in that case?

Quoted 5 lines
Quoted 3 lines

Regards,
BALATON Zoltan

No, it just doesn't show the application name. Everything else is fine.

Having a fallback would still be nice, even if it is just the fixed string "qemu". Starting a fresh build without installing it first is common while developing.

take care,
Gerd

#5 lore raw

Re: [PATCH 2/2] ui/cocoa: Do not rely on the first argument

2021年3月10日(水) 21:22 Gerd Hoffmann <[email protected]>:

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 2 lines

+ if (bundle) {

Does this break about window if the executable is not in a bundle (like when run from the command line after compiling)? Shouldn't you only put the qemu_name in this if and have some default name if bundle is not available (or fall back to argv[0] in that case?

Regards,
BALATON Zoltan

Quoted 9 lines
Quoted 1 line

No, it just doesn't show the application name. Everything else is fine.

Having a fallback would still be nice, even if it is just the fixed string "qemu". Starting a fresh build without installing it first is common while developing.

take care,
Gerd

It shows "QEMU emulator version %s" just below. Also, it can show the name even in a build tree without installing so it should be ok.

Regards,
Akihiko Odaki

#6 lore raw

Re: [PATCH 2/2] ui/cocoa: Do not rely on the first argument

Hi,

Quoted 10 lines
Quoted 7 lines

Having a fallback would still be nice, even if it is just the fixed string "qemu". Starting a fresh build without installing it first is common while developing.

take care,
Gerd

It shows "QEMU emulator version %s" just below. Also, it can show the name even in a build tree without installing so it should be ok.

Fair enough. Added to UI queue.

thanks,
Gerd