src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6138 4c64952a58fb
child 7190 11612d544fcd
permissions -rw-r--r--
Happy New Year!
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* General quit handling code for SDL */
slouken@0
    24
slouken@1330
    25
#ifdef HAVE_SIGNAL_H
slouken@0
    26
#include <signal.h>
slouken@0
    27
#endif
slouken@0
    28
slouken@0
    29
#include "SDL_events.h"
slouken@0
    30
#include "SDL_events_c.h"
slouken@0
    31
slouken@0
    32
slouken@1330
    33
#ifdef HAVE_SIGNAL_H
slouken@1895
    34
static void
slouken@1895
    35
SDL_HandleSIG(int sig)
slouken@0
    36
{
slouken@1895
    37
    /* Reset the signal handler */
slouken@1895
    38
    signal(sig, SDL_HandleSIG);
slouken@0
    39
slouken@1895
    40
    /* Signal a quit interrupt */
slouken@1895
    41
    SDL_SendQuit();
slouken@0
    42
}
slouken@1330
    43
#endif /* HAVE_SIGNAL_H */
slouken@0
    44
slouken@0
    45
/* Public functions */
slouken@1895
    46
int
slouken@1895
    47
SDL_QuitInit(void)
slouken@0
    48
{
slouken@5493
    49
#ifdef HAVE_SIGACTION
slouken@5493
    50
    struct sigaction action;
slouken@5493
    51
    sigaction(SIGINT, NULL, &action);
icculus@5577
    52
#ifdef HAVE_SA_SIGACTION
slouken@5501
    53
    if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
icculus@5577
    54
#else
icculus@5577
    55
    if ( action.sa_handler == SIG_DFL ) {
icculus@5577
    56
#endif
slouken@5493
    57
        action.sa_handler = SDL_HandleSIG;
slouken@5493
    58
        sigaction(SIGINT, &action, NULL);
slouken@5493
    59
    }
slouken@5493
    60
    sigaction(SIGTERM, NULL, &action);
icculus@5577
    61
icculus@5577
    62
#ifdef HAVE_SA_SIGACTION
slouken@5501
    63
    if ( action.sa_handler == SIG_DFL && action.sa_sigaction == (void*)SIG_DFL ) {
icculus@5577
    64
#else
icculus@5577
    65
    if ( action.sa_handler == SIG_DFL ) {
icculus@5577
    66
#endif
slouken@5493
    67
        action.sa_handler = SDL_HandleSIG;
slouken@5493
    68
        sigaction(SIGTERM, &action, NULL);
slouken@5493
    69
    }
slouken@5493
    70
#elif HAVE_SIGNAL_H
slouken@1895
    71
    void (*ohandler) (int);
slouken@0
    72
slouken@1895
    73
    /* Both SIGINT and SIGTERM are translated into quit interrupts */
slouken@1895
    74
    ohandler = signal(SIGINT, SDL_HandleSIG);
slouken@1895
    75
    if (ohandler != SIG_DFL)
slouken@1895
    76
        signal(SIGINT, ohandler);
slouken@1895
    77
    ohandler = signal(SIGTERM, SDL_HandleSIG);
slouken@1895
    78
    if (ohandler != SIG_DFL)
slouken@1895
    79
        signal(SIGTERM, ohandler);
slouken@1330
    80
#endif /* HAVE_SIGNAL_H */
slouken@0
    81
slouken@1895
    82
    /* That's it! */
slouken@1895
    83
    return (0);
slouken@0
    84
}
slouken@1895
    85
slouken@1895
    86
void
slouken@1895
    87
SDL_QuitQuit(void)
slouken@1123
    88
{
slouken@5493
    89
#ifdef HAVE_SIGACTION
slouken@5493
    90
    struct sigaction action;
slouken@5493
    91
    sigaction(SIGINT, NULL, &action);
slouken@5493
    92
    if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5493
    93
        action.sa_handler = SIG_DFL;
slouken@5493
    94
        sigaction(SIGINT, &action, NULL);
slouken@5493
    95
    }
slouken@5493
    96
    sigaction(SIGTERM, NULL, &action);
slouken@5493
    97
    if ( action.sa_handler == SDL_HandleSIG ) {
slouken@5493
    98
        action.sa_handler = SIG_DFL;
slouken@5493
    99
        sigaction(SIGTERM, &action, NULL);
slouken@5493
   100
    }
slouken@5493
   101
#elif HAVE_SIGNAL_H
slouken@1895
   102
    void (*ohandler) (int);
slouken@1123
   103
slouken@1895
   104
    ohandler = signal(SIGINT, SIG_DFL);
slouken@1895
   105
    if (ohandler != SDL_HandleSIG)
slouken@1895
   106
        signal(SIGINT, ohandler);
slouken@1895
   107
    ohandler = signal(SIGTERM, SIG_DFL);
slouken@1895
   108
    if (ohandler != SDL_HandleSIG)
slouken@1895
   109
        signal(SIGTERM, ohandler);
slouken@1330
   110
#endif /* HAVE_SIGNAL_H */
slouken@1123
   111
}
slouken@0
   112
slouken@0
   113
/* This function returns 1 if it's okay to close the application window */
slouken@1895
   114
int
slouken@1895
   115
SDL_SendQuit(void)
slouken@0
   116
{
slouken@1895
   117
    int posted;
slouken@0
   118
slouken@1895
   119
    posted = 0;
slouken@4429
   120
    if (SDL_GetEventState(SDL_QUIT) == SDL_ENABLE) {
slouken@1895
   121
        SDL_Event event;
slouken@1895
   122
        event.type = SDL_QUIT;
slouken@1895
   123
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   124
    }
slouken@1895
   125
    return (posted);
slouken@0
   126
}
slouken@1895
   127
slouken@1895
   128
/* vi: set ts=4 sw=4 expandtab: */