src/main/win32/SDL_win32_main.c
author Sam Lantinga
Sat, 25 Feb 2006 22:18:25 +0000
changeset 1433 bb6839704ed6
parent 1423 4ed717f9e509
child 1465 8dfa9a6d69a5
permissions -rw-r--r--
SDL_windows.h is no longer necessary
     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 #define WIN32_LEAN_AND_MEAN
    11 #include <windows.h>
    12 
    13 #ifdef _WIN32_WCE
    14 # define DIR_SEPERATOR TEXT("\\")
    15 # undef _getcwd
    16 # define _getcwd(str,len)	wcscpy(str,TEXT(""))
    17 # define setbuf(f,b)
    18 # define setvbuf(w,x,y,z)
    19 # define fopen		_wfopen
    20 # define freopen	_wfreopen
    21 # define remove(x)	DeleteFile(x)
    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 	int status;
   193 
   194 	/* Get the class name from argv[0] */
   195 	appname = argv[0];
   196 	if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
   197 		appname = bufp+1;
   198 	} else
   199 	if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
   200 		appname = bufp+1;
   201 	}
   202 
   203 	if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
   204 		n = SDL_strlen(appname);
   205 	else
   206 		n = (bufp-appname);
   207 
   208 	bufp = SDL_stack_alloc(char, n+1);
   209 	if ( bufp == NULL ) {
   210 		return OutOfMemory();
   211 	}
   212 	SDL_strlcpy(bufp, appname, n);
   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 	status = SDL_main(argc, argv);
   232 
   233 	/* Exit cleanly, calling atexit() functions */
   234 	exit(status);
   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 	size_t nLen;
   263 #endif
   264 #ifndef NO_STDIO_REDIRECT
   265 	FILE *newfp;
   266 #endif
   267 
   268 	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   269 	   keep them open.  This is a hack.. hopefully it will be fixed 
   270 	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   271 	 */
   272 	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   273 	if ( handle != NULL ) {
   274 		FreeLibrary(handle);
   275 	}
   276 
   277 #ifndef NO_STDIO_REDIRECT
   278 	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
   279 	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   280 		--pathlen;
   281 	}
   282 	path[pathlen] = '\0';
   283 
   284 	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   285 	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   286     
   287 	/* Redirect standard input and standard output */
   288 	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   289 
   290 #ifndef _WIN32_WCE
   291 	if ( newfp == NULL ) {	/* This happens on NT */
   292 #if !defined(stdout)
   293 		stdout = fopen(stdoutPath, TEXT("w"));
   294 #else
   295 		newfp = fopen(stdoutPath, TEXT("w"));
   296 		if ( newfp ) {
   297 			*stdout = *newfp;
   298 		}
   299 #endif
   300 	}
   301 #endif /* _WIN32_WCE */
   302 
   303 	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
   304 	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
   305 
   306 	newfp = freopen(stderrPath, TEXT("w"), stderr);
   307 #ifndef _WIN32_WCE
   308 	if ( newfp == NULL ) {	/* This happens on NT */
   309 #if !defined(stderr)
   310 		stderr = fopen(stderrPath, TEXT("w"));
   311 #else
   312 		newfp = fopen(stderrPath, TEXT("w"));
   313 		if ( newfp ) {
   314 			*stderr = *newfp;
   315 		}
   316 #endif
   317 	}
   318 #endif /* _WIN32_WCE */
   319 
   320 	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   321 	setbuf(stderr, NULL);			/* No buffering */
   322 #endif /* !NO_STDIO_REDIRECT */
   323 
   324 #ifdef _WIN32_WCE
   325 	nLen = wcslen(szCmdLine)+128+1;
   326 	bufp = SDL_stack_alloc(wchar_t, nLen*2);
   327 	wcscpy (bufp, TEXT("\""));
   328 	GetModuleFileName(NULL, bufp+1, 128-3);
   329 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   330 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   331 	nLen = wcslen(bufp)+1;
   332 	cmdline = SDL_stack_alloc(wchar_t, nLen);
   333 	if ( cmdline == NULL ) {
   334 		return OutOfMemory();
   335 	}
   336 	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   337 #else
   338 	/* Grab the command line */
   339 	bufp = GetCommandLine();
   340 	nLen = SDL_strlen(bufp)+1;
   341 	cmdline = SDL_stack_alloc(char, nLen);
   342 	if ( cmdline == NULL ) {
   343 		return OutOfMemory();
   344 	}
   345 	SDL_strlcpy(cmdline, bufp, nLen);
   346 #endif
   347 
   348 	/* Parse it into argv and argc */
   349 	argc = ParseCommandLine(cmdline, NULL);
   350 	argv = SDL_stack_alloc(char*, argc+1);
   351 	if ( argv == NULL ) {
   352 		return OutOfMemory();
   353 	}
   354 	ParseCommandLine(cmdline, argv);
   355 
   356 	/* Run the main program (after a little SDL initialization) */
   357 	console_main(argc, argv);
   358 
   359 	/* Hush little compiler, don't you cry... */
   360 	return 0;
   361 }