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