19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* General quit handling code for SDL */ 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGNAL_H 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h> 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events.h" 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events_c.h" 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGNAL_H 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_HandleSIG(int sig) 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Reset the signal handler */ 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall signal(sig, SDL_HandleSIG); 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Signal a quit interrupt */ 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateQuit(); 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SIGNAL_H */ 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Public functions */ 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_QuitInit(void) 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGACTION 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct sigaction action; 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGINT, NULL, &action); 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# ifdef HAVE_SA_SIGACTION 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) { 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# else 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( action.sa_handler == SIG_DFL ) { 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# endif 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall action.sa_handler = SDL_HandleSIG; 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGINT, &action, NULL); 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGTERM, NULL, &action); 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# ifdef HAVE_SA_SIGACTION 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) { 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# else 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( action.sa_handler == SIG_DFL ) { 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# endif 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall action.sa_handler = SDL_HandleSIG; 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGTERM, &action, NULL); 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif HAVE_SIGNAL_H 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall void (*ohandler)(int); 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Both SIGINT and SIGTERM are translated into quit interrupts */ 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ohandler = signal(SIGINT, SDL_HandleSIG); 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ohandler != SIG_DFL ) 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall signal(SIGINT, ohandler); 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ohandler = signal(SIGTERM, SDL_HandleSIG); 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ohandler != SIG_DFL ) 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall signal(SIGTERM, ohandler); 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SIGNAL_H */ 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* That's it! */ 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_QuitQuit(void) 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGACTION 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct sigaction action; 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGINT, NULL, &action); 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( action.sa_handler == SDL_HandleSIG ) { 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall action.sa_handler = SIG_DFL; 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGINT, &action, NULL); 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGTERM, NULL, &action); 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( action.sa_handler == SDL_HandleSIG ) { 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall action.sa_handler = SIG_DFL; 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sigaction(SIGTERM, &action, NULL); 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif HAVE_SIGNAL_H 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall void (*ohandler)(int); 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ohandler = signal(SIGINT, SIG_DFL); 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ohandler != SDL_HandleSIG ) 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall signal(SIGINT, ohandler); 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ohandler = signal(SIGTERM, SIG_DFL); 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ohandler != SDL_HandleSIG ) 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall signal(SIGTERM, ohandler); 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SIGNAL_H */ 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function returns 1 if it's okay to close the application window */ 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_PrivateQuit(void) 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int posted; 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall posted = 0; 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) { 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Event event; 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall event.type = SDL_QUIT; 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall posted = 1; 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PushEvent(&event); 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(posted); 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 125