Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
647 lines (524 loc) · 17.3 KB

SDL_main.c

File metadata and controls

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