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 accelerometer_rotate_state re;
68 static struct rot_evt re_to_rm[] = {
71 APPCORE_RM_PORTRAIT_NORMAL,
75 APPCORE_RM_LANDSCAPE_NORMAL,
79 APPCORE_RM_PORTRAIT_REVERSE,
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,
113 if (event_type != ACCELEROMETER_EVENT_ROTATION_CHECK) {
118 cb_event_data = (int *)(event->event_data);
120 m = __get_mode(*cb_event_data);
122 _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);
133 static void __lock_cb(keynode_t *node, void *data)
138 rot.lock = !vconf_keynode_get_bool(node);
141 m = APPCORE_RM_PORTRAIT_NORMAL;
143 rot.callback((void *)&m, m, data);
146 _DBG("[APP %d] Rotation locked", getpid());
150 _DBG("[APP %d] Rotation unlocked", getpid());
153 r = appcore_get_rotation_state(&m);
154 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(),
156 if (!r && rot.mode != m) {
157 rot.callback((void *)&m, m, data);
164 static void __add_rotlock(void *data)
170 r = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
172 _DBG("[APP %d] Rotation vconf get bool failed", getpid());
176 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb,
180 static void __del_rotlock(void)
182 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
186 EXPORT_API int appcore_set_rotation_cb(int (*cb) (void *evnet_info, enum appcore_rm, void *),
190 return rot.wm_rotate->set_rotation_cb(cb, data);
200 if (rot.callback != NULL) {
205 handle = sf_connect(ACCELEROMETER_SENSOR);
207 _ERR("sf_connect failed: %d", handle);
211 r = sf_register_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK,
212 NULL, __changed_cb, data);
214 _ERR("sf_register_event failed: %d", r);
215 sf_disconnect(handle);
223 r = sf_start(handle, 0);
225 _ERR("sf_start failed: %d", r);
226 r = sf_unregister_event(handle, ACCELEROMETER_EVENT_ROTATION_CHECK);
228 _ERR("sf_unregister_event failed: %d", r);
234 sf_disconnect(handle);
243 _MAKE_ATOM(ATOM_ROTATION_LOCK, STR_ATOM_ROTATION_LOCK);
244 root = ecore_x_window_root_first_get();
245 XSelectInput(ecore_x_display_get(), root, PropertyChangeMask);
251 EXPORT_API int appcore_unset_rotation_cb(void)
254 return rot.wm_rotate->unset_rotation_cb();
258 _retv_if(rot.callback == NULL, 0);
263 r = sf_unregister_event(rot.handle,
264 ACCELEROMETER_EVENT_ROTATION_CHECK);
266 _ERR("sf_unregister_event failed: %d", r);
274 if (rot.sf_started == 1) {
275 r = sf_stop(rot.handle);
277 _ERR("sf_stop failed: %d", r);
283 r = sf_disconnect(rot.handle);
285 _ERR("sf_disconnect failed: %d", r);
293 EXPORT_API int appcore_get_rotation_state(enum appcore_rm *curr)
296 return rot.wm_rotate->get_rotation_state(curr);
306 r = sf_check_rotation(&event);
308 _ERR("sf_check_rotation failed: %d", r);
309 *curr = APPCORE_RM_UNKNOWN;
313 *curr = __get_mode(event);
318 EXPORT_API int appcore_pause_rotation_cb(void)
321 return rot.wm_rotate->pause_rotation_cb();
325 _retv_if(rot.callback == NULL, 0);
326 _DBG("[APP %d] appcore_pause_rotation_cb is called", getpid());
331 r = sf_unregister_event(rot.handle,
332 ACCELEROMETER_EVENT_ROTATION_CHECK);
334 _ERR("sf_unregister_event in appcore_internal_sf_stop failed: %d", r);
340 if (rot.sf_started == 1) {
341 r = sf_stop(rot.handle);
343 _ERR("sf_stop in appcore_internal_sf_stop failed: %d",
354 EXPORT_API int appcore_resume_rotation_cb(void)
357 return rot.wm_rotate->resume_rotation_cb();
363 _retv_if(rot.callback == NULL, 0);
364 _DBG("[APP %d] appcore_resume_rotation_cb is called", getpid());
366 if (rot.cb_set == 0) {
367 r = sf_register_event(rot.handle,
368 ACCELEROMETER_EVENT_ROTATION_CHECK, NULL,
369 __changed_cb, rot.cbdata);
371 _ERR("sf_register_event in appcore_internal_sf_start failed: %d", r);
377 if (rot.sf_started == 0) {
378 r = sf_start(rot.handle, 0);
380 _ERR("sf_start in appcore_internal_sf_start failed: %d",
382 ret = sf_unregister_event(rot.handle,
383 ACCELEROMETER_EVENT_ROTATION_CHECK);
385 _ERR("sf_unregister_event failed: %d", ret);
392 __add_rotlock(rot.cbdata);
394 r = appcore_get_rotation_state(&m);
395 _DBG("[APP %d] Rotmode prev %d -> curr %d", getpid(), rot.mode, m);
396 if (!r && rot.mode != m && rot.lock == 0) {
397 rot.callback((void *)&m, m, rot.cbdata);
404 EXPORT_API int appcore_set_wm_rotation(struct ui_wm_rotate* wm_rotate)
406 if (!wm_rotate) return -1;
409 wm_rotate->set_rotation_cb(rot.callback, rot.cbdata);
410 appcore_unset_rotation_cb();
412 rot.wm_rotate = wm_rotate;
413 _DBG("[APP %d] Support wm rotate:%p", getpid(), wm_rotate);