diff mbox series

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

Message ID 20210309122226.23117-2-akihiko.odaki@gmail.com (mailing list archive)
State New, archived
Headers show
Series [1/2] ui/cocoa: Show QEMU icon in the about window | expand

Commit Message

Akihiko Odaki March 9, 2021, 12:22 p.m. UTC
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.

Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
---
 ui/cocoa.m | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

Comments

BALATON Zoltan March 9, 2021, 1:10 p.m. UTC | #1
On Tue, 9 Mar 2021, Akihiko Odaki wrote:
> 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.
>
> Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
> ---
> ui/cocoa.m | 29 +++++++++++++++--------------
> 1 file changed, 15 insertions(+), 14 deletions(-)
>
> diff --git a/ui/cocoa.m b/ui/cocoa.m
> index d8eacea6d22..6e94301c0d6 100644
> --- a/ui/cocoa.m
> +++ b/ui/cocoa.m
> @@ -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) {

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

> +        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;
>
Akihiko Odaki March 10, 2021, 3:31 a.m. UTC | #2
2021年3月9日(火) 22:10 BALATON Zoltan <balaton@eik.bme.hu>:
>
> On Tue, 9 Mar 2021, Akihiko Odaki wrote:
> > 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.
> >
> > Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
> > ---
> > ui/cocoa.m | 29 +++++++++++++++--------------
> > 1 file changed, 15 insertions(+), 14 deletions(-)
> >
> > diff --git a/ui/cocoa.m b/ui/cocoa.m
> > index d8eacea6d22..6e94301c0d6 100644
> > --- a/ui/cocoa.m
> > +++ b/ui/cocoa.m
> > @@ -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) {
>
> 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
>

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

Regards,
Akihiko Odaki
Gerd Hoffmann March 10, 2021, 12:22 p.m. UTC | #3
> > > +    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
> >
> 
> 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
Akihiko Odaki March 10, 2021, 3:49 p.m. UTC | #4
2021年3月10日(水) 21:22 Gerd Hoffmann <kraxel@redhat.com>:
>
> > > > +    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
> > >
> >
> > 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
Gerd Hoffmann March 11, 2021, 8:31 a.m. UTC | #5
Hi,

> > 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
diff mbox series

Patch

diff --git a/ui/cocoa.m b/ui/cocoa.m
index d8eacea6d22..6e94301c0d6 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -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;