src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1402 d910939febfa
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@0
    22
slouken@0
    23
/* General quit handling code for SDL */
slouken@0
    24
slouken@1330
    25
#include "SDL_config.h"
icculus@1152
    26
slouken@1330
    27
#ifdef HAVE_SIGNAL_H
slouken@0
    28
#include <signal.h>
slouken@0
    29
#endif
slouken@0
    30
slouken@0
    31
#include "SDL_events.h"
slouken@0
    32
#include "SDL_events_c.h"
slouken@0
    33
slouken@0
    34
slouken@1330
    35
#ifdef HAVE_SIGNAL_H
slouken@0
    36
static void SDL_HandleSIG(int sig)
slouken@0
    37
{
slouken@0
    38
	/* Reset the signal handler */
slouken@0
    39
	signal(sig, SDL_HandleSIG);
slouken@0
    40
slouken@0
    41
	/* Signal a quit interrupt */
slouken@0
    42
	SDL_PrivateQuit();
slouken@0
    43
}
slouken@1330
    44
#endif /* HAVE_SIGNAL_H */
slouken@0
    45
slouken@0
    46
/* Public functions */
slouken@0
    47
int SDL_QuitInit(void)
slouken@0
    48
{
slouken@1330
    49
#ifdef HAVE_SIGNAL_H
slouken@0
    50
	void (*ohandler)(int);
slouken@0
    51
slouken@0
    52
	/* Both SIGINT and SIGTERM are translated into quit interrupts */
slouken@1123
    53
	ohandler = signal(SIGINT, SDL_HandleSIG);
slouken@0
    54
	if ( ohandler != SIG_DFL )
slouken@0
    55
		signal(SIGINT, ohandler);
slouken@0
    56
	ohandler = signal(SIGTERM, SDL_HandleSIG);
slouken@0
    57
	if ( ohandler != SIG_DFL )
slouken@0
    58
		signal(SIGTERM, ohandler);
slouken@1330
    59
#endif /* HAVE_SIGNAL_H */
slouken@0
    60
slouken@0
    61
	/* That's it! */
slouken@0
    62
	return(0);
slouken@0
    63
}
slouken@1123
    64
void SDL_QuitQuit(void)
slouken@1123
    65
{
slouken@1330
    66
#ifdef HAVE_SIGNAL_H
slouken@1123
    67
	void (*ohandler)(int);
slouken@1123
    68
slouken@1123
    69
	ohandler = signal(SIGINT, SIG_DFL);
slouken@1123
    70
	if ( ohandler != SDL_HandleSIG )
slouken@1123
    71
		signal(SIGINT, ohandler);
slouken@1123
    72
	ohandler = signal(SIGTERM, SIG_DFL);
slouken@1123
    73
	if ( ohandler != SDL_HandleSIG )
slouken@1123
    74
		signal(SIGTERM, ohandler);
slouken@1330
    75
#endif /* HAVE_SIGNAL_H */
slouken@1123
    76
}
slouken@0
    77
slouken@0
    78
/* This function returns 1 if it's okay to close the application window */
slouken@0
    79
int SDL_PrivateQuit(void)
slouken@0
    80
{
slouken@0
    81
	int posted;
slouken@0
    82
slouken@0
    83
	posted = 0;
slouken@0
    84
	if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) {
slouken@0
    85
		SDL_Event event;
slouken@0
    86
		event.type = SDL_QUIT;
slouken@0
    87
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
    88
			posted = 1;
slouken@0
    89
			SDL_PushEvent(&event);
slouken@0
    90
		}
slouken@0
    91
	}
slouken@0
    92
	return(posted);
slouken@0
    93
}