src/events/SDL_quit.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 21 Oct 2018 22:40:17 -0400
changeset 12345 50e1cca28b39
parent 11840 4d52395a8dba
child 12503 806492103856
permissions -rw-r--r--
wasapi/win32: Sort initial device lists by device GUID.

This makes an unchanged set of hardware always report devices in the same
order on each run.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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"
icculus@9720
    23
#include "SDL_assert.h"
slouken@0
    24
slouken@0
    25
/* General quit handling code for SDL */
slouken@0
    26
slouken@1330
    27
#ifdef HAVE_SIGNAL_H
slouken@0
    28
#include <signal.h>
slouken@0
    29
#endif
slouken@0
    30
slouken@0
    31
#include "SDL_events.h"
slouken@0
    32
#include "SDL_events_c.h"
slouken@0
    33
icculus@9434
    34
static SDL_bool disable_signals = SDL_FALSE;
icculus@9720
    35
static SDL_bool send_quit_pending = SDL_FALSE;
icculus@9434
    36
slouken@1330
    37
#ifdef HAVE_SIGNAL_H
slouken@1895
    38
static void
slouken@1895
    39
SDL_HandleSIG(int sig)
slouken@0
    40
{
slouken@1895
    41
    /* Reset the signal handler */
slouken@1895
    42
    signal(sig, SDL_HandleSIG);
slouken@0
    43
icculus@9720
    44
    /* Send a quit event next time the event loop pumps. */
icculus@9720
    45
    /* We can't send it in signal handler; malloc() might be interrupted! */
icculus@9720
    46
    send_quit_pending = SDL_TRUE;
slouken@0
    47
}
slouken@1330
    48
#endif /* HAVE_SIGNAL_H */
slouken@0
    49
slouken@0
    50
/* Public functions */
icculus@9436
    51
static int
icculus@9436
    52
SDL_QuitInit_Internal(void)
slouken@0
    53
{
slouken@5493
    54
#ifdef HAVE_SIGACTION
slouken@5493
    55
    struct sigaction action;
slouken@5493
    56
    sigaction(SIGINT, NULL, &action);
icculus@5577
    57
#ifdef HAVE_SA_SIGACTION
slouken@11840
    58
    if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
icculus@5577
    59
#else
icculus@5577
    60
    if ( action.sa_handler == SIG_DFL ) {
icculus@5577
    61
#endif
slouken@5493
    62
        action.sa_handler = SDL_HandleSIG;
slouken@5493
    63
        sigaction(SIGINT, &action, NULL);
slouken@5493
    64
    }
slouken@5493
    65
    sigaction(SIGTERM, NULL, &action);
icculus@5577
    66
icculus@5577
    67
#ifdef HAVE_SA_SIGACTION
slouken@11840
    68
    if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
icculus@5577
    69
#else
icculus@5577
    70
    if ( action.sa_handler == SIG_DFL ) {
icculus@5577
    71
#endif
slouken@5493
    72
        action.sa_handler = SDL_HandleSIG;
slouken@5493
    73
        sigaction(SIGTERM, &action, NULL);
slouken@5493
    74
    }
slouken@5493
    75
#elif HAVE_SIGNAL_H
slouken@1895
    76
    void (*ohandler) (int);
slouken@0
    77
slouken@1895
    78
    /* Both SIGINT and SIGTERM are translated into quit interrupts */
slouken@1895
    79
    ohandler = signal(SIGINT, SDL_HandleSIG);
slouken@1895
    80
    if (ohandler != SIG_DFL)
slouken@1895
    81
        signal(SIGINT, ohandler);
slouken@1895
    82
    ohandler = signal(SIGTERM, SDL_HandleSIG);
slouken@1895
    83
    if (ohandler != SIG_DFL)
slouken@1895
    84
        signal(SIGTERM, ohandler);
slouken@1330
    85
#endif /* HAVE_SIGNAL_H */
slouken@0
    86
slouken@1895
    87
    /* That's it! */
icculus@9434
    88
    return 0;
slouken@0
    89
}
slouken@1895
    90
icculus@9436
    91
int
icculus@9436
    92
SDL_QuitInit(void)
slouken@1123
    93
{
slouken@10499
    94
    if (!SDL_GetHintBoolean(SDL_HINT_NO_SIGNAL_HANDLERS, SDL_FALSE)) {
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
{
icculus@9720
   139
    send_quit_pending = SDL_FALSE;
slouken@7190
   140
    return SDL_SendAppEvent(SDL_QUIT);
slouken@0
   141
}
slouken@1895
   142
icculus@9720
   143
void
icculus@9720
   144
SDL_SendPendingQuit(void)
icculus@9720
   145
{
icculus@9720
   146
    if (send_quit_pending) {
icculus@9720
   147
        SDL_SendQuit();
icculus@9720
   148
        SDL_assert(!send_quit_pending);
icculus@9720
   149
    }
icculus@9720
   150
}
icculus@9720
   151
slouken@1895
   152
/* vi: set ts=4 sw=4 expandtab: */