src/main/macos/SDL_main.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* This file takes care of command line argument parsing, and stdio redirection
icculus@1133
    24
   in the MacOS environment. (stdio/stderr is *not* directed for Mach-O builds)
slouken@0
    25
 */
slouken@0
    26
icculus@1133
    27
#if defined(__APPLE__) && defined(__MACH__)
icculus@1133
    28
#include <Carbon/Carbon.h>
icculus@1133
    29
#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
slouken@0
    30
#include <Carbon.h>
slouken@0
    31
#else
slouken@0
    32
#include <Dialogs.h>
slouken@0
    33
#include <Fonts.h>
slouken@0
    34
#include <Events.h>
slouken@0
    35
#include <Resources.h>
slouken@0
    36
#include <Folders.h>
slouken@0
    37
#endif
slouken@0
    38
slouken@0
    39
/* Include the SDL main definition header */
slouken@0
    40
#include "SDL.h"
slouken@0
    41
#include "SDL_main.h"
slouken@0
    42
#ifdef main
slouken@0
    43
#undef main
slouken@0
    44
#endif
slouken@0
    45
icculus@1133
    46
#if !(defined(__APPLE__) && defined(__MACH__))
slouken@0
    47
/* The standard output files */
slouken@0
    48
#define STDOUT_FILE	"stdout.txt"
slouken@0
    49
#define STDERR_FILE	"stderr.txt"
icculus@1133
    50
#endif
slouken@0
    51
slouken@0
    52
#if !defined(__MWERKS__) && !TARGET_API_MAC_CARBON
slouken@1662
    53
        /* In MPW, the qd global has been removed from the libraries */
slouken@1662
    54
QDGlobals qd;
slouken@0
    55
#endif
slouken@0
    56
slouken@0
    57
/* Structure for keeping prefs in 1 variable */
slouken@1662
    58
typedef struct
slouken@1662
    59
{
slouken@1662
    60
    Str255 command_line;
slouken@1662
    61
    Str255 video_driver_name;
slouken@0
    62
    Boolean output_to_file;
slouken@1662
    63
} PrefsRecord;
slouken@0
    64
slouken@0
    65
/* See if the command key is held down at startup */
slouken@1662
    66
static Boolean
slouken@1668
    67
CommandKeyIsDown(void)
slouken@0
    68
{
slouken@1662
    69
    KeyMap theKeyMap;
slouken@0
    70
slouken@1668
    71
    GetKeys(theKeyMap);
slouken@0
    72
slouken@1662
    73
    if (((unsigned char *) theKeyMap)[6] & 0x80) {
slouken@1662
    74
        return (true);
slouken@1662
    75
    }
slouken@1662
    76
    return (false);
slouken@0
    77
}
slouken@0
    78
icculus@1133
    79
#if !(defined(__APPLE__) && defined(__MACH__))
icculus@1133
    80
slouken@0
    81
/* Parse a command line buffer into arguments */
slouken@1662
    82
static int
slouken@1668
    83
ParseCommandLine(char *cmdline, char **argv)
slouken@0
    84
{
slouken@1662
    85
    char *bufp;
slouken@1662
    86
    int argc;
slouken@0
    87
slouken@1662
    88
    argc = 0;
slouken@1662
    89
    for (bufp = cmdline; *bufp;) {
slouken@1662
    90
        /* Skip leading whitespace */
slouken@1668
    91
        while (SDL_isspace(*bufp)) {
slouken@1662
    92
            ++bufp;
slouken@1662
    93
        }
slouken@1662
    94
        /* Skip over argument */
slouken@1662
    95
        if (*bufp == '"') {
slouken@1662
    96
            ++bufp;
slouken@1662
    97
            if (*bufp) {
slouken@1662
    98
                if (argv) {
slouken@1662
    99
                    argv[argc] = bufp;
slouken@1662
   100
                }
slouken@1662
   101
                ++argc;
slouken@1662
   102
            }
slouken@1662
   103
            /* Skip over word */
slouken@1662
   104
            while (*bufp && (*bufp != '"')) {
slouken@1662
   105
                ++bufp;
slouken@1662
   106
            }
slouken@1662
   107
        } else {
slouken@1662
   108
            if (*bufp) {
slouken@1662
   109
                if (argv) {
slouken@1662
   110
                    argv[argc] = bufp;
slouken@1662
   111
                }
slouken@1662
   112
                ++argc;
slouken@1662
   113
            }
slouken@1662
   114
            /* Skip over word */
slouken@1668
   115
            while (*bufp && !SDL_isspace(*bufp)) {
slouken@1662
   116
                ++bufp;
slouken@1662
   117
            }
slouken@1662
   118
        }
slouken@1662
   119
        if (*bufp) {
slouken@1662
   120
            if (argv) {
slouken@1662
   121
                *bufp = '\0';
slouken@1662
   122
            }
slouken@1662
   123
            ++bufp;
slouken@1662
   124
        }
slouken@1662
   125
    }
slouken@1662
   126
    if (argv) {
slouken@1662
   127
        argv[argc] = NULL;
slouken@1662
   128
    }
slouken@1662
   129
    return (argc);
slouken@0
   130
}
slouken@0
   131
slouken@0
   132
/* Remove the output files if there was no output written */
slouken@1662
   133
static void
slouken@1668
   134
cleanup_output(void)
slouken@0
   135
{
slouken@1662
   136
    FILE *file;
slouken@1662
   137
    int empty;
slouken@0
   138
slouken@1662
   139
    /* Flush the output in case anything is queued */
slouken@1668
   140
    fclose(stdout);
slouken@1668
   141
    fclose(stderr);
slouken@0
   142
slouken@1662
   143
    /* See if the files have any output in them */
slouken@1668
   144
    file = fopen(STDOUT_FILE, "rb");
slouken@1662
   145
    if (file) {
slouken@1668
   146
        empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@1668
   147
        fclose(file);
slouken@1662
   148
        if (empty) {
slouken@1668
   149
            remove(STDOUT_FILE);
slouken@1662
   150
        }
slouken@1662
   151
    }
slouken@1668
   152
    file = fopen(STDERR_FILE, "rb");
slouken@1662
   153
    if (file) {
slouken@1668
   154
        empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@1668
   155
        fclose(file);
slouken@1662
   156
        if (empty) {
slouken@1668
   157
            remove(STDERR_FILE);
slouken@1662
   158
        }
slouken@1662
   159
    }
slouken@0
   160
}
slouken@0
   161
icculus@1133
   162
#endif //!(defined(__APPLE__) && defined(__MACH__))
icculus@1133
   163
slouken@1662
   164
static int
slouken@1668
   165
getCurrentAppName(StrFileName name)
slouken@1662
   166
{
slouken@1662
   167
slouken@0
   168
    ProcessSerialNumber process;
slouken@1662
   169
    ProcessInfoRec process_info;
slouken@1662
   170
    FSSpec process_fsp;
slouken@1662
   171
slouken@0
   172
    process.highLongOfPSN = 0;
slouken@1662
   173
    process.lowLongOfPSN = kCurrentProcess;
slouken@1668
   174
    process_info.processInfoLength = sizeof(process_info);
slouken@1662
   175
    process_info.processName = NULL;
slouken@0
   176
    process_info.processAppSpec = &process_fsp;
slouken@1662
   177
slouken@1668
   178
    if (noErr != GetProcessInformation(&process, &process_info))
slouken@1662
   179
        return 0;
slouken@1662
   180
slouken@1668
   181
    SDL_memcpy(name, process_fsp.name, process_fsp.name[0] + 1);
slouken@0
   182
    return 1;
slouken@0
   183
}
slouken@0
   184
slouken@1662
   185
static int
slouken@1668
   186
getPrefsFile(FSSpec * prefs_fsp, int create)
slouken@1662
   187
{
slouken@0
   188
slouken@0
   189
    /* The prefs file name is the application name, possibly truncated, */
slouken@0
   190
    /* plus " Preferences */
slouken@1662
   191
slouken@1662
   192
#define  SUFFIX   " Preferences"
slouken@1662
   193
#define  MAX_NAME 19            /* 31 - strlen (SUFFIX) */
slouken@1662
   194
slouken@1662
   195
    short volume_ref_number;
slouken@1662
   196
    long directory_id;
slouken@1662
   197
    StrFileName prefs_name;
slouken@1662
   198
    StrFileName app_name;
slouken@1662
   199
slouken@0
   200
    /* Get Preferences folder - works with Multiple Users */
slouken@1662
   201
    if (noErr !=
slouken@1668
   202
        FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
slouken@1668
   203
                   &volume_ref_number, &directory_id))
slouken@1668
   204
        exit(-1);
slouken@1662
   205
slouken@1668
   206
    if (!getCurrentAppName(app_name))
slouken@1668
   207
        exit(-1);
slouken@1662
   208
slouken@0
   209
    /* Truncate if name is too long */
slouken@1662
   210
    if (app_name[0] > MAX_NAME)
slouken@0
   211
        app_name[0] = MAX_NAME;
slouken@1662
   212
slouken@1668
   213
    SDL_memcpy(prefs_name + 1, app_name + 1, app_name[0]);
slouken@1668
   214
    SDL_memcpy(prefs_name + app_name[0] + 1, SUFFIX, strlen(SUFFIX));
slouken@1668
   215
    prefs_name[0] = app_name[0] + strlen(SUFFIX);
slouken@1662
   216
slouken@0
   217
    /* Make the file spec for prefs file */
slouken@1662
   218
    if (noErr !=
slouken@1668
   219
        FSMakeFSSpec(volume_ref_number, directory_id, prefs_name, prefs_fsp))
slouken@1662
   220
    {
slouken@1662
   221
        if (!create)
slouken@0
   222
            return 0;
slouken@0
   223
        else {
slouken@0
   224
            /* Create the prefs file */
slouken@1668
   225
            SDL_memcpy(prefs_fsp->name, prefs_name, prefs_name[0] + 1);
slouken@1662
   226
            prefs_fsp->parID = directory_id;
slouken@0
   227
            prefs_fsp->vRefNum = volume_ref_number;
slouken@1662
   228
slouken@1668
   229
            FSpCreateResFile(prefs_fsp, 0x3f3f3f3f, 'pref', 0); // '????' parsed as trigraph
slouken@1662
   230
slouken@1668
   231
            if (noErr != ResError())
slouken@0
   232
                return 0;
slouken@0
   233
        }
slouken@1662
   234
    }
slouken@0
   235
    return 1;
slouken@0
   236
}
slouken@0
   237
slouken@1662
   238
static int
slouken@1668
   239
readPrefsResource(PrefsRecord * prefs)
slouken@1662
   240
{
slouken@1662
   241
slouken@0
   242
    Handle prefs_handle;
slouken@0
   243
slouken@1668
   244
    prefs_handle = Get1Resource('CLne', 128);
slouken@0
   245
slouken@1662
   246
    if (prefs_handle != NULL) {
slouken@1662
   247
        int offset = 0;
slouken@1662
   248
//              int j      = 0;
slouken@1662
   249
slouken@1668
   250
        HLock(prefs_handle);
slouken@1662
   251
slouken@1662
   252
        /* Get command line string */
slouken@1668
   253
        SDL_memcpy(prefs->command_line, *prefs_handle,
slouken@1668
   254
                   (*prefs_handle)[0] + 1);
slouken@1662
   255
slouken@1662
   256
        /* Get video driver name */
slouken@1662
   257
        offset += (*prefs_handle)[0] + 1;
slouken@1668
   258
        SDL_memcpy(prefs->video_driver_name, *prefs_handle + offset,
slouken@1668
   259
                   (*prefs_handle)[offset] + 1);
slouken@1662
   260
slouken@1662
   261
        /* Get save-to-file option (1 or 0) */
slouken@1662
   262
        offset += (*prefs_handle)[offset] + 1;
slouken@1662
   263
        prefs->output_to_file = (*prefs_handle)[offset];
slouken@1662
   264
slouken@1668
   265
        ReleaseResource(prefs_handle);
slouken@1662
   266
slouken@1668
   267
        return ResError() == noErr;
slouken@0
   268
    }
slouken@0
   269
slouken@0
   270
    return 0;
slouken@0
   271
}
slouken@0
   272
slouken@1662
   273
static int
slouken@1668
   274
writePrefsResource(PrefsRecord * prefs, short resource_file)
slouken@1662
   275
{
slouken@0
   276
slouken@0
   277
    Handle prefs_handle;
slouken@1662
   278
slouken@1668
   279
    UseResFile(resource_file);
slouken@1662
   280
slouken@1668
   281
    prefs_handle = Get1Resource('CLne', 128);
slouken@0
   282
    if (prefs_handle != NULL)
slouken@1668
   283
        RemoveResource(prefs_handle);
slouken@1662
   284
slouken@1662
   285
    prefs_handle =
slouken@1668
   286
        NewHandle(prefs->command_line[0] + prefs->video_driver_name[0] + 4);
slouken@0
   287
    if (prefs_handle != NULL) {
slouken@1662
   288
slouken@0
   289
        int offset;
slouken@1662
   290
slouken@1668
   291
        HLock(prefs_handle);
slouken@1662
   292
slouken@0
   293
        /* Command line text */
slouken@0
   294
        offset = 0;
slouken@1668
   295
        SDL_memcpy(*prefs_handle, prefs->command_line,
slouken@1668
   296
                   prefs->command_line[0] + 1);
slouken@1662
   297
slouken@0
   298
        /* Video driver name */
slouken@0
   299
        offset += prefs->command_line[0] + 1;
slouken@1668
   300
        SDL_memcpy(*prefs_handle + offset, prefs->video_driver_name,
slouken@1668
   301
                   prefs->video_driver_name[0] + 1);
slouken@1662
   302
slouken@0
   303
        /* Output-to-file option */
slouken@0
   304
        offset += prefs->video_driver_name[0] + 1;
slouken@1662
   305
        *(*((char **) prefs_handle) + offset) = (char) prefs->output_to_file;
slouken@1662
   306
        *(*((char **) prefs_handle) + offset + 1) = 0;
slouken@1662
   307
slouken@1668
   308
        AddResource(prefs_handle, 'CLne', 128, "\pCommand Line");
slouken@1668
   309
        WriteResource(prefs_handle);
slouken@1668
   310
        UpdateResFile(resource_file);
slouken@1668
   311
        DisposeHandle(prefs_handle);
slouken@1662
   312
slouken@1668
   313
        return ResError() == noErr;
slouken@0
   314
    }
slouken@1662
   315
slouken@0
   316
    return 0;
slouken@0
   317
}
slouken@0
   318
slouken@1662
   319
static int
slouken@1668
   320
readPreferences(PrefsRecord * prefs)
slouken@1662
   321
{
slouken@0
   322
slouken@1662
   323
    int no_error = 1;
slouken@0
   324
    FSSpec prefs_fsp;
slouken@0
   325
slouken@0
   326
    /* Check for prefs file first */
slouken@1668
   327
    if (getPrefsFile(&prefs_fsp, 0)) {
slouken@1662
   328
slouken@1662
   329
        short prefs_resource;
slouken@1662
   330
slouken@1668
   331
        prefs_resource = FSpOpenResFile(&prefs_fsp, fsRdPerm);
slouken@1662
   332
        if (prefs_resource == -1)       /* this shouldn't happen, but... */
slouken@0
   333
            return 0;
slouken@1662
   334
slouken@1668
   335
        UseResFile(prefs_resource);
slouken@1668
   336
        no_error = readPrefsResource(prefs);
slouken@1668
   337
        CloseResFile(prefs_resource);
slouken@0
   338
    }
slouken@1662
   339
slouken@0
   340
    /* Fall back to application's resource fork (reading only, so this is safe) */
slouken@0
   341
    else {
slouken@1662
   342
slouken@1668
   343
        no_error = readPrefsResource(prefs);
slouken@1662
   344
    }
slouken@0
   345
slouken@0
   346
    return no_error;
slouken@0
   347
}
slouken@0
   348
slouken@1662
   349
static int
slouken@1668
   350
writePreferences(PrefsRecord * prefs)
slouken@1662
   351
{
slouken@1662
   352
slouken@1662
   353
    int no_error = 1;
slouken@0
   354
    FSSpec prefs_fsp;
slouken@1662
   355
slouken@0
   356
    /* Get prefs file, create if it doesn't exist */
slouken@1668
   357
    if (getPrefsFile(&prefs_fsp, 1)) {
slouken@1662
   358
slouken@1662
   359
        short prefs_resource;
slouken@1662
   360
slouken@1668
   361
        prefs_resource = FSpOpenResFile(&prefs_fsp, fsRdWrPerm);
slouken@0
   362
        if (prefs_resource == -1)
slouken@0
   363
            return 0;
slouken@1668
   364
        no_error = writePrefsResource(prefs, prefs_resource);
slouken@1668
   365
        CloseResFile(prefs_resource);
slouken@0
   366
    }
slouken@1662
   367
slouken@0
   368
    return no_error;
slouken@0
   369
}
slouken@0
   370
slouken@0
   371
/* This is where execution begins */
slouken@1662
   372
int
slouken@1668
   373
main(int argc, char *argv[])
slouken@0
   374
{
slouken@0
   375
icculus@1133
   376
#if !(defined(__APPLE__) && defined(__MACH__))
slouken@0
   377
#pragma unused(argc, argv)
icculus@1133
   378
#endif
slouken@0
   379
slouken@1662
   380
#define DEFAULT_ARGS "\p"       /* pascal string for default args */
slouken@1662
   381
#define DEFAULT_VIDEO_DRIVER "\ptoolbox"        /* pascal string for default video driver name */
slouken@1662
   382
#define DEFAULT_OUTPUT_TO_FILE 1        /* 1 == output to file, 0 == no output */
slouken@1662
   383
slouken@1662
   384
#define VIDEO_ID_DRAWSPROCKET 1 /* these correspond to popup menu choices */
slouken@0
   385
#define VIDEO_ID_TOOLBOX      2
slouken@0
   386
slouken@1662
   387
    PrefsRecord prefs =
slouken@1662
   388
        { DEFAULT_ARGS, DEFAULT_VIDEO_DRIVER, DEFAULT_OUTPUT_TO_FILE };
slouken@1662
   389
icculus@1133
   390
#if !(defined(__APPLE__) && defined(__MACH__))
slouken@1662
   391
    int nargs;
slouken@1662
   392
    char **args;
slouken@1662
   393
    char *commandLine;
slouken@1662
   394
slouken@1662
   395
    StrFileName appNameText;
icculus@1133
   396
#endif
slouken@1662
   397
    int videodriver = VIDEO_ID_TOOLBOX;
slouken@1662
   398
    int settingsChanged = 0;
slouken@0
   399
slouken@1662
   400
    long i;
slouken@1662
   401
slouken@1662
   402
    /* Kyle's SDL command-line dialog code ... */
slouken@0
   403
#if !TARGET_API_MAC_CARBON
slouken@1668
   404
    InitGraf(&qd.thePort);
slouken@1668
   405
    InitFonts();
slouken@1668
   406
    InitWindows();
slouken@1668
   407
    InitMenus();
slouken@1668
   408
    InitDialogs(nil);
slouken@0
   409
#endif
slouken@1668
   410
    InitCursor();
slouken@1668
   411
    FlushEvents(everyEvent, 0);
slouken@0
   412
#if !TARGET_API_MAC_CARBON
slouken@1668
   413
    MaxApplZone();
slouken@0
   414
#endif
slouken@1668
   415
    MoreMasters();
slouken@1668
   416
    MoreMasters();
slouken@0
   417
#if 0
slouken@1662
   418
    /* Intialize SDL, and put up a dialog if we fail */
slouken@1668
   419
    if (SDL_Init(0) < 0) {
slouken@0
   420
slouken@0
   421
#define kErr_OK		1
slouken@0
   422
#define kErr_Text	2
slouken@0
   423
slouken@0
   424
        DialogPtr errorDialog;
slouken@1662
   425
        short dummyType;
slouken@1662
   426
        Rect dummyRect;
slouken@1662
   427
        Handle dummyHandle;
slouken@1662
   428
        short itemHit;
slouken@1662
   429
slouken@1668
   430
        errorDialog = GetNewDialog(1001, nil, (WindowPtr) - 1);
slouken@1662
   431
        if (errorDialog == NULL)
slouken@1662
   432
            return -1;
slouken@1668
   433
        DrawDialog(errorDialog);
slouken@1662
   434
slouken@1668
   435
        GetDialogItem(errorDialog, kErr_Text, &dummyType, &dummyHandle,
slouken@1668
   436
                      &dummyRect);
slouken@1668
   437
        SetDialogItemText(dummyHandle, "\pError Initializing SDL");
slouken@1662
   438
icculus@1133
   439
#if TARGET_API_MAC_CARBON
slouken@1668
   440
        SetPort(GetDialogPort(errorDialog));
icculus@1133
   441
#else
slouken@1668
   442
        SetPort(errorDialog);
icculus@1133
   443
#endif
slouken@1662
   444
        do {
slouken@1668
   445
            ModalDialog(nil, &itemHit);
slouken@1662
   446
        }
slouken@1662
   447
        while (itemHit != kErr_OK);
slouken@1662
   448
slouken@1668
   449
        DisposeDialog(errorDialog);
slouken@1668
   450
        exit(-1);
slouken@1662
   451
    }
slouken@1668
   452
    atexit(cleanup_output);
slouken@1668
   453
    atexit(SDL_Quit);
slouken@0
   454
#endif
slouken@0
   455
slouken@0
   456
/* Set up SDL's QuickDraw environment  */
slouken@0
   457
#if !TARGET_API_MAC_CARBON
slouken@1668
   458
    SDL_InitQuickDraw(&qd);
slouken@0
   459
#endif
slouken@0
   460
slouken@1668
   461
    if (readPreferences(&prefs)) {
slouken@1662
   462
slouken@1668
   463
        if (SDL_memcmp(prefs.video_driver_name + 1, "DSp", 3) == 0)
slouken@0
   464
            videodriver = 1;
slouken@1668
   465
        else if (SDL_memcmp(prefs.video_driver_name + 1, "toolbox", 7) == 0)
slouken@0
   466
            videodriver = 2;
slouken@1662
   467
    }
slouken@1662
   468
slouken@1668
   469
    if (CommandKeyIsDown()) {
slouken@0
   470
slouken@0
   471
#define kCL_OK		1
slouken@0
   472
#define kCL_Cancel	2
slouken@0
   473
#define kCL_Text	3
slouken@0
   474
#define kCL_File	4
slouken@0
   475
#define kCL_Video   6
slouken@1662
   476
slouken@0
   477
        DialogPtr commandDialog;
slouken@1662
   478
        short dummyType;
slouken@1662
   479
        Rect dummyRect;
slouken@1662
   480
        Handle dummyHandle;
slouken@1662
   481
        short itemHit;
slouken@1662
   482
#if TARGET_API_MAC_CARBON
icculus@1133
   483
        ControlRef control;
slouken@1662
   484
#endif
slouken@1662
   485
slouken@0
   486
        /* Assume that they will change settings, rather than do exhaustive check */
slouken@0
   487
        settingsChanged = 1;
slouken@1662
   488
slouken@0
   489
        /* Create dialog and display it */
slouken@1668
   490
        commandDialog = GetNewDialog(1000, nil, (WindowPtr) - 1);
slouken@1662
   491
#if TARGET_API_MAC_CARBON
slouken@1668
   492
        SetPort(GetDialogPort(commandDialog));
slouken@1662
   493
#else
slouken@1668
   494
        SetPort(commandDialog);
slouken@1662
   495
#endif
slouken@1662
   496
slouken@0
   497
        /* Setup controls */
slouken@1662
   498
#if TARGET_API_MAC_CARBON
slouken@1668
   499
        GetDialogItemAsControl(commandDialog, kCL_File, &control);
slouken@1668
   500
        SetControlValue(control, prefs.output_to_file);
slouken@1662
   501
#else
slouken@1668
   502
        GetDialogItem(commandDialog, kCL_File, &dummyType, &dummyHandle, &dummyRect);   /* MJS */
slouken@1668
   503
        SetControlValue((ControlHandle) dummyHandle, prefs.output_to_file);
slouken@1662
   504
#endif
slouken@0
   505
slouken@1668
   506
        GetDialogItem(commandDialog, kCL_Text, &dummyType, &dummyHandle,
slouken@1668
   507
                      &dummyRect);
slouken@1668
   508
        SetDialogItemText(dummyHandle, prefs.command_line);
slouken@0
   509
slouken@1662
   510
#if TARGET_API_MAC_CARBON
slouken@1668
   511
        GetDialogItemAsControl(commandDialog, kCL_Video, &control);
slouken@1668
   512
        SetControlValue(control, videodriver);
slouken@1662
   513
#else
slouken@1668
   514
        GetDialogItem(commandDialog, kCL_Video, &dummyType, &dummyHandle,
slouken@1668
   515
                      &dummyRect);
slouken@1668
   516
        SetControlValue((ControlRef) dummyHandle, videodriver);
slouken@1662
   517
#endif
slouken@0
   518
slouken@1668
   519
        SetDialogDefaultItem(commandDialog, kCL_OK);
slouken@1668
   520
        SetDialogCancelItem(commandDialog, kCL_Cancel);
slouken@0
   521
slouken@0
   522
        do {
slouken@0
   523
slouken@1668
   524
            ModalDialog(nil, &itemHit); /* wait for user response */
slouken@1662
   525
slouken@1662
   526
            /* Toggle command-line output checkbox */
slouken@1662
   527
            if (itemHit == kCL_File) {
slouken@1662
   528
#if TARGET_API_MAC_CARBON
slouken@1668
   529
                GetDialogItemAsControl(commandDialog, kCL_File, &control);
slouken@1668
   530
                SetControlValue(control, !GetControlValue(control));
slouken@1662
   531
#else
slouken@1668
   532
                GetDialogItem(commandDialog, kCL_File, &dummyType, &dummyHandle, &dummyRect);   /* MJS */
slouken@1668
   533
                SetControlValue((ControlHandle) dummyHandle,
slouken@1668
   534
                                !GetControlValue((ControlHandle)
slouken@1668
   535
                                                 dummyHandle));
slouken@1662
   536
#endif
slouken@1662
   537
            }
slouken@1662
   538
slouken@1662
   539
        }
slouken@1662
   540
        while (itemHit != kCL_OK && itemHit != kCL_Cancel);
slouken@0
   541
slouken@0
   542
        /* Get control values, even if they did not change */
slouken@1668
   543
        GetDialogItem(commandDialog, kCL_Text, &dummyType, &dummyHandle, &dummyRect);   /* MJS */
slouken@1668
   544
        GetDialogItemText(dummyHandle, prefs.command_line);
slouken@0
   545
slouken@1662
   546
#if TARGET_API_MAC_CARBON
slouken@1668
   547
        GetDialogItemAsControl(commandDialog, kCL_File, &control);
slouken@1668
   548
        prefs.output_to_file = GetControlValue(control);
slouken@1662
   549
#else
slouken@1668
   550
        GetDialogItem(commandDialog, kCL_File, &dummyType, &dummyHandle, &dummyRect);   /* MJS */
slouken@1668
   551
        prefs.output_to_file = GetControlValue((ControlHandle) dummyHandle);
slouken@1662
   552
#endif
slouken@0
   553
slouken@1662
   554
#if TARGET_API_MAC_CARBON
slouken@1668
   555
        GetDialogItemAsControl(commandDialog, kCL_Video, &control);
slouken@1668
   556
        videodriver = GetControlValue(control);
slouken@1662
   557
#else
slouken@1668
   558
        GetDialogItem(commandDialog, kCL_Video, &dummyType, &dummyHandle,
slouken@1668
   559
                      &dummyRect);
slouken@1668
   560
        videodriver = GetControlValue((ControlRef) dummyHandle);
slouken@1662
   561
#endif
slouken@0
   562
slouken@1668
   563
        DisposeDialog(commandDialog);
slouken@0
   564
slouken@1662
   565
        if (itemHit == kCL_Cancel) {
slouken@1668
   566
            exit(0);
slouken@0
   567
        }
slouken@1662
   568
    }
slouken@1662
   569
slouken@0
   570
    /* Set pseudo-environment variables for video driver, update prefs */
slouken@1662
   571
    switch (videodriver) {
slouken@1662
   572
    case VIDEO_ID_DRAWSPROCKET:
slouken@1668
   573
        SDL_putenv("SDL_VIDEODRIVER=DSp");
slouken@1668
   574
        SDL_memcpy(prefs.video_driver_name, "\pDSp", 4);
slouken@1662
   575
        break;
slouken@1662
   576
    case VIDEO_ID_TOOLBOX:
slouken@1668
   577
        SDL_putenv("SDL_VIDEODRIVER=toolbox");
slouken@1668
   578
        SDL_memcpy(prefs.video_driver_name, "\ptoolbox", 8);
slouken@1662
   579
        break;
slouken@1662
   580
    }
slouken@0
   581
icculus@1133
   582
#if !(defined(__APPLE__) && defined(__MACH__))
slouken@0
   583
    /* Redirect standard I/O to files */
slouken@1662
   584
    if (prefs.output_to_file) {
slouken@1668
   585
        freopen(STDOUT_FILE, "w", stdout);
slouken@1668
   586
        freopen(STDERR_FILE, "w", stderr);
slouken@1662
   587
    } else {
slouken@1668
   588
        fclose(stdout);
slouken@1668
   589
        fclose(stderr);
slouken@1662
   590
    }
icculus@1133
   591
#endif
slouken@1662
   592
slouken@0
   593
    if (settingsChanged) {
slouken@0
   594
        /* Save the prefs, even if they might not have changed (but probably did) */
slouken@1668
   595
        if (!writePreferences(&prefs))
slouken@1668
   596
            fprintf(stderr, "WARNING: Could not save preferences!\n");
slouken@0
   597
    }
icculus@1133
   598
#if !(defined(__APPLE__) && defined(__MACH__))
icculus@1133
   599
    appNameText[0] = 0;
slouken@1668
   600
    getCurrentAppName(appNameText);     /* check for error here ? */
slouken@0
   601
slouken@1668
   602
    commandLine = (char *) malloc(appNameText[0] + prefs.command_line[0] + 2);
slouken@1662
   603
    if (commandLine == NULL) {
slouken@1668
   604
        exit(-1);
slouken@0
   605
    }
slouken@0
   606
slouken@0
   607
    /* Rather than rewrite ParseCommandLine method, let's replace  */
slouken@0
   608
    /* any spaces in application name with underscores,            */
slouken@1662
   609
    /* so that the app name is only 1 argument                     */
slouken@1662
   610
    for (i = 1; i < 1 + appNameText[0]; i++)
slouken@1662
   611
        if (appNameText[i] == ' ')
slouken@1662
   612
            appNameText[i] = '_';
slouken@0
   613
slouken@1662
   614
    /* Copy app name & full command text to command-line C-string */
slouken@1668
   615
    SDL_memcpy(commandLine, appNameText + 1, appNameText[0]);
slouken@0
   616
    commandLine[appNameText[0]] = ' ';
slouken@1668
   617
    SDL_memcpy(commandLine + appNameText[0] + 1, prefs.command_line + 1,
slouken@1668
   618
               prefs.command_line[0]);
slouken@1662
   619
    commandLine[appNameText[0] + 1 + prefs.command_line[0]] = '\0';
slouken@0
   620
slouken@0
   621
    /* Parse C-string into argv and argc */
slouken@1668
   622
    nargs = ParseCommandLine(commandLine, NULL);
slouken@1668
   623
    args = (char **) malloc((nargs + 1) * (sizeof *args));
slouken@1662
   624
    if (args == NULL) {
slouken@1668
   625
        exit(-1);
slouken@1662
   626
    }
slouken@1668
   627
    ParseCommandLine(commandLine, args);
slouken@1662
   628
slouken@1662
   629
    /* Run the main application code */
slouken@1668
   630
    SDL_main(nargs, args);
slouken@1668
   631
    free(args);
slouken@1668
   632
    free(commandLine);
slouken@1662
   633
slouken@1662
   634
    /* Remove useless stdout.txt and stderr.txt */
slouken@1668
   635
    cleanup_output();
icculus@1133
   636
#else // defined(__APPLE__) && defined(__MACH__)
slouken@1668
   637
    SDL_main(argc, argv);
icculus@1133
   638
#endif
slouken@0
   639
slouken@1662
   640
    /* Exit cleanly, calling atexit() functions */
slouken@1668
   641
    exit(0);
slouken@1662
   642
slouken@1662
   643
    /* Never reached, but keeps the compiler quiet */
slouken@1662
   644
    return (0);
slouken@0
   645
}
slouken@1662
   646
slouken@1662
   647
/* vi: set ts=4 sw=4 expandtab: */