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