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