SDL_sysmain_callbacks.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "../SDL_main_callbacks.h"
  20. #include <emscripten.h>
  21. // For Emscripten, we let you use SDL_HINT_MAIN_CALLBACK_RATE, because it might be useful to drop it super-low for
  22. // things like loopwave that don't really do much but wait on the audio device, but be warned that browser timers
  23. // are super-unreliable in modern times, so you likely won't hit your desired callback rate with good precision.
  24. // Almost all apps should leave this alone, so we can use requestAnimationFrame, which is intended to run reliably
  25. // at the refresh rate of the user's display.
  26. static Uint32 callback_rate_increment = 0;
  27. static bool iterate_after_waitevent = false;
  28. static bool callback_rate_changed = false;
  29. static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue)
  30. {
  31. callback_rate_changed = true;
  32. iterate_after_waitevent = newValue && (SDL_strcmp(newValue, "waitevent") == 0);
  33. if (iterate_after_waitevent) {
  34. callback_rate_increment = 0;
  35. } else {
  36. const double callback_rate = newValue ? SDL_atof(newValue) : 0.0;
  37. if (callback_rate > 0.0) {
  38. callback_rate_increment = (Uint32) SDL_NS_TO_MS((double) SDL_NS_PER_SECOND / callback_rate);
  39. } else {
  40. callback_rate_increment = 0;
  41. }
  42. }
  43. }
  44. // just tell us when any new event is pushed on the queue, so we can check a flag for "waitevent" mode.
  45. static bool saw_new_event = false;
  46. static bool SDLCALL EmscriptenMainCallbackEventWatcher(void *userdata, SDL_Event *event)
  47. {
  48. saw_new_event = true;
  49. return true;
  50. }
  51. static void EmscriptenInternalMainloop(void)
  52. {
  53. // callback rate changed? Update emscripten's mainloop iteration speed.
  54. if (callback_rate_changed) {
  55. callback_rate_changed = false;
  56. if (callback_rate_increment == 0) {
  57. emscripten_set_main_loop_timing(EM_TIMING_RAF, 1);
  58. } else {
  59. emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, callback_rate_increment);
  60. }
  61. }
  62. if (iterate_after_waitevent) {
  63. SDL_PumpEvents();
  64. if (!saw_new_event) {
  65. // do nothing yet. Note that we're still going to iterate here because we can't block,
  66. // but we can stop the app's iteration from progressing until there's an event.
  67. return;
  68. }
  69. saw_new_event = false;
  70. }
  71. const SDL_AppResult rc = SDL_IterateMainCallbacks(!iterate_after_waitevent);
  72. if (rc != SDL_APP_CONTINUE) {
  73. SDL_QuitMainCallbacks(rc);
  74. emscripten_cancel_main_loop(); // kill" the mainloop, so it stops calling back into it.
  75. exit((rc == SDL_APP_FAILURE) ? 1 : 0); // hopefully this takes down everything else, too.
  76. }
  77. }
  78. int SDL_EnterAppMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
  79. {
  80. SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
  81. if (rc == SDL_APP_CONTINUE) {
  82. if (!SDL_AddEventWatch(EmscriptenMainCallbackEventWatcher, NULL)) {
  83. rc = SDL_APP_FAILURE;
  84. } else {
  85. SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
  86. callback_rate_changed = false;
  87. emscripten_set_main_loop(EmscriptenInternalMainloop, 0, 0); // don't throw an exception since we do an orderly return.
  88. if (callback_rate_increment > 0.0) {
  89. emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, callback_rate_increment);
  90. }
  91. }
  92. } else {
  93. SDL_QuitMainCallbacks(rc);
  94. }
  95. return (rc == SDL_APP_FAILURE) ? 1 : 0;
  96. }