src/main/win32/SDL_win32_main.c
author Sam Lantinga
Fri, 10 Feb 2006 06:48:43 +0000
changeset 1358 c71e05b4dc2e
parent 1336 3692456e7b0f
child 1361 19418e4422cb
permissions -rw-r--r--
More header massaging... works great on Windows. ;-)
     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 #ifndef DISABLE_VIDEO
   224 #if 0
   225 	/* Create and register our class *
   226 	   DJM: If we do this here, the user nevers gets a chance to
   227 	   putenv(SDL_WINDOWID).  This is already called later by
   228 	   the (DIB|DX5)_CreateWindow function, so it should be
   229 	   safe to comment it out here.
   230 	if ( SDL_RegisterApp(appname, CS_BYTEALIGNCLIENT, 
   231 	                     GetModuleHandle(NULL)) < 0 ) {
   232 		ShowError("WinMain() error", SDL_GetError());
   233 		exit(1);
   234 	}*/
   235 #else
   236 	/* Sam:
   237 	   We still need to pass in the application handle so that
   238 	   DirectInput will initialize properly when SDL_RegisterApp()
   239 	   is called later in the video initialization.
   240 	 */
   241 	SDL_SetModuleHandle(GetModuleHandle(NULL));
   242 #endif /* 0 */
   243 #endif /* !DISABLE_VIDEO */
   244 
   245 	/* Run the application main() code */
   246 	SDL_main(argc, argv);
   247 
   248 	/* Exit cleanly, calling atexit() functions */
   249 	exit(0);
   250 
   251 	/* Hush little compiler, don't you cry... */
   252 	return(0);
   253 }
   254 
   255 /* This is where execution begins [windowed apps] */
   256 #ifdef _WIN32_WCE
   257 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
   258 #else
   259 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   260 #endif
   261 {
   262 	HINSTANCE handle;
   263 	char **argv;
   264 	int argc;
   265 	char *cmdline;
   266 	DWORD pathlen;
   267 #ifdef _WIN32_WCE
   268 	wchar_t path[MAX_PATH];
   269 #else
   270 	char path[MAX_PATH];
   271 #endif
   272 #ifdef _WIN32_WCE
   273 	wchar_t *bufp;
   274 	int nLen;
   275 #else
   276 	char *bufp;
   277 #endif
   278 #ifndef NO_STDIO_REDIRECT
   279 	FILE *newfp;
   280 #endif
   281 
   282 	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   283 	   keep them open.  This is a hack.. hopefully it will be fixed 
   284 	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   285 	 */
   286 	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   287 	if ( handle != NULL ) {
   288 		FreeLibrary(handle);
   289 	}
   290 
   291 #ifndef NO_STDIO_REDIRECT
   292 	pathlen = GetModuleFileName(NULL, path, SDL_TABLESIZE(path));
   293 	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   294 		--pathlen;
   295 	}
   296 	path[pathlen] = '\0';
   297 
   298 	SDL_strcpy( stdoutPath, path );
   299 	SDL_strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE );
   300     
   301 	/* Redirect standard input and standard output */
   302 	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   303 
   304 #ifndef _WIN32_WCE
   305 	if ( newfp == NULL ) {	/* This happens on NT */
   306 #if !defined(stdout)
   307 		stdout = fopen(stdoutPath, TEXT("w"));
   308 #else
   309 		newfp = fopen(stdoutPath, TEXT("w"));
   310 		if ( newfp ) {
   311 			*stdout = *newfp;
   312 		}
   313 #endif
   314 	}
   315 #endif /* _WIN32_WCE */
   316 
   317 	SDL_strcpy( stderrPath, path );
   318 	SDL_strcat( stderrPath, DIR_SEPERATOR STDERR_FILE );
   319 
   320 	newfp = freopen(stderrPath, TEXT("w"), stderr);
   321 #ifndef _WIN32_WCE
   322 	if ( newfp == NULL ) {	/* This happens on NT */
   323 #if !defined(stderr)
   324 		stderr = fopen(stderrPath, TEXT("w"));
   325 #else
   326 		newfp = fopen(stderrPath, TEXT("w"));
   327 		if ( newfp ) {
   328 			*stderr = *newfp;
   329 		}
   330 #endif
   331 	}
   332 #endif /* _WIN32_WCE */
   333 
   334 	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   335 	setbuf(stderr, NULL);			/* No buffering */
   336 #endif /* !NO_STDIO_REDIRECT */
   337 
   338 #ifdef _WIN32_WCE
   339 	nLen = wcslen(szCmdLine)+128+1;
   340 	bufp = (wchar_t *)alloca(nLen*2);
   341 	wcscpy (bufp, TEXT("\""));
   342 	GetModuleFileName(NULL, bufp+1, 128-3);
   343 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   344 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   345 	nLen = wcslen(bufp)+1;
   346 	cmdline = (char *)alloca(nLen);
   347 	if ( cmdline == NULL ) {
   348 		return OutOfMemory();
   349 	}
   350 	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   351 #else
   352 	/* Grab the command line (use alloca() on Windows) */
   353 	bufp = GetCommandLine();
   354 	cmdline = (char *)alloca(SDL_strlen(bufp)+1);
   355 	if ( cmdline == NULL ) {
   356 		return OutOfMemory();
   357 	}
   358 	SDL_strcpy(cmdline, bufp);
   359 #endif
   360 
   361 	/* Parse it into argv and argc */
   362 	argc = ParseCommandLine(cmdline, NULL);
   363 	argv = (char **)alloca((argc+1)*(sizeof *argv));
   364 	if ( argv == NULL ) {
   365 		return OutOfMemory();
   366 	}
   367 	ParseCommandLine(cmdline, argv);
   368 
   369 	/* Run the main program (after a little SDL initialization) */
   370 	return(console_main(argc, argv));
   371 }