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>
5 Feel free to customize this file to suit your needs
10 #include <sys/param.h> /* for MAXPATHLEN */
13 /* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
14 but the method still is there and works. To avoid warnings, we declare
16 @interface NSApplication(SDL_Missing_Methods)
17 - (void)setAppleMenu:(NSMenu *)menu;
20 /* Use this flag to determine whether we use SDLMain.nib or not */
21 #define SDL_USE_NIB_FILE 0
23 /* Use this flag to determine whether we use CPS (docking) or not */
26 /* Portions of CPS.h */
27 typedef struct CPSProcessSerNum
33 extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
34 extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
35 extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
37 #endif /* SDL_USE_CPS */
41 static BOOL gFinderLaunch;
42 static BOOL gCalledAppMainline = FALSE;
44 static NSString *getApplicationName(void)
46 const NSDictionary *dict;
47 NSString *appName = 0;
49 /* Determine the application name */
50 dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
52 appName = [dict objectForKey: @"CFBundleName"];
54 if (![appName length])
55 appName = [[NSProcessInfo processInfo] processName];
61 /* A helper category for NSString */
62 @interface NSString (ReplaceSubString)
63 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
67 @interface NSApplication (SDLApplication)
70 @implementation NSApplication (SDLApplication)
71 /* Invoked from the Quit menu item */
72 - (void)terminate:(id)sender
74 /* Post a SDL_QUIT event */
76 event.type = SDL_QUIT;
77 SDL_PushEvent(&event);
81 /* The main class of the application, the application's delegate */
82 @implementation SDLMain
84 /* Set the working directory to the .app's parent directory */
85 - (void) setupWorkingDirectory:(BOOL)shouldChdir
89 char parentdir[MAXPATHLEN];
90 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
91 CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
92 if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
93 chdir(parentdir); /* chdir to the binary app's parent */
102 /* Fix menu to contain the real app name instead of "SDL App" */
103 - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
106 NSEnumerator *enumerator;
107 NSMenuItem *menuItem;
109 aRange = [[aMenu title] rangeOfString:@"SDL App"];
110 if (aRange.length != 0)
111 [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
113 enumerator = [[aMenu itemArray] objectEnumerator];
114 while ((menuItem = [enumerator nextObject]))
116 aRange = [[menuItem title] rangeOfString:@"SDL App"];
117 if (aRange.length != 0)
118 [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
119 if ([menuItem hasSubmenu])
120 [self fixMenu:[menuItem submenu] withAppName:appName];
126 static void setApplicationMenu(void)
128 /* warning: this code is very odd */
130 NSMenuItem *menuItem;
134 appName = getApplicationName();
135 appleMenu = [[NSMenu alloc] initWithTitle:@""];
138 title = [@"About " stringByAppendingString:appName];
139 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
141 [appleMenu addItem:[NSMenuItem separatorItem]];
143 title = [@"Hide " stringByAppendingString:appName];
144 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
146 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
147 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
149 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
151 [appleMenu addItem:[NSMenuItem separatorItem]];
153 title = [@"Quit " stringByAppendingString:appName];
154 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
157 /* Put menu into the menubar */
158 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
159 [menuItem setSubmenu:appleMenu];
160 [[NSApp mainMenu] addItem:menuItem];
162 /* Tell the application object that this is now the application menu */
163 [NSApp setAppleMenu:appleMenu];
165 /* Finally give up our references to the objects */
170 /* Create a window menu */
171 static void setupWindowMenu(void)
174 NSMenuItem *windowMenuItem;
175 NSMenuItem *menuItem;
177 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
179 /* "Minimize" item */
180 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
181 [windowMenu addItem:menuItem];
184 /* Put menu into the menubar */
185 windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
186 [windowMenuItem setSubmenu:windowMenu];
187 [[NSApp mainMenu] addItem:windowMenuItem];
189 /* Tell the application object that this is now the window menu */
190 [NSApp setWindowsMenu:windowMenu];
192 /* Finally give up our references to the objects */
193 [windowMenu release];
194 [windowMenuItem release];
197 /* Replacement for NSApplicationMain */
198 static void CustomApplicationMain (int argc, char **argv)
200 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
203 /* Ensure the application object is initialised */
204 [NSApplication sharedApplication];
208 CPSProcessSerNum PSN;
209 /* Tell the dock about us */
210 if (!CPSGetCurrentProcess(&PSN))
211 if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
212 if (!CPSSetFrontProcess(&PSN))
213 [NSApplication sharedApplication];
215 #endif /* SDL_USE_CPS */
217 /* Set up the menubar */
218 [NSApp setMainMenu:[[NSMenu alloc] init]];
219 setApplicationMenu();
222 /* Create SDLMain and make it the app delegate */
223 sdlMain = [[SDLMain alloc] init];
224 [NSApp setDelegate:sdlMain];
226 /* Start the main event loop */
237 * Catch document open requests...this lets us notice files when the app
238 * was launched by double-clicking a document, or when a document was
239 * dragged/dropped on the app's icon. You need to have a
240 * CFBundleDocumentsType section in your Info.plist to get this message,
243 * Files are added to gArgv, so to the app, they'll look like command line
244 * arguments. Previously, apps launched from the finder had nothing but
247 * This message may be received multiple times to open several docs on launch.
249 * This message is ignored once the app's mainline has been called.
251 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
258 if (!gFinderLaunch) /* MacOS is passing command line args. */
261 if (gCalledAppMainline) /* app has started, ignore this document. */
264 temparg = [filename UTF8String];
265 arglen = SDL_strlen(temparg) + 1;
266 arg = (char *) SDL_malloc(arglen);
270 newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
278 SDL_strlcpy(arg, temparg, arglen);
279 gArgv[gArgc++] = arg;
285 /* Called when the internal event loop has just started running */
286 - (void) applicationDidFinishLaunching: (NSNotification *) note
290 /* Set the working directory to the .app's parent directory */
291 [self setupWorkingDirectory:gFinderLaunch];
294 /* Set the main menu to contain the real app name instead of "SDL App" */
295 [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
298 /* Hand off to main application code */
299 gCalledAppMainline = TRUE;
300 status = SDL_main (gArgc, gArgv);
302 /* We're done, thank you for playing */
308 @implementation NSString (ReplaceSubString)
310 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
312 unsigned int bufferSize;
313 unsigned int selfLen = [self length];
314 unsigned int aStringLen = [aString length];
319 bufferSize = selfLen + aStringLen - aRange.length;
320 buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar));
322 /* Get first part into buffer */
323 localRange.location = 0;
324 localRange.length = aRange.location;
325 [self getCharacters:buffer range:localRange];
327 /* Get middle part into buffer */
328 localRange.location = 0;
329 localRange.length = aStringLen;
330 [aString getCharacters:(buffer+aRange.location) range:localRange];
332 /* Get last part into buffer */
333 localRange.location = aRange.location + aRange.length;
334 localRange.length = selfLen - localRange.location;
335 [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
337 /* Build output string */
338 result = [NSString stringWithCharacters:buffer length:bufferSize];
340 NSDeallocateMemoryPages(buffer, bufferSize);
354 /* Main entry point to executable - should *not* be SDL_main! */
355 int main (int argc, char **argv)
357 /* Copy the arguments into a global variable */
358 /* This is passed if we are launched by double-clicking */
359 if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
360 gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
368 gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
369 for (i = 0; i <= argc; i++)
375 NSApplicationMain (argc, argv);
377 CustomApplicationMain (argc, argv);