SDL_quit.c 5.0 KB

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