SDLmain.m: now with more tabs

This commit is contained in:
Alex Szpakowski
2013-03-14 16:11:17 -03:00
parent c730906c26
commit 596bb2245e
+184 -184
View File
@@ -1,8 +1,8 @@
/* SDLMain.m - main entry point for our Cocoa-ized SDL app /* SDLMain.m - main entry point for our Cocoa-ized SDL app
Initial Version: Darrell Walisser <dwaliss1@purdue.edu> Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
Non-NIB-Code & other changes: Max Horn <max@quendi.de> Non-NIB-Code & other changes: Max Horn <max@quendi.de>
Feel free to customize this file to suit your needs Feel free to customize this file to suit your needs
*/ */
//#import "SDL.h" //#import "SDL.h"
@@ -43,18 +43,18 @@ static BOOL gCalledAppMainline = FALSE;
static NSString *getApplicationName(void) static NSString *getApplicationName(void)
{ {
NSDictionary *dict; NSDictionary *dict;
NSString *appName = 0; NSString *appName = 0;
/* Determine the application name */ /* Determine the application name */
dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
if (dict) if (dict)
appName = [dict objectForKey: @"CFBundleName"]; appName = [dict objectForKey: @"CFBundleName"];
if (![appName length]) if (![appName length])
appName = [[NSProcessInfo processInfo] processName]; appName = [[NSProcessInfo processInfo] processName];
return appName; return appName;
} }
#if SDL_USE_NIB_FILE #if SDL_USE_NIB_FILE
@@ -71,10 +71,10 @@ static NSString *getApplicationName(void)
/* Invoked from the Quit menu item */ /* Invoked from the Quit menu item */
- (void)terminate:(id)sender - (void)terminate:(id)sender
{ {
/* Post a SDL_QUIT event */ /* Post a SDL_QUIT event */
SDL_Event event; SDL_Event event;
event.type = SDL_QUIT; event.type = SDL_QUIT;
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
/* Hack to make Cocoa ignore keystrokes that aren't shortcuts, otherwise it beeps on every key press */ /* Hack to make Cocoa ignore keystrokes that aren't shortcuts, otherwise it beeps on every key press */
@@ -95,15 +95,15 @@ static NSString *getApplicationName(void)
/*CHANGED to Bundle's Resource Directory. */ /*CHANGED to Bundle's Resource Directory. */
- (void) setupWorkingDirectory:(BOOL)shouldChdir - (void) setupWorkingDirectory:(BOOL)shouldChdir
{ {
if (shouldChdir) if (shouldChdir)
{ {
/* /*
char parentdir[PATH_MAX]; char parentdir[PATH_MAX];
//CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle()); //CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFURLRef url = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()); CFURLRef url = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url); CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, PATH_MAX)) { if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, PATH_MAX)) {
assert ( chdir (parentdir) == 0 ); // chdir to the binary app's parent // assert ( chdir (parentdir) == 0 ); // chdir to the binary app's parent //
} }
CFRelease(url); CFRelease(url);
CFRelease(url2); CFRelease(url2);
@@ -113,7 +113,7 @@ static NSString *getApplicationName(void)
char path[PATH_MAX]; char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX)) if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{ {
// error! // error!
} }
CFRelease(resourcesURL); CFRelease(resourcesURL);
chdir(path); chdir(path);
@@ -126,133 +126,133 @@ static NSString *getApplicationName(void)
/* Fix menu to contain the real app name instead of "SDL App" */ /* Fix menu to contain the real app name instead of "SDL App" */
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
{ {
NSRange aRange; NSRange aRange;
NSEnumerator *enumerator; NSEnumerator *enumerator;
NSMenuItem *menuItem; NSMenuItem *menuItem;
aRange = [[aMenu title] rangeOfString:@"SDL App"]; aRange = [[aMenu title] rangeOfString:@"SDL App"];
if (aRange.length != 0) if (aRange.length != 0)
[aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]]; [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
enumerator = [[aMenu itemArray] objectEnumerator]; enumerator = [[aMenu itemArray] objectEnumerator];
while ((menuItem = [enumerator nextObject])) while ((menuItem = [enumerator nextObject]))
{ {
aRange = [[menuItem title] rangeOfString:@"SDL App"]; aRange = [[menuItem title] rangeOfString:@"SDL App"];
if (aRange.length != 0) if (aRange.length != 0)
[menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]]; [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
if ([menuItem hasSubmenu]) if ([menuItem hasSubmenu])
[self fixMenu:[menuItem submenu] withAppName:appName]; [self fixMenu:[menuItem submenu] withAppName:appName];
} }
[ aMenu sizeToFit ]; [ aMenu sizeToFit ];
} }
#else #else
static void setApplicationMenu(void) static void setApplicationMenu(void)
{ {
/* warning: this code is very odd */ /* warning: this code is very odd */
NSMenu *appleMenu; NSMenu *appleMenu;
NSMenuItem *menuItem; NSMenuItem *menuItem;
NSString *title; NSString *title;
NSString *appName; NSString *appName;
appName = getApplicationName(); appName = getApplicationName();
appleMenu = [[NSMenu alloc] initWithTitle:@""]; appleMenu = [[NSMenu alloc] initWithTitle:@""];
/* Add menu items */ /* Add menu items */
title = [@"About " stringByAppendingString:appName]; title = [@"About " stringByAppendingString:appName];
[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
[appleMenu addItem:[NSMenuItem separatorItem]]; [appleMenu addItem:[NSMenuItem separatorItem]];
title = [@"Hide " stringByAppendingString:appName]; title = [@"Hide " stringByAppendingString:appName];
[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"]; [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
[appleMenu addItem:[NSMenuItem separatorItem]]; [appleMenu addItem:[NSMenuItem separatorItem]];
title = [@"Quit " stringByAppendingString:appName]; title = [@"Quit " stringByAppendingString:appName];
[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
/* Put menu into the menubar */ /* Put menu into the menubar */
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[menuItem setSubmenu:appleMenu]; [menuItem setSubmenu:appleMenu];
[[NSApp mainMenu] addItem:menuItem]; [[NSApp mainMenu] addItem:menuItem];
/* Tell the application object that this is now the application menu */ /* Tell the application object that this is now the application menu */
[NSApp setAppleMenu:appleMenu]; [NSApp setAppleMenu:appleMenu];
/* Finally give up our references to the objects */ /* Finally give up our references to the objects */
[appleMenu release]; [appleMenu release];
[menuItem release]; [menuItem release];
} }
/* Create a window menu */ /* Create a window menu */
static void setupWindowMenu(void) static void setupWindowMenu(void)
{ {
NSMenu *windowMenu; NSMenu *windowMenu;
NSMenuItem *windowMenuItem; NSMenuItem *windowMenuItem;
NSMenuItem *menuItem; NSMenuItem *menuItem;
windowMenu = [[NSMenu alloc] initWithTitle:@"Window"]; windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
/* "Minimize" item */ /* "Minimize" item */
menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
[windowMenu addItem:menuItem]; [windowMenu addItem:menuItem];
[menuItem release]; [menuItem release];
/* Put menu into the menubar */ /* Put menu into the menubar */
windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""]; windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
[windowMenuItem setSubmenu:windowMenu]; [windowMenuItem setSubmenu:windowMenu];
[[NSApp mainMenu] addItem:windowMenuItem]; [[NSApp mainMenu] addItem:windowMenuItem];
/* Tell the application object that this is now the window menu */ /* Tell the application object that this is now the window menu */
[NSApp setWindowsMenu:windowMenu]; [NSApp setWindowsMenu:windowMenu];
/* Finally give up our references to the objects */ /* Finally give up our references to the objects */
[windowMenu release]; [windowMenu release];
[windowMenuItem release]; [windowMenuItem release];
} }
/* Replacement for NSApplicationMain */ /* Replacement for NSApplicationMain */
static void CustomApplicationMain (int argc, char **argv) static void CustomApplicationMain (int argc, char **argv)
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDLMain *sdlMain; SDLMain *sdlMain;
/* Ensure the application object is initialised */ /* Ensure the application object is initialised */
[SDLApplication sharedApplication]; [SDLApplication sharedApplication];
#ifdef SDL_USE_CPS #ifdef SDL_USE_CPS
{ {
CPSProcessSerNum PSN; CPSProcessSerNum PSN;
/* Tell the dock about us */ /* Tell the dock about us */
if (!CPSGetCurrentProcess(&PSN)) if (!CPSGetCurrentProcess(&PSN))
if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103)) if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
if (!CPSSetFrontProcess(&PSN)) if (!CPSSetFrontProcess(&PSN))
[SDLApplication sharedApplication]; [SDLApplication sharedApplication];
} }
#endif /* SDL_USE_CPS */ #endif /* SDL_USE_CPS */
/* Set up the menubar */ /* Set up the menubar */
[NSApp setMainMenu:[[[NSMenu alloc] init] autorelease]]; [NSApp setMainMenu:[[[NSMenu alloc] init] autorelease]];
setApplicationMenu(); setApplicationMenu();
setupWindowMenu(); setupWindowMenu();
/* Create SDLMain and make it the app delegate */ /* Create SDLMain and make it the app delegate */
sdlMain = [[SDLMain alloc] init]; sdlMain = [[SDLMain alloc] init];
[NSApp setDelegate:sdlMain]; [NSApp setDelegate:sdlMain];
/* Start the main event loop */ /* Start the main event loop */
[NSApp run]; [NSApp run];
[sdlMain release]; [sdlMain release];
[pool release]; [pool release];
} }
#endif #endif
@@ -275,60 +275,60 @@ static void CustomApplicationMain (int argc, char **argv)
*/ */
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{ {
const char *temparg; const char *temparg;
size_t arglen; size_t arglen;
char *arg; char *arg;
char **newargv; char **newargv;
if (!gFinderLaunch) /* MacOS is passing command line args. */ if (!gFinderLaunch) /* MacOS is passing command line args. */
return FALSE; return FALSE;
if (gCalledAppMainline) /* app has started, ignore this document. */ if (gCalledAppMainline) /* app has started, ignore this document. */
return FALSE; return FALSE;
temparg = [filename UTF8String]; temparg = [filename UTF8String];
arglen = SDL_strlen(temparg) + 1; arglen = SDL_strlen(temparg) + 1;
arg = (char *) SDL_malloc(arglen); arg = (char *) SDL_malloc(arglen);
if (arg == NULL) if (arg == NULL)
return FALSE; return FALSE;
newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2)); newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
if (newargv == NULL) if (newargv == NULL)
{ {
SDL_free(arg); SDL_free(arg);
return FALSE; return FALSE;
} }
gArgv = newargv; gArgv = newargv;
SDL_strlcpy(arg, temparg, arglen); SDL_strlcpy(arg, temparg, arglen);
gArgv[gArgc++] = arg; gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL; gArgv[gArgc] = NULL;
return TRUE; return TRUE;
} }
/* Called when the internal event loop has just started running */ /* Called when the internal event loop has just started running */
- (void) applicationDidFinishLaunching: (NSNotification *) note - (void) applicationDidFinishLaunching: (NSNotification *) note
{ {
int status; int status;
/* Set the working directory to the .app's parent directory */ /* Set the working directory to the .app's parent directory */
[self setupWorkingDirectory:gFinderLaunch]; [self setupWorkingDirectory:gFinderLaunch];
#if SDL_USE_NIB_FILE #if SDL_USE_NIB_FILE
/* Set the main menu to contain the real app name instead of "SDL App" */ /* Set the main menu to contain the real app name instead of "SDL App" */
[self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()]; [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
#endif #endif
/* Set up the app to receive menu events via keyboard shortcut */ /* Set up the app to receive menu events via keyboard shortcut */
setenv("SDL_ENABLEAPPEVENTS", "1", 1); setenv("SDL_ENABLEAPPEVENTS", "1", 1);
/* Hand off to main application code */ /* Hand off to main application code */
gCalledAppMainline = TRUE; gCalledAppMainline = TRUE;
status = SDL_main (gArgc, gArgv); status = SDL_main (gArgc, gArgv);
/* We're done, thank you for playing */ /* We're done, thank you for playing */
exit(status); exit(status);
} }
@end @end
@@ -337,37 +337,37 @@ static void CustomApplicationMain (int argc, char **argv)
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
{ {
unsigned int bufferSize; unsigned int bufferSize;
unsigned int selfLen = [self length]; unsigned int selfLen = [self length];
unsigned int aStringLen = [aString length]; unsigned int aStringLen = [aString length];
unichar *buffer; unichar *buffer;
NSRange localRange; NSRange localRange;
NSString *result; NSString *result;
bufferSize = selfLen + aStringLen - aRange.length; bufferSize = selfLen + aStringLen - aRange.length;
buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar)); buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
/* Get first part into buffer */ /* Get first part into buffer */
localRange.location = 0; localRange.location = 0;
localRange.length = aRange.location; localRange.length = aRange.location;
[self getCharacters:buffer range:localRange]; [self getCharacters:buffer range:localRange];
/* Get middle part into buffer */ /* Get middle part into buffer */
localRange.location = 0; localRange.location = 0;
localRange.length = aStringLen; localRange.length = aStringLen;
[aString getCharacters:(buffer+aRange.location) range:localRange]; [aString getCharacters:(buffer+aRange.location) range:localRange];
/* Get last part into buffer */ /* Get last part into buffer */
localRange.location = aRange.location + aRange.length; localRange.location = aRange.location + aRange.length;
localRange.length = selfLen - localRange.location; localRange.length = selfLen - localRange.location;
[self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
/* Build output string */ /* Build output string */
result = [NSString stringWithCharacters:buffer length:bufferSize]; result = [NSString stringWithCharacters:buffer length:bufferSize];
NSDeallocateMemoryPages(buffer, bufferSize); NSDeallocateMemoryPages(buffer, bufferSize);
return result; return result;
} }
@end @end
@@ -382,25 +382,25 @@ static void CustomApplicationMain (int argc, char **argv)
/* Main entry point to executable - should *not* be SDL_main! */ /* Main entry point to executable - should *not* be SDL_main! */
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
/* Copy the arguments into a global variable */ /* Copy the arguments into a global variable */
/* This is passed if we are launched by double-clicking */ /* This is passed if we are launched by double-clicking */
if (argc >= 2 && strncmp (argv[1], "-psn", 4) == 0) if (argc >= 2 && strncmp (argv[1], "-psn", 4) == 0)
{ {
gArgv = (char **) SDL_malloc(sizeof (char *) * 2); gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
gArgv[0] = argv[0]; gArgv[0] = argv[0];
gArgv[1] = NULL; gArgv[1] = NULL;
gArgc = 1; gArgc = 1;
gFinderLaunch = YES; gFinderLaunch = YES;
} }
else else
{ {
int i; int i;
gArgc = argc; gArgc = argc;
gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1)); gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
for (i = 0; i <= argc; i++) for (i = 0; i <= argc; i++)
gArgv[i] = argv[i]; gArgv[i] = argv[i];
gFinderLaunch = NO; gFinderLaunch = NO;
} }
/* check to see if there are any .love files in Resources - props to stevejohnson/diordna */ /* check to see if there are any .love files in Resources - props to stevejohnson/diordna */
NSArray *lovePaths = [[NSBundle mainBundle] pathsForResourcesOfType:@"love" inDirectory:nil]; NSArray *lovePaths = [[NSBundle mainBundle] pathsForResourcesOfType:@"love" inDirectory:nil];
@@ -437,10 +437,10 @@ int main (int argc, char **argv)
} }
#if SDL_USE_NIB_FILE #if SDL_USE_NIB_FILE
[SDLApplication poseAsClass:[NSApplication class]]; [SDLApplication poseAsClass:[NSApplication class]];
NSApplicationMain (argc, argv); NSApplicationMain (argc, argv);
#else #else
CustomApplicationMain (argc, argv); CustomApplicationMain (argc, argv);
#endif #endif
return 0; return 0;
} }