Integrated signal handler from qpdfview
authorJoel Holdsworth <joel@airwebreathe.org.uk>
Sun, 31 Mar 2013 09:31:51 +0000 (10:31 +0100)
committerJoel Holdsworth <joel@airwebreathe.org.uk>
Sun, 31 Mar 2013 09:31:51 +0000 (10:31 +0100)
CMakeLists.txt
main.cpp
signalhandler.cpp [new file with mode: 0644]
signalhandler.h [new file with mode: 0644]

index fc8fb4c9e521b6856e1d7fe189e90b804b99505f..2b9f39d110c6df1ed402d02b0c979bf9055f4547 100644 (file)
@@ -90,6 +90,7 @@ configure_file (
 
 set(pulseview_SOURCES
        main.cpp
+       signalhandler.cpp
        pv/mainwindow.cpp
        pv/sigsession.cpp
        pv/data/analog.cpp
@@ -119,6 +120,7 @@ set(pulseview_SOURCES
 )
 
 set(pulseview_HEADERS
+       signalhandler.h
        pv/mainwindow.h
        pv/sigsession.h
        pv/dialogs/about.h
index fef53247deb71820a424050f899f31f7a297f350..3fcfcc776626130ab732b2dd23f2d6ba4d14babf 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -19,7 +19,6 @@
  */
 
 #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
-#include <signal.h>
 #include <stdint.h>
 #include <libsigrok/libsigrok.h>
 
 #include <QtGui/QApplication>
 #include <QDebug>
 
+#include "signalhandler.h"
 #include "pv/mainwindow.h"
 
 #include "config.h"
 
-// Global pointer to our QApplication
-QApplication *g_app = NULL;
-
 void usage()
 {
        fprintf(stdout,
@@ -48,31 +45,13 @@ void usage()
                "\n", PV_BIN_NAME, PV_DESCRIPTION);
 }
 
-/*
- * SIGINT handler (likely received Ctrl-C from terminal)
- */
-void sigint_handler(int param)
-{
-       (void)param;
-
-       qDebug("Received SIGINT.");
-
-       if (g_app)
-               g_app->quit();
-}
-
 int main(int argc, char *argv[])
 {
        int ret = 0;
        struct sr_context *sr_ctx = NULL;
        const char *open_file = NULL;
 
-       // Register a SIGINT handler
-       signal(SIGINT, sigint_handler);
-
        QApplication a(argc, argv);
-       // Now we have an application to populate our global pointer
-       g_app = &a;
 
        // Set some application metadata
        QApplication::setApplicationVersion(PV_VERSION_STRING);
@@ -148,6 +127,20 @@ int main(int argc, char *argv[])
                        pv::MainWindow w(open_file);
                        w.show();
 
+                       if(SignalHandler::prepareSignals()) {
+                               SignalHandler *const signalHandler =
+                                       new SignalHandler(&w);
+                               QObject::connect(signalHandler,
+                                       SIGNAL(sigIntReceived()),
+                                       &w, SLOT(close()));
+                               QObject::connect(signalHandler,
+                                       SIGNAL(sigTermReceived()),
+                                       &w, SLOT(close()));
+                       } else {
+                               qWarning() <<
+                                       "Could not prepare signal handler.";
+                       }
+
                        // Run the application
                        ret = a.exec();
                }
diff --git a/signalhandler.cpp b/signalhandler.cpp
new file mode 100644 (file)
index 0000000..a3ef6b8
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+
+Copyright 2013 Adam Reichold
+
+This file is part of qpdfview.
+
+qpdfview is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+qpdfview is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "signalhandler.h"
+
+#include <signal.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <QSocketNotifier>
+
+int SignalHandler::s_sockets[2];
+
+bool SignalHandler::prepareSignals()
+{
+    if(socketpair(AF_UNIX, SOCK_STREAM, 0, s_sockets) != 0)
+    {
+        return false;
+    }
+
+    struct sigaction sigAction;
+
+    sigAction.sa_handler = SignalHandler::handleSignals;
+    sigemptyset(&sigAction.sa_mask);
+    sigAction.sa_flags = SA_RESTART;
+
+    if(sigaction(SIGINT, &sigAction, 0) != 0)
+    {
+        close(s_sockets[0]);
+        close(s_sockets[1]);
+
+        return false;
+    }
+
+    if(sigaction(SIGTERM, &sigAction, 0) != 0)
+    {
+        close(s_sockets[0]);
+        close(s_sockets[1]);
+
+        return false;
+    }
+
+    return true;
+}
+
+SignalHandler::SignalHandler(QObject* parent) : QObject(parent),
+    m_socketNotifier(0)
+{
+    m_socketNotifier = new QSocketNotifier(s_sockets[1], QSocketNotifier::Read, this);
+    connect(m_socketNotifier, SIGNAL(activated(int)), SLOT(on_socketNotifier_activated()));
+}
+
+void SignalHandler::on_socketNotifier_activated()
+{
+    m_socketNotifier->setEnabled(false);
+
+    int sigNumber;
+    read(s_sockets[1], &sigNumber, sizeof(int));
+
+    switch(sigNumber)
+    {
+    case SIGINT:
+        emit sigIntReceived();
+        break;
+    case SIGTERM:
+        emit sigTermReceived();
+        break;
+    }
+
+    m_socketNotifier->setEnabled(true);
+}
+
+void SignalHandler::handleSignals(int sigNumber)
+{
+    write(s_sockets[0], &sigNumber, sizeof(int));
+}
diff --git a/signalhandler.h b/signalhandler.h
new file mode 100644 (file)
index 0000000..4801762
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+
+Copyright 2013 Adam Reichold
+
+This file is part of qpdfview.
+
+qpdfview is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+qpdfview is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef SIGNALHANDLER_H
+#define SIGNALHANDLER_H
+
+#include <QObject>
+
+class QSocketNotifier;
+
+class SignalHandler : public QObject
+{
+    Q_OBJECT
+
+public:
+    static bool prepareSignals();
+
+    explicit SignalHandler(QObject* parent = 0);
+
+signals:
+    void sigIntReceived();
+    void sigTermReceived();
+
+private slots:
+    void on_socketNotifier_activated();
+
+private:
+    static int s_sockets[2];
+
+    static void handleSignals(int sigNumber);
+
+    QSocketNotifier* m_socketNotifier;
+
+};
+
+#endif // SIGNALHANDLER_H