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.
33 #include "appcore-internal.h"
35 #define _MAKE_ATOM(a, s) \
37 a = ecore_x_atom_get(s); \
39 _ERR("##s creation failed.\n"); \
42 #define STR_ATOM_ROTATION_LOCK "_E_ROTATION_LOCK"
44 static Ecore_X_Atom ATOM_ROTATION_LOCK = 0;
45 static Ecore_X_Window root;
49 int (*callback) (enum appcore_rm, void *);
56 static struct rot_s rot;
59 enum accelerometer_rotate_state re;
63 static struct rot_evt re_to_rm[] = {
66 APPCORE_RM_PORTRAIT_NORMAL,
70 APPCORE_RM_LANDSCAPE_NORMAL,
74 APPCORE_RM_PORTRAIT_REVERSE,
78 APPCORE_RM_LANDSCAPE_REVERSE,
82 static enum appcore_rm __get_mode(int event_data)
87 m = APPCORE_RM_UNKNOWN;
89 for (i = 0; i < sizeof(re_to_rm) / sizeof(re_to_rm[0]); i++) {
90 if (re_to_rm[i].re == event_data) {
99 static void __changed_cb(unsigned int event_type, sensor_event_data_t *event,
109 if (event_type != ACCELEROMETER_EVENT_ROTATION_CHECK) {
114 cb_event_data = (int *)(event->event_data);
116 m = __get_mode(*cb_event_data);
118 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
121 if (rot.cb_set && rot.mode != m) {
122 _DBG("[APP %d] Rotation: %d -> %d", getpid(), rot.mode, m);
123 rot.callback(m, data);
129 static void __lock_cb(keynode_t *node, void *data)
135 rot.lock = vconf_keynode_get_bool(node);
138 m = APPCORE_RM_PORTRAIT_NORMAL;
140 rot.callback(m, data);
143 _DBG("[APP %d] Rotation locked", getpid());
147 _DBG("[APP %d] Rotation unlocked", getpid());
150 r = appcore_get_rotation_state(&m);
151 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(),
153 if (!r && rot.mode != m) {
154 rot.callback(m, data);
161 static void __add_rotlock(void *data)
167 r = vconf_get_bool(VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL, &lock);
169 _DBG("[APP %d] Rotation vconf get bool failed", getpid());
174 vconf_notify_key_changed(VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL, __lock_cb,
178 static void __del_rotlock(void)
180 vconf_ignore_key_changed(VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL, __lock_cb);
184 EXPORT_API int appcore_set_rotation_cb(int (*cb) (enum appcore_rm, void *),
195 if (rot.callback != NULL) {
200 handle = sf_connect(ACCELEROMETER_SENSOR);
202 _ERR("sf_connect failed: %d", handle);
206 r = sf_register_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK,
207 NULL, __changed_cb, data);
209 _ERR("sf_register_event failed: %d", r);
210 sf_disconnect(handle);
218 r = sf_start(handle, 0);
220 _ERR("sf_start failed: %d", r);
221 sf_unregister_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK);
226 sf_disconnect(handle);
234 _MAKE_ATOM(ATOM_ROTATION_LOCK, STR_ATOM_ROTATION_LOCK );
235 root = ecore_x_window_root_first_get();
236 XSelectInput(ecore_x_display_get(), root, PropertyChangeMask);
241 EXPORT_API int appcore_unset_rotation_cb(void)
245 _retv_if(rot.callback == NULL, 0);
250 r = sf_unregister_event(rot.handle,
251 ACCELEROMETER_EVENT_ROTATION_CHECK);
253 _ERR("sf_unregister_event failed: %d", r);
261 if (rot.sf_started == 1) {
262 r = sf_stop(rot.handle);
264 _ERR("sf_stop failed: %d", r);
270 r = sf_disconnect(rot.handle);
272 _ERR("sf_disconnect failed: %d", r);
280 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
290 r = sf_check_rotation(&event);
292 _ERR("sf_check_rotation failed: %d", r);
293 *curr = APPCORE_RM_UNKNOWN;
297 *curr = __get_mode(event);
302 EXPORT_API int appcore_pause_rotation_cb(void)
306 _retv_if(rot.callback == NULL, 0);
307 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
312 r = sf_unregister_event(rot.handle,
313 ACCELEROMETER_EVENT_ROTATION_CHECK);
315 _ERR("sf_unregister_event in appcore_internal_sf_stop failed: %d", r);
321 if (rot.sf_started == 1) {
322 r = sf_stop(rot.handle);
324 _ERR("sf_stop in appcore_internal_sf_stop failed: %d",
334 EXPORT_API int appcore_resume_rotation_cb(void)
339 _retv_if(rot.callback == NULL, 0);
340 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
342 if (rot.cb_set == 0) {
343 r = sf_register_event(rot.handle,
344 ACCELEROMETER_EVENT_ROTATION_CHECK, NULL,
345 __changed_cb, rot.cbdata);
347 _ERR("sf_register_event in appcore_internal_sf_start failed: %d", r);
353 if (rot.sf_started == 0) {
354 r = sf_start(rot.handle, 0);
356 _ERR("sf_start in appcore_internal_sf_start failed: %d",
358 sf_unregister_event(rot.handle,
359 ACCELEROMETER_EVENT_ROTATION_CHECK);
366 __add_rotlock(rot.cbdata);
368 r = appcore_get_rotation_state(&m);
369 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
370 if (!r && rot.mode != m && rot.lock == 0) {
371 rot.callback(m, rot.cbdata);