Git init
[framework/api/power.git] / src / power.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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. 
15  */
16
17
18 #include <string.h>
19 #include <stdbool.h>
20 #include <dlog.h>
21 #include <pmapi.h>
22
23 #include <power.h>
24 #include <vconf.h>
25
26 #undef LOG_TAG
27 #define LOG_TAG "TIZEN_SYSTEM_POWER"
28
29 #define _MSG_POWER_ERROR_IO_ERROR "I/O error"
30 #define _MSG_POWER_ERROR_INVALID_PARAMETER "Invalid parameter"
31
32 #define RETURN_ERR(err_code) \
33     do { \
34         LOGE("[%s] "_MSG_##err_code"(0x%08x)", __FUNCTION__, err_code); \
35         return err_code; \
36     }while(0)
37
38 /*
39  * Enum translations to the old API
40  */
41 int POWER_OLD_API_STATE[] =
42 {
43     LCD_NORMAL,  /* POWER_STATE_NORMAL     */
44     LCD_DIM,     /* POWER_STATE_SCREEN_DIM */
45     LCD_OFF,     /* POWER_STATE_SCREEN_OFF */
46 };
47
48 int power_lock_state(power_state_e power_state, int timeout)
49 {
50     if ( power_state > POWER_STATE_SCREEN_OFF || power_state < 0 || timeout < 0) {
51         RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
52     }
53     if(pm_lock_state(POWER_OLD_API_STATE[power_state], STAY_CUR_STATE, timeout) == 0)
54         return POWER_ERROR_NONE;
55     else
56         RETURN_ERR(POWER_ERROR_IO_ERROR);
57 }
58
59 int power_unlock_state(power_state_e power_state)
60 {
61     if ( power_state > POWER_STATE_SCREEN_OFF || power_state < 0 ) {
62         RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
63     }
64     if (pm_unlock_state(POWER_OLD_API_STATE[power_state], PM_SLEEP_MARGIN) == 0)
65         return POWER_ERROR_NONE;
66     else
67         RETURN_ERR(POWER_ERROR_IO_ERROR);
68 }
69
70 int power_wakeup()
71 {
72     if(pm_change_state(LCD_NORMAL) < 0)
73         RETURN_ERR(POWER_ERROR_IO_ERROR);
74     else
75         return POWER_ERROR_NONE;
76 }
77
78 power_state_e power_get_state(void)
79 {
80     // VCONFKEY_PM_STATE
81     int value, err;
82     err = vconf_get_int(VCONFKEY_PM_STATE, &value);
83     if(err < 0){
84         RETURN_ERR(POWER_ERROR_IO_ERROR);
85     }
86     switch(value){
87         case 1:
88             return POWER_STATE_NORMAL;
89         case 2:
90             return POWER_STATE_SCREEN_DIM;
91         case 3:
92             return POWER_STATE_SCREEN_OFF;
93
94         default:
95             return POWER_ERROR_IO_ERROR;
96     }
97 }
98
99 static power_changed_cb changed_callback = NULL;
100 static void* changed_callback_user_data = NULL;
101
102 static void power_changed_inside_cb(keynode_t* key, void* user_data)
103 {
104     char* keyname = vconf_keynode_get_name(key);
105
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);
109     }
110 }
111
112 int power_set_changed_cb(power_changed_cb callback, void* user_data)
113 {
114     int err;
115
116     if(callback == NULL)
117         RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
118
119     changed_callback = callback;
120     changed_callback_user_data = user_data;
121
122     err = vconf_notify_key_changed(VCONFKEY_PM_STATE, power_changed_inside_cb, NULL);
123     if(err < 0){
124         RETURN_ERR(POWER_ERROR_INVALID_PARAMETER);
125     }
126
127     return POWER_ERROR_NONE;
128 }
129
130 int power_unset_changed_cb(void)
131 {
132     int err = vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_changed_inside_cb);
133     if(err < 0){
134         RETURN_ERR(POWER_ERROR_IO_ERROR);
135     }
136     changed_callback = NULL;
137     changed_callback_user_data = NULL;
138
139     return POWER_ERROR_NONE;
140 }