qa: Remove lots of superfluous semicolons
[quassel.git] / src / common / windowssignalwatcher.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 #include "logmessage.h"
29
30 // This handler is called by Windows in a different thread when a console event happens
31 // FIXME: When the console window is closed, the application is supposedly terminated as soon as
32 //        this handler returns. We may want to block and wait for the main thread so set some
33 //        condition variable once shutdown is complete...
34 static BOOL WINAPI consoleCtrlHandler(DWORD ctrlType)
35 {
36   switch (ctrlType) {
37   case CTRL_C_EVENT:     // Ctrl+C
38   case CTRL_CLOSE_EVENT: // Closing the console window
39       WindowsSignalWatcher::signalHandler(SIGTERM);
40       return TRUE;
41   default:
42       return FALSE;
43   }
44 }
45
46 WindowsSignalWatcher::WindowsSignalWatcher(QObject *parent)
47     : AbstractSignalWatcher{parent}
48     , Singleton<WindowsSignalWatcher>{this}
49 {
50     static bool registered = []() {
51         qRegisterMetaType<AbstractSignalWatcher::Action>();
52         return true;
53     }();
54     Q_UNUSED(registered)
55
56     // Use POSIX-style API to register standard signals.
57     // Not sure if this is safe to use, but it has worked so far...
58     signal(SIGTERM, signalHandler);
59     signal(SIGINT,  signalHandler);
60     signal(SIGABRT, signalHandler);
61     signal(SIGSEGV, signalHandler);
62
63     // React on console window events
64     SetConsoleCtrlHandler(consoleCtrlHandler, TRUE);
65 }
66
67 void WindowsSignalWatcher::signalHandler(int signal)
68 {
69     quInfo() << "Caught signal" << signal;
70
71     switch (signal) {
72     case SIGINT:
73     case SIGTERM:
74         emit instance()->handleSignal(Action::Terminate);
75         break;
76     case SIGABRT:
77     case SIGSEGV:
78         emit instance()->handleSignal(Action::HandleCrash);
79         break;
80     default:
81         ;
82     }
83 }