diff options
-rw-r--r-- | src/plugins/sensors/blackberry/bbguihelper.cpp | 104 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/bbguihelper.h | 76 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/bbrotationsensor.cpp | 50 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/bbrotationsensor.h | 9 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/bbsensorbackend.cpp | 45 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/bbsensorbackend.h | 8 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/bbutil.h | 1 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/blackberry.pro | 4 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/main.cpp | 4 |
9 files changed, 239 insertions, 62 deletions
diff --git a/src/plugins/sensors/blackberry/bbguihelper.cpp b/src/plugins/sensors/blackberry/bbguihelper.cpp new file mode 100644 index 00000000..b074901c --- /dev/null +++ b/src/plugins/sensors/blackberry/bbguihelper.cpp @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Research In Motion +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "bbguihelper.h" + +#include <QtCore/QAbstractEventDispatcher> +#include <QtCore/QCoreApplication> +#include <bps/navigator.h> + +BbGuiHelper::BbGuiHelper(QObject *parent) + : QObject(parent), + m_currentOrientation(0), + m_applicationActive(true) +{ + // There is no API to get the current orientation or application active state at the moment. + // Therefore, we assume the application is active when this is called, and that the inital + // orientation that is set in the environment variable hasn't changed yet. + // These assumptions don't always hold, but it is the best we got so far. + // The navigator will at least inform us about updates. + const QByteArray orientationText = qgetenv("ORIENTATION"); + if (!orientationText.isEmpty()) + m_currentOrientation = orientationText.toInt(); + + QCoreApplication::eventDispatcher()->installNativeEventFilter(this); +} + +BbGuiHelper::~BbGuiHelper() +{ + QCoreApplication::eventDispatcher()->removeNativeEventFilter(this); +} + +int BbGuiHelper::currentOrientation() const +{ + return m_currentOrientation; +} + +bool BbGuiHelper::applicationActive() const +{ + return m_applicationActive; +} + +bool BbGuiHelper::nativeEventFilter(const QByteArray &eventType, void *message, long *result) +{ + Q_UNUSED(result); + Q_UNUSED(eventType); + + bps_event_t * const event = static_cast<bps_event_t *>(message); + if (event && bps_event_get_domain(event) == navigator_get_domain()) { + const int code = bps_event_get_code(event); + if (code == NAVIGATOR_ORIENTATION) { + const int newOrientation = navigator_event_get_orientation_angle(event); + if (newOrientation != m_currentOrientation) { + m_currentOrientation = newOrientation; + emit orientationChanged(); + } + } else if (code == NAVIGATOR_WINDOW_STATE) { + const bool appActive = + (navigator_event_get_window_state(event) == NAVIGATOR_WINDOW_FULLSCREEN); + if (m_applicationActive != appActive) { + m_applicationActive = appActive; + emit applicationActiveChanged(); + } + } + } + + return false; +} diff --git a/src/plugins/sensors/blackberry/bbguihelper.h b/src/plugins/sensors/blackberry/bbguihelper.h new file mode 100644 index 00000000..08c1de42 --- /dev/null +++ b/src/plugins/sensors/blackberry/bbguihelper.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Research In Motion +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef BBGUIHELPER_H +#define BBGUIHELPER_H + +#include <QtCore/QAbstractNativeEventFilter> +#include <QtCore/QObject> + +struct bps_event_t; + +// We can't depend on QtGui in this plugin, only on BPS. +// This class provides replacements for some QtGui functions, implemented using BPS. +class BbGuiHelper : public QObject, public QAbstractNativeEventFilter +{ + Q_OBJECT +public: + explicit BbGuiHelper(QObject *parent = 0); + ~BbGuiHelper(); + + // Orientation 0 is device held in normal position (Blackberry logo readable), then orientation + // increases in 90 degrees steps counter-clockwise, i.e. rotating the device to the left. + // Therefore the range is 0 to 270 degrees. + int currentOrientation() const; + + bool applicationActive() const; + + bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; + +signals: + void orientationChanged(); + void applicationActiveChanged(); + +private: + int m_currentOrientation; + bool m_applicationActive; +}; + +#endif diff --git a/src/plugins/sensors/blackberry/bbrotationsensor.cpp b/src/plugins/sensors/blackberry/bbrotationsensor.cpp index 0ed1495b..f64844ae 100644 --- a/src/plugins/sensors/blackberry/bbrotationsensor.cpp +++ b/src/plugins/sensors/blackberry/bbrotationsensor.cpp @@ -40,21 +40,18 @@ ****************************************************************************/ #include "bbrotationsensor.h" +#include "bbguihelper.h" #include "bbutil.h" #include <QtCore/qmath.h> -#include <QGuiApplication> -#include <QScreen> -#include <qpa/qplatformscreen.h> using namespace BbUtil; BbRotationSensor::BbRotationSensor(QSensor *sensor) - : BbSensorBackend<QRotationReading>(devicePath(), SENSOR_TYPE_ROTATION_MATRIX, sensor), - m_orientation(Qt::PrimaryOrientation), - m_nativeOrientation(Qt::PrimaryOrientation) + : BbSensorBackend<QRotationReading>(devicePath(), SENSOR_TYPE_ROTATION_MATRIX, sensor) { - updateOrientation(); setDescription(QLatin1String("Device rotation in degrees")); + m_mappingMatrix[0] = m_mappingMatrix[2] = 1; + m_mappingMatrix[1] = m_mappingMatrix[3] = 0; } QString BbRotationSensor::devicePath() @@ -62,6 +59,13 @@ QString BbRotationSensor::devicePath() return QLatin1String("/dev/sensor/rotMatrix"); } +void BbRotationSensor::setGuiHelper(BbGuiHelper *guiHelper) +{ + BbSensorBackend<QRotationReading>::setGuiHelper(guiHelper); + connect(guiHelper, SIGNAL(orientationChanged()), this, SLOT(updateOrientation())); + updateOrientation(); +} + void BbRotationSensor::additionalDeviceInit() { addOutputRange(-180, 180, 0 /* ? */); @@ -94,7 +98,6 @@ static void remapMatrix(const float inputMatrix[3*3], } } - bool BbRotationSensor::updateReadingFromEvent(const sensor_event_t &event, QRotationReading *reading) { // sensor_event_t has euler angles for a Z-Y'-X'' system, but the QtMobility API @@ -102,7 +105,7 @@ bool BbRotationSensor::updateReadingFromEvent(const sensor_event_t &event, QRota // So extract the euler angles using the Z-X'-Y'' system from the matrix. float xRad, yRad, zRad; - if (isAutoAxisRemappingEnabled() && m_orientation!=m_nativeOrientation) { + if (isAutoAxisRemappingEnabled() && guiHelper()->currentOrientation() != 0) { float mappedRotationMatrix[3*3]; remapMatrix(event.rotation_matrix, m_mappingMatrix, mappedRotationMatrix); matrixToEulerZXY(mappedRotationMatrix, xRad, yRad, zRad); @@ -127,27 +130,14 @@ bool BbRotationSensor::isAutoAxisRemappingEnabled() const return sensor()->property("automaticAxisRemapping").toBool(); } -bool BbRotationSensor::eventFilter(QObject *object, QEvent *event) -{ - if (object == QCoreApplication::instance() && event->type() == QEvent::OrientationChange) - updateOrientation(); - - return BbSensorBackend<QRotationReading>::eventFilter(object, event); -} - void BbRotationSensor::updateOrientation() { - QScreen *screen = QGuiApplication::primaryScreen(); - if (screen) { - const QPlatformScreen * const platformScreen = screen->handle(); - m_nativeOrientation = platformScreen->nativeOrientation(); - m_orientation = screen->orientation(); - - const int rotationAngle = screen->angleBetween(m_nativeOrientation, m_orientation); - - m_mappingMatrix[0] = cos(rotationAngle*M_PI/180); - m_mappingMatrix[1] = sin(rotationAngle*M_PI/180); - m_mappingMatrix[2] = -sin(rotationAngle*M_PI/180); - m_mappingMatrix[3] = cos(rotationAngle*M_PI/180); - } + // ### I can't really test this, the rotation matrix has too many glitches and drifts over time, + // making any measurement quite hard + const int rotationAngle = guiHelper()->currentOrientation(); + + m_mappingMatrix[0] = cos(rotationAngle*M_PI/180); + m_mappingMatrix[1] = sin(rotationAngle*M_PI/180); + m_mappingMatrix[2] = -sin(rotationAngle*M_PI/180); + m_mappingMatrix[3] = cos(rotationAngle*M_PI/180); } diff --git a/src/plugins/sensors/blackberry/bbrotationsensor.h b/src/plugins/sensors/blackberry/bbrotationsensor.h index 96511c4b..6fb7f7a0 100644 --- a/src/plugins/sensors/blackberry/bbrotationsensor.h +++ b/src/plugins/sensors/blackberry/bbrotationsensor.h @@ -53,6 +53,8 @@ public: static QString devicePath(); + void setGuiHelper(BbGuiHelper *guiHelper) Q_DECL_OVERRIDE; + protected: void additionalDeviceInit() Q_DECL_OVERRIDE; bool addDefaultRange() Q_DECL_OVERRIDE; @@ -60,13 +62,10 @@ protected: bool updateReadingFromEvent(const sensor_event_t &event, QRotationReading *reading) Q_DECL_OVERRIDE; bool isAutoAxisRemappingEnabled() const; - bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE; - -private: +private slots: void updateOrientation(); - Qt::ScreenOrientation m_orientation; - Qt::ScreenOrientation m_nativeOrientation; +private: float m_mappingMatrix[4]; }; diff --git a/src/plugins/sensors/blackberry/bbsensorbackend.cpp b/src/plugins/sensors/blackberry/bbsensorbackend.cpp index a1829b1a..8c53aa26 100644 --- a/src/plugins/sensors/blackberry/bbsensorbackend.cpp +++ b/src/plugins/sensors/blackberry/bbsensorbackend.cpp @@ -40,8 +40,8 @@ ****************************************************************************/ #include "bbsensorbackend.h" +#include "bbguihelper.h" #include <QtCore/QDebug> -#include <QtGui/QGuiApplication> #include <fcntl.h> static const int microSecondsPerSecond = 1000 * 1000; @@ -59,9 +59,8 @@ static uint hertzToMicroSeconds(int hertz) BbSensorBackendBase::BbSensorBackendBase(const QString &devicePath, sensor_type_e sensorType, QSensor *sensor) - : QSensorBackend(sensor), m_deviceFile(devicePath), m_sensorType(sensorType) + : QSensorBackend(sensor), m_deviceFile(devicePath), m_sensorType(sensorType), m_guiHelper(0) { - QCoreApplication::instance()->installEventFilter(this); connect(sensor, SIGNAL(alwaysOnChanged()), this, SLOT(applyAlwaysOnProperty())); // Set some sensible default values @@ -69,6 +68,11 @@ BbSensorBackendBase::BbSensorBackendBase(const QString &devicePath, sensor_type_ sensor->setProperty("maxBufferSize", defaultBufferSize); } +BbGuiHelper *BbSensorBackendBase::guiHelper() const +{ + return m_guiHelper; +} + QFile &BbSensorBackendBase::deviceFile() { return m_deviceFile; @@ -107,6 +111,13 @@ void BbSensorBackendBase::initSensorInfo() } } +void BbSensorBackendBase::setGuiHelper(BbGuiHelper *guiHelper) +{ + Q_ASSERT(!m_guiHelper); + m_guiHelper = guiHelper; + connect(m_guiHelper, SIGNAL(applicationActiveChanged()), this, SLOT(updatePauseState())); +} + void BbSensorBackendBase::additionalDeviceInit() { } @@ -121,27 +132,10 @@ qreal BbSensorBackendBase::convertValue(float bbValue) return bbValue; } -bool BbSensorBackendBase::eventFilter(QObject *object, QEvent *event) -{ - if (object == QCoreApplication::instance()) { - switch (event->type()) { - case QEvent::ApplicationActivate: - setPaused(false); - break; - case QEvent::ApplicationDeactivate: - if (!sensor()->isAlwaysOn()) - setPaused(true); - break; - default: - break; - } - } - - return QSensorBackend::eventFilter(object, event); -} - void BbSensorBackendBase::start() { + Q_ASSERT(m_guiHelper); + if (!m_deviceFile.open(QFile::ReadOnly | QFile::Unbuffered)) { qDebug() << "Starting sensor" << m_deviceFile.fileName() << "failed:" << m_deviceFile.errorString(); @@ -250,7 +244,7 @@ void BbSensorBackendBase::applyAlwaysOnProperty() } // We might need to pause now - setPaused(QGuiApplication::focusWindow() == 0 && !sensor()->isAlwaysOn()); + updatePauseState(); } void BbSensorBackendBase::setPaused(bool paused) @@ -268,3 +262,8 @@ void BbSensorBackendBase::setPaused(bool paused) .arg(m_deviceFile.fileName()).toLocal8Bit()); } } + +void BbSensorBackendBase::updatePauseState() +{ + setPaused(!sensor()->isAlwaysOn() && !m_guiHelper->applicationActive()); +} diff --git a/src/plugins/sensors/blackberry/bbsensorbackend.h b/src/plugins/sensors/blackberry/bbsensorbackend.h index 81c54bb1..3a5a99dc 100644 --- a/src/plugins/sensors/blackberry/bbsensorbackend.h +++ b/src/plugins/sensors/blackberry/bbsensorbackend.h @@ -56,6 +56,8 @@ #include "sensor.h" #endif +class BbGuiHelper; + class BbSensorBackendBase : public QSensorBackend { Q_OBJECT @@ -64,12 +66,14 @@ public: BbSensorBackendBase(const QString &devicePath, sensor_type_e sensorType, QSensor *sensor); void initSensorInfo(); + virtual void setGuiHelper(BbGuiHelper *guiHelper); void start() Q_DECL_OVERRIDE; void stop() Q_DECL_OVERRIDE; bool isFeatureSupported(QSensor::Feature feature) const Q_DECL_OVERRIDE; protected: + BbGuiHelper *guiHelper() const; QFile& deviceFile(); sensor_type_e sensorType() const; @@ -90,17 +94,17 @@ protected: virtual void processEvent(const sensor_event_t &sensorEvent) = 0; - virtual bool eventFilter(QObject *object, QEvent *event); - private slots: void dataAvailable(); void applyAlwaysOnProperty(); void setPaused(bool paused); + void updatePauseState(); private: QFile m_deviceFile; QScopedPointer<QSocketNotifier> m_socketNotifier; sensor_type_e m_sensorType; + BbGuiHelper *m_guiHelper; }; template<class SensorReading> diff --git a/src/plugins/sensors/blackberry/bbutil.h b/src/plugins/sensors/blackberry/bbutil.h index 86882400..528ec010 100644 --- a/src/plugins/sensors/blackberry/bbutil.h +++ b/src/plugins/sensors/blackberry/bbutil.h @@ -46,7 +46,6 @@ namespace BbUtil { void matrixToEulerZXY(const float matrix[3*3], float &thetaX, float &thetaY, float& thetaZ); - qreal radiansToDegrees(qreal radians); } diff --git a/src/plugins/sensors/blackberry/blackberry.pro b/src/plugins/sensors/blackberry/blackberry.pro index 8f7c2f3b..8a05ec0f 100644 --- a/src/plugins/sensors/blackberry/blackberry.pro +++ b/src/plugins/sensors/blackberry/blackberry.pro @@ -1,7 +1,7 @@ load(qt_build_config) TARGET = qtsensors_blackberry -QT = sensors core gui gui-private +QT = sensors core DEFINES += QT_NO_CAST_FROM_ASCII QT_NO_CAST_TO_ASCII load(qt_plugin) @@ -24,6 +24,7 @@ HEADERS += bbsensorbackend.h \ bbproximitysensor.h \ bbrotationsensor.h \ bbtemperaturesensor.h \ + bbguihelper.h \ bbutil.h SOURCES += bbsensorbackend.cpp \ @@ -40,6 +41,7 @@ SOURCES += bbsensorbackend.cpp \ bbproximitysensor.cpp \ bbrotationsensor.cpp \ bbtemperaturesensor.cpp \ + bbguihelper.cpp \ bbutil.cpp \ main.cpp diff --git a/src/plugins/sensors/blackberry/main.cpp b/src/plugins/sensors/blackberry/main.cpp index aba02aa5..6e38138c 100644 --- a/src/plugins/sensors/blackberry/main.cpp +++ b/src/plugins/sensors/blackberry/main.cpp @@ -51,6 +51,7 @@ #include "bbproximitysensor.h" #include "bbrotationsensor.h" #include "bbtemperaturesensor.h" +#include "bbguihelper.h" #include <qsensormanager.h> #include <qsensorplugin.h> @@ -136,6 +137,7 @@ public: if (sensor->identifier() == bbTemperatureSensorId) backend = new BbTemperatureSensor(sensor); backend->initSensorInfo(); + backend->setGuiHelper(&m_guiHelper); return backend; } @@ -144,6 +146,8 @@ private: { return QFile::exists(devicePath); } + + BbGuiHelper m_guiHelper; }; #include "main.moc" |