1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the tools applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "deviceorientation.h"
46 #define ORIENTATION_SERVICE QStringLiteral("com.nokia.SensorService")
47 #define ORIENTATION_PATH QStringLiteral("/org/maemo/contextkit/Screen/TopEdge")
48 #define CONTEXT_INTERFACE QStringLiteral("org.maemo.contextkit.Property")
49 #define CONTEXT_CHANGED QStringLiteral("ValueChanged")
50 #define CONTEXT_SUBSCRIBE QStringLiteral("Subscribe")
51 #define CONTEXT_UNSUBSCRIBE QStringLiteral("Unsubscribe")
52 #define CONTEXT_GET QStringLiteral("Get")
55 class HarmattanOrientation : public DeviceOrientation
59 HarmattanOrientation()
60 : o(UnknownOrientation), sensorEnabled(false)
63 // connect to the orientation change signal
64 bool ok = QDBusConnection::systemBus().connect(ORIENTATION_SERVICE, ORIENTATION_PATH,
68 SLOT(deviceOrientationChanged(QList<QVariant>,quint64)));
69 // qDebug() << "connection OK" << ok;
70 QDBusMessage reply = QDBusConnection::systemBus().call(
71 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
72 CONTEXT_INTERFACE, CONTEXT_GET));
73 if (reply.type() != QDBusMessage::ErrorMessage) {
75 qvariant_cast<QDBusArgument>(reply.arguments().at(0)) >> args;
76 deviceOrientationChanged(args, 0);
80 ~HarmattanOrientation()
82 // unsubscribe from the orientation sensor
84 QDBusConnection::systemBus().call(
85 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
86 CONTEXT_INTERFACE, CONTEXT_UNSUBSCRIBE));
89 inline Orientation orientation() const
94 void setOrientation(Orientation)
98 void pauseListening() {
100 // unsubscribe from the orientation sensor
101 QDBusConnection::systemBus().call(
102 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
103 CONTEXT_INTERFACE, CONTEXT_UNSUBSCRIBE));
104 sensorEnabled = false;
108 void resumeListening() {
109 if (!sensorEnabled) {
110 // subscribe to the orientation sensor
111 QDBusMessage reply = QDBusConnection::systemBus().call(
112 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
113 CONTEXT_INTERFACE, CONTEXT_SUBSCRIBE));
115 if (reply.type() == QDBusMessage::ErrorMessage) {
116 qWarning("Unable to retrieve device orientation: %s", qPrintable(reply.errorMessage()));
118 sensorEnabled = true;
124 void deviceOrientationChanged(QList<QVariant> args,quint64)
126 if (args.count() == 0)
128 Orientation newOrientation = toOrientation(args.at(0).toString());
129 if (newOrientation != o) {
131 emit orientationChanged();
133 // qDebug() << "orientation" << args.at(0).toString();
137 static Orientation toOrientation(const QString &nativeOrientation)
139 if (nativeOrientation == QLatin1String("top"))
141 else if (nativeOrientation == QLatin1String("left"))
143 else if (nativeOrientation == QLatin1String("bottom"))
144 return LandscapeInverted;
145 else if (nativeOrientation == QLatin1String("right"))
146 return PortraitInverted;
147 return UnknownOrientation;
155 DeviceOrientation* DeviceOrientation::instance()
157 static HarmattanOrientation *o = new HarmattanOrientation;
161 #include "deviceorientation_harmattan.moc"