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 <QBasicTimer>
34 #include <QCoreApplication>
35 #include <QDebug>
36 #include <QPointer>
37 #include <wtf/CurrentTime.h>
38
39 namespace WebCore {
40
41 class SharedTimerQt : public QObject {
42 Q_OBJECT
43
44 friend void setSharedTimerFiredFunction(void (*f)());
45 public:
46 static SharedTimerQt* inst();
47
48 void start(double);
49 void stop();
50
51 protected:
52 void timerEvent(QTimerEvent* ev);
53
54 private slots:
55 void destroy();
56
57 private:
58 SharedTimerQt();
59 ~SharedTimerQt();
60 QBasicTimer m_timer;
61 void (*m_timerFunction)();
62 };
63
SharedTimerQt()64 SharedTimerQt::SharedTimerQt()
65 : QObject()
66 , m_timerFunction(0)
67 {}
68
~SharedTimerQt()69 SharedTimerQt::~SharedTimerQt()
70 {
71 if (m_timer.isActive()) {
72 if (m_timerFunction) {
73 (m_timerFunction)();
74 m_timerFunction = 0;
75 }
76 }
77 }
78
destroy()79 void SharedTimerQt::destroy()
80 {
81 delete this;
82 }
83
inst()84 SharedTimerQt* SharedTimerQt::inst()
85 {
86 static QPointer<SharedTimerQt> timer;
87 if (!timer) {
88 timer = new SharedTimerQt();
89 timer->connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), SLOT(destroy()));
90 }
91
92 return timer;
93 }
94
start(double fireTime)95 void SharedTimerQt::start(double fireTime)
96 {
97 double interval = fireTime - currentTime();
98 unsigned int intervalInMS;
99 if (interval < 0)
100 intervalInMS = 0;
101 else {
102 interval *= 1000;
103 intervalInMS = (unsigned int)interval;
104 }
105
106 m_timer.start(intervalInMS, this);
107 }
108
stop()109 void SharedTimerQt::stop()
110 {
111 m_timer.stop();
112 }
113
timerEvent(QTimerEvent * ev)114 void SharedTimerQt::timerEvent(QTimerEvent* ev)
115 {
116 if (!m_timerFunction || ev->timerId() != m_timer.timerId())
117 return;
118
119 m_timer.stop();
120 (m_timerFunction)();
121 }
122
setSharedTimerFiredFunction(void (* f)())123 void setSharedTimerFiredFunction(void (*f)())
124 {
125 if (!QCoreApplication::instance())
126 return;
127
128 SharedTimerQt::inst()->m_timerFunction = f;
129 }
130
setSharedTimerFireTime(double fireTime)131 void setSharedTimerFireTime(double fireTime)
132 {
133 if (!QCoreApplication::instance())
134 return;
135
136 SharedTimerQt::inst()->start(fireTime);
137 }
138
stopSharedTimer()139 void stopSharedTimer()
140 {
141 if (!QCoreApplication::instance())
142 return;
143
144 SharedTimerQt::inst()->stop();
145 }
146
147 #include "SharedTimerQt.moc"
148
149 }
150
151 // vim: ts=4 sw=4 et
152