SDL_quit.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. /* General quit handling code for SDL */
  20. #ifdef HAVE_SIGNAL_H
  21. #include <signal.h>
  22. #endif
  23. #include "SDL_events_c.h"
  24. #if defined(HAVE_SIGNAL_H) || defined(HAVE_SIGACTION)
  25. #define HAVE_SIGNAL_SUPPORT 1
  26. #endif
  27. #ifdef HAVE_SIGNAL_SUPPORT
  28. static SDL_bool disable_signals = SDL_FALSE;
  29. static SDL_bool send_quit_pending = SDL_FALSE;
  30. #ifdef SDL_BACKGROUNDING_SIGNAL
  31. static SDL_bool send_backgrounding_pending = SDL_FALSE;
  32. #endif
  33. #ifdef SDL_FOREGROUNDING_SIGNAL
  34. static SDL_bool send_foregrounding_pending = SDL_FALSE;
  35. #endif
  36. static void SDL_HandleSIG(int sig)
  37. {
  38. /* Reset the signal handler */
  39. (void)signal(sig, SDL_HandleSIG);
  40. /* Send a quit event next time the event loop pumps. */
  41. /* We can't send it in signal handler; SDL_malloc() might be interrupted! */
  42. if ((sig == SIGINT) || (sig == SIGTERM)) {
  43. send_quit_pending = SDL_TRUE;
  44. }
  45. #ifdef SDL_BACKGROUNDING_SIGNAL
  46. else if (sig == SDL_BACKGROUNDING_SIGNAL) {
  47. send_backgrounding_pending = SDL_TRUE;
  48. }
  49. #endif
  50. #ifdef SDL_FOREGROUNDING_SIGNAL
  51. else if (sig == SDL_FOREGROUNDING_SIGNAL) {
  52. send_foregrounding_pending = SDL_TRUE;
  53. }
  54. #endif
  55. }
  56. static void SDL_EventSignal_Init(const int sig)
  57. {
  58. #ifdef HAVE_SIGACTION
  59. struct sigaction action;
  60. sigaction(sig, NULL, &action);
  61. #ifdef HAVE_SA_SIGACTION
  62. if (action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL) {
  63. #else
  64. if (action.sa_handler == SIG_DFL) {
  65. #endif
  66. action.sa_handler = SDL_HandleSIG;
  67. sigaction(sig, &action, NULL);
  68. }
  69. #elif HAVE_SIGNAL_H
  70. void (*ohandler)(int) = signal(sig, SDL_HandleSIG);
  71. if (ohandler != SIG_DFL) {
  72. signal(sig, ohandler);
  73. }
  74. #endif
  75. }
  76. static void SDL_EventSignal_Quit(const int sig)
  77. {
  78. #ifdef HAVE_SIGACTION
  79. struct sigaction action;
  80. sigaction(sig, NULL, &action);
  81. if (action.sa_handler == SDL_HandleSIG) {
  82. action.sa_handler = SIG_DFL;
  83. sigaction(sig, &action, NULL);
  84. }
  85. #elif HAVE_SIGNAL_H
  86. void (*ohandler)(int) = signal(sig, SIG_DFL);
  87. if (ohandler != SDL_HandleSIG) {
  88. signal(sig, ohandler);
  89. }
  90. #endif /* HAVE_SIGNAL_H */
  91. }
  92. /* Public functions */
  93. static int SDL_QuitInit_Internal(void)
  94. {
  95. /* Both SIGINT and SIGTERM are translated into quit interrupts */
  96. /* and SDL can be built to simulate iOS/Android semantics with arbitrary signals. */
  97. SDL_EventSignal_Init(SIGINT);
  98. SDL_EventSignal_Init(SIGTERM);
  99. #ifdef SDL_BACKGROUNDING_SIGNAL
  100. SDL_EventSignal_Init(SDL_BACKGROUNDING_SIGNAL);
  101. #endif
  102. #ifdef SDL_FOREGROUNDING_SIGNAL
  103. SDL_EventSignal_Init(SDL_FOREGROUNDING_SIGNAL);
  104. #endif
  105. /* That's it! */
  106. return 0;
  107. }
  108. static void SDL_QuitQuit_Internal(void)
  109. {
  110. SDL_EventSignal_Quit(SIGINT);
  111. SDL_EventSignal_Quit(SIGTERM);
  112. #ifdef SDL_BACKGROUNDING_SIGNAL
  113. SDL_EventSignal_Quit(SDL_BACKGROUNDING_SIGNAL);
  114. #endif
  115. #ifdef SDL_FOREGROUNDING_SIGNAL
  116. SDL_EventSignal_Quit(SDL_FOREGROUNDING_SIGNAL);
  117. #endif
  118. }
  119. #endif
  120. int SDL_InitQuit(void)
  121. {
  122. #ifdef HAVE_SIGNAL_SUPPORT
  123. if (!SDL_GetHintBoolean(SDL_HINT_NO_SIGNAL_HANDLERS, SDL_FALSE)) {
  124. return SDL_QuitInit_Internal();
  125. }
  126. #endif
  127. return 0;
  128. }
  129. void SDL_QuitQuit(void)
  130. {
  131. #ifdef HAVE_SIGNAL_SUPPORT
  132. if (!disable_signals) {
  133. SDL_QuitQuit_Internal();
  134. }
  135. #endif
  136. }
  137. void SDL_SendPendingSignalEvents(void)
  138. {
  139. #ifdef HAVE_SIGNAL_SUPPORT
  140. if (send_quit_pending) {
  141. SDL_SendQuit();
  142. SDL_assert(!send_quit_pending);
  143. }
  144. #ifdef SDL_BACKGROUNDING_SIGNAL
  145. if (send_backgrounding_pending) {
  146. send_backgrounding_pending = SDL_FALSE;
  147. SDL_OnApplicationWillResignActive();
  148. }
  149. #endif
  150. #ifdef SDL_FOREGROUNDING_SIGNAL
  151. if (send_foregrounding_pending) {
  152. send_foregrounding_pending = SDL_FALSE;
  153. SDL_OnApplicationDidBecomeActive();
  154. }
  155. #endif
  156. #endif
  157. }
  158. /* This function returns 1 if it's okay to close the application window */
  159. int SDL_SendQuit(void)
  160. {
  161. #ifdef HAVE_SIGNAL_SUPPORT
  162. send_quit_pending = SDL_FALSE;
  163. #endif
  164. return SDL_SendAppEvent(SDL_EVENT_QUIT);
  165. }