SDL_quit.c 5.0 KB

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