VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxSDL/VBoxSDLMain-darwin.m@ 61042

Last change on this file since 61042 was 56335, checked in by vboxsync, 10 years ago

*.def,*.sed,*.m,*.mm: whitespace and svn properties

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 11.1 KB
Line 
1/* SDLMain.m - main entry point for our Cocoa-ized SDL app
2 Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
3 Non-NIB-Code & other changes: Max Horn <max@quendi.de>
4
5 Feel free to customize this file to suit your needs
6*/
7
8#import "SDL.h"
9#import "VBoxSDLMain-darwin.h"
10#import <sys/param.h> /* for MAXPATHLEN */
11#import <unistd.h>
12#import <iprt/assert.h>
13
14/* For some reason, Apple removed setAppleMenu from the headers in 10.4,
15 but the method still is there and works. To avoid warnings, we declare
16 it ourselves here. */
17@interface NSApplication(SDL_Missing_Methods)
18- (void)setAppleMenu:(NSMenu *)menu;
19@end
20
21/* Use this flag to determine whether we use SDLMain.nib or not */
22#define SDL_USE_NIB_FILE 0
23
24/* Use this flag to determine whether we use CPS (docking) or not */
25#define SDL_USE_CPS 1
26#ifdef SDL_USE_CPS
27/* Portions of CPS.h */
28typedef struct CPSProcessSerNum
29{
30 UInt32 lo;
31 UInt32 hi;
32} CPSProcessSerNum;
33
34extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
35extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
36extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
37
38#endif /* SDL_USE_CPS */
39
40static int gArgc;
41static char **gArgv;
42static BOOL gFinderLaunch;
43static BOOL gCalledAppMainline = FALSE;
44
45static NSString *getApplicationName(void)
46{
47 NSDictionary *dict;
48 NSString *appName = 0;
49
50 /* Determine the application name */
51 dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
52 if (dict)
53 appName = [dict objectForKey: @"CFBundleName"];
54
55 if (![appName length])
56 appName = [[NSProcessInfo processInfo] processName];
57
58 return appName;
59}
60
61#if SDL_USE_NIB_FILE
62/* A helper category for NSString */
63@interface NSString (ReplaceSubString)
64- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
65@end
66#endif
67
68@interface SDLApplication : NSApplication
69@end
70
71@implementation SDLApplication
72/* Invoked from the Quit menu item */
73- (void)terminate:(id)sender
74{
75 /* Post a SDL_QUIT event */
76 SDL_Event event;
77 event.type = SDL_QUIT;
78 SDL_PushEvent(&event);
79}
80@end
81
82/* The main class of the application, the application's delegate */
83@implementation SDLMain
84
85/* Set the working directory to the .app's parent directory */
86- (void) setupWorkingDirectory:(BOOL)shouldChdir
87{
88 if (shouldChdir)
89 {
90 char parentdir[MAXPATHLEN];
91 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
92 CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
93 if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, MAXPATHLEN)) {
94 int rc = chdir(parentdir); /* chdir to the binary app's parent */
95 Assert(rc == 0);
96 }
97 CFRelease(url);
98 CFRelease(url2);
99 }
100
101}
102
103#if SDL_USE_NIB_FILE
104
105/* Fix menu to contain the real app name instead of "SDL App" */
106- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
107{
108 NSRange aRange;
109 NSEnumerator *enumerator;
110 NSMenuItem *menuItem;
111
112 aRange = [[aMenu title] rangeOfString:@"SDL App"];
113 if (aRange.length != 0)
114 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
115
116 enumerator = [[aMenu itemArray] objectEnumerator];
117 while ((menuItem = [enumerator nextObject]))
118 {
119 aRange = [[menuItem title] rangeOfString:@"SDL App"];
120 if (aRange.length != 0)
121 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
122 if ([menuItem hasSubmenu])
123 [self fixMenu:[menuItem submenu] withAppName:appName];
124 }
125 [ aMenu sizeToFit ];
126}
127
128#else
129
130static void setApplicationMenu(void)
131{
132 /* warning: this code is very odd */
133 NSMenu *appleMenu;
134 NSMenuItem *menuItem;
135 NSString *title;
136 NSString *appName;
137
138 appName = getApplicationName();
139 appleMenu = [[NSMenu alloc] initWithTitle:@""];
140
141 /* Add menu items */
142 title = [@"About " stringByAppendingString:appName];
143 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
144
145 [appleMenu addItem:[NSMenuItem separatorItem]];
146
147 title = [@"Hide " stringByAppendingString:appName];
148 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
149
150 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
151 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
152
153 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
154
155 [appleMenu addItem:[NSMenuItem separatorItem]];
156
157 title = [@"Quit " stringByAppendingString:appName];
158 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
159
160
161 /* Put menu into the menubar */
162 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
163 [menuItem setSubmenu:appleMenu];
164 [[NSApp mainMenu] addItem:menuItem];
165
166 /* Tell the application object that this is now the application menu */
167 [NSApp setAppleMenu:appleMenu];
168
169 /* Finally give up our references to the objects */
170 [appleMenu release];
171 [menuItem release];
172}
173
174/* Create a window menu */
175static void setupWindowMenu(void)
176{
177 NSMenu *windowMenu;
178 NSMenuItem *windowMenuItem;
179 NSMenuItem *menuItem;
180
181 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
182
183 /* "Minimize" item */
184 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
185 [windowMenu addItem:menuItem];
186 [menuItem release];
187
188 /* Put menu into the menubar */
189 windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
190 [windowMenuItem setSubmenu:windowMenu];
191 [[NSApp mainMenu] addItem:windowMenuItem];
192
193 /* Tell the application object that this is now the window menu */
194 [NSApp setWindowsMenu:windowMenu];
195
196 /* Finally give up our references to the objects */
197 [windowMenu release];
198 [windowMenuItem release];
199}
200
201/* Replacement for NSApplicationMain */
202static void CustomApplicationMain (int argc, char **argv)
203{
204 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
205 SDLMain *sdlMain;
206
207 /* Ensure the application object is initialised */
208 [SDLApplication sharedApplication];
209
210#ifdef SDL_USE_CPS
211 {
212 CPSProcessSerNum PSN;
213 /* Tell the dock about us */
214 if (!CPSGetCurrentProcess(&PSN))
215 if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
216 if (!CPSSetFrontProcess(&PSN))
217 [SDLApplication sharedApplication];
218 }
219#endif /* SDL_USE_CPS */
220
221 /* Set up the menubar */
222 [NSApp setMainMenu:[[NSMenu alloc] init]];
223 setApplicationMenu();
224 setupWindowMenu();
225
226 /* Create SDLMain and make it the app delegate */
227 sdlMain = [[SDLMain alloc] init];
228 [NSApp setDelegate:sdlMain];
229
230 /* Start the main event loop */
231 [NSApp run];
232
233 [sdlMain release];
234 [pool release];
235}
236
237#endif
238
239
240/*
241 * Catch document open requests...this lets us notice files when the app
242 * was launched by double-clicking a document, or when a document was
243 * dragged/dropped on the app's icon. You need to have a
244 * CFBundleDocumentsType section in your Info.plist to get this message,
245 * apparently.
246 *
247 * Files are added to gArgv, so to the app, they'll look like command line
248 * arguments. Previously, apps launched from the finder had nothing but
249 * an argv[0].
250 *
251 * This message may be received multiple times to open several docs on launch.
252 *
253 * This message is ignored once the app's mainline has been called.
254 */
255- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
256{
257 const char *temparg;
258 size_t arglen;
259 char *arg;
260 char **newargv;
261
262 if (!gFinderLaunch) /* MacOS is passing command line args. */
263 return FALSE;
264
265 if (gCalledAppMainline) /* app has started, ignore this document. */
266 return FALSE;
267
268 temparg = [filename UTF8String];
269 arglen = SDL_strlen(temparg) + 1;
270 arg = (char *) SDL_malloc(arglen);
271 if (arg == NULL)
272 return FALSE;
273
274 newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
275 if (newargv == NULL)
276 {
277 SDL_free(arg);
278 return FALSE;
279 }
280 gArgv = newargv;
281
282 SDL_strlcpy(arg, temparg, arglen);
283 gArgv[gArgc++] = arg;
284 gArgv[gArgc] = NULL;
285 return TRUE;
286}
287
288
289/* Called when the internal event loop has just started running */
290- (void) applicationDidFinishLaunching: (NSNotification *) note
291{
292 int status;
293
294 /* Set the working directory to the .app's parent directory */
295 [self setupWorkingDirectory:gFinderLaunch];
296
297#if SDL_USE_NIB_FILE
298 /* Set the main menu to contain the real app name instead of "SDL App" */
299 [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
300#endif
301
302 /* Hand off to main application code */
303 gCalledAppMainline = TRUE;
304 status = SDL_main (gArgc, gArgv);
305
306 /* We're done, thank you for playing */
307 exit(status);
308}
309@end
310
311
312@implementation NSString (ReplaceSubString)
313
314- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
315{
316 unsigned int bufferSize;
317 unsigned int selfLen = [self length];
318 unsigned int aStringLen = [aString length];
319 unichar *buffer;
320 NSRange localRange;
321 NSString *result;
322
323 bufferSize = selfLen + aStringLen - aRange.length;
324 buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
325
326 /* Get first part into buffer */
327 localRange.location = 0;
328 localRange.length = aRange.location;
329 [self getCharacters:buffer range:localRange];
330
331 /* Get middle part into buffer */
332 localRange.location = 0;
333 localRange.length = aStringLen;
334 [aString getCharacters:(buffer+aRange.location) range:localRange];
335
336 /* Get last part into buffer */
337 localRange.location = aRange.location + aRange.length;
338 localRange.length = selfLen - localRange.location;
339 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
340
341 /* Build output string */
342 result = [NSString stringWithCharacters:buffer length:bufferSize];
343
344 NSDeallocateMemoryPages(buffer, bufferSize);
345
346 return result;
347}
348
349@end
350
351
352
353#ifdef main
354# undef main
355#endif
356
357
358/* Main entry point to executable - should *not* be SDL_main! */
359int main (int argc, char **argv)
360{
361 /* Copy the arguments into a global variable */
362 /* This is passed if we are launched by double-clicking */
363 if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
364 gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
365 gArgv[0] = argv[0];
366 gArgv[1] = NULL;
367 gArgc = 1;
368 gFinderLaunch = YES;
369 } else {
370 int i;
371 gArgc = argc;
372 gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
373 for (i = 0; i <= argc; i++)
374 gArgv[i] = argv[i];
375 gFinderLaunch = NO;
376 }
377
378#if SDL_USE_NIB_FILE
379 [SDLApplication poseAsClass:[NSApplication class]];
380 NSApplicationMain (argc, argv);
381#else
382 CustomApplicationMain (argc, argv);
383#endif
384 return 0;
385}
386
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