src/events/SDL_quit.c
author Ryan C. Gordon
Tue, 24 Mar 2015 14:40:31 -0400
changeset 9436 0af69dab9bb6
parent 9435 0979c3e31211
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Patched to compile on C89 compilers.
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 */
icculus@9436
    49
static int
icculus@9436
    50
SDL_QuitInit_Internal(void)
slouken@0
    51
{
slouken@5493
    52
#ifdef HAVE_SIGACTION
slouken@5493
    53
    struct sigaction action;
slouken@5493
    54
    sigaction(SIGINT, NULL, &action);
icculus@5577
    55
#ifdef HAVE_SA_SIGACTION
slouken@5501
    56
    if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
icculus@5577
    57
#else
icculus@5577
    58
    if ( action.sa_handler == SIG_DFL ) {
icculus@5577
    59
#endif
slouken@5493
    60
        action.sa_handler = SDL_HandleSIG;
slouken@5493
    61
        sigaction(SIGINT, &action, NULL);
slouken@5493
    62
    }
slouken@5493
    63
    sigaction(SIGTERM, NULL, &action);
icculus@5577
    64
icculus@5577
    65
#ifdef HAVE_SA_SIGACTION
slouken@5501
    66
    if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
icculus@5577
    67
#else
icculus@5577
    68
    if ( action.sa_handler == SIG_DFL ) {
icculus@5577
    69
#endif
slouken@5493
    70
        action.sa_handler = SDL_HandleSIG;
slouken@5493
    71
        sigaction(SIGTERM, &action, NULL);
slouken@5493
    72
    }
slouken@5493
    73
#elif HAVE_SIGNAL_H
slouken@1895
    74
    void (*ohandler) (int);
slouken@0
    75
slouken@1895
    76
    /* Both SIGINT and SIGTERM are translated into quit interrupts */
slouken@1895
    77
    ohandler = signal(SIGINT, SDL_HandleSIG);
slouken@1895
    78
    if (ohandler != SIG_DFL)
slouken@1895
    79
        signal(SIGINT, ohandler);
slouken@1895
    80
    ohandler = signal(SIGTERM, SDL_HandleSIG);
slouken@1895
    81
    if (ohandler != SIG_DFL)
slouken@1895
    82
        signal(SIGTERM, ohandler);
slouken@1330
    83
#endif /* HAVE_SIGNAL_H */
slouken@0
    84
slouken@1895
    85
    /* That's it! */
icculus@9434
    86
    return 0;
slouken@0
    87
}
slouken@1895
    88
icculus@9436
    89
int
icculus@9436
    90
SDL_QuitInit(void)
slouken@1123
    91
{
icculus@9436
    92
    const char *hint = SDL_GetHint(SDL_HINT_NO_SIGNAL_HANDLERS);
icculus@9436
    93
    disable_signals = hint && (SDL_atoi(hint) == 1);
icculus@9436
    94
    if (!disable_signals) {
icculus@9436
    95
        return SDL_QuitInit_Internal();
icculus@9434
    96
    }
icculus@9436
    97
    return 0;
icculus@9436
    98
}
icculus@9434
    99
icculus@9436
   100
static void
icculus@9436
   101
SDL_QuitQuit_Internal(void)
icculus@9436
   102
{
slouken@5493
   103
#ifdef HAVE_SIGACTION
slouken@5493
   104
    struct sigaction action;
slouken@5493
   105
    sigaction(SIGINT, NULL, &action);
slouken@5493
   106
    if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5493
   107
        action.sa_handler = SIG_DFL;
slouken@5493
   108
        sigaction(SIGINT, &action, NULL);
slouken@5493
   109
    }
slouken@5493
   110
    sigaction(SIGTERM, NULL, &action);
slouken@5493
   111
    if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5493
   112
        action.sa_handler = SIG_DFL;
slouken@5493
   113
        sigaction(SIGTERM, &action, NULL);
slouken@5493
   114
    }
slouken@5493
   115
#elif HAVE_SIGNAL_H
slouken@1895
   116
    void (*ohandler) (int);
slouken@1123
   117
slouken@1895
   118
    ohandler = signal(SIGINT, SIG_DFL);
slouken@1895
   119
    if (ohandler != SDL_HandleSIG)
slouken@1895
   120
        signal(SIGINT, ohandler);
slouken@1895
   121
    ohandler = signal(SIGTERM, SIG_DFL);
slouken@1895
   122
    if (ohandler != SDL_HandleSIG)
slouken@1895
   123
        signal(SIGTERM, ohandler);
slouken@1330
   124
#endif /* HAVE_SIGNAL_H */
slouken@1123
   125
}
slouken@0
   126
icculus@9436
   127
void
icculus@9436
   128
SDL_QuitQuit(void)
icculus@9436
   129
{
icculus@9436
   130
    if (!disable_signals) {
icculus@9436
   131
        SDL_QuitQuit_Internal();
icculus@9436
   132
    }
icculus@9436
   133
}
icculus@9436
   134
slouken@0
   135
/* This function returns 1 if it's okay to close the application window */
slouken@1895
   136
int
slouken@1895
   137
SDL_SendQuit(void)
slouken@0
   138
{
slouken@7190
   139
    return SDL_SendAppEvent(SDL_QUIT);
slouken@0
   140
}
slouken@1895
   141
slouken@1895
   142
/* vi: set ts=4 sw=4 expandtab: */