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