1 // Timer.hh for FbTk - Fluxbox Toolkit 2 // Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen at fluxbox dot org) 3 // 4 // Timer.hh for Blackbox - An X11 Window Manager 5 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net) 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a 8 // copy of this software and associated documentation files (the "Software"), 9 // to deal in the Software without restriction, including without limitation 10 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 // and/or sell copies of the Software, and to permit persons to whom the 12 // Software is furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 // DEALINGS IN THE SOFTWARE. 24 25 #ifndef FBTK_TIMER_HH 26 #define FBTK_TIMER_HH 27 28 #include "RefCount.hh" 29 #include "Command.hh" 30 #include "FbTime.hh" 31 32 #include <string> 33 34 namespace FbTk { 35 36 /** 37 Handles Timeout 38 */ 39 class Timer { 40 public: 41 Timer(); 42 explicit Timer(const RefCount<Slot<void> > &handler); 43 ~Timer(); 44 fireOnce(bool once)45 void fireOnce(bool once) { m_once = once; } 46 void setTimeout(uint64_t timeout, bool force_start = false); 47 void setCommand(const RefCount<Slot<void> > &cmd); 48 49 template<typename Functor> setFunctor(const Functor & functor)50 void setFunctor(const Functor &functor) { 51 setCommand(RefCount<Slot<void> >(new SlotImpl<Functor, void>(functor))); 52 } 53 setInterval(int seconds)54 void setInterval(int seconds) { m_interval = seconds; } 55 void start(); 56 void stop(); 57 58 static void updateTimers(int file_descriptor); 59 60 int isTiming() const; getInterval() const61 int getInterval() const { return m_interval; } 62 doOnce() const63 int doOnce() const { return m_once; } 64 getTimeout() const65 uint64_t getTimeout() const { return m_timeout; } getStartTime() const66 uint64_t getStartTime() const { return m_start; } 67 uint64_t getEndTime() const; 68 69 protected: 70 /// force a timeout 71 void fireTimeout(); 72 73 private: 74 RefCount<Slot<void> > m_handler; ///< what to do on a timeout 75 76 bool m_once; ///< do timeout only once? 77 int m_interval; ///< Is an interval-only timer (e.g. clock), in seconds 78 79 uint64_t m_start; ///< start time in microseconds 80 uint64_t m_timeout; ///< time length in microseconds 81 }; 82 83 84 85 /// executes a command after a specified timeout 86 class DelayedCmd: public Command<void> { 87 public: 88 89 // timeout in microseconds 90 DelayedCmd(const RefCount<Slot<void> > &cmd, uint64_t timeout = 200); 91 92 // this constructor has inverted order of parameters to avoid ambiguity with the previous 93 // constructor 94 template<typename Functor> DelayedCmd(uint64_t timeout,const Functor & functor)95 DelayedCmd(uint64_t timeout, const Functor &functor) { 96 initTimer(timeout); 97 m_timer.setFunctor(functor); 98 } 99 100 void execute(); 101 static Command<void> *parse(const std::string &command, 102 const std::string &args, bool trusted); 103 private: 104 void initTimer(uint64_t timeout); 105 106 Timer m_timer; 107 }; 108 109 } // end namespace FbTk 110 111 #endif // FBTK_TIMER_HH 112