src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Mar 2011 21:37:01 -0700
changeset 5493 a8849b1193c3
parent 5262 b530ef003506
child 5501 f48c1e0ca9ae
permissions -rw-r--r--
Do not break application's signal handler installed with SA_SIGINFO Gleb Natapov to sdl If application installs SIGINT/SIGTERM signal handler with sigaction(SA_SIGINFO) syscall before initializing SDL, after initialization of SDL signal handler will be reinstalled without SA_SIGINFO flag which brings havoc when the signal handler is called. The breakage is done by SDL_QuitInit()/SDL_QuitQuit() function. They use signal() to detect that signal handler is already installed even in sigaction() is available.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 4429
diff changeset
     3
    Copyright (C) 1997-2011 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1152
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1152
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1152
diff changeset
    13
    Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1152
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1152
diff changeset
    16
    License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1152
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
    Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
    20
    slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    21
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
    22
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
/* General quit handling code for SDL */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    26
#ifdef HAVE_SIGNAL_H
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include <signal.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
#include "SDL_events.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
#include "SDL_events_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    34
#ifdef HAVE_SIGNAL_H
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    35
static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    36
SDL_HandleSIG(int sig)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    38
    /* Reset the signal handler */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    39
    signal(sig, SDL_HandleSIG);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    41
    /* Signal a quit interrupt */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    42
    SDL_SendQuit();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    43
}
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    44
#endif /* HAVE_SIGNAL_H */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    45
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    46
/* Public functions */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    47
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    48
SDL_QuitInit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    49
{
5493
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    50
#ifdef HAVE_SIGACTION
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    51
    struct sigaction action;
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    52
    sigaction(SIGINT, NULL, &action);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    53
    if ( action.sa_handler == SIG_DFL && action.sa_sigaction == SIG_DFL ) {
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    54
        action.sa_handler = SDL_HandleSIG;
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    55
        sigaction(SIGINT, &action, NULL);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    56
    }
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    57
    sigaction(SIGTERM, NULL, &action);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    58
    if ( action.sa_handler == SIG_DFL && action.sa_sigaction == SIG_DFL ) {
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    59
        action.sa_handler = SDL_HandleSIG;
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    60
        sigaction(SIGTERM, &action, NULL);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    61
    }
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    62
#elif HAVE_SIGNAL_H
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    63
    void (*ohandler) (int);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    65
    /* Both SIGINT and SIGTERM are translated into quit interrupts */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    66
    ohandler = signal(SIGINT, SDL_HandleSIG);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    67
    if (ohandler != SIG_DFL)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    68
        signal(SIGINT, ohandler);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    69
    ohandler = signal(SIGTERM, SDL_HandleSIG);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    70
    if (ohandler != SIG_DFL)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    71
        signal(SIGTERM, ohandler);
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    72
#endif /* HAVE_SIGNAL_H */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    74
    /* That's it! */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    75
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    76
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    77
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    78
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    79
SDL_QuitQuit(void)
1123
28ac87a38c17 Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    80
{
5493
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    81
#ifdef HAVE_SIGACTION
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    82
    struct sigaction action;
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    83
    sigaction(SIGINT, NULL, &action);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    84
    if ( action.sa_handler == SDL_HandleSIG ) {
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    85
        action.sa_handler = SIG_DFL;
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    86
        sigaction(SIGINT, &action, NULL);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    87
    }
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    88
    sigaction(SIGTERM, NULL, &action);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    89
    if ( action.sa_handler == SDL_HandleSIG ) {
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    90
        action.sa_handler = SIG_DFL;
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    91
        sigaction(SIGTERM, &action, NULL);
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    92
    }
a8849b1193c3 Do not break application's signal handler installed with SA_SIGINFO
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    93
#elif HAVE_SIGNAL_H
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    94
    void (*ohandler) (int);
1123
28ac87a38c17 Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    95
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    96
    ohandler = signal(SIGINT, SIG_DFL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    97
    if (ohandler != SDL_HandleSIG)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    98
        signal(SIGINT, ohandler);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    99
    ohandler = signal(SIGTERM, SIG_DFL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   100
    if (ohandler != SDL_HandleSIG)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   101
        signal(SIGTERM, ohandler);
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   102
#endif /* HAVE_SIGNAL_H */
1123
28ac87a38c17 Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   103
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   104
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   105
/* This function returns 1 if it's okay to close the application window */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   106
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   107
SDL_SendQuit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   108
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   109
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   110
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   111
    posted = 0;
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   112
    if (SDL_GetEventState(SDL_QUIT) == SDL_ENABLE) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   113
        SDL_Event event;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   114
        event.type = SDL_QUIT;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   115
        posted = (SDL_PushEvent(&event) > 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   116
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   117
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   118
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   119
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   120
/* vi: set ts=4 sw=4 expandtab: */