eco2d/code/game/source/signal_handling.c

55 lines
1.2 KiB
C
Raw Normal View History

2021-01-11 21:12:26 +00:00
#include <signal.h>
#include "zpl.h"
#include "platform.h"
#ifdef ZPL_SYSTEM_WINDOWS
2021-06-01 13:20:04 +00:00
#include <Windows.h>
2021-01-11 21:12:26 +00:00
static BOOL WINAPI _sighandler_win32_control_handler(DWORD control_type)
{
switch (control_type)
{
2021-06-01 13:20:04 +00:00
case CTRL_C_EVENT:
case DBG_CONTROL_C:
2021-01-11 21:12:26 +00:00
platform_shutdown();
return 0;
2021-06-01 13:20:04 +00:00
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_SHUTDOWN_EVENT:
2021-01-11 21:12:26 +00:00
platform_shutdown();
return 1;
}
2021-06-01 13:20:04 +00:00
2021-01-11 21:12:26 +00:00
return 0;
}
#else //POSIX complaint
#include <sys/types.h>
static void _sighandler_posix_signal_handler(int sig) {
platform_shutdown();
}
#endif
void sighandler_register() {
2021-06-01 13:20:04 +00:00
#ifdef ZPL_SYSTEM_WINDOWS
2021-01-11 21:12:26 +00:00
{
if (!SetConsoleCtrlHandler(_sighandler_win32_control_handler, 1)) {
zpl_printf("Could not set up signal handler!\n");
}
}
2021-06-01 13:20:04 +00:00
#else // POSIX compliant
2021-01-11 21:12:26 +00:00
signal(SIGINT, &_sighandler_posix_signal_handler);
signal(SIGTERM, &_sighandler_posix_signal_handler);
2021-06-01 13:20:04 +00:00
#endif
2021-01-11 21:12:26 +00:00
}
void sighandler_unregister() {
2021-06-01 13:20:04 +00:00
#ifdef ZPL_SYSTEM_WINDOWS
2021-01-11 21:12:26 +00:00
{
if (!SetConsoleCtrlHandler(_sighandler_win32_control_handler, 0)) {
zpl_printf("Could not uninstall signal handler!");
}
}
2021-06-01 13:20:04 +00:00
#else // POSIX compliant
#endif
2021-01-11 21:12:26 +00:00
}