src/main/win32/SDL_main.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Oct 2002 05:44:09 +0000
changeset 505 6b34c9dcf74c
parent 504 8c4a35e3c507
child 545 8406511f850e
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@0
     2
    SDL_main.c, placed in the public domain by Sam Lantinga  4/13/98
slouken@0
     3
slouken@0
     4
    The WinMain function -- calls your program's main() function 
slouken@0
     5
*/
slouken@0
     6
slouken@0
     7
#include <stdio.h>
slouken@0
     8
#include <string.h>
slouken@0
     9
#include <ctype.h>
slouken@0
    10
#include <stdlib.h>
slouken@0
    11
slouken@0
    12
#include <windows.h>
slouken@0
    13
#include <malloc.h>		/* For _alloca() */
slouken@0
    14
slouken@0
    15
/* Include the SDL main definition header */
slouken@0
    16
#include "SDL.h"
slouken@0
    17
#include "SDL_main.h"
slouken@0
    18
#ifdef main
slouken@112
    19
#ifndef _WIN32_WCE_EMULATION
slouken@0
    20
#undef main
slouken@0
    21
#endif
slouken@112
    22
#endif
slouken@0
    23
slouken@36
    24
/* Do we really not want stdio redirection with Windows CE? */
slouken@36
    25
#ifdef _WIN32_WCE
slouken@36
    26
#define NO_STDIO_REDIRECT
slouken@36
    27
#endif
slouken@36
    28
slouken@0
    29
/* The standard output files */
slouken@36
    30
#define STDOUT_FILE	TEXT("stdout.txt")
slouken@36
    31
#define STDERR_FILE	TEXT("stderr.txt")
slouken@36
    32
slouken@112
    33
#if defined(_WIN32_WCE) && _WIN32_WCE < 300
slouken@36
    34
/* seems to be undefined in Win CE although in online help */
slouken@36
    35
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
slouken@36
    36
slouken@36
    37
/* seems to be undefined in Win CE although in online help */
slouken@36
    38
char *strrchr(char *str, int c)
slouken@36
    39
{
slouken@36
    40
	char *p;
slouken@36
    41
slouken@36
    42
	/* Skip to the end of the string */
slouken@36
    43
	p=str;
slouken@36
    44
	while (*p)
slouken@36
    45
		p++;
slouken@36
    46
slouken@36
    47
	/* Look for the given character */
slouken@36
    48
	while ( (p >= str) && (*p != (CHAR)c) )
slouken@36
    49
		p--;
slouken@36
    50
slouken@36
    51
	/* Return NULL if character not found */
slouken@36
    52
	if ( p < str ) {
slouken@36
    53
		p = NULL;
slouken@36
    54
	}
slouken@36
    55
	return p;
slouken@36
    56
}
slouken@112
    57
#endif /* _WIN32_WCE < 300 */
slouken@0
    58
slouken@0
    59
/* Parse a command line buffer into arguments */
slouken@0
    60
static int ParseCommandLine(char *cmdline, char **argv)
slouken@0
    61
{
slouken@0
    62
	char *bufp;
slouken@0
    63
	int argc;
slouken@0
    64
slouken@0
    65
	argc = 0;
slouken@0
    66
	for ( bufp = cmdline; *bufp; ) {
slouken@0
    67
		/* Skip leading whitespace */
slouken@0
    68
		while ( isspace(*bufp) ) {
slouken@0
    69
			++bufp;
slouken@0
    70
		}
slouken@0
    71
		/* Skip over argument */
slouken@0
    72
		if ( *bufp == '"' ) {
slouken@0
    73
			++bufp;
slouken@0
    74
			if ( *bufp ) {
slouken@0
    75
				if ( argv ) {
slouken@0
    76
					argv[argc] = bufp;
slouken@0
    77
				}
slouken@0
    78
				++argc;
slouken@0
    79
			}
slouken@0
    80
			/* Skip over word */
slouken@0
    81
			while ( *bufp && (*bufp != '"') ) {
slouken@0
    82
				++bufp;
slouken@0
    83
			}
slouken@0
    84
		} else {
slouken@0
    85
			if ( *bufp ) {
slouken@0
    86
				if ( argv ) {
slouken@0
    87
					argv[argc] = bufp;
slouken@0
    88
				}
slouken@0
    89
				++argc;
slouken@0
    90
			}
slouken@0
    91
			/* Skip over word */
slouken@0
    92
			while ( *bufp && ! isspace(*bufp) ) {
slouken@0
    93
				++bufp;
slouken@0
    94
			}
slouken@0
    95
		}
slouken@0
    96
		if ( *bufp ) {
slouken@0
    97
			if ( argv ) {
slouken@0
    98
				*bufp = '\0';
slouken@0
    99
			}
slouken@0
   100
			++bufp;
slouken@0
   101
		}
slouken@0
   102
	}
slouken@0
   103
	if ( argv ) {
slouken@0
   104
		argv[argc] = NULL;
slouken@0
   105
	}
slouken@0
   106
	return(argc);
slouken@0
   107
}
slouken@0
   108
slouken@0
   109
/* Show an error message */
slouken@0
   110
static void ShowError(const char *title, const char *message)
slouken@0
   111
{
slouken@0
   112
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
slouken@0
   113
#ifdef USE_MESSAGEBOX
slouken@0
   114
	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
slouken@0
   115
#else
slouken@0
   116
	fprintf(stderr, "%s: %s\n", title, message);
slouken@0
   117
#endif
slouken@0
   118
}
slouken@0
   119
slouken@0
   120
/* Pop up an out of memory message, returns to Windows */
slouken@0
   121
static BOOL OutOfMemory(void)
slouken@0
   122
{
slouken@0
   123
	ShowError("Fatal Error", "Out of memory - aborting");
slouken@0
   124
	return FALSE;
slouken@0
   125
}
slouken@0
   126
slouken@0
   127
/* Remove the output files if there was no output written */
slouken@36
   128
static void __cdecl cleanup_output(void)
slouken@0
   129
{
slouken@36
   130
#ifndef NO_STDIO_REDIRECT
slouken@0
   131
	FILE *file;
slouken@0
   132
	int empty;
slouken@36
   133
#endif
slouken@0
   134
slouken@0
   135
	/* Flush the output in case anything is queued */
slouken@0
   136
	fclose(stdout);
slouken@0
   137
	fclose(stderr);
slouken@0
   138
slouken@36
   139
#ifndef NO_STDIO_REDIRECT
slouken@0
   140
	/* See if the files have any output in them */
slouken@0
   141
	file = fopen(STDOUT_FILE, "rb");
slouken@0
   142
	if ( file ) {
slouken@0
   143
		empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@0
   144
		fclose(file);
slouken@0
   145
		if ( empty ) {
slouken@0
   146
			remove(STDOUT_FILE);
slouken@0
   147
		}
slouken@0
   148
	}
slouken@0
   149
	file = fopen(STDERR_FILE, "rb");
slouken@0
   150
	if ( file ) {
slouken@0
   151
		empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@0
   152
		fclose(file);
slouken@0
   153
		if ( empty ) {
slouken@0
   154
			remove(STDERR_FILE);
slouken@0
   155
		}
slouken@0
   156
	}
slouken@36
   157
#endif
slouken@0
   158
}
slouken@0
   159
slouken@36
   160
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
slouken@36
   161
/* The VC++ compiler needs main defined */
slouken@0
   162
#define console_main main
slouken@0
   163
#endif
slouken@0
   164
slouken@0
   165
/* This is where execution begins [console apps] */
slouken@0
   166
int console_main(int argc, char *argv[])
slouken@0
   167
{
slouken@0
   168
	int n;
slouken@0
   169
	char *bufp, *appname;
slouken@0
   170
slouken@0
   171
	/* Get the class name from argv[0] */
slouken@0
   172
	appname = argv[0];
slouken@0
   173
	if ( (bufp=strrchr(argv[0], '\\')) != NULL ) {
slouken@0
   174
		appname = bufp+1;
slouken@0
   175
	} else
slouken@0
   176
	if ( (bufp=strrchr(argv[0], '/')) != NULL ) {
slouken@0
   177
		appname = bufp+1;
slouken@0
   178
	}
slouken@0
   179
slouken@0
   180
	if ( (bufp=strrchr(appname, '.')) == NULL )
slouken@0
   181
		n = strlen(appname);
slouken@0
   182
	else
slouken@0
   183
		n = (bufp-appname);
slouken@0
   184
slouken@0
   185
	bufp = (char *)alloca(n+1);
slouken@0
   186
	if ( bufp == NULL ) {
slouken@0
   187
		return OutOfMemory();
slouken@0
   188
	}
slouken@0
   189
	strncpy(bufp, appname, n);
slouken@0
   190
	bufp[n] = '\0';
slouken@0
   191
	appname = bufp;
slouken@0
   192
slouken@0
   193
	/* Load SDL dynamic link library */
slouken@0
   194
	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
slouken@0
   195
		ShowError("WinMain() error", SDL_GetError());
slouken@0
   196
		return(FALSE);
slouken@0
   197
	}
slouken@0
   198
	atexit(cleanup_output);
slouken@0
   199
	atexit(SDL_Quit);
slouken@0
   200
slouken@15
   201
#ifndef DISABLE_VIDEO
slouken@149
   202
#if 0
slouken@145
   203
	/* Create and register our class *
slouken@149
   204
	   DJM: If we do this here, the user nevers gets a chance to
slouken@149
   205
	   putenv(SDL_WINDOWID).  This is already called later by
slouken@149
   206
	   the (DIB|DX5)_CreateWindow function, so it should be
slouken@149
   207
	   safe to comment it out here.
slouken@0
   208
	if ( SDL_RegisterApp(appname, CS_BYTEALIGNCLIENT, 
slouken@0
   209
	                     GetModuleHandle(NULL)) < 0 ) {
slouken@0
   210
		ShowError("WinMain() error", SDL_GetError());
slouken@0
   211
		exit(1);
slouken@145
   212
	}*/
slouken@149
   213
#else
slouken@149
   214
	/* Sam:
slouken@149
   215
	   We still need to pass in the application handle so that
slouken@149
   216
	   DirectInput will initialize properly when SDL_RegisterApp()
slouken@149
   217
	   is called later in the video initialization.
slouken@149
   218
	 */
slouken@149
   219
	SDL_SetModuleHandle(GetModuleHandle(NULL));
slouken@149
   220
#endif /* 0 */
slouken@15
   221
#endif /* !DISABLE_VIDEO */
slouken@15
   222
slouken@15
   223
	/* Run the application main() code */
slouken@0
   224
	SDL_main(argc, argv);
slouken@0
   225
slouken@0
   226
	/* Exit cleanly, calling atexit() functions */
slouken@0
   227
	exit(0);
slouken@453
   228
slouken@453
   229
	/* Hush little compiler, don't you cry... */
slouken@453
   230
	return(0);
slouken@0
   231
}
slouken@0
   232
slouken@0
   233
/* This is where execution begins [windowed apps] */
slouken@36
   234
#ifdef _WIN32_WCE
slouken@36
   235
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
slouken@36
   236
#else
slouken@0
   237
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
slouken@36
   238
#endif
slouken@0
   239
{
slouken@0
   240
	HINSTANCE handle;
slouken@0
   241
	char **argv;
slouken@0
   242
	int argc;
slouken@0
   243
	char *cmdline;
slouken@36
   244
#ifdef _WIN32_WCE
slouken@36
   245
	wchar_t *bufp;
slouken@36
   246
	int nLen;
slouken@36
   247
#else
slouken@0
   248
	char *bufp;
slouken@36
   249
#endif
slouken@0
   250
#ifndef NO_STDIO_REDIRECT
slouken@0
   251
	FILE *newfp;
slouken@0
   252
#endif
slouken@0
   253
slouken@0
   254
	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
slouken@0
   255
	   keep them open.  This is a hack.. hopefully it will be fixed 
slouken@0
   256
	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
slouken@0
   257
	 */
slouken@36
   258
	handle = LoadLibrary(TEXT("DDRAW.DLL"));
slouken@0
   259
	if ( handle != NULL ) {
slouken@0
   260
		FreeLibrary(handle);
slouken@0
   261
	}
slouken@0
   262
slouken@0
   263
#ifndef NO_STDIO_REDIRECT
slouken@0
   264
	/* Redirect standard input and standard output */
slouken@0
   265
	newfp = freopen(STDOUT_FILE, "w", stdout);
slouken@0
   266
	if ( newfp == NULL ) {	/* This happens on NT */
slouken@0
   267
#if !defined(stdout)
slouken@0
   268
		stdout = fopen(STDOUT_FILE, "w");
slouken@0
   269
#else
slouken@0
   270
		newfp = fopen(STDOUT_FILE, "w");
slouken@0
   271
		if ( newfp ) {
slouken@0
   272
			*stdout = *newfp;
slouken@0
   273
		}
slouken@0
   274
#endif
slouken@0
   275
	}
slouken@0
   276
	newfp = freopen(STDERR_FILE, "w", stderr);
slouken@0
   277
	if ( newfp == NULL ) {	/* This happens on NT */
slouken@0
   278
#if !defined(stderr)
slouken@0
   279
		stderr = fopen(STDERR_FILE, "w");
slouken@0
   280
#else
slouken@0
   281
		newfp = fopen(STDERR_FILE, "w");
slouken@0
   282
		if ( newfp ) {
slouken@0
   283
			*stderr = *newfp;
slouken@0
   284
		}
slouken@0
   285
#endif
slouken@0
   286
	}
slouken@0
   287
	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
slouken@0
   288
	setbuf(stderr, NULL);			/* No buffering */
slouken@0
   289
#endif /* !NO_STDIO_REDIRECT */
slouken@0
   290
slouken@36
   291
#ifdef _WIN32_WCE
slouken@36
   292
	nLen = wcslen(szCmdLine)+128+1;
slouken@36
   293
	bufp = (wchar_t *)alloca(nLen*2);
slouken@505
   294
	wcscpy (bufp, TEXT("\""));
slouken@504
   295
	GetModuleFileName(NULL, bufp+1, 128-3);
slouken@505
   296
	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
slouken@112
   297
	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
slouken@36
   298
	nLen = wcslen(bufp)+1;
slouken@36
   299
	cmdline = (char *)alloca(nLen);
slouken@36
   300
	if ( cmdline == NULL ) {
slouken@36
   301
		return OutOfMemory();
slouken@36
   302
	}
slouken@36
   303
	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
slouken@36
   304
#else
slouken@0
   305
	/* Grab the command line (use alloca() on Windows) */
slouken@0
   306
	bufp = GetCommandLine();
slouken@0
   307
	cmdline = (char *)alloca(strlen(bufp)+1);
slouken@0
   308
	if ( cmdline == NULL ) {
slouken@0
   309
		return OutOfMemory();
slouken@0
   310
	}
slouken@0
   311
	strcpy(cmdline, bufp);
slouken@36
   312
#endif
slouken@0
   313
slouken@0
   314
	/* Parse it into argv and argc */
slouken@0
   315
	argc = ParseCommandLine(cmdline, NULL);
slouken@0
   316
	argv = (char **)alloca((argc+1)*(sizeof *argv));
slouken@0
   317
	if ( argv == NULL ) {
slouken@0
   318
		return OutOfMemory();
slouken@0
   319
	}
slouken@0
   320
	ParseCommandLine(cmdline, argv);
slouken@0
   321
slouken@0
   322
	/* Run the main program (after a little SDL initialization) */
slouken@0
   323
	return(console_main(argc, argv));
slouken@0
   324
}