| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #import "SDL.h" |
|---|
| 13 | #import "SDLMainForMacOSX.h" |
|---|
| 14 | #import <sys/param.h> |
|---|
| 15 | #import <unistd.h> |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | @interface NSApplication(SDL_Missing_Methods) |
|---|
| 21 | - (void)setAppleMenu:(NSMenu *)menu; |
|---|
| 22 | @end |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | #define SDL_USE_NIB_FILE 0 |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | #define SDL_USE_CPS 1 |
|---|
| 29 | #ifdef SDL_USE_CPS |
|---|
| 30 | |
|---|
| 31 | typedef struct CPSProcessSerNum |
|---|
| 32 | { |
|---|
| 33 | UInt32 lo; |
|---|
| 34 | UInt32 hi; |
|---|
| 35 | } CPSProcessSerNum; |
|---|
| 36 | |
|---|
| 37 | extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); |
|---|
| 38 | extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); |
|---|
| 39 | extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); |
|---|
| 40 | |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | static int gArgc; |
|---|
| 44 | static char **gArgv; |
|---|
| 45 | static BOOL gFinderLaunch; |
|---|
| 46 | static BOOL gCalledAppMainline = FALSE; |
|---|
| 47 | |
|---|
| 48 | static NSString *getApplicationName(void) |
|---|
| 49 | { |
|---|
| 50 | NSDictionary *dict; |
|---|
| 51 | NSString *appName = 0; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); |
|---|
| 55 | if (dict) |
|---|
| 56 | appName = [dict objectForKey: @"CFBundleName"]; |
|---|
| 57 | |
|---|
| 58 | if (![appName length]) |
|---|
| 59 | appName = [[NSProcessInfo processInfo] processName]; |
|---|
| 60 | |
|---|
| 61 | return appName; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | #if SDL_USE_NIB_FILE |
|---|
| 65 | |
|---|
| 66 | @interface NSString (ReplaceSubString) |
|---|
| 67 | - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString; |
|---|
| 68 | @end |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | @interface SDLApplication : NSApplication |
|---|
| 72 | @end |
|---|
| 73 | |
|---|
| 74 | @implementation SDLApplication |
|---|
| 75 | |
|---|
| 76 | - (void)terminate:(id)sender |
|---|
| 77 | { |
|---|
| 78 | |
|---|
| 79 | SDL_Event event; |
|---|
| 80 | event.type = SDL_QUIT; |
|---|
| 81 | SDL_PushEvent(&event); |
|---|
| 82 | } |
|---|
| 83 | @end |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | @implementation SDLMain |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | - (void) setupWorkingDirectory:(BOOL)shouldChdir |
|---|
| 90 | { |
|---|
| 91 | if (shouldChdir) |
|---|
| 92 | { |
|---|
| 93 | char parentdir[MAXPATHLEN]; |
|---|
| 94 | CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle()); |
|---|
| 95 | CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url); |
|---|
| 96 | if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, MAXPATHLEN)) { |
|---|
| 97 | assert ( chdir (parentdir) == 0 ); |
|---|
| 98 | } |
|---|
| 99 | CFRelease(url); |
|---|
| 100 | CFRelease(url2); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | #if SDL_USE_NIB_FILE |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName |
|---|
| 109 | { |
|---|
| 110 | NSRange aRange; |
|---|
| 111 | NSEnumerator *enumerator; |
|---|
| 112 | NSMenuItem *menuItem; |
|---|
| 113 | |
|---|
| 114 | aRange = [[aMenu title] rangeOfString:@"SDL App"]; |
|---|
| 115 | if (aRange.length != 0) |
|---|
| 116 | [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]]; |
|---|
| 117 | |
|---|
| 118 | enumerator = [[aMenu itemArray] objectEnumerator]; |
|---|
| 119 | while ((menuItem = [enumerator nextObject])) |
|---|
| 120 | { |
|---|
| 121 | aRange = [[menuItem title] rangeOfString:@"SDL App"]; |
|---|
| 122 | if (aRange.length != 0) |
|---|
| 123 | [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]]; |
|---|
| 124 | if ([menuItem hasSubmenu]) |
|---|
| 125 | [self fixMenu:[menuItem submenu] withAppName:appName]; |
|---|
| 126 | } |
|---|
| 127 | [ aMenu sizeToFit ]; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | #else |
|---|
| 131 | |
|---|
| 132 | static void setApplicationMenu(void) |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | NSMenu *appleMenu; |
|---|
| 136 | NSMenuItem *menuItem; |
|---|
| 137 | NSString *title; |
|---|
| 138 | NSString *appName; |
|---|
| 139 | |
|---|
| 140 | appName = getApplicationName(); |
|---|
| 141 | appleMenu = [[NSMenu alloc] initWithTitle:@""]; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | title = [@"About " stringByAppendingString:appName]; |
|---|
| 145 | [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; |
|---|
| 146 | |
|---|
| 147 | [appleMenu addItem:[NSMenuItem separatorItem]]; |
|---|
| 148 | |
|---|
| 149 | title = [@"Hide " stringByAppendingString:appName]; |
|---|
| 150 | [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"]; |
|---|
| 151 | |
|---|
| 152 | menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; |
|---|
| 153 | [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; |
|---|
| 154 | |
|---|
| 155 | [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; |
|---|
| 156 | |
|---|
| 157 | [appleMenu addItem:[NSMenuItem separatorItem]]; |
|---|
| 158 | |
|---|
| 159 | title = [@"Quit " stringByAppendingString:appName]; |
|---|
| 160 | [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; |
|---|
| 165 | [menuItem setSubmenu:appleMenu]; |
|---|
| 166 | [[NSApp mainMenu] addItem:menuItem]; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | [NSApp setAppleMenu:appleMenu]; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | [appleMenu release]; |
|---|
| 173 | [menuItem release]; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | static void setupWindowMenu(void) |
|---|
| 178 | { |
|---|
| 179 | NSMenu *windowMenu; |
|---|
| 180 | NSMenuItem *windowMenuItem; |
|---|
| 181 | NSMenuItem *menuItem; |
|---|
| 182 | |
|---|
| 183 | windowMenu = [[NSMenu alloc] initWithTitle:@"Window"]; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; |
|---|
| 187 | [windowMenu addItem:menuItem]; |
|---|
| 188 | [menuItem release]; |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""]; |
|---|
| 192 | [windowMenuItem setSubmenu:windowMenu]; |
|---|
| 193 | [[NSApp mainMenu] addItem:windowMenuItem]; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | [NSApp setWindowsMenu:windowMenu]; |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | [windowMenu release]; |
|---|
| 200 | [windowMenuItem release]; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | static void CustomApplicationMain (int argc, char **argv) |
|---|
| 205 | { |
|---|
| 206 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 207 | SDLMain *sdlMain; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | [SDLApplication sharedApplication]; |
|---|
| 211 | |
|---|
| 212 | #ifdef SDL_USE_CPS |
|---|
| 213 | { |
|---|
| 214 | CPSProcessSerNum PSN; |
|---|
| 215 | |
|---|
| 216 | if (!CPSGetCurrentProcess(&PSN)) |
|---|
| 217 | if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103)) |
|---|
| 218 | if (!CPSSetFrontProcess(&PSN)) |
|---|
| 219 | [SDLApplication sharedApplication]; |
|---|
| 220 | } |
|---|
| 221 | #endif |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
|---|
| 225 | setApplicationMenu(); |
|---|
| 226 | setupWindowMenu(); |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | sdlMain = [[SDLMain alloc] init]; |
|---|
| 230 | [NSApp setDelegate:sdlMain]; |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | [NSApp run]; |
|---|
| 234 | |
|---|
| 235 | [sdlMain release]; |
|---|
| 236 | [pool release]; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | #endif |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename |
|---|
| 258 | { |
|---|
| 259 | const char *temparg; |
|---|
| 260 | size_t arglen; |
|---|
| 261 | char *arg; |
|---|
| 262 | char **newargv; |
|---|
| 263 | |
|---|
| 264 | if (!gFinderLaunch) |
|---|
| 265 | return FALSE; |
|---|
| 266 | |
|---|
| 267 | if (gCalledAppMainline) |
|---|
| 268 | return FALSE; |
|---|
| 269 | |
|---|
| 270 | temparg = [filename UTF8String]; |
|---|
| 271 | arglen = SDL_strlen(temparg) + 1; |
|---|
| 272 | arg = (char *) SDL_malloc(arglen); |
|---|
| 273 | if (arg == NULL) |
|---|
| 274 | return FALSE; |
|---|
| 275 | |
|---|
| 276 | newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2)); |
|---|
| 277 | if (newargv == NULL) |
|---|
| 278 | { |
|---|
| 279 | SDL_free(arg); |
|---|
| 280 | return FALSE; |
|---|
| 281 | } |
|---|
| 282 | gArgv = newargv; |
|---|
| 283 | |
|---|
| 284 | SDL_strlcpy(arg, temparg, arglen); |
|---|
| 285 | gArgv[gArgc++] = arg; |
|---|
| 286 | gArgv[gArgc] = NULL; |
|---|
| 287 | return TRUE; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | - (void) applicationDidFinishLaunching: (NSNotification *) note |
|---|
| 293 | { |
|---|
| 294 | int status; |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | [self setupWorkingDirectory:gFinderLaunch]; |
|---|
| 298 | |
|---|
| 299 | #if SDL_USE_NIB_FILE |
|---|
| 300 | |
|---|
| 301 | [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()]; |
|---|
| 302 | #endif |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | gCalledAppMainline = TRUE; |
|---|
| 306 | status = SDL_main (gArgc, gArgv); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | exit(status); |
|---|
| 310 | } |
|---|
| 311 | @end |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | @implementation NSString (ReplaceSubString) |
|---|
| 315 | |
|---|
| 316 | - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString |
|---|
| 317 | { |
|---|
| 318 | unsigned int bufferSize; |
|---|
| 319 | unsigned int selfLen = [self length]; |
|---|
| 320 | unsigned int aStringLen = [aString length]; |
|---|
| 321 | unichar *buffer; |
|---|
| 322 | NSRange localRange; |
|---|
| 323 | NSString *result; |
|---|
| 324 | |
|---|
| 325 | bufferSize = selfLen + aStringLen - aRange.length; |
|---|
| 326 | buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar)); |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | localRange.location = 0; |
|---|
| 330 | localRange.length = aRange.location; |
|---|
| 331 | [self getCharacters:buffer range:localRange]; |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | localRange.location = 0; |
|---|
| 335 | localRange.length = aStringLen; |
|---|
| 336 | [aString getCharacters:(buffer+aRange.location) range:localRange]; |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | localRange.location = aRange.location + aRange.length; |
|---|
| 340 | localRange.length = selfLen - localRange.location; |
|---|
| 341 | [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | result = [NSString stringWithCharacters:buffer length:bufferSize]; |
|---|
| 345 | |
|---|
| 346 | NSDeallocateMemoryPages(buffer, bufferSize); |
|---|
| 347 | |
|---|
| 348 | return result; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | @end |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | #ifdef main |
|---|
| 356 | # undef main |
|---|
| 357 | #endif |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | int main (int argc, char **argv) |
|---|
| 362 | { |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { |
|---|
| 366 | gArgv = (char **) SDL_malloc(sizeof (char *) * 2); |
|---|
| 367 | gArgv[0] = argv[0]; |
|---|
| 368 | gArgv[1] = NULL; |
|---|
| 369 | gArgc = 1; |
|---|
| 370 | gFinderLaunch = YES; |
|---|
| 371 | } else { |
|---|
| 372 | int i; |
|---|
| 373 | gArgc = argc; |
|---|
| 374 | gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1)); |
|---|
| 375 | for (i = 0; i <= argc; i++) |
|---|
| 376 | gArgv[i] = argv[i]; |
|---|
| 377 | gFinderLaunch = NO; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | #if SDL_USE_NIB_FILE |
|---|
| 381 | [SDLApplication poseAsClass:[NSApplication class]]; |
|---|
| 382 | NSApplicationMain (argc, argv); |
|---|
| 383 | #else |
|---|
| 384 | CustomApplicationMain (argc, argv); |
|---|
| 385 | #endif |
|---|
| 386 | return 0; |
|---|
| 387 | } |
|---|