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