comparison src/main/win32/SDL_win32_main.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
38 #define STDOUT_FILE TEXT("stdout.txt") 38 #define STDOUT_FILE TEXT("stdout.txt")
39 #define STDERR_FILE TEXT("stderr.txt") 39 #define STDERR_FILE TEXT("stderr.txt")
40 40
41 #ifndef NO_STDIO_REDIRECT 41 #ifndef NO_STDIO_REDIRECT
42 # ifdef _WIN32_WCE 42 # ifdef _WIN32_WCE
43 static wchar_t stdoutPath[MAX_PATH]; 43 static wchar_t stdoutPath[MAX_PATH];
44 static wchar_t stderrPath[MAX_PATH]; 44 static wchar_t stderrPath[MAX_PATH];
45 # else 45 # else
46 static char stdoutPath[MAX_PATH]; 46 static char stdoutPath[MAX_PATH];
47 static char stderrPath[MAX_PATH]; 47 static char stderrPath[MAX_PATH];
48 # endif 48 # endif
49 #endif 49 #endif
50 50
51 #if defined(_WIN32_WCE) && _WIN32_WCE < 300 51 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
52 /* seems to be undefined in Win CE although in online help */ 52 /* seems to be undefined in Win CE although in online help */
53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) 53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
54 #endif /* _WIN32_WCE < 300 */ 54 #endif /* _WIN32_WCE < 300 */
55 55
56 /* Parse a command line buffer into arguments */ 56 /* Parse a command line buffer into arguments */
57 static int ParseCommandLine(char *cmdline, char **argv) 57 static int
58 { 58 ParseCommandLine (char *cmdline, char **argv)
59 char *bufp; 59 {
60 int argc; 60 char *bufp;
61 61 int argc;
62 argc = 0; 62
63 for ( bufp = cmdline; *bufp; ) { 63 argc = 0;
64 /* Skip leading whitespace */ 64 for (bufp = cmdline; *bufp;) {
65 while ( isspace(*bufp) ) { 65 /* Skip leading whitespace */
66 ++bufp; 66 while (isspace (*bufp)) {
67 } 67 ++bufp;
68 /* Skip over argument */ 68 }
69 if ( *bufp == '"' ) { 69 /* Skip over argument */
70 ++bufp; 70 if (*bufp == '"') {
71 if ( *bufp ) { 71 ++bufp;
72 if ( argv ) { 72 if (*bufp) {
73 argv[argc] = bufp; 73 if (argv) {
74 } 74 argv[argc] = bufp;
75 ++argc; 75 }
76 } 76 ++argc;
77 /* Skip over word */ 77 }
78 while ( *bufp && (*bufp != '"') ) { 78 /* Skip over word */
79 ++bufp; 79 while (*bufp && (*bufp != '"')) {
80 } 80 ++bufp;
81 } else { 81 }
82 if ( *bufp ) { 82 } else {
83 if ( argv ) { 83 if (*bufp) {
84 argv[argc] = bufp; 84 if (argv) {
85 } 85 argv[argc] = bufp;
86 ++argc; 86 }
87 } 87 ++argc;
88 /* Skip over word */ 88 }
89 while ( *bufp && ! isspace(*bufp) ) { 89 /* Skip over word */
90 ++bufp; 90 while (*bufp && !isspace (*bufp)) {
91 } 91 ++bufp;
92 } 92 }
93 if ( *bufp ) { 93 }
94 if ( argv ) { 94 if (*bufp) {
95 *bufp = '\0'; 95 if (argv) {
96 } 96 *bufp = '\0';
97 ++bufp; 97 }
98 } 98 ++bufp;
99 } 99 }
100 if ( argv ) { 100 }
101 argv[argc] = NULL; 101 if (argv) {
102 } 102 argv[argc] = NULL;
103 return(argc); 103 }
104 return (argc);
104 } 105 }
105 106
106 /* Show an error message */ 107 /* Show an error message */
107 static void ShowError(const char *title, const char *message) 108 static void
109 ShowError (const char *title, const char *message)
108 { 110 {
109 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */ 111 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
110 #ifdef USE_MESSAGEBOX 112 #ifdef USE_MESSAGEBOX
111 MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK); 113 MessageBox (NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
112 #else 114 #else
113 fprintf(stderr, "%s: %s\n", title, message); 115 fprintf (stderr, "%s: %s\n", title, message);
114 #endif 116 #endif
115 } 117 }
116 118
117 /* Pop up an out of memory message, returns to Windows */ 119 /* Pop up an out of memory message, returns to Windows */
118 static BOOL OutOfMemory(void) 120 static BOOL
119 { 121 OutOfMemory (void)
120 ShowError("Fatal Error", "Out of memory - aborting"); 122 {
121 return FALSE; 123 ShowError ("Fatal Error", "Out of memory - aborting");
124 return FALSE;
122 } 125 }
123 126
124 /* SDL_Quit() shouldn't be used with atexit() directly because 127 /* SDL_Quit() shouldn't be used with atexit() directly because
125 calling conventions may differ... */ 128 calling conventions may differ... */
126 static void cleanup(void) 129 static void
127 { 130 cleanup (void)
128 SDL_Quit(); 131 {
132 SDL_Quit ();
129 } 133 }
130 134
131 /* Remove the output files if there was no output written */ 135 /* Remove the output files if there was no output written */
132 static void cleanup_output(void) 136 static void
133 { 137 cleanup_output (void)
134 #ifndef NO_STDIO_REDIRECT 138 {
135 FILE *file; 139 #ifndef NO_STDIO_REDIRECT
136 int empty; 140 FILE *file;
137 #endif 141 int empty;
138 142 #endif
139 /* Flush the output in case anything is queued */ 143
140 fclose(stdout); 144 /* Flush the output in case anything is queued */
141 fclose(stderr); 145 fclose (stdout);
142 146 fclose (stderr);
143 #ifndef NO_STDIO_REDIRECT 147
144 /* See if the files have any output in them */ 148 #ifndef NO_STDIO_REDIRECT
145 if ( stdoutPath[0] ) { 149 /* See if the files have any output in them */
146 file = fopen(stdoutPath, TEXT("rb")); 150 if (stdoutPath[0]) {
147 if ( file ) { 151 file = fopen (stdoutPath, TEXT ("rb"));
148 empty = (fgetc(file) == EOF) ? 1 : 0; 152 if (file) {
149 fclose(file); 153 empty = (fgetc (file) == EOF) ? 1 : 0;
150 if ( empty ) { 154 fclose (file);
151 remove(stdoutPath); 155 if (empty) {
152 } 156 remove (stdoutPath);
153 } 157 }
154 } 158 }
155 if ( stderrPath[0] ) { 159 }
156 file = fopen(stderrPath, TEXT("rb")); 160 if (stderrPath[0]) {
157 if ( file ) { 161 file = fopen (stderrPath, TEXT ("rb"));
158 empty = (fgetc(file) == EOF) ? 1 : 0; 162 if (file) {
159 fclose(file); 163 empty = (fgetc (file) == EOF) ? 1 : 0;
160 if ( empty ) { 164 fclose (file);
161 remove(stderrPath); 165 if (empty) {
162 } 166 remove (stderrPath);
163 } 167 }
164 } 168 }
169 }
165 #endif 170 #endif
166 } 171 }
167 172
168 #if defined(_MSC_VER) && !defined(_WIN32_WCE) 173 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
169 /* The VC++ compiler needs main defined */ 174 /* The VC++ compiler needs main defined */
170 #define console_main main 175 #define console_main main
171 #endif 176 #endif
172 177
173 /* This is where execution begins [console apps] */ 178 /* This is where execution begins [console apps] */
174 int console_main(int argc, char *argv[]) 179 int
175 { 180 console_main (int argc, char *argv[])
176 size_t n; 181 {
177 char *bufp, *appname; 182 size_t n;
178 int status; 183 char *bufp, *appname;
179 184 int status;
180 /* Get the class name from argv[0] */ 185
181 appname = argv[0]; 186 /* Get the class name from argv[0] */
182 if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) { 187 appname = argv[0];
183 appname = bufp+1; 188 if ((bufp = SDL_strrchr (argv[0], '\\')) != NULL) {
184 } else 189 appname = bufp + 1;
185 if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) { 190 } else if ((bufp = SDL_strrchr (argv[0], '/')) != NULL) {
186 appname = bufp+1; 191 appname = bufp + 1;
187 } 192 }
188 193
189 if ( (bufp=SDL_strrchr(appname, '.')) == NULL ) 194 if ((bufp = SDL_strrchr (appname, '.')) == NULL)
190 n = SDL_strlen(appname); 195 n = SDL_strlen (appname);
191 else 196 else
192 n = (bufp-appname); 197 n = (bufp - appname);
193 198
194 bufp = SDL_stack_alloc(char, n+1); 199 bufp = SDL_stack_alloc (char, n + 1);
195 if ( bufp == NULL ) { 200 if (bufp == NULL) {
196 return OutOfMemory(); 201 return OutOfMemory ();
197 } 202 }
198 SDL_strlcpy(bufp, appname, n+1); 203 SDL_strlcpy (bufp, appname, n + 1);
199 appname = bufp; 204 appname = bufp;
200 205
201 /* Load SDL dynamic link library */ 206 /* Load SDL dynamic link library */
202 if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) { 207 if (SDL_Init (SDL_INIT_NOPARACHUTE) < 0) {
203 ShowError("WinMain() error", SDL_GetError()); 208 ShowError ("WinMain() error", SDL_GetError ());
204 return(FALSE); 209 return (FALSE);
205 } 210 }
206 atexit(cleanup_output); 211 atexit (cleanup_output);
207 atexit(cleanup); 212 atexit (cleanup);
208 213
209 /* Sam: 214 /* Sam:
210 We still need to pass in the application handle so that 215 We still need to pass in the application handle so that
211 DirectInput will initialize properly when SDL_RegisterApp() 216 DirectInput will initialize properly when SDL_RegisterApp()
212 is called later in the video initialization. 217 is called later in the video initialization.
213 */ 218 */
214 SDL_SetModuleHandle(GetModuleHandle(NULL)); 219 SDL_SetModuleHandle (GetModuleHandle (NULL));
215 220
216 /* Run the application main() code */ 221 /* Run the application main() code */
217 status = SDL_main(argc, argv); 222 status = SDL_main (argc, argv);
218 223
219 /* Exit cleanly, calling atexit() functions */ 224 /* Exit cleanly, calling atexit() functions */
220 exit(status); 225 exit (status);
221 226
222 /* Hush little compiler, don't you cry... */ 227 /* Hush little compiler, don't you cry... */
223 return 0; 228 return 0;
224 } 229 }
225 230
226 /* This is where execution begins [windowed apps] */ 231 /* This is where execution begins [windowed apps] */
227 #ifdef _WIN32_WCE 232 #ifdef _WIN32_WCE
228 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw) 233 int WINAPI
229 #else 234 WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
230 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) 235 #else
231 #endif 236 int WINAPI
232 { 237 WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
233 HINSTANCE handle; 238 #endif
234 char **argv; 239 {
235 int argc; 240 HINSTANCE handle;
236 char *cmdline; 241 char **argv;
237 DWORD pathlen; 242 int argc;
238 #ifdef _WIN32_WCE 243 char *cmdline;
239 wchar_t path[MAX_PATH]; 244 DWORD pathlen;
240 #else 245 #ifdef _WIN32_WCE
241 char path[MAX_PATH]; 246 wchar_t path[MAX_PATH];
242 #endif 247 #else
243 #ifdef _WIN32_WCE 248 char path[MAX_PATH];
244 wchar_t *bufp; 249 #endif
245 int nLen; 250 #ifdef _WIN32_WCE
246 #else 251 wchar_t *bufp;
247 char *bufp; 252 int nLen;
248 size_t nLen; 253 #else
249 #endif 254 char *bufp;
250 #ifndef NO_STDIO_REDIRECT 255 size_t nLen;
251 FILE *newfp; 256 #endif
252 #endif 257 #ifndef NO_STDIO_REDIRECT
253 258 FILE *newfp;
254 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't 259 #endif
255 keep them open. This is a hack.. hopefully it will be fixed 260
256 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. 261 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
257 */ 262 keep them open. This is a hack.. hopefully it will be fixed
258 handle = LoadLibrary(TEXT("DDRAW.DLL")); 263 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
259 if ( handle != NULL ) { 264 */
260 FreeLibrary(handle); 265 handle = LoadLibrary (TEXT ("DDRAW.DLL"));
261 } 266 if (handle != NULL) {
262 267 FreeLibrary (handle);
263 #ifndef NO_STDIO_REDIRECT 268 }
264 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); 269 #ifndef NO_STDIO_REDIRECT
265 while ( pathlen > 0 && path[pathlen] != '\\' ) { 270 pathlen = GetModuleFileName (NULL, path, SDL_arraysize (path));
266 --pathlen; 271 while (pathlen > 0 && path[pathlen] != '\\') {
267 } 272 --pathlen;
268 path[pathlen] = '\0'; 273 }
269 274 path[pathlen] = '\0';
270 #ifdef _WIN32_WCE 275
271 wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); 276 #ifdef _WIN32_WCE
272 wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); 277 wcsncpy (stdoutPath, path, SDL_arraysize (stdoutPath));
273 #else 278 wcsncat (stdoutPath, DIR_SEPERATOR STDOUT_FILE,
274 SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); 279 SDL_arraysize (stdoutPath));
275 SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); 280 #else
276 #endif 281 SDL_strlcpy (stdoutPath, path, SDL_arraysize (stdoutPath));
277 282 SDL_strlcat (stdoutPath, DIR_SEPERATOR STDOUT_FILE,
278 /* Redirect standard input and standard output */ 283 SDL_arraysize (stdoutPath));
279 newfp = freopen(stdoutPath, TEXT("w"), stdout); 284 #endif
285
286 /* Redirect standard input and standard output */
287 newfp = freopen (stdoutPath, TEXT ("w"), stdout);
280 288
281 #ifndef _WIN32_WCE 289 #ifndef _WIN32_WCE
282 if ( newfp == NULL ) { /* This happens on NT */ 290 if (newfp == NULL) { /* This happens on NT */
283 #if !defined(stdout) 291 #if !defined(stdout)
284 stdout = fopen(stdoutPath, TEXT("w")); 292 stdout = fopen (stdoutPath, TEXT ("w"));
285 #else 293 #else
286 newfp = fopen(stdoutPath, TEXT("w")); 294 newfp = fopen (stdoutPath, TEXT ("w"));
287 if ( newfp ) { 295 if (newfp) {
288 *stdout = *newfp; 296 *stdout = *newfp;
289 } 297 }
290 #endif 298 #endif
291 } 299 }
292 #endif /* _WIN32_WCE */ 300 #endif /* _WIN32_WCE */
293 301
294 #ifdef _WIN32_WCE 302 #ifdef _WIN32_WCE
295 wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) ); 303 wcsncpy (stderrPath, path, SDL_arraysize (stdoutPath));
296 wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); 304 wcsncat (stderrPath, DIR_SEPERATOR STDOUT_FILE,
297 #else 305 SDL_arraysize (stdoutPath));
298 SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) ); 306 #else
299 SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) ); 307 SDL_strlcpy (stderrPath, path, SDL_arraysize (stderrPath));
300 #endif 308 SDL_strlcat (stderrPath, DIR_SEPERATOR STDERR_FILE,
301 309 SDL_arraysize (stderrPath));
302 newfp = freopen(stderrPath, TEXT("w"), stderr); 310 #endif
311
312 newfp = freopen (stderrPath, TEXT ("w"), stderr);
303 #ifndef _WIN32_WCE 313 #ifndef _WIN32_WCE
304 if ( newfp == NULL ) { /* This happens on NT */ 314 if (newfp == NULL) { /* This happens on NT */
305 #if !defined(stderr) 315 #if !defined(stderr)
306 stderr = fopen(stderrPath, TEXT("w")); 316 stderr = fopen (stderrPath, TEXT ("w"));
307 #else 317 #else
308 newfp = fopen(stderrPath, TEXT("w")); 318 newfp = fopen (stderrPath, TEXT ("w"));
309 if ( newfp ) { 319 if (newfp) {
310 *stderr = *newfp; 320 *stderr = *newfp;
311 } 321 }
312 #endif 322 #endif
313 } 323 }
314 #endif /* _WIN32_WCE */ 324 #endif /* _WIN32_WCE */
315 325
316 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ 326 setvbuf (stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
317 setbuf(stderr, NULL); /* No buffering */ 327 setbuf (stderr, NULL); /* No buffering */
318 #endif /* !NO_STDIO_REDIRECT */ 328 #endif /* !NO_STDIO_REDIRECT */
319 329
320 #ifdef _WIN32_WCE 330 #ifdef _WIN32_WCE
321 nLen = wcslen(szCmdLine)+128+1; 331 nLen = wcslen (szCmdLine) + 128 + 1;
322 bufp = SDL_stack_alloc(wchar_t, nLen*2); 332 bufp = SDL_stack_alloc (wchar_t, nLen * 2);
323 wcscpy (bufp, TEXT("\"")); 333 wcscpy (bufp, TEXT ("\""));
324 GetModuleFileName(NULL, bufp+1, 128-3); 334 GetModuleFileName (NULL, bufp + 1, 128 - 3);
325 wcscpy (bufp+wcslen(bufp), TEXT("\" ")); 335 wcscpy (bufp + wcslen (bufp), TEXT ("\" "));
326 wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp)); 336 wcsncpy (bufp + wcslen (bufp), szCmdLine, nLen - wcslen (bufp));
327 nLen = wcslen(bufp)+1; 337 nLen = wcslen (bufp) + 1;
328 cmdline = SDL_stack_alloc(char, nLen); 338 cmdline = SDL_stack_alloc (char, nLen);
329 if ( cmdline == NULL ) { 339 if (cmdline == NULL) {
330 return OutOfMemory(); 340 return OutOfMemory ();
331 } 341 }
332 WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL); 342 WideCharToMultiByte (CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
333 #else 343 #else
334 /* Grab the command line */ 344 /* Grab the command line */
335 bufp = GetCommandLine(); 345 bufp = GetCommandLine ();
336 nLen = SDL_strlen(bufp)+1; 346 nLen = SDL_strlen (bufp) + 1;
337 cmdline = SDL_stack_alloc(char, nLen); 347 cmdline = SDL_stack_alloc (char, nLen);
338 if ( cmdline == NULL ) { 348 if (cmdline == NULL) {
339 return OutOfMemory(); 349 return OutOfMemory ();
340 } 350 }
341 SDL_strlcpy(cmdline, bufp, nLen); 351 SDL_strlcpy (cmdline, bufp, nLen);
342 #endif 352 #endif
343 353
344 /* Parse it into argv and argc */ 354 /* Parse it into argv and argc */
345 argc = ParseCommandLine(cmdline, NULL); 355 argc = ParseCommandLine (cmdline, NULL);
346 argv = SDL_stack_alloc(char*, argc+1); 356 argv = SDL_stack_alloc (char *, argc + 1);
347 if ( argv == NULL ) { 357 if (argv == NULL) {
348 return OutOfMemory(); 358 return OutOfMemory ();
349 } 359 }
350 ParseCommandLine(cmdline, argv); 360 ParseCommandLine (cmdline, argv);
351 361
352 /* Run the main program (after a little SDL initialization) */ 362 /* Run the main program (after a little SDL initialization) */
353 console_main(argc, argv); 363 console_main (argc, argv);
354 364
355 /* Hush little compiler, don't you cry... */ 365 /* Hush little compiler, don't you cry... */
356 return 0; 366 return 0;
357 } 367 }
368
369 /* vi: set ts=4 sw=4 expandtab: */