summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp')
-rw-r--r--contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp148
1 files changed, 148 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp b/contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp
new file mode 100644
index 0000000..d7bdea0
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp
@@ -0,0 +1,148 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23extern "C" {
24#include "../../core/gdk/SDL_gdk.h"
25#include "../../core/windows/SDL_windows.h"
26#include "../../events/SDL_events_c.h"
27}
28#include <XGameRuntime.h>
29#include <xsapi-c/services_c.h>
30#include <shellapi.h> // CommandLineToArgvW()
31#include <appnotify.h>
32
33// Pop up an out of memory message, returns to Windows
34static BOOL OutOfMemory(void)
35{
36 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
37 return FALSE;
38}
39
40/* Gets the arguments with GetCommandLine, converts them to argc and argv
41 and calls SDL_main */
42extern "C"
43int SDL_RunApp(int, char**, SDL_main_func mainFunction, void *reserved)
44{
45 LPWSTR *argvw;
46 char **argv;
47 int i, argc, result;
48 HRESULT hr;
49 XTaskQueueHandle taskQueue;
50
51 argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
52 if (argvw == NULL) {
53 return OutOfMemory();
54 }
55
56 /* Note that we need to be careful about how we allocate/free memory here.
57 * If the application calls SDL_SetMemoryFunctions(), we can't rely on
58 * SDL_free() to use the same allocator after SDL_main() returns.
59 */
60
61 // Parse it into argv and argc
62 argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
63 if (argv == NULL) {
64 return OutOfMemory();
65 }
66 for (i = 0; i < argc; ++i) {
67 const int utf8size = WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
68 if (!utf8size) { // uhoh?
69 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
70 return -1;
71 }
72
73 argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, utf8size); // this size includes the null-terminator character.
74 if (!argv[i]) {
75 return OutOfMemory();
76 }
77
78 if (WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, argv[i], utf8size, NULL, NULL) == 0) { // failed? uhoh!
79 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
80 return -1;
81 }
82 }
83 argv[i] = NULL;
84 LocalFree(argvw);
85
86 hr = XGameRuntimeInitialize();
87
88 if (SUCCEEDED(hr) && SDL_GetGDKTaskQueue(&taskQueue)) {
89 Uint32 titleid = 0;
90 char scidBuffer[64];
91 XblInitArgs xblArgs;
92
93 XTaskQueueSetCurrentProcessTaskQueue(taskQueue);
94
95 // Try to get the title ID and initialize Xbox Live
96 hr = XGameGetXboxTitleId(&titleid);
97 if (SUCCEEDED(hr)) {
98 SDL_zero(xblArgs);
99 xblArgs.queue = taskQueue;
100 SDL_snprintf(scidBuffer, 64, "00000000-0000-0000-0000-0000%08X", titleid);
101 xblArgs.scid = scidBuffer;
102 hr = XblInitialize(&xblArgs);
103 } else {
104 SDL_SetError("[GDK] Unable to get titleid. Will not call XblInitialize. Check MicrosoftGame.config!");
105 }
106
107 SDL_SetMainReady();
108
109 if (!GDK_RegisterChangeNotifications()) {
110 return -1;
111 }
112
113 // Run the application main() code
114 result = mainFunction(argc, argv);
115
116 GDK_UnregisterChangeNotifications();
117
118 // !!! FIXME: This follows the docs exactly, but for some reason still leaks handles on exit?
119 // Terminate the task queue and dispatch any pending tasks
120 XTaskQueueTerminate(taskQueue, false, nullptr, nullptr);
121 while (XTaskQueueDispatch(taskQueue, XTaskQueuePort::Completion, 0))
122 ;
123
124 XTaskQueueCloseHandle(taskQueue);
125
126 XGameRuntimeUninitialize();
127 } else {
128#ifdef SDL_PLATFORM_WINGDK
129 if (hr == E_GAMERUNTIME_DLL_NOT_FOUND) {
130 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "[GDK] Gaming Runtime library not found (xgameruntime.dll)", NULL);
131 } else {
132 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "[GDK] Could not initialize - aborting", NULL);
133 }
134#else
135 SDL_assert_always(0 && "[GDK] Could not initialize - aborting");
136#endif
137 result = -1;
138 }
139
140 // Free argv, to avoid memory leak
141 for (i = 0; i < argc; ++i) {
142 HeapFree(GetProcessHeap(), 0, argv[i]);
143 }
144 HeapFree(GetProcessHeap(), 0, argv);
145
146 return result;
147}
148