SDL_quit.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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"
static SDL_bool disable_signals = SDL_FALSE;
static SDL_bool send_quit_pending = SDL_FALSE;
#ifdef HAVE_SIGNAL_H
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! */
send_quit_pending = SDL_TRUE;
}
#endif /* HAVE_SIGNAL_H */
/* Public functions */
static int
SDL_QuitInit_Internal(void)
{
#ifdef HAVE_SIGACTION
struct sigaction action;
sigaction(SIGINT, NULL, &action);
#ifdef HAVE_SA_SIGACTION
if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
#else
if ( action.sa_handler == SIG_DFL ) {
#endif
action.sa_handler = SDL_HandleSIG;
sigaction(SIGINT, &action, NULL);
}
sigaction(SIGTERM, NULL, &action);
#ifdef HAVE_SA_SIGACTION
if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
#else
if ( action.sa_handler == SIG_DFL ) {
#endif
action.sa_handler = SDL_HandleSIG;
sigaction(SIGTERM, &action, NULL);
}
#elif HAVE_SIGNAL_H
void (*ohandler) (int);
/* Both SIGINT and SIGTERM are translated into quit interrupts */
ohandler = signal(SIGINT, SDL_HandleSIG);
if (ohandler != SIG_DFL)
signal(SIGINT, ohandler);
ohandler = signal(SIGTERM, SDL_HandleSIG);
if (ohandler != SIG_DFL)
signal(SIGTERM, ohandler);
#endif /* HAVE_SIGNAL_H */
/* That's it! */
return 0;
}
int
SDL_QuitInit(void)
{
if (!SDL_GetHintBoolean(SDL_HINT_NO_SIGNAL_HANDLERS, SDL_FALSE)) {
return SDL_QuitInit_Internal();
}
return 0;
}
static void
SDL_QuitQuit_Internal(void)
{
#ifdef HAVE_SIGACTION
struct sigaction action;
sigaction(SIGINT, NULL, &action);
if ( action.sa_handler == SDL_HandleSIG ) {
action.sa_handler = SIG_DFL;
sigaction(SIGINT, &action, NULL);
}
sigaction(SIGTERM, NULL, &action);
if ( action.sa_handler == SDL_HandleSIG ) {
action.sa_handler = SIG_DFL;
sigaction(SIGTERM, &action, NULL);
}
#elif HAVE_SIGNAL_H
void (*ohandler) (int);
ohandler = signal(SIGINT, SIG_DFL);
if (ohandler != SDL_HandleSIG)
signal(SIGINT, ohandler);
ohandler = signal(SIGTERM, SIG_DFL);
if (ohandler != SDL_HandleSIG)
signal(SIGTERM, ohandler);
#endif /* HAVE_SIGNAL_H */
}
void
SDL_QuitQuit(void)
{
if (!disable_signals) {
SDL_QuitQuit_Internal();
}
}
/* This function returns 1 if it's okay to close the application window */
int
SDL_SendQuit(void)
{
send_quit_pending = SDL_FALSE;
return SDL_SendAppEvent(SDL_QUIT);
}
void
SDL_SendPendingQuit(void)
{
if (send_quit_pending) {
SDL_SendQuit();
SDL_assert(!send_quit_pending);
}
}
/* vi: set ts=4 sw=4 expandtab: */
SDL_TRUE
Definition: SDL_stdinc.h:142
SDL_HandleSIG
static void SDL_HandleSIG(int sig)
Definition: SDL_quit.c:39
SDL_FALSE
Definition: SDL_stdinc.h:141
SDL_QUIT
Definition: SDL_events.h:60
SDL_events.h
SDL_QuitQuit
void SDL_QuitQuit(void)
Definition: SDL_quit.c:128
disable_signals
static SDL_bool disable_signals
Definition: SDL_quit.c:34
SDL_GetHintBoolean
#define SDL_GetHintBoolean
Definition: SDL_dynapi_overrides.h:608
send_quit_pending
static SDL_bool send_quit_pending
Definition: SDL_quit.c:35
SDL_SendQuit
int SDL_SendQuit(void)
Definition: SDL_quit.c:137
SDL_hints.h
SDL_SendPendingQuit
void SDL_SendPendingQuit(void)
Definition: SDL_quit.c:144
SDL_QuitQuit_Internal
static void SDL_QuitQuit_Internal(void)
Definition: SDL_quit.c:101
SDL_QuitInit
int SDL_QuitInit(void)
Definition: SDL_quit.c:92
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDL_events_c.h
NULL
#define NULL
Definition: begin_code.h:164
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:139
SDL_HINT_NO_SIGNAL_HANDLERS
#define SDL_HINT_NO_SIGNAL_HANDLERS
Tell SDL not to catch the SIGINT or SIGTERM signals.
Definition: SDL_hints.h:791
void
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
Definition: SDL_dynapi_procs.h:89
SDL_SendAppEvent
int SDL_SendAppEvent(SDL_EventType eventType)
Definition: SDL_events.c:919
SDL_QuitInit_Internal
static int SDL_QuitInit_Internal(void)
Definition: SDL_quit.c:52
SDL_assert.h