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