VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/vmstarter.mm

Last change on this file was 106061, checked in by vboxsync, 3 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1/* $Id: vmstarter.mm 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - Helper application for starting vbox the right way when the user double clicks on a file type association.
4 */
5
6/*
7 * Copyright (C) 2010-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#import <Cocoa/Cocoa.h>
29#include <iprt/cdefs.h>
30
31@interface AppDelegate: NSObject
32{
33NSString *m_strVBoxPath;
34}
35@end
36
37@implementation AppDelegate
38-(id) init
39{
40 self = [super init];
41 if (self)
42 {
43 /* Get the path of VBox by looking where our bundle is located. */
44 m_strVBoxPath = [[[[NSBundle mainBundle] bundlePath]
45 stringByAppendingPathComponent:@"/../../../../VirtualBox.app"]
46 stringByStandardizingPath];
47 /* We kill ourself after 1 seconds */
48 [NSTimer scheduledTimerWithTimeInterval:1.0
49 target:NSApp
50 selector:@selector(terminate:)
51 userInfo:nil
52 repeats:NO];
53 }
54
55 return self;
56}
57
58- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
59{
60 RT_NOREF(sender);
61
62 BOOL fResult = FALSE;
63 NSWorkspace *pWS = [NSWorkspace sharedWorkspace];
64 /* We need to check if vbox is running already. If so we sent an open
65 event. If not we start a new process with the file as parameter. */
66 NSArray *pApps = [pWS runningApplications];
67 bool fVBoxRuns = false;
68 for (NSRunningApplication *pApp in pApps)
69 {
70 if ([pApp.bundleIdentifier isEqualToString:@"org.virtualbox.app.VirtualBox"])
71 {
72 fVBoxRuns = true;
73 break;
74 }
75 }
76 if (fVBoxRuns)
77 {
78 /* Send the open event.
79 * Todo: check for an method which take a list of files. */
80 for (NSString *filename in filenames)
81 fResult = [pWS openFile:filename withApplication:m_strVBoxPath andDeactivate:TRUE];
82 }
83 else
84 {
85 /* Fire up a new instance of VBox. We prefer LSOpenApplication over
86 NSTask, cause it makes sure that VBox will become the front most
87 process after starting up. */
88/** @todo should replace all this with -[NSWorkspace
89 * launchApplicationAtURL:options:configuration:error:] because LSOpenApplication is deprecated in
90 * 10.10 while, FSPathMakeRef is deprecated since 10.8. */
91 /* The state horror show starts right here: */
92 OSStatus err = noErr;
93 Boolean fDir;
94 void *asyncLaunchRefCon = NULL;
95 FSRef fileRef;
96 CFStringRef file = NULL;
97 CFArrayRef args = NULL;
98 void **list = (void**)malloc(sizeof(void*) * [filenames count]);
99 for (size_t i = 0; i < [filenames count]; ++i)
100 list[i] = [filenames objectAtIndex:i];
101 do
102 {
103 NSString *strVBoxExe = [m_strVBoxPath stringByAppendingPathComponent:@"Contents/MacOS/VirtualBox"];
104 err = FSPathMakeRef((const UInt8*)[strVBoxExe UTF8String], &fileRef, &fDir);
105 if (err != noErr)
106 break;
107 args = CFArrayCreate(NULL, (const void **)list, [filenames count], &kCFTypeArrayCallBacks);
108 if (args == NULL)
109 break;
110 LSApplicationParameters par = { 0, 0, &fileRef, asyncLaunchRefCon, 0, args, 0 };
111 err = LSOpenApplication(&par, NULL);
112 if (err != noErr)
113 break;
114 fResult = TRUE;
115 }while(0);
116 if (list) /* Why bother checking, because you've crashed already if it's NULL! */
117 free(list);
118 if (file)
119 CFRelease(file);
120 if (args)
121 CFRelease(args);
122 }
123}
124@end
125
126int main()
127{
128 /* Global auto release pool. */
129 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
130 /* Create our own delegate for the application. */
131 AppDelegate *pAppDelegate = [[AppDelegate alloc] init];
132 [[NSApplication sharedApplication] setDelegate: (id<NSApplicationDelegate>)pAppDelegate]; /** @todo check out ugly cast */
133 pAppDelegate = nil;
134 /* Start the event loop. */
135 [NSApp run];
136 /* Cleanup */
137 [pool release];
138 return 0;
139}
140
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette