[2] | 1 | /* SDLMain.m - main entry point for our Cocoa-ized SDL app |
---|
| 2 | Initial Version: Darrell Walisser <dwaliss1@purdue.edu> |
---|
| 3 | Non-NIB-Code & other changes: Max Horn <max@quendi.de> |
---|
| 4 | |
---|
| 5 | Feel free to customize this file to suit your needs |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #import "SDL.h" |
---|
| 9 | #import <Cocoa/Cocoa.h> |
---|
| 10 | |
---|
| 11 | @interface SDLMain : NSObject |
---|
| 12 | @end |
---|
| 13 | |
---|
| 14 | #import <sys/param.h> /* for MAXPATHLEN */ |
---|
| 15 | #import <unistd.h> |
---|
| 16 | |
---|
| 17 | /* Use this flag to determine whether we use SDLMain.nib or not */ |
---|
| 18 | #define SDL_USE_NIB_FILE 1 |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | static int gArgc; |
---|
| 22 | static char **gArgv; |
---|
| 23 | static BOOL gFinderLaunch; |
---|
| 24 | |
---|
| 25 | #if SDL_USE_NIB_FILE |
---|
| 26 | /* A helper category for NSString */ |
---|
| 27 | @interface NSString (ReplaceSubString) |
---|
| 28 | - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString; |
---|
| 29 | @end |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | @interface SDLApplication : NSApplication |
---|
| 33 | @end |
---|
| 34 | |
---|
| 35 | @implementation SDLApplication |
---|
| 36 | /* Invoked from the Quit menu item */ |
---|
| 37 | - (void)terminate:(id)sender |
---|
| 38 | { |
---|
| 39 | /* Post a SDL_QUIT event */ |
---|
| 40 | SDL_Event event; |
---|
| 41 | event.type = SDL_QUIT; |
---|
| 42 | SDL_PushEvent(&event); |
---|
| 43 | } |
---|
| 44 | @end |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | /* The main class of the application, the application's delegate */ |
---|
| 48 | @implementation SDLMain |
---|
| 49 | - (void)quit:(id)sender |
---|
| 50 | { |
---|
| 51 | /* Post a SDL_QUIT event */ |
---|
| 52 | SDL_Event event; |
---|
| 53 | event.type = SDL_QUIT; |
---|
| 54 | SDL_PushEvent(&event); |
---|
| 55 | } |
---|
| 56 | /* Set the working directory to the .app's parent directory */ |
---|
| 57 | - (void) setupWorkingDirectory:(BOOL)shouldChdir |
---|
| 58 | { |
---|
| 59 | char parentdir[MAXPATHLEN]; |
---|
| 60 | char *c; |
---|
| 61 | |
---|
| 62 | strncpy ( parentdir, gArgv[0], sizeof(parentdir) ); |
---|
| 63 | c = (char*) parentdir; |
---|
| 64 | |
---|
| 65 | while (*c != '\0') /* go to end */ |
---|
| 66 | c++; |
---|
| 67 | |
---|
| 68 | while (*c != '/') /* back up to parent */ |
---|
| 69 | c--; |
---|
| 70 | |
---|
| 71 | *c++ = '\0'; /* cut off last part (binary name) */ |
---|
| 72 | |
---|
| 73 | if (shouldChdir) |
---|
| 74 | { |
---|
| 75 | assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */ |
---|
| 76 | assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */ |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | #if SDL_USE_NIB_FILE |
---|
| 81 | |
---|
| 82 | /* Fix menu to contain the real app name instead of "SDL App" */ |
---|
| 83 | - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName |
---|
| 84 | { |
---|
| 85 | NSRange aRange; |
---|
| 86 | NSEnumerator *enumerator; |
---|
| 87 | NSMenuItem *menuItem; |
---|
| 88 | |
---|
| 89 | aRange = [[aMenu title] rangeOfString:@"SDL App"]; |
---|
| 90 | if (aRange.length != 0) |
---|
| 91 | [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]]; |
---|
| 92 | |
---|
| 93 | enumerator = [[aMenu itemArray] objectEnumerator]; |
---|
| 94 | while ((menuItem = [enumerator nextObject])) |
---|
| 95 | { |
---|
| 96 | aRange = [[menuItem title] rangeOfString:@"SDL App"]; |
---|
| 97 | if (aRange.length != 0) |
---|
| 98 | [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]]; |
---|
| 99 | if ([menuItem hasSubmenu]) |
---|
| 100 | [self fixMenu:[menuItem submenu] withAppName:appName]; |
---|
| 101 | } |
---|
| 102 | [ aMenu sizeToFit ]; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | #endif |
---|
| 106 | |
---|
| 107 | /* Called when the internal event loop has just started running */ |
---|
| 108 | - (void) applicationDidFinishLaunching: (NSNotification *) note |
---|
| 109 | { |
---|
| 110 | int status; |
---|
| 111 | |
---|
| 112 | /* Set the working directory to the .app's parent directory */ |
---|
| 113 | [self setupWorkingDirectory:gFinderLaunch]; |
---|
| 114 | |
---|
| 115 | #if SDL_USE_NIB_FILE |
---|
| 116 | /* Set the main menu to contain the real app name instead of "SDL App" */ |
---|
| 117 | [self fixMenu:[NSApp mainMenu] withAppName:[[NSProcessInfo processInfo] processName]]; |
---|
| 118 | #endif |
---|
| 119 | |
---|
| 120 | /* Hand off to main application code */ |
---|
| 121 | status = SDL_main (gArgc, gArgv); |
---|
| 122 | |
---|
| 123 | /* We're done, thank you for playing */ |
---|
| 124 | exit(status); |
---|
| 125 | } |
---|
| 126 | @end |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | @implementation NSString (ReplaceSubString) |
---|
| 130 | |
---|
| 131 | - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString |
---|
| 132 | { |
---|
| 133 | unsigned int bufferSize; |
---|
| 134 | unsigned int selfLen = [self length]; |
---|
| 135 | unsigned int aStringLen = [aString length]; |
---|
| 136 | unichar *buffer; |
---|
| 137 | NSRange localRange; |
---|
| 138 | NSString *result; |
---|
| 139 | |
---|
| 140 | bufferSize = selfLen + aStringLen - aRange.length; |
---|
| 141 | buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar)); |
---|
| 142 | |
---|
| 143 | /* Get first part into buffer */ |
---|
| 144 | localRange.location = 0; |
---|
| 145 | localRange.length = aRange.location; |
---|
| 146 | [self getCharacters:buffer range:localRange]; |
---|
| 147 | |
---|
| 148 | /* Get middle part into buffer */ |
---|
| 149 | localRange.location = 0; |
---|
| 150 | localRange.length = aStringLen; |
---|
| 151 | [aString getCharacters:(buffer+aRange.location) range:localRange]; |
---|
| 152 | |
---|
| 153 | /* Get last part into buffer */ |
---|
| 154 | localRange.location = aRange.location + aRange.length; |
---|
| 155 | localRange.length = selfLen - localRange.location; |
---|
| 156 | [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; |
---|
| 157 | |
---|
| 158 | /* Build output string */ |
---|
| 159 | result = [NSString stringWithCharacters:buffer length:bufferSize]; |
---|
| 160 | |
---|
| 161 | NSDeallocateMemoryPages(buffer, bufferSize); |
---|
| 162 | |
---|
| 163 | return result; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | @end |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | #ifdef main |
---|
| 171 | # undef main |
---|
| 172 | #endif |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | /* Main entry point to executable - should *not* be SDL_main! */ |
---|
| 176 | int main (int argc, char **argv) |
---|
| 177 | { |
---|
| 178 | |
---|
| 179 | /* Copy the arguments into a global variable */ |
---|
| 180 | int i; |
---|
| 181 | |
---|
| 182 | /* This is passed if we are launched by double-clicking */ |
---|
| 183 | if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { |
---|
| 184 | gArgc = 1; |
---|
| 185 | gFinderLaunch = YES; |
---|
| 186 | } else { |
---|
| 187 | gArgc = argc; |
---|
| 188 | gFinderLaunch = NO; |
---|
| 189 | } |
---|
| 190 | gArgv = (char**) malloc (sizeof(*gArgv) * (gArgc+1)); |
---|
| 191 | assert (gArgv != NULL); |
---|
| 192 | for (i = 0; i < gArgc; i++) |
---|
| 193 | gArgv[i] = argv[i]; |
---|
| 194 | gArgv[i] = NULL; |
---|
| 195 | |
---|
| 196 | #if SDL_USE_NIB_FILE |
---|
| 197 | [SDLApplication poseAsClass:[NSApplication class]]; |
---|
| 198 | NSApplicationMain (argc, argv); |
---|
| 199 | #endif |
---|
| 200 | return 0; |
---|
| 201 | } |
---|