src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9720 fd60d77139fc
child 10499 363c1c7e7a41
permissions -rw-r--r--
Updated copyright to 2016
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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_internal.h"
    22 #include "SDL_hints.h"
    23 #include "SDL_assert.h"
    24 
    25 /* General quit handling code for SDL */
    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 static SDL_bool disable_signals = SDL_FALSE;
    35 static SDL_bool send_quit_pending = SDL_FALSE;
    36 
    37 #ifdef HAVE_SIGNAL_H
    38 static void
    39 SDL_HandleSIG(int sig)
    40 {
    41     /* Reset the signal handler */
    42     signal(sig, SDL_HandleSIG);
    43 
    44     /* Send a quit event next time the event loop pumps. */
    45     /* We can't send it in signal handler; malloc() might be interrupted! */
    46     send_quit_pending = SDL_TRUE;
    47 }
    48 #endif /* HAVE_SIGNAL_H */
    49 
    50 /* Public functions */
    51 static int
    52 SDL_QuitInit_Internal(void)
    53 {
    54 #ifdef HAVE_SIGACTION
    55     struct sigaction action;
    56     sigaction(SIGINT, NULL, &action);
    57 #ifdef HAVE_SA_SIGACTION
    58     if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
    59 #else
    60     if ( action.sa_handler == SIG_DFL ) {
    61 #endif
    62         action.sa_handler = SDL_HandleSIG;
    63         sigaction(SIGINT, &action, NULL);
    64     }
    65     sigaction(SIGTERM, NULL, &action);
    66 
    67 #ifdef HAVE_SA_SIGACTION
    68     if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
    69 #else
    70     if ( action.sa_handler == SIG_DFL ) {
    71 #endif
    72         action.sa_handler = SDL_HandleSIG;
    73         sigaction(SIGTERM, &action, NULL);
    74     }
    75 #elif HAVE_SIGNAL_H
    76     void (*ohandler) (int);
    77 
    78     /* Both SIGINT and SIGTERM are translated into quit interrupts */
    79     ohandler = signal(SIGINT, SDL_HandleSIG);
    80     if (ohandler != SIG_DFL)
    81         signal(SIGINT, ohandler);
    82     ohandler = signal(SIGTERM, SDL_HandleSIG);
    83     if (ohandler != SIG_DFL)
    84         signal(SIGTERM, ohandler);
    85 #endif /* HAVE_SIGNAL_H */
    86 
    87     /* That's it! */
    88     return 0;
    89 }
    90 
    91 int
    92 SDL_QuitInit(void)
    93 {
    94     const char *hint = SDL_GetHint(SDL_HINT_NO_SIGNAL_HANDLERS);
    95     disable_signals = hint && (SDL_atoi(hint) == 1);
    96     if (!disable_signals) {
    97         return SDL_QuitInit_Internal();
    98     }
    99     return 0;
   100 }
   101 
   102 static void
   103 SDL_QuitQuit_Internal(void)
   104 {
   105 #ifdef HAVE_SIGACTION
   106     struct sigaction action;
   107     sigaction(SIGINT, NULL, &action);
   108     if ( action.sa_handler == SDL_HandleSIG ) {
   109         action.sa_handler = SIG_DFL;
   110         sigaction(SIGINT, &action, NULL);
   111     }
   112     sigaction(SIGTERM, NULL, &action);
   113     if ( action.sa_handler == SDL_HandleSIG ) {
   114         action.sa_handler = SIG_DFL;
   115         sigaction(SIGTERM, &action, NULL);
   116     }
   117 #elif HAVE_SIGNAL_H
   118     void (*ohandler) (int);
   119 
   120     ohandler = signal(SIGINT, SIG_DFL);
   121     if (ohandler != SDL_HandleSIG)
   122         signal(SIGINT, ohandler);
   123     ohandler = signal(SIGTERM, SIG_DFL);
   124     if (ohandler != SDL_HandleSIG)
   125         signal(SIGTERM, ohandler);
   126 #endif /* HAVE_SIGNAL_H */
   127 }
   128 
   129 void
   130 SDL_QuitQuit(void)
   131 {
   132     if (!disable_signals) {
   133         SDL_QuitQuit_Internal();
   134     }
   135 }
   136 
   137 /* This function returns 1 if it's okay to close the application window */
   138 int
   139 SDL_SendQuit(void)
   140 {
   141     send_quit_pending = SDL_FALSE;
   142     return SDL_SendAppEvent(SDL_QUIT);
   143 }
   144 
   145 void
   146 SDL_SendPendingQuit(void)
   147 {
   148     if (send_quit_pending) {
   149         SDL_SendQuit();
   150         SDL_assert(!send_quit_pending);
   151     }
   152 }
   153 
   154 /* vi: set ts=4 sw=4 expandtab: */