SDL-1.2.14
[sdl_omap.git] / src / events / SDL_quit.c
1 /*
2     SDL - Simple DirectMedia Layer
3     Copyright (C) 1997-2009 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 #include "SDL_config.h"
23
24 /* General quit handling code for SDL */
25
26 #ifdef HAVE_SIGNAL_H
27 #include <signal.h>
28 #endif
29
30 #include "SDL_events.h"
31 #include "SDL_events_c.h"
32
33
34 #ifdef HAVE_SIGNAL_H
35 static void SDL_HandleSIG(int sig)
36 {
37         /* Reset the signal handler */
38         signal(sig, SDL_HandleSIG);
39
40         /* Signal a quit interrupt */
41         SDL_PrivateQuit();
42 }
43 #endif /* HAVE_SIGNAL_H */
44
45 /* Public functions */
46 int SDL_QuitInit(void)
47 {
48 #ifdef HAVE_SIGNAL_H
49         void (*ohandler)(int);
50
51         /* Both SIGINT and SIGTERM are translated into quit interrupts */
52         ohandler = signal(SIGINT, SDL_HandleSIG);
53         if ( ohandler != SIG_DFL )
54                 signal(SIGINT, ohandler);
55         ohandler = signal(SIGTERM, SDL_HandleSIG);
56         if ( ohandler != SIG_DFL )
57                 signal(SIGTERM, ohandler);
58 #endif /* HAVE_SIGNAL_H */
59
60         /* That's it! */
61         return(0);
62 }
63 void SDL_QuitQuit(void)
64 {
65 #ifdef HAVE_SIGNAL_H
66         void (*ohandler)(int);
67
68         ohandler = signal(SIGINT, SIG_DFL);
69         if ( ohandler != SDL_HandleSIG )
70                 signal(SIGINT, ohandler);
71         ohandler = signal(SIGTERM, SIG_DFL);
72         if ( ohandler != SDL_HandleSIG )
73                 signal(SIGTERM, ohandler);
74 #endif /* HAVE_SIGNAL_H */
75 }
76
77 /* This function returns 1 if it's okay to close the application window */
78 int SDL_PrivateQuit(void)
79 {
80         int posted;
81
82         posted = 0;
83         if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) {
84                 SDL_Event event;
85                 event.type = SDL_QUIT;
86                 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
87                         posted = 1;
88                         SDL_PushEvent(&event);
89                 }
90         }
91         return(posted);
92 }