Merge Tizen 2.0 dev to RSA
[framework/system/power-manager.git] / pm_setting.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 <stdio.h>
19 #include <stdlib.h>
20 #include "pm_setting.h"
21 #include "pm_conf.h"
22 #include "util.h"
23
24 static const char *setting_keys[SETTING_GET_END] = {
25         [SETTING_TO_NORMAL] = VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL,
26         [SETTING_LOW_BATT] = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
27         [SETTING_CHARGING] = VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
28         [SETTING_BRT_LEVEL] = VCONFKEY_SETAPPL_LCD_BRIGHTNESS,
29         [SETTING_LOCK_SCREEN] = VCONFKEY_IDLE_LOCK_STATE,
30         [SETTING_POWER_SAVING] = VCONFKEY_SETAPPL_PWRSV_SYSMODE_STATUS,
31         [SETTING_POWER_SAVING_DISPLAY] = VCONFKEY_SETAPPL_PWRSV_CUSTMODE_DISPLAY,
32 };
33
34 static int (*update_setting) (int key_idx, int val);
35
36 int get_charging_status(int *val)
37 {
38         return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, val);
39 }
40
41 int get_lowbatt_status(int *val)
42 {
43         return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, val);
44 }
45
46 int get_usb_status(int *val)
47 {
48         return vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, val);
49 }
50
51 int set_setting_pmstate(int val)
52 {
53         return vconf_set_int(VCONFKEY_PM_STATE, val);
54 }
55
56 int get_setting_brightness(int *level)
57 {
58         return vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, level);
59 }
60
61 int get_run_timeout(int *timeout)
62 {
63         int dim_timeout = -1, vconf_timeout = -1, ret;
64         get_dim_timeout(&dim_timeout);
65
66         if(dim_timeout < 0) {
67                 LOGERR("Can not get dim timeout. set default 5 seconds");
68                 dim_timeout = 5;
69         }
70
71         ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
72
73         if(vconf_timeout == 0)
74                 *timeout = 0; //timeout 0 : Always ON (Do not apply dim_timeout)
75         else
76                 *timeout = vconf_timeout - dim_timeout;
77         return ret;
78
79 }
80
81 int get_dim_timeout(int *timeout)
82 {
83         char buf[255];
84         /* TODO if needed */
85         *timeout = 5;           /* default timeout */
86         get_env("PM_TO_LCDDIM", buf, sizeof(buf));
87         LOGINFO("Get lcddim timeout [%s]", buf);
88         *timeout = atoi(buf);
89         return 0;
90 }
91
92 int get_off_timeout(int *timeout)
93 {
94         char buf[255];
95         /* TODO if needed */
96         *timeout = 5;           /* default timeout */
97         get_env("PM_TO_LCDOFF", buf, sizeof(buf));
98         LOGINFO("Get lcdoff timeout [%s]", buf);
99         *timeout = atoi(buf);
100         return 0;
101 }
102
103 static int setting_cb(keynode_t *key_nodes, void *data)
104 {
105         keynode_t *tmp = key_nodes;
106
107         if ((int)data > SETTING_END) {
108                 LOGERR("Unknown setting key: %s, idx= %d",
109                        vconf_keynode_get_name, (int)data);
110                 return -1;
111         }
112         if (update_setting != NULL) {
113                 if ((int)data >= SETTING_POWER_SAVING)
114                         update_setting((int)data, vconf_keynode_get_bool(tmp));
115                 else
116                         update_setting((int)data, vconf_keynode_get_int(tmp));
117         }
118
119         return 0;
120 }
121
122 int init_setting(int (*func) (int key_idx, int val))
123 {
124         int i;
125
126         if (func != NULL)
127                 update_setting = func;
128
129         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
130                 vconf_notify_key_changed(setting_keys[i], (void *)setting_cb,
131                                          (void *)i);
132         }
133
134         return 0;
135 }
136
137 int exit_setting()
138 {
139         int i;
140         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
141                 vconf_ignore_key_changed(setting_keys[i], (void *)setting_cb);
142         }
143
144         return 0;
145 }