From 2b67b50af18bb2a17ff10d5a37abfd85fc7e9d01 Mon Sep 17 00:00:00 2001 From: Andrew Knight Date: Sat, 15 Feb 2014 09:44:41 +0200 Subject: Initial sensors backend for WinRT/Windows Phone This provides the sensor plugin with support for accelerometer, gyroscope, rotation sensor, orientation sensor, compass, and ambient light sensor. Change-Id: Ic91a6cef98e4011858552c5cd6407b494579fe17 Reviewed-by: Thomas McGuire Reviewed-by: Lars Knoll --- src/plugins/sensors/sensors.pro | 5 + src/plugins/sensors/winrt/main.cpp | 105 ++++++++++ src/plugins/sensors/winrt/plugin.json | 1 + src/plugins/sensors/winrt/winrt.pro | 31 +++ src/plugins/sensors/winrt/winrtaccelerometer.cpp | 204 +++++++++++++++++++ src/plugins/sensors/winrt/winrtaccelerometer.h | 66 ++++++ .../sensors/winrt/winrtambientlightsensor.cpp | 203 +++++++++++++++++++ .../sensors/winrt/winrtambientlightsensor.h | 66 ++++++ src/plugins/sensors/winrt/winrtcommon.cpp | 54 +++++ src/plugins/sensors/winrt/winrtcommon.h | 62 ++++++ src/plugins/sensors/winrt/winrtcompass.cpp | 224 +++++++++++++++++++++ src/plugins/sensors/winrt/winrtcompass.h | 66 ++++++ src/plugins/sensors/winrt/winrtgyroscope.cpp | 201 ++++++++++++++++++ src/plugins/sensors/winrt/winrtgyroscope.h | 73 +++++++ .../sensors/winrt/winrtorientationsensor.cpp | 188 +++++++++++++++++ src/plugins/sensors/winrt/winrtorientationsensor.h | 66 ++++++ src/plugins/sensors/winrt/winrtrotationsensor.cpp | 199 ++++++++++++++++++ src/plugins/sensors/winrt/winrtrotationsensor.h | 73 +++++++ src/sensors/doc/src/compatmap.qdoc | 36 ++++ 19 files changed, 1923 insertions(+) create mode 100644 src/plugins/sensors/winrt/main.cpp create mode 100644 src/plugins/sensors/winrt/plugin.json create mode 100644 src/plugins/sensors/winrt/winrt.pro create mode 100644 src/plugins/sensors/winrt/winrtaccelerometer.cpp create mode 100644 src/plugins/sensors/winrt/winrtaccelerometer.h create mode 100644 src/plugins/sensors/winrt/winrtambientlightsensor.cpp create mode 100644 src/plugins/sensors/winrt/winrtambientlightsensor.h create mode 100644 src/plugins/sensors/winrt/winrtcommon.cpp create mode 100644 src/plugins/sensors/winrt/winrtcommon.h create mode 100644 src/plugins/sensors/winrt/winrtcompass.cpp create mode 100644 src/plugins/sensors/winrt/winrtcompass.h create mode 100644 src/plugins/sensors/winrt/winrtgyroscope.cpp create mode 100644 src/plugins/sensors/winrt/winrtgyroscope.h create mode 100644 src/plugins/sensors/winrt/winrtorientationsensor.cpp create mode 100644 src/plugins/sensors/winrt/winrtorientationsensor.h create mode 100644 src/plugins/sensors/winrt/winrtrotationsensor.cpp create mode 100644 src/plugins/sensors/winrt/winrtrotationsensor.h diff --git a/src/plugins/sensors/sensors.pro b/src/plugins/sensors/sensors.pro index 66f9f5a1..a566716a 100644 --- a/src/plugins/sensors/sensors.pro +++ b/src/plugins/sensors/sensors.pro @@ -18,6 +18,10 @@ ios { isEmpty(SENSORS_PLUGINS): SENSORS_PLUGINS = ios generic } +winrt { + isEmpty(SENSORS_PLUGINS): SENSORS_PLUGINS = winrt generic +} + isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, dummy):SUBDIRS += dummy isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, generic):SUBDIRS += generic isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, simulator):simulator:SUBDIRS += simulator @@ -26,3 +30,4 @@ isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, linux):linux:SUBDIRS += linux isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, android):android:SUBDIRS += android isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, sensorfw):sensorfw:SUBDIRS += sensorfw isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, ios):ios:SUBDIRS += ios +isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, winrt):winrt:SUBDIRS += winrt diff --git a/src/plugins/sensors/winrt/main.cpp b/src/plugins/sensors/winrt/main.cpp new file mode 100644 index 00000000..13b87125 --- /dev/null +++ b/src/plugins/sensors/winrt/main.cpp @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtaccelerometer.h" +#include "winrtcompass.h" +#include "winrtgyroscope.h" +#include "winrtrotationsensor.h" +#ifndef Q_OS_WINPHONE +# include "winrtambientlightsensor.h" +# include "winrtorientationsensor.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class WinRtSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "com.qt-project.Qt.QSensorPluginInterface/1.0" FILE "plugin.json") + Q_INTERFACES(QSensorPluginInterface) +public: + void registerSensors() + { + QSensorManager::registerBackend(QAccelerometer::type, QByteArrayLiteral("WinRtAccelerometer"), this); + QSensorManager::registerBackend(QCompass::type, QByteArrayLiteral("WinRtCompass"), this); + QSensorManager::registerBackend(QGyroscope::type, QByteArrayLiteral("WinRtGyroscope"), this); + QSensorManager::registerBackend(QRotationSensor::type, QByteArrayLiteral("WinRtRotationSensor"), this); +#ifndef Q_OS_WINPHONE + QSensorManager::registerBackend(QAmbientLightSensor::type, QByteArrayLiteral("WinRtAmbientLightSensor"), this); + QSensorManager::registerBackend(QOrientationSensor::type, QByteArrayLiteral("WinRtOrientationSensor"), this); +#endif + } + + QSensorBackend *createBackend(QSensor *sensor) + { + if (sensor->identifier() == QByteArrayLiteral("WinRtAccelerometer")) + return new WinRtAccelerometer(sensor); + + if (sensor->identifier() == QByteArrayLiteral("WinRtCompass")) + return new WinRtCompass(sensor); + + if (sensor->identifier() == QByteArrayLiteral("WinRtGyroscope")) + return new WinRtGyroscope(sensor); + + if (sensor->identifier() == QByteArrayLiteral("WinRtRotationSensor")) + return new WinRtRotationSensor(sensor); + +#ifndef Q_OS_WINPHONE + if (sensor->identifier() == QByteArrayLiteral("WinRtAmbientLightSensor")) + return new WinRtAmbientLightSensor(sensor); + + if (sensor->identifier() == QByteArrayLiteral("WinRtOrientationSensor")) + return new WinRtOrientationSensor(sensor); +#endif // !Q_OS_WINPHONE + + return 0; + } +}; + +#include "main.moc" + diff --git a/src/plugins/sensors/winrt/plugin.json b/src/plugins/sensors/winrt/plugin.json new file mode 100644 index 00000000..8a55b3ae --- /dev/null +++ b/src/plugins/sensors/winrt/plugin.json @@ -0,0 +1 @@ +{ "Keys": [ "notused" ] } diff --git a/src/plugins/sensors/winrt/winrt.pro b/src/plugins/sensors/winrt/winrt.pro new file mode 100644 index 00000000..494e1bf0 --- /dev/null +++ b/src/plugins/sensors/winrt/winrt.pro @@ -0,0 +1,31 @@ +TARGET = qtsensors_winrt +QT = sensors core + +PLUGIN_TYPE = sensors +load(qt_plugin) + +HEADERS += \ + winrtaccelerometer.h \ + winrtcommon.h \ + winrtcompass.h \ + winrtrotationsensor.h \ + winrtgyroscope.h + +SOURCES += \ + main.cpp \ + winrtaccelerometer.cpp \ + winrtcommon.cpp \ + winrtcompass.cpp \ + winrtrotationsensor.cpp \ + winrtgyroscope.cpp + +!winphone { + HEADERS += \ + winrtambientlightsensor.h \ + winrtorientationsensor.h + SOURCES += \ + winrtambientlightsensor.cpp \ + winrtorientationsensor.cpp +} + +OTHER_FILES = plugin.json diff --git a/src/plugins/sensors/winrt/winrtaccelerometer.cpp b/src/plugins/sensors/winrt/winrtaccelerometer.cpp new file mode 100644 index 00000000..c8e8b26c --- /dev/null +++ b/src/plugins/sensors/winrt/winrtaccelerometer.cpp @@ -0,0 +1,204 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtaccelerometer.h" +#include "winrtcommon.h" + +#include + +#include +#include +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +using namespace ABI::Windows::Foundation; +using namespace ABI::Windows::Devices::Sensors; + +typedef ITypedEventHandler AccelerometerReadingHandler; + +QT_USE_NAMESPACE + +#define GRAVITY_EARTH_MS2 9.80665 + +class WinRtAccelerometerPrivate +{ +public: + WinRtAccelerometerPrivate(WinRtAccelerometer *p) + : minimumReportInterval(0), q(p) + { + token.value = 0; + } + + QAccelerometerReading reading; + + ComPtr sensor; + EventRegistrationToken token; + quint32 minimumReportInterval; + + HRESULT readingChanged(IAccelerometer *, IAccelerometerReadingChangedEventArgs *args) + { + ComPtr value; + HRESULT hr = args->get_Reading(&value); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get accelerometer reading" << qt_error_string(hr); + return hr; + } + + DOUBLE x; + hr = value->get_AccelerationX(&x); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get acceleration X" << qt_error_string(hr); + + DOUBLE y; + hr = value->get_AccelerationY(&y); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get acceleration Y" << qt_error_string(hr); + + DOUBLE z; + hr = value->get_AccelerationZ(&z); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get acceleration Z" << qt_error_string(hr); + + DateTime dateTime; + hr = value->get_Timestamp(&dateTime); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get acceleration timestamp" << qt_error_string(hr); + + // The reading is in G force, so convert to m/s/s + reading.setX(x * GRAVITY_EARTH_MS2); + reading.setY(y * GRAVITY_EARTH_MS2); + reading.setZ(z * GRAVITY_EARTH_MS2); + reading.setTimestamp(dateTimeToMsSinceEpoch(dateTime)); + q->newReadingAvailable(); + + return S_OK; + } + +private: + WinRtAccelerometer *q; +}; + +WinRtAccelerometer::WinRtAccelerometer(QSensor *sensor) + : QSensorBackend(sensor), d_ptr(new WinRtAccelerometerPrivate(this)) +{ + Q_D(WinRtAccelerometer); + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Accelerometer); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize accelerometer factory." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = factory->GetDefault(&d->sensor); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get default accelerometer." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = d->sensor->get_MinimumReportInterval(&d->minimumReportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get the minimum report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + + setReading(&d->reading); +} + +WinRtAccelerometer::~WinRtAccelerometer() +{ +} + +void WinRtAccelerometer::start() +{ + Q_D(WinRtAccelerometer); + if (!d->sensor) + return; + if (d->token.value) + return; + + ComPtr callback = + Callback(d, &WinRtAccelerometerPrivate::readingChanged); + HRESULT hr = d->sensor->add_ReadingChanged(callback.Get(), &d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + quint32 reportInterval = qMax(d->minimumReportInterval, quint32(1000/sensor()->dataRate())); + hr = d->sensor->put_ReportInterval(reportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to set report interval." + << qt_error_string(hr); + sensorError(hr); + } +} + +void WinRtAccelerometer::stop() +{ + Q_D(WinRtAccelerometer); + if (!d->sensor) + return; + if (!d->token.value) + return; + + HRESULT hr = d->sensor->remove_ReadingChanged(d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to detach from reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->sensor->put_ReportInterval(0); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to reset report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->token.value = 0; +} diff --git a/src/plugins/sensors/winrt/winrtaccelerometer.h b/src/plugins/sensors/winrt/winrtaccelerometer.h new file mode 100644 index 00000000..a71d73b2 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtaccelerometer.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 WINRTACCELEROMETER_H +#define WINRTACCELEROMETER_H + +#include +#include + +QT_USE_NAMESPACE + +class WinRtAccelerometerPrivate; +class WinRtAccelerometer : public QSensorBackend +{ + Q_OBJECT +public: + WinRtAccelerometer(QSensor *sensor); + ~WinRtAccelerometer(); + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(WinRtAccelerometer) +}; + +#endif // WINRTACCELEROMETER_H diff --git a/src/plugins/sensors/winrt/winrtambientlightsensor.cpp b/src/plugins/sensors/winrt/winrtambientlightsensor.cpp new file mode 100644 index 00000000..18ee6418 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtambientlightsensor.cpp @@ -0,0 +1,203 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtambientlightsensor.h" +#include "winrtcommon.h" + +#include + +#include +#include +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +using namespace ABI::Windows::Foundation; +using namespace ABI::Windows::Devices::Sensors; + +typedef ITypedEventHandler LightSensorReadingHandler; + +QT_USE_NAMESPACE + +class WinRtAmbientLightSensorPrivate +{ +public: + WinRtAmbientLightSensorPrivate(WinRtAmbientLightSensor *p) + : minimumReportInterval(0), q(p) + { + token.value = 0; + } + + QAmbientLightReading reading; + + ComPtr sensor; + EventRegistrationToken token; + quint32 minimumReportInterval; + + HRESULT readingChanged(ILightSensor *, ILightSensorReadingChangedEventArgs *args) + { + ComPtr value; + HRESULT hr = args->get_Reading(&value); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get light sensor reading" << qt_error_string(hr); + return hr; + } + + FLOAT lux; + hr = value->get_IlluminanceInLux(&lux); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get illuminance value" << qt_error_string(hr); + return hr; + } + + DateTime dateTime; + hr = value->get_Timestamp(&dateTime); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get light sensor reading timestamp" << qt_error_string(hr); + return hr; + } + + // Using same values as BB light sensor + if (lux < 10) + reading.setLightLevel(QAmbientLightReading::Dark); + else if (lux < 80) + reading.setLightLevel(QAmbientLightReading::Twilight); + else if (lux < 400) + reading.setLightLevel(QAmbientLightReading::Light); + else if (lux < 2500) + reading.setLightLevel(QAmbientLightReading::Bright); + else + reading.setLightLevel(QAmbientLightReading::Sunny); + + reading.setTimestamp(dateTimeToMsSinceEpoch(dateTime)); + q->newReadingAvailable(); + return S_OK; + } + +private: + WinRtAmbientLightSensor *q; +}; + +WinRtAmbientLightSensor::WinRtAmbientLightSensor(QSensor *sensor) + : QSensorBackend(sensor), d_ptr(new WinRtAmbientLightSensorPrivate(this)) +{ + Q_D(WinRtAmbientLightSensor); + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_LightSensor); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize light sensor factory." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = factory->GetDefault(&d->sensor); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get default light sensor." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = d->sensor->get_MinimumReportInterval(&d->minimumReportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get the minimum report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + + setReading(&d->reading); +} + +WinRtAmbientLightSensor::~WinRtAmbientLightSensor() +{ +} + +void WinRtAmbientLightSensor::start() +{ + Q_D(WinRtAmbientLightSensor); + if (!d->sensor) + return; + if (d->token.value) + return; + + ComPtr callback = + Callback(d, &WinRtAmbientLightSensorPrivate::readingChanged); + HRESULT hr = d->sensor->add_ReadingChanged(callback.Get(), &d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + quint32 reportInterval = qMax(d->minimumReportInterval, quint32(1000/sensor()->dataRate())); + hr = d->sensor->put_ReportInterval(reportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to set report interval." + << qt_error_string(hr); + sensorError(hr); + } +} + +void WinRtAmbientLightSensor::stop() +{ + Q_D(WinRtAmbientLightSensor); + if (!d->sensor) + return; + if (!d->token.value) + return; + + HRESULT hr = d->sensor->remove_ReadingChanged(d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to detach from reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->sensor->put_ReportInterval(0); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to reset report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->token.value = 0; +} diff --git a/src/plugins/sensors/winrt/winrtambientlightsensor.h b/src/plugins/sensors/winrt/winrtambientlightsensor.h new file mode 100644 index 00000000..c65ae96f --- /dev/null +++ b/src/plugins/sensors/winrt/winrtambientlightsensor.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 WINRTAMBIENTLIGHTSENSOR_H +#define WINRTAMBIENTLIGHTSENSOR_H + +#include +#include + +QT_USE_NAMESPACE + +class WinRtAmbientLightSensorPrivate; +class WinRtAmbientLightSensor : public QSensorBackend +{ + Q_OBJECT +public: + WinRtAmbientLightSensor(QSensor *sensor); + ~WinRtAmbientLightSensor(); + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(WinRtAmbientLightSensor) +}; + +#endif // WINRTAMBIENTLIGHTSENSOR_H diff --git a/src/plugins/sensors/winrt/winrtcommon.cpp b/src/plugins/sensors/winrt/winrtcommon.cpp new file mode 100644 index 00000000..29b057fa --- /dev/null +++ b/src/plugins/sensors/winrt/winrtcommon.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtcommon.h" + +#include + +QT_USE_NAMESPACE + +Q_LOGGING_CATEGORY(lcWinRtSensors, "qt.sensors.winrt") + +quint64 dateTimeToMsSinceEpoch(const ABI::Windows::Foundation::DateTime &dateTime) +{ + // Convert 100-ns units since 01-01-1601 to ms since 01-01-1970 + return dateTime.UniversalTime / 10000 - 11644473600000LL; +} diff --git a/src/plugins/sensors/winrt/winrtcommon.h b/src/plugins/sensors/winrt/winrtcommon.h new file mode 100644 index 00000000..a4a0cc67 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtcommon.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 WINRTCOMMON_H +#define WINRTCOMMON_H + +#include + +namespace ABI { + namespace Windows { + namespace Foundation { + struct DateTime; + } + } +} + +QT_USE_NAMESPACE + +Q_DECLARE_LOGGING_CATEGORY(lcWinRtSensors) + +quint64 dateTimeToMsSinceEpoch(const ABI::Windows::Foundation::DateTime &dateTime); + +#endif // WINRTCOMMON_H + diff --git a/src/plugins/sensors/winrt/winrtcompass.cpp b/src/plugins/sensors/winrt/winrtcompass.cpp new file mode 100644 index 00000000..bc2430f2 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtcompass.cpp @@ -0,0 +1,224 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtcompass.h" +#include "winrtcommon.h" + +#include + +QT_USE_NAMESPACE + +#include +#include +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +using namespace ABI::Windows::Foundation; +using namespace ABI::Windows::Devices::Sensors; + +typedef ITypedEventHandler CompassReadingHandler; + +QT_USE_NAMESPACE + +class WinRtCompassPrivate +{ +public: + WinRtCompassPrivate(WinRtCompass *p) + : minimumReportInterval(0), q(p) + { + token.value = 0; + } + + QCompassReading reading; + + ComPtr sensor; + EventRegistrationToken token; + quint32 minimumReportInterval; + + HRESULT readingChanged(ICompass *, ICompassReadingChangedEventArgs *args) + { + ComPtr value; + HRESULT hr = args->get_Reading(&value); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get light sensor reading." << qt_error_string(hr); + return hr; + } + + DOUBLE heading; + hr = value->get_HeadingMagneticNorth(&heading); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get compass heading." << qt_error_string(hr); + return hr; + } + + DateTime dateTime; + hr = value->get_Timestamp(&dateTime); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get compass reading timestamp." << qt_error_string(hr); + return hr; + } +#ifndef Q_OS_WINPHONE // Windows Phone doesn't implement the accuracy interface + ComPtr accuracyReading; + hr = value.As(&accuracyReading); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to cast compass reading to obtain accuracy." << qt_error_string(hr); + return hr; + } + + MagnetometerAccuracy accuracy; + hr = accuracyReading->get_HeadingAccuracy(&accuracy); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get compass reading accuracy." << qt_error_string(hr); + return hr; + } + + switch (accuracy) { + default: + case MagnetometerAccuracy_Unknown: + reading.setCalibrationLevel(0.00); + break; + case MagnetometerAccuracy_Unreliable: + reading.setCalibrationLevel(0.33); + break; + case MagnetometerAccuracy_Approximate: + reading.setCalibrationLevel(0.67); + break; + case MagnetometerAccuracy_High: + reading.setCalibrationLevel(1.00); + break; + } +#endif + reading.setAzimuth(heading); + reading.setTimestamp(dateTimeToMsSinceEpoch(dateTime)); + q->newReadingAvailable(); + return S_OK; + } + +private: + WinRtCompass *q; +}; + +WinRtCompass::WinRtCompass(QSensor *sensor) + : QSensorBackend(sensor), d_ptr(new WinRtCompassPrivate(this)) +{ + Q_D(WinRtCompass); + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Compass); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize light sensor factory." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = factory->GetDefault(&d->sensor); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get default light sensor." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = d->sensor->get_MinimumReportInterval(&d->minimumReportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get the minimum report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + + setReading(&d->reading); +} + +WinRtCompass::~WinRtCompass() +{ +} + +void WinRtCompass::start() +{ + Q_D(WinRtCompass); + if (!d->sensor) + return; + if (d->token.value) + return; + + ComPtr callback = + Callback(d, &WinRtCompassPrivate::readingChanged); + HRESULT hr = d->sensor->add_ReadingChanged(callback.Get(), &d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + quint32 reportInterval = qMax(d->minimumReportInterval, quint32(1000/sensor()->dataRate())); + hr = d->sensor->put_ReportInterval(reportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to set report interval." + << qt_error_string(hr); + sensorError(hr); + } +} + +void WinRtCompass::stop() +{ + Q_D(WinRtCompass); + if (!d->sensor) + return; + if (!d->token.value) + return; + + HRESULT hr = d->sensor->remove_ReadingChanged(d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to detach from reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->sensor->put_ReportInterval(0); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to reset report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->token.value = 0; +} diff --git a/src/plugins/sensors/winrt/winrtcompass.h b/src/plugins/sensors/winrt/winrtcompass.h new file mode 100644 index 00000000..479ab754 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtcompass.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 WINRTCOMPASS_H +#define WINRTCOMPASS_H + +#include +#include + +QT_USE_NAMESPACE + +class WinRtCompassPrivate; +class WinRtCompass : public QSensorBackend +{ + Q_OBJECT +public: + WinRtCompass(QSensor *sensor); + ~WinRtCompass(); + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(WinRtCompass) +}; + +#endif // WINRTCOMPASS_H diff --git a/src/plugins/sensors/winrt/winrtgyroscope.cpp b/src/plugins/sensors/winrt/winrtgyroscope.cpp new file mode 100644 index 00000000..dec65123 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtgyroscope.cpp @@ -0,0 +1,201 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtgyroscope.h" +#include "winrtcommon.h" + +#include + +#include +#include +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +using namespace ABI::Windows::Foundation; +using namespace ABI::Windows::Devices::Sensors; + +typedef ITypedEventHandler InclinometerReadingHandler; + +QT_USE_NAMESPACE + +class WinRtGyroscopePrivate +{ +public: + WinRtGyroscopePrivate(WinRtGyroscope *p) + : minimumReportInterval(0), q(p) + { + token.value = 0; + } + + QGyroscopeReading reading; + + ComPtr sensor; + EventRegistrationToken token; + quint32 minimumReportInterval; + + HRESULT readingChanged(IGyrometer *, IGyrometerReadingChangedEventArgs *args) + { + ComPtr value; + HRESULT hr = args->get_Reading(&value); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get gyroscope reading" << qt_error_string(hr); + return hr; + } + + DOUBLE x; + hr = value->get_AngularVelocityX(&x); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get gyroscope X" << qt_error_string(hr); + + DOUBLE y; + hr = value->get_AngularVelocityY(&y); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get gyroscope Y" << qt_error_string(hr); + + DOUBLE z; + hr = value->get_AngularVelocityZ(&z); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get gyroscope Z" << qt_error_string(hr); + + DateTime dateTime; + hr = value->get_Timestamp(&dateTime); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get gyroscope timestamp" << qt_error_string(hr); + + reading.setX(x); + reading.setY(y); + reading.setZ(z); + reading.setTimestamp(dateTimeToMsSinceEpoch(dateTime)); + q->newReadingAvailable(); + + return S_OK; + } + +private: + WinRtGyroscope *q; +}; + +WinRtGyroscope::WinRtGyroscope(QSensor *sensor) + : QSensorBackend(sensor), d_ptr(new WinRtGyroscopePrivate(this)) +{ + Q_D(WinRtGyroscope); + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Gyrometer); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize gyroscope sensor factory." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = factory->GetDefault(&d->sensor); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get default gyroscope sensor." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = d->sensor->get_MinimumReportInterval(&d->minimumReportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get the minimum report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + + setReading(&d->reading); +} + +WinRtGyroscope::~WinRtGyroscope() +{ +} + +void WinRtGyroscope::start() +{ + Q_D(WinRtGyroscope); + if (!d->sensor) + return; + if (d->token.value) + return; + + ComPtr callback = + Callback(d, &WinRtGyroscopePrivate::readingChanged); + HRESULT hr = d->sensor->add_ReadingChanged(callback.Get(), &d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + quint32 reportInterval = qMax(d->minimumReportInterval, quint32(1000/sensor()->dataRate())); + hr = d->sensor->put_ReportInterval(reportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to set report interval." + << qt_error_string(hr); + sensorError(hr); + } +} + +void WinRtGyroscope::stop() +{ + Q_D(WinRtGyroscope); + if (!d->sensor) + return; + if (!d->token.value) + return; + + HRESULT hr = d->sensor->remove_ReadingChanged(d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to detach from reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->sensor->put_ReportInterval(0); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to reset report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->token.value = 0; +} diff --git a/src/plugins/sensors/winrt/winrtgyroscope.h b/src/plugins/sensors/winrt/winrtgyroscope.h new file mode 100644 index 00000000..6275ec85 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtgyroscope.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 WINRTGYROSCOPE_H +#define WINRTGYROSCOPE_H + +#include +#include + +QT_USE_NAMESPACE + +class WinRtGyroscopePrivate; +class WinRtGyroscope : public QSensorBackend +{ + Q_OBJECT +public: + WinRtGyroscope(QSensor *sensor); + ~WinRtGyroscope(); + + bool isFeatureSupported(QSensor::Feature feature) const Q_DECL_OVERRIDE + { + if (feature == QSensor::AxesOrientation || feature == QSensor::AccelerationMode) + return true; + return false; + } + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(WinRtGyroscope) +}; + +#endif // WINRTGYROSCOPE_H diff --git a/src/plugins/sensors/winrt/winrtorientationsensor.cpp b/src/plugins/sensors/winrt/winrtorientationsensor.cpp new file mode 100644 index 00000000..6d85e51c --- /dev/null +++ b/src/plugins/sensors/winrt/winrtorientationsensor.cpp @@ -0,0 +1,188 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtorientationsensor.h" +#include "winrtcommon.h" + +#include + +QT_USE_NAMESPACE + +#include +#include +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +using namespace ABI::Windows::Foundation; +using namespace ABI::Windows::Devices::Sensors; + +typedef ITypedEventHandler SimpleOrientationReadingHandler; + +QT_USE_NAMESPACE + +class WinRtOrientationSensorPrivate +{ +public: + WinRtOrientationSensorPrivate(WinRtOrientationSensor *p) : q(p) + { + token.value = 0; + } + + QOrientationReading reading; + + ComPtr sensor; + EventRegistrationToken token; + + HRESULT readingChanged(ISimpleOrientationSensor *, + ISimpleOrientationSensorOrientationChangedEventArgs *args) + { + SimpleOrientation value; + HRESULT hr = args->get_Orientation(&value); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get orientation value." << qt_error_string(hr); + return hr; + } + + DateTime dateTime; + hr = args->get_Timestamp(&dateTime); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get compass reading timestamp." << qt_error_string(hr); + return hr; + } + + switch (value) { + default: + reading.setOrientation(QOrientationReading::Undefined); + break; + case SimpleOrientation_NotRotated: + reading.setOrientation(QOrientationReading::TopUp); + break; + case SimpleOrientation_Rotated90DegreesCounterclockwise: + reading.setOrientation(QOrientationReading::RightUp); + break; + case SimpleOrientation_Rotated180DegreesCounterclockwise: + reading.setOrientation(QOrientationReading::TopDown); + break; + case SimpleOrientation_Rotated270DegreesCounterclockwise: + reading.setOrientation(QOrientationReading::LeftUp); + break; + case SimpleOrientation_Faceup: + reading.setOrientation(QOrientationReading::FaceUp); + break; + case SimpleOrientation_Facedown: + reading.setOrientation(QOrientationReading::FaceDown); + break; + } + + reading.setTimestamp(dateTimeToMsSinceEpoch(dateTime)); + q->newReadingAvailable(); + return S_OK; + } + +private: + WinRtOrientationSensor *q; +}; + +WinRtOrientationSensor::WinRtOrientationSensor(QSensor *sensor) + : QSensorBackend(sensor), d_ptr(new WinRtOrientationSensorPrivate(this)) +{ + Q_D(WinRtOrientationSensor); + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_SimpleOrientationSensor); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize orientation sensor factory." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = factory->GetDefault(&d->sensor); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get default orientation sensor." + << qt_error_string(hr); + sensorError(hr); + return; + } + + setReading(&d->reading); +} + +WinRtOrientationSensor::~WinRtOrientationSensor() +{ +} + +void WinRtOrientationSensor::start() +{ + Q_D(WinRtOrientationSensor); + if (!d->sensor) + return; + if (d->token.value) + return; + + ComPtr callback = + Callback(d, &WinRtOrientationSensorPrivate::readingChanged); + HRESULT hr = d->sensor->add_OrientationChanged(callback.Get(), &d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } +} + +void WinRtOrientationSensor::stop() +{ + Q_D(WinRtOrientationSensor); + if (!d->sensor) + return; + if (!d->token.value) + return; + + HRESULT hr = d->sensor->remove_OrientationChanged(d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to detach from reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + + d->token.value = 0; +} diff --git a/src/plugins/sensors/winrt/winrtorientationsensor.h b/src/plugins/sensors/winrt/winrtorientationsensor.h new file mode 100644 index 00000000..31ecf0aa --- /dev/null +++ b/src/plugins/sensors/winrt/winrtorientationsensor.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 WINRTORIENTATIONSENSOR_H +#define WINRTORIENTATIONSENSOR_H + +#include +#include + +QT_USE_NAMESPACE + +class WinRtOrientationSensorPrivate; +class WinRtOrientationSensor : public QSensorBackend +{ + Q_OBJECT +public: + WinRtOrientationSensor(QSensor *sensor); + ~WinRtOrientationSensor(); + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(WinRtOrientationSensor) +}; + +#endif // WINRTORIENTATIONSENSOR_H diff --git a/src/plugins/sensors/winrt/winrtrotationsensor.cpp b/src/plugins/sensors/winrt/winrtrotationsensor.cpp new file mode 100644 index 00000000..e4e3edc9 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtrotationsensor.cpp @@ -0,0 +1,199 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "winrtrotationsensor.h" +#include "winrtcommon.h" + +#include + +#include +#include +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +using namespace ABI::Windows::Foundation; +using namespace ABI::Windows::Devices::Sensors; + +typedef ITypedEventHandler InclinometerReadingHandler; + +QT_USE_NAMESPACE + +class WinRtRotationSensorPrivate +{ +public: + WinRtRotationSensorPrivate(WinRtRotationSensor *p) + : minimumReportInterval(0), q(p) + { + token.value = 0; + } + + QRotationReading reading; + + ComPtr sensor; + EventRegistrationToken token; + quint32 minimumReportInterval; + + HRESULT readingChanged(IInclinometer *, IInclinometerReadingChangedEventArgs *args) + { + ComPtr value; + HRESULT hr = args->get_Reading(&value); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Failed to get rotation reading" << qt_error_string(hr); + return hr; + } + + FLOAT x; + hr = value->get_PitchDegrees(&x); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get rotation X" << qt_error_string(hr); + + FLOAT y; + hr = value->get_RollDegrees(&y); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get rotation Y" << qt_error_string(hr); + + FLOAT z; + hr = value->get_YawDegrees(&z); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get rotation Z" << qt_error_string(hr); + + DateTime dateTime; + hr = value->get_Timestamp(&dateTime); + if (FAILED(hr)) + qCWarning(lcWinRtSensors) << "Failed to get rotation timestamp" << qt_error_string(hr); + + reading.setFromEuler(x, y, z); + reading.setTimestamp(dateTimeToMsSinceEpoch(dateTime)); + q->newReadingAvailable(); + + return S_OK; + } + +private: + WinRtRotationSensor *q; +}; + +WinRtRotationSensor::WinRtRotationSensor(QSensor *sensor) + : QSensorBackend(sensor), d_ptr(new WinRtRotationSensorPrivate(this)) +{ + Q_D(WinRtRotationSensor); + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Inclinometer); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize rotation sensor factory." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = factory->GetDefault(&d->sensor); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get default rotation sensor." + << qt_error_string(hr); + sensorError(hr); + return; + } + + hr = d->sensor->get_MinimumReportInterval(&d->minimumReportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get the minimum report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + + setReading(&d->reading); +} + +WinRtRotationSensor::~WinRtRotationSensor() +{ +} + +void WinRtRotationSensor::start() +{ + Q_D(WinRtRotationSensor); + if (!d->sensor) + return; + if (d->token.value) + return; + + ComPtr callback = + Callback(d, &WinRtRotationSensorPrivate::readingChanged); + HRESULT hr = d->sensor->add_ReadingChanged(callback.Get(), &d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + quint32 reportInterval = qMax(d->minimumReportInterval, quint32(1000/sensor()->dataRate())); + hr = d->sensor->put_ReportInterval(reportInterval); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to attach to set report interval." + << qt_error_string(hr); + sensorError(hr); + } +} + +void WinRtRotationSensor::stop() +{ + Q_D(WinRtRotationSensor); + if (!d->sensor) + return; + if (!d->token.value) + return; + + HRESULT hr = d->sensor->remove_ReadingChanged(d->token); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to detach from reading changed event." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->sensor->put_ReportInterval(0); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to reset report interval." + << qt_error_string(hr); + sensorError(hr); + return; + } + d->token.value = 0; +} diff --git a/src/plugins/sensors/winrt/winrtrotationsensor.h b/src/plugins/sensors/winrt/winrtrotationsensor.h new file mode 100644 index 00000000..21d823c5 --- /dev/null +++ b/src/plugins/sensors/winrt/winrtrotationsensor.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 WINRTROTATIONSENSOR_H +#define WINRTROTATIONSENSOR_H + +#include +#include + +QT_USE_NAMESPACE + +class WinRtRotationSensorPrivate; +class WinRtRotationSensor : public QSensorBackend +{ + Q_OBJECT +public: + WinRtRotationSensor(QSensor *sensor); + ~WinRtRotationSensor(); + + bool isFeatureSupported(QSensor::Feature feature) const Q_DECL_OVERRIDE + { + if (feature == QSensor::AxesOrientation || feature == QSensor::AccelerationMode) + return true; + return false; + } + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(WinRtRotationSensor) +}; + +#endif // WINRTROTATIONSENSOR_H diff --git a/src/sensors/doc/src/compatmap.qdoc b/src/sensors/doc/src/compatmap.qdoc index a287a827..622069bb 100644 --- a/src/sensors/doc/src/compatmap.qdoc +++ b/src/sensors/doc/src/compatmap.qdoc @@ -57,6 +57,8 @@ Android Linux iOS + Windows 8/RT + Windows Phone Generic Sensorfw @@ -66,6 +68,8 @@ + + @@ -77,6 +81,8 @@ + + Ambient Light Sensor @@ -84,6 +90,8 @@ + + @@ -95,6 +103,8 @@ + + Compass @@ -102,6 +112,8 @@ + + @@ -111,6 +123,8 @@ + + @@ -122,6 +136,8 @@ + + IR Proximity Sensor @@ -130,6 +146,8 @@ + + @@ -139,6 +157,8 @@ + + @@ -148,6 +168,8 @@ + + @@ -157,6 +179,8 @@ + + @@ -167,6 +191,8 @@ + + Proximity Sensor @@ -175,6 +201,8 @@ + + @@ -185,6 +213,8 @@ + + Tap Sensor @@ -194,6 +224,8 @@ + + Tilt Sensor @@ -201,6 +233,8 @@ + + @@ -211,6 +245,8 @@ + + -- cgit v1.2.3