src/main/win32/SDL_win32_main.c
changeset 754 623b453a3219
child 766 ed57c876700d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/win32/SDL_win32_main.c	Sun Dec 14 06:25:53 2003 +0000
     1.3 @@ -0,0 +1,361 @@
     1.4 +/*
     1.5 +    SDL_main.c, placed in the public domain by Sam Lantinga  4/13/98
     1.6 +
     1.7 +    The WinMain function -- calls your program's main() function
     1.8 +*/
     1.9 +
    1.10 +#include <stdio.h>
    1.11 +#include <string.h>
    1.12 +#include <ctype.h>
    1.13 +#include <stdlib.h>
    1.14 +
    1.15 +#include <windows.h>
    1.16 +#include <malloc.h>			/* For _alloca() */
    1.17 +
    1.18 +#ifdef _WIN32_WCE
    1.19 +# define DIR_SEPERATOR TEXT("\\")
    1.20 +# define _getcwd(str,len)	wcscpy(str,DIR_SEPERATOR);
    1.21 +# define setbuf(x)
    1.22 +# define setvbuf(x)
    1.23 +# define fopen		_wfopen
    1.24 +# define freopen	_wfreopen
    1.25 +# define remove(x)	DeleteFile(x)
    1.26 +# define strcat		wcscat
    1.27 +#else
    1.28 +# define DIR_SEPERATOR TEXT("/")
    1.29 +# include <direct.h>
    1.30 +#endif
    1.31 +
    1.32 +/* Include the SDL main definition header */
    1.33 +#include "SDL.h"
    1.34 +#include "SDL_main.h"
    1.35 +
    1.36 +#ifdef main
    1.37 +# ifndef _WIN32_WCE_EMULATION
    1.38 +#  undef main
    1.39 +# endif /* _WIN32_WCE_EMULATION */
    1.40 +#endif /* main */
    1.41 +
    1.42 +/* The standard output files */
    1.43 +#define STDOUT_FILE	TEXT("stdout.txt")
    1.44 +#define STDERR_FILE	TEXT("stderr.txt")
    1.45 +
    1.46 +#ifndef NO_STDIO_REDIRECT
    1.47 +# ifdef _WIN32_WCE
    1.48 +  static wchar_t stdoutPath[MAX_PATH];
    1.49 +  static wchar_t stderrPath[MAX_PATH];
    1.50 +# else
    1.51 +  static char stdoutPath[MAX_PATH];
    1.52 +  static char stderrPath[MAX_PATH];
    1.53 +# endif
    1.54 +#endif
    1.55 +
    1.56 +#if defined(_WIN32_WCE) && _WIN32_WCE < 300
    1.57 +/* seems to be undefined in Win CE although in online help */
    1.58 +#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
    1.59 +
    1.60 +/* seems to be undefined in Win CE although in online help */
    1.61 +char *strrchr(char *str, int c)
    1.62 +{
    1.63 +	char *p;
    1.64 +
    1.65 +	/* Skip to the end of the string */
    1.66 +	p=str;
    1.67 +	while (*p)
    1.68 +		p++;
    1.69 +
    1.70 +	/* Look for the given character */
    1.71 +	while ( (p >= str) && (*p != (CHAR)c) )
    1.72 +		p--;
    1.73 +
    1.74 +	/* Return NULL if character not found */
    1.75 +	if ( p < str ) {
    1.76 +		p = NULL;
    1.77 +	}
    1.78 +	return p;
    1.79 +}
    1.80 +#endif /* _WIN32_WCE < 300 */
    1.81 +
    1.82 +/* Parse a command line buffer into arguments */
    1.83 +static int ParseCommandLine(char *cmdline, char **argv)
    1.84 +{
    1.85 +	char *bufp;
    1.86 +	int argc;
    1.87 +
    1.88 +	argc = 0;
    1.89 +	for ( bufp = cmdline; *bufp; ) {
    1.90 +		/* Skip leading whitespace */
    1.91 +		while ( isspace(*bufp) ) {
    1.92 +			++bufp;
    1.93 +		}
    1.94 +		/* Skip over argument */
    1.95 +		if ( *bufp == '"' ) {
    1.96 +			++bufp;
    1.97 +			if ( *bufp ) {
    1.98 +				if ( argv ) {
    1.99 +					argv[argc] = bufp;
   1.100 +				}
   1.101 +				++argc;
   1.102 +			}
   1.103 +			/* Skip over word */
   1.104 +			while ( *bufp && (*bufp != '"') ) {
   1.105 +				++bufp;
   1.106 +			}
   1.107 +		} else {
   1.108 +			if ( *bufp ) {
   1.109 +				if ( argv ) {
   1.110 +					argv[argc] = bufp;
   1.111 +				}
   1.112 +				++argc;
   1.113 +			}
   1.114 +			/* Skip over word */
   1.115 +			while ( *bufp && ! isspace(*bufp) ) {
   1.116 +				++bufp;
   1.117 +			}
   1.118 +		}
   1.119 +		if ( *bufp ) {
   1.120 +			if ( argv ) {
   1.121 +				*bufp = '\0';
   1.122 +			}
   1.123 +			++bufp;
   1.124 +		}
   1.125 +	}
   1.126 +	if ( argv ) {
   1.127 +		argv[argc] = NULL;
   1.128 +	}
   1.129 +	return(argc);
   1.130 +}
   1.131 +
   1.132 +/* Show an error message */
   1.133 +static void ShowError(const char *title, const char *message)
   1.134 +{
   1.135 +/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
   1.136 +#ifdef USE_MESSAGEBOX
   1.137 +	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
   1.138 +#else
   1.139 +	fprintf(stderr, "%s: %s\n", title, message);
   1.140 +#endif
   1.141 +}
   1.142 +
   1.143 +/* Pop up an out of memory message, returns to Windows */
   1.144 +static BOOL OutOfMemory(void)
   1.145 +{
   1.146 +	ShowError("Fatal Error", "Out of memory - aborting");
   1.147 +	return FALSE;
   1.148 +}
   1.149 +
   1.150 +/* Remove the output files if there was no output written */
   1.151 +static void __cdecl cleanup_output(void)
   1.152 +{
   1.153 +#ifndef NO_STDIO_REDIRECT
   1.154 +	FILE *file;
   1.155 +	int empty;
   1.156 +#endif
   1.157 +
   1.158 +	/* Flush the output in case anything is queued */
   1.159 +	fclose(stdout);
   1.160 +	fclose(stderr);
   1.161 +
   1.162 +#ifndef NO_STDIO_REDIRECT
   1.163 +	/* See if the files have any output in them */
   1.164 +	if ( stdoutPath[0] ) {
   1.165 +		file = fopen(stdoutPath, TEXT("rb"));
   1.166 +		if ( file ) {
   1.167 +			empty = (fgetc(file) == EOF) ? 1 : 0;
   1.168 +			fclose(file);
   1.169 +			if ( empty ) {
   1.170 +				remove(stdoutPath);
   1.171 +			}
   1.172 +		}
   1.173 +	}
   1.174 +	if ( stderrPath[0] ) {
   1.175 +		file = fopen(stderrPath, TEXT("rb"));
   1.176 +		if ( file ) {
   1.177 +			empty = (fgetc(file) == EOF) ? 1 : 0;
   1.178 +			fclose(file);
   1.179 +			if ( empty ) {
   1.180 +				remove(stderrPath);
   1.181 +			}
   1.182 +		}
   1.183 +	}
   1.184 +#endif
   1.185 +}
   1.186 +
   1.187 +#if defined(_MSC_VER) && !defined(_WIN32_WCE)
   1.188 +/* The VC++ compiler needs main defined */
   1.189 +#define console_main main
   1.190 +#endif
   1.191 +
   1.192 +/* This is where execution begins [console apps] */
   1.193 +int console_main(int argc, char *argv[])
   1.194 +{
   1.195 +	int n;
   1.196 +	char *bufp, *appname;
   1.197 +
   1.198 +	/* Get the class name from argv[0] */
   1.199 +	appname = argv[0];
   1.200 +	if ( (bufp=strrchr(argv[0], '\\')) != NULL ) {
   1.201 +		appname = bufp+1;
   1.202 +	} else
   1.203 +	if ( (bufp=strrchr(argv[0], '/')) != NULL ) {
   1.204 +		appname = bufp+1;
   1.205 +	}
   1.206 +
   1.207 +	if ( (bufp=strrchr(appname, '.')) == NULL )
   1.208 +		n = strlen(appname);
   1.209 +	else
   1.210 +		n = (bufp-appname);
   1.211 +
   1.212 +	bufp = (char *)alloca(n+1);
   1.213 +	if ( bufp == NULL ) {
   1.214 +		return OutOfMemory();
   1.215 +	}
   1.216 +	strncpy(bufp, appname, n);
   1.217 +	bufp[n] = '\0';
   1.218 +	appname = bufp;
   1.219 +
   1.220 +	/* Load SDL dynamic link library */
   1.221 +	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
   1.222 +		ShowError("WinMain() error", SDL_GetError());
   1.223 +		return(FALSE);
   1.224 +	}
   1.225 +	atexit(cleanup_output);
   1.226 +	atexit(SDL_Quit);
   1.227 +
   1.228 +#ifndef DISABLE_VIDEO
   1.229 +#if 0
   1.230 +	/* Create and register our class *
   1.231 +	   DJM: If we do this here, the user nevers gets a chance to
   1.232 +	   putenv(SDL_WINDOWID).  This is already called later by
   1.233 +	   the (DIB|DX5)_CreateWindow function, so it should be
   1.234 +	   safe to comment it out here.
   1.235 +	if ( SDL_RegisterApp(appname, CS_BYTEALIGNCLIENT, 
   1.236 +	                     GetModuleHandle(NULL)) < 0 ) {
   1.237 +		ShowError("WinMain() error", SDL_GetError());
   1.238 +		exit(1);
   1.239 +	}*/
   1.240 +#else
   1.241 +	/* Sam:
   1.242 +	   We still need to pass in the application handle so that
   1.243 +	   DirectInput will initialize properly when SDL_RegisterApp()
   1.244 +	   is called later in the video initialization.
   1.245 +	 */
   1.246 +	SDL_SetModuleHandle(GetModuleHandle(NULL));
   1.247 +#endif /* 0 */
   1.248 +#endif /* !DISABLE_VIDEO */
   1.249 +
   1.250 +	/* Run the application main() code */
   1.251 +	SDL_main(argc, argv);
   1.252 +
   1.253 +	/* Exit cleanly, calling atexit() functions */
   1.254 +	exit(0);
   1.255 +
   1.256 +	/* Hush little compiler, don't you cry... */
   1.257 +	return(0);
   1.258 +}
   1.259 +
   1.260 +/* This is where execution begins [windowed apps] */
   1.261 +#ifdef _WIN32_WCE
   1.262 +int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
   1.263 +#else
   1.264 +int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   1.265 +#endif
   1.266 +{
   1.267 +	HINSTANCE handle;
   1.268 +	char **argv;
   1.269 +	int argc;
   1.270 +	char *cmdline;
   1.271 +#ifdef _WIN32_WCE
   1.272 +	wchar_t *bufp;
   1.273 +	int nLen;
   1.274 +#else
   1.275 +	char *bufp;
   1.276 +#endif
   1.277 +#ifndef NO_STDIO_REDIRECT
   1.278 +	FILE *newfp;
   1.279 +#endif
   1.280 +
   1.281 +	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   1.282 +	   keep them open.  This is a hack.. hopefully it will be fixed 
   1.283 +	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   1.284 +	 */
   1.285 +	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   1.286 +	if ( handle != NULL ) {
   1.287 +		FreeLibrary(handle);
   1.288 +	}
   1.289 +
   1.290 +#ifndef NO_STDIO_REDIRECT
   1.291 +	_getcwd( stdoutPath, sizeof( stdoutPath ) );
   1.292 +	strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE );
   1.293 +    
   1.294 +	/* Redirect standard input and standard output */
   1.295 +	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   1.296 +
   1.297 +#ifndef _WIN32_WCE
   1.298 +	if ( newfp == NULL ) {	/* This happens on NT */
   1.299 +#if !defined(stdout)
   1.300 +		stdout = fopen(stdoutPath, TEXT("w"));
   1.301 +#else
   1.302 +		newfp = fopen(stdoutPath, TEXT("w"));
   1.303 +		if ( newfp ) {
   1.304 +			*stdout = *newfp;
   1.305 +		}
   1.306 +#endif
   1.307 +	}
   1.308 +#endif /* _WIN32_WCE */
   1.309 +
   1.310 +	_getcwd( stderrPath, sizeof( stderrPath ) );
   1.311 +	strcat( stderrPath, DIR_SEPERATOR STDERR_FILE );
   1.312 +
   1.313 +	newfp = freopen(stderrPath, TEXT("w"), stderr);
   1.314 +#ifndef _WIN32_WCE
   1.315 +	if ( newfp == NULL ) {	/* This happens on NT */
   1.316 +#if !defined(stderr)
   1.317 +		stderr = fopen(stderrPath, TEXT("w"));
   1.318 +#else
   1.319 +		newfp = fopen(stderrPath, TEXT("w"));
   1.320 +		if ( newfp ) {
   1.321 +			*stderr = *newfp;
   1.322 +		}
   1.323 +#endif
   1.324 +	}
   1.325 +#endif /* _WIN32_WCE */
   1.326 +
   1.327 +	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   1.328 +	setbuf(stderr, NULL);			/* No buffering */
   1.329 +#endif /* !NO_STDIO_REDIRECT */
   1.330 +
   1.331 +#ifdef _WIN32_WCE
   1.332 +	nLen = wcslen(szCmdLine)+128+1;
   1.333 +	bufp = (wchar_t *)alloca(nLen*2);
   1.334 +	wcscpy (bufp, TEXT("\""));
   1.335 +	GetModuleFileName(NULL, bufp+1, 128-3);
   1.336 +	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   1.337 +	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   1.338 +	nLen = wcslen(bufp)+1;
   1.339 +	cmdline = (char *)alloca(nLen);
   1.340 +	if ( cmdline == NULL ) {
   1.341 +		return OutOfMemory();
   1.342 +	}
   1.343 +	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   1.344 +#else
   1.345 +	/* Grab the command line (use alloca() on Windows) */
   1.346 +	bufp = GetCommandLine();
   1.347 +	cmdline = (char *)alloca(strlen(bufp)+1);
   1.348 +	if ( cmdline == NULL ) {
   1.349 +		return OutOfMemory();
   1.350 +	}
   1.351 +	strcpy(cmdline, bufp);
   1.352 +#endif
   1.353 +
   1.354 +	/* Parse it into argv and argc */
   1.355 +	argc = ParseCommandLine(cmdline, NULL);
   1.356 +	argv = (char **)alloca((argc+1)*(sizeof *argv));
   1.357 +	if ( argv == NULL ) {
   1.358 +		return OutOfMemory();
   1.359 +	}
   1.360 +	ParseCommandLine(cmdline, argv);
   1.361 +
   1.362 +	/* Run the main program (after a little SDL initialization) */
   1.363 +	return(console_main(argc, argv));
   1.364 +}