SDL_main_callbacks.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. static SDL_AppEvent_func SDL_main_event_callback;
  21. static SDL_AppIterate_func SDL_main_iteration_callback;
  22. static SDL_AppQuit_func SDL_main_quit_callback;
  23. 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.
  24. static void *SDL_main_appstate = NULL;
  25. // Return true if this event needs to be processed before returning from the event watcher
  26. static SDL_bool ShouldDispatchImmediately(SDL_Event *event)
  27. {
  28. switch (event->type) {
  29. case SDL_EVENT_TERMINATING:
  30. case SDL_EVENT_LOW_MEMORY:
  31. case SDL_EVENT_WILL_ENTER_BACKGROUND:
  32. case SDL_EVENT_DID_ENTER_BACKGROUND:
  33. case SDL_EVENT_WILL_ENTER_FOREGROUND:
  34. case SDL_EVENT_DID_ENTER_FOREGROUND:
  35. return SDL_TRUE;
  36. default:
  37. return SDL_FALSE;
  38. }
  39. }
  40. static void SDL_DispatchMainCallbackEvent(SDL_Event *event)
  41. {
  42. if (SDL_AtomicGet(&apprc) == 0) { // if already quitting, don't send the event to the app.
  43. SDL_AtomicCompareAndSwap(&apprc, 0, SDL_main_event_callback(SDL_main_appstate, event));
  44. }
  45. }
  46. static void SDL_DispatchMainCallbackEvents(void)
  47. {
  48. SDL_Event events[16];
  49. for (;;) {
  50. int count = SDL_PeepEvents(events, SDL_arraysize(events), SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST);
  51. if (count <= 0) {
  52. break;
  53. }
  54. for (int i = 0; i < count; ++i) {
  55. SDL_Event *event = &events[i];
  56. if (!ShouldDispatchImmediately(event)) {
  57. SDL_DispatchMainCallbackEvent(event);
  58. }
  59. }
  60. }
  61. }
  62. static int SDLCALL SDL_MainCallbackEventWatcher(void *userdata, SDL_Event *event)
  63. {
  64. if (ShouldDispatchImmediately(event)) {
  65. // Make sure any currently queued events are processed then dispatch this before continuing
  66. SDL_DispatchMainCallbackEvents();
  67. SDL_DispatchMainCallbackEvent(event);
  68. } else {
  69. // We'll process this event later from the main event queue
  70. }
  71. return 0;
  72. }
  73. SDL_bool SDL_HasMainCallbacks(void)
  74. {
  75. if (SDL_main_iteration_callback) {
  76. return SDL_TRUE;
  77. }
  78. return SDL_FALSE;
  79. }
  80. int SDL_InitMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
  81. {
  82. SDL_main_iteration_callback = appiter;
  83. SDL_main_event_callback = appevent;
  84. SDL_main_quit_callback = appquit;
  85. SDL_AtomicSet(&apprc, 0);
  86. const int rc = appinit(&SDL_main_appstate, argc, argv);
  87. if (SDL_AtomicCompareAndSwap(&apprc, 0, rc) && (rc == 0)) { // bounce if SDL_AppInit already said abort, otherwise...
  88. // make sure we definitely have events initialized, even if the app didn't do it.
  89. if (SDL_InitSubSystem(SDL_INIT_EVENTS) == -1) {
  90. SDL_AtomicSet(&apprc, -1);
  91. return -1;
  92. }
  93. if (SDL_AddEventWatch(SDL_MainCallbackEventWatcher, NULL) < 0) {
  94. SDL_AtomicSet(&apprc, -1);
  95. return -1;
  96. }
  97. }
  98. return SDL_AtomicGet(&apprc);
  99. }
  100. int SDL_IterateMainCallbacks(SDL_bool pump_events)
  101. {
  102. if (pump_events) {
  103. SDL_PumpEvents();
  104. }
  105. SDL_DispatchMainCallbackEvents();
  106. int rc = SDL_AtomicGet(&apprc);
  107. if (rc == 0) {
  108. rc = SDL_main_iteration_callback(SDL_main_appstate);
  109. if (!SDL_AtomicCompareAndSwap(&apprc, 0, rc)) {
  110. rc = SDL_AtomicGet(&apprc); // something else already set a quit result, keep that.
  111. }
  112. }
  113. return rc;
  114. }
  115. void SDL_QuitMainCallbacks(void)
  116. {
  117. SDL_DelEventWatch(SDL_MainCallbackEventWatcher, NULL);
  118. SDL_main_quit_callback(SDL_main_appstate);
  119. SDL_main_appstate = NULL; // just in case.
  120. // for symmetry, you should explicitly Quit what you Init, but we might come through here uninitialized and SDL_Quit() will clear everything anyhow.
  121. //SDL_QuitSubSystem(SDL_INIT_EVENTS);
  122. SDL_Quit();
  123. }