comparison src/main/macos/SDL_main.c @ 1895:c121d94672cb

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