src/main/win32/SDL_win32_main.c
author Sam Lantinga
Sat, 29 Dec 2007 19:29:20 +0000
changeset 2286 41a6675d8700
parent 1895 c121d94672cb
child 4511 ae7799d16c87
permissions -rw-r--r--
Merged fix for bug #503 from SDL 1.2 revision 3487
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
/* Include the SDL main definition header */
slouken@754
    14
#include "SDL.h"
slouken@754
    15
#include "SDL_main.h"
slouken@754
    16
slouken@754
    17
#ifdef main
slouken@754
    18
# ifndef _WIN32_WCE_EMULATION
slouken@754
    19
#  undef main
slouken@754
    20
# endif /* _WIN32_WCE_EMULATION */
slouken@754
    21
#endif /* main */
slouken@754
    22
slouken@754
    23
#if defined(_WIN32_WCE) && _WIN32_WCE < 300
slouken@754
    24
/* seems to be undefined in Win CE although in online help */
slouken@754
    25
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
slouken@754
    26
#endif /* _WIN32_WCE < 300 */
slouken@754
    27
slouken@2286
    28
static void
slouken@2286
    29
UnEscapeQuotes(char *arg)
slouken@2286
    30
{
slouken@2286
    31
    char *last = NULL;
slouken@2286
    32
slouken@2286
    33
    while (*arg) {
slouken@2286
    34
        if (*arg == '"' && *last == '\\') {
slouken@2286
    35
            char *c_curr = arg;
slouken@2286
    36
            char *c_last = last;
slouken@2286
    37
slouken@2286
    38
            while (*c_curr) {
slouken@2286
    39
                *c_last = *c_curr;
slouken@2286
    40
                c_last = c_curr;
slouken@2286
    41
                c_curr++;
slouken@2286
    42
            }
slouken@2286
    43
            *c_last = '\0';
slouken@2286
    44
        }
slouken@2286
    45
        last = arg;
slouken@2286
    46
        arg++;
slouken@2286
    47
    }
slouken@2286
    48
}
slouken@2286
    49
slouken@754
    50
/* Parse a command line buffer into arguments */
slouken@1895
    51
static int
slouken@1895
    52
ParseCommandLine(char *cmdline, char **argv)
slouken@754
    53
{
slouken@1895
    54
    char *bufp;
slouken@2286
    55
    char *lastp = NULL;
slouken@2286
    56
    int argc, last_argc;
slouken@754
    57
slouken@2286
    58
    argc = last_argc = 0;
slouken@1895
    59
    for (bufp = cmdline; *bufp;) {
slouken@1895
    60
        /* Skip leading whitespace */
slouken@1895
    61
        while (isspace(*bufp)) {
slouken@1895
    62
            ++bufp;
slouken@1895
    63
        }
slouken@1895
    64
        /* Skip over argument */
slouken@1895
    65
        if (*bufp == '"') {
slouken@1895
    66
            ++bufp;
slouken@1895
    67
            if (*bufp) {
slouken@1895
    68
                if (argv) {
slouken@1895
    69
                    argv[argc] = bufp;
slouken@1895
    70
                }
slouken@1895
    71
                ++argc;
slouken@1895
    72
            }
slouken@1895
    73
            /* Skip over word */
slouken@2286
    74
            while (*bufp && (*bufp != '"' || *lastp == '\\')) {
slouken@2286
    75
                lastp = bufp;
slouken@1895
    76
                ++bufp;
slouken@1895
    77
            }
slouken@1895
    78
        } else {
slouken@1895
    79
            if (*bufp) {
slouken@1895
    80
                if (argv) {
slouken@1895
    81
                    argv[argc] = bufp;
slouken@1895
    82
                }
slouken@1895
    83
                ++argc;
slouken@1895
    84
            }
slouken@1895
    85
            /* Skip over word */
slouken@1895
    86
            while (*bufp && !isspace(*bufp)) {
slouken@1895
    87
                ++bufp;
slouken@1895
    88
            }
slouken@1895
    89
        }
slouken@1895
    90
        if (*bufp) {
slouken@1895
    91
            if (argv) {
slouken@1895
    92
                *bufp = '\0';
slouken@1895
    93
            }
slouken@1895
    94
            ++bufp;
slouken@1895
    95
        }
slouken@2286
    96
slouken@2286
    97
        /* Strip out \ from \" sequences */
slouken@2286
    98
        if (argv && last_argc != argc) {
slouken@2286
    99
            UnEscapeQuotes(argv[last_argc]);
slouken@2286
   100
        }
slouken@2286
   101
        last_argc = argc;
slouken@1895
   102
    }
slouken@1895
   103
    if (argv) {
slouken@1895
   104
        argv[argc] = NULL;
slouken@1895
   105
    }
slouken@1895
   106
    return (argc);
slouken@754
   107
}
slouken@754
   108
slouken@754
   109
/* Show an error message */
slouken@1895
   110
static void
slouken@1895
   111
ShowError(const char *title, const char *message)
slouken@754
   112
{
slouken@754
   113
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
slouken@754
   114
#ifdef USE_MESSAGEBOX
slouken@1895
   115
    MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
slouken@754
   116
#else
slouken@1895
   117
    fprintf(stderr, "%s: %s\n", title, message);
slouken@754
   118
#endif
slouken@754
   119
}
slouken@754
   120
slouken@754
   121
/* Pop up an out of memory message, returns to Windows */
slouken@1895
   122
static BOOL
slouken@1895
   123
OutOfMemory(void)
slouken@754
   124
{
slouken@1895
   125
    ShowError("Fatal Error", "Out of memory - aborting");
slouken@1895
   126
    return FALSE;
slouken@754
   127
}
slouken@754
   128
slouken@754
   129
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
slouken@754
   130
/* The VC++ compiler needs main defined */
slouken@754
   131
#define console_main main
slouken@754
   132
#endif
slouken@754
   133
slouken@754
   134
/* This is where execution begins [console apps] */
slouken@1895
   135
int
slouken@1895
   136
console_main(int argc, char *argv[])
slouken@754
   137
{
slouken@1895
   138
    int status;
slouken@754
   139
slouken@1895
   140
    /* Run the application main() code */
slouken@1895
   141
    status = SDL_main(argc, argv);
slouken@754
   142
slouken@1895
   143
    /* Exit cleanly, calling atexit() functions */
slouken@1895
   144
    exit(status);
slouken@754
   145
slouken@1895
   146
    /* Hush little compiler, don't you cry... */
slouken@1895
   147
    return 0;
slouken@754
   148
}
slouken@754
   149
slouken@754
   150
/* This is where execution begins [windowed apps] */
slouken@1895
   151
int WINAPI
slouken@1895
   152
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
slouken@754
   153
{
slouken@1895
   154
    char **argv;
slouken@1895
   155
    int argc;
slouken@1895
   156
    char *cmdline;
slouken@1286
   157
#ifdef _WIN32_WCE
slouken@1895
   158
    wchar_t *bufp;
slouken@1895
   159
    int nLen;
slouken@754
   160
#else
slouken@1895
   161
    char *bufp;
slouken@1895
   162
    size_t nLen;
slouken@754
   163
#endif
slouken@754
   164
slouken@1465
   165
#ifdef _WIN32_WCE
slouken@1895
   166
    nLen = wcslen(szCmdLine) + 128 + 1;
slouken@1895
   167
    bufp = SDL_stack_alloc(wchar_t, nLen * 2);
slouken@1895
   168
    wcscpy(bufp, TEXT("\""));
slouken@1895
   169
    GetModuleFileName(NULL, bufp + 1, 128 - 3);
slouken@1895
   170
    wcscpy(bufp + wcslen(bufp), TEXT("\" "));
slouken@1895
   171
    wcsncpy(bufp + wcslen(bufp), szCmdLine, nLen - wcslen(bufp));
slouken@1895
   172
    nLen = wcslen(bufp) + 1;
slouken@1895
   173
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1895
   174
    if (cmdline == NULL) {
slouken@1895
   175
        return OutOfMemory();
slouken@1895
   176
    }
slouken@1895
   177
    WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
slouken@754
   178
#else
slouken@1895
   179
    /* Grab the command line */
slouken@1895
   180
    bufp = GetCommandLine();
slouken@1895
   181
    nLen = SDL_strlen(bufp) + 1;
slouken@1895
   182
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1895
   183
    if (cmdline == NULL) {
slouken@1895
   184
        return OutOfMemory();
slouken@1895
   185
    }
slouken@1895
   186
    SDL_strlcpy(cmdline, bufp, nLen);
slouken@1465
   187
#endif
slouken@754
   188
slouken@1895
   189
    /* Parse it into argv and argc */
slouken@1895
   190
    argc = ParseCommandLine(cmdline, NULL);
slouken@1895
   191
    argv = SDL_stack_alloc(char *, argc + 1);
slouken@1895
   192
    if (argv == NULL) {
slouken@1895
   193
        return OutOfMemory();
slouken@1895
   194
    }
slouken@1895
   195
    ParseCommandLine(cmdline, argv);
slouken@754
   196
slouken@1895
   197
    /* Run the main program */
slouken@1895
   198
    console_main(argc, argv);
slouken@754
   199
slouken@1895
   200
    /* Hush little compiler, don't you cry... */
slouken@1895
   201
    return 0;
slouken@1895
   202
}
slouken@754
   203
slouken@1895
   204
/* vi: set ts=4 sw=4 expandtab: */