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"
45 #include <sensrvchannelfinder.h>
46 #include <sensrvdatalistener.h>
47 #include <sensrvchannel.h>
48 #include <sensrvorientationsensor.h>
50 class SymbianOrientation : public DeviceOrientation, public MSensrvDataListener
55 : DeviceOrientation(), m_current(UnknownOrientation), m_sensorChannel(0), m_channelOpen(false)
59 qWarning("No valid sensors found.");
64 if (m_sensorChannel) {
65 m_sensorChannel->StopDataListening();
66 m_sensorChannel->CloseChannel();
67 delete m_sensorChannel;
73 CSensrvChannelFinder *channelFinder = CSensrvChannelFinder::NewLC();
74 RSensrvChannelInfoList channelInfoList;
75 CleanupClosePushL(channelInfoList);
77 TSensrvChannelInfo searchConditions;
78 searchConditions.iChannelType = KSensrvChannelTypeIdOrientationData;
79 channelFinder->FindChannelsL(channelInfoList, searchConditions);
81 for (int i = 0; i < channelInfoList.Count(); ++i) {
82 TRAPD(error, m_sensorChannel = CSensrvChannel::NewL(channelInfoList[i]));
84 TRAP(error, m_sensorChannel->OpenChannelL());
86 TRAP(error, m_sensorChannel->StartDataListeningL(this, 1, 1, 0));
91 delete m_sensorChannel;
96 channelInfoList.Close();
97 CleanupStack::Pop(&channelInfoList);
98 CleanupStack::PopAndDestroy(channelFinder);
101 Orientation orientation() const
106 void setOrientation(Orientation) { }
109 DeviceOrientation::Orientation m_current;
110 CSensrvChannel *m_sensorChannel;
112 void pauseListening() {
113 if (m_sensorChannel && m_channelOpen) {
114 m_sensorChannel->StopDataListening();
115 m_sensorChannel->CloseChannel();
116 m_channelOpen = false;
120 void resumeListening() {
121 if (m_sensorChannel && !m_channelOpen) {
122 TRAPD(error, m_sensorChannel->OpenChannelL());
124 TRAP(error, m_sensorChannel->StartDataListeningL(this, 1, 1, 0));
126 m_channelOpen = true;
130 delete m_sensorChannel;
136 void DataReceived(CSensrvChannel &channel, TInt count, TInt dataLost)
139 if (channel.GetChannelInfo().iChannelType == KSensrvChannelTypeIdOrientationData) {
140 TSensrvOrientationData data;
141 for (int i = 0; i < count; ++i) {
142 TPckgBuf<TSensrvOrientationData> dataBuf;
143 channel.GetData(dataBuf);
145 Orientation orientation = UnknownOrientation;
146 switch (data.iDeviceOrientation) {
147 case TSensrvOrientationData::EOrientationDisplayUp:
148 orientation = Portrait;
150 case TSensrvOrientationData::EOrientationDisplayRightUp:
151 orientation = Landscape;
153 case TSensrvOrientationData::EOrientationDisplayLeftUp:
154 orientation = LandscapeInverted;
156 case TSensrvOrientationData::EOrientationDisplayDown:
157 orientation = PortraitInverted;
159 case TSensrvOrientationData::EOrientationUndefined:
160 case TSensrvOrientationData::EOrientationDisplayUpwards:
161 case TSensrvOrientationData::EOrientationDisplayDownwards:
166 if (m_current != orientation && orientation != UnknownOrientation) {
167 m_current = orientation;
168 emit orientationChanged();
174 void DataError(CSensrvChannel& /* channel */, TSensrvErrorSeverity /* error */)
178 void GetDataListenerInterfaceL(TUid /* interfaceUid */, TAny*& /* interface */)
184 DeviceOrientation* DeviceOrientation::instance()
186 static SymbianOrientation *o = 0;
188 o = new SymbianOrientation;
192 #include "deviceorientation_symbian.moc"