From 365b0c6330ea607706b708d92da7a46b1ed1fb15 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 2 Feb 2008 11:09:04 +0000 Subject: introduced libsys implemented signal handling --- src/Makefile.am | 2 +- src/client/Makefile.am | 6 ++---- src/client/input.cc | 4 ++-- src/common/common.h | 2 ++ src/core/Makefile.am | 2 +- src/core/applicationinterface.cc | 30 +++++++++++++++++++++++++++- src/filesystem/Makefile.am | 3 ++- src/server/Makefile.am | 6 ++---- src/sys/Makefile.am | 6 ++++++ src/sys/sys.cc | 42 ++++++++++++++++++++++++++++++++++++++++ src/sys/sys.h | 19 ++++++++++++++++++ 11 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 src/sys/Makefile.am create mode 100644 src/sys/sys.cc create mode 100644 src/sys/sys.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index ab95099..ad350fc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ # set the include path found by configure AM_CPPFLAGS = $(DEBUG_CFLAGS) $(WARN_CFLAGS) $(all_includes) -SUBDIRS = math common filesystem core game server gl client +SUBDIRS = math common sys filesystem core game server gl client diff --git a/src/client/Makefile.am b/src/client/Makefile.am index e191f5f..9807963 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -3,10 +3,8 @@ METASOURCES = AUTO bin_PROGRAMS = osirion osirion_LDADD = $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/common/libcommon.la \ - $(top_builddir)/src/filesystem/libfilesystem.la \ - $(top_builddir)/src/core/libcore.la \ - $(top_builddir)/src/game/libgame.la \ + $(top_builddir)/src/common/libcommon.la $(top_builddir)/src/sys/libsys.la \ + $(top_builddir)/src/filesystem/libfilesystem.la $(top_builddir)/src/core/libcore.la $(top_builddir)/src/game/libgame.la \ $(top_builddir)/src/gl/libgl.la osirion_SOURCES = application.cc camera.cc client.cc console.cc hud.cc input.cc main.cc \ shipdrawer.cc stardrawer.cc video.cc view.cc diff --git a/src/client/input.cc b/src/client/input.cc index 5f38d35..3e37a19 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -46,7 +46,7 @@ void Input::handle_keypressed(SDL_keysym* keysym) { switch( keysym->sym ) { case SDLK_ESCAPE: - client::application.shutdown(); + application.shutdown(); break; case SDLK_LEFT: camera.rotate_left(); @@ -91,7 +91,7 @@ void Input::process() handle_keyreleased( &event.key.keysym ); break; case SDL_QUIT: - client::application.shutdown(); + application.shutdown(); break; } diff --git a/src/common/common.h b/src/common/common.h index 61aa47c..2fdec9d 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -10,6 +10,8 @@ #include "config.h" /// common functions and components that can be use by any subsytem +/** libcommon has no dependencies + */ namespace common {} #include "common/consoleinterface.h" diff --git a/src/core/Makefile.am b/src/core/Makefile.am index b062f9e..8046b79 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -4,7 +4,7 @@ INCLUDES = -I$(top_srcdir)/src libcore_la_SOURCES = applicationinterface.cc gameinterface.cc libcore_la_LDFLAGS = -avoid-version libcore_la_LIBADD = $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/common/libcommon.la \ + $(top_builddir)/src/common/libcommon.la $(top_builddir)/src/sys/libsys.la \ $(top_builddir)/src/filesystem/libfilesystem.la noinst_LTLIBRARIES = libcore.la diff --git a/src/core/applicationinterface.cc b/src/core/applicationinterface.cc index 9ef5c16..ba22d8c 100644 --- a/src/core/applicationinterface.cc +++ b/src/core/applicationinterface.cc @@ -6,14 +6,37 @@ #include "core/core.h" #include "filesystem/filesystem.h" +#include "sys/sys.h" #include "common/common.h" #include #include +#include +#include + namespace core { +extern "C" void signal_handler(int signum) +{ + switch(signum){ + case SIGHUP: + case SIGINT: + case SIGQUIT: + case SIGTERM: + con_warn << "received signal " << signum << ", shutting down..." << std::endl; + if (ApplicationInterface::instance()); + ApplicationInterface::instance()->shutdown(); + exit(1); + break; + default: + con_warn << "received signal " << signum << ", terminated..." << std::endl; + exit(1); + break; + } +} + ApplicationInterface *ApplicationInterface::applicationinterface_instance = 0; ApplicationInterface::ApplicationInterface() { @@ -22,6 +45,11 @@ ApplicationInterface::ApplicationInterface() { exit(2); } applicationinterface_instance = this; + + sys::signal(SIGHUP, signal_handler); + sys::signal(SIGINT, signal_handler); + sys::signal(SIGQUIT, signal_handler); + sys::signal(SIGTERM, signal_handler); } ApplicationInterface::~ApplicationInterface() @@ -29,7 +57,7 @@ ApplicationInterface::~ApplicationInterface() applicationinterface_instance = 0; } -ApplicationInterface *ApplicationInterface::instance() +ApplicationInterface *ApplicationInterface::instance() { return applicationinterface_instance; } diff --git a/src/filesystem/Makefile.am b/src/filesystem/Makefile.am index c072b02..2eb094f 100644 --- a/src/filesystem/Makefile.am +++ b/src/filesystem/Makefile.am @@ -2,7 +2,8 @@ METASOURCES = AUTO libfilesystem_la_SOURCES = file.cc filesystem.cc inifile.cc path.cc libfilesystem_la_LDFLAGS = -avoid-version -no-undefined -libfilesystem_la_LIBADD = $(top_builddir)/src/common/libcommon.la +libfilesystem_la_LIBADD = $(top_builddir)/src/sys/libsys.la \ + $(top_builddir)/src/common/libcommon.la noinst_LTLIBRARIES = libfilesystem.la noinst_HEADERS = file.h filesystem.h inifile.h path.h diff --git a/src/server/Makefile.am b/src/server/Makefile.am index c8695ca..729fabf 100644 --- a/src/server/Makefile.am +++ b/src/server/Makefile.am @@ -2,10 +2,8 @@ METASOURCES = AUTO bin_PROGRAMS = osiriond osiriond_SOURCES = application.cc console.cc main.cc server.cc timer.cc osiriond_LDADD = $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/common/libcommon.la \ - $(top_builddir)/src/filesystem/libfilesystem.la \ - $(top_builddir)/src/core/libcore.la \ - $(top_builddir)/src/game/libgame.la + $(top_builddir)/src/common/libcommon.la $(top_builddir)/src/sys/libsys.la \ + $(top_builddir)/src/filesystem/libfilesystem.la $(top_builddir)/src/core/libcore.la $(top_builddir)/src/game/libgame.la noinst_HEADERS = application.h console.h server.h timer.h INCLUDES = -I$(top_srcdir)/src diff --git a/src/sys/Makefile.am b/src/sys/Makefile.am new file mode 100644 index 0000000..44f69c4 --- /dev/null +++ b/src/sys/Makefile.am @@ -0,0 +1,6 @@ +INCLUDES = -I$(top_srcdir)/src +METASOURCES = AUTO +libsys_la_LDFLAGS = -avoid-version -no-undefined +noinst_LTLIBRARIES = libsys.la +libsys_la_SOURCES = sys.cc +noinst_HEADERS = sys.h diff --git a/src/sys/sys.cc b/src/sys/sys.cc new file mode 100644 index 0000000..97da23e --- /dev/null +++ b/src/sys/sys.cc @@ -0,0 +1,42 @@ +/* + sys/sys.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +// project headers +#include "sys/sys.h" + +// system headers +#include + +#include +#include + + +namespace sys { + +bool mkdir(const char *path) +{ +#ifdef _WIN32 + ::mkdir(path); + // FIXME check return value + return true; +#else + return (::mkdir(path, 0777) == 0); +#endif +} + +void signal(int signum, signalfunc handler) +{ +#ifndef _WIN32 + struct sigaction sa; + + sa.sa_handler = handler; + sa.sa_flags = 0; + + sigaction(signum, &sa, 0); +#endif +} + +} diff --git a/src/sys/sys.h b/src/sys/sys.h new file mode 100644 index 0000000..0d05b67 --- /dev/null +++ b/src/sys/sys.h @@ -0,0 +1,19 @@ +/* + sys/sys.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_SYS_H__ +#define __INCLUDED_SYS_H__ + +/// contains operating system dependent functions +namespace sys { + typedef void (* signalfunc)(int signum); + + bool mkdir(const char *path); + void signal(int signum, signalfunc handler); +} + +#endif // __INCLUDED_SYS_H__ + -- cgit v1.2.3