src/macosx/SDLMain.mm

Go to the documentation of this file.
00001 /*   SDLMain.m - main entry point for our Cocoa-ized SDL app
00002        Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
00003        Non-NIB-Code & other changes: Max Horn <max@quendi.de>
00004 
00005 */
00006 #import <AppKit/AppKit.h>
00007 #import <SDL/SDL.h>
00008 #import "SDLMain.h"
00009 #import <sys/param.h> /* for MAXPATHLEN */
00010 #import <unistd.h>
00011 #include "aa_config.h"
00012 #include "tError.h"
00013 #include "tConfiguration.h"
00014 #include "uMenu.h"
00015 #include "AAURLHandler.h"
00016 #include "AAGrowlPlugin.h"
00017 
00018 /* Use this flag to determine whether we use SDLMain.nib or not */
00019 #define         SDL_USE_NIB_FILE        1
00020 
00021 static int    gArgc;
00022 static char  **gArgv;
00023 static BOOL   gFinderLaunch;
00024 
00025 #if SDL_USE_NIB_FILE
00026 /* A helper category for NSString */
00027 @interface NSString (ReplaceSubString)
00028 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
00029 @end
00030 #else
00031 /* An internal Apple class used to setup Apple menus */
00032 @interface NSAppleMenuController:NSObject {}
00033 - (void)controlMenu:(NSMenu *)aMenu;
00034 @end
00035 #endif
00036 
00037 SDL_Event event;
00038 @interface SDLApplication : NSApplication
00039 @end
00040 
00041 @implementation SDLApplication
00042 /* Invoked from the Quit menu item */
00043 
00044 - (void)terminate:(id)sender
00045 {
00046     st_SaveConfig();
00047     uMenu::quickexit=true;  
00048 }
00049 
00050 @end
00051 
00053 void MacOSX_SetCWD(char **argv) {
00054     char buffer;
00055     int count = 2, i;
00056     strcpy(buffer, argv[0]);
00057     
00058     if ( !strstr(buffer, ".app") )
00059     {
00060         //it's not a .app bundle
00061         return;
00062     }
00063 
00064     for ( i = strlen(buffer); i > 0 && count > 0; i-- )
00065     {
00066         if (buffer[i] == '/')
00067             count--;
00068     }
00069     
00070     if ( i == 0 )
00071         return;
00072     
00073     i+=2;
00074     buffer = 0;
00075     strcat( buffer, "Resources");
00076     chdir( buffer );
00077 }
00078 
00079 /* The main class of the application, the application's delegate */
00080 @implementation SDLMain
00081 
00082 - (void)quit:(id)sender
00083 {
00084     st_SaveConfig();
00085     uMenu::quickexit=true;    
00086 }
00087 
00088 /* Fix menu to contain the real app name instead of "SDL App" */
00089 - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
00090 {
00091     NSRange aRange;
00092     NSEnumerator *enumerator;
00093     NSMenuItem *menuItem;
00094 
00095     aRange = [[aMenu title] rangeOfString:@"SDL App"];
00096     if (aRange.length != 0)
00097         [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
00098 
00099     enumerator = [[aMenu itemArray] objectEnumerator];
00100     while ((menuItem = [enumerator nextObject]))
00101     {
00102         aRange = [[menuItem title] rangeOfString:@"SDL App"];
00103         if (aRange.length != 0)
00104             [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
00105         if ([menuItem hasSubmenu])
00106             [self fixMenu:[menuItem submenu] withAppName:appName];
00107     }
00108     [aMenu sizeToFit];
00109 }
00110 
00111 /* Called when the internal event loop has just started running */
00112 - (void) applicationDidFinishLaunching: (NSNotification *) note
00113 {
00114     int status;
00115 
00116 #if SDL_USE_NIB_FILE
00117     /* Set the main menu to contain the real app name instead of "SDL App" */
00118     [self fixMenu:[NSApp mainMenu] withAppName:[[NSProcessInfo processInfo] processName]];
00119 #endif
00120 
00121     /* Hand off to main application code */
00122     status = SDL_main(gArgc, gArgv);
00123     
00124     /* We're done, thank you for playing */
00125     exit(status);
00126 }
00127 @end
00128 
00129 
00130 @implementation NSString (ReplaceSubString)
00131 
00132 - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
00133 {
00134     unsigned int bufferSize;
00135     unsigned int selfLen = [self length];
00136     unsigned int aStringLen = [aString length];
00137     unichar *buffer;
00138     NSRange localRange;
00139     NSString *result;
00140 
00141     bufferSize = selfLen + aStringLen - aRange.length;
00142     buffer = (unichar *) NSAllocateMemoryPages(bufferSize*sizeof(unichar));
00143     
00144     /* Get first part into buffer */
00145     localRange.location = 0;
00146     localRange.length = aRange.location;
00147     [self getCharacters:buffer range:localRange];
00148     
00149     /* Get middle part into buffer */
00150     localRange.location = 0;
00151     localRange.length = aStringLen;
00152     [aString getCharacters:(buffer+aRange.location) range:localRange];
00153      
00154     /* Get last part into buffer */
00155     localRange.location = aRange.location + aRange.length;
00156     localRange.length = selfLen - localRange.location;
00157     [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
00158     
00159     /* Build output string */
00160     result = [NSString stringWithCharacters:buffer length:bufferSize];
00161     NSDeallocateMemoryPages(buffer, bufferSize);
00162     
00163     return result;
00164 }
00165 
00166 @end
00167 
00168 #ifdef main
00169 #  undef main
00170 #endif
00171 
00172 /* Main entry point to executable - should *not* be SDL_main! */
00173 int main (int argc, char **argv)
00174 {
00175     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00176     AAGrowlPlugin *growl = [[AAGrowlPlugin alloc] init];
00177     [growl startGrowling];
00178     SetupAAURLHandler();
00179 
00180     /* This is passed if we are launched by double-clicking */
00181     if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 )
00182     {
00183         gArgc = 1;
00184         gFinderLaunch = YES;
00185     }
00186     else
00187     {
00188         gArgc = argc;
00189         gFinderLaunch = NO;
00190     }
00191 
00192     gArgv = (char**)malloc( sizeof(*gArgv) * (gArgc+1) );
00193     tASSERT (gArgv != NULL);
00194     int i = 0;
00195     for (i; i < gArgc; i++)
00196     {
00197         gArgv = (char *) argv[i];
00198     }
00199 
00200     gArgv = NULL;
00201         MacOSX_SetCWD(gArgv);
00202     
00203 #if SDL_USE_NIB_FILE
00204     [SDLApplication poseAsClass:[NSApplication class]];
00205     NSApplicationMain (argc, (const char **)argv);
00206 #else
00207     CustomApplicationMain (argc, argv);
00208 #endif
00209     CleanupAAURLHandler();
00210     [growl release];
00211     [pool release];
00212     return 0;
00213 }

Generated on Sat Mar 15 22:55:48 2008 for Armagetron Advanced by  doxygen 1.5.4