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