src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1152 51a8702d8ecd
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
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
icculus@1152
    25
#if defined (_WIN32_WCE)
icculus@1152
    26
#define NO_SIGNAL_H
icculus@1152
    27
#endif
icculus@1152
    28
slouken@0
    29
#include <stdio.h>
slouken@0
    30
#ifndef NO_SIGNAL_H
slouken@0
    31
#include <signal.h>
slouken@0
    32
#endif
slouken@0
    33
slouken@0
    34
#include "SDL_events.h"
slouken@0
    35
#include "SDL_events_c.h"
slouken@0
    36
slouken@0
    37
slouken@0
    38
#ifndef NO_SIGNAL_H
slouken@0
    39
static void SDL_HandleSIG(int sig)
slouken@0
    40
{
slouken@0
    41
	/* Reset the signal handler */
slouken@0
    42
	signal(sig, SDL_HandleSIG);
slouken@0
    43
slouken@0
    44
	/* Signal a quit interrupt */
slouken@0
    45
	SDL_PrivateQuit();
slouken@0
    46
}
slouken@0
    47
#endif /* NO_SIGNAL_H */
slouken@0
    48
slouken@0
    49
/* Public functions */
slouken@0
    50
int SDL_QuitInit(void)
slouken@0
    51
{
slouken@0
    52
#ifndef NO_SIGNAL_H
slouken@0
    53
	void (*ohandler)(int);
slouken@0
    54
slouken@0
    55
	/* Both SIGINT and SIGTERM are translated into quit interrupts */
slouken@1123
    56
	ohandler = signal(SIGINT, SDL_HandleSIG);
slouken@0
    57
	if ( ohandler != SIG_DFL )
slouken@0
    58
		signal(SIGINT, ohandler);
slouken@0
    59
	ohandler = signal(SIGTERM, SDL_HandleSIG);
slouken@0
    60
	if ( ohandler != SIG_DFL )
slouken@0
    61
		signal(SIGTERM, ohandler);
slouken@0
    62
#endif /* NO_SIGNAL_H */
slouken@0
    63
slouken@0
    64
	/* That's it! */
slouken@0
    65
	return(0);
slouken@0
    66
}
slouken@1123
    67
void SDL_QuitQuit(void)
slouken@1123
    68
{
slouken@1123
    69
#ifndef NO_SIGNAL_H
slouken@1123
    70
	void (*ohandler)(int);
slouken@1123
    71
slouken@1123
    72
	ohandler = signal(SIGINT, SIG_DFL);
slouken@1123
    73
	if ( ohandler != SDL_HandleSIG )
slouken@1123
    74
		signal(SIGINT, ohandler);
slouken@1123
    75
	ohandler = signal(SIGTERM, SIG_DFL);
slouken@1123
    76
	if ( ohandler != SDL_HandleSIG )
slouken@1123
    77
		signal(SIGTERM, ohandler);
slouken@1123
    78
#endif /* NO_SIGNAL_H */
slouken@1123
    79
}
slouken@0
    80
slouken@0
    81
/* This function returns 1 if it's okay to close the application window */
slouken@0
    82
int SDL_PrivateQuit(void)
slouken@0
    83
{
slouken@0
    84
	int posted;
slouken@0
    85
slouken@0
    86
	posted = 0;
slouken@0
    87
	if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) {
slouken@0
    88
		SDL_Event event;
slouken@0
    89
		event.type = SDL_QUIT;
slouken@0
    90
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
    91
			posted = 1;
slouken@0
    92
			SDL_PushEvent(&event);
slouken@0
    93
		}
slouken@0
    94
	}
slouken@0
    95
	return(posted);
slouken@0
    96
}