Atlas - SDL_sysmain_callbacks.c
Home / ext / SDL / src / main / emscripten Lines: 1 | Size: 4658 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 27// For Emscripten, we let you use SDL_HINT_MAIN_CALLBACK_RATE, because it might be useful to drop it super-low for 28// things like loopwave that don't really do much but wait on the audio device, but be warned that browser timers 29// are super-unreliable in modern times, so you likely won't hit your desired callback rate with good precision. 30// Almost all apps should leave this alone, so we can use requestAnimationFrame, which is intended to run reliably 31// at the refresh rate of the user's display. 32static Uint32 callback_rate_increment = 0; 33static bool iterate_after_waitevent = false; 34static bool callback_rate_changed = false; 35static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue) 36{ 37 callback_rate_changed = true; 38 iterate_after_waitevent = newValue && (SDL_strcmp(newValue, "waitevent") == 0); 39 if (iterate_after_waitevent) { 40 callback_rate_increment = 0; 41 } else { 42 const double callback_rate = newValue ? SDL_atof(newValue) : 0.0; 43 if (callback_rate > 0.0) { 44 callback_rate_increment = (Uint32) SDL_NS_TO_MS((double) SDL_NS_PER_SECOND / callback_rate); 45 } else { 46 callback_rate_increment = 0; 47 } 48 } 49} 50 51// just tell us when any new event is pushed on the queue, so we can check a flag for "waitevent" mode. 52static bool saw_new_event = false; 53static bool SDLCALL EmscriptenMainCallbackEventWatcher(void *userdata, SDL_Event *event) 54{ 55 saw_new_event = true; 56 return true; 57} 58 59static void EmscriptenInternalMainloop(void) 60{ 61 // callback rate changed? Update emscripten's mainloop iteration speed. 62 if (callback_rate_changed) { 63 callback_rate_changed = false; 64 if (callback_rate_increment == 0) { 65 emscripten_set_main_loop_timing(EM_TIMING_RAF, 1); 66 } else { 67 emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, callback_rate_increment); 68 } 69 } 70 71 if (iterate_after_waitevent) { 72 SDL_PumpEvents(); 73 if (!saw_new_event) { 74 // do nothing yet. Note that we're still going to iterate here because we can't block, 75 // but we can stop the app's iteration from progressing until there's an event. 76 return; 77 } 78 saw_new_event = false; 79 } 80 81 const SDL_AppResult rc = SDL_IterateMainCallbacks(!iterate_after_waitevent); 82 if (rc != SDL_APP_CONTINUE) { 83 SDL_QuitMainCallbacks(rc); 84 emscripten_cancel_main_loop(); // kill" the mainloop, so it stops calling back into it. 85 exit((rc == SDL_APP_FAILURE) ? 1 : 0); // hopefully this takes down everything else, too. 86 } 87} 88 89int SDL_EnterAppMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit) 90{ 91 SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit); 92 if (rc == SDL_APP_CONTINUE) { 93 if (!SDL_AddEventWatch(EmscriptenMainCallbackEventWatcher, NULL)) { 94 rc = SDL_APP_FAILURE; 95 } else { 96 SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL); 97 callback_rate_changed = false; 98 emscripten_set_main_loop(EmscriptenInternalMainloop, 0, 0); // don't throw an exception since we do an orderly return. 99 if (callback_rate_increment > 0.0) { 100 emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, callback_rate_increment); 101 } 102 } 103 } else { 104 SDL_QuitMainCallbacks(rc); 105 } 106 return (rc == SDL_APP_FAILURE) ? 1 : 0; 107} 108 109[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.