Merge commit '852f2a6343518919e5ca8d3c1bbcab9f493e3cd8'
This commit is contained in:
142
external/sdl/SDL/src/main/SDL_main_callbacks.c
vendored
Normal file
142
external/sdl/SDL/src/main/SDL_main_callbacks.c
vendored
Normal file
@ -0,0 +1,142 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "SDL_internal.h"
|
||||
#include "SDL_main_callbacks.h"
|
||||
|
||||
static SDL_AppEvent_func SDL_main_event_callback;
|
||||
static SDL_AppIterate_func SDL_main_iteration_callback;
|
||||
static SDL_AppQuit_func SDL_main_quit_callback;
|
||||
static 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.
|
||||
|
||||
// Return true if this event needs to be processed before returning from the event watcher
|
||||
static SDL_bool ShouldDispatchImmediately(SDL_Event *event)
|
||||
{
|
||||
switch (event->type) {
|
||||
case SDL_EVENT_TERMINATING:
|
||||
case SDL_EVENT_LOW_MEMORY:
|
||||
case SDL_EVENT_WILL_ENTER_BACKGROUND:
|
||||
case SDL_EVENT_DID_ENTER_BACKGROUND:
|
||||
case SDL_EVENT_WILL_ENTER_FOREGROUND:
|
||||
case SDL_EVENT_DID_ENTER_FOREGROUND:
|
||||
return SDL_TRUE;
|
||||
default:
|
||||
return SDL_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static void SDL_DispatchMainCallbackEvent(SDL_Event *event)
|
||||
{
|
||||
if (SDL_AtomicGet(&apprc) == 0) { // if already quitting, don't send the event to the app.
|
||||
SDL_AtomicCAS(&apprc, 0, SDL_main_event_callback(event));
|
||||
}
|
||||
}
|
||||
|
||||
static void SDL_DispatchMainCallbackEvents()
|
||||
{
|
||||
SDL_Event events[16];
|
||||
|
||||
for (;;) {
|
||||
int count = SDL_PeepEvents(events, SDL_arraysize(events), SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST);
|
||||
if (count <= 0) {
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < count; ++i) {
|
||||
SDL_Event *event = &events[i];
|
||||
if (!ShouldDispatchImmediately(event)) {
|
||||
SDL_DispatchMainCallbackEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int SDLCALL SDL_MainCallbackEventWatcher(void *userdata, SDL_Event *event)
|
||||
{
|
||||
if (ShouldDispatchImmediately(event)) {
|
||||
// Make sure any currently queued events are processed then dispatch this before continuing
|
||||
SDL_DispatchMainCallbackEvents();
|
||||
SDL_DispatchMainCallbackEvent(event);
|
||||
} else {
|
||||
// We'll process this event later from the main event queue
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_bool SDL_HasMainCallbacks()
|
||||
{
|
||||
if (SDL_main_iteration_callback) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
int SDL_InitMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
|
||||
{
|
||||
SDL_main_iteration_callback = appiter;
|
||||
SDL_main_event_callback = appevent;
|
||||
SDL_main_quit_callback = appquit;
|
||||
SDL_AtomicSet(&apprc, 0);
|
||||
|
||||
const int rc = appinit(argc, argv);
|
||||
if (SDL_AtomicCAS(&apprc, 0, rc) && (rc == 0)) { // bounce if SDL_AppInit already said abort, otherwise...
|
||||
// make sure we definitely have events initialized, even if the app didn't do it.
|
||||
if (SDL_InitSubSystem(SDL_INIT_EVENTS) == -1) {
|
||||
SDL_AtomicSet(&apprc, -1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (SDL_AddEventWatch(SDL_MainCallbackEventWatcher, NULL) < 0) {
|
||||
SDL_AtomicSet(&apprc, -1);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return SDL_AtomicGet(&apprc);
|
||||
}
|
||||
|
||||
int SDL_IterateMainCallbacks(SDL_bool pump_events)
|
||||
{
|
||||
if (pump_events) {
|
||||
SDL_PumpEvents();
|
||||
}
|
||||
SDL_DispatchMainCallbackEvents();
|
||||
|
||||
int rc = SDL_AtomicGet(&apprc);
|
||||
if (rc == 0) {
|
||||
rc = SDL_main_iteration_callback();
|
||||
if (!SDL_AtomicCAS(&apprc, 0, rc)) {
|
||||
rc = SDL_AtomicGet(&apprc); // something else already set a quit result, keep that.
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
void SDL_QuitMainCallbacks(void)
|
||||
{
|
||||
SDL_DelEventWatch(SDL_MainCallbackEventWatcher, NULL);
|
||||
SDL_main_quit_callback();
|
||||
|
||||
// for symmetry, you should explicitly Quit what you Init, but we might come through here uninitialized and SDL_Quit() will clear everything anyhow.
|
||||
//SDL_QuitSubSystem(SDL_INIT_EVENTS);
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
32
external/sdl/SDL/src/main/SDL_main_callbacks.h
vendored
Normal file
32
external/sdl/SDL/src/main/SDL_main_callbacks.h
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL_main_callbacks_h_
|
||||
#define SDL_main_callbacks_h_
|
||||
|
||||
SDL_bool SDL_HasMainCallbacks();
|
||||
int SDL_InitMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func _appiter, SDL_AppEvent_func _appevent, SDL_AppQuit_func _appquit);
|
||||
int SDL_IterateMainCallbacks(SDL_bool pump_events);
|
||||
void SDL_QuitMainCallbacks(void);
|
||||
|
||||
#endif // SDL_main_callbacks_h_
|
||||
|
||||
|
47
external/sdl/SDL/src/main/emscripten/SDL_sysmain_callbacks.c
vendored
Normal file
47
external/sdl/SDL/src/main/emscripten/SDL_sysmain_callbacks.c
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "SDL_internal.h"
|
||||
#include "../SDL_main_callbacks.h"
|
||||
|
||||
#include <emscripten.h>
|
||||
|
||||
static void EmscriptenInternalMainloop(void)
|
||||
{
|
||||
const int rc = SDL_IterateMainCallbacks(SDL_TRUE);
|
||||
if (rc != 0) {
|
||||
SDL_QuitMainCallbacks();
|
||||
emscripten_cancel_main_loop(); // kill" the mainloop, so it stops calling back into it.
|
||||
exit((rc < 0) ? 1 : 0); // hopefully this takes down everything else, too.
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
|
||||
{
|
||||
const int rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
|
||||
if (rc == 0) {
|
||||
emscripten_set_main_loop(EmscriptenInternalMainloop, 0, 0); // run at refresh rate, don't throw an exception since we do an orderly return.
|
||||
} else {
|
||||
SDL_QuitMainCallbacks();
|
||||
}
|
||||
return (rc < 0) ? 1 : 0;
|
||||
}
|
||||
|
83
external/sdl/SDL/src/main/generic/SDL_sysmain_callbacks.c
vendored
Normal file
83
external/sdl/SDL/src/main/generic/SDL_sysmain_callbacks.c
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "SDL_internal.h"
|
||||
#include "../SDL_main_callbacks.h"
|
||||
#include "../../video/SDL_sysvideo.h"
|
||||
|
||||
#ifndef __IOS__
|
||||
|
||||
static int callback_rate_increment = 0;
|
||||
|
||||
static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue)
|
||||
{
|
||||
const int callback_rate = newValue ? SDL_atoi(newValue) : 60;
|
||||
if (callback_rate > 0) {
|
||||
callback_rate_increment = ((Uint64) 1000000000) / ((Uint64) callback_rate);
|
||||
} else {
|
||||
callback_rate_increment = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
|
||||
{
|
||||
int rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
|
||||
if (rc == 0) {
|
||||
SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
|
||||
|
||||
Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0;
|
||||
|
||||
while ((rc = SDL_IterateMainCallbacks(SDL_TRUE)) == 0) {
|
||||
// !!! FIXME: this can be made more complicated if we decide to
|
||||
// !!! FIXME: optionally hand off callback responsibility to the
|
||||
// !!! FIXME: video subsystem (for example, if Wayland has a
|
||||
// !!! FIXME: protocol to drive an animation loop, maybe we hand
|
||||
// !!! FIXME: off to them here if/when the video subsystem becomes
|
||||
// !!! FIXME: initialized).
|
||||
|
||||
// !!! FIXME: maybe respect this hint even if there _is_ a window.
|
||||
// if there's no window, try to run at about 60fps (or whatever rate
|
||||
// the hint requested). This makes this not eat all the CPU in
|
||||
// simple things like loopwave. If there's a window, we run as fast
|
||||
// as possible, which means we'll clamp to vsync in common cases,
|
||||
// and won't be restrained to vsync if the app is doing a benchmark
|
||||
// or doesn't want to be, based on how they've set up that window.
|
||||
if ((callback_rate_increment == 0) || SDL_HasWindows()) {
|
||||
next_iteration = 0; // just clear the timer and run at the pace the video subsystem allows.
|
||||
} else {
|
||||
const Uint64 now = SDL_GetTicksNS();
|
||||
if (next_iteration > now) { // Running faster than the limit, sleep a little.
|
||||
SDL_DelayNS(next_iteration - now);
|
||||
} else {
|
||||
next_iteration = now; // running behind (or just lost the window)...reset the timer.
|
||||
}
|
||||
next_iteration += callback_rate_increment;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_DelHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
|
||||
}
|
||||
SDL_QuitMainCallbacks();
|
||||
|
||||
return (rc < 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
#endif // !__IOS__
|
82
external/sdl/SDL/src/main/ios/SDL_sysmain_callbacks.m
vendored
Normal file
82
external/sdl/SDL/src/main/ios/SDL_sysmain_callbacks.m
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "SDL_internal.h"
|
||||
#include "../SDL_main_callbacks.h"
|
||||
|
||||
#ifdef __IOS__
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface SDLIosMainCallbacksDisplayLink : NSObject
|
||||
@property(nonatomic, retain) CADisplayLink *displayLink;
|
||||
- (void)appIteration:(CADisplayLink *)sender;
|
||||
- (instancetype)init:(SDL_AppIterate_func)_appiter quitfunc:(SDL_AppQuit_func)_appquit;
|
||||
@end
|
||||
|
||||
static SDLIosMainCallbacksDisplayLink *globalDisplayLink;
|
||||
|
||||
@implementation SDLIosMainCallbacksDisplayLink
|
||||
|
||||
- (instancetype)init:(SDL_AppIterate_func)_appiter quitfunc:(SDL_AppQuit_func)_appquit;
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(appIteration:)];
|
||||
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)appIteration:(CADisplayLink *)sender
|
||||
{
|
||||
const int rc = SDL_IterateMainCallbacks(SDL_TRUE);
|
||||
if (rc != 0) {
|
||||
[self.displayLink invalidate];
|
||||
self.displayLink = nil;
|
||||
globalDisplayLink = nil;
|
||||
SDL_QuitMainCallbacks();
|
||||
exit((rc < 0) ? 1 : 0);
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
// SDL_RunApp will land in UIApplicationMain, which calls SDL_main from postFinishLaunch, which calls this.
|
||||
// When we return from here, we're living in the RunLoop, and a CADisplayLink is firing regularly for us.
|
||||
int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
|
||||
{
|
||||
const int rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
|
||||
if (rc == 0) {
|
||||
globalDisplayLink = [[SDLIosMainCallbacksDisplayLink alloc] init:appiter quitfunc:appquit];
|
||||
if (globalDisplayLink != nil) {
|
||||
return 0; // this will fall all the way out of SDL_main, where UIApplicationMain will keep running the RunLoop.
|
||||
}
|
||||
}
|
||||
|
||||
// appinit requested quit, just bounce out now.
|
||||
SDL_QuitMainCallbacks();
|
||||
exit((rc < 0) ? 1 : 0);
|
||||
|
||||
return 1; // just in case.
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user