]> Dogcows Code - chaz/openbox/blobdiff - src/Timer.cc
caps
[chaz/openbox] / src / Timer.cc
index 8cd4714eb00b14bfaeec07ebd06e1d0fe2a673b1..99f05ba9360f2166e10baee3afe4877ed34ddde6 100644 (file)
@@ -1,5 +1,6 @@
-// Timer.cc for Openbox
-// Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
+// -*- mode: C++; indent-tabs-mode: nil; -*-
+// Timer.cc for Blackbox - An X11 Window Manager
+// Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
 //
 // Permission is hereby granted, free of charge, to any person obtaining a
 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 // DEALINGS IN THE SOFTWARE.
 
-// stupid macros needed to access some functions in version 2 of the GNU C
-// library
-#ifndef   _GNU_SOURCE
-#  define _GNU_SOURCE
-#endif // _GNU_SOURCE
-
 #ifdef    HAVE_CONFIG_H
 #  include "../config.h"
 #endif // HAVE_CONFIG_H
 
-#include "BaseDisplay.h"
-#include "Timer.h"
+#include "BaseDisplay.hh"
+#include "Timer.hh"
+#include "Util.hh"
+
+BTimer::BTimer(TimerQueueManager *m, TimeoutHandler *h) {
+  manager = m;
+  handler = h;
 
-BTimer::BTimer(BaseDisplay &d, TimeoutHandler &h) : display(d), handler(h) {
-  once = timing = False;
+  recur = timing = False;
 }
 
+
 BTimer::~BTimer(void) {
   if (timing) stop();
 }
 
+
 void BTimer::setTimeout(long t) {
   _timeout.tv_sec = t / 1000;
-  _timeout.tv_usec = t;
-  _timeout.tv_usec -= (_timeout.tv_sec * 1000);
+  _timeout.tv_usec = t % 1000;
   _timeout.tv_usec *= 1000;
-  if (timing) {
-    display.removeTimer(this);
-    display.addTimer(this);     // reorder the display
-  }
 }
 
-void BTimer::setTimeout(timeval t) {
+
+void BTimer::setTimeout(const timeval &t) {
   _timeout.tv_sec = t.tv_sec;
   _timeout.tv_usec = t.tv_usec;
 }
 
+
 void BTimer::start(void) {
   gettimeofday(&_start, 0);
 
   if (! timing) {
     timing = True;
-    display.addTimer(this);
+    manager->addTimer(this);
   }
 }
 
+
 void BTimer::stop(void) {
-  if (timing) {
-    timing = False;
+  timing = False;
 
-    display.removeTimer(this);
-  }
+  manager->removeTimer(this);
 }
 
+
+void BTimer::halt(void) {
+  timing = False;
+}
+
+
 void BTimer::fireTimeout(void) {
-  handler.timeout();
+  if (handler)
+    handler->timeout();
+}
+
+
+timeval BTimer::timeRemaining(const timeval &tm) const {
+  timeval ret = endpoint();
+
+  ret.tv_sec  -= tm.tv_sec;
+  ret.tv_usec -= tm.tv_usec;
+
+  return normalizeTimeval(ret);
+}
+
+
+timeval BTimer::endpoint(void) const {
+  timeval ret;
+
+  ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
+  ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
+
+  return normalizeTimeval(ret);
+}
+
+
+bool BTimer::shouldFire(const timeval &tm) const {
+  timeval end = endpoint();
+
+  return ! ((tm.tv_sec < end.tv_sec) ||
+            (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
 }
This page took 0.025627 seconds and 4 git commands to generate.