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"
36 /* Fixme: to be added for wayland works */
37 #define _MAKE_ATOM(a, s) \
39 a = ecore_x_atom_get(s); \
41 _ERR("##s creation failed.\n"); \
44 #define STR_ATOM_ROTATION_LOCK "_E_ROTATION_LOCK"
46 static Ecore_X_Atom ATOM_ROTATION_LOCK = 0;
47 static Ecore_X_Window root;
52 int (*callback) (void *event_info, enum appcore_rm, void *);
59 struct ui_wm_rotate* wm_rotate;
61 static struct rot_s rot;
64 enum auto_rotation_state re;
68 static struct rot_evt re_to_rm[] = {
70 AUTO_ROTATION_DEGREE_0,
71 APPCORE_RM_PORTRAIT_NORMAL,
74 AUTO_ROTATION_DEGREE_90,
75 APPCORE_RM_LANDSCAPE_NORMAL,
78 AUTO_ROTATION_DEGREE_180,
79 APPCORE_RM_PORTRAIT_REVERSE,
82 AUTO_ROTATION_DEGREE_270,
83 APPCORE_RM_LANDSCAPE_REVERSE,
87 static enum appcore_rm __get_mode(sensor_data_t data)
93 m = APPCORE_RM_UNKNOWN;
94 if (data.value_count > 0) {
95 event = (int)data.values[0];
97 _ERR("Failed to get sensor data");
101 for (i = 0; i < sizeof(re_to_rm) / sizeof(re_to_rm[0]); i++) {
102 if (re_to_rm[i].re == event) {
111 static void __changed_cb(sensor_t sensor, unsigned int event_type,
112 sensor_data_t *data, void *user_data)
119 if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT) {
124 m = __get_mode(*data);
126 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
129 if (rot.cb_set && rot.mode != m) {
130 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
131 rot.callback((void *)&m, m, data);
137 static void __lock_cb(keynode_t *node, void *data)
142 rot.lock = !vconf_keynode_get_bool(node);
145 m = APPCORE_RM_PORTRAIT_NORMAL;
147 rot.callback((void *)&m, m, data);
150 _DBG("[APP %d] Rotation locked", getpid());
154 _DBG("[APP %d] Rotation unlocked", getpid());
157 r = appcore_get_rotation_state(&m);
158 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(),
160 if (!r && rot.mode != m) {
161 rot.callback((void *)&m, m, data);
168 static void __add_rotlock(void *data)
174 r = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
176 _DBG("[APP %d] Rotation vconf get bool failed", getpid());
180 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb,
184 static void __del_rotlock(void)
186 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
190 EXPORT_API int appcore_set_rotation_cb(int (*cb) (void *evnet_info, enum appcore_rm, void *),
194 return rot.wm_rotate->set_rotation_cb(cb, data);
198 sensor_t sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
205 if (rot.callback != NULL) {
210 handle = sensord_connect(sensor);
212 _ERR("sensord_connect failed: %d", handle);
216 r = sensord_register_event(handle, AUTO_ROTATION_CHANGE_STATE_EVENT,
217 SENSOR_INTERVAL_NORMAL, 0, __changed_cb, data);
219 _ERR("sensord_register_event failed");
220 sensord_disconnect(handle);
228 r = sensord_start(handle, 0);
230 _ERR("sensord_start failed");
231 r = sensord_unregister_event(handle, AUTO_ROTATION_CHANGE_STATE_EVENT);
233 _ERR("sensord_unregister_event failed");
238 rot.sensord_started = 0;
239 sensord_disconnect(handle);
242 rot.sensord_started = 1;
248 _MAKE_ATOM(ATOM_ROTATION_LOCK, STR_ATOM_ROTATION_LOCK);
249 root = ecore_x_window_root_first_get();
250 XSelectInput(ecore_x_display_get(), root, PropertyChangeMask);
256 EXPORT_API int appcore_unset_rotation_cb(void)
259 return rot.wm_rotate->unset_rotation_cb();
263 _retv_if(rot.callback == NULL, 0);
268 r = sensord_unregister_event(rot.handle,
269 AUTO_ROTATION_CHANGE_STATE_EVENT);
271 _ERR("sensord_unregister_event failed");
279 if (rot.sensord_started == 1) {
280 r = sensord_stop(rot.handle);
282 _ERR("sensord_stop failed");
285 rot.sensord_started = 0;
288 r = sensord_disconnect(rot.handle);
290 _ERR("sensord_disconnect failed");
298 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
301 return rot.wm_rotate->get_rotation_state(curr);
311 r = sensord_get_data(rot.handle, AUTO_ROTATION_SENSOR, &data);
313 _ERR("sensord_get_data failed");
314 *curr = APPCORE_RM_UNKNOWN;
318 *curr = __get_mode(data);
323 EXPORT_API int appcore_pause_rotation_cb(void)
326 return rot.wm_rotate->pause_rotation_cb();
330 _retv_if(rot.callback == NULL, 0);
331 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
336 r = sensord_unregister_event(rot.handle,
337 AUTO_ROTATION_CHANGE_STATE_EVENT);
339 _ERR("sensord_unregister_event failed");
345 if (rot.sensord_started == 1) {
346 r = sensord_stop(rot.handle);
348 _ERR("sensord_stop failed");
351 rot.sensord_started = 0;
358 EXPORT_API int appcore_resume_rotation_cb(void)
361 return rot.wm_rotate->resume_rotation_cb();
366 _retv_if(rot.callback == NULL, 0);
367 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
369 if (rot.cb_set == 0) {
370 r = sensord_register_event(rot.handle,
371 AUTO_ROTATION_CHANGE_STATE_EVENT,
372 SENSOR_INTERVAL_NORMAL, 0, __changed_cb, rot.cbdata);
374 _ERR("sensord_register_event failed");
380 if (rot.sensord_started == 0) {
381 r = sensord_start(rot.handle, 0);
383 _ERR("sensord_start failed");
384 r = sensord_unregister_event(rot.handle,
385 AUTO_ROTATION_CHANGE_STATE_EVENT);
387 _ERR("sensord_unregister_event failed");
391 rot.sensord_started = 1;
394 __add_rotlock(rot.cbdata);
396 r = appcore_get_rotation_state(&m);
397 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
398 if (!r && rot.mode != m && rot.lock == 0) {
399 rot.callback((void *)&m, m, rot.cbdata);
406 EXPORT_API int appcore_set_wm_rotation(struct ui_wm_rotate* wm_rotate)
408 if (!wm_rotate) return -1;
411 wm_rotate->set_rotation_cb(rot.callback, rot.cbdata);
412 appcore_unset_rotation_cb();
414 rot.wm_rotate = wm_rotate;
415 _DBG("[APP %d] Support wm rotate:%p", getpid(), wm_rotate);