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 SDLApplication : NSApplication 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@implementation 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 [ aMenu sizeToFit ]; 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void setApplicationMenu(void) 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* warning: this code is very odd */ 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSMenu *appleMenu; 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSMenuItem *menuItem; 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSString *title; 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSString *appName; 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall appName = getApplicationName(); 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall appleMenu = [[NSMenu alloc] initWithTitle:@""]; 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Add menu items */ 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall title = [@"About " stringByAppendingString:appName]; 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [appleMenu addItem:[NSMenuItem separatorItem]]; 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall title = [@"Hide " stringByAppendingString:appName]; 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"]; 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [appleMenu addItem:[NSMenuItem separatorItem]]; 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall title = [@"Quit " stringByAppendingString:appName]; 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Put menu into the menubar */ 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [menuItem setSubmenu:appleMenu]; 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [[NSApp mainMenu] addItem:menuItem]; 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Tell the application object that this is now the application menu */ 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [NSApp setAppleMenu:appleMenu]; 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Finally give up our references to the objects */ 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [appleMenu release]; 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [menuItem release]; 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a window menu */ 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void setupWindowMenu(void) 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSMenu *windowMenu; 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSMenuItem *windowMenuItem; 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSMenuItem *menuItem; 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall windowMenu = [[NSMenu alloc] initWithTitle:@"Window"]; 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* "Minimize" item */ 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [windowMenu addItem:menuItem]; 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [menuItem release]; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Put menu into the menubar */ 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""]; 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [windowMenuItem setSubmenu:windowMenu]; 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [[NSApp mainMenu] addItem:windowMenuItem]; 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Tell the application object that this is now the window menu */ 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [NSApp setWindowsMenu:windowMenu]; 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Finally give up our references to the objects */ 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [windowMenu release]; 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [windowMenuItem release]; 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Replacement for NSApplicationMain */ 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CustomApplicationMain (int argc, char **argv) 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDLMain *sdlMain; 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Ensure the application object is initialised */ 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [SDLApplication sharedApplication]; 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_USE_CPS 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CPSProcessSerNum PSN; 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Tell the dock about us */ 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!CPSGetCurrentProcess(&PSN)) 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103)) 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!CPSSetFrontProcess(&PSN)) 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [SDLApplication sharedApplication]; 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_USE_CPS */ 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set up the menubar */ 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [NSApp setMainMenu:[[NSMenu alloc] init]]; 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall setApplicationMenu(); 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall setupWindowMenu(); 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Create SDLMain and make it the app delegate */ 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sdlMain = [[SDLMain alloc] init]; 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [NSApp setDelegate:sdlMain]; 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Start the main event loop */ 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [NSApp run]; 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [sdlMain release]; 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [pool release]; 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Catch document open requests...this lets us notice files when the app 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * was launched by double-clicking a document, or when a document was 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * dragged/dropped on the app's icon. You need to have a 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * CFBundleDocumentsType section in your Info.plist to get this message, 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * apparently. 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Files are added to gArgv, so to the app, they'll look like command line 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * arguments. Previously, apps launched from the finder had nothing but 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * an argv[0]. 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This message may be received multiple times to open several docs on launch. 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This message is ignored once the app's mainline has been called. 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const char *temparg; 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall size_t arglen; 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char *arg; 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char **newargv; 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!gFinderLaunch) /* MacOS is passing command line args. */ 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return FALSE; 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (gCalledAppMainline) /* app has started, ignore this document. */ 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return FALSE; 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall temparg = [filename UTF8String]; 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall arglen = SDL_strlen(temparg) + 1; 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall arg = (char *) SDL_malloc(arglen); 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (arg == NULL) 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return FALSE; 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2)); 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (newargv == NULL) 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(arg); 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return FALSE; 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv = newargv; 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_strlcpy(arg, temparg, arglen); 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv[gArgc++] = arg; 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv[gArgc] = NULL; 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return TRUE; 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Called when the internal event loop has just started running */ 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (void) applicationDidFinishLaunching: (NSNotification *) note 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int status; 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the working directory to the .app's parent directory */ 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [self setupWorkingDirectory:gFinderLaunch]; 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_USE_NIB_FILE 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the main menu to contain the real app name instead of "SDL App" */ 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()]; 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Hand off to main application code */ 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gCalledAppMainline = TRUE; 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall status = SDL_main (gArgc, gArgv); 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We're done, thank you for playing */ 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall exit(status); 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@implementation NSString (ReplaceSubString) 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned int bufferSize; 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned int selfLen = [self length]; 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned int aStringLen = [aString length]; 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unichar *buffer; 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSRange localRange; 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSString *result; 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall bufferSize = selfLen + aStringLen - aRange.length; 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar)); 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get first part into buffer */ 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall localRange.location = 0; 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall localRange.length = aRange.location; 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [self getCharacters:buffer range:localRange]; 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get middle part into buffer */ 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall localRange.location = 0; 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall localRange.length = aStringLen; 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [aString getCharacters:(buffer+aRange.location) range:localRange]; 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get last part into buffer */ 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall localRange.location = aRange.location + aRange.length; 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall localRange.length = selfLen - localRange.location; 3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; 3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Build output string */ 3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = [NSString stringWithCharacters:buffer length:bufferSize]; 3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSDeallocateMemoryPages(buffer, bufferSize); 3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end 3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef main 3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# undef main 3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Main entry point to executable - should *not* be SDL_main! */ 3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main (int argc, char **argv) 3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Copy the arguments into a global variable */ 3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* This is passed if we are launched by double-clicking */ 3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { 3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv = (char **) SDL_malloc(sizeof (char *) * 2); 3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv[0] = argv[0]; 3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv[1] = NULL; 3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgc = 1; 3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gFinderLaunch = YES; 3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; 3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgc = argc; 3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1)); 3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i = 0; i <= argc; i++) 3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gArgv[i] = argv[i]; 3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall gFinderLaunch = NO; 3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_USE_NIB_FILE 3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall [SDLApplication poseAsClass:[NSApplication class]]; 3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NSApplicationMain (argc, argv); 3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CustomApplicationMain (argc, argv); 3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return 0; 3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 384