src/events/SDL_quit.c
author Ryan C. Gordon
Tue, 24 Mar 2015 14:29:25 -0400
changeset 9434 0a695c9c3c71
parent 8149 681eb46b8ac4
child 9435 0979c3e31211
permissions -rw-r--r--
Added a hint to prevent SDL from installing signal handlers.

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