src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Mar 2011 13:33:29 -0700
branchSDL-1.2
changeset 5500 27348c0ae529
parent 5492 b214b7ce9b00
child 5565 4225f09eb8cb
permissions -rw-r--r--
Fixed compiler warning
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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);
slouken@5500
    51
	if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
slouken@5492
    52
		action.sa_handler = SDL_HandleSIG;
slouken@5492
    53
		sigaction(SIGINT, &action, NULL);
slouken@5492
    54
	}
slouken@5492
    55
	sigaction(SIGTERM, NULL, &action);
slouken@5500
    56
	if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
slouken@5492
    57
		action.sa_handler = SDL_HandleSIG;
slouken@5492
    58
		sigaction(SIGTERM, &action, NULL);
slouken@5492
    59
	}
slouken@5492
    60
#elif HAVE_SIGNAL_H
slouken@0
    61
	void (*ohandler)(int);
slouken@0
    62
slouken@0
    63
	/* Both SIGINT and SIGTERM are translated into quit interrupts */
slouken@1123
    64
	ohandler = signal(SIGINT, SDL_HandleSIG);
slouken@0
    65
	if ( ohandler != SIG_DFL )
slouken@0
    66
		signal(SIGINT, ohandler);
slouken@0
    67
	ohandler = signal(SIGTERM, SDL_HandleSIG);
slouken@0
    68
	if ( ohandler != SIG_DFL )
slouken@0
    69
		signal(SIGTERM, ohandler);
slouken@1330
    70
#endif /* HAVE_SIGNAL_H */
slouken@0
    71
slouken@0
    72
	/* That's it! */
slouken@0
    73
	return(0);
slouken@0
    74
}
slouken@1123
    75
void SDL_QuitQuit(void)
slouken@1123
    76
{
slouken@5492
    77
#ifdef HAVE_SIGACTION
slouken@5492
    78
	struct sigaction action;
slouken@5492
    79
	sigaction(SIGINT, NULL, &action);
slouken@5492
    80
	if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5492
    81
		action.sa_handler = SIG_DFL;
slouken@5492
    82
		sigaction(SIGINT, &action, NULL);
slouken@5492
    83
	}
slouken@5492
    84
	sigaction(SIGTERM, NULL, &action);
slouken@5492
    85
	if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5492
    86
		action.sa_handler = SIG_DFL;
slouken@5492
    87
		sigaction(SIGTERM, &action, NULL);
slouken@5492
    88
	}
slouken@5492
    89
#elif HAVE_SIGNAL_H
slouken@1123
    90
	void (*ohandler)(int);
slouken@1123
    91
slouken@1123
    92
	ohandler = signal(SIGINT, SIG_DFL);
slouken@1123
    93
	if ( ohandler != SDL_HandleSIG )
slouken@1123
    94
		signal(SIGINT, ohandler);
slouken@1123
    95
	ohandler = signal(SIGTERM, SIG_DFL);
slouken@1123
    96
	if ( ohandler != SDL_HandleSIG )
slouken@1123
    97
		signal(SIGTERM, ohandler);
slouken@1330
    98
#endif /* HAVE_SIGNAL_H */
slouken@1123
    99
}
slouken@0
   100
slouken@0
   101
/* This function returns 1 if it's okay to close the application window */
slouken@0
   102
int SDL_PrivateQuit(void)
slouken@0
   103
{
slouken@0
   104
	int posted;
slouken@0
   105
slouken@0
   106
	posted = 0;
slouken@0
   107
	if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) {
slouken@0
   108
		SDL_Event event;
slouken@0
   109
		event.type = SDL_QUIT;
slouken@0
   110
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
   111
			posted = 1;
slouken@0
   112
			SDL_PushEvent(&event);
slouken@0
   113
		}
slouken@0
   114
	}
slouken@0
   115
	return(posted);
slouken@0
   116
}