From 2e8ed6a6b5a71f581dd2bae65d27941ce65735b7 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 3 Apr 2012 19:49:56 +0200 Subject: [PATCH] Implemented SIGINT handler --- base/application.cpp | 12 ++++++++++++ base/application.h | 17 +++++++++++++++-- base/unix.h | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/base/application.cpp b/base/application.cpp index 01840c581..901b59b85 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -359,3 +359,15 @@ bool Application::IsDebugging(void) const { return m_Debugging; } + +void Application::SigIntHandler(int signum) +{ + Application::Instance->Shutdown(); + +#ifndef _WIN32 + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_DFL; + sigaction(SIGINT, &sa, NULL); +#endif /* _WIN32 */ +} diff --git a/base/application.h b/base/application.h index 406cc8f6c..e4211146b 100644 --- a/base/application.h +++ b/base/application.h @@ -45,8 +45,14 @@ public: const string& GetExeDirectory(void); bool IsDebugging(void) const; + void SigIntHandler(int signum); }; +inline void sigint_handler(int signum) +{ + Application::Instance->SigIntHandler(signum); +} + template int application_main(int argc, char **argv) { @@ -54,6 +60,13 @@ int application_main(int argc, char **argv) Application::Instance = make_shared(); +#ifndef _WIN32 + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = sigint_handler; + sigaction(SIGINT, &sa, NULL); +#endif /* _WIN32 */ + vector args; for (int i = 0; i < argc; i++) @@ -71,7 +84,7 @@ int application_main(int argc, char **argv) string klass = typeid(ex).name(); - #ifdef HAVE_GCC_ABI_DEMANGLE +#ifdef HAVE_GCC_ABI_DEMANGLE int status; char *realname = abi::__cxa_demangle(klass.c_str(), 0, 0, &status); @@ -79,7 +92,7 @@ int application_main(int argc, char **argv) klass = string(realname); free(realname); } - #endif /* HAVE_GCC_ABI_DEMANGLE */ +#endif /* HAVE_GCC_ABI_DEMANGLE */ cout << "Exception: " << klass << endl; cout << "Message: " << ex.GetMessage() << endl; diff --git a/base/unix.h b/base/unix.h index ea946f348..f0eba440f 100644 --- a/base/unix.h +++ b/base/unix.h @@ -11,6 +11,7 @@ #include #include #include +#include void Sleep(unsigned long milliseconds);