1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
9 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
14 ** Please review the following information to ensure the GNU Lesser General
15 ** Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
24 ** Alternatively, this file may be used in accordance with the terms and
25 ** conditions contained in a signed written agreement between you and Nokia.
27 ** If you have questions regarding the use of this file, please contact
28 ** Nokia at qt-info@nokia.com.
30 **************************************************************************/
32 #include "deviceorientation.h"
36 #define ORIENTATION_SERVICE "com.nokia.SensorService"
37 #define ORIENTATION_PATH "/org/maemo/contextkit/Screen/TopEdge"
38 #define CONTEXT_INTERFACE "org.maemo.contextkit.Property"
39 #define CONTEXT_CHANGED "ValueChanged"
40 #define CONTEXT_SUBSCRIBE "Subscribe"
41 #define CONTEXT_UNSUBSCRIBE "Unsubscribe"
42 #define CONTEXT_GET "Get"
45 class HarmattanOrientation : public DeviceOrientation
49 HarmattanOrientation()
50 : o(UnknownOrientation), sensorEnabled(false)
53 // connect to the orientation change signal
54 bool ok = QDBusConnection::systemBus().connect(ORIENTATION_SERVICE, ORIENTATION_PATH,
58 SLOT(deviceOrientationChanged(QList<QVariant>,quint64)));
59 // qDebug() << "connection OK" << ok;
60 QDBusMessage reply = QDBusConnection::systemBus().call(
61 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
62 CONTEXT_INTERFACE, CONTEXT_GET));
63 if (reply.type() != QDBusMessage::ErrorMessage) {
65 qvariant_cast<QDBusArgument>(reply.arguments().at(0)) >> args;
66 deviceOrientationChanged(args, 0);
70 ~HarmattanOrientation()
72 // unsubscribe from the orientation sensor
74 QDBusConnection::systemBus().call(
75 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
76 CONTEXT_INTERFACE, CONTEXT_UNSUBSCRIBE));
79 inline Orientation orientation() const
84 void setOrientation(Orientation)
88 void pauseListening() {
90 // unsubscribe from the orientation sensor
91 QDBusConnection::systemBus().call(
92 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
93 CONTEXT_INTERFACE, CONTEXT_UNSUBSCRIBE));
94 sensorEnabled = false;
98 void resumeListening() {
100 // subscribe to the orientation sensor
101 QDBusMessage reply = QDBusConnection::systemBus().call(
102 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
103 CONTEXT_INTERFACE, CONTEXT_SUBSCRIBE));
105 if (reply.type() == QDBusMessage::ErrorMessage) {
106 qWarning("Unable to retrieve device orientation: %s", qPrintable(reply.errorMessage()));
108 sensorEnabled = true;
114 void deviceOrientationChanged(QList<QVariant> args,quint64)
116 if (args.count() == 0)
118 Orientation newOrientation = toOrientation(args.at(0).toString());
119 if (newOrientation != o) {
121 emit orientationChanged();
123 // qDebug() << "orientation" << args.at(0).toString();
127 static Orientation toOrientation(const QString &nativeOrientation)
129 if (nativeOrientation == "top")
131 else if (nativeOrientation == "left")
133 else if (nativeOrientation == "bottom")
134 return LandscapeInverted;
135 else if (nativeOrientation == "right")
136 return PortraitInverted;
137 return UnknownOrientation;
145 DeviceOrientation* DeviceOrientation::instance()
147 static HarmattanOrientation *o = new HarmattanOrientation;
151 #include "deviceorientation_harmattan.moc"