19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*   SDLMain.m - main entry point for our Cocoa-ized SDL app
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       Non-NIB-Code & other changes: Max Horn <max@quendi.de>
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Feel free to customize this file to suit your needs
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDLMain.h"
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/param.h> /* for MAXPATHLEN */
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but the method still is there and works. To avoid warnings, we declare
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall it ourselves here. */
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@interface NSApplication(SDL_Missing_Methods)
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (void)setAppleMenu:(NSMenu *)menu;
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Use this flag to determine whether we use SDLMain.nib or not */
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define		SDL_USE_NIB_FILE	0
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Use this flag to determine whether we use CPS (docking) or not */
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define		SDL_USE_CPS		1
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_USE_CPS
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Portions of CPS.h */
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct CPSProcessSerNum
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UInt32		lo;
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UInt32		hi;
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} CPSProcessSerNum;
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern OSErr	CPSGetCurrentProcess( CPSProcessSerNum *psn);
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern OSErr 	CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern OSErr	CPSSetFrontProcess( CPSProcessSerNum *psn);
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_USE_CPS */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int    gArgc;
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char  **gArgv;
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic BOOL   gFinderLaunch;
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic BOOL   gCalledAppMainline = FALSE;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic NSString *getApplicationName(void)
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const NSDictionary *dict;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSString *appName = 0;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Determine the application name */
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (dict)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        appName = [dict objectForKey: @"CFBundleName"];
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (![appName length])
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        appName = [[NSProcessInfo processInfo] processName];
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return appName;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_USE_NIB_FILE
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* A helper category for NSString */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@interface NSString (ReplaceSubString)
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@interface NSApplication (SDLApplication)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@implementation NSApplication (SDLApplication)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Invoked from the Quit menu item */
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (void)terminate:(id)sender
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Post a SDL_QUIT event */
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Event event;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    event.type = SDL_QUIT;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PushEvent(&event);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The main class of the application, the application's delegate */
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@implementation SDLMain
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set the working directory to the .app's parent directory */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (void) setupWorkingDirectory:(BOOL)shouldChdir
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (shouldChdir)
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        char parentdir[MAXPATHLEN];
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            chdir(parentdir);   /* chdir to the binary app's parent */
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CFRelease(url);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CFRelease(url2);
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_USE_NIB_FILE
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Fix menu to contain the real app name instead of "SDL App" */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSRange aRange;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSEnumerator *enumerator;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSMenuItem *menuItem;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    aRange = [[aMenu title] rangeOfString:@"SDL App"];
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (aRange.length != 0)
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    enumerator = [[aMenu itemArray] objectEnumerator];
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ((menuItem = [enumerator nextObject]))
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        aRange = [[menuItem title] rangeOfString:@"SDL App"];
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (aRange.length != 0)
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ([menuItem hasSubmenu])
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            [self fixMenu:[menuItem submenu] withAppName:appName];
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void setApplicationMenu(void)
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* warning: this code is very odd */
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSMenu *appleMenu;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSMenuItem *menuItem;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSString *title;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSString *appName;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    appName = getApplicationName();
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    appleMenu = [[NSMenu alloc] initWithTitle:@""];
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Add menu items */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    title = [@"About " stringByAppendingString:appName];
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [appleMenu addItem:[NSMenuItem separatorItem]];
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    title = [@"Hide " stringByAppendingString:appName];
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [appleMenu addItem:[NSMenuItem separatorItem]];
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    title = [@"Quit " stringByAppendingString:appName];
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Put menu into the menubar */
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [menuItem setSubmenu:appleMenu];
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [[NSApp mainMenu] addItem:menuItem];
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Tell the application object that this is now the application menu */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [NSApp setAppleMenu:appleMenu];
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Finally give up our references to the objects */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [appleMenu release];
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [menuItem release];
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a window menu */
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void setupWindowMenu(void)
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSMenu      *windowMenu;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSMenuItem  *windowMenuItem;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSMenuItem  *menuItem;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* "Minimize" item */
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [windowMenu addItem:menuItem];
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [menuItem release];
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Put menu into the menubar */
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [windowMenuItem setSubmenu:windowMenu];
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [[NSApp mainMenu] addItem:windowMenuItem];
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Tell the application object that this is now the window menu */
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [NSApp setWindowsMenu:windowMenu];
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Finally give up our references to the objects */
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [windowMenu release];
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [windowMenuItem release];
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Replacement for NSApplicationMain */
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CustomApplicationMain (int argc, char **argv)
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSAutoreleasePool	*pool = [[NSAutoreleasePool alloc] init];
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDLMain				*sdlMain;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Ensure the application object is initialised */
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [NSApplication sharedApplication];
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_USE_CPS
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CPSProcessSerNum PSN;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Tell the dock about us */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (!CPSGetCurrentProcess(&PSN))
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if (!CPSSetFrontProcess(&PSN))
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    [NSApplication sharedApplication];
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_USE_CPS */
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set up the menubar */
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [NSApp setMainMenu:[[NSMenu alloc] init]];
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    setApplicationMenu();
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    setupWindowMenu();
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Create SDLMain and make it the app delegate */
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sdlMain = [[SDLMain alloc] init];
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [NSApp setDelegate:sdlMain];
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Start the main event loop */
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [NSApp run];
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [sdlMain release];
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [pool release];
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Catch document open requests...this lets us notice files when the app
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  was launched by double-clicking a document, or when a document was
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  dragged/dropped on the app's icon. You need to have a
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  CFBundleDocumentsType section in your Info.plist to get this message,
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  apparently.
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Files are added to gArgv, so to the app, they'll look like command line
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  arguments. Previously, apps launched from the finder had nothing but
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  an argv[0].
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This message may be received multiple times to open several docs on launch.
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This message is ignored once the app's mainline has been called.
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *temparg;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t arglen;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *arg;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char **newargv;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!gFinderLaunch)  /* MacOS is passing command line args. */
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return FALSE;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (gCalledAppMainline)  /* app has started, ignore this document. */
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return FALSE;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    temparg = [filename UTF8String];
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    arglen = SDL_strlen(temparg) + 1;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    arg = (char *) SDL_malloc(arglen);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (arg == NULL)
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return FALSE;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (newargv == NULL)
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_free(arg);
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return FALSE;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gArgv = newargv;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strlcpy(arg, temparg, arglen);
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gArgv[gArgc++] = arg;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gArgv[gArgc] = NULL;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return TRUE;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Called when the internal event loop has just started running */
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (void) applicationDidFinishLaunching: (NSNotification *) note
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int status;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the working directory to the .app's parent directory */
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [self setupWorkingDirectory:gFinderLaunch];
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_USE_NIB_FILE
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the main menu to contain the real app name instead of "SDL App" */
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Hand off to main application code */
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gCalledAppMainline = TRUE;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status = SDL_main (gArgc, gArgv);
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* We're done, thank you for playing */
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    exit(status);
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@implementation NSString (ReplaceSubString)
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned int bufferSize;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned int selfLen = [self length];
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned int aStringLen = [aString length];
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unichar *buffer;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSRange localRange;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSString *result;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bufferSize = selfLen + aStringLen - aRange.length;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar));
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Get first part into buffer */
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    localRange.location = 0;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    localRange.length = aRange.location;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [self getCharacters:buffer range:localRange];
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Get middle part into buffer */
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    localRange.location = 0;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    localRange.length = aStringLen;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [aString getCharacters:(buffer+aRange.location) range:localRange];
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Get last part into buffer */
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    localRange.location = aRange.location + aRange.length;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    localRange.length = selfLen - localRange.location;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Build output string */
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    result = [NSString stringWithCharacters:buffer length:bufferSize];
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSDeallocateMemoryPages(buffer, bufferSize);
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return result;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef main
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#  undef main
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Main entry point to executable - should *not* be SDL_main! */
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main (int argc, char **argv)
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Copy the arguments into a global variable */
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* This is passed if we are launched by double-clicking */
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gArgv[0] = argv[0];
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gArgv[1] = NULL;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gArgc = 1;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gFinderLaunch = YES;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int i;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gArgc = argc;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for (i = 0; i <= argc; i++)
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            gArgv[i] = argv[i];
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        gFinderLaunch = NO;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
374e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall/* ANDROID_BEGIN */
375e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    /* if -no-window is used, we don't want to launch a graphical
376e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall     * application that creates a Menu bar and steals the focus.
377e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall     */
378e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    {
379e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        int nn;
380e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        for (nn = 0; nn < argc; nn++) {
381e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall            if ( !strcmp( argv[nn], "-no-window" ) ) {
382e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                return SDL_main (argc, argv);
383e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall            }
384e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        }
385e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    }
386e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall/* ANDROID_END */
387e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_USE_NIB_FILE
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSApplicationMain (argc, argv);
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    CustomApplicationMain (argc, argv);
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
396