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