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_deprecated.h>
28 //#include <sensor_auto_rotation.h>
35 #include "appcore-internal.h"
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"
45 #define MAX_LOCAL_BUFSZ 128
47 static Ecore_X_Atom ATOM_ROTATION_LOCK = 0;
48 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;
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(int event_data)
92 m = APPCORE_RM_UNKNOWN;
94 for (i = 0; i < sizeof(re_to_rm) / sizeof(re_to_rm[0]); i++) {
95 if (re_to_rm[i].re == event_data) {
104 static void __changed_cb(unsigned int event_type, sensor_event_data_t *event,
114 if (event_type != AUTO_ROTATION_EVENT_CHANGE_STATE) {
119 cb_event_data = (int *)(event->event_data);
121 m = __get_mode(*cb_event_data);
123 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
125 if (rot.cb_set && rot.mode != m) {
126 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
127 rot.callback((void *)&m, m, data);
134 static void __lock_cb(keynode_t *node, void *data)
139 rot.lock = !vconf_keynode_get_bool(node);
142 m = APPCORE_RM_PORTRAIT_NORMAL;
144 rot.callback((void *)&m, m, data);
147 _DBG("[APP %d] Rotation locked", getpid());
151 _DBG("[APP %d] Rotation unlocked", getpid());
154 r = appcore_get_rotation_state(&m);
155 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(),
157 if (!r && rot.mode != m) {
158 rot.callback((void *)&m, m, data);
165 static void __add_rotlock(void *data)
171 r = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
173 _DBG("[APP %d] Rotation vconf get bool failed", getpid());
178 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb,
182 static void __del_rotlock(void)
184 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
188 EXPORT_API int appcore_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *),
192 return rot.wm_rotate->set_rotation_cb(cb, data);
202 if (rot.callback != NULL) {
207 handle = sf_connect(AUTO_ROTATION_SENSOR);
209 _ERR("sf_connect failed: %d", handle);
213 r = sf_register_event(handle, AUTO_ROTATION_EVENT_CHANGE_STATE,
214 NULL, __changed_cb, data);
216 _ERR("sf_register_event failed: %d", r);
217 sf_disconnect(handle);
225 r = sf_start(handle, 0);
227 _ERR("sf_start failed: %d", r);
228 r = sf_unregister_event(handle, AUTO_ROTATION_EVENT_CHANGE_STATE);
230 _ERR("sf_unregister_event failed: %d", r);
236 sf_disconnect(handle);
244 _MAKE_ATOM(ATOM_ROTATION_LOCK, STR_ATOM_ROTATION_LOCK );
245 root = ecore_x_window_root_first_get();
246 XSelectInput(ecore_x_display_get(), root, PropertyChangeMask);
251 EXPORT_API int appcore_unset_rotation_cb(void)
254 return rot.wm_rotate->unset_rotation_cb();
259 _retv_if(rot.callback == NULL, 0);
264 r = sf_unregister_event(rot.handle,
265 AUTO_ROTATION_EVENT_CHANGE_STATE);
267 _ERR("sf_unregister_event failed: %d", r);
275 if (rot.sf_started == 1) {
276 r = sf_stop(rot.handle);
278 _ERR("sf_stop failed: %d", r);
284 r = sf_disconnect(rot.handle);
286 _ERR("sf_disconnect failed: %d", r);
294 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
297 return rot.wm_rotate->get_rotation_state(curr);
309 r = sf_get_data(rot.handle, AUTO_ROTATION_BASE_DATA_SET, &data);
310 if (r == 1 && data.value_count > 0) {
311 event = data.values[0];
313 _ERR("sf_get_data for AUTO_ROTATION_BASE_DATA_SET failed: %d", r);
314 *curr = APPCORE_RM_UNKNOWN;
318 *curr = __get_mode(event);
323 EXPORT_API int appcore_pause_rotation_cb(void)
326 return rot.wm_rotate->pause_rotation_cb();
331 _retv_if(rot.callback == NULL, 0);
332 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
337 r = sf_unregister_event(rot.handle,
338 AUTO_ROTATION_EVENT_CHANGE_STATE);
340 _ERR("sf_unregister_event in appcore_internal_sf_stop failed: %d", r);
346 if (rot.sf_started == 1) {
347 r = sf_stop(rot.handle);
349 _ERR("sf_stop in appcore_internal_sf_stop failed: %d",
360 EXPORT_API int appcore_resume_rotation_cb(void)
363 return rot.wm_rotate->resume_rotation_cb();
369 _retv_if(rot.callback == NULL, 0);
370 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
372 if (rot.cb_set == 0) {
373 r = sf_register_event(rot.handle,
374 AUTO_ROTATION_EVENT_CHANGE_STATE, NULL,
375 __changed_cb, rot.cbdata);
377 _ERR("sf_register_event in appcore_internal_sf_start failed: %d", r);
383 if (rot.sf_started == 0) {
384 r = sf_start(rot.handle, 0);
386 _ERR("sf_start in appcore_internal_sf_start failed: %d",
388 ret = sf_unregister_event(rot.handle,
389 AUTO_ROTATION_EVENT_CHANGE_STATE);
391 _ERR("sf_unregister_event failed: %d", ret);
398 __add_rotlock(rot.cbdata);
400 r = appcore_get_rotation_state(&m);
401 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
402 if (!r && rot.mode != m && rot.lock == 0) {
403 rot.callback((void *)&m, m, rot.cbdata);
410 EXPORT_API int appcore_set_wm_rotation(struct ui_wm_rotate* wm_rotate)
412 if (!wm_rotate) return -1;
415 wm_rotate->set_rotation_cb(rot.callback, rot.cbdata);
416 appcore_unset_rotation_cb();
418 rot.wm_rotate = wm_rotate;
419 _DBG("[APP %d] Support wm rotate:%p", getpid(), wm_rotate);