From 713f5cd3194be972beee0e207fa1a39044d3c407 Mon Sep 17 00:00:00 2001 From: Thomas McGuire Date: Wed, 14 Mar 2012 11:51:51 +0100 Subject: Factor out getTimestamp() The code was duplicated, and we need it again for blackberry. Change-Id: Ib97b8481eac5918d077096a0cad1babf2fc00d1a Reviewed-by: Thomas McGuire Reviewed-by: Lorn Potter --- plugins/sensors/dummy/dummy.pri | 2 + plugins/sensors/dummy/dummyaccelerometer.cpp | 1 + plugins/sensors/dummy/dummycommon.cpp | 35 ------------- plugins/sensors/dummy/dummycommon.h | 3 -- plugins/sensors/dummy/dummylightsensor.cpp | 1 + plugins/sensors/n900/n900.pri | 2 + plugins/sensors/n900/n900accelerometer.cpp | 1 + plugins/sensors/n900/n900filebasedsensor.cpp | 13 ----- plugins/sensors/n900/n900filebasedsensor.h | 3 -- plugins/sensors/n900/n900lightsensor.cpp | 1 + plugins/sensors/n900/n900proximitysensor.cpp | 1 + plugins/sensors/util/util.cpp | 78 ++++++++++++++++++++++++++++ plugins/sensors/util/util.h | 50 ++++++++++++++++++ 13 files changed, 137 insertions(+), 54 deletions(-) create mode 100644 plugins/sensors/util/util.cpp create mode 100644 plugins/sensors/util/util.h diff --git a/plugins/sensors/dummy/dummy.pri b/plugins/sensors/dummy/dummy.pri index 502081c7a9..08011b5fae 100644 --- a/plugins/sensors/dummy/dummy.pri +++ b/plugins/sensors/dummy/dummy.pri @@ -1,9 +1,11 @@ HEADERS += dummycommon.h\ dummyaccelerometer.h\ dummylightsensor.h\ + ../util/util.h SOURCES += dummycommon.cpp\ dummyaccelerometer.cpp\ dummylightsensor.cpp\ main.cpp\ + ../util/util.cpp diff --git a/plugins/sensors/dummy/dummyaccelerometer.cpp b/plugins/sensors/dummy/dummyaccelerometer.cpp index b747452ccf..d4492344a0 100644 --- a/plugins/sensors/dummy/dummyaccelerometer.cpp +++ b/plugins/sensors/dummy/dummyaccelerometer.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "dummyaccelerometer.h" +#include "../util/util.h" #include #include diff --git a/plugins/sensors/dummy/dummycommon.cpp b/plugins/sensors/dummy/dummycommon.cpp index 5eb9f1901f..d3632fedf4 100644 --- a/plugins/sensors/dummy/dummycommon.cpp +++ b/plugins/sensors/dummy/dummycommon.cpp @@ -41,14 +41,6 @@ #include "dummycommon.h" -#ifdef Q_OS_WINCE -#include -// WINCE has but using clock() gives a link error because -// the function isn't actually implemented. -#else -#include -#endif - dummycommon::dummycommon(QSensor *sensor) : QSensorBackend(sensor) , m_timerid(0) @@ -87,30 +79,3 @@ void dummycommon::timerEvent(QTimerEvent * /*event*/) { poll(); } - -quint64 dummycommon::getTimestamp() -{ -#ifdef Q_OS_WINCE - // This implementation is based on code found here: - // http://social.msdn.microsoft.com/Forums/en/vssmartdevicesnative/thread/74870c6c-76c5-454c-8533-812cfca585f8 - HANDLE currentThread = GetCurrentThread(); - FILETIME creationTime, exitTime, kernalTime, userTime; - GetThreadTimes(currentThread, &creationTime, &exitTime, &kernalTime, &userTime); - - ULARGE_INTEGER uli; - uli.LowPart = userTime.dwLowDateTime; - uli.HighPart = userTime.dwHighDateTime; - ULONGLONG systemTimeInMS = uli.QuadPart/10000; - return static_cast(systemTimeInMS); -#else - struct timespec tv; - int ok; - - ok = clock_gettime(CLOCK_MONOTONIC, &tv); - Q_ASSERT(ok == 0); - - quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds - return result; -#endif -} - diff --git a/plugins/sensors/dummy/dummycommon.h b/plugins/sensors/dummy/dummycommon.h index 0f5de94a57..45592d50da 100644 --- a/plugins/sensors/dummy/dummycommon.h +++ b/plugins/sensors/dummy/dummycommon.h @@ -56,9 +56,6 @@ public: virtual void poll() = 0; void timerEvent(QTimerEvent * /*event*/); -protected: - quint64 getTimestamp(); - private: int m_timerid; }; diff --git a/plugins/sensors/dummy/dummylightsensor.cpp b/plugins/sensors/dummy/dummylightsensor.cpp index b5048f6313..93744f0eec 100644 --- a/plugins/sensors/dummy/dummylightsensor.cpp +++ b/plugins/sensors/dummy/dummylightsensor.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "dummylightsensor.h" +#include "../util/util.h" #include #include diff --git a/plugins/sensors/n900/n900.pri b/plugins/sensors/n900/n900.pri index bf2a069fa4..d5df98ad26 100644 --- a/plugins/sensors/n900/n900.pri +++ b/plugins/sensors/n900/n900.pri @@ -2,10 +2,12 @@ HEADERS += n900filebasedsensor.h\ n900accelerometer.h\ n900lightsensor.h\ n900proximitysensor.h\ + ../util/util.h SOURCES += n900filebasedsensor.cpp\ n900accelerometer.cpp\ n900lightsensor.cpp\ n900proximitysensor.cpp\ main.cpp\ + ../util/util.cpp diff --git a/plugins/sensors/n900/n900accelerometer.cpp b/plugins/sensors/n900/n900accelerometer.cpp index cca61c35b7..45a3298e24 100644 --- a/plugins/sensors/n900/n900accelerometer.cpp +++ b/plugins/sensors/n900/n900accelerometer.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "n900accelerometer.h" +#include "../util/util.h" #include #include #include diff --git a/plugins/sensors/n900/n900filebasedsensor.cpp b/plugins/sensors/n900/n900filebasedsensor.cpp index 33c07aa076..e7bcc76ed5 100644 --- a/plugins/sensors/n900/n900filebasedsensor.cpp +++ b/plugins/sensors/n900/n900filebasedsensor.cpp @@ -40,7 +40,6 @@ ****************************************************************************/ #include "n900filebasedsensor.h" -#include #include n900filebasedsensor::n900filebasedsensor(QSensor *sensor) @@ -90,18 +89,6 @@ void n900filebasedsensor::timerEvent(QTimerEvent * /*event*/) poll(); } -quint64 n900filebasedsensor::getTimestamp() -{ - struct timespec tv; - int ok; - - ok = clock_gettime(CLOCK_MONOTONIC, &tv); - Q_ASSERT(ok == 0); - - quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds - return result; -} - void n900filebasedsensor::pollnow() { poll(); diff --git a/plugins/sensors/n900/n900filebasedsensor.h b/plugins/sensors/n900/n900filebasedsensor.h index b45de54620..e17cdf1fef 100644 --- a/plugins/sensors/n900/n900filebasedsensor.h +++ b/plugins/sensors/n900/n900filebasedsensor.h @@ -58,9 +58,6 @@ public: virtual void poll() = 0; void timerEvent(QTimerEvent * /*event*/); -protected: - quint64 getTimestamp(); - private Q_SLOTS: void pollnow(); diff --git a/plugins/sensors/n900/n900lightsensor.cpp b/plugins/sensors/n900/n900lightsensor.cpp index 8f88c9497a..5d4ccabd3a 100644 --- a/plugins/sensors/n900/n900lightsensor.cpp +++ b/plugins/sensors/n900/n900lightsensor.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "n900lightsensor.h" +#include "../util/util.h" #include #include diff --git a/plugins/sensors/n900/n900proximitysensor.cpp b/plugins/sensors/n900/n900proximitysensor.cpp index 65e1ba17c7..953ad445c4 100644 --- a/plugins/sensors/n900/n900proximitysensor.cpp +++ b/plugins/sensors/n900/n900proximitysensor.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "n900proximitysensor.h" +#include "../util/util.h" #include #include #include diff --git a/plugins/sensors/util/util.cpp b/plugins/sensors/util/util.cpp new file mode 100644 index 0000000000..11985812cc --- /dev/null +++ b/plugins/sensors/util/util.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "util.h" + +#ifdef Q_OS_WINCE +#include +// WINCE has but using clock() gives a link error because +// the function isn't actually implemented. +#endif +#ifdef Q_OS_UNIX +#include +#endif + +#if (defined Q_OS_UNIX) || (defined Q_OS_WINCE) +quint64 getTimestamp() +{ +#ifdef Q_OS_WINCE + // This implementation is based on code found here: + // http://social.msdn.microsoft.com/Forums/en/vssmartdevicesnative/thread/74870c6c-76c5-454c-8533-812cfca585f8 + HANDLE currentThread = GetCurrentThread(); + FILETIME creationTime, exitTime, kernalTime, userTime; + GetThreadTimes(currentThread, &creationTime, &exitTime, &kernalTime, &userTime); + + ULARGE_INTEGER uli; + uli.LowPart = userTime.dwLowDateTime; + uli.HighPart = userTime.dwHighDateTime; + ULONGLONG systemTimeInMS = uli.QuadPart/10000; + return static_cast(systemTimeInMS); +#else + struct timespec tv; + int ok; + + ok = clock_gettime(CLOCK_MONOTONIC, &tv); + Q_ASSERT(ok == 0); + + quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds + return result; +#endif +} +#endif diff --git a/plugins/sensors/util/util.h b/plugins/sensors/util/util.h new file mode 100644 index 0000000000..4e42caa969 --- /dev/null +++ b/plugins/sensors/util/util.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef UTIL_H +#define UTIL_H + +#include + +#if (defined Q_OS_UNIX) || (defined Q_OS_WINCE) +quint64 getTimestamp(); +#endif + +#endif -- cgit v1.2.3