| 1 |
/* |
| 2 |
SDL_windows_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 |
#include "SDL_config.h" |
| 7 |
|
| 8 |
#ifndef UNICODE |
| 9 |
#define UNICODE 1 |
| 10 |
#endif |
| 11 |
|
| 12 |
#include <windows.h> |
| 13 |
#include <shellapi.h> /* CommandLineToArgvW() */ |
| 14 |
|
| 15 |
#define WIN_StringToUTF8W(S) SDL_iconv_string("UTF-8", "UTF-16LE", (const char *)(S), (SDL_wcslen(S) + 1) * sizeof(WCHAR)) |
| 16 |
|
| 17 |
/* See https://github.com/libsdl-org/SDL/pull/7607 */ |
| 18 |
/* force_align_arg_pointer attribute requires gcc >= 4.2.x. */ |
| 19 |
#if defined(__clang__) |
| 20 |
#define HAVE_FORCE_ALIGN_ARG_POINTER |
| 21 |
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) |
| 22 |
#define HAVE_FORCE_ALIGN_ARG_POINTER |
| 23 |
#endif |
| 24 |
#if defined(__GNUC__) && defined(__i386__) && defined(HAVE_FORCE_ALIGN_ARG_POINTER) |
| 25 |
#define MINGW32_FORCEALIGN __attribute__((force_align_arg_pointer)) |
| 26 |
#else |
| 27 |
#define MINGW32_FORCEALIGN |
| 28 |
#endif |
| 29 |
|
| 30 |
/* Include the SDL main definition header */ |
| 31 |
#include "SDL.h" |
| 32 |
#include "SDL_main.h" |
| 33 |
|
| 34 |
#ifdef main |
| 35 |
#undef main |
| 36 |
#endif /* main */ |
| 37 |
|
| 38 |
/* Pop up an out of memory message, returns to Windows */ |
| 39 |
static BOOL OutOfMemory(void) |
| 40 |
{ |
| 41 |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL); |
| 42 |
return FALSE; |
| 43 |
} |
| 44 |
|
| 45 |
#if defined(_MSC_VER) |
| 46 |
/* The VC++ compiler needs main/wmain defined */ |
| 47 |
#define console_ansi_main main |
| 48 |
#if UNICODE |
| 49 |
#define console_wmain wmain |
| 50 |
#endif |
| 51 |
#endif |
| 52 |
|
| 53 |
/* Gets the arguments with GetCommandLine, converts them to argc and argv |
| 54 |
and calls SDL_main */ |
| 55 |
static int main_getcmdline(void) |
| 56 |
{ |
| 57 |
LPWSTR *argvw; |
| 58 |
char **argv; |
| 59 |
int i, argc, result; |
| 60 |
|
| 61 |
argvw = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 62 |
if (argvw == NULL) { |
| 63 |
return OutOfMemory(); |
| 64 |
} |
| 65 |
|
| 66 |
/* Note that we need to be careful about how we allocate/free memory here. |
| 67 |
* If the application calls SDL_SetMemoryFunctions(), we can't rely on |
| 68 |
* SDL_free() to use the same allocator after SDL_main() returns. |
| 69 |
*/ |
| 70 |
|
| 71 |
/* Parse it into argv and argc */ |
| 72 |
argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv)); |
| 73 |
if (argv == NULL) { |
| 74 |
return OutOfMemory(); |
| 75 |
} |
| 76 |
for (i = 0; i < argc; ++i) { |
| 77 |
DWORD len; |
| 78 |
char *arg = WIN_StringToUTF8W(argvw[i]); |
| 79 |
if (arg == NULL) { |
| 80 |
return OutOfMemory(); |
| 81 |
} |
| 82 |
len = (DWORD)SDL_strlen(arg); |
| 83 |
argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (size_t)len + 1); |
| 84 |
if (!argv[i]) { |
| 85 |
return OutOfMemory(); |
| 86 |
} |
| 87 |
SDL_memcpy(argv[i], arg, len); |
| 88 |
SDL_free(arg); |
| 89 |
} |
| 90 |
argv[i] = NULL; |
| 91 |
LocalFree(argvw); |
| 92 |
|
| 93 |
SDL_SetMainReady(); |
| 94 |
|
| 95 |
/* Run the application main() code */ |
| 96 |
result = SDL_main(argc, argv); |
| 97 |
|
| 98 |
/* Free argv, to avoid memory leak */ |
| 99 |
for (i = 0; i < argc; ++i) { |
| 100 |
HeapFree(GetProcessHeap(), 0, argv[i]); |
| 101 |
} |
| 102 |
HeapFree(GetProcessHeap(), 0, argv); |
| 103 |
|
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
/* This is where execution begins [console apps, ansi] */ |
| 108 |
int console_ansi_main(int argc, char *argv[]) |
| 109 |
{ |
| 110 |
return main_getcmdline(); |
| 111 |
} |
| 112 |
|
| 113 |
#if UNICODE |
| 114 |
/* This is where execution begins [console apps, unicode] */ |
| 115 |
int console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp) |
| 116 |
{ |
| 117 |
return main_getcmdline(); |
| 118 |
} |
| 119 |
#endif |
| 120 |
|
| 121 |
/* This is where execution begins [windowed apps] */ |
| 122 |
int WINAPI MINGW32_FORCEALIGN |
| 123 |
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) /* NOLINT(readability-inconsistent-declaration-parameter-name) */ |
| 124 |
{ |
| 125 |
return main_getcmdline(); |
| 126 |
} |