Xcode/TemplatesForXcodeTiger/SDL OpenGL Application/SDLMain.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Sep 2011 14:44:37 -0400
branchSDL-1.2
changeset 5895 402eac854030
child 12048 eb70ab731d29
permissions -rw-r--r--
Unpacked Xcode.tar.gz.
icculus@5895
     1
/*   SDLMain.m - main entry point for our Cocoa-ized SDL app
icculus@5895
     2
       Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
icculus@5895
     3
       Non-NIB-Code & other changes: Max Horn <max@quendi.de>
icculus@5895
     4
icculus@5895
     5
    Feel free to customize this file to suit your needs
icculus@5895
     6
*/
icculus@5895
     7
icculus@5895
     8
#include "SDL.h"
icculus@5895
     9
#include "SDLMain.h"
icculus@5895
    10
#include <sys/param.h> /* for MAXPATHLEN */
icculus@5895
    11
#include <unistd.h>
icculus@5895
    12
icculus@5895
    13
/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
icculus@5895
    14
 but the method still is there and works. To avoid warnings, we declare
icculus@5895
    15
 it ourselves here. */
icculus@5895
    16
@interface NSApplication(SDL_Missing_Methods)
icculus@5895
    17
- (void)setAppleMenu:(NSMenu *)menu;
icculus@5895
    18
@end
icculus@5895
    19
icculus@5895
    20
/* Use this flag to determine whether we use SDLMain.nib or not */
icculus@5895
    21
#define		SDL_USE_NIB_FILE	0
icculus@5895
    22
icculus@5895
    23
/* Use this flag to determine whether we use CPS (docking) or not */
icculus@5895
    24
#define		SDL_USE_CPS		1
icculus@5895
    25
#ifdef SDL_USE_CPS
icculus@5895
    26
/* Portions of CPS.h */
icculus@5895
    27
typedef struct CPSProcessSerNum
icculus@5895
    28
{
icculus@5895
    29
	UInt32		lo;
icculus@5895
    30
	UInt32		hi;
icculus@5895
    31
} CPSProcessSerNum;
icculus@5895
    32
icculus@5895
    33
extern OSErr	CPSGetCurrentProcess( CPSProcessSerNum *psn);
icculus@5895
    34
extern OSErr 	CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
icculus@5895
    35
extern OSErr	CPSSetFrontProcess( CPSProcessSerNum *psn);
icculus@5895
    36
icculus@5895
    37
#endif /* SDL_USE_CPS */
icculus@5895
    38
icculus@5895
    39
static int    gArgc;
icculus@5895
    40
static char  **gArgv;
icculus@5895
    41
static BOOL   gFinderLaunch;
icculus@5895
    42
static BOOL   gCalledAppMainline = FALSE;
icculus@5895
    43
icculus@5895
    44
static NSString *getApplicationName(void)
icculus@5895
    45
{
icculus@5895
    46
    const NSDictionary *dict;
icculus@5895
    47
    NSString *appName = 0;
icculus@5895
    48
icculus@5895
    49
    /* Determine the application name */
icculus@5895
    50
    dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
icculus@5895
    51
    if (dict)
icculus@5895
    52
        appName = [dict objectForKey: @"CFBundleName"];
icculus@5895
    53
    
icculus@5895
    54
    if (![appName length])
icculus@5895
    55
        appName = [[NSProcessInfo processInfo] processName];
icculus@5895
    56
icculus@5895
    57
    return appName;
icculus@5895
    58
}
icculus@5895
    59
icculus@5895
    60
#if SDL_USE_NIB_FILE
icculus@5895
    61
/* A helper category for NSString */
icculus@5895
    62
@interface NSString (ReplaceSubString)
icculus@5895
    63
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
icculus@5895
    64
@end
icculus@5895
    65
#endif
icculus@5895
    66
icculus@5895
    67
@interface SDLApplication : NSApplication
icculus@5895
    68
@end
icculus@5895
    69
icculus@5895
    70
@implementation SDLApplication
icculus@5895
    71
/* Invoked from the Quit menu item */
icculus@5895
    72
- (void)terminate:(id)sender
icculus@5895
    73
{
icculus@5895
    74
    /* Post a SDL_QUIT event */
icculus@5895
    75
    SDL_Event event;
icculus@5895
    76
    event.type = SDL_QUIT;
icculus@5895
    77
    SDL_PushEvent(&event);
icculus@5895
    78
}
icculus@5895
    79
@end
icculus@5895
    80
icculus@5895
    81
/* The main class of the application, the application's delegate */
icculus@5895
    82
@implementation SDLMain
icculus@5895
    83
icculus@5895
    84
/* Set the working directory to the .app's parent directory */
icculus@5895
    85
- (void) setupWorkingDirectory:(BOOL)shouldChdir
icculus@5895
    86
{
icculus@5895
    87
    if (shouldChdir)
icculus@5895
    88
    {
icculus@5895
    89
        char parentdir[MAXPATHLEN];
icculus@5895
    90
        CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
icculus@5895
    91
        CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
icculus@5895
    92
        if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
icculus@5895
    93
            chdir(parentdir);   /* chdir to the binary app's parent */
icculus@5895
    94
        }
icculus@5895
    95
        CFRelease(url);
icculus@5895
    96
        CFRelease(url2);
icculus@5895
    97
    }
icculus@5895
    98
}
icculus@5895
    99
icculus@5895
   100
#if SDL_USE_NIB_FILE
icculus@5895
   101
icculus@5895
   102
/* Fix menu to contain the real app name instead of "SDL App" */
icculus@5895
   103
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
icculus@5895
   104
{
icculus@5895
   105
    NSRange aRange;
icculus@5895
   106
    NSEnumerator *enumerator;
icculus@5895
   107
    NSMenuItem *menuItem;
icculus@5895
   108
icculus@5895
   109
    aRange = [[aMenu title] rangeOfString:@"SDL App"];
icculus@5895
   110
    if (aRange.length != 0)
icculus@5895
   111
        [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
icculus@5895
   112
icculus@5895
   113
    enumerator = [[aMenu itemArray] objectEnumerator];
icculus@5895
   114
    while ((menuItem = [enumerator nextObject]))
icculus@5895
   115
    {
icculus@5895
   116
        aRange = [[menuItem title] rangeOfString:@"SDL App"];
icculus@5895
   117
        if (aRange.length != 0)
icculus@5895
   118
            [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
icculus@5895
   119
        if ([menuItem hasSubmenu])
icculus@5895
   120
            [self fixMenu:[menuItem submenu] withAppName:appName];
icculus@5895
   121
    }
icculus@5895
   122
    [ aMenu sizeToFit ];
icculus@5895
   123
}
icculus@5895
   124
icculus@5895
   125
#else
icculus@5895
   126
icculus@5895
   127
static void setApplicationMenu(void)
icculus@5895
   128
{
icculus@5895
   129
    /* warning: this code is very odd */
icculus@5895
   130
    NSMenu *appleMenu;
icculus@5895
   131
    NSMenuItem *menuItem;
icculus@5895
   132
    NSString *title;
icculus@5895
   133
    NSString *appName;
icculus@5895
   134
    
icculus@5895
   135
    appName = getApplicationName();
icculus@5895
   136
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
icculus@5895
   137
    
icculus@5895
   138
    /* Add menu items */
icculus@5895
   139
    title = [@"About " stringByAppendingString:appName];
icculus@5895
   140
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
icculus@5895
   141
icculus@5895
   142
    [appleMenu addItem:[NSMenuItem separatorItem]];
icculus@5895
   143
icculus@5895
   144
    title = [@"Hide " stringByAppendingString:appName];
icculus@5895
   145
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
icculus@5895
   146
icculus@5895
   147
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
icculus@5895
   148
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
icculus@5895
   149
icculus@5895
   150
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
icculus@5895
   151
icculus@5895
   152
    [appleMenu addItem:[NSMenuItem separatorItem]];
icculus@5895
   153
icculus@5895
   154
    title = [@"Quit " stringByAppendingString:appName];
icculus@5895
   155
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
icculus@5895
   156
icculus@5895
   157
    
icculus@5895
   158
    /* Put menu into the menubar */
icculus@5895
   159
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
icculus@5895
   160
    [menuItem setSubmenu:appleMenu];
icculus@5895
   161
    [[NSApp mainMenu] addItem:menuItem];
icculus@5895
   162
icculus@5895
   163
    /* Tell the application object that this is now the application menu */
icculus@5895
   164
    [NSApp setAppleMenu:appleMenu];
icculus@5895
   165
icculus@5895
   166
    /* Finally give up our references to the objects */
icculus@5895
   167
    [appleMenu release];
icculus@5895
   168
    [menuItem release];
icculus@5895
   169
}
icculus@5895
   170
icculus@5895
   171
/* Create a window menu */
icculus@5895
   172
static void setupWindowMenu(void)
icculus@5895
   173
{
icculus@5895
   174
    NSMenu      *windowMenu;
icculus@5895
   175
    NSMenuItem  *windowMenuItem;
icculus@5895
   176
    NSMenuItem  *menuItem;
icculus@5895
   177
icculus@5895
   178
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
icculus@5895
   179
    
icculus@5895
   180
    /* "Minimize" item */
icculus@5895
   181
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
icculus@5895
   182
    [windowMenu addItem:menuItem];
icculus@5895
   183
    [menuItem release];
icculus@5895
   184
    
icculus@5895
   185
    /* Put menu into the menubar */
icculus@5895
   186
    windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
icculus@5895
   187
    [windowMenuItem setSubmenu:windowMenu];
icculus@5895
   188
    [[NSApp mainMenu] addItem:windowMenuItem];
icculus@5895
   189
    
icculus@5895
   190
    /* Tell the application object that this is now the window menu */
icculus@5895
   191
    [NSApp setWindowsMenu:windowMenu];
icculus@5895
   192
icculus@5895
   193
    /* Finally give up our references to the objects */
icculus@5895
   194
    [windowMenu release];
icculus@5895
   195
    [windowMenuItem release];
icculus@5895
   196
}
icculus@5895
   197
icculus@5895
   198
/* Replacement for NSApplicationMain */
icculus@5895
   199
static void CustomApplicationMain (int argc, char **argv)
icculus@5895
   200
{
icculus@5895
   201
    NSAutoreleasePool	*pool = [[NSAutoreleasePool alloc] init];
icculus@5895
   202
    SDLMain				*sdlMain;
icculus@5895
   203
icculus@5895
   204
    /* Ensure the application object is initialised */
icculus@5895
   205
    [SDLApplication sharedApplication];
icculus@5895
   206
    
icculus@5895
   207
#ifdef SDL_USE_CPS
icculus@5895
   208
    {
icculus@5895
   209
        CPSProcessSerNum PSN;
icculus@5895
   210
        /* Tell the dock about us */
icculus@5895
   211
        if (!CPSGetCurrentProcess(&PSN))
icculus@5895
   212
            if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
icculus@5895
   213
                if (!CPSSetFrontProcess(&PSN))
icculus@5895
   214
                    [SDLApplication sharedApplication];
icculus@5895
   215
    }
icculus@5895
   216
#endif /* SDL_USE_CPS */
icculus@5895
   217
icculus@5895
   218
    /* Set up the menubar */
icculus@5895
   219
    [NSApp setMainMenu:[[NSMenu alloc] init]];
icculus@5895
   220
    setApplicationMenu();
icculus@5895
   221
    setupWindowMenu();
icculus@5895
   222
icculus@5895
   223
    /* Create SDLMain and make it the app delegate */
icculus@5895
   224
    sdlMain = [[SDLMain alloc] init];
icculus@5895
   225
    [NSApp setDelegate:sdlMain];
icculus@5895
   226
    
icculus@5895
   227
    /* Start the main event loop */
icculus@5895
   228
    [NSApp run];
icculus@5895
   229
    
icculus@5895
   230
    [sdlMain release];
icculus@5895
   231
    [pool release];
icculus@5895
   232
}
icculus@5895
   233
icculus@5895
   234
#endif
icculus@5895
   235
icculus@5895
   236
icculus@5895
   237
/*
icculus@5895
   238
 * Catch document open requests...this lets us notice files when the app
icculus@5895
   239
 *  was launched by double-clicking a document, or when a document was
icculus@5895
   240
 *  dragged/dropped on the app's icon. You need to have a
icculus@5895
   241
 *  CFBundleDocumentsType section in your Info.plist to get this message,
icculus@5895
   242
 *  apparently.
icculus@5895
   243
 *
icculus@5895
   244
 * Files are added to gArgv, so to the app, they'll look like command line
icculus@5895
   245
 *  arguments. Previously, apps launched from the finder had nothing but
icculus@5895
   246
 *  an argv[0].
icculus@5895
   247
 *
icculus@5895
   248
 * This message may be received multiple times to open several docs on launch.
icculus@5895
   249
 *
icculus@5895
   250
 * This message is ignored once the app's mainline has been called.
icculus@5895
   251
 */
icculus@5895
   252
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
icculus@5895
   253
{
icculus@5895
   254
    const char *temparg;
icculus@5895
   255
    size_t arglen;
icculus@5895
   256
    char *arg;
icculus@5895
   257
    char **newargv;
icculus@5895
   258
icculus@5895
   259
    if (!gFinderLaunch)  /* MacOS is passing command line args. */
icculus@5895
   260
        return FALSE;
icculus@5895
   261
icculus@5895
   262
    if (gCalledAppMainline)  /* app has started, ignore this document. */
icculus@5895
   263
        return FALSE;
icculus@5895
   264
icculus@5895
   265
    temparg = [filename UTF8String];
icculus@5895
   266
    arglen = SDL_strlen(temparg) + 1;
icculus@5895
   267
    arg = (char *) SDL_malloc(arglen);
icculus@5895
   268
    if (arg == NULL)
icculus@5895
   269
        return FALSE;
icculus@5895
   270
icculus@5895
   271
    newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
icculus@5895
   272
    if (newargv == NULL)
icculus@5895
   273
    {
icculus@5895
   274
        SDL_free(arg);
icculus@5895
   275
        return FALSE;
icculus@5895
   276
    }
icculus@5895
   277
    gArgv = newargv;
icculus@5895
   278
icculus@5895
   279
    SDL_strlcpy(arg, temparg, arglen);
icculus@5895
   280
    gArgv[gArgc++] = arg;
icculus@5895
   281
    gArgv[gArgc] = NULL;
icculus@5895
   282
    return TRUE;
icculus@5895
   283
}
icculus@5895
   284
icculus@5895
   285
icculus@5895
   286
/* Called when the internal event loop has just started running */
icculus@5895
   287
- (void) applicationDidFinishLaunching: (NSNotification *) note
icculus@5895
   288
{
icculus@5895
   289
    int status;
icculus@5895
   290
icculus@5895
   291
    /* Set the working directory to the .app's parent directory */
icculus@5895
   292
    [self setupWorkingDirectory:gFinderLaunch];
icculus@5895
   293
icculus@5895
   294
#if SDL_USE_NIB_FILE
icculus@5895
   295
    /* Set the main menu to contain the real app name instead of "SDL App" */
icculus@5895
   296
    [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
icculus@5895
   297
#endif
icculus@5895
   298
icculus@5895
   299
    /* Hand off to main application code */
icculus@5895
   300
    gCalledAppMainline = TRUE;
icculus@5895
   301
    status = SDL_main (gArgc, gArgv);
icculus@5895
   302
icculus@5895
   303
    /* We're done, thank you for playing */
icculus@5895
   304
    exit(status);
icculus@5895
   305
}
icculus@5895
   306
@end
icculus@5895
   307
icculus@5895
   308
icculus@5895
   309
@implementation NSString (ReplaceSubString)
icculus@5895
   310
icculus@5895
   311
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
icculus@5895
   312
{
icculus@5895
   313
    unsigned int bufferSize;
icculus@5895
   314
    unsigned int selfLen = [self length];
icculus@5895
   315
    unsigned int aStringLen = [aString length];
icculus@5895
   316
    unichar *buffer;
icculus@5895
   317
    NSRange localRange;
icculus@5895
   318
    NSString *result;
icculus@5895
   319
icculus@5895
   320
    bufferSize = selfLen + aStringLen - aRange.length;
icculus@5895
   321
    buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar));
icculus@5895
   322
    
icculus@5895
   323
    /* Get first part into buffer */
icculus@5895
   324
    localRange.location = 0;
icculus@5895
   325
    localRange.length = aRange.location;
icculus@5895
   326
    [self getCharacters:buffer range:localRange];
icculus@5895
   327
    
icculus@5895
   328
    /* Get middle part into buffer */
icculus@5895
   329
    localRange.location = 0;
icculus@5895
   330
    localRange.length = aStringLen;
icculus@5895
   331
    [aString getCharacters:(buffer+aRange.location) range:localRange];
icculus@5895
   332
     
icculus@5895
   333
    /* Get last part into buffer */
icculus@5895
   334
    localRange.location = aRange.location + aRange.length;
icculus@5895
   335
    localRange.length = selfLen - localRange.location;
icculus@5895
   336
    [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
icculus@5895
   337
    
icculus@5895
   338
    /* Build output string */
icculus@5895
   339
    result = [NSString stringWithCharacters:buffer length:bufferSize];
icculus@5895
   340
    
icculus@5895
   341
    NSDeallocateMemoryPages(buffer, bufferSize);
icculus@5895
   342
    
icculus@5895
   343
    return result;
icculus@5895
   344
}
icculus@5895
   345
icculus@5895
   346
@end
icculus@5895
   347
icculus@5895
   348
icculus@5895
   349
icculus@5895
   350
#ifdef main
icculus@5895
   351
#  undef main
icculus@5895
   352
#endif
icculus@5895
   353
icculus@5895
   354
icculus@5895
   355
/* Main entry point to executable - should *not* be SDL_main! */
icculus@5895
   356
int main (int argc, char **argv)
icculus@5895
   357
{
icculus@5895
   358
    /* Copy the arguments into a global variable */
icculus@5895
   359
    /* This is passed if we are launched by double-clicking */
icculus@5895
   360
    if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
icculus@5895
   361
        gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
icculus@5895
   362
        gArgv[0] = argv[0];
icculus@5895
   363
        gArgv[1] = NULL;
icculus@5895
   364
        gArgc = 1;
icculus@5895
   365
        gFinderLaunch = YES;
icculus@5895
   366
    } else {
icculus@5895
   367
        int i;
icculus@5895
   368
        gArgc = argc;
icculus@5895
   369
        gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
icculus@5895
   370
        for (i = 0; i <= argc; i++)
icculus@5895
   371
            gArgv[i] = argv[i];
icculus@5895
   372
        gFinderLaunch = NO;
icculus@5895
   373
    }
icculus@5895
   374
icculus@5895
   375
#if SDL_USE_NIB_FILE
icculus@5895
   376
    [SDLApplication poseAsClass:[NSApplication class]];
icculus@5895
   377
    NSApplicationMain (argc, argv);
icculus@5895
   378
#else
icculus@5895
   379
    CustomApplicationMain (argc, argv);
icculus@5895
   380
#endif
icculus@5895
   381
    return 0;
icculus@5895
   382
}
icculus@5895
   383