4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
27 #include <sensor_internal.h>
30 #include "appcore-internal.h"
34 int (*callback) (void *event_info, enum appcore_rm, void *);
41 struct ui_wm_rotate* wm_rotate;
43 static struct rot_s rot;
46 enum auto_rotation_state re;
50 static struct rot_evt re_to_rm[] = {
52 AUTO_ROTATION_DEGREE_0,
53 APPCORE_RM_PORTRAIT_NORMAL,
56 AUTO_ROTATION_DEGREE_90,
57 APPCORE_RM_LANDSCAPE_NORMAL,
60 AUTO_ROTATION_DEGREE_180,
61 APPCORE_RM_PORTRAIT_REVERSE,
64 AUTO_ROTATION_DEGREE_270,
65 APPCORE_RM_LANDSCAPE_REVERSE,
69 static enum appcore_rm __get_mode(sensor_data_t data)
75 m = APPCORE_RM_UNKNOWN;
76 if (data.value_count > 0) {
77 event = (int)data.values[0];
79 _ERR("Failed to get sensor data");
83 for (i = 0; i < sizeof(re_to_rm) / sizeof(re_to_rm[0]); i++) {
84 if (re_to_rm[i].re == event) {
93 static void __changed_cb(sensor_t sensor, unsigned int event_type,
94 sensor_data_t *data, void *user_data)
101 if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT) {
106 m = __get_mode(*data);
108 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
111 if (rot.cb_set && rot.mode != m) {
112 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
113 rot.callback((void *)&m, m, user_data);
119 static void __lock_cb(keynode_t *node, void *data)
124 rot.lock = !vconf_keynode_get_bool(node);
127 m = APPCORE_RM_PORTRAIT_NORMAL;
129 rot.callback((void *)&m, m, data);
132 _DBG("[APP %d] Rotation locked", getpid());
136 _DBG("[APP %d] Rotation unlocked", getpid());
139 r = appcore_get_rotation_state(&m);
140 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(),
142 if (!r && rot.mode != m) {
143 rot.callback((void *)&m, m, data);
150 static void __add_rotlock(void *data)
156 r = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
158 _DBG("[APP %d] Rotation vconf get bool failed", getpid());
162 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb,
166 static void __del_rotlock(void)
168 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
172 EXPORT_API int appcore_set_rotation_cb(int (*cb) (void *evnet_info, enum appcore_rm, void *),
176 return rot.wm_rotate->set_rotation_cb(cb, data);
180 sensor_t sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
187 if (rot.callback != NULL) {
192 handle = sensord_connect(sensor);
194 _ERR("sensord_connect failed: %d", handle);
198 r = sensord_register_event(handle, AUTO_ROTATION_CHANGE_STATE_EVENT,
199 SENSOR_INTERVAL_NORMAL, 0, __changed_cb, data);
201 _ERR("sensord_register_event failed");
202 sensord_disconnect(handle);
210 r = sensord_start(handle, 0);
212 _ERR("sensord_start failed");
213 r = sensord_unregister_event(handle, AUTO_ROTATION_CHANGE_STATE_EVENT);
215 _ERR("sensord_unregister_event failed");
220 rot.sensord_started = 0;
221 sensord_disconnect(handle);
224 rot.sensord_started = 1;
232 EXPORT_API int appcore_unset_rotation_cb(void)
235 return rot.wm_rotate->unset_rotation_cb();
239 _retv_if(rot.callback == NULL, 0);
244 r = sensord_unregister_event(rot.handle,
245 AUTO_ROTATION_CHANGE_STATE_EVENT);
247 _ERR("sensord_unregister_event failed");
255 if (rot.sensord_started == 1) {
256 r = sensord_stop(rot.handle);
258 _ERR("sensord_stop failed");
261 rot.sensord_started = 0;
264 r = sensord_disconnect(rot.handle);
266 _ERR("sensord_disconnect failed");
274 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
277 return rot.wm_rotate->get_rotation_state(curr);
287 r = sensord_get_data(rot.handle, AUTO_ROTATION_SENSOR, &data);
289 _ERR("sensord_get_data failed");
290 *curr = APPCORE_RM_UNKNOWN;
294 *curr = __get_mode(data);
299 EXPORT_API int appcore_pause_rotation_cb(void)
302 return rot.wm_rotate->pause_rotation_cb();
306 _retv_if(rot.callback == NULL, 0);
307 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
312 r = sensord_unregister_event(rot.handle,
313 AUTO_ROTATION_CHANGE_STATE_EVENT);
315 _ERR("sensord_unregister_event failed");
321 if (rot.sensord_started == 1) {
322 r = sensord_stop(rot.handle);
324 _ERR("sensord_stop failed");
327 rot.sensord_started = 0;
334 EXPORT_API int appcore_resume_rotation_cb(void)
337 return rot.wm_rotate->resume_rotation_cb();
342 _retv_if(rot.callback == NULL, 0);
343 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
345 if (rot.cb_set == 0) {
346 r = sensord_register_event(rot.handle,
347 AUTO_ROTATION_CHANGE_STATE_EVENT,
348 SENSOR_INTERVAL_NORMAL, 0, __changed_cb, rot.cbdata);
350 _ERR("sensord_register_event failed");
356 if (rot.sensord_started == 0) {
357 r = sensord_start(rot.handle, 0);
359 _ERR("sensord_start failed");
360 r = sensord_unregister_event(rot.handle,
361 AUTO_ROTATION_CHANGE_STATE_EVENT);
363 _ERR("sensord_unregister_event failed");
367 rot.sensord_started = 1;
370 __add_rotlock(rot.cbdata);
372 r = appcore_get_rotation_state(&m);
373 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
374 if (!r && rot.mode != m && rot.lock == 0) {
375 rot.callback((void *)&m, m, rot.cbdata);
382 EXPORT_API int appcore_set_wm_rotation(struct ui_wm_rotate* wm_rotate)
384 if (!wm_rotate) return -1;
387 wm_rotate->set_rotation_cb(rot.callback, rot.cbdata);
388 appcore_unset_rotation_cb();
390 rot.wm_rotate = wm_rotate;
391 _DBG("[APP %d] Support wm rotate:%p", getpid(), wm_rotate);