2001-04-26 16:45:43 +00:00
|
|
|
/*
|
2014-03-13 21:21:26 -07:00
|
|
|
SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2003-01-21 04:15:21 +00:00
|
|
|
The WinMain function -- calls your program's main() function
|
2001-04-26 16:45:43 +00:00
|
|
|
*/
|
2011-10-31 05:56:58 -04:00
|
|
|
#include "SDL_config.h"
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2013-10-17 23:02:29 -07:00
|
|
|
/* Include this so we define UNICODE properly */
|
|
|
|
#include "../../core/windows/SDL_windows.h"
|
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
/* Include the SDL main definition header */
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_main.h"
|
|
|
|
|
2003-01-21 04:15:21 +00:00
|
|
|
#ifdef main
|
|
|
|
# undef main
|
|
|
|
#endif /* main */
|
2001-05-23 23:35:10 +00:00
|
|
|
|
2007-12-29 19:29:20 +00:00
|
|
|
static void
|
|
|
|
UnEscapeQuotes(char *arg)
|
|
|
|
{
|
|
|
|
char *last = NULL;
|
|
|
|
|
|
|
|
while (*arg) {
|
2012-12-01 23:30:53 -08:00
|
|
|
if (*arg == '"' && (last != NULL && *last == '\\')) {
|
2007-12-29 19:29:20 +00:00
|
|
|
char *c_curr = arg;
|
|
|
|
char *c_last = last;
|
|
|
|
|
|
|
|
while (*c_curr) {
|
|
|
|
*c_last = *c_curr;
|
|
|
|
c_last = c_curr;
|
|
|
|
c_curr++;
|
|
|
|
}
|
|
|
|
*c_last = '\0';
|
|
|
|
}
|
|
|
|
last = arg;
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
/* Parse a command line buffer into arguments */
|
2006-07-10 21:04:37 +00:00
|
|
|
static int
|
|
|
|
ParseCommandLine(char *cmdline, char **argv)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
char *bufp;
|
2007-12-29 19:29:20 +00:00
|
|
|
char *lastp = NULL;
|
|
|
|
int argc, last_argc;
|
2006-07-10 21:04:37 +00:00
|
|
|
|
2007-12-29 19:29:20 +00:00
|
|
|
argc = last_argc = 0;
|
2006-07-10 21:04:37 +00:00
|
|
|
for (bufp = cmdline; *bufp;) {
|
|
|
|
/* Skip leading whitespace */
|
2013-07-14 19:53:50 +02:00
|
|
|
while (SDL_isspace(*bufp)) {
|
2006-07-10 21:04:37 +00:00
|
|
|
++bufp;
|
|
|
|
}
|
|
|
|
/* Skip over argument */
|
|
|
|
if (*bufp == '"') {
|
|
|
|
++bufp;
|
|
|
|
if (*bufp) {
|
|
|
|
if (argv) {
|
|
|
|
argv[argc] = bufp;
|
|
|
|
}
|
|
|
|
++argc;
|
|
|
|
}
|
|
|
|
/* Skip over word */
|
2010-07-12 22:08:50 -07:00
|
|
|
lastp = bufp;
|
2007-12-29 19:29:20 +00:00
|
|
|
while (*bufp && (*bufp != '"' || *lastp == '\\')) {
|
|
|
|
lastp = bufp;
|
2006-07-10 21:04:37 +00:00
|
|
|
++bufp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (*bufp) {
|
|
|
|
if (argv) {
|
|
|
|
argv[argc] = bufp;
|
|
|
|
}
|
|
|
|
++argc;
|
|
|
|
}
|
|
|
|
/* Skip over word */
|
2013-07-14 19:53:50 +02:00
|
|
|
while (*bufp && !SDL_isspace(*bufp)) {
|
2006-07-10 21:04:37 +00:00
|
|
|
++bufp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*bufp) {
|
|
|
|
if (argv) {
|
|
|
|
*bufp = '\0';
|
|
|
|
}
|
|
|
|
++bufp;
|
|
|
|
}
|
2007-12-29 19:29:20 +00:00
|
|
|
|
|
|
|
/* Strip out \ from \" sequences */
|
|
|
|
if (argv && last_argc != argc) {
|
|
|
|
UnEscapeQuotes(argv[last_argc]);
|
|
|
|
}
|
|
|
|
last_argc = argc;
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
if (argv) {
|
|
|
|
argv[argc] = NULL;
|
|
|
|
}
|
|
|
|
return (argc);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop up an out of memory message, returns to Windows */
|
2006-07-10 21:04:37 +00:00
|
|
|
static BOOL
|
|
|
|
OutOfMemory(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2014-07-27 19:52:52 -04:00
|
|
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
|
2006-07-10 21:04:37 +00:00
|
|
|
return FALSE;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-15 10:59:39 -04:00
|
|
|
#if defined(_MSC_VER)
|
2001-05-23 23:35:10 +00:00
|
|
|
/* The VC++ compiler needs main defined */
|
2001-04-26 16:45:43 +00:00
|
|
|
#define console_main main
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This is where execution begins [console apps] */
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
console_main(int argc, char *argv[])
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2013-06-05 21:23:59 -07:00
|
|
|
SDL_SetMainReady();
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Run the application main() code */
|
2014-07-27 19:52:52 -04:00
|
|
|
return SDL_main(argc, argv);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is where execution begins [windowed apps] */
|
2006-07-10 21:04:37 +00:00
|
|
|
int WINAPI
|
2012-11-10 10:37:38 -08:00
|
|
|
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
char **argv;
|
|
|
|
int argc;
|
|
|
|
char *cmdline;
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Grab the command line */
|
2012-11-10 10:37:38 -08:00
|
|
|
TCHAR *text = GetCommandLine();
|
|
|
|
#if UNICODE
|
Fixed bug 2786 - "UCS-2-INTERNAL" iconv encoding is not supported everywhere, use UTF-16LE instead
Jonas Kulla
src/main/windows/SDL_windows_main.c:137:
cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));
I'm trying to compile an SDL2 application for windows using the mingw-w64 32bit toolchain provided by my distro (Fedora 19). However, even the simplest test program that does nothing at all fails to startup with a "Fatal error - out of memory" message because the mingw iconv library provided by my distro does not support the "UCS-2-INTERNAL" encoding and the conversion returns null.
From my little bit of research, it turns out that even though this encoding is supported by the external GNU libiconv library, some glibc versions (?) don't support it with their internal iconv routines, and will instead provide the native endian encoding when "UCS-2" is specified.
Nonetheless, I wonder why the native endianness is considered in the first place when Windows doesn't even run on any big endian archs (to my knowledge). And true enough, 'WIN_StringToUTF8' from core/windows/SDL_windows.h is used everywhere else in the windows backend, which is just a macro to iconv with "UTF-16LE" as source. Therefore it would IMO make sense to use this macro here as well, which would solve my problem (patch attached).
2014-11-28 04:51:33 -08:00
|
|
|
cmdline = WIN_StringToUTF8(text);
|
2012-11-10 10:37:38 -08:00
|
|
|
#else
|
|
|
|
cmdline = SDL_strdup(text);
|
|
|
|
#endif
|
2006-07-10 21:04:37 +00:00
|
|
|
if (cmdline == NULL) {
|
|
|
|
return OutOfMemory();
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Parse it into argv and argc */
|
|
|
|
argc = ParseCommandLine(cmdline, NULL);
|
|
|
|
argv = SDL_stack_alloc(char *, argc + 1);
|
|
|
|
if (argv == NULL) {
|
|
|
|
return OutOfMemory();
|
|
|
|
}
|
|
|
|
ParseCommandLine(cmdline, argv);
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Run the main program */
|
|
|
|
console_main(argc, argv);
|
2006-02-19 23:46:34 +00:00
|
|
|
|
2013-07-06 15:56:06 +02:00
|
|
|
SDL_stack_free(argv);
|
|
|
|
|
2012-11-10 10:37:38 -08:00
|
|
|
SDL_free(cmdline);
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Hush little compiler, don't you cry... */
|
|
|
|
return 0;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
|
2011-10-31 05:56:58 -04:00
|
|
|
#endif /* __WIN32__ */
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|