Skip to content

Latest commit

 

History

History
209 lines (178 loc) · 5.03 KB

SDL_quit.c

File metadata and controls

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