tizen 2.3.1 release
[framework/web/mobile/wrt-plugins-tizen.git] / src / Systeminfo / SystemInfoDeviceOrientation.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <string>
19 #include <vconf.h>
20 #include <Logger.h>
21 #include <PlatformException.h>
22 #include "SystemInfoDeviceOrientation.h"
23 #include "SystemInfo.h"
24 #include "SystemInfoUtil.h"
25
26 namespace DeviceAPI {
27 namespace SystemInfo {
28
29 namespace {
30     const std::string ORIENTATION_PORTRAIT_PRIMARY = "PORTRAIT_PRIMARY";
31     const std::string ORIENTATION_PORTRAIT_SECONDARY = "PORTRAIT_SECONDARY";
32     const std::string ORIENTATION_LANDSCAPE_PRIMARY = "LANDSCAPE_PRIMARY";
33     const std::string ORIENTATION_LANDSCAPE_SECONDARY = "LANDSCAPE_SECONDARY";
34 }
35
36 SystemInfoDeviceOrientation::SystemInfoDeviceOrientation()
37 {
38     LOGD("Entered");
39
40     m_status = fetchStatus();
41     m_is_auto_rotation = fetchIsAutoRotation();
42 }
43
44 SystemInfoDeviceOrientation::~SystemInfoDeviceOrientation()
45 {
46     LOGD("Entered");
47 }
48
49 std::string SystemInfoDeviceOrientation::getStatus() const
50 {
51     LOGD("Entered");
52     return m_status;
53 }
54
55 bool SystemInfoDeviceOrientation::isAutoRotation() const
56 {
57     LOGD("Entered");
58     return m_is_auto_rotation;
59 }
60
61 std::string SystemInfoDeviceOrientation::fetchStatus()
62 {
63     LOGD("Entered");
64     int rotation = AUTO_ROTATION_DEGREE_UNKNOWN;
65     std::string status = ORIENTATION_PORTRAIT_PRIMARY;
66
67     sensor_data_t data;
68     int handle = connectSensor();
69
70     bool ret = sensord_get_data(handle, AUTO_ROTATION_BASE_DATA_SET, &data);
71     if (ret == false) {
72         LOGE("Failed to get data(sensord_get_data)");
73     } else {
74         LOGD("size of the data value array:%d", data.value_count);
75         if (data.value_count > 0 ) {
76             rotation = data.values[0];
77             LOGD("ratation value : %d", rotation);
78         } else {
79             LOGE("Failed to get data : the size of array is less than 0");
80         }
81     }
82
83     switch (rotation) {
84         case AUTO_ROTATION_DEGREE_UNKNOWN:
85         case AUTO_ROTATION_DEGREE_0:
86             LOGD("AUTO_ROTATION_DEGREE_0");
87             status = ORIENTATION_PORTRAIT_PRIMARY;
88             break;
89         case AUTO_ROTATION_DEGREE_90:
90             LOGD("AUTO_ROTATION_DEGREE_90");
91             status = ORIENTATION_LANDSCAPE_PRIMARY;
92             break;
93         case AUTO_ROTATION_DEGREE_180:
94             LOGD("AUTO_ROTATION_DEGREE_180");
95             status = ORIENTATION_PORTRAIT_SECONDARY;
96             break;
97         case AUTO_ROTATION_DEGREE_270:
98             LOGD("AUTO_ROTATION_DEGREE_270");
99             status = ORIENTATION_LANDSCAPE_SECONDARY;
100             break;
101         default:
102             LOGE("Received unexpected data: %u", rotation);
103             throw Common::UnknownException("Received unexpected data");
104     }
105
106     disconnectSensor(handle);
107
108     return status;
109 }
110
111 bool SystemInfoDeviceOrientation::fetchIsAutoRotation()
112 {
113     LOGD("Entered");
114     int is_auto_rotation = 0;
115     int ret = 0;
116
117     ret = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &is_auto_rotation);
118     if (ret == VCONF_OK) {
119         if (is_auto_rotation) {
120             return true;
121         }
122     } else {
123         std::string log_msg = "Failed to check VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL";
124         LOGE("%s", log_msg.c_str());
125         SystemInfoUtil::throwSystemInfoException(0, log_msg);
126     }
127     return false;
128 }
129
130 int SystemInfoDeviceOrientation::connectSensor()
131 {
132     LOGD("Entered");
133     sensor_t sensor;
134     sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
135
136     int handle_orientation = sensord_connect(sensor);
137     if (handle_orientation < 0) {
138         std::string log_msg = "Failed to connect auto rotation sensor";
139         LOGE("%s", log_msg.c_str());
140         SystemInfoUtil::throwSystemInfoException(0, log_msg);
141     }
142
143     int state = sensord_start(handle_orientation, 0);
144     if(state < 0) {
145         sensord_disconnect(handle_orientation);
146         std::string log_msg = "Failed to start auto rotation sensor";
147         LOGE("%s", log_msg.c_str());
148         SystemInfoUtil::throwSystemInfoException(0, log_msg);
149     }
150     LOGD("Sensor starts successfully = %d", handle_orientation);
151     return handle_orientation;
152 }
153
154 void SystemInfoDeviceOrientation::disconnectSensor(int handle_orientation)
155 {
156     LOGD("Entered");
157
158     int state = sensord_stop(handle_orientation);
159     LOGD("handle_orientation sf_stop state = %d", state);
160
161     state = sensord_disconnect(handle_orientation);
162     LOGD("handle_orientation sf_disconnect state = %d", state);
163 }
164
165 } // SystemInfo
166 } // DeviceAPI