signal handling

isolation_bkp/dynres
Dominik Madarász 2021-01-11 22:12:26 +01:00
parent 617e9742d3
commit f74580b3cb
7 changed files with 72 additions and 5 deletions

View File

@ -23,7 +23,9 @@ add_library(client-common STATIC
source/network.c
source/game.c
source/main.c
source/signal_handling.c
header/signal_handling.h
header/network.h
header/platform.h
header/game.h

View File

@ -0,0 +1,4 @@
#pragma once
void sighandler_register();
void sighandler_unregister();

View File

@ -6,7 +6,7 @@ void game_init() {
}
void game_shutdown() {
platform_shutdown();
//
}
uint8_t game_is_running() {

View File

@ -1,9 +1,11 @@
#define ZPL_IMPL
#include "zpl.h"
#include "game.h"
#include "signal_handling.h"
int main(void)
{
sighandler_register();
game_init();
while (game_is_running())
@ -14,5 +16,6 @@ int main(void)
}
game_shutdown();
sighandler_unregister();
return 0;
}

View File

@ -1,4 +1,3 @@
#define ZPL_IMPL
#include "zpl.h"
#define LIBRG_IMPL

View File

@ -1,18 +1,24 @@
#include "platform.h"
#include "zpl.h"
#include <stdio.h>
uint8_t is_running;
void platform_init() {
printf("eco2d-cli\n");
is_running = 1;
}
void platform_shutdown() {
printf("Bye!\n");
printf("\nBye!\n");
is_running = 0;
}
uint8_t platform_is_running() {
return is_running;
}
void platform_render() {
// TODO
zpl_printf("o");
zpl_sleep_ms(1000);
}

View File

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