SDL_sysmain_callbacks.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "../../video/SDL_sysvideo.h"
  21. #ifndef SDL_PLATFORM_IOS
  22. static int callback_rate_increment = 0;
  23. static bool iterate_after_waitevent = false;
  24. static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue)
  25. {
  26. iterate_after_waitevent = newValue && (SDL_strcmp(newValue, "waitevent") == 0);
  27. if (iterate_after_waitevent) {
  28. callback_rate_increment = 0;
  29. } else {
  30. const int callback_rate = newValue ? SDL_atoi(newValue) : 0;
  31. if (callback_rate > 0) {
  32. callback_rate_increment = ((Uint64) 1000000000) / ((Uint64) callback_rate);
  33. } else {
  34. callback_rate_increment = 0;
  35. }
  36. }
  37. }
  38. static SDL_AppResult GenericIterateMainCallbacks(void)
  39. {
  40. if (iterate_after_waitevent) {
  41. SDL_WaitEvent(NULL);
  42. }
  43. return SDL_IterateMainCallbacks(!iterate_after_waitevent);
  44. }
  45. int SDL_EnterAppMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
  46. {
  47. SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
  48. if (rc == 0) {
  49. SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
  50. Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0;
  51. while ((rc = GenericIterateMainCallbacks()) == SDL_APP_CONTINUE) {
  52. // !!! FIXME: this can be made more complicated if we decide to
  53. // !!! FIXME: optionally hand off callback responsibility to the
  54. // !!! FIXME: video subsystem (for example, if Wayland has a
  55. // !!! FIXME: protocol to drive an animation loop, maybe we hand
  56. // !!! FIXME: off to them here if/when the video subsystem becomes
  57. // !!! FIXME: initialized).
  58. // Try to run at whatever rate the hint requested. This makes this
  59. // not eat all the CPU in simple things like loopwave. By
  60. // default, we run as fast as possible, which means we'll clamp to
  61. // vsync in common cases, and won't be restrained to vsync if the
  62. // app is doing a benchmark or doesn't want to be, based on how
  63. // they've set up that window.
  64. if (callback_rate_increment == 0) {
  65. next_iteration = 0; // just clear the timer and run at the pace the video subsystem allows.
  66. } else {
  67. const Uint64 now = SDL_GetTicksNS();
  68. if (next_iteration > now) { // Running faster than the limit, sleep a little.
  69. SDL_DelayPrecise(next_iteration - now);
  70. } else {
  71. next_iteration = now; // if running behind, reset the timer. If right on time, `next_iteration` already equals `now`.
  72. }
  73. next_iteration += callback_rate_increment;
  74. }
  75. }
  76. SDL_RemoveHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
  77. }
  78. SDL_QuitMainCallbacks(rc);
  79. return (rc == SDL_APP_FAILURE) ? 1 : 0;
  80. }
  81. #endif // !SDL_PLATFORM_IOS