|
1 /* |
|
2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
|
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * 1. Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * 2. Redistributions in binary form must reproduce the above copyright |
|
11 * notice, this list of conditions and the following disclaimer in the |
|
12 * documentation and/or other materials provided with the distribution. |
|
13 * |
|
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
|
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
|
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
|
24 * THE POSSIBILITY OF SUCH DAMAGE. |
|
25 */ |
|
26 |
|
27 #include "RunLoop.h" |
|
28 |
|
29 #include "WorkItem.h" |
|
30 |
|
31 #include <QApplication> |
|
32 #include <QAbstractEventDispatcher> |
|
33 #include <QObject> |
|
34 #include <QMetaMethod> |
|
35 #include <QMetaObject> |
|
36 #include <QTimerEvent> |
|
37 |
|
38 class RunLoop::TimerObject : public QObject |
|
39 { |
|
40 Q_OBJECT |
|
41 public: |
|
42 TimerObject(RunLoop* runLoop) : m_runLoop(runLoop) |
|
43 { |
|
44 int methodIndex = metaObject()->indexOfMethod("performWork()"); |
|
45 m_method = metaObject()->method(methodIndex); |
|
46 } |
|
47 |
|
48 Q_SLOT void performWork() { m_runLoop->performWork(); } |
|
49 inline void wakeUp() { m_method.invoke(this, Qt::QueuedConnection); } |
|
50 |
|
51 protected: |
|
52 virtual void timerEvent(QTimerEvent* event) |
|
53 { |
|
54 RunLoop::TimerBase::timerFired(m_runLoop, event->timerId()); |
|
55 } |
|
56 |
|
57 private: |
|
58 RunLoop* m_runLoop; |
|
59 QMetaMethod m_method; |
|
60 }; |
|
61 |
|
62 void RunLoop::run() |
|
63 { |
|
64 QCoreApplication::exec(); |
|
65 } |
|
66 |
|
67 void RunLoop::stop() |
|
68 { |
|
69 // implement |
|
70 } |
|
71 |
|
72 RunLoop::RunLoop() |
|
73 : m_timerObject(new TimerObject(this)) |
|
74 { |
|
75 } |
|
76 |
|
77 RunLoop::~RunLoop() |
|
78 { |
|
79 delete m_timerObject; |
|
80 } |
|
81 |
|
82 void RunLoop::wakeUp() |
|
83 { |
|
84 m_timerObject->wakeUp(); |
|
85 } |
|
86 |
|
87 // RunLoop::Timer |
|
88 |
|
89 void RunLoop::TimerBase::timerFired(RunLoop* runLoop, int ID) |
|
90 { |
|
91 TimerMap::iterator it = runLoop->m_activeTimers.find(ID); |
|
92 ASSERT(it != runLoop->m_activeTimers.end()); |
|
93 TimerBase* timer = it->second; |
|
94 |
|
95 // FIMXE: Support repeating timers. |
|
96 timer->stop(); |
|
97 timer->fired(); |
|
98 } |
|
99 |
|
100 RunLoop::TimerBase::TimerBase(RunLoop* runLoop) |
|
101 : m_runLoop(runLoop) |
|
102 , m_ID(0) |
|
103 { |
|
104 } |
|
105 |
|
106 RunLoop::TimerBase::~TimerBase() |
|
107 { |
|
108 stop(); |
|
109 } |
|
110 |
|
111 void RunLoop::TimerBase::start(double nextFireInterval, double /*repeatInterval*/) |
|
112 { |
|
113 // FIMXE: Support repeating timers. |
|
114 stop(); |
|
115 int millis = static_cast<int>(nextFireInterval * 1000); |
|
116 m_ID = m_runLoop->m_timerObject->startTimer(millis); |
|
117 ASSERT(m_ID); |
|
118 m_runLoop->m_activeTimers.set(m_ID, this); |
|
119 } |
|
120 |
|
121 void RunLoop::TimerBase::stop() |
|
122 { |
|
123 if (!m_ID) |
|
124 return; |
|
125 TimerMap::iterator it = m_runLoop->m_activeTimers.find(m_ID); |
|
126 if (it == m_runLoop->m_activeTimers.end()) |
|
127 return; |
|
128 |
|
129 m_runLoop->m_activeTimers.remove(it); |
|
130 m_runLoop->m_timerObject->killTimer(m_ID); |
|
131 m_ID = 0; |
|
132 } |
|
133 |
|
134 bool RunLoop::TimerBase::isActive() const |
|
135 { |
|
136 return m_ID; |
|
137 } |
|
138 |
|
139 #include "RunLoopQt.moc" |