From 391c4f2ca43b16cba0f9119e5d302fa697f2615c Mon Sep 17 00:00:00 2001 From: Akash Date: Wed, 11 Oct 2023 22:36:10 -0700 Subject: [PATCH] Add signal handlers for Windows (#3167) --- gazebo/Server.cc | 4 +++- gazebo/gazebo_main.cc | 9 ++++++++- gazebo/gui/GuiIface.cc | 4 +++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/gazebo/Server.cc b/gazebo/Server.cc index bf758ddb25..cb64723b94 100644 --- a/gazebo/Server.cc +++ b/gazebo/Server.cc @@ -643,9 +643,11 @@ void Server::Fini() ///////////////////////////////////////////////// void Server::Run() { -#ifndef _WIN32 // Now that we're about to run, install a signal handler to allow for // graceful shutdown on Ctrl-C. +#ifdef _WIN32 + signal(SIGINT, Server::SigInt); +#else struct sigaction sigact; sigact.sa_flags = 0; sigact.sa_handler = Server::SigInt; diff --git a/gazebo/gazebo_main.cc b/gazebo/gazebo_main.cc index 0d7ee00541..b799e60513 100644 --- a/gazebo/gazebo_main.cc +++ b/gazebo/gazebo_main.cc @@ -22,6 +22,7 @@ #include #else #include +#include #endif #include "gazebo/common/Console.hh" @@ -227,6 +228,11 @@ int main(int _argc, char **_argv) std::atomic g_shouldExit = false; +void sig_handler(int /*signo*/) +{ + g_shouldExit = true; +} + int main(int _argc, char **_argv) { if (_argc >= 2 && @@ -235,6 +241,8 @@ int main(int _argc, char **_argv) return 0; } + signal(SIGINT, sig_handler); + std::vector argvServer; std::vector argvClient; @@ -272,7 +280,6 @@ int main(int _argc, char **_argv) { g_shouldExit = true; } - } // Cleanup diff --git a/gazebo/gui/GuiIface.cc b/gazebo/gui/GuiIface.cc index 1d6c03ed8a..cd915c1870 100644 --- a/gazebo/gui/GuiIface.cc +++ b/gazebo/gui/GuiIface.cc @@ -408,9 +408,11 @@ bool gui::run(int _argc, char **_argv) mainWindow->RenderWidget()->AddPlugins(g_plugins_to_load); -#ifndef _WIN32 // Now that we're about to run, install a signal handler to allow for // graceful shutdown on Ctrl-C. +#ifdef _WIN32 + signal(SIGINT, signal_handler); +#else struct sigaction sigact; sigact.sa_flags = 0; sigact.sa_handler = signal_handler;