Implement IRCv3 Server-Time
[quassel.git] / src / common / windowssignalwatcher.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "windowssignalwatcher.h"
22
23 #include <signal.h>
24 #include <windows.h>
25
26 #include <QDebug>
27
28 // This handler is called by Windows in a different thread when a console event happens
29 // FIXME: When the console window is closed, the application is supposedly terminated as soon as
30 //        this handler returns. We may want to block and wait for the main thread so set some
31 //        condition variable once shutdown is complete...
32 static BOOL WINAPI consoleCtrlHandler(DWORD ctrlType)
33 {
34     switch (ctrlType) {
35     case CTRL_C_EVENT:      // Ctrl+C
36     case CTRL_CLOSE_EVENT:  // Closing the console window
37         WindowsSignalWatcher::signalHandler(SIGTERM);
38         return TRUE;
39     default:
40         return FALSE;
41     }
42 }
43
44 WindowsSignalWatcher::WindowsSignalWatcher(QObject* parent)
45     : AbstractSignalWatcher{parent}
46     , Singleton<WindowsSignalWatcher>{this}
47 {
48     static bool registered = []() {
49         qRegisterMetaType<AbstractSignalWatcher::Action>();
50         return true;
51     }();
52     Q_UNUSED(registered)
53
54     // Use POSIX-style API to register standard signals.
55     // Not sure if this is safe to use, but it has worked so far...
56     signal(SIGTERM, signalHandler);
57     signal(SIGINT,  signalHandler);
58     signal(SIGABRT, signalHandler);
59     signal(SIGSEGV, signalHandler);
60
61     // React on console window events
62     SetConsoleCtrlHandler(consoleCtrlHandler, TRUE);
63 }
64
65 void WindowsSignalWatcher::signalHandler(int signal)
66 {
67     qInfo() << "Caught signal" << signal;
68
69     switch (signal) {
70     case SIGINT:
71     case SIGTERM:
72         emit instance()->handleSignal(Action::Terminate);
73         break;
74     case SIGABRT:
75     case SIGSEGV:
76         emit instance()->handleSignal(Action::HandleCrash);
77         break;
78     default:
79         ;
80     }
81 }