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.
30 #include "appcore-internal.h"
38 /*Fixme: to be added for wayland works*/
39 #define _MAKE_ATOM(a, s) \
41 a = ecore_x_atom_get(s); \
43 _ERR("##s creation failed.\n"); \
46 #define STR_ATOM_ROTATION_LOCK "_E_ROTATION_LOCK"
48 static Ecore_X_Atom ATOM_ROTATION_LOCK = 0;
49 static Ecore_X_Window root;
54 int (*callback) (enum appcore_rm, void *);
61 struct ui_wm_rotate* wm_rotate;
63 static struct rot_s rot;
66 enum accelerometer_rotate_state re;
70 static struct rot_evt re_to_rm[] = {
73 APPCORE_RM_PORTRAIT_NORMAL,
77 APPCORE_RM_LANDSCAPE_NORMAL,
81 APPCORE_RM_PORTRAIT_REVERSE,
85 APPCORE_RM_LANDSCAPE_REVERSE,
89 static enum appcore_rm __get_mode(int event_data)
94 m = APPCORE_RM_UNKNOWN;
96 for (i = 0; i < sizeof(re_to_rm) / sizeof(re_to_rm[0]); i++) {
97 if (re_to_rm[i].re == event_data) {
106 static void __changed_cb(unsigned int event_type, sensor_event_data_t *event,
116 if (event_type != ACCELEROMETER_EVENT_ROTATION_CHECK) {
121 cb_event_data = (int *)(event->event_data);
123 m = __get_mode(*cb_event_data);
125 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
128 if (rot.cb_set && rot.mode != m) {
129 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
130 rot.callback(m, data);
136 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(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(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());
181 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb,
185 static void __del_rotlock(void)
187 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
191 EXPORT_API int appcore_set_rotation_cb(int (*cb) (enum appcore_rm, void *),
195 return rot.wm_rotate->set_rotation_cb(cb, data);
206 if (rot.callback != NULL) {
211 handle = sf_connect(ACCELEROMETER_SENSOR);
213 _ERR("sf_connect failed: %d", handle);
217 r = sf_register_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK,
218 NULL, __changed_cb, data);
220 _ERR("sf_register_event failed: %d", r);
221 sf_disconnect(handle);
229 r = sf_start(handle, 0);
231 _ERR("sf_start failed: %d", r);
232 r = sf_unregister_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK);
234 _ERR("sf_unregister_event failed: %d", r);
240 sf_disconnect(handle);
249 _MAKE_ATOM(ATOM_ROTATION_LOCK, STR_ATOM_ROTATION_LOCK );
250 root = ecore_x_window_root_first_get();
251 XSelectInput(ecore_x_display_get(), root, PropertyChangeMask);
257 EXPORT_API int appcore_unset_rotation_cb(void)
260 return rot.wm_rotate->unset_rotation_cb();
265 _retv_if(rot.callback == NULL, 0);
270 r = sf_unregister_event(rot.handle,
271 ACCELEROMETER_EVENT_ROTATION_CHECK);
273 _ERR("sf_unregister_event failed: %d", r);
281 if (rot.sf_started == 1) {
282 r = sf_stop(rot.handle);
284 _ERR("sf_stop failed: %d", r);
290 r = sf_disconnect(rot.handle);
292 _ERR("sf_disconnect failed: %d", r);
300 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
303 return rot.wm_rotate->get_rotation_state(curr);
314 r = sf_check_rotation(&event);
316 _ERR("sf_check_rotation failed: %d", r);
317 *curr = APPCORE_RM_UNKNOWN;
321 *curr = __get_mode(event);
326 EXPORT_API int appcore_pause_rotation_cb(void)
329 return rot.wm_rotate->pause_rotation_cb();
334 _retv_if(rot.callback == NULL, 0);
335 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
340 r = sf_unregister_event(rot.handle,
341 ACCELEROMETER_EVENT_ROTATION_CHECK);
343 _ERR("sf_unregister_event in appcore_internal_sf_stop failed: %d", r);
349 if (rot.sf_started == 1) {
350 r = sf_stop(rot.handle);
352 _ERR("sf_stop in appcore_internal_sf_stop failed: %d",
363 EXPORT_API int appcore_resume_rotation_cb(void)
366 return rot.wm_rotate->resume_rotation_cb();
372 _retv_if(rot.callback == NULL, 0);
373 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
375 if (rot.cb_set == 0) {
376 r = sf_register_event(rot.handle,
377 ACCELEROMETER_EVENT_ROTATION_CHECK, NULL,
378 __changed_cb, rot.cbdata);
380 _ERR("sf_register_event in appcore_internal_sf_start failed: %d", r);
386 if (rot.sf_started == 0) {
387 r = sf_start(rot.handle, 0);
389 _ERR("sf_start in appcore_internal_sf_start failed: %d",
391 ret = sf_unregister_event(rot.handle,
392 ACCELEROMETER_EVENT_ROTATION_CHECK);
394 _ERR("sf_unregister_event failed: %d", ret);
401 __add_rotlock(rot.cbdata);
403 r = appcore_get_rotation_state(&m);
404 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
405 if (!r && rot.mode != m && rot.lock == 0) {
406 rot.callback(m, rot.cbdata);
413 EXPORT_API int appcore_set_wm_rotation(struct ui_wm_rotate* wm_rotate)
415 if (!wm_rotate) return -1;
418 wm_rotate->set_rotation_cb(rot.callback, rot.cbdata);
419 appcore_unset_rotation_cb();
421 rot.wm_rotate = wm_rotate;
422 _DBG("[APP %d] Support wm rotate:%p", getpid(), wm_rotate);