src/main/win32/SDL_win32_main.c
author Sam Lantinga
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
     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,
   279              SDL_arraysize (stdoutPath));
   280 #else
   281     SDL_strlcpy (stdoutPath, path, SDL_arraysize (stdoutPath));
   282     SDL_strlcat (stdoutPath, DIR_SEPERATOR STDOUT_FILE,
   283                  SDL_arraysize (stdoutPath));
   284 #endif
   285 
   286     /* Redirect standard input and standard output */
   287     newfp = freopen (stdoutPath, TEXT ("w"), stdout);
   288 
   289 #ifndef _WIN32_WCE
   290     if (newfp == NULL) {        /* This happens on NT */
   291 #if !defined(stdout)
   292         stdout = fopen (stdoutPath, TEXT ("w"));
   293 #else
   294         newfp = fopen (stdoutPath, TEXT ("w"));
   295         if (newfp) {
   296             *stdout = *newfp;
   297         }
   298 #endif
   299     }
   300 #endif /* _WIN32_WCE */
   301 
   302 #ifdef _WIN32_WCE
   303     wcsncpy (stderrPath, path, SDL_arraysize (stdoutPath));
   304     wcsncat (stderrPath, DIR_SEPERATOR STDOUT_FILE,
   305              SDL_arraysize (stdoutPath));
   306 #else
   307     SDL_strlcpy (stderrPath, path, SDL_arraysize (stderrPath));
   308     SDL_strlcat (stderrPath, DIR_SEPERATOR STDERR_FILE,
   309                  SDL_arraysize (stderrPath));
   310 #endif
   311 
   312     newfp = freopen (stderrPath, TEXT ("w"), stderr);
   313 #ifndef _WIN32_WCE
   314     if (newfp == NULL) {        /* This happens on NT */
   315 #if !defined(stderr)
   316         stderr = fopen (stderrPath, TEXT ("w"));
   317 #else
   318         newfp = fopen (stderrPath, TEXT ("w"));
   319         if (newfp) {
   320             *stderr = *newfp;
   321         }
   322 #endif
   323     }
   324 #endif /* _WIN32_WCE */
   325 
   326     setvbuf (stdout, NULL, _IOLBF, BUFSIZ);     /* Line buffered */
   327     setbuf (stderr, NULL);      /* No buffering */
   328 #endif /* !NO_STDIO_REDIRECT */
   329 
   330 #ifdef _WIN32_WCE
   331     nLen = wcslen (szCmdLine) + 128 + 1;
   332     bufp = SDL_stack_alloc (wchar_t, nLen * 2);
   333     wcscpy (bufp, TEXT ("\""));
   334     GetModuleFileName (NULL, bufp + 1, 128 - 3);
   335     wcscpy (bufp + wcslen (bufp), TEXT ("\" "));
   336     wcsncpy (bufp + wcslen (bufp), szCmdLine, nLen - wcslen (bufp));
   337     nLen = wcslen (bufp) + 1;
   338     cmdline = SDL_stack_alloc (char, nLen);
   339     if (cmdline == NULL) {
   340         return OutOfMemory ();
   341     }
   342     WideCharToMultiByte (CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   343 #else
   344     /* Grab the command line */
   345     bufp = GetCommandLine ();
   346     nLen = SDL_strlen (bufp) + 1;
   347     cmdline = SDL_stack_alloc (char, nLen);
   348     if (cmdline == NULL) {
   349         return OutOfMemory ();
   350     }
   351     SDL_strlcpy (cmdline, bufp, nLen);
   352 #endif
   353 
   354     /* Parse it into argv and argc */
   355     argc = ParseCommandLine (cmdline, NULL);
   356     argv = SDL_stack_alloc (char *, argc + 1);
   357     if (argv == NULL) {
   358         return OutOfMemory ();
   359     }
   360     ParseCommandLine (cmdline, argv);
   361 
   362     /* Run the main program (after a little SDL initialization) */
   363     console_main (argc, argv);
   364 
   365     /* Hush little compiler, don't you cry... */
   366     return 0;
   367 }
   368 
   369 /* vi: set ts=4 sw=4 expandtab: */