From 519330ffbaedde07929caa0d2a8aca71da56eb9f Mon Sep 17 00:00:00 2001 From: "Brayan M. Salazar" Date: Tue, 23 Jul 2024 15:20:23 -0400 Subject: [PATCH] React to some common Unix signals. --- CMakeLists.txt | 2 +- src/main.cpp | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57f0fd7..458cfff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.28) project(BtScreenLocker - VERSION 1.6.4 + VERSION 1.6.5 LANGUAGES CXX DESCRIPTION "Daemon to lock your screen if trusted Bluetooth devices go away." ) diff --git a/src/main.cpp b/src/main.cpp index f662f38..2e7231d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -11,16 +12,18 @@ #include #include #include +#include #include "listener.hpp" #include "logger.hpp" #include "screenlocker.hpp" +void setupSignals(const QVector sigs); +void signalHandler(int signal); QList commandLineOptions(const char *name); QTranslator *setAppLanguage(std::map &logMessages, const QString &language); int enableAutostart(const QCommandLineParser &parser, Logger &logger, const char *name); void registerDBusService(Listener &listener, Logger &logger); -void usage(const char *programName); int main(int argc, char *argv[]) { @@ -29,6 +32,8 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); a.setQuitOnLastWindowClosed(false); + setupSignals({ SIGINT, SIGHUP, SIGTERM, SIGQUIT }); + QCommandLineParser parser; parser.setApplicationDescription(PROJECT_DESCRIPTION); parser.addHelpOption(); @@ -111,6 +116,19 @@ int main(int argc, char *argv[]) return a.exec(); } +void setupSignals(const QVector sigs) +{ + for (const int sig : sigs) { + signal(sig, signalHandler); + } +} + +void signalHandler(int signal) +{ + qInfo() << "Exiting because of signal:" << signal; + QApplication::exit(signal); +} + QList commandLineOptions(const char *name) { QList options;