src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 5577 ced8a6788629
child 6885 700f1b25f77f
permissions -rwxr-xr-x
Happy New Year!
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 /* General quit handling code for SDL */
    24 
    25 #ifdef HAVE_SIGNAL_H
    26 #include <signal.h>
    27 #endif
    28 
    29 #include "SDL_events.h"
    30 #include "SDL_events_c.h"
    31 
    32 
    33 #ifdef HAVE_SIGNAL_H
    34 static void
    35 SDL_HandleSIG(int sig)
    36 {
    37     /* Reset the signal handler */
    38     signal(sig, SDL_HandleSIG);
    39 
    40     /* Signal a quit interrupt */
    41     SDL_SendQuit();
    42 }
    43 #endif /* HAVE_SIGNAL_H */
    44 
    45 /* Public functions */
    46 int
    47 SDL_QuitInit(void)
    48 {
    49 #ifdef HAVE_SIGACTION
    50     struct sigaction action;
    51     sigaction(SIGINT, NULL, &action);
    52 #ifdef HAVE_SA_SIGACTION
    53     if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
    54 #else
    55     if ( action.sa_handler == SIG_DFL ) {
    56 #endif
    57         action.sa_handler = SDL_HandleSIG;
    58         sigaction(SIGINT, &action, NULL);
    59     }
    60     sigaction(SIGTERM, NULL, &action);
    61 
    62 #ifdef HAVE_SA_SIGACTION
    63     if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
    64 #else
    65     if ( action.sa_handler == SIG_DFL ) {
    66 #endif
    67         action.sa_handler = SDL_HandleSIG;
    68         sigaction(SIGTERM, &action, NULL);
    69     }
    70 #elif HAVE_SIGNAL_H
    71     void (*ohandler) (int);
    72 
    73     /* Both SIGINT and SIGTERM are translated into quit interrupts */
    74     ohandler = signal(SIGINT, SDL_HandleSIG);
    75     if (ohandler != SIG_DFL)
    76         signal(SIGINT, ohandler);
    77     ohandler = signal(SIGTERM, SDL_HandleSIG);
    78     if (ohandler != SIG_DFL)
    79         signal(SIGTERM, ohandler);
    80 #endif /* HAVE_SIGNAL_H */
    81 
    82     /* That's it! */
    83     return (0);
    84 }
    85 
    86 void
    87 SDL_QuitQuit(void)
    88 {
    89 #ifdef HAVE_SIGACTION
    90     struct sigaction action;
    91     sigaction(SIGINT, NULL, &action);
    92     if ( action.sa_handler == SDL_HandleSIG ) {
    93         action.sa_handler = SIG_DFL;
    94         sigaction(SIGINT, &action, NULL);
    95     }
    96     sigaction(SIGTERM, NULL, &action);
    97     if ( action.sa_handler == SDL_HandleSIG ) {
    98         action.sa_handler = SIG_DFL;
    99         sigaction(SIGTERM, &action, NULL);
   100     }
   101 #elif HAVE_SIGNAL_H
   102     void (*ohandler) (int);
   103 
   104     ohandler = signal(SIGINT, SIG_DFL);
   105     if (ohandler != SDL_HandleSIG)
   106         signal(SIGINT, ohandler);
   107     ohandler = signal(SIGTERM, SIG_DFL);
   108     if (ohandler != SDL_HandleSIG)
   109         signal(SIGTERM, ohandler);
   110 #endif /* HAVE_SIGNAL_H */
   111 }
   112 
   113 /* This function returns 1 if it's okay to close the application window */
   114 int
   115 SDL_SendQuit(void)
   116 {
   117     int posted;
   118 
   119     posted = 0;
   120     if (SDL_GetEventState(SDL_QUIT) == SDL_ENABLE) {
   121         SDL_Event event;
   122         event.type = SDL_QUIT;
   123         posted = (SDL_PushEvent(&event) > 0);
   124     }
   125     return (posted);
   126 }
   127 
   128 /* vi: set ts=4 sw=4 expandtab: */