src/main/win32/SDL_win32_main.c
author Ryan C. Gordon
Sat, 10 Sep 2011 19:37:20 -0400
branchSDL-1.2
changeset 5882 32558685a904
parent 4246 8b8314cc34a6
child 6129 19c7412a6345
permissions -rw-r--r--
Corrected datatype used for LoadLibrary() return value.
slouken@754
     1
/*
slouken@754
     2
    SDL_main.c, placed in the public domain by Sam Lantinga  4/13/98
slouken@754
     3
slouken@754
     4
    The WinMain function -- calls your program's main() function
slouken@754
     5
*/
slouken@754
     6
slouken@754
     7
#include <stdio.h>
slouken@754
     8
#include <stdlib.h>
slouken@754
     9
slouken@1433
    10
#define WIN32_LEAN_AND_MEAN
slouken@1433
    11
#include <windows.h>
slouken@1336
    12
slouken@754
    13
#ifdef _WIN32_WCE
slouken@754
    14
# define DIR_SEPERATOR TEXT("\\")
slouken@766
    15
# undef _getcwd
slouken@766
    16
# define _getcwd(str,len)	wcscpy(str,TEXT(""))
slouken@766
    17
# define setbuf(f,b)
slouken@766
    18
# define setvbuf(w,x,y,z)
slouken@754
    19
# define fopen		_wfopen
slouken@754
    20
# define freopen	_wfreopen
slouken@754
    21
# define remove(x)	DeleteFile(x)
slouken@754
    22
#else
slouken@754
    23
# define DIR_SEPERATOR TEXT("/")
slouken@754
    24
# include <direct.h>
slouken@754
    25
#endif
slouken@754
    26
slouken@754
    27
/* Include the SDL main definition header */
slouken@754
    28
#include "SDL.h"
slouken@754
    29
#include "SDL_main.h"
slouken@754
    30
slouken@754
    31
#ifdef main
slouken@754
    32
# ifndef _WIN32_WCE_EMULATION
slouken@754
    33
#  undef main
slouken@754
    34
# endif /* _WIN32_WCE_EMULATION */
slouken@754
    35
#endif /* main */
slouken@754
    36
slouken@754
    37
/* The standard output files */
slouken@754
    38
#define STDOUT_FILE	TEXT("stdout.txt")
slouken@754
    39
#define STDERR_FILE	TEXT("stderr.txt")
slouken@754
    40
slouken@4246
    41
/* Set a variable to tell if the stdio redirect has been enabled. */
slouken@4246
    42
static int stdioRedirectEnabled = 0;
slouken@4246
    43
slouken@4246
    44
#ifdef _WIN32_WCE
slouken@754
    45
  static wchar_t stdoutPath[MAX_PATH];
slouken@754
    46
  static wchar_t stderrPath[MAX_PATH];
slouken@4246
    47
#else
slouken@754
    48
  static char stdoutPath[MAX_PATH];
slouken@754
    49
  static char stderrPath[MAX_PATH];
slouken@754
    50
#endif
slouken@754
    51
slouken@754
    52
#if defined(_WIN32_WCE) && _WIN32_WCE < 300
slouken@754
    53
/* seems to be undefined in Win CE although in online help */
slouken@754
    54
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
slouken@754
    55
#endif /* _WIN32_WCE < 300 */
slouken@754
    56
slouken@4118
    57
static void UnEscapeQuotes( char *arg )
slouken@4118
    58
{
slouken@4118
    59
	char *last = NULL;
slouken@4118
    60
slouken@4118
    61
	while( *arg ) {
slouken@4118
    62
		if( *arg == '"' && *last == '\\' ) {
slouken@4118
    63
			char *c_curr = arg;
slouken@4118
    64
			char *c_last = last;
slouken@4118
    65
slouken@4118
    66
			while( *c_curr ) {
slouken@4118
    67
				*c_last = *c_curr;
slouken@4118
    68
				c_last = c_curr;
slouken@4118
    69
				c_curr++;
slouken@4118
    70
			}
slouken@4118
    71
			*c_last = '\0';
slouken@4118
    72
		}
slouken@4118
    73
		last = arg;
slouken@4118
    74
		arg++;
slouken@4118
    75
	}
slouken@4118
    76
}
slouken@4118
    77
slouken@754
    78
/* Parse a command line buffer into arguments */
slouken@754
    79
static int ParseCommandLine(char *cmdline, char **argv)
slouken@754
    80
{
slouken@754
    81
	char *bufp;
slouken@4118
    82
	char *lastp = NULL;
slouken@4118
    83
	int argc, last_argc;
slouken@754
    84
slouken@4118
    85
	argc = last_argc = 0;
slouken@754
    86
	for ( bufp = cmdline; *bufp; ) {
slouken@754
    87
		/* Skip leading whitespace */
slouken@754
    88
		while ( isspace(*bufp) ) {
slouken@754
    89
			++bufp;
slouken@754
    90
		}
slouken@754
    91
		/* Skip over argument */
slouken@754
    92
		if ( *bufp == '"' ) {
slouken@754
    93
			++bufp;
slouken@754
    94
			if ( *bufp ) {
slouken@754
    95
				if ( argv ) {
slouken@754
    96
					argv[argc] = bufp;
slouken@754
    97
				}
slouken@754
    98
				++argc;
slouken@754
    99
			}
slouken@754
   100
			/* Skip over word */
slouken@4118
   101
			while ( *bufp && ( *bufp != '"' || *lastp == '\\' ) ) {
slouken@4118
   102
				lastp = bufp;
slouken@754
   103
				++bufp;
slouken@754
   104
			}
slouken@754
   105
		} else {
slouken@754
   106
			if ( *bufp ) {
slouken@754
   107
				if ( argv ) {
slouken@754
   108
					argv[argc] = bufp;
slouken@754
   109
				}
slouken@754
   110
				++argc;
slouken@754
   111
			}
slouken@754
   112
			/* Skip over word */
slouken@754
   113
			while ( *bufp && ! isspace(*bufp) ) {
slouken@754
   114
				++bufp;
slouken@754
   115
			}
slouken@754
   116
		}
slouken@754
   117
		if ( *bufp ) {
slouken@754
   118
			if ( argv ) {
slouken@754
   119
				*bufp = '\0';
slouken@754
   120
			}
slouken@754
   121
			++bufp;
slouken@754
   122
		}
slouken@4118
   123
slouken@4118
   124
		/* Strip out \ from \" sequences */
slouken@4118
   125
		if( argv && last_argc != argc ) {
slouken@4118
   126
			UnEscapeQuotes( argv[last_argc] );	
slouken@4118
   127
		}
slouken@4118
   128
		last_argc = argc;	
slouken@754
   129
	}
slouken@754
   130
	if ( argv ) {
slouken@754
   131
		argv[argc] = NULL;
slouken@754
   132
	}
slouken@754
   133
	return(argc);
slouken@754
   134
}
slouken@754
   135
slouken@754
   136
/* Show an error message */
slouken@754
   137
static void ShowError(const char *title, const char *message)
slouken@754
   138
{
slouken@754
   139
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
slouken@754
   140
#ifdef USE_MESSAGEBOX
slouken@754
   141
	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
slouken@754
   142
#else
slouken@754
   143
	fprintf(stderr, "%s: %s\n", title, message);
slouken@754
   144
#endif
slouken@754
   145
}
slouken@754
   146
slouken@754
   147
/* Pop up an out of memory message, returns to Windows */
slouken@754
   148
static BOOL OutOfMemory(void)
slouken@754
   149
{
slouken@754
   150
	ShowError("Fatal Error", "Out of memory - aborting");
slouken@754
   151
	return FALSE;
slouken@754
   152
}
slouken@754
   153
slouken@1769
   154
/* SDL_Quit() shouldn't be used with atexit() directly because
slouken@1769
   155
   calling conventions may differ... */
slouken@1769
   156
static void cleanup(void)
slouken@1769
   157
{
slouken@1769
   158
	SDL_Quit();
slouken@1769
   159
}
slouken@1769
   160
slouken@754
   161
/* Remove the output files if there was no output written */
slouken@4246
   162
static void cleanup_output(void) {
slouken@754
   163
	FILE *file;
slouken@754
   164
	int empty;
slouken@754
   165
slouken@754
   166
	/* Flush the output in case anything is queued */
slouken@754
   167
	fclose(stdout);
slouken@754
   168
	fclose(stderr);
slouken@754
   169
slouken@4246
   170
	/* Without redirection we're done */
slouken@4246
   171
	if (!stdioRedirectEnabled) {
slouken@4246
   172
		return;
slouken@4246
   173
	}
slouken@4246
   174
slouken@754
   175
	/* See if the files have any output in them */
slouken@754
   176
	if ( stdoutPath[0] ) {
slouken@754
   177
		file = fopen(stdoutPath, TEXT("rb"));
slouken@754
   178
		if ( file ) {
slouken@754
   179
			empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@754
   180
			fclose(file);
slouken@754
   181
			if ( empty ) {
slouken@754
   182
				remove(stdoutPath);
slouken@754
   183
			}
slouken@754
   184
		}
slouken@754
   185
	}
slouken@754
   186
	if ( stderrPath[0] ) {
slouken@754
   187
		file = fopen(stderrPath, TEXT("rb"));
slouken@754
   188
		if ( file ) {
slouken@754
   189
			empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@754
   190
			fclose(file);
slouken@754
   191
			if ( empty ) {
slouken@754
   192
				remove(stderrPath);
slouken@754
   193
			}
slouken@754
   194
		}
slouken@754
   195
	}
slouken@4246
   196
}
slouken@4246
   197
slouken@4246
   198
/* Redirect the output (stdout and stderr) to a file */
slouken@4246
   199
static void redirect_output(void)
slouken@4246
   200
{
slouken@4246
   201
	DWORD pathlen;
slouken@4246
   202
#ifdef _WIN32_WCE
slouken@4246
   203
	wchar_t path[MAX_PATH];
slouken@4246
   204
#else
slouken@4246
   205
	char path[MAX_PATH];
slouken@754
   206
#endif
slouken@4246
   207
	FILE *newfp;
slouken@4246
   208
slouken@4246
   209
	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
slouken@4246
   210
	while ( pathlen > 0 && path[pathlen] != '\\' ) {
slouken@4246
   211
		--pathlen;
slouken@4246
   212
	}
slouken@4246
   213
	path[pathlen] = '\0';
slouken@4246
   214
slouken@4246
   215
#ifdef _WIN32_WCE
slouken@4246
   216
	wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
slouken@4246
   217
	wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
slouken@4246
   218
#else
slouken@4246
   219
	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
slouken@4246
   220
	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
slouken@4246
   221
#endif
slouken@4246
   222
    
slouken@4246
   223
	/* Redirect standard input and standard output */
slouken@4246
   224
	newfp = freopen(stdoutPath, TEXT("w"), stdout);
slouken@4246
   225
slouken@4246
   226
#ifndef _WIN32_WCE
slouken@4246
   227
	if ( newfp == NULL ) {	/* This happens on NT */
slouken@4246
   228
#if !defined(stdout)
slouken@4246
   229
		stdout = fopen(stdoutPath, TEXT("w"));
slouken@4246
   230
#else
slouken@4246
   231
		newfp = fopen(stdoutPath, TEXT("w"));
slouken@4246
   232
		if ( newfp ) {
slouken@4246
   233
			*stdout = *newfp;
slouken@4246
   234
		}
slouken@4246
   235
#endif
slouken@4246
   236
	}
slouken@4246
   237
#endif /* _WIN32_WCE */
slouken@4246
   238
slouken@4246
   239
#ifdef _WIN32_WCE
slouken@4246
   240
	wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
slouken@4246
   241
	wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
slouken@4246
   242
#else
slouken@4246
   243
	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
slouken@4246
   244
	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
slouken@4246
   245
#endif
slouken@4246
   246
slouken@4246
   247
	newfp = freopen(stderrPath, TEXT("w"), stderr);
slouken@4246
   248
#ifndef _WIN32_WCE
slouken@4246
   249
	if ( newfp == NULL ) {	/* This happens on NT */
slouken@4246
   250
#if !defined(stderr)
slouken@4246
   251
		stderr = fopen(stderrPath, TEXT("w"));
slouken@4246
   252
#else
slouken@4246
   253
		newfp = fopen(stderrPath, TEXT("w"));
slouken@4246
   254
		if ( newfp ) {
slouken@4246
   255
			*stderr = *newfp;
slouken@4246
   256
		}
slouken@4246
   257
#endif
slouken@4246
   258
	}
slouken@4246
   259
#endif /* _WIN32_WCE */
slouken@4246
   260
slouken@4246
   261
	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
slouken@4246
   262
	setbuf(stderr, NULL);			/* No buffering */
slouken@4246
   263
	stdioRedirectEnabled = 1;
slouken@754
   264
}
slouken@754
   265
slouken@754
   266
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
slouken@754
   267
/* The VC++ compiler needs main defined */
slouken@754
   268
#define console_main main
slouken@754
   269
#endif
slouken@754
   270
slouken@754
   271
/* This is where execution begins [console apps] */
slouken@754
   272
int console_main(int argc, char *argv[])
slouken@754
   273
{
slouken@1472
   274
	size_t n;
slouken@754
   275
	char *bufp, *appname;
slouken@1423
   276
	int status;
slouken@754
   277
slouken@754
   278
	/* Get the class name from argv[0] */
slouken@754
   279
	appname = argv[0];
slouken@1336
   280
	if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
slouken@754
   281
		appname = bufp+1;
slouken@754
   282
	} else
slouken@1336
   283
	if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
slouken@754
   284
		appname = bufp+1;
slouken@754
   285
	}
slouken@754
   286
slouken@1336
   287
	if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
slouken@1336
   288
		n = SDL_strlen(appname);
slouken@754
   289
	else
slouken@754
   290
		n = (bufp-appname);
slouken@754
   291
slouken@1423
   292
	bufp = SDL_stack_alloc(char, n+1);
slouken@754
   293
	if ( bufp == NULL ) {
slouken@754
   294
		return OutOfMemory();
slouken@754
   295
	}
slouken@1769
   296
	SDL_strlcpy(bufp, appname, n+1);
slouken@754
   297
	appname = bufp;
slouken@754
   298
slouken@754
   299
	/* Load SDL dynamic link library */
slouken@754
   300
	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
slouken@754
   301
		ShowError("WinMain() error", SDL_GetError());
slouken@754
   302
		return(FALSE);
slouken@754
   303
	}
slouken@754
   304
	atexit(cleanup_output);
slouken@1769
   305
	atexit(cleanup);
slouken@754
   306
slouken@754
   307
	/* Sam:
slouken@754
   308
	   We still need to pass in the application handle so that
slouken@754
   309
	   DirectInput will initialize properly when SDL_RegisterApp()
slouken@754
   310
	   is called later in the video initialization.
slouken@754
   311
	 */
slouken@754
   312
	SDL_SetModuleHandle(GetModuleHandle(NULL));
slouken@754
   313
slouken@754
   314
	/* Run the application main() code */
slouken@1423
   315
	status = SDL_main(argc, argv);
slouken@754
   316
slouken@754
   317
	/* Exit cleanly, calling atexit() functions */
slouken@1423
   318
	exit(status);
slouken@754
   319
slouken@754
   320
	/* Hush little compiler, don't you cry... */
slouken@1423
   321
	return 0;
slouken@754
   322
}
slouken@754
   323
slouken@754
   324
/* This is where execution begins [windowed apps] */
slouken@754
   325
#ifdef _WIN32_WCE
slouken@754
   326
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
slouken@754
   327
#else
slouken@754
   328
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
slouken@754
   329
#endif
slouken@754
   330
{
icculus@5882
   331
	HMODULE handle;
slouken@754
   332
	char **argv;
slouken@754
   333
	int argc;
slouken@754
   334
	char *cmdline;
slouken@4246
   335
	char *env_str;
slouken@754
   336
#ifdef _WIN32_WCE
slouken@754
   337
	wchar_t *bufp;
slouken@754
   338
	int nLen;
slouken@754
   339
#else
slouken@754
   340
	char *bufp;
slouken@1379
   341
	size_t nLen;
slouken@754
   342
#endif
slouken@754
   343
slouken@754
   344
	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
slouken@754
   345
	   keep them open.  This is a hack.. hopefully it will be fixed 
slouken@754
   346
	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
slouken@754
   347
	 */
slouken@754
   348
	handle = LoadLibrary(TEXT("DDRAW.DLL"));
slouken@754
   349
	if ( handle != NULL ) {
slouken@754
   350
		FreeLibrary(handle);
slouken@754
   351
	}
slouken@754
   352
slouken@4246
   353
	/* Check for stdio redirect settings and do the redirection */
slouken@4246
   354
	if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) {
slouken@4246
   355
		if (SDL_atoi(env_str)) {
slouken@4246
   356
			redirect_output();
slouken@4246
   357
		}
slouken@1286
   358
	}
slouken@4246
   359
#ifndef NO_STDIO_REDIRECT
slouken@4246
   360
	else {
slouken@4246
   361
		redirect_output();
slouken@4246
   362
	}
slouken@754
   363
#endif
slouken@754
   364
slouken@754
   365
#ifdef _WIN32_WCE
slouken@754
   366
	nLen = wcslen(szCmdLine)+128+1;
slouken@1423
   367
	bufp = SDL_stack_alloc(wchar_t, nLen*2);
slouken@754
   368
	wcscpy (bufp, TEXT("\""));
slouken@754
   369
	GetModuleFileName(NULL, bufp+1, 128-3);
slouken@754
   370
	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
slouken@754
   371
	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
slouken@754
   372
	nLen = wcslen(bufp)+1;
slouken@1465
   373
	cmdline = SDL_stack_alloc(char, nLen);
slouken@754
   374
	if ( cmdline == NULL ) {
slouken@754
   375
		return OutOfMemory();
slouken@754
   376
	}
slouken@754
   377
	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
slouken@754
   378
#else
slouken@1423
   379
	/* Grab the command line */
slouken@754
   380
	bufp = GetCommandLine();
slouken@1379
   381
	nLen = SDL_strlen(bufp)+1;
slouken@1379
   382
	cmdline = SDL_stack_alloc(char, nLen);
slouken@754
   383
	if ( cmdline == NULL ) {
slouken@754
   384
		return OutOfMemory();
slouken@754
   385
	}
slouken@1379
   386
	SDL_strlcpy(cmdline, bufp, nLen);
slouken@754
   387
#endif
slouken@754
   388
slouken@754
   389
	/* Parse it into argv and argc */
slouken@754
   390
	argc = ParseCommandLine(cmdline, NULL);
slouken@1379
   391
	argv = SDL_stack_alloc(char*, argc+1);
slouken@754
   392
	if ( argv == NULL ) {
slouken@754
   393
		return OutOfMemory();
slouken@754
   394
	}
slouken@754
   395
	ParseCommandLine(cmdline, argv);
slouken@754
   396
slouken@754
   397
	/* Run the main program (after a little SDL initialization) */
slouken@1423
   398
	console_main(argc, argv);
slouken@1379
   399
slouken@1423
   400
	/* Hush little compiler, don't you cry... */
slouken@1423
   401
	return 0;
slouken@754
   402
}