Stop the accelerometer while the screen is locked
[ameter] / mainwindow.cpp
index 672c4e2..e3bac8c 100644 (file)
@@ -1,5 +1,10 @@
 #include <QtCore/QCoreApplication>
 
+#ifdef Q_WS_MAEMO_5
+#include <mce/dbus-names.h>
+#include <mce/mode-names.h>
+#endif
+
 #include "mainwindow.h"
 #include "asettings.h"
 #include "about.h"
@@ -12,6 +17,10 @@ extern int smoothing;
 
 int data_rate = 0;
 
+#ifdef Q_WS_MAEMO_5
+static QDBusConnection dBusConnection = QDBusConnection::systemBus();
+#endif
+
 MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
 {
@@ -37,7 +46,14 @@ MainWindow::MainWindow(QWidget *parent)
        accelerometer->addFilter(awidget);
        accelerometer->setDataRate(data_rate);
        accelerometer->start();
-       
+
+#ifdef Q_WS_MAEMO_5
+       dBusInterface = new QDBusInterface(MCE_SERVICE, MCE_REQUEST_PATH,
+               MCE_REQUEST_IF, dBusConnection, this);
+       dBusConnection.connect(MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
+               MCE_DISPLAY_SIG, this, SLOT(screenChange(const QDBusMessage &)));
+#endif
 }
 
 MainWindow::~MainWindow()
@@ -57,8 +73,12 @@ void MainWindow::showSettings()
 {
        SettingsDialog dialog(this);
        QSettings settings("igorinov", "ameter", this);
+       int code;
 
-       if (dialog.exec() != QDialog::Accepted)
+       accelerometer->stop();
+       code = dialog.exec();
+       accelerometer->start();
+       if (code != QDialog::Accepted)
        {
                return;
        }
@@ -70,24 +90,11 @@ void MainWindow::showSettings()
        settings.setValue("rate", data_rate);
 
        awidget->setGravity(g_n);
-       accelerometer->stop();
        accelerometer->setDataRate(data_rate);
-       accelerometer->start();
 }
 
 void MainWindow::setOrientation(ScreenOrientation orientation)
 {
-#if defined(Q_OS_SYMBIAN)
-    // If the version of Qt on the device is < 4.7.2, that attribute won't work
-    if (orientation != ScreenOrientationAuto) {
-        const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
-        if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
-            qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
-            return;
-        }
-    }
-#endif // Q_OS_SYMBIAN
-
     Qt::WidgetAttribute attribute;
     switch (orientation) {
 #if QT_VERSION < 0x040702
@@ -129,3 +136,16 @@ void MainWindow::showExpanded()
 #endif
 }
 
+#ifdef Q_WS_MAEMO_5
+void MainWindow::screenChange(const QDBusMessage &message)
+{
+    QString state = message.arguments().at(0).toString();
+    if (!state.isEmpty()) {
+        if (state == MCE_DISPLAY_OFF_STRING)
+            accelerometer->stop();
+        if (state == MCE_DISPLAY_ON_STRING)
+            accelerometer->start();
+    }
+}
+#endif
+