deeb45dc5a856f385cbb6b6eac4ef2d0adbde0a7
[profile/ivi/qtdeclarative.git] / tools / qmlviewer / deviceorientation_symbian.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the tools applications of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "deviceorientation.h"
43
44 #include <e32base.h>
45 #include <sensrvchannelfinder.h>
46 #include <sensrvdatalistener.h>
47 #include <sensrvchannel.h>
48 #include <sensrvorientationsensor.h>
49
50 class SymbianOrientation : public DeviceOrientation, public MSensrvDataListener
51 {
52     Q_OBJECT
53 public:
54     SymbianOrientation()
55         : DeviceOrientation(), m_current(UnknownOrientation), m_sensorChannel(0), m_channelOpen(false)
56     {
57         TRAP_IGNORE(initL());
58         if (!m_sensorChannel)
59             qWarning("No valid sensors found.");
60     }
61
62     ~SymbianOrientation()
63     {
64         if (m_sensorChannel) {
65             m_sensorChannel->StopDataListening();
66             m_sensorChannel->CloseChannel();
67             delete m_sensorChannel;
68         }
69     }
70
71     void initL()
72     {
73         CSensrvChannelFinder *channelFinder = CSensrvChannelFinder::NewLC();
74         RSensrvChannelInfoList channelInfoList;
75         CleanupClosePushL(channelInfoList);
76
77         TSensrvChannelInfo searchConditions;
78         searchConditions.iChannelType = KSensrvChannelTypeIdOrientationData;
79         channelFinder->FindChannelsL(channelInfoList, searchConditions);
80
81         for (int i = 0; i < channelInfoList.Count(); ++i) {
82             TRAPD(error, m_sensorChannel = CSensrvChannel::NewL(channelInfoList[i]));
83             if (!error)
84                 TRAP(error, m_sensorChannel->OpenChannelL());
85             if (!error) {
86                 TRAP(error, m_sensorChannel->StartDataListeningL(this, 1, 1, 0));
87                 m_channelOpen = true;
88                 break;
89            }
90             if (error) {
91                 delete m_sensorChannel;
92                 m_sensorChannel = 0;
93             }
94         }
95
96         channelInfoList.Close();
97         CleanupStack::Pop(&channelInfoList);
98         CleanupStack::PopAndDestroy(channelFinder);
99     }
100
101     Orientation orientation() const
102     {
103         return m_current;
104     }
105
106    void setOrientation(Orientation) { }
107
108 private:
109     DeviceOrientation::Orientation m_current;
110     CSensrvChannel *m_sensorChannel;
111     bool m_channelOpen;
112     void pauseListening() {
113         if (m_sensorChannel && m_channelOpen) {
114             m_sensorChannel->StopDataListening();
115             m_sensorChannel->CloseChannel();
116             m_channelOpen = false;
117         }
118     }
119
120     void resumeListening() {
121         if (m_sensorChannel && !m_channelOpen) {
122             TRAPD(error, m_sensorChannel->OpenChannelL());
123             if (!error) {
124                 TRAP(error, m_sensorChannel->StartDataListeningL(this, 1, 1, 0));
125                 if (!error) {
126                     m_channelOpen = true;
127                 }
128             }
129             if (error) {
130                 delete m_sensorChannel;
131                 m_sensorChannel = 0;
132             }
133         }
134     }
135
136     void DataReceived(CSensrvChannel &channel, TInt count, TInt dataLost)
137     {
138         Q_UNUSED(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);
144                 data = dataBuf();
145                 Orientation orientation = UnknownOrientation;
146                 switch (data.iDeviceOrientation) {
147                 case TSensrvOrientationData::EOrientationDisplayUp:
148                     orientation = Portrait;
149                     break;
150                 case TSensrvOrientationData::EOrientationDisplayRightUp:
151                     orientation = Landscape;
152                     break;
153                 case TSensrvOrientationData::EOrientationDisplayLeftUp:
154                     orientation = LandscapeInverted;
155                     break;
156                 case TSensrvOrientationData::EOrientationDisplayDown:
157                     orientation = PortraitInverted;
158                     break;
159                 case TSensrvOrientationData::EOrientationUndefined:
160                 case TSensrvOrientationData::EOrientationDisplayUpwards:
161                 case TSensrvOrientationData::EOrientationDisplayDownwards:
162                 default:
163                     break;
164                 }
165
166                 if (m_current != orientation && orientation != UnknownOrientation) {
167                     m_current = orientation;
168                     emit orientationChanged();
169                 }
170            }
171         }
172     }
173
174    void DataError(CSensrvChannel& /* channel */, TSensrvErrorSeverity /* error */)
175    {
176    }
177
178    void GetDataListenerInterfaceL(TUid /* interfaceUid */, TAny*& /* interface */)
179    {
180    }
181 };
182
183
184 DeviceOrientation* DeviceOrientation::instance()
185 {
186     static SymbianOrientation *o = 0;
187     if (!o)
188         o = new SymbianOrientation;
189     return o;
190 }
191
192 #include "deviceorientation_symbian.moc"