src/events/SDL_quit.c
author Sam Lantinga
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
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* General quit handling code for SDL */
    24 
    25 #include "SDL_config.h"
    26 
    27 #ifdef HAVE_SIGNAL_H
    28 #include <signal.h>
    29 #endif
    30 
    31 #include "SDL_events.h"
    32 #include "SDL_events_c.h"
    33 
    34 
    35 #ifdef HAVE_SIGNAL_H
    36 static void SDL_HandleSIG(int sig)
    37 {
    38 	/* Reset the signal handler */
    39 	signal(sig, SDL_HandleSIG);
    40 
    41 	/* Signal a quit interrupt */
    42 	SDL_PrivateQuit();
    43 }
    44 #endif /* HAVE_SIGNAL_H */
    45 
    46 /* Public functions */
    47 int SDL_QuitInit(void)
    48 {
    49 #ifdef HAVE_SIGNAL_H
    50 	void (*ohandler)(int);
    51 
    52 	/* Both SIGINT and SIGTERM are translated into quit interrupts */
    53 	ohandler = signal(SIGINT, SDL_HandleSIG);
    54 	if ( ohandler != SIG_DFL )
    55 		signal(SIGINT, ohandler);
    56 	ohandler = signal(SIGTERM, SDL_HandleSIG);
    57 	if ( ohandler != SIG_DFL )
    58 		signal(SIGTERM, ohandler);
    59 #endif /* HAVE_SIGNAL_H */
    60 
    61 	/* That's it! */
    62 	return(0);
    63 }
    64 void SDL_QuitQuit(void)
    65 {
    66 #ifdef HAVE_SIGNAL_H
    67 	void (*ohandler)(int);
    68 
    69 	ohandler = signal(SIGINT, SIG_DFL);
    70 	if ( ohandler != SDL_HandleSIG )
    71 		signal(SIGINT, ohandler);
    72 	ohandler = signal(SIGTERM, SIG_DFL);
    73 	if ( ohandler != SDL_HandleSIG )
    74 		signal(SIGTERM, ohandler);
    75 #endif /* HAVE_SIGNAL_H */
    76 }
    77 
    78 /* This function returns 1 if it's okay to close the application window */
    79 int SDL_PrivateQuit(void)
    80 {
    81 	int posted;
    82 
    83 	posted = 0;
    84 	if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) {
    85 		SDL_Event event;
    86 		event.type = SDL_QUIT;
    87 		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
    88 			posted = 1;
    89 			SDL_PushEvent(&event);
    90 		}
    91 	}
    92 	return(posted);
    93 }