src/events/SDL_quit.c
author Ozkan Sezer <sezeroz@gmail.com>
Sat, 24 Mar 2018 22:41:17 +0300
branchSDL-1.2
changeset 11972 0b5ac2b90ab1
parent 6137 4720145f848b
permissions -rw-r--r--
backfort fix for bug #3739: handle %lu, %li and %ld in SDL_SetError.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* General quit handling code for SDL */
slouken@0
    25
slouken@1330
    26
#ifdef HAVE_SIGNAL_H
slouken@0
    27
#include <signal.h>
slouken@0
    28
#endif
slouken@0
    29
slouken@0
    30
#include "SDL_events.h"
slouken@0
    31
#include "SDL_events_c.h"
slouken@0
    32
slouken@0
    33
slouken@1330
    34
#ifdef HAVE_SIGNAL_H
slouken@0
    35
static void SDL_HandleSIG(int sig)
slouken@0
    36
{
slouken@0
    37
	/* Reset the signal handler */
slouken@0
    38
	signal(sig, SDL_HandleSIG);
slouken@0
    39
slouken@0
    40
	/* Signal a quit interrupt */
slouken@0
    41
	SDL_PrivateQuit();
slouken@0
    42
}
slouken@1330
    43
#endif /* HAVE_SIGNAL_H */
slouken@0
    44
slouken@0
    45
/* Public functions */
slouken@0
    46
int SDL_QuitInit(void)
slouken@0
    47
{
slouken@5492
    48
#ifdef HAVE_SIGACTION
slouken@5492
    49
	struct sigaction action;
slouken@5492
    50
	sigaction(SIGINT, NULL, &action);
patmandin@5565
    51
#  ifdef HAVE_SA_SIGACTION
slouken@5500
    52
	if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
patmandin@5565
    53
#  else
patmandin@5565
    54
	if ( action.sa_handler == SIG_DFL ) {
patmandin@5565
    55
#  endif
slouken@5492
    56
		action.sa_handler = SDL_HandleSIG;
slouken@5492
    57
		sigaction(SIGINT, &action, NULL);
slouken@5492
    58
	}
slouken@5492
    59
	sigaction(SIGTERM, NULL, &action);
patmandin@5565
    60
#  ifdef HAVE_SA_SIGACTION
slouken@5500
    61
	if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
patmandin@5565
    62
#  else
patmandin@5565
    63
	if ( action.sa_handler == SIG_DFL ) {
patmandin@5565
    64
#  endif
slouken@5492
    65
		action.sa_handler = SDL_HandleSIG;
slouken@5492
    66
		sigaction(SIGTERM, &action, NULL);
slouken@5492
    67
	}
slouken@5492
    68
#elif HAVE_SIGNAL_H
slouken@0
    69
	void (*ohandler)(int);
slouken@0
    70
slouken@0
    71
	/* Both SIGINT and SIGTERM are translated into quit interrupts */
slouken@1123
    72
	ohandler = signal(SIGINT, SDL_HandleSIG);
slouken@0
    73
	if ( ohandler != SIG_DFL )
slouken@0
    74
		signal(SIGINT, ohandler);
slouken@0
    75
	ohandler = signal(SIGTERM, SDL_HandleSIG);
slouken@0
    76
	if ( ohandler != SIG_DFL )
slouken@0
    77
		signal(SIGTERM, ohandler);
slouken@1330
    78
#endif /* HAVE_SIGNAL_H */
slouken@0
    79
slouken@0
    80
	/* That's it! */
slouken@0
    81
	return(0);
slouken@0
    82
}
slouken@1123
    83
void SDL_QuitQuit(void)
slouken@1123
    84
{
slouken@5492
    85
#ifdef HAVE_SIGACTION
slouken@5492
    86
	struct sigaction action;
slouken@5492
    87
	sigaction(SIGINT, NULL, &action);
slouken@5492
    88
	if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5492
    89
		action.sa_handler = SIG_DFL;
slouken@5492
    90
		sigaction(SIGINT, &action, NULL);
slouken@5492
    91
	}
slouken@5492
    92
	sigaction(SIGTERM, NULL, &action);
slouken@5492
    93
	if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5492
    94
		action.sa_handler = SIG_DFL;
slouken@5492
    95
		sigaction(SIGTERM, &action, NULL);
slouken@5492
    96
	}
slouken@5492
    97
#elif HAVE_SIGNAL_H
slouken@1123
    98
	void (*ohandler)(int);
slouken@1123
    99
slouken@1123
   100
	ohandler = signal(SIGINT, SIG_DFL);
slouken@1123
   101
	if ( ohandler != SDL_HandleSIG )
slouken@1123
   102
		signal(SIGINT, ohandler);
slouken@1123
   103
	ohandler = signal(SIGTERM, SIG_DFL);
slouken@1123
   104
	if ( ohandler != SDL_HandleSIG )
slouken@1123
   105
		signal(SIGTERM, ohandler);
slouken@1330
   106
#endif /* HAVE_SIGNAL_H */
slouken@1123
   107
}
slouken@0
   108
slouken@0
   109
/* This function returns 1 if it's okay to close the application window */
slouken@0
   110
int SDL_PrivateQuit(void)
slouken@0
   111
{
slouken@0
   112
	int posted;
slouken@0
   113
slouken@0
   114
	posted = 0;
slouken@0
   115
	if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) {
slouken@0
   116
		SDL_Event event;
slouken@0
   117
		event.type = SDL_QUIT;
slouken@0
   118
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
   119
			posted = 1;
slouken@0
   120
			SDL_PushEvent(&event);
slouken@0
   121
		}
slouken@0
   122
	}
slouken@0
   123
	return(posted);
slouken@0
   124
}