SDL 2.0
SDL_quit.c
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "../SDL_internal.h"
22#include "SDL_hints.h"
23#include "SDL_assert.h"
24
25/* General quit handling code for SDL */
26
27#ifdef HAVE_SIGNAL_H
28#include <signal.h>
29#endif
30
31#include "SDL_events.h"
32#include "SDL_events_c.h"
33
34#if defined(HAVE_SIGNAL_H) || defined(HAVE_SIGACTION)
35#define HAVE_SIGNAL_SUPPORT 1
36#endif
37
38#ifdef HAVE_SIGNAL_SUPPORT
41
42#ifdef SDL_BACKGROUNDING_SIGNAL
43static SDL_bool send_backgrounding_pending = SDL_FALSE;
44#endif
45
46#ifdef SDL_FOREGROUNDING_SIGNAL
47static SDL_bool send_foregrounding_pending = SDL_FALSE;
48#endif
49
50static void
52{
53 /* Reset the signal handler */
54 signal(sig, SDL_HandleSIG);
55
56 /* Send a quit event next time the event loop pumps. */
57 /* We can't send it in signal handler; malloc() might be interrupted! */
58 if ((sig == SIGINT) || (sig == SIGTERM)) {
60 }
61
62 #ifdef SDL_BACKGROUNDING_SIGNAL
63 else if (sig == SDL_BACKGROUNDING_SIGNAL) {
64 send_backgrounding_pending = SDL_TRUE;
65 }
66 #endif
67
68 #ifdef SDL_FOREGROUNDING_SIGNAL
69 else if (sig == SDL_FOREGROUNDING_SIGNAL) {
70 send_foregrounding_pending = SDL_TRUE;
71 }
72 #endif
73}
74
75static void
77{
78#ifdef HAVE_SIGACTION
79 struct sigaction action;
80
81 sigaction(sig, NULL, &action);
82#ifdef HAVE_SA_SIGACTION
83 if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
84#else
85 if ( action.sa_handler == SIG_DFL ) {
86#endif
87 action.sa_handler = SDL_HandleSIG;
88 sigaction(sig, &action, NULL);
89 }
90#elif HAVE_SIGNAL_H
91 void (*ohandler) (int) = signal(sig, SDL_HandleSIG);
92 if (ohandler != SIG_DFL) {
93 signal(sig, ohandler);
94 }
95#endif
96}
97
98static void
100{
101#ifdef HAVE_SIGACTION
102 struct sigaction action;
103 sigaction(sig, NULL, &action);
104 if ( action.sa_handler == SDL_HandleSIG ) {
105 action.sa_handler = SIG_DFL;
106 sigaction(sig, &action, NULL);
107 }
108#elif HAVE_SIGNAL_H
109 void (*ohandler) (int) = signal(sig, SIG_DFL);
110 if (ohandler != SDL_HandleSIG) {
111 signal(sig, ohandler);
112 }
113#endif /* HAVE_SIGNAL_H */
114}
115
116/* Public functions */
117static int
119{
120 /* Both SIGINT and SIGTERM are translated into quit interrupts */
121 /* and SDL can be built to simulate iOS/Android semantics with arbitrary signals. */
122 SDL_EventSignal_Init(SIGINT);
123 SDL_EventSignal_Init(SIGTERM);
124
125 #ifdef SDL_BACKGROUNDING_SIGNAL
126 SDL_EventSignal_Init(SDL_BACKGROUNDING_SIGNAL);
127 #endif
128
129 #ifdef SDL_FOREGROUNDING_SIGNAL
130 SDL_EventSignal_Init(SDL_FOREGROUNDING_SIGNAL);
131 #endif
132
133 /* That's it! */
134 return 0;
135}
136
137static void
139{
140 SDL_EventSignal_Quit(SIGINT);
141 SDL_EventSignal_Quit(SIGTERM);
142
143 #ifdef SDL_BACKGROUNDING_SIGNAL
144 SDL_EventSignal_Quit(SDL_BACKGROUNDING_SIGNAL);
145 #endif
146
147 #ifdef SDL_FOREGROUNDING_SIGNAL
148 SDL_EventSignal_Quit(SDL_FOREGROUNDING_SIGNAL);
149 #endif
150}
151#endif
152
153int
155{
156#ifdef HAVE_SIGNAL_SUPPORT
158 return SDL_QuitInit_Internal();
159 }
160#endif
161 return 0;
162}
163
164void
166{
167#ifdef HAVE_SIGNAL_SUPPORT
168 if (!disable_signals) {
170 }
171#endif
172}
173
174void
176{
177#ifdef HAVE_SIGNAL_SUPPORT
178 if (send_quit_pending) {
179 SDL_SendQuit();
181 }
182
183 #ifdef SDL_BACKGROUNDING_SIGNAL
184 if (send_backgrounding_pending) {
185 send_backgrounding_pending = SDL_FALSE;
187 }
188 #endif
189
190 #ifdef SDL_FOREGROUNDING_SIGNAL
191 if (send_foregrounding_pending) {
192 send_foregrounding_pending = SDL_FALSE;
194 }
195 #endif
196#endif
197}
198
199/* This function returns 1 if it's okay to close the application window */
200int
202{
203#ifdef HAVE_SIGNAL_SUPPORT
205#endif
207}
208
209/* vi: set ts=4 sw=4 expandtab: */
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define SDL_GetHintBoolean
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
int SDL_SendAppEvent(SDL_EventType eventType)
Definition: SDL_events.c:965
@ SDL_QUIT
Definition: SDL_events.h:60
#define SDL_HINT_NO_SIGNAL_HANDLERS
Tell SDL not to catch the SIGINT or SIGTERM signals.
Definition: SDL_hints.h:923
static SDL_bool disable_signals
Definition: SDL_quit.c:39
static void SDL_EventSignal_Init(const int sig)
Definition: SDL_quit.c:76
static SDL_bool send_quit_pending
Definition: SDL_quit.c:40
static void SDL_EventSignal_Quit(const int sig)
Definition: SDL_quit.c:99
static void SDL_QuitQuit_Internal(void)
Definition: SDL_quit.c:138
void SDL_SendPendingSignalEvents(void)
Definition: SDL_quit.c:175
int SDL_QuitInit(void)
Definition: SDL_quit.c:154
static int SDL_QuitInit_Internal(void)
Definition: SDL_quit.c:118
static void SDL_HandleSIG(int sig)
Definition: SDL_quit.c:51
void SDL_QuitQuit(void)
Definition: SDL_quit.c:165
int SDL_SendQuit(void)
Definition: SDL_quit.c:201
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
void SDL_OnApplicationDidBecomeActive(void)
Definition: SDL_video.c:4061
void SDL_OnApplicationWillResignActive(void)
Definition: SDL_video.c:4039
#define NULL
Definition: begin_code.h:167