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