• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 George Staikos <staikos@kde.org>
3  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
4  * Copyright (C) 2008 Holger Hans Peter Freyther
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 
31 #include "config.h"
32 
33 #include <wtf/CurrentTime.h>
34 
35 #include <QBasicTimer>
36 #include <QCoreApplication>
37 #include <QDebug>
38 #include <QPointer>
39 
40 namespace WebCore {
41 
42 class SharedTimerQt : public QObject {
43     friend void setSharedTimerFiredFunction(void (*f)());
44 public:
45     static SharedTimerQt* inst();
46 
47     void start(double);
48     void stop();
49 
50 protected:
51     void timerEvent(QTimerEvent* ev);
52 
53 private:
54     SharedTimerQt(QObject* parent);
55     ~SharedTimerQt();
56     QBasicTimer m_timer;
57     void (*m_timerFunction)();
58 };
59 
SharedTimerQt(QObject * parent)60 SharedTimerQt::SharedTimerQt(QObject* parent)
61     : QObject(parent)
62     , m_timerFunction(0)
63 {}
64 
~SharedTimerQt()65 SharedTimerQt::~SharedTimerQt()
66 {
67     if (m_timer.isActive())
68         (m_timerFunction)();
69 }
70 
inst()71 SharedTimerQt* SharedTimerQt::inst()
72 {
73     static QPointer<SharedTimerQt> timer;
74     if (!timer)
75         timer = new SharedTimerQt(QCoreApplication::instance());
76 
77     return timer;
78 }
79 
start(double fireTime)80 void SharedTimerQt::start(double fireTime)
81 {
82     double interval = fireTime - currentTime();
83     unsigned int intervalInMS;
84     if (interval < 0)
85         intervalInMS = 0;
86     else {
87         interval *= 1000;
88         intervalInMS = (unsigned int)interval;
89     }
90 
91     m_timer.start(intervalInMS, this);
92 }
93 
stop()94 void SharedTimerQt::stop()
95 {
96     m_timer.stop();
97 }
98 
timerEvent(QTimerEvent * ev)99 void SharedTimerQt::timerEvent(QTimerEvent* ev)
100 {
101     if (!m_timerFunction || ev->timerId() != m_timer.timerId())
102         return;
103 
104     m_timer.stop();
105     (m_timerFunction)();
106 }
107 
setSharedTimerFiredFunction(void (* f)())108 void setSharedTimerFiredFunction(void (*f)())
109 {
110     if (!QCoreApplication::instance())
111         return;
112 
113     SharedTimerQt::inst()->m_timerFunction = f;
114 }
115 
setSharedTimerFireTime(double fireTime)116 void setSharedTimerFireTime(double fireTime)
117 {
118     if (!QCoreApplication::instance())
119         return;
120 
121     SharedTimerQt::inst()->start(fireTime);
122 }
123 
stopSharedTimer()124 void stopSharedTimer()
125 {
126     if (!QCoreApplication::instance())
127         return;
128 
129     SharedTimerQt::inst()->stop();
130 }
131 
132 }
133 
134 // vim: ts=4 sw=4 et
135