+static enum appcore_base_rm __get_rm(sensor_data_t data)
+{
+ int event;
+ enum appcore_base_rm rm;
+
+ if (data.value_count <= 0) {
+ _ERR("Failed to get sensor data");
+ return APPCORE_BASE_RM_UNKNOWN;
+ }
+
+ event = data.values[0];
+ switch (event) {
+ case AUTO_ROTATION_DEGREE_0:
+ rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
+ break;
+ case AUTO_ROTATION_DEGREE_90:
+ rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
+ break;
+ case AUTO_ROTATION_DEGREE_180:
+ rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
+ break;
+ case AUTO_ROTATION_DEGREE_270:
+ rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
+ break;
+ default:
+ rm = APPCORE_BASE_RM_UNKNOWN;
+ break;
+ }
+
+ return rm;
+}
+
+static void __lock_cb(keynode_t *node, void *user_data)
+{
+ bool r;
+ sensor_data_t data;
+ enum appcore_base_rm rm;
+
+ __rotation.lock = !vconf_keynode_get_bool(node);
+ if (__rotation.lock) {
+ _DBG("Rotation locked");
+ rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
+ } else {
+ _DBG("Rotation unlocked");
+ r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
+ if (!r) {
+ _ERR("Failed to get sensor data");
+ return;
+ }
+
+ rm = __get_rm(data);
+ if (rm == APPCORE_BASE_RM_UNKNOWN) {
+ _ERR("Unknown mode");
+ return;
+ }
+ }
+
+ if (__rotation.rm == rm)
+ return;
+
+ _DBG("Rotation: %d -> %d", __rotation.rm, rm);
+ __rotation.rm = rm;
+ __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
+}
+
+static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
+ sensor_data_t *data, void *user_data)
+{
+ enum appcore_base_rm rm;
+
+ if (data == NULL)
+ return;
+
+ if (__rotation.lock)
+ return;
+
+ if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
+ return;
+
+ rm = __get_rm(*data);
+ if (rm == APPCORE_BASE_RM_UNKNOWN) {
+ _ERR("Unknown mode");
+ return;
+ }
+
+ _DBG("Rotation: %d -> %d", __rotation.rm, rm);
+ __rotation.rm = rm;
+ __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
+}
+
+static void __fini_rotation(void)
+{
+ if (!__rotation.initialized)
+ return;
+
+ vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
+ sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
+ sensord_stop(__rotation.conn);
+ sensord_disconnect(__rotation.conn);
+
+ __rotation.lock = 0;
+ __rotation.initialized = false;
+}
+
+static void __init_rotation(void)
+{
+ sensor_t sensor;
+ int lock;
+ bool r;
+
+ if (__rotation.initialized)
+ return;
+
+ sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
+ __rotation.conn = sensord_connect(sensor);
+ if (__rotation.conn < 0) {
+ _ERR("Failed to connect sensord");
+ return;
+ }
+
+ r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
+ SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
+ if (!r) {
+ _ERR("Failed to register auto rotation change event");
+ sensord_disconnect(__rotation.conn);
+ return;
+ }
+
+ r = sensord_start(__rotation.conn, 0);
+ if (!r) {
+ _ERR("Failed to start sensord");
+ sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
+ sensord_disconnect(__rotation.conn);
+ return;
+ }
+
+ lock = 0;
+ vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
+ vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
+
+ __rotation.lock = !lock;
+ __rotation.initialized = true;
+}
+
+static void __charger_status_changed_cb(keynode_t *keynode, void *user_data)
+{
+ if (TIZEN_FEATURE_CHARGER_STATUS) {
+ __rotation.charger_status = vconf_keynode_get_int(keynode);
+ if (__rotation.charger_status) {
+ if (__rotation.ref)
+ __init_rotation();
+ } else {
+ if (__rotation.ref)
+ __fini_rotation();
+ }
+ _DBG("charger status(%d)", __rotation.charger_status);
+ }
+}
+
+static void __unregister_rotation_changed_event(void)
+{
+ if (!__rotation.ref)
+ return;
+
+ __rotation.ref--;
+ if (__rotation.ref > 1)
+ return;
+
+ __fini_rotation();
+ if (TIZEN_FEATURE_CHARGER_STATUS) {
+ vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
+ __charger_status_changed_cb);
+ }
+
+ __rotation.ref = 0;
+}
+
+static void __register_rotation_changed_event(void)
+{
+ if (__rotation.ref) {
+ __rotation.ref++;
+ return;
+ }
+
+ if (TIZEN_FEATURE_CHARGER_STATUS) {
+ vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS,
+ &__rotation.charger_status);
+ vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
+ __charger_status_changed_cb, NULL);
+ if (__rotation.charger_status)
+ __init_rotation();
+ } else {
+ __init_rotation();
+ }
+
+ __rotation.ref++;
+}
+