all repos — fluxbox @ c87d64e19a6d061b03bb6722595571e6c068a333

custom fork of the fluxbox windowmanager

first
fluxgen fluxgen
commit

c87d64e19a6d061b03bb6722595571e6c068a333

parent

fdd8313050bb2f36a204e9104dc78c9c268e4d52

3 files changed, 219 insertions(+), 0 deletions(-)

jump to
A 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); +} + +};
A 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 <signal.h> + +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
A 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 <iostream> + +using namespace std; +using namespace FbTk; + +class IntSig:public SignalHandler::EventHandler { +public: + void handleSignal(int signum) { + assert(signum == SIGINT); + cerr<<"Signal SIGINT!"<<endl; + exit(0); + } +}; + +class AllSig:public SignalHandler::EventHandler { +public: + void handleSignal(int signum) { + switch (signum) { + case SIGTERM: + cerr<<"SIGTERM"; + break; + case SIGUSR1: + cerr<<"SIGUSR1"; + break; + case SIGUSR2: + cerr<<"SIGUSR2"; + break; + default: + cerr<<"signum = "<<signum; + } + cerr<<endl; + if (signum == SIGTERM) + exit(1); // end program + } +}; + +int main(int argc, char **argv) { + SignalHandler *sigh = SignalHandler::instance(); + IntSig handler; + AllSig allhand; + + sigh->registerHandler(SIGINT, &handler); + sigh->registerHandler(SIGTERM, &allhand); + sigh->registerHandler(SIGUSR1, &allhand); + sigh->registerHandler(SIGUSR2, &allhand); + cerr<<"Send signals to me :)"<<endl; + while (1) { + + } +} + +