#include "SDL_internal.h"
#include "../SDL_main_callbacks.h"
#include "../../video/SDL_sysvideo.h"
#ifndef SDL_PLATFORM_IOS
static Uint64 callback_rate_increment = 0;
static bool iterate_after_waitevent = false;
static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue)
{
iterate_after_waitevent = newValue && (SDL_strcmp(newValue, "waitevent") == 0);
if (iterate_after_waitevent) {
callback_rate_increment = 0;
} else {
const double callback_rate = newValue ? SDL_atof(newValue) : 0.0;
if (callback_rate > 0.0) {
callback_rate_increment = (Uint64) ((double) SDL_NS_PER_SECOND / callback_rate);
} else {
callback_rate_increment = 0;
}
}
}
static SDL_AppResult GenericIterateMainCallbacks(void)
{
if (iterate_after_waitevent) {
SDL_WaitEvent(NULL);
}
return SDL_IterateMainCallbacks(!iterate_after_waitevent);
}
int SDL_EnterAppMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
{
SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
if (rc == SDL_APP_CONTINUE) {
SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0;
while ((rc = GenericIterateMainCallbacks()) == SDL_APP_CONTINUE) {
if (callback_rate_increment == 0) {
next_iteration = 0; } else {
const Uint64 now = SDL_GetTicksNS();
if (next_iteration > now) { SDL_DelayPrecise(next_iteration - now);
} else {
next_iteration = now; }
next_iteration += callback_rate_increment;
}
}
SDL_RemoveHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
}
SDL_QuitMainCallbacks(rc);
return (rc == SDL_APP_FAILURE) ? 1 : 0;
}
#endif