summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/main')
-rw-r--r--contrib/SDL-3.2.8/src/main/SDL_main_callbacks.c149
-rw-r--r--contrib/SDL-3.2.8/src/main/SDL_main_callbacks.h32
-rw-r--r--contrib/SDL-3.2.8/src/main/SDL_runapp.c43
-rw-r--r--contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_callbacks.c47
-rw-r--r--contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_runapp.c56
-rw-r--r--contrib/SDL-3.2.8/src/main/gdk/SDL_sysmain_runapp.cpp148
-rw-r--r--contrib/SDL-3.2.8/src/main/generic/SDL_sysmain_callbacks.c96
-rw-r--r--contrib/SDL-3.2.8/src/main/ios/SDL_sysmain_callbacks.m88
-rw-r--r--contrib/SDL-3.2.8/src/main/n3ds/SDL_sysmain_runapp.c48
-rw-r--r--contrib/SDL-3.2.8/src/main/ps2/SDL_sysmain_runapp.c84
-rw-r--r--contrib/SDL-3.2.8/src/main/psp/SDL_sysmain_runapp.c82
-rw-r--r--contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c99
12 files changed, 972 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/main/SDL_main_callbacks.c b/contrib/SDL-3.2.8/src/main/SDL_main_callbacks.c
new file mode 100644
index 0000000..9f3d9c1
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/SDL_main_callbacks.c
@@ -0,0 +1,149 @@
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
22#include "SDL_internal.h"
23#include "SDL_main_callbacks.h"
24
25static SDL_AppEvent_func SDL_main_event_callback;
26static SDL_AppIterate_func SDL_main_iteration_callback;
27static SDL_AppQuit_func SDL_main_quit_callback;
28static SDL_AtomicInt apprc; // use an atomic, since events might land from any thread and we don't want to wrap this all in a mutex. A CAS makes sure we only move from zero once.
29static void *SDL_main_appstate = NULL;
30
31// Return true if this event needs to be processed before returning from the event watcher
32static bool ShouldDispatchImmediately(SDL_Event *event)
33{
34 switch (event->type) {
35 case SDL_EVENT_TERMINATING:
36 case SDL_EVENT_LOW_MEMORY:
37 case SDL_EVENT_WILL_ENTER_BACKGROUND:
38 case SDL_EVENT_DID_ENTER_BACKGROUND:
39 case SDL_EVENT_WILL_ENTER_FOREGROUND:
40 case SDL_EVENT_DID_ENTER_FOREGROUND:
41 return true;
42 default:
43 return false;
44 }
45}
46
47static void SDL_DispatchMainCallbackEvent(SDL_Event *event)
48{
49 if (SDL_GetAtomicInt(&apprc) == SDL_APP_CONTINUE) { // if already quitting, don't send the event to the app.
50 SDL_CompareAndSwapAtomicInt(&apprc, SDL_APP_CONTINUE, SDL_main_event_callback(SDL_main_appstate, event));
51 }
52}
53
54static void SDL_DispatchMainCallbackEvents(void)
55{
56 SDL_Event events[16];
57
58 for (;;) {
59 int count = SDL_PeepEvents(events, SDL_arraysize(events), SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST);
60 if (count <= 0) {
61 break;
62 }
63 for (int i = 0; i < count; ++i) {
64 SDL_Event *event = &events[i];
65 if (!ShouldDispatchImmediately(event)) {
66 SDL_DispatchMainCallbackEvent(event);
67 }
68 }
69 }
70}
71
72static bool SDLCALL SDL_MainCallbackEventWatcher(void *userdata, SDL_Event *event)
73{
74 if (ShouldDispatchImmediately(event)) {
75 // Make sure any currently queued events are processed then dispatch this before continuing
76 SDL_DispatchMainCallbackEvents();
77 SDL_DispatchMainCallbackEvent(event);
78
79 // Make sure that we quit if we get a terminating event
80 if (event->type == SDL_EVENT_TERMINATING) {
81 SDL_CompareAndSwapAtomicInt(&apprc, SDL_APP_CONTINUE, SDL_APP_SUCCESS);
82 }
83 } else {
84 // We'll process this event later from the main event queue
85 }
86 return true;
87}
88
89bool SDL_HasMainCallbacks(void)
90{
91 if (SDL_main_iteration_callback) {
92 return true;
93 }
94 return false;
95}
96
97SDL_AppResult SDL_InitMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
98{
99 SDL_main_iteration_callback = appiter;
100 SDL_main_event_callback = appevent;
101 SDL_main_quit_callback = appquit;
102 SDL_SetAtomicInt(&apprc, SDL_APP_CONTINUE);
103
104 const SDL_AppResult rc = appinit(&SDL_main_appstate, argc, argv);
105 if (SDL_CompareAndSwapAtomicInt(&apprc, SDL_APP_CONTINUE, rc) && (rc == SDL_APP_CONTINUE)) { // bounce if SDL_AppInit already said abort, otherwise...
106 // make sure we definitely have events initialized, even if the app didn't do it.
107 if (!SDL_InitSubSystem(SDL_INIT_EVENTS)) {
108 SDL_SetAtomicInt(&apprc, SDL_APP_FAILURE);
109 return SDL_APP_FAILURE;
110 }
111
112 if (!SDL_AddEventWatch(SDL_MainCallbackEventWatcher, NULL)) {
113 SDL_SetAtomicInt(&apprc, SDL_APP_FAILURE);
114 return SDL_APP_FAILURE;
115 }
116 }
117
118 return (SDL_AppResult)SDL_GetAtomicInt(&apprc);
119}
120
121SDL_AppResult SDL_IterateMainCallbacks(bool pump_events)
122{
123 if (pump_events) {
124 SDL_PumpEvents();
125 }
126 SDL_DispatchMainCallbackEvents();
127
128 SDL_AppResult rc = (SDL_AppResult)SDL_GetAtomicInt(&apprc);
129 if (rc == SDL_APP_CONTINUE) {
130 rc = SDL_main_iteration_callback(SDL_main_appstate);
131 if (!SDL_CompareAndSwapAtomicInt(&apprc, SDL_APP_CONTINUE, rc)) {
132 rc = (SDL_AppResult)SDL_GetAtomicInt(&apprc); // something else already set a quit result, keep that.
133 }
134 }
135 return rc;
136}
137
138void SDL_QuitMainCallbacks(SDL_AppResult result)
139{
140 SDL_RemoveEventWatch(SDL_MainCallbackEventWatcher, NULL);
141 SDL_main_quit_callback(SDL_main_appstate, result);
142 SDL_main_appstate = NULL; // just in case.
143
144 // for symmetry, you should explicitly Quit what you Init, but we might come through here uninitialized and SDL_Quit() will clear everything anyhow.
145 //SDL_QuitSubSystem(SDL_INIT_EVENTS);
146
147 SDL_Quit();
148}
149
diff --git a/contrib/SDL-3.2.8/src/main/SDL_main_callbacks.h b/contrib/SDL-3.2.8/src/main/SDL_main_callbacks.h
new file mode 100644
index 0000000..1fd3725
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/SDL_main_callbacks.h
@@ -0,0 +1,32 @@
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
22#ifndef SDL_main_callbacks_h_
23#define SDL_main_callbacks_h_
24
25bool SDL_HasMainCallbacks(void);
26SDL_AppResult SDL_InitMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func _appiter, SDL_AppEvent_func _appevent, SDL_AppQuit_func _appquit);
27SDL_AppResult SDL_IterateMainCallbacks(bool pump_events);
28void SDL_QuitMainCallbacks(SDL_AppResult result);
29
30#endif // SDL_main_callbacks_h_
31
32
diff --git a/contrib/SDL-3.2.8/src/main/SDL_runapp.c b/contrib/SDL-3.2.8/src/main/SDL_runapp.c
new file mode 100644
index 0000000..ddda4a6
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/SDL_runapp.c
@@ -0,0 +1,43 @@
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
23/* Most platforms that use/need SDL_main have their own SDL_RunApp() implementation.
24 * If not, you can special case it here by appending || defined(__YOUR_PLATFORM__) */
25#if ( !defined(SDL_MAIN_NEEDED) && !defined(SDL_MAIN_AVAILABLE) ) || defined(SDL_PLATFORM_ANDROID)
26
27int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved)
28{
29 (void)reserved;
30
31 if(!argv)
32 {
33 // make sure argv isn't NULL, in case some user code doesn't like that
34 static char dummyargv0[] = { 'S', 'D', 'L', '_', 'a', 'p', 'p', '\0' };
35 static char* argvdummy[2] = { dummyargv0, NULL };
36 argc = 1;
37 argv = argvdummy;
38 }
39
40 return mainFunction(argc, argv);
41}
42
43#endif
diff --git a/contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_callbacks.c b/contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_callbacks.c
new file mode 100644
index 0000000..d3b2f95
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_callbacks.c
@@ -0,0 +1,47 @@
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
22#include "SDL_internal.h"
23#include "../SDL_main_callbacks.h"
24
25#include <emscripten.h>
26
27static void EmscriptenInternalMainloop(void)
28{
29 const SDL_AppResult rc = SDL_IterateMainCallbacks(true);
30 if (rc != SDL_APP_CONTINUE) {
31 SDL_QuitMainCallbacks(rc);
32 emscripten_cancel_main_loop(); // kill" the mainloop, so it stops calling back into it.
33 exit((rc == SDL_APP_FAILURE) ? 1 : 0); // hopefully this takes down everything else, too.
34 }
35}
36
37int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
38{
39 const SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
40 if (rc == SDL_APP_CONTINUE) {
41 emscripten_set_main_loop(EmscriptenInternalMainloop, 0, 0); // run at refresh rate, don't throw an exception since we do an orderly return.
42 } else {
43 SDL_QuitMainCallbacks(rc);
44 }
45 return (rc == SDL_APP_FAILURE) ? 1 : 0;
46}
47
diff --git a/contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_runapp.c b/contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_runapp.c
new file mode 100644
index 0000000..20dd6eb
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/emscripten/SDL_sysmain_runapp.c
@@ -0,0 +1,56 @@
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
23#ifdef SDL_PLATFORM_EMSCRIPTEN
24
25#include <emscripten/emscripten.h>
26
27EM_JS_DEPS(sdlrunapp, "$dynCall,$stringToNewUTF8");
28
29int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved)
30{
31 (void)reserved;
32
33 // Move any URL params that start with "SDL_" over to environment
34 // variables, so the hint system can pick them up, etc, much like a user
35 // can set them from a shell prompt on a desktop machine. Ignore all
36 // other params, in case the app wants to use them for something.
37 MAIN_THREAD_EM_ASM({
38 var parms = new URLSearchParams(window.location.search);
39 for (const [key, value] of parms) {
40 if (key.startsWith("SDL_")) {
41 var ckey = stringToNewUTF8(key);
42 var cvalue = stringToNewUTF8(value);
43 if ((ckey != 0) && (cvalue != 0)) {
44 //console.log("Setting SDL env var '" + key + "' to '" + value + "' ...");
45 dynCall('iiii', $0, [ckey, cvalue, 1]);
46 }
47 _free(ckey); // these must use free(), not SDL_free()!
48 _free(cvalue);
49 }
50 }
51 }, SDL_setenv_unsafe);
52
53 return mainFunction(argc, argv);
54}
55
56#endif
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
diff --git a/contrib/SDL-3.2.8/src/main/generic/SDL_sysmain_callbacks.c b/contrib/SDL-3.2.8/src/main/generic/SDL_sysmain_callbacks.c
new file mode 100644
index 0000000..716489f
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/generic/SDL_sysmain_callbacks.c
@@ -0,0 +1,96 @@
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
22#include "SDL_internal.h"
23#include "../SDL_main_callbacks.h"
24#include "../../video/SDL_sysvideo.h"
25
26#ifndef SDL_PLATFORM_IOS
27
28static int callback_rate_increment = 0;
29static bool iterate_after_waitevent = false;
30
31static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue)
32{
33 iterate_after_waitevent = newValue && (SDL_strcmp(newValue, "waitevent") == 0);
34 if (iterate_after_waitevent) {
35 callback_rate_increment = 0;
36 } else {
37 const int callback_rate = newValue ? SDL_atoi(newValue) : 0;
38 if (callback_rate > 0) {
39 callback_rate_increment = ((Uint64) 1000000000) / ((Uint64) callback_rate);
40 } else {
41 callback_rate_increment = 0;
42 }
43 }
44}
45
46static SDL_AppResult GenericIterateMainCallbacks(void)
47{
48 if (iterate_after_waitevent) {
49 SDL_WaitEvent(NULL);
50 }
51 return SDL_IterateMainCallbacks(!iterate_after_waitevent);
52}
53
54int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
55{
56 SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
57 if (rc == 0) {
58 SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
59
60 Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0;
61
62 while ((rc = GenericIterateMainCallbacks()) == SDL_APP_CONTINUE) {
63 // !!! FIXME: this can be made more complicated if we decide to
64 // !!! FIXME: optionally hand off callback responsibility to the
65 // !!! FIXME: video subsystem (for example, if Wayland has a
66 // !!! FIXME: protocol to drive an animation loop, maybe we hand
67 // !!! FIXME: off to them here if/when the video subsystem becomes
68 // !!! FIXME: initialized).
69
70 // Try to run at whatever rate the hint requested. This makes this
71 // not eat all the CPU in simple things like loopwave. By
72 // default, we run as fast as possible, which means we'll clamp to
73 // vsync in common cases, and won't be restrained to vsync if the
74 // app is doing a benchmark or doesn't want to be, based on how
75 // they've set up that window.
76 if (callback_rate_increment == 0) {
77 next_iteration = 0; // just clear the timer and run at the pace the video subsystem allows.
78 } else {
79 const Uint64 now = SDL_GetTicksNS();
80 if (next_iteration > now) { // Running faster than the limit, sleep a little.
81 SDL_DelayPrecise(next_iteration - now);
82 } else {
83 next_iteration = now; // if running behind, reset the timer. If right on time, `next_iteration` already equals `now`.
84 }
85 next_iteration += callback_rate_increment;
86 }
87 }
88
89 SDL_RemoveHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
90 }
91 SDL_QuitMainCallbacks(rc);
92
93 return (rc == SDL_APP_FAILURE) ? 1 : 0;
94}
95
96#endif // !SDL_PLATFORM_IOS
diff --git a/contrib/SDL-3.2.8/src/main/ios/SDL_sysmain_callbacks.m b/contrib/SDL-3.2.8/src/main/ios/SDL_sysmain_callbacks.m
new file mode 100644
index 0000000..becbda2
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/ios/SDL_sysmain_callbacks.m
@@ -0,0 +1,88 @@
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
22#include "SDL_internal.h"
23#include "../SDL_main_callbacks.h"
24
25#ifdef SDL_PLATFORM_IOS
26
27#import <UIKit/UIKit.h>
28
29#include "../../video/uikit/SDL_uikitevents.h" // For SDL_UpdateLifecycleObserver()
30
31
32@interface SDLIosMainCallbacksDisplayLink : NSObject
33@property(nonatomic, retain) CADisplayLink *displayLink;
34- (void)appIteration:(CADisplayLink *)sender;
35- (instancetype)init:(SDL_AppIterate_func)_appiter quitfunc:(SDL_AppQuit_func)_appquit;
36@end
37
38static SDLIosMainCallbacksDisplayLink *globalDisplayLink;
39
40@implementation SDLIosMainCallbacksDisplayLink
41
42- (instancetype)init:(SDL_AppIterate_func)_appiter quitfunc:(SDL_AppQuit_func)_appquit;
43{
44 if ((self = [super init])) {
45 self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(appIteration:)];
46 [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
47 }
48 return self;
49}
50
51- (void)appIteration:(CADisplayLink *)sender
52{
53 const SDL_AppResult rc = SDL_IterateMainCallbacks(true);
54 if (rc != SDL_APP_CONTINUE) {
55 [self.displayLink invalidate];
56 self.displayLink = nil;
57 globalDisplayLink = nil;
58 SDL_QuitMainCallbacks(rc);
59 SDL_UpdateLifecycleObserver();
60 exit((rc == SDL_APP_FAILURE) ? 1 : 0);
61 }
62}
63@end
64
65// SDL_RunApp will land in UIApplicationMain, which calls SDL_main from postFinishLaunch, which calls this.
66// When we return from here, we're living in the RunLoop, and a CADisplayLink is firing regularly for us.
67int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
68{
69 SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
70 if (rc == SDL_APP_CONTINUE) {
71 globalDisplayLink = [[SDLIosMainCallbacksDisplayLink alloc] init:appiter quitfunc:appquit];
72 if (globalDisplayLink == nil) {
73 rc = SDL_APP_FAILURE;
74 } else {
75 return 0; // this will fall all the way out of SDL_main, where UIApplicationMain will keep running the RunLoop.
76 }
77 }
78
79 // appinit requested quit, just bounce out now.
80 SDL_QuitMainCallbacks(rc);
81 exit((rc == SDL_APP_FAILURE) ? 1 : 0);
82
83 return 1; // just in case.
84}
85
86#endif
87
88
diff --git a/contrib/SDL-3.2.8/src/main/n3ds/SDL_sysmain_runapp.c b/contrib/SDL-3.2.8/src/main/n3ds/SDL_sysmain_runapp.c
new file mode 100644
index 0000000..74ead99
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/n3ds/SDL_sysmain_runapp.c
@@ -0,0 +1,48 @@
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
22#include "SDL_internal.h"
23
24#ifdef SDL_PLATFORM_3DS
25
26#include <3ds.h>
27
28int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved)
29{
30 int result;
31 // init
32 osSetSpeedupEnable(true);
33 romfsInit();
34
35 SDL_SetMainReady();
36 result = mainFunction(argc, argv);
37
38 // quit
39 romfsExit();
40
41 return result;
42}
43
44#ifdef __cplusplus
45} // extern "C"
46#endif
47
48#endif
diff --git a/contrib/SDL-3.2.8/src/main/ps2/SDL_sysmain_runapp.c b/contrib/SDL-3.2.8/src/main/ps2/SDL_sysmain_runapp.c
new file mode 100644
index 0000000..23443c5
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/ps2/SDL_sysmain_runapp.c
@@ -0,0 +1,84 @@
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
22#include "SDL_internal.h"
23
24#ifdef SDL_PLATFORM_PS2
25
26// SDL_RunApp() code for PS2 based on SDL_ps2_main.c, fjtrujy@gmail.com
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include <stdio.h>
32
33#include <kernel.h>
34#include <sifrpc.h>
35#include <iopcontrol.h>
36#include <sbv_patches.h>
37#include <ps2_filesystem_driver.h>
38
39__attribute__((weak)) void reset_IOP(void)
40{
41 SifInitRpc(0);
42 while (!SifIopReset(NULL, 0)) {
43 }
44 while (!SifIopSync()) {
45 }
46}
47
48static void prepare_IOP(void)
49{
50 reset_IOP();
51 SifInitRpc(0);
52 sbv_patch_enable_lmb();
53 sbv_patch_disable_prefix_check();
54 sbv_patch_fileio();
55}
56
57static void init_drivers(void)
58{
59 init_ps2_filesystem_driver();
60}
61
62static void deinit_drivers(void)
63{
64 deinit_ps2_filesystem_driver();
65}
66
67int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved)
68{
69 int res;
70 (void)reserved;
71
72 prepare_IOP();
73 init_drivers();
74
75 SDL_SetMainReady();
76
77 res = mainFunction(argc, argv);
78
79 deinit_drivers();
80
81 return res;
82}
83
84#endif // SDL_PLATFORM_PS2
diff --git a/contrib/SDL-3.2.8/src/main/psp/SDL_sysmain_runapp.c b/contrib/SDL-3.2.8/src/main/psp/SDL_sysmain_runapp.c
new file mode 100644
index 0000000..e89f5ff
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/psp/SDL_sysmain_runapp.c
@@ -0,0 +1,82 @@
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
22#include "SDL_internal.h"
23
24#ifdef SDL_PLATFORM_PSP
25
26// SDL_RunApp() for PSP based on SDL_psp_main.c, placed in the public domain by Sam Lantinga 3/13/14
27
28#include <pspkernel.h>
29#include <pspthreadman.h>
30#include "../../events/SDL_events_c.h"
31
32/* If application's main() is redefined as SDL_main, and libSDL_main is
33 linked, then this file will create the standard exit callback,
34 define the PSP_MODULE_INFO macro, and exit back to the browser when
35 the program is finished.
36
37 You can still override other parameters in your own code if you
38 desire, such as PSP_HEAP_SIZE_KB, PSP_MAIN_THREAD_ATTR,
39 PSP_MAIN_THREAD_STACK_SIZE, etc.
40*/
41
42PSP_MODULE_INFO("SDL App", 0, 1, 0);
43PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);
44
45int sdl_psp_exit_callback(int arg1, int arg2, void *common)
46{
47 SDL_SendQuit();
48 return 0;
49}
50
51int sdl_psp_callback_thread(SceSize args, void *argp)
52{
53 int cbid;
54 cbid = sceKernelCreateCallback("Exit Callback",
55 sdl_psp_exit_callback, NULL);
56 sceKernelRegisterExitCallback(cbid);
57 sceKernelSleepThreadCB();
58 return 0;
59}
60
61int sdl_psp_setup_callbacks(void)
62{
63 int thid;
64 thid = sceKernelCreateThread("update_thread",
65 sdl_psp_callback_thread, 0x11, 0xFA0, 0, 0);
66 if (thid >= 0) {
67 sceKernelStartThread(thid, 0, 0);
68 }
69 return thid;
70}
71
72int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved)
73{
74 (void)reserved;
75 sdl_psp_setup_callbacks();
76
77 SDL_SetMainReady();
78
79 return mainFunction(argc, argv);
80}
81
82#endif // SDL_PLATFORM_PSP
diff --git a/contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c b/contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c
new file mode 100644
index 0000000..d8c3fac
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c
@@ -0,0 +1,99 @@
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
23#ifdef SDL_PLATFORM_WIN32
24
25#include "../../core/windows/SDL_windows.h"
26
27/* Win32-specific SDL_RunApp(), which does most of the SDL_main work,
28 based on SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98 */
29
30#include <shellapi.h> // CommandLineToArgvW()
31
32// Pop up an out of memory message, returns to Windows
33static int OutOfMemory(void)
34{
35 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
36 return -1;
37}
38
39int MINGW32_FORCEALIGN SDL_RunApp(int _argc, char* _argv[], SDL_main_func mainFunction, void * reserved)
40{
41 /* Gets the arguments with GetCommandLine, converts them to argc and argv
42 and calls SDL_main */
43
44 LPWSTR *argvw;
45 char **argv;
46 int i, argc, result;
47
48 (void)_argc; (void)_argv; (void)reserved;
49
50 argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
51 if (!argvw) {
52 return OutOfMemory();
53 }
54
55 /* Note that we need to be careful about how we allocate/free memory here.
56 * If the application calls SDL_SetMemoryFunctions(), we can't rely on
57 * SDL_free() to use the same allocator after SDL_main() returns.
58 */
59
60 // Parse it into argv and argc
61 argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
62 if (!argv) {
63 return OutOfMemory();
64 }
65 for (i = 0; i < argc; ++i) {
66 const int utf8size = WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
67 if (!utf8size) { // uhoh?
68 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
69 return -1;
70 }
71
72 argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, utf8size); // this size includes the null-terminator character.
73 if (!argv[i]) {
74 return OutOfMemory();
75 }
76
77 if (WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, argv[i], utf8size, NULL, NULL) == 0) { // failed? uhoh!
78 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
79 return -1;
80 }
81 }
82 argv[i] = NULL;
83 LocalFree(argvw);
84
85 SDL_SetMainReady();
86
87 // Run the application main() code
88 result = mainFunction(argc, argv);
89
90 // Free argv, to avoid memory leak
91 for (i = 0; i < argc; ++i) {
92 HeapFree(GetProcessHeap(), 0, argv[i]);
93 }
94 HeapFree(GetProcessHeap(), 0, argv);
95
96 return result;
97}
98
99#endif // SDL_PLATFORM_WIN32