src/main/macos/SDL_main.c
author Sam Lantinga
Sun, 21 Sep 2003 18:32:04 +0000
changeset 720 f90d80d68071
parent 297 f6ffac90895c
child 769 b8d311d90021
permissions -rw-r--r--
N Sep 17 8791 Sam Lantinga Re: tks source released
Date: Sun, 07 Sep 2003 02:51:58 +0200
From: Stephane Marchesin
Subject: [SDL] Two little patches

Compiling SDL with a recent gcc (gcc 3.3.1, 3.3 doesn't have this
behaviour) gives some nasty warnings :

SDL_blit_A.c: In function `BlitRGBtoRGBSurfaceAlpha128MMX':
SDL_blit_A.c:223: warning: integer constant is too large for "long" type
SDL_blit_A.c:225: warning: integer constant is too large for "long" type
SDL_blit_A.c:227: warning: integer constant is too large for "long" type
[...]

The first attached patch (longlongfix.patch) tells gcc to really treat
those constants as unsigned long long and not long.

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