1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** 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
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General 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.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "deviceorientation.h"
46 #define ORIENTATION_SERVICE "com.nokia.SensorService"
47 #define ORIENTATION_PATH "/org/maemo/contextkit/Screen/TopEdge"
48 #define CONTEXT_INTERFACE "org.maemo.contextkit.Property"
49 #define CONTEXT_CHANGED "ValueChanged"
50 #define CONTEXT_SUBSCRIBE "Subscribe"
51 #define CONTEXT_UNSUBSCRIBE "Unsubscribe"
52 #define CONTEXT_GET "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 == "top")
141 else if (nativeOrientation == "left")
143 else if (nativeOrientation == "bottom")
144 return LandscapeInverted;
145 else if (nativeOrientation == "right")
146 return PortraitInverted;
147 return UnknownOrientation;
155 DeviceOrientation* DeviceOrientation::instance()
157 static HarmattanOrientation *o = new HarmattanOrientation;
161 #include "deviceorientation_harmattan.moc"