2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
27 #define LOG_TAG "TIZEN_SYSTEM_POWER"
29 #define _MSG_POWER_ERROR_IO_ERROR "I/O error"
30 #define _MSG_POWER_ERROR_INVALID_PARAMETER "Invalid parameter"
32 #define RETURN_ERR(err_code) \
34 LOGE("[%s] "_MSG_##err_code"(0x%08x)", __FUNCTION__, err_code); \
39 * Enum translations to the old API
41 int POWER_OLD_API_STATE[] =
43 LCD_NORMAL, /* POWER_STATE_NORMAL */
44 LCD_DIM, /* POWER_STATE_SCREEN_DIM */
45 LCD_OFF, /* POWER_STATE_SCREEN_OFF */
48 int power_lock_state(power_state_e power_state, int timeout)
50 if ( power_state > POWER_STATE_SCREEN_OFF || power_state < 0 || timeout < 0) {
51 RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
53 if(pm_lock_state(POWER_OLD_API_STATE[power_state], STAY_CUR_STATE, timeout) == 0)
54 return POWER_ERROR_NONE;
56 RETURN_ERR(POWER_ERROR_IO_ERROR);
59 int power_unlock_state(power_state_e power_state)
61 if ( power_state > POWER_STATE_SCREEN_OFF || power_state < 0 ) {
62 RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
64 if (pm_unlock_state(POWER_OLD_API_STATE[power_state], PM_SLEEP_MARGIN) == 0)
65 return POWER_ERROR_NONE;
67 RETURN_ERR(POWER_ERROR_IO_ERROR);
72 if(pm_change_state(LCD_NORMAL) < 0)
73 RETURN_ERR(POWER_ERROR_IO_ERROR);
75 return POWER_ERROR_NONE;
78 power_state_e power_get_state(void)
82 err = vconf_get_int(VCONFKEY_PM_STATE, &value);
84 RETURN_ERR(POWER_ERROR_IO_ERROR);
88 return POWER_STATE_NORMAL;
90 return POWER_STATE_SCREEN_DIM;
92 return POWER_STATE_SCREEN_OFF;
95 return POWER_ERROR_IO_ERROR;
99 static power_changed_cb changed_callback = NULL;
100 static void* changed_callback_user_data = NULL;
102 static void power_changed_inside_cb(keynode_t* key, void* user_data)
104 char* keyname = vconf_keynode_get_name(key);
106 if(keyname != NULL && changed_callback != NULL && strcmp(keyname, VCONFKEY_PM_STATE) == 0){
107 power_state_e state = power_get_state();
108 changed_callback(state, changed_callback_user_data);
112 int power_set_changed_cb(power_changed_cb callback, void* user_data)
117 RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
119 changed_callback = callback;
120 changed_callback_user_data = user_data;
122 err = vconf_notify_key_changed(VCONFKEY_PM_STATE, power_changed_inside_cb, NULL);
124 RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
127 return POWER_ERROR_NONE;
130 int power_unset_changed_cb(void)
132 int err = vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_changed_inside_cb);
134 RETURN_ERR(POWER_ERROR_IO_ERROR);
136 changed_callback = NULL;
137 changed_callback_user_data = NULL;
139 return POWER_ERROR_NONE;