From c87d64e19a6d061b03bb6722595571e6c068a333 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Wed, 14 Aug 2002 00:01:57 +0000 Subject: first --- src/SignalHandler.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ src/SignalHandler.hh | 69 +++++++++++++++++++++++++++++++++++++++++++++ src/tests/signaltest.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 src/SignalHandler.cc create mode 100644 src/SignalHandler.hh create mode 100644 src/tests/signaltest.cc diff --git a/src/SignalHandler.cc b/src/SignalHandler.cc new file mode 100644 index 0000000..67f83fa --- /dev/null +++ b/src/SignalHandler.cc @@ -0,0 +1,75 @@ +// SignalHandler.cc for Fluxbox Window Manager +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: SignalHandler.cc,v 1.1 2002/08/13 23:57:04 fluxgen Exp $ + +#include "SignalHandler.hh" + +namespace FbTk { + +SignalHandler::EventHandler *SignalHandler::s_signal_handler[NSIG]; + +SignalHandler::SignalHandler() { + // clear signal list + for (int i=0; i < NSIG; ++i) + s_signal_handler[i] = 0; +} + +SignalHandler *SignalHandler::instance() { + static SignalHandler singleton; + return &singleton; +} + +bool SignalHandler::registerHandler(int signum, EventHandler *eh, EventHandler **oldhandler_ret) { + // must be less than NSIG + if (signum >= NSIG) + return false; + + // get old signal handler for this signum + if (oldhandler_ret != 0) + *oldhandler_ret = s_signal_handler[signum]; + + struct sigaction sa; + // set callback + sa.sa_handler = SignalHandler::handleSignal; + sigemptyset (&sa.sa_mask); + sa.sa_flags = 0; + + if (sigaction(signum, &sa, 0) == -1) + return false; + + s_signal_handler[signum] = eh; + + return true; +} + +void SignalHandler::removeHandler(int signum) { + if (signum < NSIG) + s_signal_handler[signum] = 0; // clear handler pointer +} + +void SignalHandler::handleSignal(int signum) { + // make sure we got a handler for this signal + if (s_signal_handler[signum] != 0) + s_signal_handler[signum]->handleSignal(signum); +} + +}; diff --git a/src/SignalHandler.hh b/src/SignalHandler.hh new file mode 100644 index 0000000..81eec9f --- /dev/null +++ b/src/SignalHandler.hh @@ -0,0 +1,69 @@ +// SignalHandler.hh for Fluxbox Window Manager +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: SignalHandler.hh,v 1.1 2002/08/13 23:57:04 fluxgen Exp $ + +#ifndef FBTK_SIGNALHANDLER_HH +#define FBTK_SIGNALHANDLER_HH + +#include + +namespace FbTk { + +/** + Handles system signals, singleton. +*/ +class SignalHandler { +public: + /** + Inherit this class to handle signals. + */ + class EventHandler { + public: + virtual void handleSignal(int signum) = 0; + }; + + /// get singleton object + static SignalHandler *instance(); + /** + Register an event handler + @return true on success else false + @param signum signal number + @param eh event handler + @param oldhandler_ret return handler to old sighandler + */ + bool registerHandler(int signum, EventHandler *eh, EventHandler **oldhandler_ret = 0); + /** + removes the signum handler + @param signum signal number + */ + void removeHandler(int signum); +private: + SignalHandler(); + + static void handleSignal(int signum); + + static EventHandler *s_signal_handler[NSIG]; ///< NSIG defined in signal.h +}; + +}; // end namespace FbTk + +#endif // FBTK_SIGNALHANDLER_HH diff --git a/src/tests/signaltest.cc b/src/tests/signaltest.cc new file mode 100644 index 0000000..7d0b53a --- /dev/null +++ b/src/tests/signaltest.cc @@ -0,0 +1,75 @@ +// signaltest.cc for testing signal handler in fluxbox +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#include "../SignalHandler.hh" + +#include + +using namespace std; +using namespace FbTk; + +class IntSig:public SignalHandler::EventHandler { +public: + void handleSignal(int signum) { + assert(signum == SIGINT); + cerr<<"Signal SIGINT!"<