From 6d178306f71564471cc08eb7dc98743c3752cac4 Mon Sep 17 00:00:00 2001 From: Harald Fernengel Date: Wed, 16 Jun 2010 15:17:33 +0200 Subject: Fix the N900 device orientation backend Use D-Bus calls instead of polling Reviewed-by: Alan Alpert --- tools/qml/deviceorientation_maemo.cpp | 123 +++++++++++++++------------------- tools/qml/qml.pri | 1 + 2 files changed, 56 insertions(+), 68 deletions(-) diff --git a/tools/qml/deviceorientation_maemo.cpp b/tools/qml/deviceorientation_maemo.cpp index 7948e2addb..443edc8071 100644 --- a/tools/qml/deviceorientation_maemo.cpp +++ b/tools/qml/deviceorientation_maemo.cpp @@ -40,100 +40,87 @@ ****************************************************************************/ #include "deviceorientation.h" -#include -#include +#include + +#include +#include 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 cff65be9b9..58d8cc1e1b 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 -- cgit v1.2.1