summaryrefslogtreecommitdiffstats
path: root/tools/qml
diff options
context:
space:
mode:
authorHarald Fernengel <harald.fernengel@nokia.com>2010-06-16 13:17:33 (GMT)
committerHarald Fernengel <harald.fernengel@nokia.com>2010-06-16 13:17:33 (GMT)
commit6d178306f71564471cc08eb7dc98743c3752cac4 (patch)
tree056b93b32a54f4642b98a9ba55625e0bbf1a5a19 /tools/qml
parent6d4e14ef0437ce8f73bddbcb267cf5ef708fbdec (diff)
downloadQt-6d178306f71564471cc08eb7dc98743c3752cac4.zip
Qt-6d178306f71564471cc08eb7dc98743c3752cac4.tar.gz
Qt-6d178306f71564471cc08eb7dc98743c3752cac4.tar.bz2
Fix the N900 device orientation backend
Use D-Bus calls instead of polling Reviewed-by: Alan Alpert
Diffstat (limited to 'tools/qml')
-rw-r--r--tools/qml/deviceorientation_maemo.cpp123
-rw-r--r--tools/qml/qml.pri1
2 files changed, 56 insertions, 68 deletions
diff --git a/tools/qml/deviceorientation_maemo.cpp b/tools/qml/deviceorientation_maemo.cpp
index 7948e2a..443edc8 100644
--- a/tools/qml/deviceorientation_maemo.cpp
+++ b/tools/qml/deviceorientation_maemo.cpp
@@ -40,100 +40,87 @@
****************************************************************************/
#include "deviceorientation.h"
-#include <stdio.h>
-#include <stdlib.h>
+#include <QtDBus>
+
+#include <mce/mode-names.h>
+#include <mce/dbus-names.h>
class MaemoOrientation : public DeviceOrientation
{
Q_OBJECT
public:
MaemoOrientation()
- : DeviceOrientation(),m_current(Portrait), m_lastSeen(Portrait), m_lastSeenCount(0)
+ : o(UnknownOrientation)
{
- m_current = get();
- if (m_current == UnknownOrientation)
- m_current = Portrait;
+ // enable the orientation sensor
+ QDBusConnection::systemBus().call(
+ QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
+ MCE_REQUEST_IF, MCE_ACCELEROMETER_ENABLE_REQ));
+
+ // query the initial orientation
+ QDBusMessage reply = QDBusConnection::systemBus().call(
+ QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
+ MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET));
+ if (reply.type() == QDBusMessage::ErrorMessage) {
+ qWarning("Unable to retrieve device orientation: %s", qPrintable(reply.errorMessage()));
+ } else {
+ o = toOrientation(reply.arguments().value(0).toString());
+ }
- startTimer(100);
+ // connect to the orientation change signal
+ QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
+ MCE_DEVICE_ORIENTATION_SIG,
+ this,
+ SLOT(deviceOrientationChanged(QString)));
}
- Orientation orientation() const {
- return m_current;
+ ~MaemoOrientation()
+ {
+ // disable the orientation sensor
+ QDBusConnection::systemBus().call(
+ QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
+ MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
}
- void setOrientation(Orientation) { }
-
-protected:
- virtual void timerEvent(QTimerEvent *)
+ inline Orientation orientation() const
{
- Orientation c = get();
-
- if (c == m_lastSeen) {
- m_lastSeenCount++;
- } else {
- m_lastSeenCount = 0;
- m_lastSeen = c;
- }
-
- if (m_lastSeen != UnknownOrientation && m_lastSeen != m_current && m_lastSeenCount > 4) {
- m_current = m_lastSeen;
- emit orientationChanged();
- printf("%d\n", m_current);
- }
+ return o;
}
-signals:
- void changed();
-
-private:
- Orientation m_current;
- Orientation m_lastSeen;
- int m_lastSeenCount;
-
- Orientation get()
+ void setOrientation(Orientation o)
{
- Orientation o = UnknownOrientation;
-
- int ax, ay, az;
-
- read(&ax, &ay, &az);
+ }
- if (abs(az) > 850) {
- o = UnknownOrientation;
- } else if (ax < -750) {
- o = Portrait;
- } else if (ax > 750) {
- o = PortraitInverted;
- } else if (ay < -750) {
- o = Landscape;
- } else if (ay > 750) {
- o = LandscapeInverted;
- }
+private Q_SLOTS:
+ void deviceOrientationChanged(const QString &newOrientation)
+ {
+ o = toOrientation(newOrientation);
- return o;
+ emit orientationChanged();
+// printf("%d\n", o);
}
- int read(int *ax,int *ay,int *az)
+private:
+ static Orientation toOrientation(const QString &nativeOrientation)
{
- static const char *accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
-
- FILE *fd;
- int rs;
- fd = fopen(accel_filename, "r");
- if(fd==NULL){ printf("liqaccel, cannot open for reading\n"); return -1;}
- rs=fscanf((FILE*) fd,"%i %i %i",ax,ay,az);
- fclose(fd);
- if(rs != 3){ printf("liqaccel, cannot read information\n"); return -2;}
- return 0;
+ if (nativeOrientation == MCE_ORIENTATION_LANDSCAPE)
+ return Landscape;
+ else if (nativeOrientation == MCE_ORIENTATION_LANDSCAPE_INVERTED)
+ return LandscapeInverted;
+ else if (nativeOrientation == MCE_ORIENTATION_PORTRAIT)
+ return Portrait;
+ else if (nativeOrientation == MCE_ORIENTATION_PORTRAIT_INVERTED)
+ return PortraitInverted;
+ return UnknownOrientation;
}
-};
+private:
+ Orientation o;
+};
DeviceOrientation* DeviceOrientation::instance()
{
- static MaemoOrientation *o = 0;
- if (!o)
- o = new MaemoOrientation;
+ static MaemoOrientation *o = new MaemoOrientation;
return o;
}
diff --git a/tools/qml/qml.pri b/tools/qml/qml.pri
index cff65be..58d8cc1 100644
--- a/tools/qml/qml.pri
+++ b/tools/qml/qml.pri
@@ -18,6 +18,7 @@ SOURCES += $$PWD/qmlruntime.cpp \
RESOURCES = $$PWD/qmlruntime.qrc
maemo5 {
+ QT += dbus
SOURCES += $$PWD/deviceorientation_maemo.cpp
} else {
SOURCES += $$PWD/deviceorientation.cpp