1 /*
2  * Copyright (C) 2019-2021 Ashar Khan <ashar786khan@gmail.com>
3  *
4  * This file is part of CP Editor.
5  *
6  * CP Editor is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * I will not be responsible if CP Editor behaves in unexpected way and
12  * causes your ratings to go down and or lose any important contest.
13  *
14  * Believe Software is "Software" and it isn't immune to bugs.
15  *
16  */
17 
18 /**
19  * This class is based on https://stackoverflow.com/a/7582555/12601364
20  */
21 
22 #ifndef SIGNALHANDLER_HPP
23 #define SIGNALHANDLER_HPP
24 
25 #include <QObject>
26 
27 class SignalHandler : public QObject
28 {
29     Q_OBJECT
30 
31   public:
32     explicit SignalHandler(int mask = DEFAULT_SIGNALS);
33     ~SignalHandler() override;
34 
35     enum SIGNALS
36     {
37         SIG_UNHANDLED = 0,
38         // Physical signal not supported by this class
39         SIG_NOOP = 1,
40         // The application is requested to do a no-op (only a target that platform-specific signals map to when they
41         // can't be raised anyway)
42         SIG_INT = 2,
43         // Control+C (should terminate but consider that it's a normal way to do so; can delay a bit)
44         SIG_TERM = 4,
45         // Control+Break (should terminate now without regarding the consquences)
46         SIG_CLOSE = 8,
47         // Container window closed (should perform normal termination, like Ctrl^C) [Windows only; on Linux it maps to
48         // SIG_TERM]
49         SIG_RELOAD = 16,
50         // Reload the configuration [Linux only, physical signal is SIGHUP; on Windows it maps to SIG_NOOP]
51         DEFAULT_SIGNALS = SIG_INT | SIG_TERM | SIG_CLOSE | SIG_RELOAD,
52     };
53     static const int numSignals = 6;
54 
55     bool handleSignal(int signal);
56 
57   signals:
58     void signalReceived(int signal);
59 
60   private:
61     int _mask;
62 };
63 
64 #endif // SIGNALHANDLER_HPP
65