src/main/win32/SDL_win32_main.c
author Sam Lantinga
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1379 c0a74f199ecf
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
     1 /*
     2     SDL_main.c, placed in the public domain by Sam Lantinga  4/13/98
     3 
     4     The WinMain function -- calls your program's main() function
     5 */
     6 
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 
    10 #include "SDL_windows.h"
    11 
    12 #ifdef _WIN32_WCE
    13 # define DIR_SEPERATOR TEXT("\\")
    14 # undef _getcwd
    15 # define _getcwd(str,len)	wcscpy(str,TEXT(""))
    16 # define setbuf(f,b)
    17 # define setvbuf(w,x,y,z)
    18 # define fopen		_wfopen
    19 # define freopen	_wfreopen
    20 # define remove(x)	DeleteFile(x)
    21 # define strcat		wcscat
    22 #else
    23 # define DIR_SEPERATOR TEXT("/")
    24 # include <direct.h>
    25 #endif
    26 
    27 /* Include the SDL main definition header */
    28 #include "SDL.h"
    29 #include "SDL_main.h"
    30 
    31 #ifdef main
    32 # ifndef _WIN32_WCE_EMULATION
    33 #  undef main
    34 # endif /* _WIN32_WCE_EMULATION */
    35 #endif /* main */
    36 
    37 /* The standard output files */
    38 #define STDOUT_FILE	TEXT("stdout.txt")
    39 #define STDERR_FILE	TEXT("stderr.txt")
    40 
    41 #ifndef NO_STDIO_REDIRECT
    42 # ifdef _WIN32_WCE
    43   static wchar_t stdoutPath[MAX_PATH];
    44   static wchar_t stderrPath[MAX_PATH];
    45 # else
    46   static char stdoutPath[MAX_PATH];
    47   static char stderrPath[MAX_PATH];
    48 # endif
    49 #endif
    50 
    51 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
    52 /* seems to be undefined in Win CE although in online help */
    53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
    54 
    55 /* seems to be undefined in Win CE although in online help */
    56 char *SDL_strrchr(char *str, int c)
    57 {
    58 	char *p;
    59 
    60 	/* Skip to the end of the string */
    61 	p=str;
    62 	while (*p)
    63 		p++;
    64 
    65 	/* Look for the given character */
    66 	while ( (p >= str) && (*p != (CHAR)c) )
    67 		p--;
    68 
    69 	/* Return NULL if character not found */
    70 	if ( p < str ) {
    71 		p = NULL;
    72 	}
    73 	return p;
    74 }
    75 #endif /* _WIN32_WCE < 300 */
    76 
    77 /* Parse a command line buffer into arguments */
    78 static int ParseCommandLine(char *cmdline, char **argv)
    79 {
    80 	char *bufp;
    81 	int argc;
    82 
    83 	argc = 0;
    84 	for ( bufp = cmdline; *bufp; ) {
    85 		/* Skip leading whitespace */
    86 		while ( isspace(*bufp) ) {
    87 			++bufp;
    88 		}
    89 		/* Skip over argument */
    90 		if ( *bufp == '"' ) {
    91 			++bufp;
    92 			if ( *bufp ) {
    93 				if ( argv ) {
    94 					argv[argc] = bufp;
    95 				}
    96 				++argc;
    97 			}
    98 			/* Skip over word */
    99 			while ( *bufp && (*bufp != '"') ) {
   100 				++bufp;
   101 			}
   102 		} else {
   103 			if ( *bufp ) {
   104 				if ( argv ) {
   105 					argv[argc] = bufp;
   106 				}
   107 				++argc;
   108 			}
   109 			/* Skip over word */
   110 			while ( *bufp && ! isspace(*bufp) ) {
   111 				++bufp;
   112 			}
   113 		}
   114 		if ( *bufp ) {
   115 			if ( argv ) {
   116 				*bufp = '\0';
   117 			}
   118 			++bufp;
   119 		}
   120 	}
   121 	if ( argv ) {
   122 		argv[argc] = NULL;
   123 	}
   124 	return(argc);
   125 }
   126 
   127 /* Show an error message */
   128 static void ShowError(const char *title, const char *message)
   129 {
   130 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
   131 #ifdef USE_MESSAGEBOX
   132 	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
   133 #else
   134 	fprintf(stderr, "%s: %s\n", title, message);
   135 #endif
   136 }
   137 
   138 /* Pop up an out of memory message, returns to Windows */
   139 static BOOL OutOfMemory(void)
   140 {
   141 	ShowError("Fatal Error", "Out of memory - aborting");
   142 	return FALSE;
   143 }
   144 
   145 /* Remove the output files if there was no output written */
   146 static void __cdecl cleanup_output(void)
   147 {
   148 #ifndef NO_STDIO_REDIRECT
   149 	FILE *file;
   150 	int empty;
   151 #endif
   152 
   153 	/* Flush the output in case anything is queued */
   154 	fclose(stdout);
   155 	fclose(stderr);
   156 
   157 #ifndef NO_STDIO_REDIRECT
   158 	/* See if the files have any output in them */
   159 	if ( stdoutPath[0] ) {
   160 		file = fopen(stdoutPath, TEXT("rb"));
   161 		if ( file ) {
   162 			empty = (fgetc(file) == EOF) ? 1 : 0;
   163 			fclose(file);
   164 			if ( empty ) {
   165 				remove(stdoutPath);
   166 			}
   167 		}
   168 	}
   169 	if ( stderrPath[0] ) {
   170 		file = fopen(stderrPath, TEXT("rb"));
   171 		if ( file ) {
   172 			empty = (fgetc(file) == EOF) ? 1 : 0;
   173 			fclose(file);
   174 			if ( empty ) {
   175 				remove(stderrPath);
   176 			}
   177 		}
   178 	}
   179 #endif
   180 }
   181 
   182 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
   183 /* The VC++ compiler needs main defined */
   184 #define console_main main
   185 #endif
   186 
   187 /* This is where execution begins [console apps] */
   188 int console_main(int argc, char *argv[])
   189 {
   190 	int n;
   191 	char *bufp, *appname;
   192 
   193 	/* Get the class name from argv[0] */
   194 	appname = argv[0];
   195 	if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
   196 		appname = bufp+1;
   197 	} else
   198 	if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
   199 		appname = bufp+1;
   200 	}
   201 
   202 	if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
   203 		n = SDL_strlen(appname);
   204 	else
   205 		n = (bufp-appname);
   206 
   207 	bufp = (char *)alloca(n+1);
   208 	if ( bufp == NULL ) {
   209 		return OutOfMemory();
   210 	}
   211 	SDL_strncpy(bufp, appname, n);
   212 	bufp[n] = '\0';
   213 	appname = bufp;
   214 
   215 	/* Load SDL dynamic link library */
   216 	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
   217 		ShowError("WinMain() error", SDL_GetError());
   218 		return(FALSE);
   219 	}
   220 	atexit(cleanup_output);
   221 	atexit(SDL_Quit);
   222 
   223 	/* Sam:
   224 	   We still need to pass in the application handle so that
   225 	   DirectInput will initialize properly when SDL_RegisterApp()
   226 	   is called later in the video initialization.
   227 	 */
   228 	SDL_SetModuleHandle(GetModuleHandle(NULL));
   229 
   230 	/* Run the application main() code */
   231 	SDL_main(argc, argv);
   232 
   233 	/* Exit cleanly, calling atexit() functions */
   234 	exit(0);
   235 
   236 	/* Hush little compiler, don't you cry... */
   237 	return(0);
   238 }
   239 
   240 /* This is where execution begins [windowed apps] */
   241 #ifdef _WIN32_WCE
   242 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
   243 #else
   244 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   245 #endif
   246 {
   247 	HINSTANCE handle;
   248 	char **argv;
   249 	int argc;
   250 	char *cmdline;
   251 	DWORD pathlen;
   252 #ifdef _WIN32_WCE
   253 	wchar_t path[MAX_PATH];
   254 #else
   255 	char path[MAX_PATH];
   256 #endif
   257 #ifdef _WIN32_WCE
   258 	wchar_t *bufp;
   259 	int nLen;
   260 #else
   261 	char *bufp;
   262 #endif
   263 #ifndef NO_STDIO_REDIRECT
   264 	FILE *newfp;
   265 #endif
   266 
   267 	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   268 	   keep them open.  This is a hack.. hopefully it will be fixed 
   269 	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   270 	 */
   271 	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   272 	if ( handle != NULL ) {
   273 		FreeLibrary(handle);
   274 	}
   275 
   276 #ifndef NO_STDIO_REDIRECT
   277 	pathlen = GetModuleFileName(NULL, path, SDL_TABLESIZE(path));
   278 	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   279 		--pathlen;
   280 	}
   281 	path[pathlen] = '\0';
   282 
   283 	SDL_strcpy( stdoutPath, path );
   284 	SDL_strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE );
   285     
   286 	/* Redirect standard input and standard output */
   287 	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   288 
   289 #ifndef _WIN32_WCE
   290 	if ( newfp == NULL ) {	/* This happens on NT */
   291 #if !defined(stdout)
   292 		stdout = fopen(stdoutPath, TEXT("w"));
   293 #else
   294 		newfp = fopen(stdoutPath, TEXT("w"));
   295 		if ( newfp ) {
   296 			*stdout = *newfp;
   297 		}
   298 #endif
   299 	}
   300 #endif /* _WIN32_WCE */
   301 
   302 	SDL_strcpy( stderrPath, path );
   303 	SDL_strcat( stderrPath, DIR_SEPERATOR STDERR_FILE );
   304 
   305 	newfp = freopen(stderrPath, TEXT("w"), stderr);
   306 #ifndef _WIN32_WCE
   307 	if ( newfp == NULL ) {	/* This happens on NT */
   308 #if !defined(stderr)
   309 		stderr = fopen(stderrPath, TEXT("w"));
   310 #else
   311 		newfp = fopen(stderrPath, TEXT("w"));
   312 		if ( newfp ) {
   313 			*stderr = *newfp;
   314 		}
   315 #endif
   316 	}
   317 #endif /* _WIN32_WCE */
   318 
   319 	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   320 	setbuf(stderr, NULL);			/* No buffering */
   321 #endif /* !NO_STDIO_REDIRECT */
   322 
   323 #ifdef _WIN32_WCE
   324 	nLen = wcslen(szCmdLine)+128+1;
   325 	bufp = (wchar_t *)alloca(nLen*2);
   326 	wcscpy (bufp, TEXT("\""));
   327 	GetModuleFileName(NULL, bufp+1, 128-3);
   328 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   329 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   330 	nLen = wcslen(bufp)+1;
   331 	cmdline = (char *)alloca(nLen);
   332 	if ( cmdline == NULL ) {
   333 		return OutOfMemory();
   334 	}
   335 	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   336 #else
   337 	/* Grab the command line (use alloca() on Windows) */
   338 	bufp = GetCommandLine();
   339 	cmdline = (char *)alloca(SDL_strlen(bufp)+1);
   340 	if ( cmdline == NULL ) {
   341 		return OutOfMemory();
   342 	}
   343 	SDL_strcpy(cmdline, bufp);
   344 #endif
   345 
   346 	/* Parse it into argv and argc */
   347 	argc = ParseCommandLine(cmdline, NULL);
   348 	argv = (char **)alloca((argc+1)*(sizeof *argv));
   349 	if ( argv == NULL ) {
   350 		return OutOfMemory();
   351 	}
   352 	ParseCommandLine(cmdline, argv);
   353 
   354 	/* Run the main program (after a little SDL initialization) */
   355 	return(console_main(argc, argv));
   356 }