src/main/win32/SDL_win32_main.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 15:48:44 +0000
changeset 766 ed57c876700d
parent 754 623b453a3219
child 1286 d7bca8c8161e
permissions -rw-r--r--
Date: Wed, 26 Nov 2003 01:52:02 +0800
From: "Andy Pfiffer"
Subject: [SDL] patch: PocketPC 2000 diffs for release_1_2_6

I was making a backup of my current workspace of SDL for PocketPC,
and thought I would send out my diffs.

Apologies in advance if the patch has become whitespace mangled.

These diffs are relative to CVS tag release_1_2_6, and contain (I think)
all changes that have been mentioned on the mailing list in the last
few months.

I only have PocketPC 2000 & an ARM-based device (iPaq 3635), and I
only use Embedded Visual Tools 3.0, so I can't say for sure if it
breaks other PocketPC 200[023] builds.
     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 #ifdef _WIN32_WCE
   270 	wchar_t *bufp;
   271 	int nLen;
   272 #else
   273 	char *bufp;
   274 #endif
   275 #ifndef NO_STDIO_REDIRECT
   276 	FILE *newfp;
   277 #endif
   278 
   279 	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   280 	   keep them open.  This is a hack.. hopefully it will be fixed 
   281 	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   282 	 */
   283 	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   284 	if ( handle != NULL ) {
   285 		FreeLibrary(handle);
   286 	}
   287 
   288 #ifndef NO_STDIO_REDIRECT
   289 	_getcwd( stdoutPath, sizeof( stdoutPath ) );
   290 	strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE );
   291     
   292 	/* Redirect standard input and standard output */
   293 	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   294 
   295 #ifndef _WIN32_WCE
   296 	if ( newfp == NULL ) {	/* This happens on NT */
   297 #if !defined(stdout)
   298 		stdout = fopen(stdoutPath, TEXT("w"));
   299 #else
   300 		newfp = fopen(stdoutPath, TEXT("w"));
   301 		if ( newfp ) {
   302 			*stdout = *newfp;
   303 		}
   304 #endif
   305 	}
   306 #endif /* _WIN32_WCE */
   307 
   308 	_getcwd( stderrPath, sizeof( stderrPath ) );
   309 	strcat( stderrPath, DIR_SEPERATOR STDERR_FILE );
   310 
   311 	newfp = freopen(stderrPath, TEXT("w"), stderr);
   312 #ifndef _WIN32_WCE
   313 	if ( newfp == NULL ) {	/* This happens on NT */
   314 #if !defined(stderr)
   315 		stderr = fopen(stderrPath, TEXT("w"));
   316 #else
   317 		newfp = fopen(stderrPath, TEXT("w"));
   318 		if ( newfp ) {
   319 			*stderr = *newfp;
   320 		}
   321 #endif
   322 	}
   323 #endif /* _WIN32_WCE */
   324 
   325 	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   326 	setbuf(stderr, NULL);			/* No buffering */
   327 #endif /* !NO_STDIO_REDIRECT */
   328 
   329 #ifdef _WIN32_WCE
   330 	nLen = wcslen(szCmdLine)+128+1;
   331 	bufp = (wchar_t *)alloca(nLen*2);
   332 	wcscpy (bufp, TEXT("\""));
   333 	GetModuleFileName(NULL, bufp+1, 128-3);
   334 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   335 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   336 	nLen = wcslen(bufp)+1;
   337 	cmdline = (char *)alloca(nLen);
   338 	if ( cmdline == NULL ) {
   339 		return OutOfMemory();
   340 	}
   341 	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   342 #else
   343 	/* Grab the command line (use alloca() on Windows) */
   344 	bufp = GetCommandLine();
   345 	cmdline = (char *)alloca(strlen(bufp)+1);
   346 	if ( cmdline == NULL ) {
   347 		return OutOfMemory();
   348 	}
   349 	strcpy(cmdline, bufp);
   350 #endif
   351 
   352 	/* Parse it into argv and argc */
   353 	argc = ParseCommandLine(cmdline, NULL);
   354 	argv = (char **)alloca((argc+1)*(sizeof *argv));
   355 	if ( argv == NULL ) {
   356 		return OutOfMemory();
   357 	}
   358 	ParseCommandLine(cmdline, argv);
   359 
   360 	/* Run the main program (after a little SDL initialization) */
   361 	return(console_main(argc, argv));
   362 }