From a032d191c81e09ab7c654935a2fea33fb47691b3 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 28 Feb 2021 19:00:54 +1000 Subject: [PATCH] Qt: Hook signal handlers for CTRL+C --- src/duckstation-qt/main.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/duckstation-qt/main.cpp b/src/duckstation-qt/main.cpp index b9a502dd7..d9de4da93 100644 --- a/src/duckstation-qt/main.cpp +++ b/src/duckstation-qt/main.cpp @@ -5,8 +5,10 @@ #include "qtutils.h" #include #include +#include #include #include +#include static bool ParseCommandLineParameters(QApplication& app, QtHostInterface* host_interface, std::unique_ptr* boot_params) @@ -26,6 +28,28 @@ static bool ParseCommandLineParameters(QApplication& app, QtHostInterface* host_ return host_interface->ParseCommandLineParameters(args.size(), converted_argv.data(), boot_params); } +static void SignalHandler(int signal) +{ + // First try the normal (graceful) shutdown/exit. + static bool graceful_shutdown_attempted = false; + if (!graceful_shutdown_attempted) + { + std::fprintf(stderr, "Received CTRL+C, attempting graceful shutdown. Press CTRL+C again to force.\n"); + graceful_shutdown_attempted = true; + QtHostInterface::GetInstance()->requestExit(); + return; + } + + std::signal(signal, SIG_DFL); + std::quick_exit(1); +} + +static void HookSignals() +{ + std::signal(SIGINT, SignalHandler); + std::signal(SIGTERM, SignalHandler); +} + int main(int argc, char* argv[]) { CrashHandler::Install(); @@ -64,6 +88,7 @@ int main(int argc, char* argv[]) } window->initializeAndShow(); + HookSignals(); // if we're in batch mode, don't bother refreshing the game list as it won't be used if (!host_interface->inBatchMode())