mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-28 20:09:38 +08:00
main: Rewrite the Windows implementation of SDL_RunApp()
This new implementation only parses the command line into an argv when the provided argv is NULL. This lets programs that don't want to/can't include `SDL_main.h` to do their own custom argument processing before explicitly calling `SDL_RunApp()` without having SDL clobber the argv. If the user includes `SDL_main.h` as normal, the behavior remains the same as before (because `SDL_main_impl.h` passes a NULL argv). In addition, this new implementation performs fewer allocations and no longer leaks on failure.
This commit is contained in:
committed by
Ryan C. Gordon
parent
f0d958d850
commit
d0e70c37ab
@@ -24,74 +24,80 @@
|
|||||||
|
|
||||||
#include "../../core/windows/SDL_windows.h"
|
#include "../../core/windows/SDL_windows.h"
|
||||||
|
|
||||||
/* Win32-specific SDL_RunApp(), which does most of the SDL_main work,
|
|
||||||
based on SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98 */
|
|
||||||
|
|
||||||
#include <shellapi.h> // CommandLineToArgvW()
|
#include <shellapi.h> // CommandLineToArgvW()
|
||||||
|
|
||||||
// Pop up an out of memory message, returns to Windows
|
|
||||||
static int OutOfMemory(void)
|
static int OutOfMemory(void)
|
||||||
{
|
{
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MINGW32_FORCEALIGN SDL_RunApp(int _argc, char *_argv[], SDL_main_func mainFunction, void * reserved)
|
static int ErrorProcessingCommandLine(void)
|
||||||
{
|
{
|
||||||
/* Gets the arguments with GetCommandLine, converts them to argc and argv
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments - aborting", NULL);
|
||||||
and calls SDL_main */
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
LPWSTR *argvw;
|
int SDL_RunApp(int caller_argc, char *caller_argv[], SDL_main_func mainFunction, void * reserved)
|
||||||
char **argv;
|
{
|
||||||
int i, argc, result;
|
int result;
|
||||||
|
(void)reserved;
|
||||||
|
|
||||||
(void)_argc; (void)_argv; (void)reserved;
|
// If the provided argv is valid, we pass it to the main function as-is, since it's probably what the user wants.
|
||||||
|
// Otherwise, we take a NULL argv as an instruction for SDL to parse the command line into an argv.
|
||||||
|
// On Windows, when SDL provides the main entry point, argv is always NULL.
|
||||||
|
if (caller_argv && caller_argc >= 0) {
|
||||||
|
result = mainFunction(caller_argc, caller_argv);
|
||||||
|
} else {
|
||||||
|
// We need to be careful about how we allocate/free memory here. We can't use SDL_alloc()/SDL_free()
|
||||||
|
// because the application might have used SDL_SetMemoryFunctions() to change the allocator.
|
||||||
|
LPWSTR *argvw = NULL;
|
||||||
|
char **argv = NULL;
|
||||||
|
|
||||||
argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
|
const LPWSTR command_line = GetCommandLineW();
|
||||||
if (!argvw) {
|
|
||||||
return OutOfMemory();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Note that we need to be careful about how we allocate/free memory here.
|
// Because of how the Windows command line is structured, we know for sure that the buffer size required to
|
||||||
* If the application calls SDL_SetMemoryFunctions(), we can't rely on
|
// store all argument strings converted to UTF-8 (with null terminators) is guaranteed to be less than or equal
|
||||||
* SDL_free() to use the same allocator after SDL_main() returns.
|
// to the size of the original command line string converted to UTF-8.
|
||||||
*/
|
const int argdata_size = WideCharToMultiByte(CP_UTF8, 0, command_line, -1, NULL, 0, NULL, NULL); // Includes the null terminator
|
||||||
|
if (!argdata_size) {
|
||||||
// Parse it into argv and argc
|
result = ErrorProcessingCommandLine();
|
||||||
argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
|
goto cleanup;
|
||||||
if (!argv) {
|
|
||||||
return OutOfMemory();
|
|
||||||
}
|
|
||||||
for (i = 0; i < argc; ++i) {
|
|
||||||
const int utf8size = WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
|
|
||||||
if (!utf8size) { // uhoh?
|
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, utf8size); // this size includes the null-terminator character.
|
int argc;
|
||||||
if (!argv[i]) {
|
argvw = CommandLineToArgvW(command_line, &argc);
|
||||||
return OutOfMemory();
|
if (!argvw || argc < 0) {
|
||||||
|
result = OutOfMemory();
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, argv[i], utf8size, NULL, NULL) == 0) { // failed? uhoh!
|
// Allocate argv followed by the argument string buffer as one contiguous allocation.
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
|
argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv) + argdata_size);
|
||||||
return -1;
|
if (!argv) {
|
||||||
|
result = OutOfMemory();
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
char *argdata = ((char *)argv) + (argc + 1) * sizeof(*argv);
|
||||||
|
int argdata_index = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; ++i) {
|
||||||
|
const int bytes_written = WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, argdata + argdata_index, argdata_size - argdata_index, NULL, NULL);
|
||||||
|
if (!bytes_written) {
|
||||||
|
result = ErrorProcessingCommandLine();
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
argv[i] = argdata + argdata_index;
|
||||||
|
argdata_index += bytes_written;
|
||||||
|
}
|
||||||
|
argv[argc] = NULL;
|
||||||
|
|
||||||
|
result = mainFunction(argc, argv);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
HeapFree(GetProcessHeap(), 0, argv);
|
||||||
|
LocalFree(argvw);
|
||||||
}
|
}
|
||||||
argv[i] = NULL;
|
|
||||||
LocalFree(argvw);
|
|
||||||
|
|
||||||
SDL_SetMainReady();
|
|
||||||
|
|
||||||
// Run the application main() code
|
|
||||||
result = mainFunction(argc, argv);
|
|
||||||
|
|
||||||
// Free argv, to avoid memory leak
|
|
||||||
for (i = 0; i < argc; ++i) {
|
|
||||||
HeapFree(GetProcessHeap(), 0, argv[i]);
|
|
||||||
}
|
|
||||||
HeapFree(GetProcessHeap(), 0, argv);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user