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
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@1668
    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@1668
    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@1668
    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@1668
   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@1668
   113
    MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
slouken@754
   114
#else
slouken@1668
   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@1668
   121
OutOfMemory(void)
slouken@754
   122
{
slouken@1668
   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@1668
   130
cleanup(void)
slouken@1659
   131
{
slouken@1668
   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@1668
   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@1668
   145
    fclose(stdout);
slouken@1668
   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@1668
   151
        file = fopen(stdoutPath, TEXT("rb"));
slouken@1662
   152
        if (file) {
slouken@1668
   153
            empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@1668
   154
            fclose(file);
slouken@1662
   155
            if (empty) {
slouken@1668
   156
                remove(stdoutPath);
slouken@1662
   157
            }
slouken@1662
   158
        }
slouken@1662
   159
    }
slouken@1662
   160
    if (stderrPath[0]) {
slouken@1668
   161
        file = fopen(stderrPath, TEXT("rb"));
slouken@1662
   162
        if (file) {
slouken@1668
   163
            empty = (fgetc(file) == EOF) ? 1 : 0;
slouken@1668
   164
            fclose(file);
slouken@1662
   165
            if (empty) {
slouken@1668
   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@1668
   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@1668
   188
    if ((bufp = SDL_strrchr(argv[0], '\\')) != NULL) {
slouken@1662
   189
        appname = bufp + 1;
slouken@1668
   190
    } else if ((bufp = SDL_strrchr(argv[0], '/')) != NULL) {
slouken@1662
   191
        appname = bufp + 1;
slouken@1662
   192
    }
slouken@754
   193
slouken@1668
   194
    if ((bufp = SDL_strrchr(appname, '.')) == NULL)
slouken@1668
   195
        n = SDL_strlen(appname);
slouken@1662
   196
    else
slouken@1662
   197
        n = (bufp - appname);
slouken@754
   198
slouken@1668
   199
    bufp = SDL_stack_alloc(char, n + 1);
slouken@1662
   200
    if (bufp == NULL) {
slouken@1668
   201
        return OutOfMemory();
slouken@1662
   202
    }
slouken@1668
   203
    SDL_strlcpy(bufp, appname, n + 1);
slouken@1662
   204
    appname = bufp;
slouken@754
   205
slouken@1662
   206
    /* Load SDL dynamic link library */
slouken@1668
   207
    if (SDL_Init(SDL_INIT_NOPARACHUTE) < 0) {
slouken@1668
   208
        ShowError("WinMain() error", SDL_GetError());
slouken@1662
   209
        return (FALSE);
slouken@1662
   210
    }
slouken@1668
   211
    atexit(cleanup_output);
slouken@1668
   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@1668
   219
    SDL_SetModuleHandle(GetModuleHandle(NULL));
slouken@754
   220
slouken@1662
   221
    /* Run the application main() code */
slouken@1668
   222
    status = SDL_main(argc, argv);
slouken@754
   223
slouken@1662
   224
    /* Exit cleanly, calling atexit() functions */
slouken@1668
   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@1668
   234
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
slouken@754
   235
#else
slouken@1662
   236
int WINAPI
slouken@1668
   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@1668
   265
    handle = LoadLibrary(TEXT("DDRAW.DLL"));
slouken@1662
   266
    if (handle != NULL) {
slouken@1668
   267
        FreeLibrary(handle);
slouken@1662
   268
    }
slouken@754
   269
#ifndef NO_STDIO_REDIRECT
slouken@1668
   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@1668
   277
    wcsncpy(stdoutPath, path, SDL_arraysize(stdoutPath));
slouken@1668
   278
    wcsncat(stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath));
slouken@1465
   279
#else
slouken@1668
   280
    SDL_strlcpy(stdoutPath, path, SDL_arraysize(stdoutPath));
slouken@1668
   281
    SDL_strlcat(stdoutPath, DIR_SEPERATOR STDOUT_FILE,
slouken@1668
   282
                SDL_arraysize(stdoutPath));
slouken@1465
   283
#endif
slouken@1662
   284
slouken@1662
   285
    /* Redirect standard input and standard output */
slouken@1668
   286
    newfp = freopen(stdoutPath, TEXT("w"), stdout);
slouken@754
   287
slouken@754
   288
#ifndef _WIN32_WCE
slouken@1662
   289
    if (newfp == NULL) {        /* This happens on NT */
slouken@754
   290
#if !defined(stdout)
slouken@1668
   291
        stdout = fopen(stdoutPath, TEXT("w"));
slouken@754
   292
#else
slouken@1668
   293
        newfp = fopen(stdoutPath, TEXT("w"));
slouken@1662
   294
        if (newfp) {
slouken@1662
   295
            *stdout = *newfp;
slouken@1662
   296
        }
slouken@754
   297
#endif
slouken@1662
   298
    }
slouken@754
   299
#endif /* _WIN32_WCE */
slouken@754
   300
slouken@1465
   301
#ifdef _WIN32_WCE
slouken@1668
   302
    wcsncpy(stderrPath, path, SDL_arraysize(stdoutPath));
slouken@1668
   303
    wcsncat(stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath));
slouken@1465
   304
#else
slouken@1668
   305
    SDL_strlcpy(stderrPath, path, SDL_arraysize(stderrPath));
slouken@1668
   306
    SDL_strlcat(stderrPath, DIR_SEPERATOR STDERR_FILE,
slouken@1668
   307
                SDL_arraysize(stderrPath));
slouken@1465
   308
#endif
slouken@754
   309
slouken@1668
   310
    newfp = freopen(stderrPath, TEXT("w"), stderr);
slouken@754
   311
#ifndef _WIN32_WCE
slouken@1662
   312
    if (newfp == NULL) {        /* This happens on NT */
slouken@754
   313
#if !defined(stderr)
slouken@1668
   314
        stderr = fopen(stderrPath, TEXT("w"));
slouken@754
   315
#else
slouken@1668
   316
        newfp = fopen(stderrPath, TEXT("w"));
slouken@1662
   317
        if (newfp) {
slouken@1662
   318
            *stderr = *newfp;
slouken@1662
   319
        }
slouken@754
   320
#endif
slouken@1662
   321
    }
slouken@754
   322
#endif /* _WIN32_WCE */
slouken@754
   323
slouken@1668
   324
    setvbuf(stdout, NULL, _IOLBF, BUFSIZ);      /* Line buffered */
slouken@1668
   325
    setbuf(stderr, NULL);       /* No buffering */
slouken@754
   326
#endif /* !NO_STDIO_REDIRECT */
slouken@754
   327
slouken@754
   328
#ifdef _WIN32_WCE
slouken@1668
   329
    nLen = wcslen(szCmdLine) + 128 + 1;
slouken@1668
   330
    bufp = SDL_stack_alloc(wchar_t, nLen * 2);
slouken@1668
   331
    wcscpy(bufp, TEXT("\""));
slouken@1668
   332
    GetModuleFileName(NULL, bufp + 1, 128 - 3);
slouken@1668
   333
    wcscpy(bufp + wcslen(bufp), TEXT("\" "));
slouken@1668
   334
    wcsncpy(bufp + wcslen(bufp), szCmdLine, nLen - wcslen(bufp));
slouken@1668
   335
    nLen = wcslen(bufp) + 1;
slouken@1668
   336
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1662
   337
    if (cmdline == NULL) {
slouken@1668
   338
        return OutOfMemory();
slouken@1662
   339
    }
slouken@1668
   340
    WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
slouken@754
   341
#else
slouken@1662
   342
    /* Grab the command line */
slouken@1668
   343
    bufp = GetCommandLine();
slouken@1668
   344
    nLen = SDL_strlen(bufp) + 1;
slouken@1668
   345
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1662
   346
    if (cmdline == NULL) {
slouken@1668
   347
        return OutOfMemory();
slouken@1662
   348
    }
slouken@1668
   349
    SDL_strlcpy(cmdline, bufp, nLen);
slouken@754
   350
#endif
slouken@754
   351
slouken@1662
   352
    /* Parse it into argv and argc */
slouken@1668
   353
    argc = ParseCommandLine(cmdline, NULL);
slouken@1668
   354
    argv = SDL_stack_alloc(char *, argc + 1);
slouken@1662
   355
    if (argv == NULL) {
slouken@1668
   356
        return OutOfMemory();
slouken@1662
   357
    }
slouken@1668
   358
    ParseCommandLine(cmdline, argv);
slouken@754
   359
slouken@1662
   360
    /* Run the main program (after a little SDL initialization) */
slouken@1668
   361
    console_main(argc, argv);
slouken@1379
   362
slouken@1662
   363
    /* Hush little compiler, don't you cry... */
slouken@1662
   364
    return 0;
slouken@754
   365
}
slouken@1662
   366
slouken@1662
   367
/* vi: set ts=4 sw=4 expandtab: */