src/main/win32/SDL_win32_main.c
author Sam Lantinga
Mon, 06 Mar 2006 08:11:10 +0000
changeset 1472 4aac8563c296
parent 1465 8dfa9a6d69a5
child 1659 14717b52abc0
permissions -rw-r--r--
Fixed more Win64 portability issues
     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 #endif /* _WIN32_WCE < 300 */
    55 
    56 /* Parse a command line buffer into arguments */
    57 static int ParseCommandLine(char *cmdline, char **argv)
    58 {
    59 	char *bufp;
    60 	int argc;
    61 
    62 	argc = 0;
    63 	for ( bufp = cmdline; *bufp; ) {
    64 		/* Skip leading whitespace */
    65 		while ( isspace(*bufp) ) {
    66 			++bufp;
    67 		}
    68 		/* Skip over argument */
    69 		if ( *bufp == '"' ) {
    70 			++bufp;
    71 			if ( *bufp ) {
    72 				if ( argv ) {
    73 					argv[argc] = bufp;
    74 				}
    75 				++argc;
    76 			}
    77 			/* Skip over word */
    78 			while ( *bufp && (*bufp != '"') ) {
    79 				++bufp;
    80 			}
    81 		} else {
    82 			if ( *bufp ) {
    83 				if ( argv ) {
    84 					argv[argc] = bufp;
    85 				}
    86 				++argc;
    87 			}
    88 			/* Skip over word */
    89 			while ( *bufp && ! isspace(*bufp) ) {
    90 				++bufp;
    91 			}
    92 		}
    93 		if ( *bufp ) {
    94 			if ( argv ) {
    95 				*bufp = '\0';
    96 			}
    97 			++bufp;
    98 		}
    99 	}
   100 	if ( argv ) {
   101 		argv[argc] = NULL;
   102 	}
   103 	return(argc);
   104 }
   105 
   106 /* Show an error message */
   107 static void ShowError(const char *title, const char *message)
   108 {
   109 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
   110 #ifdef USE_MESSAGEBOX
   111 	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
   112 #else
   113 	fprintf(stderr, "%s: %s\n", title, message);
   114 #endif
   115 }
   116 
   117 /* Pop up an out of memory message, returns to Windows */
   118 static BOOL OutOfMemory(void)
   119 {
   120 	ShowError("Fatal Error", "Out of memory - aborting");
   121 	return FALSE;
   122 }
   123 
   124 /* Remove the output files if there was no output written */
   125 static void __cdecl cleanup_output(void)
   126 {
   127 #ifndef NO_STDIO_REDIRECT
   128 	FILE *file;
   129 	int empty;
   130 #endif
   131 
   132 	/* Flush the output in case anything is queued */
   133 	fclose(stdout);
   134 	fclose(stderr);
   135 
   136 #ifndef NO_STDIO_REDIRECT
   137 	/* See if the files have any output in them */
   138 	if ( stdoutPath[0] ) {
   139 		file = fopen(stdoutPath, TEXT("rb"));
   140 		if ( file ) {
   141 			empty = (fgetc(file) == EOF) ? 1 : 0;
   142 			fclose(file);
   143 			if ( empty ) {
   144 				remove(stdoutPath);
   145 			}
   146 		}
   147 	}
   148 	if ( stderrPath[0] ) {
   149 		file = fopen(stderrPath, TEXT("rb"));
   150 		if ( file ) {
   151 			empty = (fgetc(file) == EOF) ? 1 : 0;
   152 			fclose(file);
   153 			if ( empty ) {
   154 				remove(stderrPath);
   155 			}
   156 		}
   157 	}
   158 #endif
   159 }
   160 
   161 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
   162 /* The VC++ compiler needs main defined */
   163 #define console_main main
   164 #endif
   165 
   166 /* This is where execution begins [console apps] */
   167 int console_main(int argc, char *argv[])
   168 {
   169 	size_t n;
   170 	char *bufp, *appname;
   171 	int status;
   172 
   173 	/* Get the class name from argv[0] */
   174 	appname = argv[0];
   175 	if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
   176 		appname = bufp+1;
   177 	} else
   178 	if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
   179 		appname = bufp+1;
   180 	}
   181 
   182 	if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
   183 		n = SDL_strlen(appname);
   184 	else
   185 		n = (bufp-appname);
   186 
   187 	bufp = SDL_stack_alloc(char, n+1);
   188 	if ( bufp == NULL ) {
   189 		return OutOfMemory();
   190 	}
   191 	SDL_strlcpy(bufp, appname, n);
   192 	appname = bufp;
   193 
   194 	/* Load SDL dynamic link library */
   195 	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
   196 		ShowError("WinMain() error", SDL_GetError());
   197 		return(FALSE);
   198 	}
   199 	atexit(cleanup_output);
   200 	atexit(SDL_Quit);
   201 
   202 	/* Sam:
   203 	   We still need to pass in the application handle so that
   204 	   DirectInput will initialize properly when SDL_RegisterApp()
   205 	   is called later in the video initialization.
   206 	 */
   207 	SDL_SetModuleHandle(GetModuleHandle(NULL));
   208 
   209 	/* Run the application main() code */
   210 	status = SDL_main(argc, argv);
   211 
   212 	/* Exit cleanly, calling atexit() functions */
   213 	exit(status);
   214 
   215 	/* Hush little compiler, don't you cry... */
   216 	return 0;
   217 }
   218 
   219 /* This is where execution begins [windowed apps] */
   220 #ifdef _WIN32_WCE
   221 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
   222 #else
   223 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   224 #endif
   225 {
   226 	HINSTANCE handle;
   227 	char **argv;
   228 	int argc;
   229 	char *cmdline;
   230 	DWORD pathlen;
   231 #ifdef _WIN32_WCE
   232 	wchar_t path[MAX_PATH];
   233 #else
   234 	char path[MAX_PATH];
   235 #endif
   236 #ifdef _WIN32_WCE
   237 	wchar_t *bufp;
   238 	int nLen;
   239 #else
   240 	char *bufp;
   241 	size_t nLen;
   242 #endif
   243 #ifndef NO_STDIO_REDIRECT
   244 	FILE *newfp;
   245 #endif
   246 
   247 	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   248 	   keep them open.  This is a hack.. hopefully it will be fixed 
   249 	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   250 	 */
   251 	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   252 	if ( handle != NULL ) {
   253 		FreeLibrary(handle);
   254 	}
   255 
   256 #ifndef NO_STDIO_REDIRECT
   257 	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
   258 	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   259 		--pathlen;
   260 	}
   261 	path[pathlen] = '\0';
   262 
   263 #ifdef _WIN32_WCE
   264 	wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   265 	wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   266 #else
   267 	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   268 	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   269 #endif
   270     
   271 	/* Redirect standard input and standard output */
   272 	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   273 
   274 #ifndef _WIN32_WCE
   275 	if ( newfp == NULL ) {	/* This happens on NT */
   276 #if !defined(stdout)
   277 		stdout = fopen(stdoutPath, TEXT("w"));
   278 #else
   279 		newfp = fopen(stdoutPath, TEXT("w"));
   280 		if ( newfp ) {
   281 			*stdout = *newfp;
   282 		}
   283 #endif
   284 	}
   285 #endif /* _WIN32_WCE */
   286 
   287 #ifdef _WIN32_WCE
   288 	wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
   289 	wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   290 #else
   291 	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
   292 	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
   293 #endif
   294 
   295 	newfp = freopen(stderrPath, TEXT("w"), stderr);
   296 #ifndef _WIN32_WCE
   297 	if ( newfp == NULL ) {	/* This happens on NT */
   298 #if !defined(stderr)
   299 		stderr = fopen(stderrPath, TEXT("w"));
   300 #else
   301 		newfp = fopen(stderrPath, TEXT("w"));
   302 		if ( newfp ) {
   303 			*stderr = *newfp;
   304 		}
   305 #endif
   306 	}
   307 #endif /* _WIN32_WCE */
   308 
   309 	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   310 	setbuf(stderr, NULL);			/* No buffering */
   311 #endif /* !NO_STDIO_REDIRECT */
   312 
   313 #ifdef _WIN32_WCE
   314 	nLen = wcslen(szCmdLine)+128+1;
   315 	bufp = SDL_stack_alloc(wchar_t, nLen*2);
   316 	wcscpy (bufp, TEXT("\""));
   317 	GetModuleFileName(NULL, bufp+1, 128-3);
   318 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   319 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   320 	nLen = wcslen(bufp)+1;
   321 	cmdline = SDL_stack_alloc(char, nLen);
   322 	if ( cmdline == NULL ) {
   323 		return OutOfMemory();
   324 	}
   325 	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   326 #else
   327 	/* Grab the command line */
   328 	bufp = GetCommandLine();
   329 	nLen = SDL_strlen(bufp)+1;
   330 	cmdline = SDL_stack_alloc(char, nLen);
   331 	if ( cmdline == NULL ) {
   332 		return OutOfMemory();
   333 	}
   334 	SDL_strlcpy(cmdline, bufp, nLen);
   335 #endif
   336 
   337 	/* Parse it into argv and argc */
   338 	argc = ParseCommandLine(cmdline, NULL);
   339 	argv = SDL_stack_alloc(char*, argc+1);
   340 	if ( argv == NULL ) {
   341 		return OutOfMemory();
   342 	}
   343 	ParseCommandLine(cmdline, argv);
   344 
   345 	/* Run the main program (after a little SDL initialization) */
   346 	console_main(argc, argv);
   347 
   348 	/* Hush little compiler, don't you cry... */
   349 	return 0;
   350 }