[qnut] fixed main window show/hide on tray icon click event
[nut.git] / nuts / sighandler.cpp
blob6851b405481af056a607b4b7d70c13f2a152727d
1 //
2 // C++ Implementation: sighandler
3 //
4 // Description:
5 //
6 //
7 // Author: Stefan Bühler <stbuehler@web.de>, (C) 2007
8 //
9 // Copyright: See COPYING file that comes with this distribution
13 #include "sighandler.h"
15 extern "C" {
16 // pipe
17 #include <unistd.h>
18 // signal
19 #include <signal.h>
20 // fcntl
21 #include <fcntl.h>
24 #include <iostream>
26 #include <QSocketNotifier>
27 #include <QCoreApplication>
29 namespace nuts {
30 static int pipefd[2];
32 static void sig2pipe(int sig) {
33 write(pipefd[1], (char*) &sig, sizeof(sig));
36 SigHandler::SigHandler(bool quitOnSignal)
37 : quitOnSignal(quitOnSignal) {
38 if (pipefd[1] != 0) return;
39 if (pipe(pipefd) != 0) return;
40 fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
41 fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
43 signal(SIGTERM, sig2pipe);
44 signal(SIGINT, sig2pipe);
46 QSocketNotifier *r = new QSocketNotifier(pipefd[0], QSocketNotifier::Read, this);
47 connect(r, SIGNAL(activated(int)), this, SLOT(pipe_rcv()));
50 SigHandler::~SigHandler() {
51 close(pipefd[0]);
52 close(pipefd[1]);
53 pipefd[0] = pipefd[1] = 0;
55 void SigHandler::pipe_rcv() {
56 int signum = 0;
57 if (read(pipefd[0], &signum, sizeof(signum))) {
58 std::cout << "Received signal: " << signum << std::endl;
59 emit gotSignal(signum);
60 switch (signum) {
61 case SIGTERM:
62 case SIGINT:
63 if (quitOnSignal) {
64 emit appQuit();
65 QCoreApplication::quit();