summaryrefslogtreecommitdiffstats
path: root/src/plugins/sensors/blackberry/bbrotationsensor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/sensors/blackberry/bbrotationsensor.cpp')
-rw-r--r--src/plugins/sensors/blackberry/bbrotationsensor.cpp70
1 files changed, 68 insertions, 2 deletions
diff --git a/src/plugins/sensors/blackberry/bbrotationsensor.cpp b/src/plugins/sensors/blackberry/bbrotationsensor.cpp
index fe34ea4e..28dc49c9 100644
--- a/src/plugins/sensors/blackberry/bbrotationsensor.cpp
+++ b/src/plugins/sensors/blackberry/bbrotationsensor.cpp
@@ -42,10 +42,16 @@
#include "bbutil.h"
#include <QtCore/qmath.h>
+#include <QGuiApplication>
+#include <QScreen>
+#include <qpa/qplatformscreen.h>
BbRotationSensor::BbRotationSensor(QSensor *sensor)
- : BbSensorBackend<QRotationReading>(devicePath(), SENSOR_TYPE_ROTATION_MATRIX, sensor)
+ : BbSensorBackend<QRotationReading>(devicePath(), SENSOR_TYPE_ROTATION_MATRIX, sensor),
+ m_orientation(Qt::PrimaryOrientation),
+ m_nativeOrientation(Qt::PrimaryOrientation)
{
+ updateOrientation();
setDescription(QLatin1String("Device rotation in degrees"));
}
@@ -95,16 +101,46 @@ static void matrixToEulerZXY(const float matrix[3*3],
}
}
+static void remapMatrix(const float inputMatrix[3*3],
+ const float mappingMatrix[4],
+ float outputMatrix[3*3])
+{
+ int i,j,k;
+
+ for (i = 0; i < 3; i++) {
+ for (j = 0; j < 2; j++) { //only goto 2 because last column stays unchanged
+
+ outputMatrix[i*3+j] = 0;
+
+ for (k = 0; k < 2; k++) { //only goto 2 because we know rotation matrix is zero in bottom row
+ outputMatrix[i*3+j] += inputMatrix[i*3+k] * mappingMatrix[k*2+j];
+ }
+ }
+
+ outputMatrix[i*3+2] = inputMatrix[i*3+2];
+ }
+}
+
+
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
// uses Z-X'-Y''.
// So extract the euler angles using the Z-X'-Y'' system from the matrix.
float xRad, yRad, zRad;
- matrixToEulerZXY(event.rotation_matrix, xRad, yRad, zRad);
+
+ if (isAutoAxisRemappingEnabled() && m_orientation!=m_nativeOrientation) {
+ float mappedRotationMatrix[3*3];
+ remapMatrix(event.rotation_matrix, m_mappingMatrix, mappedRotationMatrix);
+ matrixToEulerZXY(mappedRotationMatrix, xRad, yRad, zRad);
+ } else {
+ matrixToEulerZXY(event.rotation_matrix, xRad, yRad, zRad);
+ }
+
reading->setX(radiansToDegrees(xRad));
reading->setY(radiansToDegrees(yRad));
reading->setZ(radiansToDegrees(zRad));
+
return true;
}
@@ -112,3 +148,33 @@ qreal BbRotationSensor::convertValue(float bbValueInRad)
{
return radiansToDegrees(bbValueInRad);
}
+
+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);
+ }
+}